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",
47 struct cmd_run_shell_data
{
55 cmd_run_shell_print(struct job
*job
, const char *msg
)
57 struct cmd_run_shell_data
*cdata
= job
->data
;
58 struct window_pane
*wp
= NULL
;
60 if (cdata
->wp_id
!= -1)
61 wp
= window_pane_find_by_id(cdata
->wp_id
);
63 cmdq_print(cdata
->cmdq
, "%s", msg
);
67 if (window_pane_set_mode(wp
, &window_copy_mode
) == 0)
68 window_copy_init_for_output(wp
);
69 if (wp
->mode
== &window_copy_mode
)
70 window_copy_add(wp
, "%s", msg
);
74 cmd_run_shell_exec(struct cmd
*self
, struct cmd_q
*cmdq
)
76 struct args
*args
= self
->args
;
77 struct cmd_run_shell_data
*cdata
;
80 struct session
*s
= NULL
;
81 struct winlink
*wl
= NULL
;
82 struct window_pane
*wp
= NULL
;
83 struct format_tree
*ft
;
85 if (args_has(args
, 't'))
86 wl
= cmd_find_pane(cmdq
, args_get(args
, 't'), &s
, &wp
);
88 c
= cmd_find_client(cmdq
, NULL
, 1);
89 if (c
!= NULL
&& c
->session
!= NULL
) {
92 wp
= wl
->window
->active
;
98 format_session(ft
, s
);
99 if (s
!= NULL
&& wl
!= NULL
)
100 format_winlink(ft
, s
, wl
);
102 format_window_pane(ft
, wp
);
103 shellcmd
= format_expand(ft
, args
->argv
[0]);
106 cdata
= xmalloc(sizeof *cdata
);
107 cdata
->cmd
= shellcmd
;
108 cdata
->bflag
= args_has(args
, 'b');
109 cdata
->wp_id
= wp
!= NULL
? (int) wp
->id
: -1;
114 job_run(shellcmd
, s
, cmd_run_shell_callback
, cmd_run_shell_free
, cdata
);
117 return (CMD_RETURN_NORMAL
);
118 return (CMD_RETURN_WAIT
);
122 cmd_run_shell_callback(struct job
*job
)
124 struct cmd_run_shell_data
*cdata
= job
->data
;
125 struct cmd_q
*cmdq
= cdata
->cmdq
;
126 char *cmd
, *msg
, *line
;
137 if ((line
= evbuffer_readline(job
->event
->input
)) != NULL
) {
138 cmd_run_shell_print(job
, line
);
142 } while (line
!= NULL
);
144 size
= EVBUFFER_LENGTH(job
->event
->input
);
146 line
= xmalloc(size
+ 1);
147 memcpy(line
, EVBUFFER_DATA(job
->event
->input
), size
);
150 cmd_run_shell_print(job
, line
);
157 if (WIFEXITED(job
->status
)) {
158 if ((retcode
= WEXITSTATUS(job
->status
)) != 0)
159 xasprintf(&msg
, "'%s' returned %d", cmd
, retcode
);
160 } else if (WIFSIGNALED(job
->status
)) {
161 retcode
= WTERMSIG(job
->status
);
162 xasprintf(&msg
, "'%s' terminated by signal %d", cmd
, retcode
);
166 cmdq_info(cmdq
, "%s", msg
);
168 cmd_run_shell_print(job
, msg
);
174 cmd_run_shell_free(void *data
)
176 struct cmd_run_shell_data
*cdata
= data
;
177 struct cmd_q
*cmdq
= cdata
->cmdq
;
179 if (!cmdq_free(cmdq
) && !cdata
->bflag
)