1 |
|
|
/* $OpenBSD: cmd-if-shell.c,v 1.43 2016/04/29 17:12:12 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 |
|
|
.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 *cmd_if; |
54 |
|
|
char *cmd_else; |
55 |
|
|
|
56 |
|
|
struct cmd_q *cmdq; |
57 |
|
|
struct mouse_event mouse; |
58 |
|
|
|
59 |
|
|
int bflag; |
60 |
|
|
int references; |
61 |
|
|
}; |
62 |
|
|
|
63 |
|
|
enum cmd_retval |
64 |
|
|
cmd_if_shell_exec(struct cmd *self, struct cmd_q *cmdq) |
65 |
|
|
{ |
66 |
|
|
struct args *args = self->args; |
67 |
|
|
struct cmd_if_shell_data *cdata; |
68 |
|
|
char *shellcmd, *cmd, *cause; |
69 |
|
|
struct cmd_list *cmdlist; |
70 |
|
|
struct session *s = cmdq->state.tflag.s; |
71 |
|
|
struct winlink *wl = cmdq->state.tflag.wl; |
72 |
|
|
struct window_pane *wp = cmdq->state.tflag.wp; |
73 |
|
|
struct format_tree *ft; |
74 |
|
|
const char *cwd; |
75 |
|
|
|
76 |
|
|
if (cmdq->client != NULL && cmdq->client->session == NULL) |
77 |
|
|
cwd = cmdq->client->cwd; |
78 |
|
|
else if (s != NULL) |
79 |
|
|
cwd = s->cwd; |
80 |
|
|
else |
81 |
|
|
cwd = NULL; |
82 |
|
|
|
83 |
|
|
ft = format_create(cmdq, 0); |
84 |
|
|
format_defaults(ft, cmdq->state.c, s, wl, wp); |
85 |
|
|
shellcmd = format_expand(ft, args->argv[0]); |
86 |
|
|
format_free(ft); |
87 |
|
|
|
88 |
|
|
if (args_has(args, 'F')) { |
89 |
|
|
cmd = NULL; |
90 |
|
|
if (*shellcmd != '0' && *shellcmd != '\0') |
91 |
|
|
cmd = args->argv[1]; |
92 |
|
|
else if (args->argc == 3) |
93 |
|
|
cmd = args->argv[2]; |
94 |
|
|
free(shellcmd); |
95 |
|
|
if (cmd == NULL) |
96 |
|
|
return (CMD_RETURN_NORMAL); |
97 |
|
|
if (cmd_string_parse(cmd, &cmdlist, NULL, 0, &cause) != 0) { |
98 |
|
|
if (cause != NULL) { |
99 |
|
|
cmdq_error(cmdq, "%s", cause); |
100 |
|
|
free(cause); |
101 |
|
|
} |
102 |
|
|
return (CMD_RETURN_ERROR); |
103 |
|
|
} |
104 |
|
|
cmdq_run(cmdq, cmdlist, &cmdq->item->mouse); |
105 |
|
|
cmd_list_free(cmdlist); |
106 |
|
|
return (CMD_RETURN_NORMAL); |
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
cdata = xmalloc(sizeof *cdata); |
110 |
|
|
|
111 |
|
|
cdata->cmd_if = xstrdup(args->argv[1]); |
112 |
|
|
if (args->argc == 3) |
113 |
|
|
cdata->cmd_else = xstrdup(args->argv[2]); |
114 |
|
|
else |
115 |
|
|
cdata->cmd_else = NULL; |
116 |
|
|
|
117 |
|
|
cdata->bflag = args_has(args, 'b'); |
118 |
|
|
|
119 |
|
|
cdata->cmdq = cmdq; |
120 |
|
|
memcpy(&cdata->mouse, &cmdq->item->mouse, sizeof cdata->mouse); |
121 |
|
|
cmdq->references++; |
122 |
|
|
|
123 |
|
|
cdata->references = 1; |
124 |
|
|
job_run(shellcmd, s, cwd, cmd_if_shell_callback, cmd_if_shell_free, |
125 |
|
|
cdata); |
126 |
|
|
free(shellcmd); |
127 |
|
|
|
128 |
|
|
if (cdata->bflag) |
129 |
|
|
return (CMD_RETURN_NORMAL); |
130 |
|
|
return (CMD_RETURN_WAIT); |
131 |
|
|
} |
132 |
|
|
|
133 |
|
|
void |
134 |
|
|
cmd_if_shell_callback(struct job *job) |
135 |
|
|
{ |
136 |
|
|
struct cmd_if_shell_data *cdata = job->data; |
137 |
|
|
struct cmd_q *cmdq = cdata->cmdq, *cmdq1; |
138 |
|
|
struct cmd_list *cmdlist; |
139 |
|
|
char *cause, *cmd; |
140 |
|
|
|
141 |
|
|
if (cmdq->flags & CMD_Q_DEAD) |
142 |
|
|
return; |
143 |
|
|
|
144 |
|
|
if (!WIFEXITED(job->status) || WEXITSTATUS(job->status) != 0) |
145 |
|
|
cmd = cdata->cmd_else; |
146 |
|
|
else |
147 |
|
|
cmd = cdata->cmd_if; |
148 |
|
|
if (cmd == NULL) |
149 |
|
|
return; |
150 |
|
|
|
151 |
|
|
if (cmd_string_parse(cmd, &cmdlist, NULL, 0, &cause) != 0) { |
152 |
|
|
if (cause != NULL) { |
153 |
|
|
cmdq_error(cmdq, "%s", cause); |
154 |
|
|
free(cause); |
155 |
|
|
} |
156 |
|
|
return; |
157 |
|
|
} |
158 |
|
|
|
159 |
|
|
cmdq1 = cmdq_new(cmdq->client); |
160 |
|
|
cmdq1->flags |= cmdq->flags & CMD_Q_NOHOOKS; |
161 |
|
|
cmdq1->emptyfn = cmd_if_shell_done; |
162 |
|
|
cmdq1->data = cdata; |
163 |
|
|
|
164 |
|
|
cdata->references++; |
165 |
|
|
cmdq_run(cmdq1, cmdlist, &cdata->mouse); |
166 |
|
|
cmd_list_free(cmdlist); |
167 |
|
|
} |
168 |
|
|
|
169 |
|
|
void |
170 |
|
|
cmd_if_shell_done(struct cmd_q *cmdq1) |
171 |
|
|
{ |
172 |
|
|
struct cmd_if_shell_data *cdata = cmdq1->data; |
173 |
|
|
struct cmd_q *cmdq = cdata->cmdq; |
174 |
|
|
|
175 |
|
|
if (cmdq1->client_exit >= 0) |
176 |
|
|
cmdq->client_exit = cmdq1->client_exit; |
177 |
|
|
cmdq_free(cmdq1); |
178 |
|
|
|
179 |
|
|
if (--cdata->references != 0) |
180 |
|
|
return; |
181 |
|
|
|
182 |
|
|
if (!cmdq_free(cmdq) && !cdata->bflag) |
183 |
|
|
cmdq_continue(cmdq); |
184 |
|
|
|
185 |
|
|
free(cdata->cmd_else); |
186 |
|
|
free(cdata->cmd_if); |
187 |
|
|
free(cdata); |
188 |
|
|
} |
189 |
|
|
|
190 |
|
|
void |
191 |
|
|
cmd_if_shell_free(void *data) |
192 |
|
|
{ |
193 |
|
|
struct cmd_if_shell_data *cdata = data; |
194 |
|
|
struct cmd_q *cmdq = cdata->cmdq; |
195 |
|
|
|
196 |
|
|
if (--cdata->references != 0) |
197 |
|
|
return; |
198 |
|
|
|
199 |
|
|
if (!cmdq_free(cmdq) && !cdata->bflag) |
200 |
|
|
cmdq_continue(cmdq); |
201 |
|
|
|
202 |
|
|
free(cdata->cmd_else); |
203 |
|
|
free(cdata->cmd_if); |
204 |
|
|
free(cdata); |
205 |
|
|
} |