Fix compiler warnings, missing #include. From Thomas Adam.
[tmux-openbsd.git] / cmd-run-shell.c
blobef1dbdd4f4a940afb75f22f365608a75dcb15b6b
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 <stdlib.h>
24 #include <string.h>
26 #include "tmux.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 = {
39 "run-shell", "run",
40 "bt:", 1, 1,
41 "[-b] " CMD_TARGET_PANE_USAGE " shell-command",
43 NULL,
44 NULL,
45 cmd_run_shell_exec
48 struct cmd_run_shell_data {
49 char *cmd;
50 struct cmd_q *cmdq;
51 int bflag;
52 int wp_id;
55 void
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);
63 if (wp == NULL) {
64 cmdq_print(cdata->cmdq, "%s", msg);
65 return;
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);
74 enum cmd_retval
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;
79 char *shellcmd;
80 struct client *c;
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);
88 else {
89 c = cmd_find_client(cmdq, NULL, 1);
90 if (c != NULL && c->session != NULL) {
91 s = c->session;
92 wl = s->curw;
93 wp = wl->window->active;
97 ft = format_create();
98 if (s != NULL)
99 format_session(ft, s);
100 if (s != NULL && wl != NULL)
101 format_winlink(ft, s, wl);
102 if (wp != NULL)
103 format_window_pane(ft, wp);
104 shellcmd = format_expand(ft, args->argv[0]);
105 format_free(ft);
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;
112 cdata->cmdq = cmdq;
113 cmdq->references++;
115 job_run(shellcmd, s, cmd_run_shell_callback, cmd_run_shell_free, cdata);
117 if (cdata->bflag)
118 return (CMD_RETURN_NORMAL);
119 return (CMD_RETURN_WAIT);
122 void
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;
128 size_t size;
129 int retcode;
130 u_int lines;
132 if (cmdq->dead)
133 return;
134 cmd = cdata->cmd;
136 lines = 0;
137 do {
138 if ((line = evbuffer_readline(job->event->input)) != NULL) {
139 cmd_run_shell_print(job, line);
140 free(line);
141 lines++;
143 } while (line != NULL);
145 size = EVBUFFER_LENGTH(job->event->input);
146 if (size != 0) {
147 line = xmalloc(size + 1);
148 memcpy(line, EVBUFFER_DATA(job->event->input), size);
149 line[size] = '\0';
151 cmd_run_shell_print(job, line);
152 lines++;
154 free(line);
157 msg = NULL;
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);
165 if (msg != NULL) {
166 if (lines == 0)
167 cmdq_info(cmdq, "%s", msg);
168 else
169 cmd_run_shell_print(job, msg);
170 free(msg);
174 void
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)
181 cmdq_continue(cmdq);
183 free(cdata->cmd);
184 free(cdata);