xref: /openbsd/usr.bin/tmux/cmd-confirm-before.c (revision 274d7c50)
1 /* $OpenBSD: cmd-confirm-before.c,v 1.37 2019/05/23 11:13:30 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 cmd_retval	cmd_confirm_before_exec(struct cmd *,
32 			    struct cmdq_item *);
33 
34 static int	cmd_confirm_before_callback(struct client *, void *,
35 		    const char *, int);
36 static void	cmd_confirm_before_free(void *);
37 
38 const struct cmd_entry cmd_confirm_before_entry = {
39 	.name = "confirm-before",
40 	.alias = "confirm",
41 
42 	.args = { "p:t:", 1, 1 },
43 	.usage = "[-p prompt] " CMD_TARGET_CLIENT_USAGE " command",
44 
45 	.flags = 0,
46 	.exec = cmd_confirm_before_exec
47 };
48 
49 struct cmd_confirm_before_data {
50 	char	*cmd;
51 };
52 
53 static enum cmd_retval
54 cmd_confirm_before_exec(struct cmd *self, struct cmdq_item *item)
55 {
56 	struct args			*args = self->args;
57 	struct cmd_confirm_before_data	*cdata;
58 	struct client			*c;
59 	char				*cmd, *copy, *new_prompt, *ptr;
60 	const char			*prompt;
61 
62 	if ((c = cmd_find_client(item, args_get(args, 't'), 0)) == NULL)
63 		return (CMD_RETURN_ERROR);
64 
65 	if ((prompt = args_get(args, 'p')) != NULL)
66 		xasprintf(&new_prompt, "%s ", prompt);
67 	else {
68 		ptr = copy = xstrdup(args->argv[0]);
69 		cmd = strsep(&ptr, " \t");
70 		xasprintf(&new_prompt, "Confirm '%s'? (y/n) ", cmd);
71 		free(copy);
72 	}
73 
74 	cdata = xmalloc(sizeof *cdata);
75 	cdata->cmd = xstrdup(args->argv[0]);
76 
77 	status_prompt_set(c, new_prompt, NULL,
78 	    cmd_confirm_before_callback, cmd_confirm_before_free, cdata,
79 	    PROMPT_SINGLE);
80 
81 	free(new_prompt);
82 	return (CMD_RETURN_NORMAL);
83 }
84 
85 static int
86 cmd_confirm_before_callback(struct client *c, void *data, const char *s,
87     __unused int done)
88 {
89 	struct cmd_confirm_before_data	*cdata = data;
90 	struct cmdq_item		*new_item;
91 	struct cmd_parse_result		*pr;
92 
93 	if (c->flags & CLIENT_DEAD)
94 		return (0);
95 
96 	if (s == NULL || *s == '\0')
97 		return (0);
98 	if (tolower((u_char)s[0]) != 'y' || s[1] != '\0')
99 		return (0);
100 
101 	pr = cmd_parse_from_string(cdata->cmd, NULL);
102 	switch (pr->status) {
103 	case CMD_PARSE_EMPTY:
104 		new_item = NULL;
105 		break;
106 	case CMD_PARSE_ERROR:
107 		new_item = cmdq_get_error(pr->error);
108 		free(pr->error);
109 		cmdq_append(c, new_item);
110 		break;
111 	case CMD_PARSE_SUCCESS:
112 		new_item = cmdq_get_command(pr->cmdlist, NULL, NULL, 0);
113 		cmd_list_free(pr->cmdlist);
114 		cmdq_append(c, new_item);
115 		break;
116 	}
117 
118 	return (0);
119 }
120 
121 static void
122 cmd_confirm_before_free(void *data)
123 {
124 	struct cmd_confirm_before_data	*cdata = data;
125 
126 	free(cdata->cmd);
127 	free(cdata);
128 }
129