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>
30 * Runs a command without a window.
33 static enum args_parse_type
cmd_run_shell_args_parse(struct args
*, u_int
,
35 static enum cmd_retval
cmd_run_shell_exec(struct cmd
*,
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
= {
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
},
53 .exec
= cmd_run_shell_exec
56 struct cmd_run_shell_data
{
57 struct client
*client
;
59 struct args_command_state
*state
;
61 struct cmdq_item
*item
;
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
);
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
);
88 if (cdata
->item
!= NULL
) {
89 cmdq_print(cdata
->item
, "%s", msg
);
92 if (cmd_find_from_nothing(&fs
, 0) != 0)
99 wme
= TAILQ_FIRST(&wp
->modes
);
100 if (wme
== NULL
|| wme
->mode
!= &window_view_mode
)
101 window_pane_set_mode(wp
, NULL
, &window_view_mode
, NULL
, NULL
);
102 window_copy_add(wp
, "%s", msg
);
105 static enum cmd_retval
106 cmd_run_shell_exec(struct cmd
*self
, struct cmdq_item
*item
)
108 struct args
*args
= cmd_get_args(self
);
109 struct cmd_find_state
*target
= cmdq_get_target(item
);
110 struct cmd_run_shell_data
*cdata
;
111 struct client
*tc
= cmdq_get_target_client(item
);
112 struct session
*s
= target
->s
;
113 struct window_pane
*wp
= target
->wp
;
114 const char *delay
, *cmd
;
118 int wait
= !args_has(args
, 'b');
120 if ((delay
= args_get(args
, 'd')) != NULL
) {
121 d
= strtod(delay
, &end
);
123 cmdq_error(item
, "invalid delay time: %s", delay
);
124 return (CMD_RETURN_ERROR
);
126 } else if (args_count(args
) == 0)
127 return (CMD_RETURN_NORMAL
);
129 cdata
= xcalloc(1, sizeof *cdata
);
130 if (!args_has(args
, 'C')) {
131 cmd
= args_string(args
, 0);
133 cdata
->cmd
= format_single_from_target(item
, cmd
);
135 cdata
->state
= args_make_commands_prepare(self
, item
, 0, NULL
,
139 if (args_has(args
, 't') && wp
!= NULL
)
140 cdata
->wp_id
= wp
->id
;
145 cdata
->client
= cmdq_get_client(item
);
149 cdata
->flags
|= JOB_NOWAIT
;
151 if (cdata
->client
!= NULL
)
152 cdata
->client
->references
++;
154 cdata
->cwd
= xstrdup(server_client_get_cwd(cmdq_get_client(item
), s
));
158 session_add_ref(s
, __func__
);
160 evtimer_set(&cdata
->timer
, cmd_run_shell_timer
, cdata
);
163 tv
.tv_sec
= (time_t)d
;
164 tv
.tv_usec
= (d
- (double)tv
.tv_sec
) * 1000000U;
165 evtimer_add(&cdata
->timer
, &tv
);
167 event_active(&cdata
->timer
, EV_TIMEOUT
, 1);
170 return (CMD_RETURN_NORMAL
);
171 return (CMD_RETURN_WAIT
);
175 cmd_run_shell_timer(__unused
int fd
, __unused
short events
, void* arg
)
177 struct cmd_run_shell_data
*cdata
= arg
;
178 struct client
*c
= cdata
->client
;
179 const char *cmd
= cdata
->cmd
;
180 struct cmdq_item
*item
= cdata
->item
, *new_item
;
181 struct cmd_list
*cmdlist
;
184 if (cdata
->state
== NULL
) {
186 if (cdata
->item
!= NULL
)
187 cmdq_continue(cdata
->item
);
188 cmd_run_shell_free(cdata
);
191 if (job_run(cmd
, 0, NULL
, NULL
, cdata
->s
, cdata
->cwd
, NULL
,
192 cmd_run_shell_callback
, cmd_run_shell_free
, cdata
,
193 cdata
->flags
, -1, -1) == NULL
)
194 cmd_run_shell_free(cdata
);
198 cmdlist
= args_make_commands(cdata
->state
, 0, NULL
, &error
);
199 if (cmdlist
== NULL
) {
200 if (cdata
->item
== NULL
) {
201 *error
= toupper((u_char
)*error
);
202 status_message_set(c
, -1, 1, 0, "%s", error
);
204 cmdq_error(cdata
->item
, "%s", error
);
206 } else if (item
== NULL
) {
207 new_item
= cmdq_get_command(cmdlist
, NULL
);
208 cmdq_append(c
, new_item
);
210 new_item
= cmdq_get_command(cmdlist
, cmdq_get_state(item
));
211 cmdq_insert_after(item
, new_item
);
214 if (cdata
->item
!= NULL
)
215 cmdq_continue(cdata
->item
);
216 cmd_run_shell_free(cdata
);
220 cmd_run_shell_callback(struct job
*job
)
222 struct cmd_run_shell_data
*cdata
= job_get_data(job
);
223 struct bufferevent
*event
= job_get_event(job
);
224 struct cmdq_item
*item
= cdata
->item
;
225 char *cmd
= cdata
->cmd
, *msg
= NULL
, *line
;
230 if ((line
= evbuffer_readline(event
->input
)) != NULL
) {
231 cmd_run_shell_print(job
, line
);
234 } while (line
!= NULL
);
236 size
= EVBUFFER_LENGTH(event
->input
);
238 line
= xmalloc(size
+ 1);
239 memcpy(line
, EVBUFFER_DATA(event
->input
), size
);
242 cmd_run_shell_print(job
, 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
);
258 cmd_run_shell_print(job
, msg
);
262 if (cmdq_get_client(item
) != NULL
&&
263 cmdq_get_client(item
)->session
== NULL
)
264 cmdq_get_client(item
)->retval
= retcode
;
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
);