Annotation of src/usr.bin/tmux/cmd-run-shell.c, Revision 1.74
1.74 ! nicm 1: /* $OpenBSD: cmd-run-shell.c,v 1.73 2021/04/12 09:36:12 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.70 nicm 44: .args = { "bd:Ct:", 0, 1 },
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.70 nicm 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);
1.40 nicm 117:
1.37 nicm 118: cdata = xcalloc(1, sizeof *cdata);
1.60 nicm 119: if (args->argc != 0)
1.67 nicm 120: cdata->cmd = format_single_from_target(item, args->argv[0]);
1.2 nicm 121:
1.70 nicm 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:
1.40 nicm 131: if (args_has(args, 't') && wp != NULL)
132: cdata->wp_id = wp->id;
133: else
134: cdata->wp_id = -1;
135:
1.70 nicm 136: if (wait) {
137: cdata->client = cmdq_get_client(item);
1.41 nicm 138: cdata->item = item;
1.70 nicm 139: } else {
140: cdata->client = tc;
1.69 nicm 141: cdata->flags |= JOB_NOWAIT;
1.70 nicm 142: }
143: if (cdata->client != NULL)
144: cdata->client->references++;
1.2 nicm 145:
1.66 nicm 146: cdata->cwd = xstrdup(server_client_get_cwd(cmdq_get_client(item), s));
1.70 nicm 147:
1.60 nicm 148: cdata->s = s;
1.61 nicm 149: if (s != NULL)
150: session_add_ref(s, __func__);
1.60 nicm 151:
152: evtimer_set(&cdata->timer, cmd_run_shell_timer, cdata);
1.70 nicm 153: if (delay != NULL) {
1.60 nicm 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
1.70 nicm 159: event_active(&cdata->timer, EV_TIMEOUT, 1);
1.2 nicm 160:
1.70 nicm 161: if (!wait)
1.18 nicm 162: return (CMD_RETURN_NORMAL);
163: return (CMD_RETURN_WAIT);
1.2 nicm 164: }
1.1 nicm 165:
1.38 nicm 166: static void
1.60 nicm 167: cmd_run_shell_timer(__unused int fd, __unused short events, void* arg)
168: {
169: struct cmd_run_shell_data *cdata = arg;
1.70 nicm 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;
1.60 nicm 175:
1.70 nicm 176: if (cmd != NULL && cdata->shell) {
1.71 nicm 177: if (job_run(cmd, 0, NULL, cdata->s, cdata->cwd, NULL,
1.69 nicm 178: cmd_run_shell_callback, cmd_run_shell_free, cdata,
179: cdata->flags, -1, -1) == NULL)
1.60 nicm 180: cmd_run_shell_free(cdata);
1.70 nicm 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) {
1.72 nicm 193: if (cdata->item == NULL) {
194: *error = toupper((u_char)*error);
1.73 nicm 195: status_message_set(c, -1, 1, 0, "%s", error);
1.72 nicm 196: } else
197: cmdq_error(cdata->item, "%s", error);
198: free(error);
1.70 nicm 199: }
1.60 nicm 200: }
1.70 nicm 201:
202: if (cdata->item != NULL)
203: cmdq_continue(cdata->item);
204: cmd_run_shell_free(cdata);
1.60 nicm 205: }
206:
207: static void
1.2 nicm 208: cmd_run_shell_callback(struct job *job)
209: {
1.54 nicm 210: struct cmd_run_shell_data *cdata = job_get_data(job);
211: struct bufferevent *event = job_get_event(job);
1.63 nicm 212: struct cmdq_item *item = cdata->item;
1.54 nicm 213: char *cmd = cdata->cmd, *msg = NULL, *line;
1.5 nicm 214: size_t size;
1.54 nicm 215: int retcode, status;
1.7 nicm 216:
1.5 nicm 217: do {
1.54 nicm 218: if ((line = evbuffer_readline(event->input)) != NULL) {
1.18 nicm 219: cmd_run_shell_print(job, line);
1.16 nicm 220: free(line);
1.5 nicm 221: }
222: } while (line != NULL);
223:
1.54 nicm 224: size = EVBUFFER_LENGTH(event->input);
1.5 nicm 225: if (size != 0) {
226: line = xmalloc(size + 1);
1.54 nicm 227: memcpy(line, EVBUFFER_DATA(event->input), size);
1.5 nicm 228: line[size] = '\0';
1.2 nicm 229:
1.14 nicm 230: cmd_run_shell_print(job, line);
1.2 nicm 231:
1.12 nicm 232: free(line);
1.1 nicm 233: }
234:
1.54 nicm 235: status = job_get_status(job);
236: if (WIFEXITED(status)) {
237: if ((retcode = WEXITSTATUS(status)) != 0)
1.2 nicm 238: xasprintf(&msg, "'%s' returned %d", cmd, retcode);
1.54 nicm 239: } else if (WIFSIGNALED(status)) {
240: retcode = WTERMSIG(status);
1.2 nicm 241: xasprintf(&msg, "'%s' terminated by signal %d", cmd, retcode);
1.63 nicm 242: retcode += 128;
1.68 nicm 243: } else
244: retcode = 0;
1.25 nicm 245: if (msg != NULL)
246: cmd_run_shell_print(job, msg);
247: free(msg);
1.40 nicm 248:
1.63 nicm 249: if (item != NULL) {
1.66 nicm 250: if (cmdq_get_client(item) != NULL &&
251: cmdq_get_client(item)->session == NULL)
252: cmdq_get_client(item)->retval = retcode;
1.63 nicm 253: cmdq_continue(item);
254: }
1.2 nicm 255: }
256:
1.38 nicm 257: static void
1.2 nicm 258: cmd_run_shell_free(void *data)
259: {
260: struct cmd_run_shell_data *cdata = data;
261:
1.60 nicm 262: evtimer_del(&cdata->timer);
1.61 nicm 263: if (cdata->s != NULL)
264: session_remove_ref(cdata->s, __func__);
1.70 nicm 265: if (cdata->client != NULL)
266: server_client_unref(cdata->client);
1.60 nicm 267: free(cdata->cwd);
1.12 nicm 268: free(cdata->cmd);
269: free(cdata);
1.1 nicm 270: }