4 * Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 * Asks for confirmation before executing a command.
28 int cmd_confirm_before_exec(struct cmd
*, struct cmd_ctx
*);
29 void cmd_confirm_before_init(struct cmd
*, int);
31 int cmd_confirm_before_callback(void *, const char *);
32 void cmd_confirm_before_free(void *);
34 const struct cmd_entry cmd_confirm_before_entry
= {
35 "confirm-before", "confirm",
36 CMD_TARGET_CLIENT_USAGE
" command",
38 cmd_confirm_before_init
,
40 cmd_confirm_before_exec
,
45 struct cmd_confirm_before_data
{
51 cmd_confirm_before_init(struct cmd
*self
, int key
)
53 struct cmd_target_data
*data
;
55 cmd_target_init(self
, key
);
60 data
->arg
= xstrdup("kill-window");
63 data
->arg
= xstrdup("kill-pane");
69 cmd_confirm_before_exec(struct cmd
*self
, struct cmd_ctx
*ctx
)
71 struct cmd_target_data
*data
= self
->data
;
72 struct cmd_confirm_before_data
*cdata
;
74 char *buf
, *cmd
, *ptr
;
76 if (ctx
->curclient
== NULL
) {
77 ctx
->error(ctx
, "must be run interactively");
81 if ((c
= cmd_find_client(ctx
, data
->target
)) == NULL
)
84 ptr
= xstrdup(data
->arg
);
85 if ((cmd
= strtok(ptr
, " \t")) == NULL
)
87 xasprintf(&buf
, "Confirm '%s'? (y/n) ", cmd
);
90 cdata
= xmalloc(sizeof *cdata
);
91 cdata
->cmd
= xstrdup(data
->arg
);
93 status_prompt_set(cdata
->c
, buf
,
94 cmd_confirm_before_callback
, cmd_confirm_before_free
, cdata
,
102 cmd_confirm_before_callback(void *data
, const char *s
)
104 struct cmd_confirm_before_data
*cdata
= data
;
105 struct client
*c
= cdata
->c
;
106 struct cmd_list
*cmdlist
;
110 if (s
== NULL
|| *s
== '\0')
112 if (tolower((u_char
) s
[0]) != 'y' || s
[1] != '\0')
115 if (cmd_string_parse(cdata
->cmd
, &cmdlist
, &cause
) != 0) {
117 *cause
= toupper((u_char
) *cause
);
118 status_message_set(c
, "%s", cause
);
127 ctx
.error
= key_bindings_error
;
128 ctx
.print
= key_bindings_print
;
129 ctx
.info
= key_bindings_info
;
131 ctx
.cmdclient
= NULL
;
133 cmd_list_exec(cmdlist
, &ctx
);
134 cmd_list_free(cmdlist
);
140 cmd_confirm_before_free(void *data
)
142 struct cmd_confirm_before_data
*cdata
= data
;
144 if (cdata
->cmd
!= NULL
)