Add -M to always turn mouse on in a menu, GitHub issue 3779.
[tmux-openbsd.git] / cmd-run-shell.c
blobddb5b1b44f190e6f6d7305b91c7610eec6d25c4e
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
5 * Copyright (c) 2009 Nicholas Marriott <nicm@openbsd.org>
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.
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.
20 #include <sys/types.h>
21 #include <sys/wait.h>
23 #include <ctype.h>
24 #include <stdlib.h>
25 #include <string.h>
27 #include "tmux.h"
30 * Runs a command without a window.
33 static enum args_parse_type cmd_run_shell_args_parse(struct args *, u_int,
34 char **);
35 static enum cmd_retval cmd_run_shell_exec(struct cmd *,
36 struct cmdq_item *);
38 static void cmd_run_shell_timer(int, short, void *);
39 static void cmd_run_shell_callback(struct job *);
40 static void cmd_run_shell_free(void *);
41 static void cmd_run_shell_print(struct job *, const char *);
43 const struct cmd_entry cmd_run_shell_entry = {
44 .name = "run-shell",
45 .alias = "run",
47 .args = { "bd:Ct:c:", 0, 2, cmd_run_shell_args_parse },
48 .usage = "[-bC] [-c start-directory] [-d delay] " CMD_TARGET_PANE_USAGE
49 " [shell-command]",
51 .target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
53 .flags = 0,
54 .exec = cmd_run_shell_exec
57 struct cmd_run_shell_data {
58 struct client *client;
59 char *cmd;
60 struct args_command_state *state;
61 char *cwd;
62 struct cmdq_item *item;
63 struct session *s;
64 int wp_id;
65 struct event timer;
66 int flags;
69 static enum args_parse_type
70 cmd_run_shell_args_parse(struct args *args, __unused u_int idx,
71 __unused char **cause)
73 if (args_has(args, 'C'))
74 return (ARGS_PARSE_COMMANDS_OR_STRING);
75 return (ARGS_PARSE_STRING);
78 static void
79 cmd_run_shell_print(struct job *job, const char *msg)
81 struct cmd_run_shell_data *cdata = job_get_data(job);
82 struct window_pane *wp = NULL;
83 struct cmd_find_state fs;
84 struct window_mode_entry *wme;
86 if (cdata->wp_id != -1)
87 wp = window_pane_find_by_id(cdata->wp_id);
88 if (wp == NULL && cdata->item != NULL && cdata->client != NULL)
89 wp = server_client_get_pane(cdata->client);
90 if (wp == NULL && cmd_find_from_nothing(&fs, 0) == 0)
91 wp = fs.wp;
92 if (wp == NULL)
93 return;
95 wme = TAILQ_FIRST(&wp->modes);
96 if (wme == NULL || wme->mode != &window_view_mode)
97 window_pane_set_mode(wp, NULL, &window_view_mode, NULL, NULL);
98 window_copy_add(wp, 1, "%s", msg);
101 static enum cmd_retval
102 cmd_run_shell_exec(struct cmd *self, struct cmdq_item *item)
104 struct args *args = cmd_get_args(self);
105 struct cmd_find_state *target = cmdq_get_target(item);
106 struct cmd_run_shell_data *cdata;
107 struct client *c = cmdq_get_client(item);
108 struct client *tc = cmdq_get_target_client(item);
109 struct session *s = target->s;
110 struct window_pane *wp = target->wp;
111 const char *delay, *cmd;
112 double d;
113 struct timeval tv;
114 char *end;
115 int wait = !args_has(args, 'b');
117 if ((delay = args_get(args, 'd')) != NULL) {
118 d = strtod(delay, &end);
119 if (*end != '\0') {
120 cmdq_error(item, "invalid delay time: %s", delay);
121 return (CMD_RETURN_ERROR);
123 } else if (args_count(args) == 0)
124 return (CMD_RETURN_NORMAL);
126 cdata = xcalloc(1, sizeof *cdata);
127 if (!args_has(args, 'C')) {
128 cmd = args_string(args, 0);
129 if (cmd != NULL)
130 cdata->cmd = format_single_from_target(item, cmd);
131 } else {
132 cdata->state = args_make_commands_prepare(self, item, 0, NULL,
133 wait, 1);
136 if (args_has(args, 't') && wp != NULL)
137 cdata->wp_id = wp->id;
138 else
139 cdata->wp_id = -1;
141 if (wait) {
142 cdata->client = c;
143 cdata->item = item;
144 } else {
145 cdata->client = tc;
146 cdata->flags |= JOB_NOWAIT;
148 if (cdata->client != NULL)
149 cdata->client->references++;
150 if (args_has(args, 'c'))
151 cdata->cwd = xstrdup(args_get(args, 'c'));
152 else
153 cdata->cwd = xstrdup(server_client_get_cwd(c, s));
155 cdata->s = s;
156 if (s != NULL)
157 session_add_ref(s, __func__);
159 evtimer_set(&cdata->timer, cmd_run_shell_timer, cdata);
160 if (delay != NULL) {
161 timerclear(&tv);
162 tv.tv_sec = (time_t)d;
163 tv.tv_usec = (d - (double)tv.tv_sec) * 1000000U;
164 evtimer_add(&cdata->timer, &tv);
165 } else
166 event_active(&cdata->timer, EV_TIMEOUT, 1);
168 if (!wait)
169 return (CMD_RETURN_NORMAL);
170 return (CMD_RETURN_WAIT);
173 static void
174 cmd_run_shell_timer(__unused int fd, __unused short events, void* arg)
176 struct cmd_run_shell_data *cdata = arg;
177 struct client *c = cdata->client;
178 const char *cmd = cdata->cmd;
179 struct cmdq_item *item = cdata->item, *new_item;
180 struct cmd_list *cmdlist;
181 char *error;
183 if (cdata->state == NULL) {
184 if (cmd == NULL) {
185 if (cdata->item != NULL)
186 cmdq_continue(cdata->item);
187 cmd_run_shell_free(cdata);
188 return;
190 if (job_run(cmd, 0, NULL, NULL, cdata->s, cdata->cwd, NULL,
191 cmd_run_shell_callback, cmd_run_shell_free, cdata,
192 cdata->flags, -1, -1) == NULL)
193 cmd_run_shell_free(cdata);
194 return;
197 cmdlist = args_make_commands(cdata->state, 0, NULL, &error);
198 if (cmdlist == NULL) {
199 if (cdata->item == NULL) {
200 *error = toupper((u_char)*error);
201 status_message_set(c, -1, 1, 0, "%s", error);
202 } else
203 cmdq_error(cdata->item, "%s", error);
204 free(error);
205 } else if (item == NULL) {
206 new_item = cmdq_get_command(cmdlist, NULL);
207 cmdq_append(c, new_item);
208 } else {
209 new_item = cmdq_get_command(cmdlist, cmdq_get_state(item));
210 cmdq_insert_after(item, new_item);
213 if (cdata->item != NULL)
214 cmdq_continue(cdata->item);
215 cmd_run_shell_free(cdata);
218 static void
219 cmd_run_shell_callback(struct job *job)
221 struct cmd_run_shell_data *cdata = job_get_data(job);
222 struct bufferevent *event = job_get_event(job);
223 struct cmdq_item *item = cdata->item;
224 char *cmd = cdata->cmd, *msg = NULL, *line;
225 size_t size;
226 int retcode, status;
228 do {
229 line = evbuffer_readln(event->input, NULL, EVBUFFER_EOL_LF);
230 if (line != NULL) {
231 cmd_run_shell_print(job, line);
232 free(line);
234 } while (line != NULL);
236 size = EVBUFFER_LENGTH(event->input);
237 if (size != 0) {
238 line = xmalloc(size + 1);
239 memcpy(line, EVBUFFER_DATA(event->input), size);
240 line[size] = '\0';
242 cmd_run_shell_print(job, line);
244 free(line);
247 status = job_get_status(job);
248 if (WIFEXITED(status)) {
249 if ((retcode = WEXITSTATUS(status)) != 0)
250 xasprintf(&msg, "'%s' returned %d", cmd, retcode);
251 } else if (WIFSIGNALED(status)) {
252 retcode = WTERMSIG(status);
253 xasprintf(&msg, "'%s' terminated by signal %d", cmd, retcode);
254 retcode += 128;
255 } else
256 retcode = 0;
257 if (msg != NULL)
258 cmd_run_shell_print(job, msg);
259 free(msg);
261 if (item != NULL) {
262 if (cmdq_get_client(item) != NULL &&
263 cmdq_get_client(item)->session == NULL)
264 cmdq_get_client(item)->retval = retcode;
265 cmdq_continue(item);
269 static void
270 cmd_run_shell_free(void *data)
272 struct cmd_run_shell_data *cdata = data;
274 evtimer_del(&cdata->timer);
275 if (cdata->s != NULL)
276 session_remove_ref(cdata->s, __func__);
277 if (cdata->client != NULL)
278 server_client_unref(cdata->client);
279 if (cdata->state != NULL)
280 args_make_commands_free(cdata->state);
281 free(cdata->cwd);
282 free(cdata->cmd);
283 free(cdata);