Annotation of src/usr.bin/tmux/cmd-if-shell.c, Revision 1.28
1.28 ! nicm 1: /* $OpenBSD: cmd-if-shell.c,v 1.27 2014/12/02 23:39:02 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.13 nicm 47: char *cmd_if;
48: char *cmd_else;
1.20 nicm 49: struct cmd_q *cmdq;
50: int bflag;
51: int started;
1.5 nicm 52: };
1.1 nicm 53:
1.16 nicm 54: enum cmd_retval
1.20 nicm 55: cmd_if_shell_exec(struct cmd *self, struct cmd_q *cmdq)
1.1 nicm 56: {
1.10 nicm 57: struct args *args = self->args;
1.5 nicm 58: struct cmd_if_shell_data *cdata;
1.27 nicm 59: char *shellcmd, *cmd, *cause;
60: struct cmd_list *cmdlist;
1.23 nicm 61: struct client *c;
1.21 nicm 62: struct session *s = NULL;
63: struct winlink *wl = NULL;
64: struct window_pane *wp = NULL;
1.19 nicm 65: struct format_tree *ft;
66:
1.21 nicm 67: if (args_has(args, 't'))
68: wl = cmd_find_pane(cmdq, args_get(args, 't'), &s, &wp);
1.23 nicm 69: else {
70: c = cmd_find_client(cmdq, NULL, 1);
71: if (c != NULL && c->session != NULL) {
72: s = c->session;
73: wl = s->curw;
74: wp = wl->window->active;
75: }
76: }
1.19 nicm 77:
78: ft = format_create();
1.28 ! nicm 79: format_defaults(ft, NULL, s, wl, wp);
1.19 nicm 80: shellcmd = format_expand(ft, args->argv[0]);
81: format_free(ft);
1.27 nicm 82:
83: if (args_has(args, 'F')) {
84: cmd = NULL;
85: if (*shellcmd != '0' && *shellcmd != '\0')
86: cmd = args->argv[1];
87: else if (args->argc == 3)
88: cmd = args->argv[2];
89: if (cmd == NULL)
90: return (CMD_RETURN_NORMAL);
91: if (cmd_string_parse(cmd, &cmdlist, NULL, 0, &cause) != 0) {
92: if (cause != NULL) {
93: cmdq_error(cmdq, "%s", cause);
94: free(cause);
95: }
96: return (CMD_RETURN_ERROR);
97: }
98: cmdq_run(cmdq, cmdlist);
99: cmd_list_free(cmdlist);
100: return (CMD_RETURN_NORMAL);
101: }
1.5 nicm 102:
103: cdata = xmalloc(sizeof *cdata);
1.13 nicm 104: cdata->cmd_if = xstrdup(args->argv[1]);
1.14 nicm 105: if (args->argc == 3)
106: cdata->cmd_else = xstrdup(args->argv[2]);
1.13 nicm 107: else
108: cdata->cmd_else = NULL;
1.20 nicm 109: cdata->bflag = args_has(args, 'b');
1.5 nicm 110:
1.20 nicm 111: cdata->started = 0;
112: cdata->cmdq = cmdq;
113: cmdq->references++;
1.5 nicm 114:
1.22 nicm 115: job_run(shellcmd, s, cmd_if_shell_callback, cmd_if_shell_free, cdata);
1.19 nicm 116: free(shellcmd);
1.1 nicm 117:
1.20 nicm 118: if (cdata->bflag)
119: return (CMD_RETURN_NORMAL);
120: return (CMD_RETURN_WAIT);
1.1 nicm 121: }
122:
1.5 nicm 123: void
124: cmd_if_shell_callback(struct job *job)
1.1 nicm 125: {
1.5 nicm 126: struct cmd_if_shell_data *cdata = job->data;
1.20 nicm 127: struct cmd_q *cmdq = cdata->cmdq, *cmdq1;
1.1 nicm 128: struct cmd_list *cmdlist;
1.14 nicm 129: char *cause, *cmd;
1.1 nicm 130:
1.20 nicm 131: if (cmdq->dead)
132: return;
133:
134: if (!WIFEXITED(job->status) || WEXITSTATUS(job->status) != 0)
1.13 nicm 135: cmd = cdata->cmd_else;
1.20 nicm 136: else
1.13 nicm 137: cmd = cdata->cmd_if;
1.20 nicm 138: if (cmd == NULL)
139: return;
140:
141: if (cmd_string_parse(cmd, &cmdlist, NULL, 0, &cause) != 0) {
1.1 nicm 142: if (cause != NULL) {
1.20 nicm 143: cmdq_error(cmdq, "%s", cause);
1.15 nicm 144: free(cause);
1.1 nicm 145: }
1.5 nicm 146: return;
1.1 nicm 147: }
148:
1.20 nicm 149: cdata->started = 1;
150:
151: cmdq1 = cmdq_new(cmdq->client);
152: cmdq1->emptyfn = cmd_if_shell_done;
153: cmdq1->data = cdata;
154:
155: cmdq_run(cmdq1, cmdlist);
1.1 nicm 156: cmd_list_free(cmdlist);
157: }
158:
159: void
1.20 nicm 160: cmd_if_shell_done(struct cmd_q *cmdq1)
161: {
162: struct cmd_if_shell_data *cdata = cmdq1->data;
163: struct cmd_q *cmdq = cdata->cmdq;
1.25 nicm 164:
165: if (cmdq1->client_exit >= 0)
166: cmdq->client_exit = cmdq1->client_exit;
1.20 nicm 167:
168: if (!cmdq_free(cmdq) && !cdata->bflag)
169: cmdq_continue(cmdq);
170:
171: cmdq_free(cmdq1);
172:
173: free(cdata->cmd_else);
174: free(cdata->cmd_if);
175: free(cdata);
176: }
177:
178: void
1.5 nicm 179: cmd_if_shell_free(void *data)
1.1 nicm 180: {
1.5 nicm 181: struct cmd_if_shell_data *cdata = data;
1.20 nicm 182: struct cmd_q *cmdq = cdata->cmdq;
183:
184: if (cdata->started)
185: return;
1.1 nicm 186:
1.20 nicm 187: if (!cmdq_free(cmdq) && !cdata->bflag)
188: cmdq_continue(cmdq);
1.1 nicm 189:
1.15 nicm 190: free(cdata->cmd_else);
191: free(cdata->cmd_if);
192: free(cdata);
1.1 nicm 193: }