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.
26 * Asks for confirmation before executing a command.
29 void cmd_confirm_before_key_binding(struct cmd
*, int);
30 enum cmd_retval
cmd_confirm_before_exec(struct cmd
*, struct cmd_q
*);
32 int cmd_confirm_before_callback(void *, const char *);
33 void cmd_confirm_before_free(void *);
35 const struct cmd_entry cmd_confirm_before_entry
= {
36 "confirm-before", "confirm",
38 "[-p prompt] " CMD_TARGET_CLIENT_USAGE
" command",
40 cmd_confirm_before_key_binding
,
42 cmd_confirm_before_exec
45 struct cmd_confirm_before_data
{
47 struct client
*client
;
51 cmd_confirm_before_key_binding(struct cmd
*self
, int key
)
55 self
->args
= args_create(1, "kill-window");
56 args_set(self
->args
, 'p', "kill-window #W? (y/n)");
59 self
->args
= args_create(1, "kill-pane");
60 args_set(self
->args
, 'p', "kill-pane #P? (y/n)");
63 self
->args
= args_create(0);
69 cmd_confirm_before_exec(struct cmd
*self
, struct cmd_q
*cmdq
)
71 struct args
*args
= self
->args
;
72 struct cmd_confirm_before_data
*cdata
;
74 char *cmd
, *copy
, *new_prompt
, *ptr
;
77 if ((c
= cmd_find_client(cmdq
, args_get(args
, 't'), 0)) == NULL
)
78 return (CMD_RETURN_ERROR
);
80 if ((prompt
= args_get(args
, 'p')) != NULL
)
81 xasprintf(&new_prompt
, "%s ", prompt
);
83 ptr
= copy
= xstrdup(args
->argv
[0]);
84 cmd
= strsep(&ptr
, " \t");
85 xasprintf(&new_prompt
, "Confirm '%s'? (y/n) ", cmd
);
89 cdata
= xmalloc(sizeof *cdata
);
90 cdata
->cmd
= xstrdup(args
->argv
[0]);
93 cdata
->client
->references
++;
95 status_prompt_set(c
, new_prompt
, NULL
,
96 cmd_confirm_before_callback
, cmd_confirm_before_free
, cdata
,
100 return (CMD_RETURN_NORMAL
);
104 cmd_confirm_before_callback(void *data
, const char *s
)
106 struct cmd_confirm_before_data
*cdata
= data
;
107 struct client
*c
= cdata
->client
;
108 struct cmd_list
*cmdlist
;
111 if (c
->flags
& CLIENT_DEAD
)
114 if (s
== NULL
|| *s
== '\0')
116 if (tolower((u_char
) s
[0]) != 'y' || s
[1] != '\0')
119 if (cmd_string_parse(cdata
->cmd
, &cmdlist
, NULL
, 0, &cause
) != 0) {
121 cmdq_error(c
->cmdq
, "%s", cause
);
127 cmdq_run(c
->cmdq
, cmdlist
);
128 cmd_list_free(cmdlist
);
134 cmd_confirm_before_free(void *data
)
136 struct cmd_confirm_before_data
*cdata
= data
;
137 struct client
*c
= cdata
->client
;