Annotation of src/usr.bin/tmux/cmd-if-shell.c, Revision 1.62
1.62 ! nicm 1: /* $OpenBSD: cmd-if-shell.c,v 1.61 2019/05/23 11:13:30 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.55 nicm 70: struct client *c = cmd_find_client(item, NULL, 1);
71: struct session *s = item->target.s;
72: struct winlink *wl = item->target.wl;
73: struct window_pane *wp = item->target.wp;
1.61 nicm 74: struct cmd_parse_input pi;
75: struct cmd_parse_result *pr;
1.41 nicm 76:
1.51 nicm 77: shellcmd = format_single(item, args->argv[0], c, s, wl, wp);
1.27 nicm 78: if (args_has(args, 'F')) {
79: if (*shellcmd != '0' && *shellcmd != '\0')
80: cmd = args->argv[1];
81: else if (args->argc == 3)
82: cmd = args->argv[2];
1.61 nicm 83: else
84: cmd = NULL;
1.35 nicm 85: free(shellcmd);
1.27 nicm 86: if (cmd == NULL)
87: return (CMD_RETURN_NORMAL);
1.61 nicm 88:
89: memset(&pi, 0, sizeof pi);
90: if (self->file != NULL)
91: pi.file = self->file;
92: pi.line = self->line;
93: pi.item = item;
94: pi.c = c;
95: cmd_find_copy_state(&pi.fs, &item->target);
96:
97: pr = cmd_parse_from_string(cmd, &pi);
98: switch (pr->status) {
99: case CMD_PARSE_EMPTY:
100: break;
101: case CMD_PARSE_ERROR:
102: cmdq_error(item, "%s", pr->error);
103: free(pr->error);
1.27 nicm 104: return (CMD_RETURN_ERROR);
1.61 nicm 105: case CMD_PARSE_SUCCESS:
106: new_item = cmdq_get_command(pr->cmdlist, NULL, m, 0);
107: cmdq_insert_after(item, new_item);
108: cmd_list_free(pr->cmdlist);
109: break;
1.27 nicm 110: }
111: return (CMD_RETURN_NORMAL);
112: }
1.5 nicm 113:
1.44 nicm 114: cdata = xcalloc(1, sizeof *cdata);
1.30 nicm 115:
1.13 nicm 116: cdata->cmd_if = xstrdup(args->argv[1]);
1.14 nicm 117: if (args->argc == 3)
118: cdata->cmd_else = xstrdup(args->argv[2]);
1.13 nicm 119: else
120: cdata->cmd_else = NULL;
1.61 nicm 121: memcpy(&cdata->mouse, m, sizeof cdata->mouse);
1.30 nicm 122:
1.48 nicm 123: cdata->client = item->client;
1.54 nicm 124: if (cdata->client != NULL)
125: cdata->client->references++;
1.5 nicm 126:
1.47 nicm 127: if (!args_has(args, 'b'))
1.48 nicm 128: cdata->item = item;
1.47 nicm 129: else
1.48 nicm 130: cdata->item = NULL;
1.61 nicm 131:
132: memset(&cdata->input, 0, sizeof cdata->input);
133: if (self->file != NULL)
134: cdata->input.file = xstrdup(self->file);
135: cdata->input.line = self->line;
136: cdata->input.item = cdata->item;
137: cdata->input.c = c;
138: if (cdata->input.c != NULL)
139: cdata->input.c->references++;
140: cmd_find_copy_state(&cdata->input.fs, &item->target);
1.5 nicm 141:
1.60 nicm 142: if (job_run(shellcmd, s, server_client_get_cwd(item->client, s), NULL,
143: cmd_if_shell_callback, cmd_if_shell_free, cdata, 0) == NULL) {
144: cmdq_error(item, "failed to run command: %s", shellcmd);
145: free(shellcmd);
146: free(cdata);
147: return (CMD_RETURN_ERROR);
148: }
1.19 nicm 149: free(shellcmd);
1.1 nicm 150:
1.47 nicm 151: if (args_has(args, 'b'))
1.20 nicm 152: return (CMD_RETURN_NORMAL);
153: return (CMD_RETURN_WAIT);
1.1 nicm 154: }
155:
1.45 nicm 156: static void
1.5 nicm 157: cmd_if_shell_callback(struct job *job)
1.1 nicm 158: {
1.59 nicm 159: struct cmd_if_shell_data *cdata = job_get_data(job);
1.47 nicm 160: struct client *c = cdata->client;
1.61 nicm 161: struct mouse_event *m = &cdata->mouse;
1.62 ! nicm 162: struct cmdq_item *new_item = NULL;
1.61 nicm 163: char *cmd;
1.59 nicm 164: int status;
1.61 nicm 165: struct cmd_parse_result *pr;
1.20 nicm 166:
1.59 nicm 167: status = job_get_status(job);
168: if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
1.13 nicm 169: cmd = cdata->cmd_else;
1.20 nicm 170: else
1.13 nicm 171: cmd = cdata->cmd_if;
1.20 nicm 172: if (cmd == NULL)
1.47 nicm 173: goto out;
1.20 nicm 174:
1.61 nicm 175: pr = cmd_parse_from_string(cmd, &cdata->input);
176: switch (pr->status) {
177: case CMD_PARSE_EMPTY:
178: break;
179: case CMD_PARSE_ERROR:
1.62 ! nicm 180: if (cdata->item != NULL)
! 181: cmdq_error(cdata->item, "%s", pr->error);
1.61 nicm 182: free(pr->error);
183: break;
184: case CMD_PARSE_SUCCESS:
185: new_item = cmdq_get_command(pr->cmdlist, NULL, m, 0);
186: cmd_list_free(pr->cmdlist);
187: break;
1.1 nicm 188: }
1.48 nicm 189: if (new_item != NULL) {
190: if (cdata->item == NULL)
191: cmdq_append(c, new_item);
1.47 nicm 192: else
1.48 nicm 193: cmdq_insert_after(cdata->item, new_item);
1.47 nicm 194: }
1.25 nicm 195:
1.47 nicm 196: out:
1.48 nicm 197: if (cdata->item != NULL)
198: cdata->item->flags &= ~CMDQ_WAITING;
1.20 nicm 199: }
200:
1.45 nicm 201: static void
1.5 nicm 202: cmd_if_shell_free(void *data)
1.1 nicm 203: {
1.5 nicm 204: struct cmd_if_shell_data *cdata = data;
1.1 nicm 205:
1.54 nicm 206: if (cdata->client != NULL)
207: server_client_unref(cdata->client);
1.1 nicm 208:
1.15 nicm 209: free(cdata->cmd_else);
210: free(cdata->cmd_if);
1.44 nicm 211:
1.61 nicm 212: if (cdata->input.c != NULL)
213: server_client_unref(cdata->input.c);
214: free((void *)cdata->input.file);
215:
1.15 nicm 216: free(cdata);
1.1 nicm 217: }