xref: /openbsd-src/usr.bin/tmux/cmd-if-shell.c (revision 7350f337b9e3eb4461d99580e625c7ef148d107c)
1 /* $OpenBSD: cmd-if-shell.c,v 1.64 2019/06/18 11:08:42 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 },
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_parse_input	 input;
53 
54 	char			*cmd_if;
55 	char			*cmd_else;
56 
57 	struct client		*client;
58 	struct cmdq_item	*item;
59 	struct mouse_event	 mouse;
60 };
61 
62 static enum cmd_retval
63 cmd_if_shell_exec(struct cmd *self, struct cmdq_item *item)
64 {
65 	struct args			*args = self->args;
66 	struct mouse_event		*m = &item->shared->mouse;
67 	struct cmd_if_shell_data	*cdata;
68 	char				*shellcmd, *cmd;
69 	struct cmdq_item		*new_item;
70 	struct cmd_find_state		*fs = &item->target;
71 	struct client			*c = cmd_find_client(item, NULL, 1);
72 	struct session			*s = fs->s;
73 	struct winlink			*wl = fs->wl;
74 	struct window_pane		*wp = fs->wp;
75 	struct cmd_parse_input		 pi;
76 	struct cmd_parse_result		*pr;
77 
78 	shellcmd = format_single(item, args->argv[0], c, s, wl, wp);
79 	if (args_has(args, 'F')) {
80 		if (*shellcmd != '0' && *shellcmd != '\0')
81 			cmd = args->argv[1];
82 		else if (args->argc == 3)
83 			cmd = args->argv[2];
84 		else
85 			cmd = NULL;
86 		free(shellcmd);
87 		if (cmd == NULL)
88 			return (CMD_RETURN_NORMAL);
89 
90 		memset(&pi, 0, sizeof pi);
91 		if (self->file != NULL)
92 			pi.file = self->file;
93 		pi.line = self->line;
94 		pi.item = item;
95 		pi.c = c;
96 		cmd_find_copy_state(&pi.fs, fs);
97 
98 		pr = cmd_parse_from_string(cmd, &pi);
99 		switch (pr->status) {
100 		case CMD_PARSE_EMPTY:
101 			break;
102 		case CMD_PARSE_ERROR:
103 			cmdq_error(item, "%s", pr->error);
104 			free(pr->error);
105 			return (CMD_RETURN_ERROR);
106 		case CMD_PARSE_SUCCESS:
107 			new_item = cmdq_get_command(pr->cmdlist, fs, m, 0);
108 			cmdq_insert_after(item, new_item);
109 			cmd_list_free(pr->cmdlist);
110 			break;
111 		}
112 		return (CMD_RETURN_NORMAL);
113 	}
114 
115 	cdata = xcalloc(1, sizeof *cdata);
116 
117 	cdata->cmd_if = xstrdup(args->argv[1]);
118 	if (args->argc == 3)
119 		cdata->cmd_else = xstrdup(args->argv[2]);
120 	else
121 		cdata->cmd_else = NULL;
122 	memcpy(&cdata->mouse, m, sizeof cdata->mouse);
123 
124 	cdata->client = item->client;
125 	if (cdata->client != NULL)
126 		cdata->client->references++;
127 
128 	if (!args_has(args, 'b'))
129 		cdata->item = item;
130 	else
131 		cdata->item = NULL;
132 
133 	memset(&cdata->input, 0, sizeof cdata->input);
134 	if (self->file != NULL)
135 		cdata->input.file = xstrdup(self->file);
136 	cdata->input.line = self->line;
137 	cdata->input.item = cdata->item;
138 	cdata->input.c = c;
139 	if (cdata->input.c != NULL)
140 		cdata->input.c->references++;
141 	cmd_find_copy_state(&cdata->input.fs, fs);
142 
143 	if (job_run(shellcmd, s, server_client_get_cwd(item->client, s), NULL,
144 	    cmd_if_shell_callback, cmd_if_shell_free, cdata, 0) == NULL) {
145 		cmdq_error(item, "failed to run command: %s", shellcmd);
146 		free(shellcmd);
147 		free(cdata);
148 		return (CMD_RETURN_ERROR);
149 	}
150 	free(shellcmd);
151 
152 	if (args_has(args, 'b'))
153 		return (CMD_RETURN_NORMAL);
154 	return (CMD_RETURN_WAIT);
155 }
156 
157 static void
158 cmd_if_shell_callback(struct job *job)
159 {
160 	struct cmd_if_shell_data	*cdata = job_get_data(job);
161 	struct client			*c = cdata->client;
162 	struct mouse_event		*m = &cdata->mouse;
163 	struct cmdq_item		*new_item = NULL;
164 	char				*cmd;
165 	int				 status;
166 	struct cmd_parse_result		*pr;
167 
168 	status = job_get_status(job);
169 	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
170 		cmd = cdata->cmd_else;
171 	else
172 		cmd = cdata->cmd_if;
173 	if (cmd == NULL)
174 		goto out;
175 
176 	pr = cmd_parse_from_string(cmd, &cdata->input);
177 	switch (pr->status) {
178 	case CMD_PARSE_EMPTY:
179 		break;
180 	case CMD_PARSE_ERROR:
181 		if (cdata->item != NULL)
182 		       cmdq_error(cdata->item, "%s", pr->error);
183 		free(pr->error);
184 		break;
185 	case CMD_PARSE_SUCCESS:
186 		new_item = cmdq_get_command(pr->cmdlist, NULL, m, 0);
187 		cmd_list_free(pr->cmdlist);
188 		break;
189 	}
190 	if (new_item != NULL) {
191 		if (cdata->item == NULL)
192 			cmdq_append(c, new_item);
193 		else
194 			cmdq_insert_after(cdata->item, new_item);
195 	}
196 
197 out:
198 	if (cdata->item != NULL)
199 		cmdq_continue(cdata->item);
200 }
201 
202 static void
203 cmd_if_shell_free(void *data)
204 {
205 	struct cmd_if_shell_data	*cdata = data;
206 
207 	if (cdata->client != NULL)
208 		server_client_unref(cdata->client);
209 
210 	free(cdata->cmd_else);
211 	free(cdata->cmd_if);
212 
213 	if (cdata->input.c != NULL)
214 		server_client_unref(cdata->input.c);
215 	free((void *)cdata->input.file);
216 
217 	free(cdata);
218 }
219