Annotation of src/usr.bin/tmux/cmd-run-shell.c, Revision 1.42
1.42 ! nicm 1: /* $OpenBSD: cmd-run-shell.c,v 1.41 2016/10/16 19:04:05 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.12 nicm 23: #include <stdlib.h>
1.1 nicm 24: #include <string.h>
25:
26: #include "tmux.h"
27:
28: /*
29: * Runs a command without a window.
30: */
31:
1.41 nicm 32: static enum cmd_retval cmd_run_shell_exec(struct cmd *, struct cmdq_item *);
1.14 nicm 33:
1.38 nicm 34: static void cmd_run_shell_callback(struct job *);
35: static void cmd_run_shell_free(void *);
36: static void cmd_run_shell_print(struct job *, const char *);
1.2 nicm 37:
1.1 nicm 38: const struct cmd_entry cmd_run_shell_entry = {
1.34 nicm 39: .name = "run-shell",
40: .alias = "run",
41:
42: .args = { "bt:", 1, 1 },
43: .usage = "[-b] " CMD_TARGET_PANE_USAGE " shell-command",
44:
1.35 nicm 45: .tflag = CMD_PANE_CANFAIL,
46:
47: .flags = 0,
1.34 nicm 48: .exec = cmd_run_shell_exec
1.1 nicm 49: };
50:
1.2 nicm 51: struct cmd_run_shell_data {
1.41 nicm 52: char *cmd;
53: struct cmdq_item *item;
54: int wp_id;
1.2 nicm 55: };
56:
1.38 nicm 57: static void
1.14 nicm 58: cmd_run_shell_print(struct job *job, const char *msg)
59: {
60: struct cmd_run_shell_data *cdata = job->data;
1.21 nicm 61: struct window_pane *wp = NULL;
1.14 nicm 62:
1.21 nicm 63: if (cdata->wp_id != -1)
64: wp = window_pane_find_by_id(cdata->wp_id);
1.42 ! nicm 65: if (cdata->item != NULL && wp == NULL) {
1.41 nicm 66: cmdq_print(cdata->item, "%s", msg);
1.14 nicm 67: return;
68: }
69:
70: if (window_pane_set_mode(wp, &window_copy_mode) == 0)
71: window_copy_init_for_output(wp);
72: if (wp->mode == &window_copy_mode)
73: window_copy_add(wp, "%s", msg);
74: }
75:
1.38 nicm 76: static enum cmd_retval
1.41 nicm 77: cmd_run_shell_exec(struct cmd *self, struct cmdq_item *item)
1.1 nicm 78: {
1.10 nicm 79: struct args *args = self->args;
1.2 nicm 80: struct cmd_run_shell_data *cdata;
1.17 nicm 81: char *shellcmd;
1.41 nicm 82: struct session *s = item->state.tflag.s;
83: struct winlink *wl = item->state.tflag.wl;
84: struct window_pane *wp = item->state.tflag.wp;
1.17 nicm 85: struct format_tree *ft;
1.30 nicm 86: const char *cwd;
1.14 nicm 87:
1.41 nicm 88: if (item->client != NULL && item->client->session == NULL)
89: cwd = item->client->cwd;
1.33 nicm 90: else if (s != NULL)
91: cwd = s->cwd;
92: else
93: cwd = NULL;
1.40 nicm 94:
1.41 nicm 95: ft = format_create(item, 0);
96: format_defaults(ft, item->state.c, s, wl, wp);
1.17 nicm 97: shellcmd = format_expand(ft, args->argv[0]);
98: format_free(ft);
99:
1.37 nicm 100: cdata = xcalloc(1, sizeof *cdata);
1.17 nicm 101: cdata->cmd = shellcmd;
1.2 nicm 102:
1.40 nicm 103: if (args_has(args, 't') && wp != NULL)
104: cdata->wp_id = wp->id;
105: else
106: cdata->wp_id = -1;
107:
108: if (!args_has(args, 'b'))
1.41 nicm 109: cdata->item = item;
1.2 nicm 110:
1.28 nicm 111: job_run(shellcmd, s, cwd, cmd_run_shell_callback, cmd_run_shell_free,
112: cdata);
1.2 nicm 113:
1.40 nicm 114: if (args_has(args, 'b'))
1.18 nicm 115: return (CMD_RETURN_NORMAL);
116: return (CMD_RETURN_WAIT);
1.2 nicm 117: }
1.1 nicm 118:
1.38 nicm 119: static void
1.2 nicm 120: cmd_run_shell_callback(struct job *job)
121: {
122: struct cmd_run_shell_data *cdata = job->data;
1.40 nicm 123: char *cmd = cdata->cmd, *msg, *line;
1.5 nicm 124: size_t size;
1.2 nicm 125: int retcode;
1.5 nicm 126: u_int lines;
1.7 nicm 127:
1.5 nicm 128: lines = 0;
129: do {
130: if ((line = evbuffer_readline(job->event->input)) != NULL) {
1.18 nicm 131: cmd_run_shell_print(job, line);
1.16 nicm 132: free(line);
1.5 nicm 133: lines++;
134: }
135: } while (line != NULL);
136:
137: size = EVBUFFER_LENGTH(job->event->input);
138: if (size != 0) {
139: line = xmalloc(size + 1);
140: memcpy(line, EVBUFFER_DATA(job->event->input), size);
141: line[size] = '\0';
1.2 nicm 142:
1.14 nicm 143: cmd_run_shell_print(job, line);
1.5 nicm 144: lines++;
1.2 nicm 145:
1.12 nicm 146: free(line);
1.1 nicm 147: }
148:
149: msg = NULL;
1.2 nicm 150: if (WIFEXITED(job->status)) {
151: if ((retcode = WEXITSTATUS(job->status)) != 0)
152: xasprintf(&msg, "'%s' returned %d", cmd, retcode);
153: } else if (WIFSIGNALED(job->status)) {
154: retcode = WTERMSIG(job->status);
155: xasprintf(&msg, "'%s' terminated by signal %d", cmd, retcode);
1.1 nicm 156: }
1.25 nicm 157: if (msg != NULL)
158: cmd_run_shell_print(job, msg);
159: free(msg);
1.40 nicm 160:
1.41 nicm 161: if (cdata->item != NULL)
162: cdata->item->flags &= ~CMDQ_WAITING;
1.2 nicm 163: }
164:
1.38 nicm 165: static void
1.2 nicm 166: cmd_run_shell_free(void *data)
167: {
168: struct cmd_run_shell_data *cdata = data;
169:
1.12 nicm 170: free(cdata->cmd);
171: free(cdata);
1.1 nicm 172: }