Need to set clients in context before changing their reference count.
[tmux-openbsd.git] / cmd-confirm-before.c
blobf9491095d07ce712fa39d8a10edaa388f3b2af0b
1 /* $OpenBSD$ */
3 /*
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.
19 #include <ctype.h>
20 #include <stdlib.h>
21 #include <string.h>
23 #include "tmux.h"
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_ctx *);
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",
37 "p:t:", 1, 1,
38 "[-p prompt] " CMD_TARGET_CLIENT_USAGE " command",
40 cmd_confirm_before_key_binding,
41 NULL,
42 cmd_confirm_before_exec
45 struct cmd_confirm_before_data {
46 struct client *c;
47 char *cmd;
50 void
51 cmd_confirm_before_key_binding(struct cmd *self, int key)
53 switch (key) {
54 case '&':
55 self->args = args_create(1, "kill-window");
56 args_set(self->args, 'p', "kill-window #W? (y/n)");
57 break;
58 case 'x':
59 self->args = args_create(1, "kill-pane");
60 args_set(self->args, 'p', "kill-pane #P? (y/n)");
61 break;
62 default:
63 self->args = args_create(0);
64 break;
68 enum cmd_retval
69 cmd_confirm_before_exec(struct cmd *self, struct cmd_ctx *ctx)
71 struct args *args = self->args;
72 struct cmd_confirm_before_data *cdata;
73 struct client *c;
74 char *cmd, *copy, *new_prompt, *ptr;
75 const char *prompt;
77 if (ctx->curclient == NULL) {
78 ctx->error(ctx, "must be run interactively");
79 return (CMD_RETURN_ERROR);
82 if ((c = cmd_find_client(ctx, args_get(args, 't'))) == NULL)
83 return (CMD_RETURN_ERROR);
85 if ((prompt = args_get(args, 'p')) != NULL)
86 xasprintf(&new_prompt, "%s ", prompt);
87 else {
88 ptr = copy = xstrdup(args->argv[0]);
89 cmd = strsep(&ptr, " \t");
90 xasprintf(&new_prompt, "Confirm '%s'? (y/n) ", cmd);
91 free(copy);
94 cdata = xmalloc(sizeof *cdata);
95 cdata->cmd = xstrdup(args->argv[0]);
96 cdata->c = c;
97 status_prompt_set(cdata->c, new_prompt, NULL,
98 cmd_confirm_before_callback, cmd_confirm_before_free, cdata,
99 PROMPT_SINGLE);
101 free(new_prompt);
102 return (CMD_RETURN_YIELD);
106 cmd_confirm_before_callback(void *data, const char *s)
108 struct cmd_confirm_before_data *cdata = data;
109 struct client *c = cdata->c;
110 struct cmd_list *cmdlist;
111 struct cmd_ctx *ctx;
112 char *cause;
114 if (s == NULL || *s == '\0')
115 return (0);
116 if (tolower((u_char) s[0]) != 'y' || s[1] != '\0')
117 return (0);
119 if (cmd_string_parse(cdata->cmd, &cmdlist, &cause) != 0) {
120 if (cause != NULL) {
121 *cause = toupper((u_char) *cause);
122 status_message_set(c, "%s", cause);
123 free(cause);
125 return (0);
128 ctx = cmd_get_ctx(NULL, c);
129 ctx->error = key_bindings_error;
130 ctx->print = key_bindings_print;
131 ctx->info = key_bindings_info;
133 cmd_list_exec(cmdlist, ctx);
134 cmd_list_free(cmdlist);
135 cmd_free_ctx(ctx);
137 return (0);
140 void
141 cmd_confirm_before_free(void *data)
143 struct cmd_confirm_before_data *cdata = data;
145 free(cdata->cmd);
146 free(cdata);