Annotation of src/usr.bin/tmux/cmd-if-shell.c, Revision 1.54
1.54 ! nicm 1: /* $OpenBSD: cmd-if-shell.c,v 1.53 2017/04/21 14:01:19 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 enum cmd_retval cmd_if_shell_error(struct cmdq_item *, void *);
35: static void cmd_if_shell_callback(struct job *);
36: static void cmd_if_shell_free(void *);
1.1 nicm 37:
38: const struct cmd_entry cmd_if_shell_entry = {
1.39 nicm 39: .name = "if-shell",
40: .alias = "if",
41:
42: .args = { "bFt:", 2, 3 },
43: .usage = "[-bF] " CMD_TARGET_PANE_USAGE " shell-command command "
44: "[command]",
45:
1.40 nicm 46: .tflag = CMD_PANE_CANFAIL,
47:
48: .flags = 0,
1.39 nicm 49: .exec = cmd_if_shell_exec
1.1 nicm 50: };
51:
1.5 nicm 52: struct cmd_if_shell_data {
1.44 nicm 53: char *file;
54: u_int line;
55:
1.30 nicm 56: char *cmd_if;
57: char *cmd_else;
58:
1.47 nicm 59: struct client *client;
1.48 nicm 60: struct cmdq_item *item;
1.30 nicm 61: struct mouse_event mouse;
1.5 nicm 62: };
1.1 nicm 63:
1.45 nicm 64: static enum cmd_retval
1.48 nicm 65: cmd_if_shell_exec(struct cmd *self, struct cmdq_item *item)
1.1 nicm 66: {
1.10 nicm 67: struct args *args = self->args;
1.53 nicm 68: struct cmdq_shared *shared = item->shared;
1.5 nicm 69: struct cmd_if_shell_data *cdata;
1.27 nicm 70: char *shellcmd, *cmd, *cause;
71: struct cmd_list *cmdlist;
1.48 nicm 72: struct cmdq_item *new_item;
1.51 nicm 73: struct client *c = item->state.c;
1.48 nicm 74: struct session *s = item->state.tflag.s;
75: struct winlink *wl = item->state.tflag.wl;
76: struct window_pane *wp = item->state.tflag.wp;
1.34 nicm 77: const char *cwd;
1.19 nicm 78:
1.48 nicm 79: if (item->client != NULL && item->client->session == NULL)
80: cwd = item->client->cwd;
1.38 nicm 81: else if (s != NULL)
82: cwd = s->cwd;
83: else
84: cwd = NULL;
1.41 nicm 85:
1.51 nicm 86: shellcmd = format_single(item, args->argv[0], c, s, wl, wp);
1.27 nicm 87: if (args_has(args, 'F')) {
88: cmd = NULL;
89: if (*shellcmd != '0' && *shellcmd != '\0')
90: cmd = args->argv[1];
91: else if (args->argc == 3)
92: cmd = args->argv[2];
1.35 nicm 93: free(shellcmd);
1.27 nicm 94: if (cmd == NULL)
95: return (CMD_RETURN_NORMAL);
1.49 nicm 96: cmdlist = cmd_string_parse(cmd, NULL, 0, &cause);
97: if (cmdlist == NULL) {
1.27 nicm 98: if (cause != NULL) {
1.48 nicm 99: cmdq_error(item, "%s", cause);
1.27 nicm 100: free(cause);
101: }
102: return (CMD_RETURN_ERROR);
103: }
1.53 nicm 104: new_item = cmdq_get_command(cmdlist, NULL, &shared->mouse, 0);
1.48 nicm 105: cmdq_insert_after(item, new_item);
1.27 nicm 106: cmd_list_free(cmdlist);
107: return (CMD_RETURN_NORMAL);
108: }
1.5 nicm 109:
1.44 nicm 110: cdata = xcalloc(1, sizeof *cdata);
111: if (self->file != NULL) {
112: cdata->file = xstrdup(self->file);
113: cdata->line = self->line;
114: }
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.30 nicm 121:
1.48 nicm 122: cdata->client = item->client;
1.54 ! nicm 123: if (cdata->client != NULL)
! 124: cdata->client->references++;
1.5 nicm 125:
1.47 nicm 126: if (!args_has(args, 'b'))
1.48 nicm 127: cdata->item = item;
1.47 nicm 128: else
1.48 nicm 129: cdata->item = NULL;
1.53 nicm 130: memcpy(&cdata->mouse, &shared->mouse, sizeof cdata->mouse);
1.5 nicm 131:
1.52 nicm 132: job_run(shellcmd, s, cwd, NULL, cmd_if_shell_callback,
133: cmd_if_shell_free, cdata);
1.19 nicm 134: free(shellcmd);
1.1 nicm 135:
1.47 nicm 136: if (args_has(args, 'b'))
1.20 nicm 137: return (CMD_RETURN_NORMAL);
138: return (CMD_RETURN_WAIT);
1.1 nicm 139: }
140:
1.47 nicm 141: static enum cmd_retval
1.48 nicm 142: cmd_if_shell_error(struct cmdq_item *item, void *data)
1.47 nicm 143: {
144: char *error = data;
145:
1.48 nicm 146: cmdq_error(item, "%s", error);
1.47 nicm 147: free(error);
148:
149: return (CMD_RETURN_NORMAL);
150: }
151:
1.45 nicm 152: static void
1.5 nicm 153: cmd_if_shell_callback(struct job *job)
1.1 nicm 154: {
1.5 nicm 155: struct cmd_if_shell_data *cdata = job->data;
1.47 nicm 156: struct client *c = cdata->client;
1.1 nicm 157: struct cmd_list *cmdlist;
1.48 nicm 158: struct cmdq_item *new_item;
1.47 nicm 159: char *cause, *cmd, *file = cdata->file;
160: u_int line = cdata->line;
1.20 nicm 161:
162: if (!WIFEXITED(job->status) || WEXITSTATUS(job->status) != 0)
1.13 nicm 163: cmd = cdata->cmd_else;
1.20 nicm 164: else
1.13 nicm 165: cmd = cdata->cmd_if;
1.20 nicm 166: if (cmd == NULL)
1.47 nicm 167: goto out;
1.20 nicm 168:
1.49 nicm 169: cmdlist = cmd_string_parse(cmd, file, line, &cause);
170: if (cmdlist == NULL) {
1.47 nicm 171: if (cause != NULL)
1.48 nicm 172: new_item = cmdq_get_callback(cmd_if_shell_error, cause);
1.47 nicm 173: else
1.48 nicm 174: new_item = NULL;
1.47 nicm 175: } else {
1.48 nicm 176: new_item = cmdq_get_command(cmdlist, NULL, &cdata->mouse, 0);
1.47 nicm 177: cmd_list_free(cmdlist);
1.1 nicm 178: }
179:
1.48 nicm 180: if (new_item != NULL) {
181: if (cdata->item == NULL)
182: cmdq_append(c, new_item);
1.47 nicm 183: else
1.48 nicm 184: cmdq_insert_after(cdata->item, new_item);
1.47 nicm 185: }
1.25 nicm 186:
1.47 nicm 187: out:
1.48 nicm 188: if (cdata->item != NULL)
189: cdata->item->flags &= ~CMDQ_WAITING;
1.20 nicm 190: }
191:
1.45 nicm 192: static void
1.5 nicm 193: cmd_if_shell_free(void *data)
1.1 nicm 194: {
1.5 nicm 195: struct cmd_if_shell_data *cdata = data;
1.1 nicm 196:
1.54 ! nicm 197: if (cdata->client != NULL)
! 198: server_client_unref(cdata->client);
1.1 nicm 199:
1.15 nicm 200: free(cdata->cmd_else);
201: free(cdata->cmd_if);
1.44 nicm 202:
203: free(cdata->file);
1.15 nicm 204: free(cdata);
1.1 nicm 205: }