xref: /openbsd/usr.bin/tmux/cmd-run-shell.c (revision 8529ddd3)
1 /* $OpenBSD: cmd-run-shell.c,v 1.28 2015/04/24 22:19:36 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
5  * Copyright (c) 2009 Nicholas Marriott <nicm@openbsd.org>
6  *
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.
10  *
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.
18  */
19 
20 #include <sys/types.h>
21 #include <sys/wait.h>
22 
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #include "tmux.h"
27 
28 /*
29  * Runs a command without a window.
30  */
31 
32 enum cmd_retval	 cmd_run_shell_exec(struct cmd *, struct cmd_q *);
33 
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 *);
37 
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",
42 	0,
43 	cmd_run_shell_exec
44 };
45 
46 struct cmd_run_shell_data {
47 	char		*cmd;
48 	struct cmd_q	*cmdq;
49 	int		 bflag;
50 	int		 wp_id;
51 };
52 
53 void
54 cmd_run_shell_print(struct job *job, const char *msg)
55 {
56 	struct cmd_run_shell_data	*cdata = job->data;
57 	struct window_pane		*wp = NULL;
58 
59 	if (cdata->wp_id != -1)
60 		wp = window_pane_find_by_id(cdata->wp_id);
61 	if (wp == NULL) {
62 		cmdq_print(cdata->cmdq, "%s", msg);
63 		return;
64 	}
65 
66 	if (window_pane_set_mode(wp, &window_copy_mode) == 0)
67 		window_copy_init_for_output(wp);
68 	if (wp->mode == &window_copy_mode)
69 		window_copy_add(wp, "%s", msg);
70 }
71 
72 enum cmd_retval
73 cmd_run_shell_exec(struct cmd *self, struct cmd_q *cmdq)
74 {
75 	struct args			*args = self->args;
76 	struct cmd_run_shell_data	*cdata;
77 	char				*shellcmd;
78 	struct client			*c;
79 	struct session			*s = NULL;
80 	struct winlink			*wl = NULL;
81 	struct window_pane		*wp = NULL;
82 	struct format_tree		*ft;
83 	int				 cwd;
84 
85 	if (args_has(args, 't')) {
86 		wl = cmd_find_pane(cmdq, args_get(args, 't'), &s, &wp);
87 		cwd = wp->cwd;
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;
94 		}
95 		if (cmdq->client != NULL && cmdq->client->session == NULL)
96 			cwd = cmdq->client->cwd;
97 		else if (s != NULL)
98 			cwd = s->cwd;
99 		else
100 			cwd = -1;
101 	}
102 
103 	ft = format_create();
104 	format_defaults(ft, NULL, s, wl, wp);
105 	shellcmd = format_expand(ft, args->argv[0]);
106 	format_free(ft);
107 
108 	cdata = xmalloc(sizeof *cdata);
109 	cdata->cmd = shellcmd;
110 	cdata->bflag = args_has(args, 'b');
111 	cdata->wp_id = wp != NULL ? (int) wp->id : -1;
112 
113 	cdata->cmdq = cmdq;
114 	cmdq->references++;
115 
116 	job_run(shellcmd, s, cwd, cmd_run_shell_callback, cmd_run_shell_free,
117 	    cdata);
118 
119 	if (cdata->bflag)
120 		return (CMD_RETURN_NORMAL);
121 	return (CMD_RETURN_WAIT);
122 }
123 
124 void
125 cmd_run_shell_callback(struct job *job)
126 {
127 	struct cmd_run_shell_data	*cdata = job->data;
128 	struct cmd_q			*cmdq = cdata->cmdq;
129 	char				*cmd, *msg, *line;
130 	size_t				 size;
131 	int				 retcode;
132 	u_int				 lines;
133 
134 	if (cmdq->dead)
135 		return;
136 	cmd = cdata->cmd;
137 
138 	lines = 0;
139 	do {
140 		if ((line = evbuffer_readline(job->event->input)) != NULL) {
141 			cmd_run_shell_print(job, line);
142 			free(line);
143 			lines++;
144 		}
145 	} while (line != NULL);
146 
147 	size = EVBUFFER_LENGTH(job->event->input);
148 	if (size != 0) {
149 		line = xmalloc(size + 1);
150 		memcpy(line, EVBUFFER_DATA(job->event->input), size);
151 		line[size] = '\0';
152 
153 		cmd_run_shell_print(job, line);
154 		lines++;
155 
156 		free(line);
157 	}
158 
159 	msg = NULL;
160 	if (WIFEXITED(job->status)) {
161 		if ((retcode = WEXITSTATUS(job->status)) != 0)
162 			xasprintf(&msg, "'%s' returned %d", cmd, retcode);
163 	} else if (WIFSIGNALED(job->status)) {
164 		retcode = WTERMSIG(job->status);
165 		xasprintf(&msg, "'%s' terminated by signal %d", cmd, retcode);
166 	}
167 	if (msg != NULL)
168 		cmd_run_shell_print(job, msg);
169 	free(msg);
170 }
171 
172 void
173 cmd_run_shell_free(void *data)
174 {
175 	struct cmd_run_shell_data	*cdata = data;
176 	struct cmd_q			*cmdq = cdata->cmdq;
177 
178 	if (!cmdq_free(cmdq) && !cdata->bflag)
179 		cmdq_continue(cmdq);
180 
181 	free(cdata->cmd);
182 	free(cdata);
183 }
184