xref: /openbsd-src/usr.bin/tmux/cmd-if-shell.c (revision d4c5fc9dc00f5a9cadd8c2de4e52d85d3c1c6003)
1 /* $OpenBSD: cmd-if-shell.c,v 1.58 2018/05/24 09:42:49 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 	char			*file;
53 	u_int			 line;
54 
55 	char			*cmd_if;
56 	char			*cmd_else;
57 
58 	struct client		*client;
59 	struct cmdq_item	*item;
60 	struct mouse_event	 mouse;
61 };
62 
63 static enum cmd_retval
64 cmd_if_shell_exec(struct cmd *self, struct cmdq_item *item)
65 {
66 	struct args			*args = self->args;
67 	struct cmdq_shared		*shared = item->shared;
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 client			*c = cmd_find_client(item, NULL, 1);
73 	struct session			*s = item->target.s;
74 	struct winlink			*wl = item->target.wl;
75 	struct window_pane		*wp = item->target.wp;
76 
77 	shellcmd = format_single(item, args->argv[0], c, s, wl, wp);
78 	if (args_has(args, 'F')) {
79 		cmd = NULL;
80 		if (*shellcmd != '0' && *shellcmd != '\0')
81 			cmd = args->argv[1];
82 		else if (args->argc == 3)
83 			cmd = args->argv[2];
84 		free(shellcmd);
85 		if (cmd == NULL)
86 			return (CMD_RETURN_NORMAL);
87 		cmdlist = cmd_string_parse(cmd, NULL, 0, &cause);
88 		if (cmdlist == NULL) {
89 			if (cause != NULL) {
90 				cmdq_error(item, "%s", cause);
91 				free(cause);
92 			}
93 			return (CMD_RETURN_ERROR);
94 		}
95 		new_item = cmdq_get_command(cmdlist, NULL, &shared->mouse, 0);
96 		cmdq_insert_after(item, new_item);
97 		cmd_list_free(cmdlist);
98 		return (CMD_RETURN_NORMAL);
99 	}
100 
101 	cdata = xcalloc(1, sizeof *cdata);
102 	if (self->file != NULL) {
103 		cdata->file = xstrdup(self->file);
104 		cdata->line = self->line;
105 	}
106 
107 	cdata->cmd_if = xstrdup(args->argv[1]);
108 	if (args->argc == 3)
109 		cdata->cmd_else = xstrdup(args->argv[2]);
110 	else
111 		cdata->cmd_else = NULL;
112 
113 	cdata->client = item->client;
114 	if (cdata->client != NULL)
115 		cdata->client->references++;
116 
117 	if (!args_has(args, 'b'))
118 		cdata->item = item;
119 	else
120 		cdata->item = NULL;
121 	memcpy(&cdata->mouse, &shared->mouse, sizeof cdata->mouse);
122 
123 	job_run(shellcmd, s, server_client_get_cwd(item->client, s), NULL,
124 	    cmd_if_shell_callback, cmd_if_shell_free, cdata, 0);
125 	free(shellcmd);
126 
127 	if (args_has(args, 'b'))
128 		return (CMD_RETURN_NORMAL);
129 	return (CMD_RETURN_WAIT);
130 }
131 
132 static void
133 cmd_if_shell_callback(struct job *job)
134 {
135 	struct cmd_if_shell_data	*cdata = job->data;
136 	struct client			*c = cdata->client;
137 	struct cmd_list			*cmdlist;
138 	struct cmdq_item		*new_item;
139 	char				*cause, *cmd, *file = cdata->file;
140 	u_int				 line = cdata->line;
141 
142 	if (!WIFEXITED(job->status) || WEXITSTATUS(job->status) != 0)
143 		cmd = cdata->cmd_else;
144 	else
145 		cmd = cdata->cmd_if;
146 	if (cmd == NULL)
147 		goto out;
148 
149 	cmdlist = cmd_string_parse(cmd, file, line, &cause);
150 	if (cmdlist == NULL) {
151 		if (cause != NULL && cdata->item != NULL)
152 			cmdq_error(cdata->item, "%s", cause);
153 		free(cause);
154 		new_item = NULL;
155 	} else {
156 		new_item = cmdq_get_command(cmdlist, NULL, &cdata->mouse, 0);
157 		cmd_list_free(cmdlist);
158 	}
159 
160 	if (new_item != NULL) {
161 		if (cdata->item == NULL)
162 			cmdq_append(c, new_item);
163 		else
164 			cmdq_insert_after(cdata->item, new_item);
165 	}
166 
167 out:
168 	if (cdata->item != NULL)
169 		cdata->item->flags &= ~CMDQ_WAITING;
170 }
171 
172 static void
173 cmd_if_shell_free(void *data)
174 {
175 	struct cmd_if_shell_data	*cdata = data;
176 
177 	if (cdata->client != NULL)
178 		server_client_unref(cdata->client);
179 
180 	free(cdata->cmd_else);
181 	free(cdata->cmd_if);
182 
183 	free(cdata->file);
184 	free(cdata);
185 }
186