1 1.4 christos /* $OpenBSD$ */ 2 1.1 jmmv 3 1.1 jmmv /* 4 1.1 jmmv * Copyright (c) 2009 Tiago Cunha <me (at) tiagocunha.org> 5 1.1 jmmv * Copyright (c) 2009 Nicholas Marriott <nicm (at) openbsd.org> 6 1.1 jmmv * 7 1.1 jmmv * Permission to use, copy, modify, and distribute this software for any 8 1.1 jmmv * purpose with or without fee is hereby granted, provided that the above 9 1.1 jmmv * copyright notice and this permission notice appear in all copies. 10 1.1 jmmv * 11 1.1 jmmv * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 1.1 jmmv * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 1.1 jmmv * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 1.1 jmmv * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 1.1 jmmv * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER 16 1.1 jmmv * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING 17 1.1 jmmv * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 1.1 jmmv */ 19 1.1 jmmv 20 1.1 jmmv #include <sys/types.h> 21 1.1 jmmv #include <sys/wait.h> 22 1.1 jmmv 23 1.12 wiz #include <ctype.h> 24 1.3 christos #include <stdlib.h> 25 1.1 jmmv #include <string.h> 26 1.1 jmmv 27 1.1 jmmv #include "tmux.h" 28 1.1 jmmv 29 1.1 jmmv /* 30 1.3 christos * Executes a tmux command if a shell command returns true or false. 31 1.1 jmmv */ 32 1.1 jmmv 33 1.12 wiz static enum args_parse_type cmd_if_shell_args_parse(struct args *, u_int, 34 1.12 wiz char **); 35 1.12 wiz static enum cmd_retval cmd_if_shell_exec(struct cmd *, 36 1.12 wiz struct cmdq_item *); 37 1.1 jmmv 38 1.12 wiz static void cmd_if_shell_callback(struct job *); 39 1.12 wiz static void cmd_if_shell_free(void *); 40 1.1 jmmv 41 1.1 jmmv const struct cmd_entry cmd_if_shell_entry = { 42 1.5 christos .name = "if-shell", 43 1.5 christos .alias = "if", 44 1.5 christos 45 1.12 wiz .args = { "bFt:", 2, 3, cmd_if_shell_args_parse }, 46 1.5 christos .usage = "[-bF] " CMD_TARGET_PANE_USAGE " shell-command command " 47 1.5 christos "[command]", 48 1.5 christos 49 1.7 christos .target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL }, 50 1.5 christos 51 1.5 christos .flags = 0, 52 1.5 christos .exec = cmd_if_shell_exec 53 1.1 jmmv }; 54 1.1 jmmv 55 1.1 jmmv struct cmd_if_shell_data { 56 1.12 wiz struct args_command_state *cmd_if; 57 1.12 wiz struct args_command_state *cmd_else; 58 1.6 christos 59 1.12 wiz struct client *client; 60 1.12 wiz struct cmdq_item *item; 61 1.12 wiz }; 62 1.4 christos 63 1.12 wiz static enum args_parse_type 64 1.12 wiz cmd_if_shell_args_parse(__unused struct args *args, u_int idx, 65 1.12 wiz __unused char **cause) 66 1.12 wiz { 67 1.12 wiz if (idx == 1 || idx == 2) 68 1.12 wiz return (ARGS_PARSE_COMMANDS_OR_STRING); 69 1.12 wiz return (ARGS_PARSE_STRING); 70 1.12 wiz } 71 1.1 jmmv 72 1.6 christos static enum cmd_retval 73 1.6 christos cmd_if_shell_exec(struct cmd *self, struct cmdq_item *item) 74 1.1 jmmv { 75 1.11 christos struct args *args = cmd_get_args(self); 76 1.11 christos struct cmd_find_state *target = cmdq_get_target(item); 77 1.1 jmmv struct cmd_if_shell_data *cdata; 78 1.12 wiz struct cmdq_item *new_item; 79 1.12 wiz char *shellcmd; 80 1.11 christos struct client *tc = cmdq_get_target_client(item); 81 1.11 christos struct session *s = target->s; 82 1.12 wiz struct cmd_list *cmdlist; 83 1.12 wiz u_int count = args_count(args); 84 1.12 wiz int wait = !args_has(args, 'b'); 85 1.3 christos 86 1.12 wiz shellcmd = format_single_from_target(item, args_string(args, 0)); 87 1.4 christos if (args_has(args, 'F')) { 88 1.4 christos if (*shellcmd != '0' && *shellcmd != '\0') 89 1.12 wiz cmdlist = args_make_commands_now(self, item, 1, 0); 90 1.12 wiz else if (count == 3) 91 1.12 wiz cmdlist = args_make_commands_now(self, item, 2, 0); 92 1.12 wiz else { 93 1.12 wiz free(shellcmd); 94 1.12 wiz return (CMD_RETURN_NORMAL); 95 1.12 wiz } 96 1.5 christos free(shellcmd); 97 1.12 wiz if (cmdlist == NULL) 98 1.4 christos return (CMD_RETURN_ERROR); 99 1.12 wiz new_item = cmdq_get_command(cmdlist, cmdq_get_state(item)); 100 1.12 wiz cmdq_insert_after(item, new_item); 101 1.4 christos return (CMD_RETURN_NORMAL); 102 1.4 christos } 103 1.4 christos 104 1.6 christos cdata = xcalloc(1, sizeof *cdata); 105 1.4 christos 106 1.12 wiz cdata->cmd_if = args_make_commands_prepare(self, item, 1, NULL, wait, 107 1.12 wiz 0); 108 1.12 wiz if (count == 3) { 109 1.12 wiz cdata->cmd_else = args_make_commands_prepare(self, item, 2, 110 1.12 wiz NULL, wait, 0); 111 1.12 wiz } 112 1.4 christos 113 1.12 wiz if (wait) { 114 1.11 christos cdata->client = cmdq_get_client(item); 115 1.12 wiz cdata->item = item; 116 1.12 wiz } else 117 1.11 christos cdata->client = tc; 118 1.7 christos if (cdata->client != NULL) 119 1.7 christos cdata->client->references++; 120 1.3 christos 121 1.12 wiz if (job_run(shellcmd, 0, NULL, NULL, s, 122 1.11 christos server_client_get_cwd(cmdq_get_client(item), s), NULL, 123 1.11 christos cmd_if_shell_callback, cmd_if_shell_free, cdata, 0, -1, 124 1.11 christos -1) == NULL) { 125 1.9 christos cmdq_error(item, "failed to run command: %s", shellcmd); 126 1.9 christos free(shellcmd); 127 1.13 wiz cmd_if_shell_free(cdata); 128 1.9 christos return (CMD_RETURN_ERROR); 129 1.9 christos } 130 1.3 christos free(shellcmd); 131 1.3 christos 132 1.12 wiz if (!wait) 133 1.3 christos return (CMD_RETURN_NORMAL); 134 1.3 christos return (CMD_RETURN_WAIT); 135 1.1 jmmv } 136 1.1 jmmv 137 1.6 christos static void 138 1.1 jmmv cmd_if_shell_callback(struct job *job) 139 1.1 jmmv { 140 1.9 christos struct cmd_if_shell_data *cdata = job_get_data(job); 141 1.6 christos struct client *c = cdata->client; 142 1.12 wiz struct cmdq_item *item = cdata->item, *new_item; 143 1.12 wiz struct args_command_state *state; 144 1.12 wiz struct cmd_list *cmdlist; 145 1.12 wiz char *error; 146 1.9 christos int status; 147 1.1 jmmv 148 1.9 christos status = job_get_status(job); 149 1.9 christos if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) 150 1.12 wiz state = cdata->cmd_else; 151 1.3 christos else 152 1.12 wiz state = cdata->cmd_if; 153 1.12 wiz if (state == NULL) 154 1.6 christos goto out; 155 1.1 jmmv 156 1.12 wiz cmdlist = args_make_commands(state, 0, NULL, &error); 157 1.12 wiz if (cmdlist == NULL) { 158 1.12 wiz if (cdata->item == NULL) { 159 1.12 wiz *error = toupper((u_char)*error); 160 1.13 wiz status_message_set(c, -1, 1, 0, 0, "%s", error); 161 1.12 wiz } else 162 1.12 wiz cmdq_error(cdata->item, "%s", error); 163 1.12 wiz free(error); 164 1.12 wiz } else if (item == NULL) { 165 1.12 wiz new_item = cmdq_get_command(cmdlist, NULL); 166 1.12 wiz cmdq_append(c, new_item); 167 1.12 wiz } else { 168 1.12 wiz new_item = cmdq_get_command(cmdlist, cmdq_get_state(item)); 169 1.12 wiz cmdq_insert_after(item, new_item); 170 1.6 christos } 171 1.3 christos 172 1.6 christos out: 173 1.6 christos if (cdata->item != NULL) 174 1.10 christos cmdq_continue(cdata->item); 175 1.3 christos } 176 1.3 christos 177 1.6 christos static void 178 1.1 jmmv cmd_if_shell_free(void *data) 179 1.1 jmmv { 180 1.1 jmmv struct cmd_if_shell_data *cdata = data; 181 1.3 christos 182 1.7 christos if (cdata->client != NULL) 183 1.7 christos server_client_unref(cdata->client); 184 1.1 jmmv 185 1.12 wiz if (cdata->cmd_else != NULL) 186 1.12 wiz args_make_commands_free(cdata->cmd_else); 187 1.12 wiz args_make_commands_free(cdata->cmd_if); 188 1.10 christos 189 1.3 christos free(cdata); 190 1.1 jmmv } 191