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>
29 * Runs a command without a window.
32 enum cmd_retval
cmd_run_shell_exec(struct cmd
*, struct cmd_q
*);
34 void cmd_run_shell_callback(struct job
*);
35 void cmd_run_shell_free(void *);
36 void cmd_run_shell_print(struct job
*, const char *);
38 const struct cmd_entry cmd_run_shell_entry
= {
41 "[-b] " CMD_TARGET_PANE_USAGE
" shell-command",
48 struct cmd_run_shell_data
{
56 cmd_run_shell_print(struct job
*job
, const char *msg
)
58 struct cmd_run_shell_data
*cdata
= job
->data
;
59 struct window_pane
*wp
= NULL
;
61 if (cdata
->wp_id
!= -1)
62 wp
= window_pane_find_by_id(cdata
->wp_id
);
64 cmdq_print(cdata
->cmdq
, "%s", msg
);
68 if (window_pane_set_mode(wp
, &window_copy_mode
) == 0)
69 window_copy_init_for_output(wp
);
70 if (wp
->mode
== &window_copy_mode
)
71 window_copy_add(wp
, "%s", msg
);
75 cmd_run_shell_exec(struct cmd
*self
, struct cmd_q
*cmdq
)
77 struct args
*args
= self
->args
;
78 struct cmd_run_shell_data
*cdata
;
81 struct session
*s
= NULL
;
82 struct winlink
*wl
= NULL
;
83 struct window_pane
*wp
= NULL
;
84 struct format_tree
*ft
;
86 if (args_has(args
, 't'))
87 wl
= cmd_find_pane(cmdq
, args_get(args
, 't'), &s
, &wp
);
89 c
= cmd_find_client(cmdq
, NULL
, 1);
90 if (c
!= NULL
&& c
->session
!= NULL
) {
93 wp
= wl
->window
->active
;
99 format_session(ft
, s
);
100 if (s
!= NULL
&& wl
!= NULL
)
101 format_winlink(ft
, s
, wl
);
103 format_window_pane(ft
, wp
);
104 shellcmd
= format_expand(ft
, args
->argv
[0]);
107 cdata
= xmalloc(sizeof *cdata
);
108 cdata
->cmd
= shellcmd
;
109 cdata
->bflag
= args_has(args
, 'b');
110 cdata
->wp_id
= wp
!= NULL
? (int) wp
->id
: -1;
115 job_run(shellcmd
, s
, cmd_run_shell_callback
, cmd_run_shell_free
, cdata
);
118 return (CMD_RETURN_NORMAL
);
119 return (CMD_RETURN_WAIT
);
123 cmd_run_shell_callback(struct job
*job
)
125 struct cmd_run_shell_data
*cdata
= job
->data
;
126 struct cmd_q
*cmdq
= cdata
->cmdq
;
127 char *cmd
, *msg
, *line
;
138 if ((line
= evbuffer_readline(job
->event
->input
)) != NULL
) {
139 cmd_run_shell_print(job
, line
);
143 } while (line
!= NULL
);
145 size
= EVBUFFER_LENGTH(job
->event
->input
);
147 line
= xmalloc(size
+ 1);
148 memcpy(line
, EVBUFFER_DATA(job
->event
->input
), size
);
151 cmd_run_shell_print(job
, line
);
158 if (WIFEXITED(job
->status
)) {
159 if ((retcode
= WEXITSTATUS(job
->status
)) != 0)
160 xasprintf(&msg
, "'%s' returned %d", cmd
, retcode
);
161 } else if (WIFSIGNALED(job
->status
)) {
162 retcode
= WTERMSIG(job
->status
);
163 xasprintf(&msg
, "'%s' terminated by signal %d", cmd
, retcode
);
167 cmdq_info(cmdq
, "%s", msg
);
169 cmd_run_shell_print(job
, msg
);
175 cmd_run_shell_free(void *data
)
177 struct cmd_run_shell_data
*cdata
= data
;
178 struct cmd_q
*cmdq
= cdata
->cmdq
;
180 if (!cmdq_free(cmdq
) && !cdata
->bflag
)