1 /* $OpenBSD: cmd-run-shell.c,v 1.26 2014/10/20 22:29:25 nicm Exp $ */ 2 3 /* 4 * Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org> 5 * Copyright (c) 2009 Nicholas Marriott <nicm@openbsd.org> 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 23 #include <stdlib.h> 24 #include <string.h> 25 26 #include "tmux.h" 27 28 /* 29 * Runs a command without a window. 30 */ 31 32 enum cmd_retval cmd_run_shell_exec(struct cmd *, struct cmd_q *); 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 *); 37 38 const struct cmd_entry cmd_run_shell_entry = { 39 "run-shell", "run", 40 "bt:", 1, 1, 41 "[-b] " CMD_TARGET_PANE_USAGE " shell-command", 42 0, 43 cmd_run_shell_exec 44 }; 45 46 struct cmd_run_shell_data { 47 char *cmd; 48 struct cmd_q *cmdq; 49 int bflag; 50 int wp_id; 51 }; 52 53 void 54 cmd_run_shell_print(struct job *job, const char *msg) 55 { 56 struct cmd_run_shell_data *cdata = job->data; 57 struct window_pane *wp = NULL; 58 59 if (cdata->wp_id != -1) 60 wp = window_pane_find_by_id(cdata->wp_id); 61 if (wp == NULL) { 62 cmdq_print(cdata->cmdq, "%s", msg); 63 return; 64 } 65 66 if (window_pane_set_mode(wp, &window_copy_mode) == 0) 67 window_copy_init_for_output(wp); 68 if (wp->mode == &window_copy_mode) 69 window_copy_add(wp, "%s", msg); 70 } 71 72 enum cmd_retval 73 cmd_run_shell_exec(struct cmd *self, struct cmd_q *cmdq) 74 { 75 struct args *args = self->args; 76 struct cmd_run_shell_data *cdata; 77 char *shellcmd; 78 struct client *c; 79 struct session *s = NULL; 80 struct winlink *wl = NULL; 81 struct window_pane *wp = NULL; 82 struct format_tree *ft; 83 84 if (args_has(args, 't')) 85 wl = cmd_find_pane(cmdq, args_get(args, 't'), &s, &wp); 86 else { 87 c = cmd_find_client(cmdq, NULL, 1); 88 if (c != NULL && c->session != NULL) { 89 s = c->session; 90 wl = s->curw; 91 wp = wl->window->active; 92 } 93 } 94 95 ft = format_create(); 96 if (s != NULL) 97 format_session(ft, s); 98 if (s != NULL && wl != NULL) 99 format_winlink(ft, s, wl); 100 if (wp != NULL) 101 format_window_pane(ft, wp); 102 shellcmd = format_expand(ft, args->argv[0]); 103 format_free(ft); 104 105 cdata = xmalloc(sizeof *cdata); 106 cdata->cmd = shellcmd; 107 cdata->bflag = args_has(args, 'b'); 108 cdata->wp_id = wp != NULL ? (int) wp->id : -1; 109 110 cdata->cmdq = cmdq; 111 cmdq->references++; 112 113 job_run(shellcmd, s, cmd_run_shell_callback, cmd_run_shell_free, cdata); 114 115 if (cdata->bflag) 116 return (CMD_RETURN_NORMAL); 117 return (CMD_RETURN_WAIT); 118 } 119 120 void 121 cmd_run_shell_callback(struct job *job) 122 { 123 struct cmd_run_shell_data *cdata = job->data; 124 struct cmd_q *cmdq = cdata->cmdq; 125 char *cmd, *msg, *line; 126 size_t size; 127 int retcode; 128 u_int lines; 129 130 if (cmdq->dead) 131 return; 132 cmd = cdata->cmd; 133 134 lines = 0; 135 do { 136 if ((line = evbuffer_readline(job->event->input)) != NULL) { 137 cmd_run_shell_print(job, line); 138 free(line); 139 lines++; 140 } 141 } while (line != NULL); 142 143 size = EVBUFFER_LENGTH(job->event->input); 144 if (size != 0) { 145 line = xmalloc(size + 1); 146 memcpy(line, EVBUFFER_DATA(job->event->input), size); 147 line[size] = '\0'; 148 149 cmd_run_shell_print(job, line); 150 lines++; 151 152 free(line); 153 } 154 155 msg = NULL; 156 if (WIFEXITED(job->status)) { 157 if ((retcode = WEXITSTATUS(job->status)) != 0) 158 xasprintf(&msg, "'%s' returned %d", cmd, retcode); 159 } else if (WIFSIGNALED(job->status)) { 160 retcode = WTERMSIG(job->status); 161 xasprintf(&msg, "'%s' terminated by signal %d", cmd, retcode); 162 } 163 if (msg != NULL) 164 cmd_run_shell_print(job, msg); 165 free(msg); 166 } 167 168 void 169 cmd_run_shell_free(void *data) 170 { 171 struct cmd_run_shell_data *cdata = data; 172 struct cmd_q *cmdq = cdata->cmdq; 173 174 if (!cmdq_free(cmdq) && !cdata->bflag) 175 cmdq_continue(cmdq); 176 177 free(cdata->cmd); 178 free(cdata); 179 } 180