Annotation of src/usr.bin/tmux/cmd-if-shell.c, Revision 1.58
1.58 ! nicm 1: /* $OpenBSD: cmd-if-shell.c,v 1.57 2018/03/08 08:09:10 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.44 nicm 52: char *file;
53: u_int line;
54:
1.30 nicm 55: char *cmd_if;
56: char *cmd_else;
57:
1.47 nicm 58: struct client *client;
1.48 nicm 59: struct cmdq_item *item;
1.30 nicm 60: struct mouse_event mouse;
1.5 nicm 61: };
1.1 nicm 62:
1.45 nicm 63: static enum cmd_retval
1.48 nicm 64: cmd_if_shell_exec(struct cmd *self, struct cmdq_item *item)
1.1 nicm 65: {
1.10 nicm 66: struct args *args = self->args;
1.53 nicm 67: struct cmdq_shared *shared = item->shared;
1.5 nicm 68: struct cmd_if_shell_data *cdata;
1.27 nicm 69: char *shellcmd, *cmd, *cause;
70: struct cmd_list *cmdlist;
1.48 nicm 71: struct cmdq_item *new_item;
1.55 nicm 72: struct client *c = cmd_find_client(item, NULL, 1);
73: struct session *s = item->target.s;
74: struct winlink *wl = item->target.wl;
75: struct window_pane *wp = item->target.wp;
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: cmd = NULL;
80: if (*shellcmd != '0' && *shellcmd != '\0')
81: cmd = args->argv[1];
82: else if (args->argc == 3)
83: cmd = args->argv[2];
1.35 nicm 84: free(shellcmd);
1.27 nicm 85: if (cmd == NULL)
86: return (CMD_RETURN_NORMAL);
1.49 nicm 87: cmdlist = cmd_string_parse(cmd, NULL, 0, &cause);
88: if (cmdlist == NULL) {
1.27 nicm 89: if (cause != NULL) {
1.48 nicm 90: cmdq_error(item, "%s", cause);
1.27 nicm 91: free(cause);
92: }
93: return (CMD_RETURN_ERROR);
94: }
1.53 nicm 95: new_item = cmdq_get_command(cmdlist, NULL, &shared->mouse, 0);
1.48 nicm 96: cmdq_insert_after(item, new_item);
1.27 nicm 97: cmd_list_free(cmdlist);
98: return (CMD_RETURN_NORMAL);
99: }
1.5 nicm 100:
1.44 nicm 101: cdata = xcalloc(1, sizeof *cdata);
102: if (self->file != NULL) {
103: cdata->file = xstrdup(self->file);
104: cdata->line = self->line;
105: }
1.30 nicm 106:
1.13 nicm 107: cdata->cmd_if = xstrdup(args->argv[1]);
1.14 nicm 108: if (args->argc == 3)
109: cdata->cmd_else = xstrdup(args->argv[2]);
1.13 nicm 110: else
111: cdata->cmd_else = NULL;
1.30 nicm 112:
1.48 nicm 113: cdata->client = item->client;
1.54 nicm 114: if (cdata->client != NULL)
115: cdata->client->references++;
1.5 nicm 116:
1.47 nicm 117: if (!args_has(args, 'b'))
1.48 nicm 118: cdata->item = item;
1.47 nicm 119: else
1.48 nicm 120: cdata->item = NULL;
1.53 nicm 121: memcpy(&cdata->mouse, &shared->mouse, sizeof cdata->mouse);
1.5 nicm 122:
1.58 ! nicm 123: job_run(shellcmd, s, server_client_get_cwd(item->client, s), NULL,
! 124: cmd_if_shell_callback, cmd_if_shell_free, cdata, 0);
1.19 nicm 125: free(shellcmd);
1.1 nicm 126:
1.47 nicm 127: if (args_has(args, 'b'))
1.20 nicm 128: return (CMD_RETURN_NORMAL);
129: return (CMD_RETURN_WAIT);
1.1 nicm 130: }
131:
1.45 nicm 132: static void
1.5 nicm 133: cmd_if_shell_callback(struct job *job)
1.1 nicm 134: {
1.5 nicm 135: struct cmd_if_shell_data *cdata = job->data;
1.47 nicm 136: struct client *c = cdata->client;
1.1 nicm 137: struct cmd_list *cmdlist;
1.48 nicm 138: struct cmdq_item *new_item;
1.47 nicm 139: char *cause, *cmd, *file = cdata->file;
140: u_int line = cdata->line;
1.20 nicm 141:
142: if (!WIFEXITED(job->status) || WEXITSTATUS(job->status) != 0)
1.13 nicm 143: cmd = cdata->cmd_else;
1.20 nicm 144: else
1.13 nicm 145: cmd = cdata->cmd_if;
1.20 nicm 146: if (cmd == NULL)
1.47 nicm 147: goto out;
1.20 nicm 148:
1.49 nicm 149: cmdlist = cmd_string_parse(cmd, file, line, &cause);
150: if (cmdlist == NULL) {
1.56 nicm 151: if (cause != NULL && cdata->item != NULL)
152: cmdq_error(cdata->item, "%s", cause);
153: free(cause);
154: new_item = NULL;
1.47 nicm 155: } else {
1.48 nicm 156: new_item = cmdq_get_command(cmdlist, NULL, &cdata->mouse, 0);
1.47 nicm 157: cmd_list_free(cmdlist);
1.1 nicm 158: }
159:
1.48 nicm 160: if (new_item != NULL) {
161: if (cdata->item == NULL)
162: cmdq_append(c, new_item);
1.47 nicm 163: else
1.48 nicm 164: cmdq_insert_after(cdata->item, new_item);
1.47 nicm 165: }
1.25 nicm 166:
1.47 nicm 167: out:
1.48 nicm 168: if (cdata->item != NULL)
169: cdata->item->flags &= ~CMDQ_WAITING;
1.20 nicm 170: }
171:
1.45 nicm 172: static void
1.5 nicm 173: cmd_if_shell_free(void *data)
1.1 nicm 174: {
1.5 nicm 175: struct cmd_if_shell_data *cdata = data;
1.1 nicm 176:
1.54 nicm 177: if (cdata->client != NULL)
178: server_client_unref(cdata->client);
1.1 nicm 179:
1.15 nicm 180: free(cdata->cmd_else);
181: free(cdata->cmd_if);
1.44 nicm 182:
183: free(cdata->file);
1.15 nicm 184: free(cdata);
1.1 nicm 185: }