xref: /openbsd-src/usr.bin/tmux/cmd-run-shell.c (revision d530b5976a6a7344784fc41a6428f671c179a0b5)
1 /* $OpenBSD: cmd-run-shell.c,v 1.74 2021/08/13 06:50:42 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 <ctype.h>
24 #include <stdlib.h>
25 #include <string.h>
26 
27 #include "tmux.h"
28 
29 /*
30  * Runs a command without a window.
31  */
32 
33 static enum cmd_retval	cmd_run_shell_exec(struct cmd *, struct cmdq_item *);
34 
35 static void	cmd_run_shell_timer(int, short, void *);
36 static void	cmd_run_shell_callback(struct job *);
37 static void	cmd_run_shell_free(void *);
38 static void	cmd_run_shell_print(struct job *, const char *);
39 
40 const struct cmd_entry cmd_run_shell_entry = {
41 	.name = "run-shell",
42 	.alias = "run",
43 
44 	.args = { "bd:Ct:", 0, 1 },
45 	.usage = "[-bC] [-d delay] " CMD_TARGET_PANE_USAGE " [shell-command]",
46 
47 	.target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
48 
49 	.flags = 0,
50 	.exec = cmd_run_shell_exec
51 };
52 
53 struct cmd_run_shell_data {
54 	struct client		*client;
55 	char			*cmd;
56 	int			 shell;
57 	char			*cwd;
58 	struct cmdq_item	*item;
59 	struct session		*s;
60 	int			 wp_id;
61 	struct event		 timer;
62 	int			 flags;
63 	struct cmd_parse_input	 pi;
64 };
65 
66 static void
67 cmd_run_shell_print(struct job *job, const char *msg)
68 {
69 	struct cmd_run_shell_data	*cdata = job_get_data(job);
70 	struct window_pane		*wp = NULL;
71 	struct cmd_find_state		 fs;
72 	struct window_mode_entry	*wme;
73 
74 	if (cdata->wp_id != -1)
75 		wp = window_pane_find_by_id(cdata->wp_id);
76 	if (wp == NULL) {
77 		if (cdata->item != NULL) {
78 			cmdq_print(cdata->item, "%s", msg);
79 			return;
80 		}
81 		if (cmd_find_from_nothing(&fs, 0) != 0)
82 			return;
83 		wp = fs.wp;
84 		if (wp == NULL)
85 			return;
86 	}
87 
88 	wme = TAILQ_FIRST(&wp->modes);
89 	if (wme == NULL || wme->mode != &window_view_mode)
90 		window_pane_set_mode(wp, NULL, &window_view_mode, NULL, NULL);
91 	window_copy_add(wp, "%s", msg);
92 }
93 
94 static enum cmd_retval
95 cmd_run_shell_exec(struct cmd *self, struct cmdq_item *item)
96 {
97 	struct args			*args = cmd_get_args(self);
98 	struct cmd_find_state		*target = cmdq_get_target(item);
99 	struct cmd_run_shell_data	*cdata;
100 	struct client			*tc = cmdq_get_target_client(item);
101 	struct session			*s = target->s;
102 	struct window_pane		*wp = target->wp;
103 	const char			*delay;
104 	double				 d;
105 	struct timeval			 tv;
106 	char				*end;
107 	int				 wait = !args_has(args, 'b');
108 
109 	if ((delay = args_get(args, 'd')) != NULL) {
110 		d = strtod(delay, &end);
111 		if (*end != '\0') {
112 			cmdq_error(item, "invalid delay time: %s", delay);
113 			return (CMD_RETURN_ERROR);
114 		}
115 	} else if (args->argc == 0)
116 		return (CMD_RETURN_NORMAL);
117 
118 	cdata = xcalloc(1, sizeof *cdata);
119 	if (args->argc != 0)
120 		cdata->cmd = format_single_from_target(item, args->argv[0]);
121 
122 	cdata->shell = !args_has(args, 'C');
123 	if (!cdata->shell) {
124 		cmd_get_source(self, &cdata->pi.file, &cdata->pi.line);
125 		if (wait)
126 			cdata->pi.item = item;
127 		cdata->pi.c = tc;
128 		cmd_find_copy_state(&cdata->pi.fs, target);
129 	}
130 
131 	if (args_has(args, 't') && wp != NULL)
132 		cdata->wp_id = wp->id;
133 	else
134 		cdata->wp_id = -1;
135 
136 	if (wait) {
137 		cdata->client = cmdq_get_client(item);
138 		cdata->item = item;
139 	} else {
140 		cdata->client = tc;
141 		cdata->flags |= JOB_NOWAIT;
142 	}
143 	if (cdata->client != NULL)
144 		cdata->client->references++;
145 
146 	cdata->cwd = xstrdup(server_client_get_cwd(cmdq_get_client(item), s));
147 
148 	cdata->s = s;
149 	if (s != NULL)
150 		session_add_ref(s, __func__);
151 
152 	evtimer_set(&cdata->timer, cmd_run_shell_timer, cdata);
153 	if (delay != NULL) {
154 		timerclear(&tv);
155 		tv.tv_sec = (time_t)d;
156 		tv.tv_usec = (d - (double)tv.tv_sec) * 1000000U;
157 		evtimer_add(&cdata->timer, &tv);
158 	} else
159 		event_active(&cdata->timer, EV_TIMEOUT, 1);
160 
161 	if (!wait)
162 		return (CMD_RETURN_NORMAL);
163 	return (CMD_RETURN_WAIT);
164 }
165 
166 static void
167 cmd_run_shell_timer(__unused int fd, __unused short events, void* arg)
168 {
169 	struct cmd_run_shell_data	*cdata = arg;
170 	struct client			*c = cdata->client;
171 	const char			*cmd = cdata->cmd;
172 	char				*error;
173 	struct cmdq_item		*item = cdata->item;
174 	enum cmd_parse_status		 status;
175 
176 	if (cmd != NULL && cdata->shell) {
177 		if (job_run(cmd, 0, NULL, cdata->s, cdata->cwd, NULL,
178 		    cmd_run_shell_callback, cmd_run_shell_free, cdata,
179 		    cdata->flags, -1, -1) == NULL)
180 			cmd_run_shell_free(cdata);
181 		return;
182 	}
183 
184 	if (cmd != NULL) {
185 		if (item != NULL) {
186 			status = cmd_parse_and_insert(cmd, &cdata->pi, item,
187 			    cmdq_get_state(item), &error);
188 		} else {
189 			status = cmd_parse_and_append(cmd, &cdata->pi, c, NULL,
190 			    &error);
191 		}
192 		if (status == CMD_PARSE_ERROR) {
193 			if (cdata->item == NULL) {
194 				*error = toupper((u_char)*error);
195 				status_message_set(c, -1, 1, 0, "%s", error);
196 			} else
197 				cmdq_error(cdata->item, "%s", error);
198 			free(error);
199 		}
200 	}
201 
202 	if (cdata->item != NULL)
203 		cmdq_continue(cdata->item);
204 	cmd_run_shell_free(cdata);
205 }
206 
207 static void
208 cmd_run_shell_callback(struct job *job)
209 {
210 	struct cmd_run_shell_data	*cdata = job_get_data(job);
211 	struct bufferevent		*event = job_get_event(job);
212 	struct cmdq_item		*item = cdata->item;
213 	char				*cmd = cdata->cmd, *msg = NULL, *line;
214 	size_t				 size;
215 	int				 retcode, status;
216 
217 	do {
218 		if ((line = evbuffer_readline(event->input)) != NULL) {
219 			cmd_run_shell_print(job, line);
220 			free(line);
221 		}
222 	} while (line != NULL);
223 
224 	size = EVBUFFER_LENGTH(event->input);
225 	if (size != 0) {
226 		line = xmalloc(size + 1);
227 		memcpy(line, EVBUFFER_DATA(event->input), size);
228 		line[size] = '\0';
229 
230 		cmd_run_shell_print(job, line);
231 
232 		free(line);
233 	}
234 
235 	status = job_get_status(job);
236 	if (WIFEXITED(status)) {
237 		if ((retcode = WEXITSTATUS(status)) != 0)
238 			xasprintf(&msg, "'%s' returned %d", cmd, retcode);
239 	} else if (WIFSIGNALED(status)) {
240 		retcode = WTERMSIG(status);
241 		xasprintf(&msg, "'%s' terminated by signal %d", cmd, retcode);
242 		retcode += 128;
243 	} else
244 		retcode = 0;
245 	if (msg != NULL)
246 		cmd_run_shell_print(job, msg);
247 	free(msg);
248 
249 	if (item != NULL) {
250 		if (cmdq_get_client(item) != NULL &&
251 		    cmdq_get_client(item)->session == NULL)
252 			cmdq_get_client(item)->retval = retcode;
253 		cmdq_continue(item);
254 	}
255 }
256 
257 static void
258 cmd_run_shell_free(void *data)
259 {
260 	struct cmd_run_shell_data	*cdata = data;
261 
262 	evtimer_del(&cdata->timer);
263 	if (cdata->s != NULL)
264 		session_remove_ref(cdata->s, __func__);
265 	if (cdata->client != NULL)
266 		server_client_unref(cdata->client);
267 	free(cdata->cwd);
268 	free(cdata->cmd);
269 	free(cdata);
270 }
271