Annotation of src/usr.bin/tmux/cmd-if-shell.c, Revision 1.64
1.64 ! nicm 1: /* $OpenBSD: cmd-if-shell.c,v 1.63 2019/06/12 08:47:07 nicm Exp $ */
1.1 nicm 2:
3: /*
4: * Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
1.5 nicm 5: * Copyright (c) 2009 Nicholas Marriott <nicm@openbsd.org>
1.1 nicm 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>
1.5 nicm 21: #include <sys/wait.h>
1.1 nicm 22:
1.15 nicm 23: #include <stdlib.h>
1.1 nicm 24: #include <string.h>
25:
26: #include "tmux.h"
27:
28: /*
1.14 nicm 29: * Executes a tmux command if a shell command returns true or false.
1.1 nicm 30: */
31:
1.48 nicm 32: static enum cmd_retval cmd_if_shell_exec(struct cmd *, struct cmdq_item *);
1.1 nicm 33:
1.48 nicm 34: static void cmd_if_shell_callback(struct job *);
35: static void cmd_if_shell_free(void *);
1.1 nicm 36:
37: const struct cmd_entry cmd_if_shell_entry = {
1.39 nicm 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:
1.55 nicm 45: .target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
1.40 nicm 46:
47: .flags = 0,
1.39 nicm 48: .exec = cmd_if_shell_exec
1.1 nicm 49: };
50:
1.5 nicm 51: struct cmd_if_shell_data {
1.61 nicm 52: struct cmd_parse_input input;
1.44 nicm 53:
1.30 nicm 54: char *cmd_if;
55: char *cmd_else;
56:
1.47 nicm 57: struct client *client;
1.48 nicm 58: struct cmdq_item *item;
1.30 nicm 59: struct mouse_event mouse;
1.5 nicm 60: };
1.1 nicm 61:
1.45 nicm 62: static enum cmd_retval
1.48 nicm 63: cmd_if_shell_exec(struct cmd *self, struct cmdq_item *item)
1.1 nicm 64: {
1.10 nicm 65: struct args *args = self->args;
1.61 nicm 66: struct mouse_event *m = &item->shared->mouse;
1.5 nicm 67: struct cmd_if_shell_data *cdata;
1.61 nicm 68: char *shellcmd, *cmd;
1.48 nicm 69: struct cmdq_item *new_item;
1.63 nicm 70: struct cmd_find_state *fs = &item->target;
1.55 nicm 71: struct client *c = cmd_find_client(item, NULL, 1);
1.63 nicm 72: struct session *s = fs->s;
73: struct winlink *wl = fs->wl;
74: struct window_pane *wp = fs->wp;
1.61 nicm 75: struct cmd_parse_input pi;
76: struct cmd_parse_result *pr;
1.41 nicm 77:
1.51 nicm 78: shellcmd = format_single(item, args->argv[0], c, s, wl, wp);
1.27 nicm 79: if (args_has(args, 'F')) {
80: if (*shellcmd != '0' && *shellcmd != '\0')
81: cmd = args->argv[1];
82: else if (args->argc == 3)
83: cmd = args->argv[2];
1.61 nicm 84: else
85: cmd = NULL;
1.35 nicm 86: free(shellcmd);
1.27 nicm 87: if (cmd == NULL)
88: return (CMD_RETURN_NORMAL);
1.61 nicm 89:
90: memset(&pi, 0, sizeof pi);
91: if (self->file != NULL)
92: pi.file = self->file;
93: pi.line = self->line;
94: pi.item = item;
95: pi.c = c;
1.63 nicm 96: cmd_find_copy_state(&pi.fs, fs);
1.61 nicm 97:
98: pr = cmd_parse_from_string(cmd, &pi);
99: switch (pr->status) {
100: case CMD_PARSE_EMPTY:
101: break;
102: case CMD_PARSE_ERROR:
103: cmdq_error(item, "%s", pr->error);
104: free(pr->error);
1.27 nicm 105: return (CMD_RETURN_ERROR);
1.61 nicm 106: case CMD_PARSE_SUCCESS:
1.63 nicm 107: new_item = cmdq_get_command(pr->cmdlist, fs, m, 0);
1.61 nicm 108: cmdq_insert_after(item, new_item);
109: cmd_list_free(pr->cmdlist);
110: break;
1.27 nicm 111: }
112: return (CMD_RETURN_NORMAL);
113: }
1.5 nicm 114:
1.44 nicm 115: cdata = xcalloc(1, sizeof *cdata);
1.30 nicm 116:
1.13 nicm 117: cdata->cmd_if = xstrdup(args->argv[1]);
1.14 nicm 118: if (args->argc == 3)
119: cdata->cmd_else = xstrdup(args->argv[2]);
1.13 nicm 120: else
121: cdata->cmd_else = NULL;
1.61 nicm 122: memcpy(&cdata->mouse, m, sizeof cdata->mouse);
1.30 nicm 123:
1.48 nicm 124: cdata->client = item->client;
1.54 nicm 125: if (cdata->client != NULL)
126: cdata->client->references++;
1.5 nicm 127:
1.47 nicm 128: if (!args_has(args, 'b'))
1.48 nicm 129: cdata->item = item;
1.47 nicm 130: else
1.48 nicm 131: cdata->item = NULL;
1.61 nicm 132:
133: memset(&cdata->input, 0, sizeof cdata->input);
134: if (self->file != NULL)
135: cdata->input.file = xstrdup(self->file);
136: cdata->input.line = self->line;
137: cdata->input.item = cdata->item;
138: cdata->input.c = c;
139: if (cdata->input.c != NULL)
140: cdata->input.c->references++;
1.63 nicm 141: cmd_find_copy_state(&cdata->input.fs, fs);
1.5 nicm 142:
1.60 nicm 143: if (job_run(shellcmd, s, server_client_get_cwd(item->client, s), NULL,
144: cmd_if_shell_callback, cmd_if_shell_free, cdata, 0) == NULL) {
145: cmdq_error(item, "failed to run command: %s", shellcmd);
146: free(shellcmd);
147: free(cdata);
148: return (CMD_RETURN_ERROR);
149: }
1.19 nicm 150: free(shellcmd);
1.1 nicm 151:
1.47 nicm 152: if (args_has(args, 'b'))
1.20 nicm 153: return (CMD_RETURN_NORMAL);
154: return (CMD_RETURN_WAIT);
1.1 nicm 155: }
156:
1.45 nicm 157: static void
1.5 nicm 158: cmd_if_shell_callback(struct job *job)
1.1 nicm 159: {
1.59 nicm 160: struct cmd_if_shell_data *cdata = job_get_data(job);
1.47 nicm 161: struct client *c = cdata->client;
1.61 nicm 162: struct mouse_event *m = &cdata->mouse;
1.62 nicm 163: struct cmdq_item *new_item = NULL;
1.61 nicm 164: char *cmd;
1.59 nicm 165: int status;
1.61 nicm 166: struct cmd_parse_result *pr;
1.20 nicm 167:
1.59 nicm 168: status = job_get_status(job);
169: if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
1.13 nicm 170: cmd = cdata->cmd_else;
1.20 nicm 171: else
1.13 nicm 172: cmd = cdata->cmd_if;
1.20 nicm 173: if (cmd == NULL)
1.47 nicm 174: goto out;
1.20 nicm 175:
1.61 nicm 176: pr = cmd_parse_from_string(cmd, &cdata->input);
177: switch (pr->status) {
178: case CMD_PARSE_EMPTY:
179: break;
180: case CMD_PARSE_ERROR:
1.62 nicm 181: if (cdata->item != NULL)
182: cmdq_error(cdata->item, "%s", pr->error);
1.61 nicm 183: free(pr->error);
184: break;
185: case CMD_PARSE_SUCCESS:
186: new_item = cmdq_get_command(pr->cmdlist, NULL, m, 0);
187: cmd_list_free(pr->cmdlist);
188: break;
1.1 nicm 189: }
1.48 nicm 190: if (new_item != NULL) {
191: if (cdata->item == NULL)
192: cmdq_append(c, new_item);
1.47 nicm 193: else
1.48 nicm 194: cmdq_insert_after(cdata->item, new_item);
1.47 nicm 195: }
1.25 nicm 196:
1.47 nicm 197: out:
1.48 nicm 198: if (cdata->item != NULL)
1.64 ! nicm 199: cmdq_continue(cdata->item);
1.20 nicm 200: }
201:
1.45 nicm 202: static void
1.5 nicm 203: cmd_if_shell_free(void *data)
1.1 nicm 204: {
1.5 nicm 205: struct cmd_if_shell_data *cdata = data;
1.1 nicm 206:
1.54 nicm 207: if (cdata->client != NULL)
208: server_client_unref(cdata->client);
1.1 nicm 209:
1.15 nicm 210: free(cdata->cmd_else);
211: free(cdata->cmd_if);
1.44 nicm 212:
1.61 nicm 213: if (cdata->input.c != NULL)
214: server_client_unref(cdata->input.c);
215: free((void *)cdata->input.file);
216:
1.15 nicm 217: free(cdata);
1.1 nicm 218: }