xref: /openbsd/usr.bin/tmux/cmd-run-shell.c (revision b2f25228)
1*b2f25228Snicm /* $OpenBSD: cmd-run-shell.c,v 1.86 2024/05/14 07:33:01 nicm Exp $ */
2e74cd7aaSnicm 
3e74cd7aaSnicm /*
4e74cd7aaSnicm  * Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
50158b57dSnicm  * Copyright (c) 2009 Nicholas Marriott <nicm@openbsd.org>
6e74cd7aaSnicm  *
7e74cd7aaSnicm  * Permission to use, copy, modify, and distribute this software for any
8e74cd7aaSnicm  * purpose with or without fee is hereby granted, provided that the above
9e74cd7aaSnicm  * copyright notice and this permission notice appear in all copies.
10e74cd7aaSnicm  *
11e74cd7aaSnicm  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12e74cd7aaSnicm  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13e74cd7aaSnicm  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14e74cd7aaSnicm  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15e74cd7aaSnicm  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
16e74cd7aaSnicm  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
17e74cd7aaSnicm  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18e74cd7aaSnicm  */
19e74cd7aaSnicm 
20e74cd7aaSnicm #include <sys/types.h>
21e74cd7aaSnicm #include <sys/wait.h>
22e74cd7aaSnicm 
235d57355aSnicm #include <ctype.h>
247d053cf9Snicm #include <stdlib.h>
25e74cd7aaSnicm #include <string.h>
26e74cd7aaSnicm 
27e74cd7aaSnicm #include "tmux.h"
28e74cd7aaSnicm 
29e74cd7aaSnicm /*
30e74cd7aaSnicm  * Runs a command without a window.
31e74cd7aaSnicm  */
32e74cd7aaSnicm 
33e4c0b811Snicm static enum args_parse_type	cmd_run_shell_args_parse(struct args *, u_int,
34e4c0b811Snicm 				    char **);
35e4c0b811Snicm static enum cmd_retval		cmd_run_shell_exec(struct cmd *,
36e4c0b811Snicm 				    struct cmdq_item *);
373db5aa9cSnicm 
38f3705452Snicm static void	cmd_run_shell_timer(int, short, void *);
39dc1f0f5fSnicm static void	cmd_run_shell_callback(struct job *);
40dc1f0f5fSnicm static void	cmd_run_shell_free(void *);
41dc1f0f5fSnicm static void	cmd_run_shell_print(struct job *, const char *);
42a8ffebc3Snicm 
43e74cd7aaSnicm const struct cmd_entry cmd_run_shell_entry = {
44c057646bSnicm 	.name = "run-shell",
45c057646bSnicm 	.alias = "run",
46c057646bSnicm 
47f853135fSnicm 	.args = { "bd:Ct:c:", 0, 2, cmd_run_shell_args_parse },
48f853135fSnicm 	.usage = "[-bC] [-c start-directory] [-d delay] " CMD_TARGET_PANE_USAGE
49f853135fSnicm 	         " [shell-command]",
50c057646bSnicm 
51bf0d297eSnicm 	.target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
528d471e80Snicm 
538d471e80Snicm 	.flags = 0,
54c057646bSnicm 	.exec = cmd_run_shell_exec
55e74cd7aaSnicm };
56e74cd7aaSnicm 
57a8ffebc3Snicm struct cmd_run_shell_data {
58caf1a96fSnicm 	struct client			*client;
59a8ffebc3Snicm 	char				*cmd;
6067a3a2b6Snicm 	struct args_command_state	*state;
61f3705452Snicm 	char				*cwd;
6268e0a7f2Snicm 	struct cmdq_item		*item;
63f3705452Snicm 	struct session			*s;
645be049e6Snicm 	int				 wp_id;
65f3705452Snicm 	struct event			 timer;
66dc0b8f77Snicm 	int				 flags;
67a8ffebc3Snicm };
68a8ffebc3Snicm 
69e4c0b811Snicm static enum args_parse_type
cmd_run_shell_args_parse(struct args * args,__unused u_int idx,__unused char ** cause)70e4c0b811Snicm cmd_run_shell_args_parse(struct args *args, __unused u_int idx,
71e4c0b811Snicm     __unused char **cause)
72e4c0b811Snicm {
73e4c0b811Snicm 	if (args_has(args, 'C'))
74e4c0b811Snicm 		return (ARGS_PARSE_COMMANDS_OR_STRING);
75e4c0b811Snicm 	return (ARGS_PARSE_STRING);
76e4c0b811Snicm }
77e4c0b811Snicm 
78dc1f0f5fSnicm static void
cmd_run_shell_print(struct job * job,const char * msg)793db5aa9cSnicm cmd_run_shell_print(struct job *job, const char *msg)
803db5aa9cSnicm {
819430fe9eSnicm 	struct cmd_run_shell_data	*cdata = job_get_data(job);
825be049e6Snicm 	struct window_pane		*wp = NULL;
8353773dd5Snicm 	struct cmd_find_state		 fs;
842c8678f7Snicm 	struct window_mode_entry	*wme;
853db5aa9cSnicm 
865be049e6Snicm 	if (cdata->wp_id != -1)
873db5aa9cSnicm 		wp = window_pane_find_by_id(cdata->wp_id);
88*b2f25228Snicm 	if (wp == NULL) {
89*b2f25228Snicm 		if (cdata->item != NULL) {
90*b2f25228Snicm 			cmdq_print(cdata->item, "%s", msg);
91*b2f25228Snicm 			return;
92*b2f25228Snicm 		}
93*b2f25228Snicm 		if (cdata->item != NULL && cdata->client != NULL)
9416be08e6Snicm 			wp = server_client_get_pane(cdata->client);
9516be08e6Snicm 		if (wp == NULL && cmd_find_from_nothing(&fs, 0) == 0)
9653773dd5Snicm 			wp = fs.wp;
9753773dd5Snicm 		if (wp == NULL)
9853773dd5Snicm 			return;
99*b2f25228Snicm 	}
1003db5aa9cSnicm 
1012c8678f7Snicm 	wme = TAILQ_FIRST(&wp->modes);
1022c8678f7Snicm 	if (wme == NULL || wme->mode != &window_view_mode)
103cdfe74adSnicm 		window_pane_set_mode(wp, NULL, &window_view_mode, NULL, NULL);
10416be08e6Snicm 	window_copy_add(wp, 1, "%s", msg);
1053db5aa9cSnicm }
1063db5aa9cSnicm 
107dc1f0f5fSnicm static enum cmd_retval
cmd_run_shell_exec(struct cmd * self,struct cmdq_item * item)10868e0a7f2Snicm cmd_run_shell_exec(struct cmd *self, struct cmdq_item *item)
109e74cd7aaSnicm {
11090d7ba38Snicm 	struct args			*args = cmd_get_args(self);
111040343aeSnicm 	struct cmd_find_state		*target = cmdq_get_target(item);
112a8ffebc3Snicm 	struct cmd_run_shell_data	*cdata;
113f853135fSnicm 	struct client			*c = cmdq_get_client(item);
114caf1a96fSnicm 	struct client			*tc = cmdq_get_target_client(item);
115040343aeSnicm 	struct session			*s = target->s;
116040343aeSnicm 	struct window_pane		*wp = target->wp;
1172db6a388Snicm 	const char			*delay, *cmd;
118f3705452Snicm 	double				 d;
119f3705452Snicm 	struct timeval			 tv;
120f3705452Snicm 	char				*end;
121caf1a96fSnicm 	int				 wait = !args_has(args, 'b');
122caf1a96fSnicm 
123caf1a96fSnicm 	if ((delay = args_get(args, 'd')) != NULL) {
124caf1a96fSnicm 		d = strtod(delay, &end);
125caf1a96fSnicm 		if (*end != '\0') {
126caf1a96fSnicm 			cmdq_error(item, "invalid delay time: %s", delay);
127caf1a96fSnicm 			return (CMD_RETURN_ERROR);
128caf1a96fSnicm 		}
1291693b10bSnicm 	} else if (args_count(args) == 0)
130caf1a96fSnicm 		return (CMD_RETURN_NORMAL);
131765b9a58Snicm 
13210c3dabaSnicm 	cdata = xcalloc(1, sizeof *cdata);
1332db6a388Snicm 	if (!args_has(args, 'C')) {
1342db6a388Snicm 		cmd = args_string(args, 0);
1351693b10bSnicm 		if (cmd != NULL)
1361693b10bSnicm 			cdata->cmd = format_single_from_target(item, cmd);
1372db6a388Snicm 	} else {
13867a3a2b6Snicm 		cdata->state = args_make_commands_prepare(self, item, 0, NULL,
13967a3a2b6Snicm 		    wait, 1);
140caf1a96fSnicm 	}
141caf1a96fSnicm 
142ca74ffffSnicm 	if (args_has(args, 't') && wp != NULL)
143ca74ffffSnicm 		cdata->wp_id = wp->id;
144ca74ffffSnicm 	else
145ca74ffffSnicm 		cdata->wp_id = -1;
146a8ffebc3Snicm 
147caf1a96fSnicm 	if (wait) {
148f853135fSnicm 		cdata->client = c;
14968e0a7f2Snicm 		cdata->item = item;
150caf1a96fSnicm 	} else {
151caf1a96fSnicm 		cdata->client = tc;
152dc0b8f77Snicm 		cdata->flags |= JOB_NOWAIT;
153caf1a96fSnicm 	}
154caf1a96fSnicm 	if (cdata->client != NULL)
155caf1a96fSnicm 		cdata->client->references++;
156f853135fSnicm 	if (args_has(args, 'c'))
157f853135fSnicm 		cdata->cwd = xstrdup(args_get(args, 'c'));
158f853135fSnicm 	else
159f853135fSnicm 		cdata->cwd = xstrdup(server_client_get_cwd(c, s));
160caf1a96fSnicm 
161f3705452Snicm 	cdata->s = s;
162f841917fSnicm 	if (s != NULL)
163f3705452Snicm 		session_add_ref(s, __func__);
164f3705452Snicm 
165f3705452Snicm 	evtimer_set(&cdata->timer, cmd_run_shell_timer, cdata);
166caf1a96fSnicm 	if (delay != NULL) {
167f3705452Snicm 		timerclear(&tv);
168f3705452Snicm 		tv.tv_sec = (time_t)d;
169f3705452Snicm 		tv.tv_usec = (d - (double)tv.tv_sec) * 1000000U;
170f3705452Snicm 		evtimer_add(&cdata->timer, &tv);
171f3705452Snicm 	} else
172caf1a96fSnicm 		event_active(&cdata->timer, EV_TIMEOUT, 1);
173a8ffebc3Snicm 
174caf1a96fSnicm 	if (!wait)
175175d36ccSnicm 		return (CMD_RETURN_NORMAL);
176175d36ccSnicm 	return (CMD_RETURN_WAIT);
177e74cd7aaSnicm }
178e74cd7aaSnicm 
179dc1f0f5fSnicm static void
cmd_run_shell_timer(__unused int fd,__unused short events,void * arg)180f3705452Snicm cmd_run_shell_timer(__unused int fd, __unused short events, void* arg)
181f3705452Snicm {
182f3705452Snicm 	struct cmd_run_shell_data	*cdata = arg;
183caf1a96fSnicm 	struct client			*c = cdata->client;
184caf1a96fSnicm 	const char			*cmd = cdata->cmd;
1852db6a388Snicm 	struct cmdq_item		*item = cdata->item, *new_item;
18667a3a2b6Snicm 	struct cmd_list			*cmdlist;
18767a3a2b6Snicm 	char				*error;
188f3705452Snicm 
1892d792bf3Snicm 	if (cdata->state == NULL) {
1902d792bf3Snicm 		if (cmd == NULL) {
1912d792bf3Snicm 			if (cdata->item != NULL)
1922d792bf3Snicm 				cmdq_continue(cdata->item);
1932d792bf3Snicm 			cmd_run_shell_free(cdata);
1942d792bf3Snicm 			return;
1952d792bf3Snicm 		}
19603a1f8ddSnicm 		if (job_run(cmd, 0, NULL, NULL, cdata->s, cdata->cwd, NULL,
197dc0b8f77Snicm 		    cmd_run_shell_callback, cmd_run_shell_free, cdata,
198dc0b8f77Snicm 		    cdata->flags, -1, -1) == NULL)
199f3705452Snicm 			cmd_run_shell_free(cdata);
200caf1a96fSnicm 		return;
201caf1a96fSnicm 	}
202caf1a96fSnicm 
20367a3a2b6Snicm 	cmdlist = args_make_commands(cdata->state, 0, NULL, &error);
20467a3a2b6Snicm 	if (cmdlist == NULL) {
20567a3a2b6Snicm 		if (cdata->item == NULL) {
20667a3a2b6Snicm 			*error = toupper((u_char)*error);
20767a3a2b6Snicm 			status_message_set(c, -1, 1, 0, "%s", error);
20867a3a2b6Snicm 		} else
20967a3a2b6Snicm 			cmdq_error(cdata->item, "%s", error);
21067a3a2b6Snicm 		free(error);
21167a3a2b6Snicm 	} else if (item == NULL) {
21267a3a2b6Snicm 		new_item = cmdq_get_command(cmdlist, NULL);
2132db6a388Snicm 		cmdq_append(c, new_item);
214f3705452Snicm 	} else {
21567a3a2b6Snicm 		new_item = cmdq_get_command(cmdlist, cmdq_get_state(item));
2162db6a388Snicm 		cmdq_insert_after(item, new_item);
217caf1a96fSnicm 	}
218caf1a96fSnicm 
219f3705452Snicm 	if (cdata->item != NULL)
220f3705452Snicm 		cmdq_continue(cdata->item);
221f3705452Snicm 	cmd_run_shell_free(cdata);
222f3705452Snicm }
223f3705452Snicm 
224f3705452Snicm static void
cmd_run_shell_callback(struct job * job)225a8ffebc3Snicm cmd_run_shell_callback(struct job *job)
226a8ffebc3Snicm {
2279430fe9eSnicm 	struct cmd_run_shell_data	*cdata = job_get_data(job);
2289430fe9eSnicm 	struct bufferevent		*event = job_get_event(job);
229b372330dSnicm 	struct cmdq_item		*item = cdata->item;
2309430fe9eSnicm 	char				*cmd = cdata->cmd, *msg = NULL, *line;
23124abd014Snicm 	size_t				 size;
2329430fe9eSnicm 	int				 retcode, status;
233a8ffebc3Snicm 
23424abd014Snicm 	do {
23516be08e6Snicm 		line = evbuffer_readln(event->input, NULL, EVBUFFER_EOL_LF);
23616be08e6Snicm 		if (line != NULL) {
2373db5aa9cSnicm 			cmd_run_shell_print(job, line);
238f5a9523cSnicm 			free(line);
23924abd014Snicm 		}
24024abd014Snicm 	} while (line != NULL);
24124abd014Snicm 
2429430fe9eSnicm 	size = EVBUFFER_LENGTH(event->input);
24324abd014Snicm 	if (size != 0) {
24424abd014Snicm 		line = xmalloc(size + 1);
2459430fe9eSnicm 		memcpy(line, EVBUFFER_DATA(event->input), size);
24624abd014Snicm 		line[size] = '\0';
24724abd014Snicm 
2483db5aa9cSnicm 		cmd_run_shell_print(job, line);
24924abd014Snicm 
2507d053cf9Snicm 		free(line);
25124abd014Snicm 	}
252a8ffebc3Snicm 
2539430fe9eSnicm 	status = job_get_status(job);
2549430fe9eSnicm 	if (WIFEXITED(status)) {
2559430fe9eSnicm 		if ((retcode = WEXITSTATUS(status)) != 0)
256a8ffebc3Snicm 			xasprintf(&msg, "'%s' returned %d", cmd, retcode);
2579430fe9eSnicm 	} else if (WIFSIGNALED(status)) {
2589430fe9eSnicm 		retcode = WTERMSIG(status);
259a8ffebc3Snicm 		xasprintf(&msg, "'%s' terminated by signal %d", cmd, retcode);
260b372330dSnicm 		retcode += 128;
26167c16a7cSnicm 	} else
26267c16a7cSnicm 		retcode = 0;
2637f6133c3Snicm 	if (msg != NULL)
2643db5aa9cSnicm 		cmd_run_shell_print(job, msg);
2657d053cf9Snicm 	free(msg);
266765b9a58Snicm 
267b372330dSnicm 	if (item != NULL) {
268040343aeSnicm 		if (cmdq_get_client(item) != NULL &&
269040343aeSnicm 		    cmdq_get_client(item)->session == NULL)
270040343aeSnicm 			cmdq_get_client(item)->retval = retcode;
271b372330dSnicm 		cmdq_continue(item);
272b372330dSnicm 	}
273e74cd7aaSnicm }
274a8ffebc3Snicm 
275dc1f0f5fSnicm static void
cmd_run_shell_free(void * data)276a8ffebc3Snicm cmd_run_shell_free(void *data)
277a8ffebc3Snicm {
278a8ffebc3Snicm 	struct cmd_run_shell_data	*cdata = data;
279a8ffebc3Snicm 
280f3705452Snicm 	evtimer_del(&cdata->timer);
281f841917fSnicm 	if (cdata->s != NULL)
282f3705452Snicm 		session_remove_ref(cdata->s, __func__);
283caf1a96fSnicm 	if (cdata->client != NULL)
284caf1a96fSnicm 		server_client_unref(cdata->client);
28567a3a2b6Snicm 	if (cdata->state != NULL)
28667a3a2b6Snicm 		args_make_commands_free(cdata->state);
287f3705452Snicm 	free(cdata->cwd);
2887d053cf9Snicm 	free(cdata->cmd);
2897d053cf9Snicm 	free(cdata);
290e74cd7aaSnicm }
291