Annotation of src/usr.bin/tmux/cmd-run-shell.c, Revision 1.22
1.22 ! nicm 1: /* $OpenBSD: cmd-run-shell.c,v 1.21 2013/03/25 11:36:42 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.18 nicm 32: enum cmd_retval cmd_run_shell_exec(struct cmd *, struct cmd_q *);
1.14 nicm 33:
34: void cmd_run_shell_callback(struct job *);
35: void cmd_run_shell_free(void *);
36: 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 = {
39: "run-shell", "run",
1.18 nicm 40: "bt:", 1, 1,
41: "[-b] " CMD_TARGET_PANE_USAGE " shell-command",
1.10 nicm 42: 0,
43: NULL,
44: NULL,
45: cmd_run_shell_exec
1.1 nicm 46: };
47:
1.2 nicm 48: struct cmd_run_shell_data {
49: char *cmd;
1.18 nicm 50: struct cmd_q *cmdq;
51: int bflag;
1.21 nicm 52: int wp_id;
1.2 nicm 53: };
54:
1.14 nicm 55: void
56: cmd_run_shell_print(struct job *job, const char *msg)
57: {
58: struct cmd_run_shell_data *cdata = job->data;
1.21 nicm 59: struct window_pane *wp = NULL;
1.14 nicm 60:
1.21 nicm 61: if (cdata->wp_id != -1)
62: wp = window_pane_find_by_id(cdata->wp_id);
1.14 nicm 63: if (wp == NULL) {
1.18 nicm 64: cmdq_print(cdata->cmdq, "%s", msg);
1.14 nicm 65: return;
66: }
67:
68: if (window_pane_set_mode(wp, &window_copy_mode) == 0)
69: window_copy_init_for_output(wp);
70: if (wp->mode == &window_copy_mode)
71: window_copy_add(wp, "%s", msg);
72: }
73:
1.13 nicm 74: enum cmd_retval
1.18 nicm 75: cmd_run_shell_exec(struct cmd *self, struct cmd_q *cmdq)
1.1 nicm 76: {
1.10 nicm 77: struct args *args = self->args;
1.2 nicm 78: struct cmd_run_shell_data *cdata;
1.17 nicm 79: char *shellcmd;
1.21 nicm 80: struct session *s = NULL;
81: struct winlink *wl = NULL;
82: struct window_pane *wp = NULL;
1.17 nicm 83: struct format_tree *ft;
1.14 nicm 84:
1.21 nicm 85: if (args_has(args, 't'))
86: wl = cmd_find_pane(cmdq, args_get(args, 't'), &s, &wp);
1.2 nicm 87:
1.17 nicm 88: ft = format_create();
1.21 nicm 89: if (s != NULL)
90: format_session(ft, s);
91: if (s != NULL && wl != NULL)
92: format_winlink(ft, s, wl);
93: if (wp != NULL)
94: format_window_pane(ft, wp);
1.17 nicm 95: shellcmd = format_expand(ft, args->argv[0]);
96: format_free(ft);
97:
1.2 nicm 98: cdata = xmalloc(sizeof *cdata);
1.17 nicm 99: cdata->cmd = shellcmd;
1.18 nicm 100: cdata->bflag = args_has(args, 'b');
1.21 nicm 101: cdata->wp_id = wp != NULL ? (int) wp->id : -1;
1.2 nicm 102:
1.18 nicm 103: cdata->cmdq = cmdq;
104: cmdq->references++;
1.2 nicm 105:
1.22 ! nicm 106: job_run(shellcmd, s, cmd_run_shell_callback, cmd_run_shell_free, cdata);
1.2 nicm 107:
1.18 nicm 108: if (cdata->bflag)
109: return (CMD_RETURN_NORMAL);
110: return (CMD_RETURN_WAIT);
1.2 nicm 111: }
1.1 nicm 112:
1.2 nicm 113: void
114: cmd_run_shell_callback(struct job *job)
115: {
116: struct cmd_run_shell_data *cdata = job->data;
1.18 nicm 117: struct cmd_q *cmdq = cdata->cmdq;
1.5 nicm 118: char *cmd, *msg, *line;
119: size_t size;
1.2 nicm 120: int retcode;
1.5 nicm 121: u_int lines;
1.7 nicm 122:
1.18 nicm 123: if (cmdq->dead)
1.7 nicm 124: return;
1.18 nicm 125: cmd = cdata->cmd;
1.2 nicm 126:
1.5 nicm 127: lines = 0;
128: do {
129: if ((line = evbuffer_readline(job->event->input)) != NULL) {
1.18 nicm 130: cmd_run_shell_print(job, line);
1.16 nicm 131: free(line);
1.5 nicm 132: lines++;
133: }
134: } while (line != NULL);
135:
136: size = EVBUFFER_LENGTH(job->event->input);
137: if (size != 0) {
138: line = xmalloc(size + 1);
139: memcpy(line, EVBUFFER_DATA(job->event->input), size);
140: line[size] = '\0';
1.2 nicm 141:
1.14 nicm 142: cmd_run_shell_print(job, line);
1.5 nicm 143: lines++;
1.2 nicm 144:
1.12 nicm 145: free(line);
1.1 nicm 146: }
147:
148: msg = NULL;
1.2 nicm 149: if (WIFEXITED(job->status)) {
150: if ((retcode = WEXITSTATUS(job->status)) != 0)
151: xasprintf(&msg, "'%s' returned %d", cmd, retcode);
152: } else if (WIFSIGNALED(job->status)) {
153: retcode = WTERMSIG(job->status);
154: xasprintf(&msg, "'%s' terminated by signal %d", cmd, retcode);
1.1 nicm 155: }
156: if (msg != NULL) {
1.14 nicm 157: if (lines == 0)
1.18 nicm 158: cmdq_info(cmdq, "%s", msg);
1.1 nicm 159: else
1.14 nicm 160: cmd_run_shell_print(job, msg);
1.12 nicm 161: free(msg);
1.1 nicm 162: }
1.2 nicm 163: }
164:
165: void
166: cmd_run_shell_free(void *data)
167: {
168: struct cmd_run_shell_data *cdata = data;
1.18 nicm 169: struct cmd_q *cmdq = cdata->cmdq;
1.2 nicm 170:
1.18 nicm 171: if (!cmdq_free(cmdq) && !cdata->bflag)
172: cmdq_continue(cmdq);
1.2 nicm 173:
1.12 nicm 174: free(cdata->cmd);
175: free(cdata);
1.1 nicm 176: }