Add a command queue to standardize and simplify commands that call other
[tmux-openbsd.git] / cmd-if-shell.c
blobe2a459728b1bf1d97c4ef5a46d3273e2b8df25ed
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
5 * Copyright (c) 2009 Nicholas Marriott <nicm@openbsd.org>
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.
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.
20 #include <sys/types.h>
21 #include <sys/wait.h>
23 #include <stdlib.h>
24 #include <string.h>
26 #include "tmux.h"
29 * Executes a tmux command if a shell command returns true or false.
32 enum cmd_retval cmd_if_shell_exec(struct cmd *, struct cmd_q *);
34 void cmd_if_shell_callback(struct job *);
35 void cmd_if_shell_done(struct cmd_q *);
36 void cmd_if_shell_free(void *);
38 const struct cmd_entry cmd_if_shell_entry = {
39 "if-shell", "if",
40 "bt:", 2, 3,
41 "[-b] " CMD_TARGET_PANE_USAGE " shell-command command [command]",
43 NULL,
44 NULL,
45 cmd_if_shell_exec
48 struct cmd_if_shell_data {
49 char *cmd_if;
50 char *cmd_else;
51 struct cmd_q *cmdq;
52 int bflag;
53 int started;
56 enum cmd_retval
57 cmd_if_shell_exec(struct cmd *self, struct cmd_q *cmdq)
59 struct args *args = self->args;
60 struct cmd_if_shell_data *cdata;
61 char *shellcmd;
62 struct session *s;
63 struct winlink *wl;
64 struct window_pane *wp;
65 struct format_tree *ft;
67 wl = cmd_find_pane(cmdq, args_get(args, 't'), &s, &wp);
68 if (wl == NULL)
69 return (CMD_RETURN_ERROR);
71 ft = format_create();
72 format_session(ft, s);
73 format_winlink(ft, s, wl);
74 format_window_pane(ft, wp);
75 shellcmd = format_expand(ft, args->argv[0]);
76 format_free(ft);
78 cdata = xmalloc(sizeof *cdata);
79 cdata->cmd_if = xstrdup(args->argv[1]);
80 if (args->argc == 3)
81 cdata->cmd_else = xstrdup(args->argv[2]);
82 else
83 cdata->cmd_else = NULL;
84 cdata->bflag = args_has(args, 'b');
86 cdata->started = 0;
87 cdata->cmdq = cmdq;
88 cmdq->references++;
90 job_run(shellcmd, cmd_if_shell_callback, cmd_if_shell_free, cdata);
91 free(shellcmd);
93 if (cdata->bflag)
94 return (CMD_RETURN_NORMAL);
95 return (CMD_RETURN_WAIT);
98 void
99 cmd_if_shell_callback(struct job *job)
101 struct cmd_if_shell_data *cdata = job->data;
102 struct cmd_q *cmdq = cdata->cmdq, *cmdq1;
103 struct cmd_list *cmdlist;
104 char *cause, *cmd;
106 if (cmdq->dead)
107 return;
109 if (!WIFEXITED(job->status) || WEXITSTATUS(job->status) != 0)
110 cmd = cdata->cmd_else;
111 else
112 cmd = cdata->cmd_if;
113 if (cmd == NULL)
114 return;
116 if (cmd_string_parse(cmd, &cmdlist, NULL, 0, &cause) != 0) {
117 if (cause != NULL) {
118 cmdq_error(cmdq, "%s", cause);
119 free(cause);
121 return;
124 cdata->started = 1;
126 cmdq1 = cmdq_new(cmdq->client);
127 cmdq1->emptyfn = cmd_if_shell_done;
128 cmdq1->data = cdata;
130 cmdq_run(cmdq1, cmdlist);
131 cmd_list_free(cmdlist);
134 void
135 cmd_if_shell_done(struct cmd_q *cmdq1)
137 struct cmd_if_shell_data *cdata = cmdq1->data;
138 struct cmd_q *cmdq = cdata->cmdq;
140 if (!cmdq_free(cmdq) && !cdata->bflag)
141 cmdq_continue(cmdq);
143 cmdq_free(cmdq1);
145 free(cdata->cmd_else);
146 free(cdata->cmd_if);
147 free(cdata);
150 void
151 cmd_if_shell_free(void *data)
153 struct cmd_if_shell_data *cdata = data;
154 struct cmd_q *cmdq = cdata->cmdq;
156 if (cdata->started)
157 return;
159 if (!cmdq_free(cmdq) && !cdata->bflag)
160 cmdq_continue(cmdq);
162 free(cdata->cmd_else);
163 free(cdata->cmd_if);
164 free(cdata);