xref: /openbsd/usr.bin/tmux/cmd-if-shell.c (revision 2db6a388)
1 /* $OpenBSD: cmd-if-shell.c,v 1.80 2021/08/23 12:33:55 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
5  * Copyright (c) 2009 Nicholas Marriott <nicm@openbsd.org>
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>
21 #include <sys/wait.h>
22 
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #include "tmux.h"
27 
28 /*
29  * Executes a tmux command if a shell command returns true or false.
30  */
31 
32 static enum cmd_retval	cmd_if_shell_exec(struct cmd *, struct cmdq_item *);
33 
34 static void	cmd_if_shell_callback(struct job *);
35 static void	cmd_if_shell_free(void *);
36 
37 const struct cmd_entry cmd_if_shell_entry = {
38 	.name = "if-shell",
39 	.alias = "if",
40 
41 	.args = { "bFt:", 2, 3, NULL },
42 	.usage = "[-bF] " CMD_TARGET_PANE_USAGE " shell-command command "
43 		 "[command]",
44 
45 	.target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
46 
47 	.flags = 0,
48 	.exec = cmd_if_shell_exec
49 };
50 
51 struct cmd_if_shell_data {
52 	struct cmd_list		*cmd_if;
53 	struct cmd_list		*cmd_else;
54 
55 	struct client		*client;
56 	struct cmdq_item	*item;
57 };
58 
59 static enum cmd_retval
60 cmd_if_shell_exec(struct cmd *self, struct cmdq_item *item)
61 {
62 	struct args			*args = cmd_get_args(self);
63 	struct cmd_find_state		*target = cmdq_get_target(item);
64 	struct cmd_if_shell_data	*cdata;
65 	struct cmdq_item		*new_item;
66 	char				*shellcmd;
67 	struct client			*tc = cmdq_get_target_client(item);
68 	struct session			*s = target->s;
69 	struct cmd_list			*cmdlist = NULL;
70 	u_int				 count = args_count(args);
71 
72 	shellcmd = format_single_from_target(item, args_string(args, 0));
73 	if (args_has(args, 'F')) {
74 		if (*shellcmd != '0' && *shellcmd != '\0')
75 			cmdlist = args_make_commands_now(self, item, 1);
76 		else if (count == 3)
77 			cmdlist = args_make_commands_now(self, item, 2);
78 		else {
79 			free(shellcmd);
80 			return (CMD_RETURN_NORMAL);
81 		}
82 		free(shellcmd);
83 		if (cmdlist == NULL)
84 			return (CMD_RETURN_ERROR);
85 		new_item = cmdq_get_command(cmdlist, cmdq_get_state(item));
86 		cmdq_insert_after(item, new_item);
87 		return (CMD_RETURN_NORMAL);
88 	}
89 
90 	cdata = xcalloc(1, sizeof *cdata);
91 
92 	cdata->cmd_if = args_make_commands_now(self, item, 1);
93 	if (cdata->cmd_if == NULL)
94 	    return (CMD_RETURN_ERROR);
95 	if (count == 3) {
96 		cdata->cmd_else = args_make_commands_now(self, item, 2);
97 		if (cdata->cmd_else == NULL)
98 		    return (CMD_RETURN_ERROR);
99 	}
100 
101 	if (!args_has(args, 'b'))
102 		cdata->client = cmdq_get_client(item);
103 	else
104 		cdata->client = tc;
105 	if (cdata->client != NULL)
106 		cdata->client->references++;
107 
108 	if (!args_has(args, 'b'))
109 		cdata->item = item;
110 
111 	if (job_run(shellcmd, 0, NULL, s,
112 	    server_client_get_cwd(cmdq_get_client(item), s), NULL,
113 	    cmd_if_shell_callback, cmd_if_shell_free, cdata, 0, -1,
114 	    -1) == NULL) {
115 		cmdq_error(item, "failed to run command: %s", shellcmd);
116 		free(shellcmd);
117 		free(cdata);
118 		return (CMD_RETURN_ERROR);
119 	}
120 	free(shellcmd);
121 
122 	if (args_has(args, 'b'))
123 		return (CMD_RETURN_NORMAL);
124 	return (CMD_RETURN_WAIT);
125 }
126 
127 static void
128 cmd_if_shell_callback(struct job *job)
129 {
130 	struct cmd_if_shell_data	*cdata = job_get_data(job);
131 	struct client			*c = cdata->client;
132 	struct cmdq_item		*item = cdata->item, *new_item;
133 	struct cmd_list			*cmdlist;
134 	int				 status;
135 
136 	status = job_get_status(job);
137 	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
138 		cmdlist = cdata->cmd_else;
139 	else
140 		cmdlist = cdata->cmd_if;
141 	if (cmdlist == NULL)
142 		goto out;
143 
144 	if (item == NULL) {
145 		new_item = cmdq_get_command(cmdlist, NULL);
146 		cmdq_append(c, new_item);
147 	} else {
148 		new_item = cmdq_get_command(cmdlist, cmdq_get_state(item));
149 		cmdq_insert_after(item, new_item);
150 	}
151 
152 out:
153 	if (cdata->item != NULL)
154 		cmdq_continue(cdata->item);
155 }
156 
157 static void
158 cmd_if_shell_free(void *data)
159 {
160 	struct cmd_if_shell_data	*cdata = data;
161 
162 	if (cdata->client != NULL)
163 		server_client_unref(cdata->client);
164 
165 	if (cdata->cmd_else != NULL)
166 		cmd_list_free(cdata->cmd_else);
167 	cmd_list_free(cdata->cmd_if);
168 
169 	free(cdata);
170 }
171