xref: /openbsd/usr.bin/tmux/cmd-if-shell.c (revision 054f42ac)
1 /* $OpenBSD: cmd-if-shell.c,v 1.70 2020/04/13 14:04:25 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
5  * Copyright (c) 2009 Nicholas Marriott <nicm@openbsd.org>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
16  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
17  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <sys/types.h>
21 #include <sys/wait.h>
22 
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #include "tmux.h"
27 
28 /*
29  * Executes a tmux command if a shell command returns true or false.
30  */
31 
32 static enum cmd_retval	cmd_if_shell_exec(struct cmd *, struct cmdq_item *);
33 
34 static void		cmd_if_shell_callback(struct job *);
35 static void		cmd_if_shell_free(void *);
36 
37 const struct cmd_entry cmd_if_shell_entry = {
38 	.name = "if-shell",
39 	.alias = "if",
40 
41 	.args = { "bFt:", 2, 3 },
42 	.usage = "[-bF] " CMD_TARGET_PANE_USAGE " shell-command command "
43 		 "[command]",
44 
45 	.target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
46 
47 	.flags = 0,
48 	.exec = cmd_if_shell_exec
49 };
50 
51 struct cmd_if_shell_data {
52 	struct cmd_parse_input	 input;
53 
54 	char			*cmd_if;
55 	char			*cmd_else;
56 
57 	struct client		*client;
58 	struct cmdq_item	*item;
59 	struct mouse_event	 mouse;
60 };
61 
62 static enum cmd_retval
63 cmd_if_shell_exec(struct cmd *self, struct cmdq_item *item)
64 {
65 	struct args			*args = cmd_get_args(self);
66 	struct cmdq_state		*state = cmdq_get_state(item);
67 	struct cmd_find_state		*target = cmdq_get_target(item);
68 	struct mouse_event		*m = &state->event.m;
69 	struct cmd_if_shell_data	*cdata;
70 	char				*shellcmd, *cmd;
71 	const char			*file;
72 	struct cmdq_item		*new_item;
73 	struct client			*c = cmd_find_client(item, NULL, 1);
74 	struct session			*s = target->s;
75 	struct cmd_parse_input		 pi;
76 	struct cmd_parse_result		*pr;
77 
78 	shellcmd = format_single_from_target(item, args->argv[0], c);
79 	if (args_has(args, 'F')) {
80 		if (*shellcmd != '0' && *shellcmd != '\0')
81 			cmd = args->argv[1];
82 		else if (args->argc == 3)
83 			cmd = args->argv[2];
84 		else
85 			cmd = NULL;
86 		free(shellcmd);
87 		if (cmd == NULL)
88 			return (CMD_RETURN_NORMAL);
89 
90 		memset(&pi, 0, sizeof pi);
91 		cmd_get_source(self, &pi.file, &pi.line);
92 		pi.item = item;
93 		pi.c = c;
94 		cmd_find_copy_state(&pi.fs, target);
95 
96 		pr = cmd_parse_from_string(cmd, &pi);
97 		switch (pr->status) {
98 		case CMD_PARSE_EMPTY:
99 			break;
100 		case CMD_PARSE_ERROR:
101 			cmdq_error(item, "%s", pr->error);
102 			free(pr->error);
103 			return (CMD_RETURN_ERROR);
104 		case CMD_PARSE_SUCCESS:
105 			new_item = cmdq_get_command(pr->cmdlist, target, m, 0);
106 			cmdq_insert_after(item, new_item);
107 			cmd_list_free(pr->cmdlist);
108 			break;
109 		}
110 		return (CMD_RETURN_NORMAL);
111 	}
112 
113 	cdata = xcalloc(1, sizeof *cdata);
114 
115 	cdata->cmd_if = xstrdup(args->argv[1]);
116 	if (args->argc == 3)
117 		cdata->cmd_else = xstrdup(args->argv[2]);
118 	else
119 		cdata->cmd_else = NULL;
120 	memcpy(&cdata->mouse, m, sizeof cdata->mouse);
121 
122 	if (!args_has(args, 'b'))
123 		cdata->client = cmdq_get_client(item);
124 	else
125 		cdata->client = c;
126 	if (cdata->client != NULL)
127 		cdata->client->references++;
128 
129 	if (!args_has(args, 'b'))
130 		cdata->item = item;
131 	else
132 		cdata->item = NULL;
133 
134 	memset(&cdata->input, 0, sizeof cdata->input);
135 	cmd_get_source(self, &file, &cdata->input.line);
136 	if (file != NULL)
137 		cdata->input.file = xstrdup(file);
138 	cdata->input.c = c;
139 	if (cdata->input.c != NULL)
140 		cdata->input.c->references++;
141 	cmd_find_copy_state(&cdata->input.fs, target);
142 
143 	if (job_run(shellcmd, s,
144 	    server_client_get_cwd(cmdq_get_client(item), s), NULL,
145 	    cmd_if_shell_callback, cmd_if_shell_free, cdata, 0, -1,
146 	    -1) == NULL) {
147 		cmdq_error(item, "failed to run command: %s", shellcmd);
148 		free(shellcmd);
149 		free(cdata);
150 		return (CMD_RETURN_ERROR);
151 	}
152 	free(shellcmd);
153 
154 	if (args_has(args, 'b'))
155 		return (CMD_RETURN_NORMAL);
156 	return (CMD_RETURN_WAIT);
157 }
158 
159 static void
160 cmd_if_shell_callback(struct job *job)
161 {
162 	struct cmd_if_shell_data	*cdata = job_get_data(job);
163 	struct client			*c = cdata->client;
164 	struct mouse_event		*m = &cdata->mouse;
165 	struct cmdq_item		*new_item = NULL;
166 	char				*cmd;
167 	int				 status;
168 	struct cmd_parse_result		*pr;
169 
170 	status = job_get_status(job);
171 	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
172 		cmd = cdata->cmd_else;
173 	else
174 		cmd = cdata->cmd_if;
175 	if (cmd == NULL)
176 		goto out;
177 
178 	pr = cmd_parse_from_string(cmd, &cdata->input);
179 	switch (pr->status) {
180 	case CMD_PARSE_EMPTY:
181 		break;
182 	case CMD_PARSE_ERROR:
183 		if (cdata->item != NULL)
184 		       cmdq_error(cdata->item, "%s", pr->error);
185 		free(pr->error);
186 		break;
187 	case CMD_PARSE_SUCCESS:
188 		new_item = cmdq_get_command(pr->cmdlist, NULL, m, 0);
189 		cmd_list_free(pr->cmdlist);
190 		break;
191 	}
192 	if (new_item != NULL) {
193 		if (cdata->item == NULL)
194 			cmdq_append(c, new_item);
195 		else
196 			cmdq_insert_after(cdata->item, new_item);
197 	}
198 
199 out:
200 	if (cdata->item != NULL)
201 		cmdq_continue(cdata->item);
202 }
203 
204 static void
205 cmd_if_shell_free(void *data)
206 {
207 	struct cmd_if_shell_data	*cdata = data;
208 
209 	if (cdata->client != NULL)
210 		server_client_unref(cdata->client);
211 
212 	free(cdata->cmd_else);
213 	free(cdata->cmd_if);
214 
215 	if (cdata->input.c != NULL)
216 		server_client_unref(cdata->input.c);
217 	free((void *)cdata->input.file);
218 
219 	free(cdata);
220 }
221