1 /* $OpenBSD: cmd-if-shell.c,v 1.61 2019/05/23 11:13:30 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 * Executes a tmux command if a shell command returns true or false. 30 */ 31 32 static enum cmd_retval cmd_if_shell_exec(struct cmd *, struct cmdq_item *); 33 34 static void cmd_if_shell_callback(struct job *); 35 static void cmd_if_shell_free(void *); 36 37 const struct cmd_entry cmd_if_shell_entry = { 38 .name = "if-shell", 39 .alias = "if", 40 41 .args = { "bFt:", 2, 3 }, 42 .usage = "[-bF] " CMD_TARGET_PANE_USAGE " shell-command command " 43 "[command]", 44 45 .target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL }, 46 47 .flags = 0, 48 .exec = cmd_if_shell_exec 49 }; 50 51 struct cmd_if_shell_data { 52 struct cmd_parse_input input; 53 54 char *cmd_if; 55 char *cmd_else; 56 57 struct client *client; 58 struct cmdq_item *item; 59 struct mouse_event mouse; 60 }; 61 62 static enum cmd_retval 63 cmd_if_shell_exec(struct cmd *self, struct cmdq_item *item) 64 { 65 struct args *args = self->args; 66 struct mouse_event *m = &item->shared->mouse; 67 struct cmd_if_shell_data *cdata; 68 char *shellcmd, *cmd; 69 struct cmdq_item *new_item; 70 struct client *c = cmd_find_client(item, NULL, 1); 71 struct session *s = item->target.s; 72 struct winlink *wl = item->target.wl; 73 struct window_pane *wp = item->target.wp; 74 struct cmd_parse_input pi; 75 struct cmd_parse_result *pr; 76 77 shellcmd = format_single(item, args->argv[0], c, s, wl, wp); 78 if (args_has(args, 'F')) { 79 if (*shellcmd != '0' && *shellcmd != '\0') 80 cmd = args->argv[1]; 81 else if (args->argc == 3) 82 cmd = args->argv[2]; 83 else 84 cmd = NULL; 85 free(shellcmd); 86 if (cmd == NULL) 87 return (CMD_RETURN_NORMAL); 88 89 memset(&pi, 0, sizeof pi); 90 if (self->file != NULL) 91 pi.file = self->file; 92 pi.line = self->line; 93 pi.item = item; 94 pi.c = c; 95 cmd_find_copy_state(&pi.fs, &item->target); 96 97 pr = cmd_parse_from_string(cmd, &pi); 98 switch (pr->status) { 99 case CMD_PARSE_EMPTY: 100 break; 101 case CMD_PARSE_ERROR: 102 cmdq_error(item, "%s", pr->error); 103 free(pr->error); 104 return (CMD_RETURN_ERROR); 105 case CMD_PARSE_SUCCESS: 106 new_item = cmdq_get_command(pr->cmdlist, NULL, m, 0); 107 cmdq_insert_after(item, new_item); 108 cmd_list_free(pr->cmdlist); 109 break; 110 } 111 return (CMD_RETURN_NORMAL); 112 } 113 114 cdata = xcalloc(1, sizeof *cdata); 115 116 cdata->cmd_if = xstrdup(args->argv[1]); 117 if (args->argc == 3) 118 cdata->cmd_else = xstrdup(args->argv[2]); 119 else 120 cdata->cmd_else = NULL; 121 memcpy(&cdata->mouse, m, sizeof cdata->mouse); 122 123 cdata->client = item->client; 124 if (cdata->client != NULL) 125 cdata->client->references++; 126 127 if (!args_has(args, 'b')) 128 cdata->item = item; 129 else 130 cdata->item = NULL; 131 132 memset(&cdata->input, 0, sizeof cdata->input); 133 if (self->file != NULL) 134 cdata->input.file = xstrdup(self->file); 135 cdata->input.line = self->line; 136 cdata->input.item = cdata->item; 137 cdata->input.c = c; 138 if (cdata->input.c != NULL) 139 cdata->input.c->references++; 140 cmd_find_copy_state(&cdata->input.fs, &item->target); 141 142 if (job_run(shellcmd, s, server_client_get_cwd(item->client, s), NULL, 143 cmd_if_shell_callback, cmd_if_shell_free, cdata, 0) == NULL) { 144 cmdq_error(item, "failed to run command: %s", shellcmd); 145 free(shellcmd); 146 free(cdata); 147 return (CMD_RETURN_ERROR); 148 } 149 free(shellcmd); 150 151 if (args_has(args, 'b')) 152 return (CMD_RETURN_NORMAL); 153 return (CMD_RETURN_WAIT); 154 } 155 156 static void 157 cmd_if_shell_callback(struct job *job) 158 { 159 struct cmd_if_shell_data *cdata = job_get_data(job); 160 struct client *c = cdata->client; 161 struct mouse_event *m = &cdata->mouse; 162 struct cmdq_item *new_item; 163 char *cmd; 164 int status; 165 struct cmd_parse_result *pr; 166 167 status = job_get_status(job); 168 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) 169 cmd = cdata->cmd_else; 170 else 171 cmd = cdata->cmd_if; 172 if (cmd == NULL) 173 goto out; 174 175 pr = cmd_parse_from_string(cmd, &cdata->input); 176 switch (pr->status) { 177 case CMD_PARSE_EMPTY: 178 new_item = NULL; 179 break; 180 case CMD_PARSE_ERROR: 181 new_item = cmdq_get_error(pr->error); 182 free(pr->error); 183 break; 184 case CMD_PARSE_SUCCESS: 185 new_item = cmdq_get_command(pr->cmdlist, NULL, m, 0); 186 cmd_list_free(pr->cmdlist); 187 break; 188 } 189 if (new_item != NULL) { 190 if (cdata->item == NULL) 191 cmdq_append(c, new_item); 192 else 193 cmdq_insert_after(cdata->item, new_item); 194 } 195 196 out: 197 if (cdata->item != NULL) 198 cdata->item->flags &= ~CMDQ_WAITING; 199 } 200 201 static void 202 cmd_if_shell_free(void *data) 203 { 204 struct cmd_if_shell_data *cdata = data; 205 206 if (cdata->client != NULL) 207 server_client_unref(cdata->client); 208 209 free(cdata->cmd_else); 210 free(cdata->cmd_if); 211 212 if (cdata->input.c != NULL) 213 server_client_unref(cdata->input.c); 214 free((void *)cdata->input.file); 215 216 free(cdata); 217 } 218