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