1 /* $OpenBSD$ */ 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 enum cmd_retval cmd_if_shell_error(struct cmdq_item *, void *); 35 static void cmd_if_shell_callback(struct job *); 36 static void cmd_if_shell_free(void *); 37 38 const struct cmd_entry cmd_if_shell_entry = { 39 .name = "if-shell", 40 .alias = "if", 41 42 .args = { "bFt:", 2, 3 }, 43 .usage = "[-bF] " CMD_TARGET_PANE_USAGE " shell-command command " 44 "[command]", 45 46 .tflag = CMD_PANE_CANFAIL, 47 48 .flags = 0, 49 .exec = cmd_if_shell_exec 50 }; 51 52 struct cmd_if_shell_data { 53 char *file; 54 u_int line; 55 56 char *cmd_if; 57 char *cmd_else; 58 59 struct client *client; 60 struct cmdq_item *item; 61 struct mouse_event mouse; 62 }; 63 64 static enum cmd_retval 65 cmd_if_shell_exec(struct cmd *self, struct cmdq_item *item) 66 { 67 struct args *args = self->args; 68 struct cmd_if_shell_data *cdata; 69 char *shellcmd, *cmd, *cause; 70 struct cmd_list *cmdlist; 71 struct cmdq_item *new_item; 72 struct client *c = item->state.c; 73 struct session *s = item->state.tflag.s; 74 struct winlink *wl = item->state.tflag.wl; 75 struct window_pane *wp = item->state.tflag.wp; 76 const char *cwd; 77 78 if (item->client != NULL && item->client->session == NULL) 79 cwd = item->client->cwd; 80 else if (s != NULL) 81 cwd = s->cwd; 82 else 83 cwd = NULL; 84 85 shellcmd = format_single(item, args->argv[0], c, s, wl, wp); 86 if (args_has(args, 'F')) { 87 cmd = NULL; 88 if (*shellcmd != '0' && *shellcmd != '\0') 89 cmd = args->argv[1]; 90 else if (args->argc == 3) 91 cmd = args->argv[2]; 92 free(shellcmd); 93 if (cmd == NULL) 94 return (CMD_RETURN_NORMAL); 95 cmdlist = cmd_string_parse(cmd, NULL, 0, &cause); 96 if (cmdlist == NULL) { 97 if (cause != NULL) { 98 cmdq_error(item, "%s", cause); 99 free(cause); 100 } 101 return (CMD_RETURN_ERROR); 102 } 103 new_item = cmdq_get_command(cmdlist, NULL, &item->mouse, 0); 104 cmdq_insert_after(item, new_item); 105 cmd_list_free(cmdlist); 106 return (CMD_RETURN_NORMAL); 107 } 108 109 cdata = xcalloc(1, sizeof *cdata); 110 if (self->file != NULL) { 111 cdata->file = xstrdup(self->file); 112 cdata->line = self->line; 113 } 114 115 cdata->cmd_if = xstrdup(args->argv[1]); 116 if (args->argc == 3) 117 cdata->cmd_else = xstrdup(args->argv[2]); 118 else 119 cdata->cmd_else = NULL; 120 121 cdata->client = item->client; 122 cdata->client->references++; 123 124 if (!args_has(args, 'b')) 125 cdata->item = item; 126 else 127 cdata->item = NULL; 128 memcpy(&cdata->mouse, &item->mouse, sizeof cdata->mouse); 129 130 job_run(shellcmd, s, cwd, cmd_if_shell_callback, cmd_if_shell_free, 131 cdata); 132 free(shellcmd); 133 134 if (args_has(args, 'b')) 135 return (CMD_RETURN_NORMAL); 136 return (CMD_RETURN_WAIT); 137 } 138 139 static enum cmd_retval 140 cmd_if_shell_error(struct cmdq_item *item, void *data) 141 { 142 char *error = data; 143 144 cmdq_error(item, "%s", error); 145 free(error); 146 147 return (CMD_RETURN_NORMAL); 148 } 149 150 static void 151 cmd_if_shell_callback(struct job *job) 152 { 153 struct cmd_if_shell_data *cdata = job->data; 154 struct client *c = cdata->client; 155 struct cmd_list *cmdlist; 156 struct cmdq_item *new_item; 157 char *cause, *cmd, *file = cdata->file; 158 u_int line = cdata->line; 159 160 if (!WIFEXITED(job->status) || WEXITSTATUS(job->status) != 0) 161 cmd = cdata->cmd_else; 162 else 163 cmd = cdata->cmd_if; 164 if (cmd == NULL) 165 goto out; 166 167 cmdlist = cmd_string_parse(cmd, file, line, &cause); 168 if (cmdlist == NULL) { 169 if (cause != NULL) 170 new_item = cmdq_get_callback(cmd_if_shell_error, cause); 171 else 172 new_item = NULL; 173 } else { 174 new_item = cmdq_get_command(cmdlist, NULL, &cdata->mouse, 0); 175 cmd_list_free(cmdlist); 176 } 177 178 if (new_item != NULL) { 179 if (cdata->item == NULL) 180 cmdq_append(c, new_item); 181 else 182 cmdq_insert_after(cdata->item, new_item); 183 } 184 185 out: 186 if (cdata->item != NULL) 187 cdata->item->flags &= ~CMDQ_WAITING; 188 } 189 190 static void 191 cmd_if_shell_free(void *data) 192 { 193 struct cmd_if_shell_data *cdata = data; 194 195 server_client_unref(cdata->client); 196 197 free(cdata->cmd_else); 198 free(cdata->cmd_if); 199 200 free(cdata->file); 201 free(cdata); 202 } 203