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 void cmd_confirm_before_key_binding(struct cmd
*, int);
29 int cmd_confirm_before_exec(struct cmd
*, struct cmd_ctx
*);
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",
37 CMD_TARGET_CLIENT_USAGE
" command",
39 cmd_confirm_before_key_binding
,
41 cmd_confirm_before_exec
44 struct cmd_confirm_before_data
{
50 cmd_confirm_before_key_binding(struct cmd
*self
, int key
)
54 self
->args
= args_create(1, "kill-window");
57 self
->args
= args_create(1, "kill-pane");
60 self
->args
= args_create(0);
66 cmd_confirm_before_exec(struct cmd
*self
, struct cmd_ctx
*ctx
)
68 struct args
*args
= self
->args
;
69 struct cmd_confirm_before_data
*cdata
;
71 char *buf
, *cmd
, *ptr
;
73 if (ctx
->curclient
== NULL
) {
74 ctx
->error(ctx
, "must be run interactively");
78 if ((c
= cmd_find_client(ctx
, args_get(args
, 't'))) == NULL
)
81 ptr
= xstrdup(args
->argv
[0]);
82 if ((cmd
= strtok(ptr
, " \t")) == NULL
)
84 xasprintf(&buf
, "Confirm '%s'? (y/n) ", cmd
);
87 cdata
= xmalloc(sizeof *cdata
);
88 cdata
->cmd
= xstrdup(args
->argv
[0]);
90 status_prompt_set(cdata
->c
, buf
,
91 cmd_confirm_before_callback
, cmd_confirm_before_free
, cdata
,
99 cmd_confirm_before_callback(void *data
, const char *s
)
101 struct cmd_confirm_before_data
*cdata
= data
;
102 struct client
*c
= cdata
->c
;
103 struct cmd_list
*cmdlist
;
107 if (s
== NULL
|| *s
== '\0')
109 if (tolower((u_char
) s
[0]) != 'y' || s
[1] != '\0')
112 if (cmd_string_parse(cdata
->cmd
, &cmdlist
, &cause
) != 0) {
114 *cause
= toupper((u_char
) *cause
);
115 status_message_set(c
, "%s", cause
);
124 ctx
.error
= key_bindings_error
;
125 ctx
.print
= key_bindings_print
;
126 ctx
.info
= key_bindings_info
;
128 ctx
.cmdclient
= NULL
;
130 cmd_list_exec(cmdlist
, &ctx
);
131 cmd_list_free(cmdlist
);
137 cmd_confirm_before_free(void *data
)
139 struct cmd_confirm_before_data
*cdata
= data
;
141 if (cdata
->cmd
!= NULL
)