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