Annotation of src/usr.bin/tmux/cmd-run-shell.c, Revision 1.43
1.43 ! nicm 1: /* $OpenBSD: cmd-run-shell.c,v 1.42 2016/11/11 11:37:25 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.43 ! nicm 62: struct cmd_find_state fs;
1.14 nicm 63:
1.21 nicm 64: if (cdata->wp_id != -1)
65: wp = window_pane_find_by_id(cdata->wp_id);
1.43 ! nicm 66: if (wp == NULL) {
! 67: if (cdata->item != NULL) {
! 68: cmdq_print(cdata->item, "%s", msg);
! 69: return;
! 70: }
! 71: if (cmd_find_current (&fs, NULL, CMD_FIND_QUIET) != 0)
! 72: return;
! 73: wp = fs.wp;
! 74: if (wp == NULL)
! 75: return;
1.14 nicm 76: }
77:
78: if (window_pane_set_mode(wp, &window_copy_mode) == 0)
79: window_copy_init_for_output(wp);
80: if (wp->mode == &window_copy_mode)
81: window_copy_add(wp, "%s", msg);
82: }
83:
1.38 nicm 84: static enum cmd_retval
1.41 nicm 85: cmd_run_shell_exec(struct cmd *self, struct cmdq_item *item)
1.1 nicm 86: {
1.10 nicm 87: struct args *args = self->args;
1.2 nicm 88: struct cmd_run_shell_data *cdata;
1.17 nicm 89: char *shellcmd;
1.41 nicm 90: struct session *s = item->state.tflag.s;
91: struct winlink *wl = item->state.tflag.wl;
92: struct window_pane *wp = item->state.tflag.wp;
1.17 nicm 93: struct format_tree *ft;
1.30 nicm 94: const char *cwd;
1.14 nicm 95:
1.41 nicm 96: if (item->client != NULL && item->client->session == NULL)
97: cwd = item->client->cwd;
1.33 nicm 98: else if (s != NULL)
99: cwd = s->cwd;
100: else
101: cwd = NULL;
1.40 nicm 102:
1.41 nicm 103: ft = format_create(item, 0);
104: format_defaults(ft, item->state.c, s, wl, wp);
1.17 nicm 105: shellcmd = format_expand(ft, args->argv[0]);
106: format_free(ft);
107:
1.37 nicm 108: cdata = xcalloc(1, sizeof *cdata);
1.17 nicm 109: cdata->cmd = shellcmd;
1.2 nicm 110:
1.40 nicm 111: if (args_has(args, 't') && wp != NULL)
112: cdata->wp_id = wp->id;
113: else
114: cdata->wp_id = -1;
115:
116: if (!args_has(args, 'b'))
1.41 nicm 117: cdata->item = item;
1.2 nicm 118:
1.28 nicm 119: job_run(shellcmd, s, cwd, cmd_run_shell_callback, cmd_run_shell_free,
120: cdata);
1.2 nicm 121:
1.40 nicm 122: if (args_has(args, 'b'))
1.18 nicm 123: return (CMD_RETURN_NORMAL);
124: return (CMD_RETURN_WAIT);
1.2 nicm 125: }
1.1 nicm 126:
1.38 nicm 127: static void
1.2 nicm 128: cmd_run_shell_callback(struct job *job)
129: {
130: struct cmd_run_shell_data *cdata = job->data;
1.40 nicm 131: char *cmd = cdata->cmd, *msg, *line;
1.5 nicm 132: size_t size;
1.2 nicm 133: int retcode;
1.5 nicm 134: u_int lines;
1.7 nicm 135:
1.5 nicm 136: lines = 0;
137: do {
138: if ((line = evbuffer_readline(job->event->input)) != NULL) {
1.18 nicm 139: cmd_run_shell_print(job, line);
1.16 nicm 140: free(line);
1.5 nicm 141: lines++;
142: }
143: } while (line != NULL);
144:
145: size = EVBUFFER_LENGTH(job->event->input);
146: if (size != 0) {
147: line = xmalloc(size + 1);
148: memcpy(line, EVBUFFER_DATA(job->event->input), size);
149: line[size] = '\0';
1.2 nicm 150:
1.14 nicm 151: cmd_run_shell_print(job, line);
1.5 nicm 152: lines++;
1.2 nicm 153:
1.12 nicm 154: free(line);
1.1 nicm 155: }
156:
157: msg = NULL;
1.2 nicm 158: if (WIFEXITED(job->status)) {
159: if ((retcode = WEXITSTATUS(job->status)) != 0)
160: xasprintf(&msg, "'%s' returned %d", cmd, retcode);
161: } else if (WIFSIGNALED(job->status)) {
162: retcode = WTERMSIG(job->status);
163: xasprintf(&msg, "'%s' terminated by signal %d", cmd, retcode);
1.1 nicm 164: }
1.25 nicm 165: if (msg != NULL)
166: cmd_run_shell_print(job, msg);
167: free(msg);
1.40 nicm 168:
1.41 nicm 169: if (cdata->item != NULL)
170: cdata->item->flags &= ~CMDQ_WAITING;
1.2 nicm 171: }
172:
1.38 nicm 173: static void
1.2 nicm 174: cmd_run_shell_free(void *data)
175: {
176: struct cmd_run_shell_data *cdata = data;
177:
1.12 nicm 178: free(cdata->cmd);
179: free(cdata);
1.1 nicm 180: }