1 /* $OpenBSD: cmd-if-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 * 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 "if-shell", "if", 40 "bt:", 2, 3, 41 "[-b] " CMD_TARGET_PANE_USAGE " shell-command command [command]", 42 0, 43 cmd_if_shell_exec 44 }; 45 46 struct cmd_if_shell_data { 47 char *cmd_if; 48 char *cmd_else; 49 struct cmd_q *cmdq; 50 int bflag; 51 int started; 52 }; 53 54 enum cmd_retval 55 cmd_if_shell_exec(struct cmd *self, struct cmd_q *cmdq) 56 { 57 struct args *args = self->args; 58 struct cmd_if_shell_data *cdata; 59 char *shellcmd; 60 struct client *c; 61 struct session *s = NULL; 62 struct winlink *wl = NULL; 63 struct window_pane *wp = NULL; 64 struct format_tree *ft; 65 66 if (args_has(args, 't')) 67 wl = cmd_find_pane(cmdq, args_get(args, 't'), &s, &wp); 68 else { 69 c = cmd_find_client(cmdq, NULL, 1); 70 if (c != NULL && c->session != NULL) { 71 s = c->session; 72 wl = s->curw; 73 wp = wl->window->active; 74 } 75 } 76 77 ft = format_create(); 78 if (s != NULL) 79 format_session(ft, s); 80 if (s != NULL && wl != NULL) 81 format_winlink(ft, s, wl); 82 if (wp != NULL) 83 format_window_pane(ft, wp); 84 shellcmd = format_expand(ft, args->argv[0]); 85 format_free(ft); 86 87 cdata = xmalloc(sizeof *cdata); 88 cdata->cmd_if = xstrdup(args->argv[1]); 89 if (args->argc == 3) 90 cdata->cmd_else = xstrdup(args->argv[2]); 91 else 92 cdata->cmd_else = NULL; 93 cdata->bflag = args_has(args, 'b'); 94 95 cdata->started = 0; 96 cdata->cmdq = cmdq; 97 cmdq->references++; 98 99 job_run(shellcmd, s, cmd_if_shell_callback, cmd_if_shell_free, cdata); 100 free(shellcmd); 101 102 if (cdata->bflag) 103 return (CMD_RETURN_NORMAL); 104 return (CMD_RETURN_WAIT); 105 } 106 107 void 108 cmd_if_shell_callback(struct job *job) 109 { 110 struct cmd_if_shell_data *cdata = job->data; 111 struct cmd_q *cmdq = cdata->cmdq, *cmdq1; 112 struct cmd_list *cmdlist; 113 char *cause, *cmd; 114 115 if (cmdq->dead) 116 return; 117 118 if (!WIFEXITED(job->status) || WEXITSTATUS(job->status) != 0) 119 cmd = cdata->cmd_else; 120 else 121 cmd = cdata->cmd_if; 122 if (cmd == NULL) 123 return; 124 125 if (cmd_string_parse(cmd, &cmdlist, NULL, 0, &cause) != 0) { 126 if (cause != NULL) { 127 cmdq_error(cmdq, "%s", cause); 128 free(cause); 129 } 130 return; 131 } 132 133 cdata->started = 1; 134 135 cmdq1 = cmdq_new(cmdq->client); 136 cmdq1->emptyfn = cmd_if_shell_done; 137 cmdq1->data = cdata; 138 139 cmdq_run(cmdq1, cmdlist); 140 cmd_list_free(cmdlist); 141 } 142 143 void 144 cmd_if_shell_done(struct cmd_q *cmdq1) 145 { 146 struct cmd_if_shell_data *cdata = cmdq1->data; 147 struct cmd_q *cmdq = cdata->cmdq; 148 149 if (cmdq1->client_exit >= 0) 150 cmdq->client_exit = cmdq1->client_exit; 151 152 if (!cmdq_free(cmdq) && !cdata->bflag) 153 cmdq_continue(cmdq); 154 155 cmdq_free(cmdq1); 156 157 free(cdata->cmd_else); 158 free(cdata->cmd_if); 159 free(cdata); 160 } 161 162 void 163 cmd_if_shell_free(void *data) 164 { 165 struct cmd_if_shell_data *cdata = data; 166 struct cmd_q *cmdq = cdata->cmdq; 167 168 if (cdata->started) 169 return; 170 171 if (!cmdq_free(cmdq) && !cdata->bflag) 172 cmdq_continue(cmdq); 173 174 free(cdata->cmd_else); 175 free(cdata->cmd_if); 176 free(cdata); 177 } 178