Annotation of src/usr.bin/tmux/cmd-if-shell.c, Revision 1.35
1.35 ! nicm 1: /* $OpenBSD: cmd-if-shell.c,v 1.34 2015/10/31 08:13:58 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.20 nicm 32: enum cmd_retval cmd_if_shell_exec(struct cmd *, struct cmd_q *);
1.1 nicm 33:
1.5 nicm 34: void cmd_if_shell_callback(struct job *);
1.20 nicm 35: void cmd_if_shell_done(struct cmd_q *);
1.5 nicm 36: void cmd_if_shell_free(void *);
1.1 nicm 37:
38: const struct cmd_entry cmd_if_shell_entry = {
39: "if-shell", "if",
1.27 nicm 40: "bFt:", 2, 3,
41: "[-bF] " CMD_TARGET_PANE_USAGE " shell-command command [command]",
1.10 nicm 42: 0,
43: cmd_if_shell_exec
1.1 nicm 44: };
45:
1.5 nicm 46: struct cmd_if_shell_data {
1.30 nicm 47: char *cmd_if;
48: char *cmd_else;
49:
50: struct cmd_q *cmdq;
51: struct mouse_event mouse;
52:
53: int bflag;
1.31 nicm 54: int references;
1.5 nicm 55: };
1.1 nicm 56:
1.16 nicm 57: enum cmd_retval
1.20 nicm 58: cmd_if_shell_exec(struct cmd *self, struct cmd_q *cmdq)
1.1 nicm 59: {
1.10 nicm 60: struct args *args = self->args;
1.5 nicm 61: struct cmd_if_shell_data *cdata;
1.27 nicm 62: char *shellcmd, *cmd, *cause;
63: struct cmd_list *cmdlist;
1.23 nicm 64: struct client *c;
1.21 nicm 65: struct session *s = NULL;
66: struct winlink *wl = NULL;
67: struct window_pane *wp = NULL;
1.19 nicm 68: struct format_tree *ft;
1.34 nicm 69: const char *cwd;
1.19 nicm 70:
1.32 nicm 71: if (args_has(args, 't')) {
1.21 nicm 72: wl = cmd_find_pane(cmdq, args_get(args, 't'), &s, &wp);
1.32 nicm 73: cwd = wp->cwd;
74: } else {
1.23 nicm 75: c = cmd_find_client(cmdq, NULL, 1);
76: if (c != NULL && c->session != NULL) {
77: s = c->session;
78: wl = s->curw;
79: wp = wl->window->active;
80: }
1.32 nicm 81: if (cmdq->client != NULL && cmdq->client->session == NULL)
82: cwd = cmdq->client->cwd;
83: else if (s != NULL)
84: cwd = s->cwd;
85: else
1.34 nicm 86: cwd = NULL;
1.23 nicm 87: }
1.19 nicm 88:
89: ft = format_create();
1.28 nicm 90: format_defaults(ft, NULL, s, wl, wp);
1.19 nicm 91: shellcmd = format_expand(ft, args->argv[0]);
92: format_free(ft);
1.27 nicm 93:
94: if (args_has(args, 'F')) {
95: cmd = NULL;
96: if (*shellcmd != '0' && *shellcmd != '\0')
97: cmd = args->argv[1];
98: else if (args->argc == 3)
99: cmd = args->argv[2];
1.35 ! nicm 100: free(shellcmd);
1.27 nicm 101: if (cmd == NULL)
102: return (CMD_RETURN_NORMAL);
103: if (cmd_string_parse(cmd, &cmdlist, NULL, 0, &cause) != 0) {
104: if (cause != NULL) {
105: cmdq_error(cmdq, "%s", cause);
106: free(cause);
107: }
108: return (CMD_RETURN_ERROR);
109: }
1.30 nicm 110: cmdq_run(cmdq, cmdlist, &cmdq->item->mouse);
1.27 nicm 111: cmd_list_free(cmdlist);
112: return (CMD_RETURN_NORMAL);
113: }
1.5 nicm 114:
115: cdata = xmalloc(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.30 nicm 122:
1.20 nicm 123: cdata->bflag = args_has(args, 'b');
1.5 nicm 124:
1.20 nicm 125: cdata->cmdq = cmdq;
1.30 nicm 126: memcpy(&cdata->mouse, &cmdq->item->mouse, sizeof cdata->mouse);
1.20 nicm 127: cmdq->references++;
1.5 nicm 128:
1.31 nicm 129: cdata->references = 1;
1.32 nicm 130: job_run(shellcmd, s, cwd, cmd_if_shell_callback, cmd_if_shell_free,
131: cdata);
1.19 nicm 132: free(shellcmd);
1.1 nicm 133:
1.20 nicm 134: if (cdata->bflag)
135: return (CMD_RETURN_NORMAL);
136: return (CMD_RETURN_WAIT);
1.1 nicm 137: }
138:
1.5 nicm 139: void
140: cmd_if_shell_callback(struct job *job)
1.1 nicm 141: {
1.5 nicm 142: struct cmd_if_shell_data *cdata = job->data;
1.20 nicm 143: struct cmd_q *cmdq = cdata->cmdq, *cmdq1;
1.1 nicm 144: struct cmd_list *cmdlist;
1.14 nicm 145: char *cause, *cmd;
1.1 nicm 146:
1.33 nicm 147: if (cmdq->flags & CMD_Q_DEAD)
1.20 nicm 148: return;
149:
150: if (!WIFEXITED(job->status) || WEXITSTATUS(job->status) != 0)
1.13 nicm 151: cmd = cdata->cmd_else;
1.20 nicm 152: else
1.13 nicm 153: cmd = cdata->cmd_if;
1.20 nicm 154: if (cmd == NULL)
155: return;
156:
157: if (cmd_string_parse(cmd, &cmdlist, NULL, 0, &cause) != 0) {
1.1 nicm 158: if (cause != NULL) {
1.20 nicm 159: cmdq_error(cmdq, "%s", cause);
1.15 nicm 160: free(cause);
1.1 nicm 161: }
1.5 nicm 162: return;
1.1 nicm 163: }
164:
1.20 nicm 165: cmdq1 = cmdq_new(cmdq->client);
166: cmdq1->emptyfn = cmd_if_shell_done;
167: cmdq1->data = cdata;
168:
1.31 nicm 169: cdata->references++;
1.30 nicm 170: cmdq_run(cmdq1, cmdlist, &cdata->mouse);
1.1 nicm 171: cmd_list_free(cmdlist);
172: }
173:
174: void
1.20 nicm 175: cmd_if_shell_done(struct cmd_q *cmdq1)
176: {
177: struct cmd_if_shell_data *cdata = cmdq1->data;
178: struct cmd_q *cmdq = cdata->cmdq;
1.25 nicm 179:
180: if (cmdq1->client_exit >= 0)
181: cmdq->client_exit = cmdq1->client_exit;
1.31 nicm 182: cmdq_free(cmdq1);
183:
184: if (--cdata->references != 0)
185: return;
1.20 nicm 186:
187: if (!cmdq_free(cmdq) && !cdata->bflag)
188: cmdq_continue(cmdq);
189:
190: free(cdata->cmd_else);
191: free(cdata->cmd_if);
192: free(cdata);
193: }
194:
195: void
1.5 nicm 196: cmd_if_shell_free(void *data)
1.1 nicm 197: {
1.5 nicm 198: struct cmd_if_shell_data *cdata = data;
1.20 nicm 199: struct cmd_q *cmdq = cdata->cmdq;
200:
1.31 nicm 201: if (--cdata->references != 0)
1.20 nicm 202: return;
1.1 nicm 203:
1.20 nicm 204: if (!cmdq_free(cmdq) && !cdata->bflag)
205: cmdq_continue(cmdq);
1.1 nicm 206:
1.15 nicm 207: free(cdata->cmd_else);
208: free(cdata->cmd_if);
209: free(cdata);
1.1 nicm 210: }