xref: /openbsd-src/usr.bin/tmux/cmd-run-shell.c (revision 90d7ba3861d079c6fa4f1960b5f66a00fbd31640)
1 /* $OpenBSD: cmd-run-shell.c,v 1.65 2020/04/13 08:26:27 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  * Runs a command without a window.
30  */
31 
32 static enum cmd_retval	cmd_run_shell_exec(struct cmd *, struct cmdq_item *);
33 
34 static void	cmd_run_shell_timer(int, short, void *);
35 static void	cmd_run_shell_callback(struct job *);
36 static void	cmd_run_shell_free(void *);
37 static void	cmd_run_shell_print(struct job *, const char *);
38 
39 const struct cmd_entry cmd_run_shell_entry = {
40 	.name = "run-shell",
41 	.alias = "run",
42 
43 	.args = { "bd:t:", 0, 1 },
44 	.usage = "[-b] [-d delay] " CMD_TARGET_PANE_USAGE " [shell-command]",
45 
46 	.target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
47 
48 	.flags = 0,
49 	.exec = cmd_run_shell_exec
50 };
51 
52 struct cmd_run_shell_data {
53 	char			*cmd;
54 	char			*cwd;
55 	struct cmdq_item	*item;
56 	struct session		*s;
57 	int			 wp_id;
58 	struct event		 timer;
59 };
60 
61 static void
62 cmd_run_shell_print(struct job *job, const char *msg)
63 {
64 	struct cmd_run_shell_data	*cdata = job_get_data(job);
65 	struct window_pane		*wp = NULL;
66 	struct cmd_find_state		 fs;
67 	struct window_mode_entry	*wme;
68 
69 	if (cdata->wp_id != -1)
70 		wp = window_pane_find_by_id(cdata->wp_id);
71 	if (wp == NULL) {
72 		if (cdata->item != NULL) {
73 			cmdq_print(cdata->item, "%s", msg);
74 			return;
75 		}
76 		if (cmd_find_from_nothing(&fs, 0) != 0)
77 			return;
78 		wp = fs.wp;
79 		if (wp == NULL)
80 			return;
81 	}
82 
83 	wme = TAILQ_FIRST(&wp->modes);
84 	if (wme == NULL || wme->mode != &window_view_mode)
85 		window_pane_set_mode(wp, NULL, &window_view_mode, NULL, NULL);
86 	window_copy_add(wp, "%s", msg);
87 }
88 
89 static enum cmd_retval
90 cmd_run_shell_exec(struct cmd *self, struct cmdq_item *item)
91 {
92 	struct args			*args = cmd_get_args(self);
93 	struct cmd_run_shell_data	*cdata;
94 	struct client			*c = cmd_find_client(item, NULL, 1);
95 	struct session			*s = item->target.s;
96 	struct winlink			*wl = item->target.wl;
97 	struct window_pane		*wp = item->target.wp;
98 	const char			*delay;
99 	double				 d;
100 	struct timeval			 tv;
101 	char				*end;
102 
103 	cdata = xcalloc(1, sizeof *cdata);
104 	if (args->argc != 0)
105 		cdata->cmd = format_single(item, args->argv[0], c, s, wl, wp);
106 
107 	if (args_has(args, 't') && wp != NULL)
108 		cdata->wp_id = wp->id;
109 	else
110 		cdata->wp_id = -1;
111 
112 	if (!args_has(args, 'b'))
113 		cdata->item = item;
114 
115 	cdata->cwd = xstrdup(server_client_get_cwd(item->client, s));
116 	cdata->s = s;
117 	if (s != NULL)
118 		session_add_ref(s, __func__);
119 
120 	evtimer_set(&cdata->timer, cmd_run_shell_timer, cdata);
121 
122 	if ((delay = args_get(args, 'd')) != NULL) {
123 		d = strtod(delay, &end);
124 		if (*end != '\0') {
125 			cmdq_error(item, "invalid delay time: %s", delay);
126 			cmd_run_shell_free(cdata);
127 			return (CMD_RETURN_ERROR);
128 		}
129 		timerclear(&tv);
130 		tv.tv_sec = (time_t)d;
131 		tv.tv_usec = (d - (double)tv.tv_sec) * 1000000U;
132 		evtimer_add(&cdata->timer, &tv);
133 	} else
134 		cmd_run_shell_timer(-1, 0, cdata);
135 
136 	if (args_has(args, 'b'))
137 		return (CMD_RETURN_NORMAL);
138 	return (CMD_RETURN_WAIT);
139 }
140 
141 static void
142 cmd_run_shell_timer(__unused int fd, __unused short events, void* arg)
143 {
144 	struct cmd_run_shell_data	*cdata = arg;
145 
146 	if (cdata->cmd != NULL) {
147 		if (job_run(cdata->cmd, cdata->s, cdata->cwd, NULL,
148 		    cmd_run_shell_callback, cmd_run_shell_free, cdata, 0, -1,
149 		    -1) == NULL)
150 			cmd_run_shell_free(cdata);
151 	} else {
152 		if (cdata->item != NULL)
153 			cmdq_continue(cdata->item);
154 		cmd_run_shell_free(cdata);
155 	}
156 }
157 
158 static void
159 cmd_run_shell_callback(struct job *job)
160 {
161 	struct cmd_run_shell_data	*cdata = job_get_data(job);
162 	struct bufferevent		*event = job_get_event(job);
163 	struct cmdq_item		*item = cdata->item;
164 	char				*cmd = cdata->cmd, *msg = NULL, *line;
165 	size_t				 size;
166 	int				 retcode, status;
167 
168 	do {
169 		if ((line = evbuffer_readline(event->input)) != NULL) {
170 			cmd_run_shell_print(job, line);
171 			free(line);
172 		}
173 	} while (line != NULL);
174 
175 	size = EVBUFFER_LENGTH(event->input);
176 	if (size != 0) {
177 		line = xmalloc(size + 1);
178 		memcpy(line, EVBUFFER_DATA(event->input), size);
179 		line[size] = '\0';
180 
181 		cmd_run_shell_print(job, line);
182 
183 		free(line);
184 	}
185 
186 	status = job_get_status(job);
187 	if (WIFEXITED(status)) {
188 		if ((retcode = WEXITSTATUS(status)) != 0)
189 			xasprintf(&msg, "'%s' returned %d", cmd, retcode);
190 	} else if (WIFSIGNALED(status)) {
191 		retcode = WTERMSIG(status);
192 		xasprintf(&msg, "'%s' terminated by signal %d", cmd, retcode);
193 		retcode += 128;
194 	}
195 	if (msg != NULL)
196 		cmd_run_shell_print(job, msg);
197 	free(msg);
198 
199 	if (item != NULL) {
200 		if (item->client != NULL && item->client->session == NULL)
201 			item->client->retval = retcode;
202 		cmdq_continue(item);
203 	}
204 }
205 
206 static void
207 cmd_run_shell_free(void *data)
208 {
209 	struct cmd_run_shell_data	*cdata = data;
210 
211 	evtimer_del(&cdata->timer);
212 	if (cdata->s != NULL)
213 		session_remove_ref(cdata->s, __func__);
214 	free(cdata->cwd);
215 	free(cdata->cmd);
216 	free(cdata);
217 }
218