SGR 0 should not end hyperlink, reported by Lucas Trzesniewski.
[tmux-openbsd.git] / cmd-run-shell.c
blob560eface7d4ab8753f1fe3f3b000c93d514ebc77
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:", 0, 1, cmd_run_shell_args_parse },
48 .usage = "[-bC] [-d delay] " CMD_TARGET_PANE_USAGE " [shell-command]",
50 .target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
52 .flags = 0,
53 .exec = cmd_run_shell_exec
56 struct cmd_run_shell_data {
57 struct client *client;
58 char *cmd;
59 struct args_command_state *state;
60 char *cwd;
61 struct cmdq_item *item;
62 struct session *s;
63 int wp_id;
64 struct event timer;
65 int flags;
68 static enum args_parse_type
69 cmd_run_shell_args_parse(struct args *args, __unused u_int idx,
70 __unused char **cause)
72 if (args_has(args, 'C'))
73 return (ARGS_PARSE_COMMANDS_OR_STRING);
74 return (ARGS_PARSE_STRING);
77 static void
78 cmd_run_shell_print(struct job *job, const char *msg)
80 struct cmd_run_shell_data *cdata = job_get_data(job);
81 struct window_pane *wp = NULL;
82 struct cmd_find_state fs;
83 struct window_mode_entry *wme;
85 if (cdata->wp_id != -1)
86 wp = window_pane_find_by_id(cdata->wp_id);
87 if (wp == NULL && cdata->item != NULL && cdata->client != NULL)
88 wp = server_client_get_pane(cdata->client);
89 if (wp == NULL && cmd_find_from_nothing(&fs, 0) == 0)
90 wp = fs.wp;
91 if (wp == NULL)
92 return;
94 wme = TAILQ_FIRST(&wp->modes);
95 if (wme == NULL || wme->mode != &window_view_mode)
96 window_pane_set_mode(wp, NULL, &window_view_mode, NULL, NULL);
97 window_copy_add(wp, 1, "%s", msg);
100 static enum cmd_retval
101 cmd_run_shell_exec(struct cmd *self, struct cmdq_item *item)
103 struct args *args = cmd_get_args(self);
104 struct cmd_find_state *target = cmdq_get_target(item);
105 struct cmd_run_shell_data *cdata;
106 struct client *tc = cmdq_get_target_client(item);
107 struct session *s = target->s;
108 struct window_pane *wp = target->wp;
109 const char *delay, *cmd;
110 double d;
111 struct timeval tv;
112 char *end;
113 int wait = !args_has(args, 'b');
115 if ((delay = args_get(args, 'd')) != NULL) {
116 d = strtod(delay, &end);
117 if (*end != '\0') {
118 cmdq_error(item, "invalid delay time: %s", delay);
119 return (CMD_RETURN_ERROR);
121 } else if (args_count(args) == 0)
122 return (CMD_RETURN_NORMAL);
124 cdata = xcalloc(1, sizeof *cdata);
125 if (!args_has(args, 'C')) {
126 cmd = args_string(args, 0);
127 if (cmd != NULL)
128 cdata->cmd = format_single_from_target(item, cmd);
129 } else {
130 cdata->state = args_make_commands_prepare(self, item, 0, NULL,
131 wait, 1);
134 if (args_has(args, 't') && wp != NULL)
135 cdata->wp_id = wp->id;
136 else
137 cdata->wp_id = -1;
139 if (wait) {
140 cdata->client = cmdq_get_client(item);
141 cdata->item = item;
142 } else {
143 cdata->client = tc;
144 cdata->flags |= JOB_NOWAIT;
146 if (cdata->client != NULL)
147 cdata->client->references++;
149 cdata->cwd = xstrdup(server_client_get_cwd(cmdq_get_client(item), s));
151 cdata->s = s;
152 if (s != NULL)
153 session_add_ref(s, __func__);
155 evtimer_set(&cdata->timer, cmd_run_shell_timer, cdata);
156 if (delay != NULL) {
157 timerclear(&tv);
158 tv.tv_sec = (time_t)d;
159 tv.tv_usec = (d - (double)tv.tv_sec) * 1000000U;
160 evtimer_add(&cdata->timer, &tv);
161 } else
162 event_active(&cdata->timer, EV_TIMEOUT, 1);
164 if (!wait)
165 return (CMD_RETURN_NORMAL);
166 return (CMD_RETURN_WAIT);
169 static void
170 cmd_run_shell_timer(__unused int fd, __unused short events, void* arg)
172 struct cmd_run_shell_data *cdata = arg;
173 struct client *c = cdata->client;
174 const char *cmd = cdata->cmd;
175 struct cmdq_item *item = cdata->item, *new_item;
176 struct cmd_list *cmdlist;
177 char *error;
179 if (cdata->state == NULL) {
180 if (cmd == NULL) {
181 if (cdata->item != NULL)
182 cmdq_continue(cdata->item);
183 cmd_run_shell_free(cdata);
184 return;
186 if (job_run(cmd, 0, NULL, NULL, cdata->s, cdata->cwd, NULL,
187 cmd_run_shell_callback, cmd_run_shell_free, cdata,
188 cdata->flags, -1, -1) == NULL)
189 cmd_run_shell_free(cdata);
190 return;
193 cmdlist = args_make_commands(cdata->state, 0, NULL, &error);
194 if (cmdlist == NULL) {
195 if (cdata->item == NULL) {
196 *error = toupper((u_char)*error);
197 status_message_set(c, -1, 1, 0, "%s", error);
198 } else
199 cmdq_error(cdata->item, "%s", error);
200 free(error);
201 } else if (item == NULL) {
202 new_item = cmdq_get_command(cmdlist, NULL);
203 cmdq_append(c, new_item);
204 } else {
205 new_item = cmdq_get_command(cmdlist, cmdq_get_state(item));
206 cmdq_insert_after(item, new_item);
209 if (cdata->item != NULL)
210 cmdq_continue(cdata->item);
211 cmd_run_shell_free(cdata);
214 static void
215 cmd_run_shell_callback(struct job *job)
217 struct cmd_run_shell_data *cdata = job_get_data(job);
218 struct bufferevent *event = job_get_event(job);
219 struct cmdq_item *item = cdata->item;
220 char *cmd = cdata->cmd, *msg = NULL, *line;
221 size_t size;
222 int retcode, status;
224 do {
225 line = evbuffer_readln(event->input, NULL, EVBUFFER_EOL_LF);
226 if (line != NULL) {
227 cmd_run_shell_print(job, line);
228 free(line);
230 } while (line != NULL);
232 size = EVBUFFER_LENGTH(event->input);
233 if (size != 0) {
234 line = xmalloc(size + 1);
235 memcpy(line, EVBUFFER_DATA(event->input), size);
236 line[size] = '\0';
238 cmd_run_shell_print(job, line);
240 free(line);
243 status = job_get_status(job);
244 if (WIFEXITED(status)) {
245 if ((retcode = WEXITSTATUS(status)) != 0)
246 xasprintf(&msg, "'%s' returned %d", cmd, retcode);
247 } else if (WIFSIGNALED(status)) {
248 retcode = WTERMSIG(status);
249 xasprintf(&msg, "'%s' terminated by signal %d", cmd, retcode);
250 retcode += 128;
251 } else
252 retcode = 0;
253 if (msg != NULL)
254 cmd_run_shell_print(job, msg);
255 free(msg);
257 if (item != NULL) {
258 if (cmdq_get_client(item) != NULL &&
259 cmdq_get_client(item)->session == NULL)
260 cmdq_get_client(item)->retval = retcode;
261 cmdq_continue(item);
265 static void
266 cmd_run_shell_free(void *data)
268 struct cmd_run_shell_data *cdata = data;
270 evtimer_del(&cdata->timer);
271 if (cdata->s != NULL)
272 session_remove_ref(cdata->s, __func__);
273 if (cdata->client != NULL)
274 server_client_unref(cdata->client);
275 if (cdata->state != NULL)
276 args_make_commands_free(cdata->state);
277 free(cdata->cwd);
278 free(cdata->cmd);
279 free(cdata);