199e242abSchristos /* $OpenBSD$ */
2698d5317Sjmmv
3698d5317Sjmmv /*
4698d5317Sjmmv * Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
5698d5317Sjmmv * Copyright (c) 2009 Nicholas Marriott <nicm@openbsd.org>
6698d5317Sjmmv *
7698d5317Sjmmv * Permission to use, copy, modify, and distribute this software for any
8698d5317Sjmmv * purpose with or without fee is hereby granted, provided that the above
9698d5317Sjmmv * copyright notice and this permission notice appear in all copies.
10698d5317Sjmmv *
11698d5317Sjmmv * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12698d5317Sjmmv * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13698d5317Sjmmv * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14698d5317Sjmmv * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15698d5317Sjmmv * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
16698d5317Sjmmv * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
17698d5317Sjmmv * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18698d5317Sjmmv */
19698d5317Sjmmv
20698d5317Sjmmv #include <sys/types.h>
21698d5317Sjmmv #include <sys/wait.h>
22698d5317Sjmmv
23*46548964Swiz #include <ctype.h>
2461fba46bSchristos #include <stdlib.h>
25698d5317Sjmmv #include <string.h>
26698d5317Sjmmv
27698d5317Sjmmv #include "tmux.h"
28698d5317Sjmmv
29698d5317Sjmmv /*
3061fba46bSchristos * Executes a tmux command if a shell command returns true or false.
31698d5317Sjmmv */
32698d5317Sjmmv
33*46548964Swiz static enum args_parse_type cmd_if_shell_args_parse(struct args *, u_int,
34*46548964Swiz char **);
35*46548964Swiz static enum cmd_retval cmd_if_shell_exec(struct cmd *,
36*46548964Swiz struct cmdq_item *);
37698d5317Sjmmv
38e9a2d6faSchristos static void cmd_if_shell_callback(struct job *);
39e9a2d6faSchristos static void cmd_if_shell_free(void *);
40698d5317Sjmmv
41698d5317Sjmmv const struct cmd_entry cmd_if_shell_entry = {
42f26e8bc9Schristos .name = "if-shell",
43f26e8bc9Schristos .alias = "if",
44f26e8bc9Schristos
45*46548964Swiz .args = { "bFt:", 2, 3, cmd_if_shell_args_parse },
46f26e8bc9Schristos .usage = "[-bF] " CMD_TARGET_PANE_USAGE " shell-command command "
47f26e8bc9Schristos "[command]",
48f26e8bc9Schristos
49fe99a117Schristos .target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
50f26e8bc9Schristos
51f26e8bc9Schristos .flags = 0,
52f26e8bc9Schristos .exec = cmd_if_shell_exec
53698d5317Sjmmv };
54698d5317Sjmmv
55698d5317Sjmmv struct cmd_if_shell_data {
56*46548964Swiz struct args_command_state *cmd_if;
57*46548964Swiz struct args_command_state *cmd_else;
5899e242abSchristos
59e9a2d6faSchristos struct client *client;
60e9a2d6faSchristos struct cmdq_item *item;
61698d5317Sjmmv };
62698d5317Sjmmv
63*46548964Swiz static enum args_parse_type
cmd_if_shell_args_parse(__unused struct args * args,u_int idx,__unused char ** cause)64*46548964Swiz cmd_if_shell_args_parse(__unused struct args *args, u_int idx,
65*46548964Swiz __unused char **cause)
66*46548964Swiz {
67*46548964Swiz if (idx == 1 || idx == 2)
68*46548964Swiz return (ARGS_PARSE_COMMANDS_OR_STRING);
69*46548964Swiz return (ARGS_PARSE_STRING);
70*46548964Swiz }
71*46548964Swiz
72e9a2d6faSchristos static enum cmd_retval
cmd_if_shell_exec(struct cmd * self,struct cmdq_item * item)73e9a2d6faSchristos cmd_if_shell_exec(struct cmd *self, struct cmdq_item *item)
74698d5317Sjmmv {
75e271dbb8Schristos struct args *args = cmd_get_args(self);
76e271dbb8Schristos struct cmd_find_state *target = cmdq_get_target(item);
77698d5317Sjmmv struct cmd_if_shell_data *cdata;
78*46548964Swiz struct cmdq_item *new_item;
79*46548964Swiz char *shellcmd;
80e271dbb8Schristos struct client *tc = cmdq_get_target_client(item);
81e271dbb8Schristos struct session *s = target->s;
82*46548964Swiz struct cmd_list *cmdlist;
83*46548964Swiz u_int count = args_count(args);
84*46548964Swiz int wait = !args_has(args, 'b');
8561fba46bSchristos
86*46548964Swiz shellcmd = format_single_from_target(item, args_string(args, 0));
8799e242abSchristos if (args_has(args, 'F')) {
8899e242abSchristos if (*shellcmd != '0' && *shellcmd != '\0')
89*46548964Swiz cmdlist = args_make_commands_now(self, item, 1, 0);
90*46548964Swiz else if (count == 3)
91*46548964Swiz cmdlist = args_make_commands_now(self, item, 2, 0);
92*46548964Swiz else {
93f26e8bc9Schristos free(shellcmd);
9499e242abSchristos return (CMD_RETURN_NORMAL);
9530744affSchristos }
96*46548964Swiz free(shellcmd);
97*46548964Swiz if (cmdlist == NULL)
98*46548964Swiz return (CMD_RETURN_ERROR);
99*46548964Swiz new_item = cmdq_get_command(cmdlist, cmdq_get_state(item));
100*46548964Swiz cmdq_insert_after(item, new_item);
10199e242abSchristos return (CMD_RETURN_NORMAL);
10299e242abSchristos }
10399e242abSchristos
104e9a2d6faSchristos cdata = xcalloc(1, sizeof *cdata);
10599e242abSchristos
106*46548964Swiz cdata->cmd_if = args_make_commands_prepare(self, item, 1, NULL, wait,
107*46548964Swiz 0);
108*46548964Swiz if (count == 3) {
109*46548964Swiz cdata->cmd_else = args_make_commands_prepare(self, item, 2,
110*46548964Swiz NULL, wait, 0);
111*46548964Swiz }
11299e242abSchristos
113*46548964Swiz if (wait) {
114e271dbb8Schristos cdata->client = cmdq_get_client(item);
115*46548964Swiz cdata->item = item;
116*46548964Swiz } else
117e271dbb8Schristos cdata->client = tc;
118fe99a117Schristos if (cdata->client != NULL)
119e9a2d6faSchristos cdata->client->references++;
120698d5317Sjmmv
121*46548964Swiz if (job_run(shellcmd, 0, NULL, NULL, s,
122e271dbb8Schristos server_client_get_cwd(cmdq_get_client(item), s), NULL,
123e271dbb8Schristos cmd_if_shell_callback, cmd_if_shell_free, cdata, 0, -1,
124e271dbb8Schristos -1) == NULL) {
1250a274e86Schristos cmdq_error(item, "failed to run command: %s", shellcmd);
1260a274e86Schristos free(shellcmd);
1270a274e86Schristos free(cdata);
1280a274e86Schristos return (CMD_RETURN_ERROR);
1290a274e86Schristos }
13061fba46bSchristos free(shellcmd);
131698d5317Sjmmv
132*46548964Swiz if (!wait)
13361fba46bSchristos return (CMD_RETURN_NORMAL);
13461fba46bSchristos return (CMD_RETURN_WAIT);
135698d5317Sjmmv }
136698d5317Sjmmv
137e9a2d6faSchristos static void
cmd_if_shell_callback(struct job * job)138698d5317Sjmmv cmd_if_shell_callback(struct job *job)
139698d5317Sjmmv {
1400a274e86Schristos struct cmd_if_shell_data *cdata = job_get_data(job);
141e9a2d6faSchristos struct client *c = cdata->client;
142*46548964Swiz struct cmdq_item *item = cdata->item, *new_item;
143*46548964Swiz struct args_command_state *state;
144*46548964Swiz struct cmd_list *cmdlist;
145*46548964Swiz char *error;
1460a274e86Schristos int status;
147698d5317Sjmmv
1480a274e86Schristos status = job_get_status(job);
1490a274e86Schristos if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
150*46548964Swiz state = cdata->cmd_else;
15161fba46bSchristos else
152*46548964Swiz state = cdata->cmd_if;
153*46548964Swiz if (state == NULL)
154e9a2d6faSchristos goto out;
155698d5317Sjmmv
156*46548964Swiz cmdlist = args_make_commands(state, 0, NULL, &error);
157*46548964Swiz if (cmdlist == NULL) {
158*46548964Swiz if (cdata->item == NULL) {
159*46548964Swiz *error = toupper((u_char)*error);
160*46548964Swiz status_message_set(c, -1, 1, 0, "%s", error);
161*46548964Swiz } else
162*46548964Swiz cmdq_error(cdata->item, "%s", error);
163*46548964Swiz free(error);
164*46548964Swiz } else if (item == NULL) {
165*46548964Swiz new_item = cmdq_get_command(cmdlist, NULL);
166e9a2d6faSchristos cmdq_append(c, new_item);
167*46548964Swiz } else {
168*46548964Swiz new_item = cmdq_get_command(cmdlist, cmdq_get_state(item));
169*46548964Swiz cmdq_insert_after(item, new_item);
17061fba46bSchristos }
17161fba46bSchristos
172e9a2d6faSchristos out:
173e9a2d6faSchristos if (cdata->item != NULL)
17430744affSchristos cmdq_continue(cdata->item);
175e9a2d6faSchristos }
176e9a2d6faSchristos
177e9a2d6faSchristos static void
cmd_if_shell_free(void * data)178698d5317Sjmmv cmd_if_shell_free(void *data)
179698d5317Sjmmv {
180698d5317Sjmmv struct cmd_if_shell_data *cdata = data;
181698d5317Sjmmv
182fe99a117Schristos if (cdata->client != NULL)
183e9a2d6faSchristos server_client_unref(cdata->client);
18461fba46bSchristos
185*46548964Swiz if (cdata->cmd_else != NULL)
186*46548964Swiz args_make_commands_free(cdata->cmd_else);
187*46548964Swiz args_make_commands_free(cdata->cmd_if);
18830744affSchristos
18961fba46bSchristos free(cdata);
190698d5317Sjmmv }
191