xref: /openbsd/usr.bin/tmux/cmd-confirm-before.c (revision 5dea098c)
1 /* $OpenBSD: cmd-confirm-before.c,v 1.54 2024/04/15 08:19:55 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
5  *
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.
9  *
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.
17  */
18 
19 #include <sys/types.h>
20 
21 #include <ctype.h>
22 #include <stdlib.h>
23 #include <string.h>
24 
25 #include "tmux.h"
26 
27 /*
28  * Asks for confirmation before executing a command.
29  */
30 
31 static enum args_parse_type	cmd_confirm_before_args_parse(struct args *,
32 				    u_int, char **);
33 static enum cmd_retval		cmd_confirm_before_exec(struct cmd *,
34 				    struct cmdq_item *);
35 
36 static int	cmd_confirm_before_callback(struct client *, void *,
37 		    const char *, int);
38 static void	cmd_confirm_before_free(void *);
39 
40 const struct cmd_entry cmd_confirm_before_entry = {
41 	.name = "confirm-before",
42 	.alias = "confirm",
43 
44 	.args = { "bc:p:t:y", 1, 1, cmd_confirm_before_args_parse },
45 	.usage = "[-by] [-c confirm_key] [-p prompt] " CMD_TARGET_CLIENT_USAGE
46 		 " command",
47 
48 	.flags = CMD_CLIENT_TFLAG,
49 	.exec = cmd_confirm_before_exec
50 };
51 
52 struct cmd_confirm_before_data {
53 	struct cmdq_item	*item;
54 	struct cmd_list		*cmdlist;
55 	u_char			 confirm_key;
56 	int			 default_yes;
57 };
58 
59 static enum args_parse_type
60 cmd_confirm_before_args_parse(__unused struct args *args, __unused u_int idx,
61     __unused char **cause)
62 {
63 	return (ARGS_PARSE_COMMANDS_OR_STRING);
64 }
65 
66 static enum cmd_retval
67 cmd_confirm_before_exec(struct cmd *self, struct cmdq_item *item)
68 {
69 	struct args			*args = cmd_get_args(self);
70 	struct cmd_confirm_before_data	*cdata;
71 	struct client			*tc = cmdq_get_target_client(item);
72 	struct cmd_find_state		*target = cmdq_get_target(item);
73 	char				*new_prompt;
74 	const char			*confirm_key, *prompt, *cmd;
75 	int				 wait = !args_has(args, 'b');
76 
77 	cdata = xcalloc(1, sizeof *cdata);
78 	cdata->cmdlist = args_make_commands_now(self, item, 0, 1);
79 	if (cdata->cmdlist == NULL) {
80 		free(cdata);
81 		return (CMD_RETURN_ERROR);
82 	}
83 
84 	if (wait)
85 		cdata->item = item;
86 
87 	cdata->default_yes = args_has(args, 'y');
88 	if ((confirm_key = args_get(args, 'c')) != NULL) {
89 		if (confirm_key[1] == '\0' &&
90 		    confirm_key[0] > 31 &&
91 		    confirm_key[0] < 127)
92 			cdata->confirm_key = confirm_key[0];
93 		else {
94 			cmdq_error(item, "invalid confirm key");
95 			return (CMD_RETURN_ERROR);
96 		}
97 	}
98 	else
99 		cdata->confirm_key = 'y';
100 
101 	if ((prompt = args_get(args, 'p')) != NULL)
102 		xasprintf(&new_prompt, "%s ", prompt);
103 	else {
104 		cmd = cmd_get_entry(cmd_list_first(cdata->cmdlist))->name;
105 		xasprintf(&new_prompt, "Confirm '%s'? (%c/n) ",
106 		cmd, cdata->confirm_key);
107 	}
108 
109 	status_prompt_set(tc, target, new_prompt, NULL,
110 	    cmd_confirm_before_callback, cmd_confirm_before_free, cdata,
111 	    PROMPT_SINGLE, PROMPT_TYPE_COMMAND);
112 	free(new_prompt);
113 
114 	if (!wait)
115 		return (CMD_RETURN_NORMAL);
116 	return (CMD_RETURN_WAIT);
117 }
118 
119 static int
120 cmd_confirm_before_callback(struct client *c, void *data, const char *s,
121     __unused int done)
122 {
123 	struct cmd_confirm_before_data	*cdata = data;
124 	struct cmdq_item		*item = cdata->item, *new_item;
125 	int				 retcode = 1;
126 
127 	if (c->flags & CLIENT_DEAD)
128 		goto out;
129 
130 	if (s == NULL)
131 		goto out;
132 	if (s[0] != cdata->confirm_key && (s[0] != '\0' || !cdata->default_yes))
133 		goto out;
134 	retcode = 0;
135 
136 	if (item == NULL) {
137 		new_item = cmdq_get_command(cdata->cmdlist, NULL);
138 		cmdq_append(c, new_item);
139 	} else {
140 		new_item = cmdq_get_command(cdata->cmdlist,
141 		    cmdq_get_state(item));
142 		cmdq_insert_after(item, new_item);
143 	}
144 
145 out:
146 	if (item != NULL) {
147 		if (cmdq_get_client(item) != NULL &&
148 		    cmdq_get_client(item)->session == NULL)
149 			cmdq_get_client(item)->retval = retcode;
150 		cmdq_continue(item);
151 	}
152 	return (0);
153 }
154 
155 static void
156 cmd_confirm_before_free(void *data)
157 {
158 	struct cmd_confirm_before_data	*cdata = data;
159 
160 	cmd_list_free(cdata->cmdlist);
161 	free(cdata);
162 }
163