xref: /openbsd-src/usr.bin/tmux/cmd-if-shell.c (revision 48950c12d106c85f315112191a0228d7b83b9510)
1 /* $OpenBSD: cmd-if-shell.c,v 1.23 2013/03/25 16:04:07 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 enum cmd_retval	 cmd_if_shell_exec(struct cmd *, struct cmd_q *);
33 
34 void	cmd_if_shell_callback(struct job *);
35 void	cmd_if_shell_done(struct cmd_q *);
36 void	cmd_if_shell_free(void *);
37 
38 const struct cmd_entry cmd_if_shell_entry = {
39 	"if-shell", "if",
40 	"bt:", 2, 3,
41 	"[-b] " CMD_TARGET_PANE_USAGE " shell-command command [command]",
42 	0,
43 	NULL,
44 	NULL,
45 	cmd_if_shell_exec
46 };
47 
48 struct cmd_if_shell_data {
49 	char		*cmd_if;
50 	char		*cmd_else;
51 	struct cmd_q	*cmdq;
52 	int		 bflag;
53 	int		 started;
54 };
55 
56 enum cmd_retval
57 cmd_if_shell_exec(struct cmd *self, struct cmd_q *cmdq)
58 {
59 	struct args			*args = self->args;
60 	struct cmd_if_shell_data	*cdata;
61 	char				*shellcmd;
62 	struct client			*c;
63 	struct session			*s = NULL;
64 	struct winlink			*wl = NULL;
65 	struct window_pane		*wp = NULL;
66 	struct format_tree		*ft;
67 
68 	if (args_has(args, 't'))
69 		wl = cmd_find_pane(cmdq, args_get(args, 't'), &s, &wp);
70 	else {
71 		c = cmd_find_client(cmdq, NULL, 1);
72 		if (c != NULL && c->session != NULL) {
73 			s = c->session;
74 			wl = s->curw;
75 			wp = wl->window->active;
76 		}
77 	}
78 
79 	ft = format_create();
80 	if (s != NULL)
81 		format_session(ft, s);
82 	if (s != NULL && wl != NULL)
83 		format_winlink(ft, s, wl);
84 	if (wp != NULL)
85 		format_window_pane(ft, wp);
86 	shellcmd = format_expand(ft, args->argv[0]);
87 	format_free(ft);
88 
89 	cdata = xmalloc(sizeof *cdata);
90 	cdata->cmd_if = xstrdup(args->argv[1]);
91 	if (args->argc == 3)
92 		cdata->cmd_else = xstrdup(args->argv[2]);
93 	else
94 		cdata->cmd_else = NULL;
95 	cdata->bflag = args_has(args, 'b');
96 
97 	cdata->started = 0;
98 	cdata->cmdq = cmdq;
99 	cmdq->references++;
100 
101 	job_run(shellcmd, s, cmd_if_shell_callback, cmd_if_shell_free, cdata);
102 	free(shellcmd);
103 
104 	if (cdata->bflag)
105 		return (CMD_RETURN_NORMAL);
106 	return (CMD_RETURN_WAIT);
107 }
108 
109 void
110 cmd_if_shell_callback(struct job *job)
111 {
112 	struct cmd_if_shell_data	*cdata = job->data;
113 	struct cmd_q			*cmdq = cdata->cmdq, *cmdq1;
114 	struct cmd_list			*cmdlist;
115 	char				*cause, *cmd;
116 
117 	if (cmdq->dead)
118 		return;
119 
120 	if (!WIFEXITED(job->status) || WEXITSTATUS(job->status) != 0)
121 		cmd = cdata->cmd_else;
122 	else
123 		cmd = cdata->cmd_if;
124 	if (cmd == NULL)
125 		return;
126 
127 	if (cmd_string_parse(cmd, &cmdlist, NULL, 0, &cause) != 0) {
128 		if (cause != NULL) {
129 			cmdq_error(cmdq, "%s", cause);
130 			free(cause);
131 		}
132 		return;
133 	}
134 
135 	cdata->started = 1;
136 
137 	cmdq1 = cmdq_new(cmdq->client);
138 	cmdq1->emptyfn = cmd_if_shell_done;
139 	cmdq1->data = cdata;
140 
141 	cmdq_run(cmdq1, cmdlist);
142 	cmd_list_free(cmdlist);
143 }
144 
145 void
146 cmd_if_shell_done(struct cmd_q *cmdq1)
147 {
148 	struct cmd_if_shell_data	*cdata = cmdq1->data;
149 	struct cmd_q			*cmdq = cdata->cmdq;
150 
151 	if (!cmdq_free(cmdq) && !cdata->bflag)
152 		cmdq_continue(cmdq);
153 
154 	cmdq_free(cmdq1);
155 
156 	free(cdata->cmd_else);
157 	free(cdata->cmd_if);
158 	free(cdata);
159 }
160 
161 void
162 cmd_if_shell_free(void *data)
163 {
164 	struct cmd_if_shell_data	*cdata = data;
165 	struct cmd_q			*cmdq = cdata->cmdq;
166 
167 	if (cdata->started)
168 		return;
169 
170 	if (!cmdq_free(cmdq) && !cdata->bflag)
171 		cmdq_continue(cmdq);
172 
173 	free(cdata->cmd_else);
174 	free(cdata->cmd_if);
175 	free(cdata);
176 }
177