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 enum cmd_retval cmd_if_shell_exec(struct cmd *, struct cmd_q *); 33 34 void cmd_if_shell_callback(struct job *); 35 void cmd_if_shell_done(struct cmd_q *); 36 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 *cmd_if; 54 char *cmd_else; 55 56 struct cmd_q *cmdq; 57 struct mouse_event mouse; 58 59 int bflag; 60 int references; 61 }; 62 63 enum cmd_retval 64 cmd_if_shell_exec(struct cmd *self, struct cmd_q *cmdq) 65 { 66 struct args *args = self->args; 67 struct cmd_if_shell_data *cdata; 68 char *shellcmd, *cmd, *cause; 69 struct cmd_list *cmdlist; 70 struct session *s = cmdq->state.tflag.s; 71 struct winlink *wl = cmdq->state.tflag.wl; 72 struct window_pane *wp = cmdq->state.tflag.wp; 73 struct format_tree *ft; 74 const char *cwd; 75 76 if (cmdq->client != NULL && cmdq->client->session == NULL) 77 cwd = cmdq->client->cwd; 78 else if (s != NULL) 79 cwd = s->cwd; 80 else 81 cwd = NULL; 82 83 ft = format_create(cmdq, 0); 84 format_defaults(ft, NULL, s, wl, wp); 85 shellcmd = format_expand(ft, args->argv[0]); 86 format_free(ft); 87 88 if (args_has(args, 'F')) { 89 cmd = NULL; 90 if (*shellcmd != '0' && *shellcmd != '\0') 91 cmd = args->argv[1]; 92 else if (args->argc == 3) 93 cmd = args->argv[2]; 94 free(shellcmd); 95 if (cmd == NULL) 96 return (CMD_RETURN_NORMAL); 97 if (cmd_string_parse(cmd, &cmdlist, NULL, 0, &cause) != 0) { 98 if (cause != NULL) { 99 cmdq_error(cmdq, "%s", cause); 100 free(cause); 101 } 102 return (CMD_RETURN_ERROR); 103 } 104 cmdq_run(cmdq, cmdlist, &cmdq->item->mouse); 105 cmd_list_free(cmdlist); 106 return (CMD_RETURN_NORMAL); 107 } 108 109 cdata = xmalloc(sizeof *cdata); 110 111 cdata->cmd_if = xstrdup(args->argv[1]); 112 if (args->argc == 3) 113 cdata->cmd_else = xstrdup(args->argv[2]); 114 else 115 cdata->cmd_else = NULL; 116 117 cdata->bflag = args_has(args, 'b'); 118 119 cdata->cmdq = cmdq; 120 memcpy(&cdata->mouse, &cmdq->item->mouse, sizeof cdata->mouse); 121 cmdq->references++; 122 123 cdata->references = 1; 124 job_run(shellcmd, s, cwd, cmd_if_shell_callback, cmd_if_shell_free, 125 cdata); 126 free(shellcmd); 127 128 if (cdata->bflag) 129 return (CMD_RETURN_NORMAL); 130 return (CMD_RETURN_WAIT); 131 } 132 133 void 134 cmd_if_shell_callback(struct job *job) 135 { 136 struct cmd_if_shell_data *cdata = job->data; 137 struct cmd_q *cmdq = cdata->cmdq, *cmdq1; 138 struct cmd_list *cmdlist; 139 char *cause, *cmd; 140 141 if (cmdq->flags & CMD_Q_DEAD) 142 return; 143 144 if (!WIFEXITED(job->status) || WEXITSTATUS(job->status) != 0) 145 cmd = cdata->cmd_else; 146 else 147 cmd = cdata->cmd_if; 148 if (cmd == NULL) 149 return; 150 151 if (cmd_string_parse(cmd, &cmdlist, NULL, 0, &cause) != 0) { 152 if (cause != NULL) { 153 cmdq_error(cmdq, "%s", cause); 154 free(cause); 155 } 156 return; 157 } 158 159 cmdq1 = cmdq_new(cmdq->client); 160 cmdq1->emptyfn = cmd_if_shell_done; 161 cmdq1->data = cdata; 162 163 cdata->references++; 164 cmdq_run(cmdq1, cmdlist, &cdata->mouse); 165 cmd_list_free(cmdlist); 166 } 167 168 void 169 cmd_if_shell_done(struct cmd_q *cmdq1) 170 { 171 struct cmd_if_shell_data *cdata = cmdq1->data; 172 struct cmd_q *cmdq = cdata->cmdq; 173 174 if (cmdq1->client_exit >= 0) 175 cmdq->client_exit = cmdq1->client_exit; 176 cmdq_free(cmdq1); 177 178 if (--cdata->references != 0) 179 return; 180 181 if (!cmdq_free(cmdq) && !cdata->bflag) 182 cmdq_continue(cmdq); 183 184 free(cdata->cmd_else); 185 free(cdata->cmd_if); 186 free(cdata); 187 } 188 189 void 190 cmd_if_shell_free(void *data) 191 { 192 struct cmd_if_shell_data *cdata = data; 193 struct cmd_q *cmdq = cdata->cmdq; 194 195 if (--cdata->references != 0) 196 return; 197 198 if (!cmdq_free(cmdq) && !cdata->bflag) 199 cmdq_continue(cmdq); 200 201 free(cdata->cmd_else); 202 free(cdata->cmd_if); 203 free(cdata); 204 } 205