xref: /openbsd-src/usr.bin/tmux/cmd-if-shell.c (revision ba06422051ccbb677d8784759ac8e114b0db67e3)
1 /* $OpenBSD: cmd-if-shell.c,v 1.48 2016/10/16 19:04:05 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 enum cmd_retval	cmd_if_shell_error(struct cmdq_item *, void *);
35 static void		cmd_if_shell_callback(struct job *);
36 static void		cmd_if_shell_free(void *);
37 
38 const struct cmd_entry cmd_if_shell_entry = {
39 	.name = "if-shell",
40 	.alias = "if",
41 
42 	.args = { "bFt:", 2, 3 },
43 	.usage = "[-bF] " CMD_TARGET_PANE_USAGE " shell-command command "
44 		 "[command]",
45 
46 	.tflag = CMD_PANE_CANFAIL,
47 
48 	.flags = 0,
49 	.exec = cmd_if_shell_exec
50 };
51 
52 struct cmd_if_shell_data {
53 	char			*file;
54 	u_int			 line;
55 
56 	char			*cmd_if;
57 	char			*cmd_else;
58 
59 	struct client		*client;
60 	struct cmdq_item	*item;
61 	struct mouse_event	 mouse;
62 };
63 
64 static enum cmd_retval
65 cmd_if_shell_exec(struct cmd *self, struct cmdq_item *item)
66 {
67 	struct args			*args = self->args;
68 	struct cmd_if_shell_data	*cdata;
69 	char				*shellcmd, *cmd, *cause;
70 	struct cmd_list			*cmdlist;
71 	struct cmdq_item		*new_item;
72 	struct session			*s = item->state.tflag.s;
73 	struct winlink			*wl = item->state.tflag.wl;
74 	struct window_pane		*wp = item->state.tflag.wp;
75 	struct format_tree		*ft;
76 	const char			*cwd;
77 
78 	if (item->client != NULL && item->client->session == NULL)
79 		cwd = item->client->cwd;
80 	else if (s != NULL)
81 		cwd = s->cwd;
82 	else
83 		cwd = NULL;
84 
85 	ft = format_create(item, 0);
86 	format_defaults(ft, item->state.c, s, wl, wp);
87 	shellcmd = format_expand(ft, args->argv[0]);
88 	format_free(ft);
89 
90 	if (args_has(args, 'F')) {
91 		cmd = NULL;
92 		if (*shellcmd != '0' && *shellcmd != '\0')
93 			cmd = args->argv[1];
94 		else if (args->argc == 3)
95 			cmd = args->argv[2];
96 		free(shellcmd);
97 		if (cmd == NULL)
98 			return (CMD_RETURN_NORMAL);
99 		if (cmd_string_parse(cmd, &cmdlist, NULL, 0, &cause) != 0) {
100 			if (cause != NULL) {
101 				cmdq_error(item, "%s", cause);
102 				free(cause);
103 			}
104 			return (CMD_RETURN_ERROR);
105 		}
106 		new_item = cmdq_get_command(cmdlist, NULL, &item->mouse, 0);
107 		cmdq_insert_after(item, new_item);
108 		cmd_list_free(cmdlist);
109 		return (CMD_RETURN_NORMAL);
110 	}
111 
112 	cdata = xcalloc(1, sizeof *cdata);
113 	if (self->file != NULL) {
114 		cdata->file = xstrdup(self->file);
115 		cdata->line = self->line;
116 	}
117 
118 	cdata->cmd_if = xstrdup(args->argv[1]);
119 	if (args->argc == 3)
120 		cdata->cmd_else = xstrdup(args->argv[2]);
121 	else
122 		cdata->cmd_else = NULL;
123 
124 	cdata->client = item->client;
125 	cdata->client->references++;
126 
127 	if (!args_has(args, 'b'))
128 		cdata->item = item;
129 	else
130 		cdata->item = NULL;
131 	memcpy(&cdata->mouse, &item->mouse, sizeof cdata->mouse);
132 
133 	job_run(shellcmd, s, cwd, cmd_if_shell_callback, cmd_if_shell_free,
134 	    cdata);
135 	free(shellcmd);
136 
137 	if (args_has(args, 'b'))
138 		return (CMD_RETURN_NORMAL);
139 	return (CMD_RETURN_WAIT);
140 }
141 
142 static enum cmd_retval
143 cmd_if_shell_error(struct cmdq_item *item, void *data)
144 {
145 	char	*error = data;
146 
147 	cmdq_error(item, "%s", error);
148 	free(error);
149 
150 	return (CMD_RETURN_NORMAL);
151 }
152 
153 static void
154 cmd_if_shell_callback(struct job *job)
155 {
156 	struct cmd_if_shell_data	*cdata = job->data;
157 	struct client			*c = cdata->client;
158 	struct cmd_list			*cmdlist;
159 	struct cmdq_item		*new_item;
160 	char				*cause, *cmd, *file = cdata->file;
161 	u_int				 line = cdata->line;
162 
163 	if (!WIFEXITED(job->status) || WEXITSTATUS(job->status) != 0)
164 		cmd = cdata->cmd_else;
165 	else
166 		cmd = cdata->cmd_if;
167 	if (cmd == NULL)
168 		goto out;
169 
170 	if (cmd_string_parse(cmd, &cmdlist, file, line, &cause) != 0) {
171 		if (cause != NULL)
172 			new_item = cmdq_get_callback(cmd_if_shell_error, cause);
173 		else
174 			new_item = NULL;
175 	} else {
176 		new_item = cmdq_get_command(cmdlist, NULL, &cdata->mouse, 0);
177 		cmd_list_free(cmdlist);
178 	}
179 
180 	if (new_item != NULL) {
181 		if (cdata->item == NULL)
182 			cmdq_append(c, new_item);
183 		else
184 			cmdq_insert_after(cdata->item, new_item);
185 	}
186 
187 out:
188 	if (cdata->item != NULL)
189 		cdata->item->flags &= ~CMDQ_WAITING;
190 }
191 
192 static void
193 cmd_if_shell_free(void *data)
194 {
195 	struct cmd_if_shell_data	*cdata = data;
196 
197 	server_client_unref(cdata->client);
198 
199 	free(cdata->cmd_else);
200 	free(cdata->cmd_if);
201 
202 	free(cdata->file);
203 	free(cdata);
204 }
205