xref: /openbsd-src/usr.bin/tmux/cmd-respawn-pane.c (revision c90a81c56dcebd6a1b73fe4aff9b03385b8e63b3)
1 /* $OpenBSD: cmd-respawn-pane.c,v 1.26 2017/07/21 09:17:19 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com>
5  * Copyright (c) 2011 Marcel P. Partap <mpartap@gmx.net>
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 
22 #include <stdlib.h>
23 #include <unistd.h>
24 
25 #include "tmux.h"
26 
27 /*
28  * Respawn a pane (restart the command). Kill existing if -k given.
29  */
30 
31 static enum cmd_retval	cmd_respawn_pane_exec(struct cmd *, struct cmdq_item *);
32 
33 const struct cmd_entry cmd_respawn_pane_entry = {
34 	.name = "respawn-pane",
35 	.alias = "respawnp",
36 
37 	.args = { "c:kt:", 0, -1 },
38 	.usage = "[-c start-directory] [-k] " CMD_TARGET_PANE_USAGE
39 	         " [command]",
40 
41 	.target = { 't', CMD_FIND_PANE, 0 },
42 
43 	.flags = 0,
44 	.exec = cmd_respawn_pane_exec
45 };
46 
47 static enum cmd_retval
48 cmd_respawn_pane_exec(struct cmd *self, struct cmdq_item *item)
49 {
50 	struct args		*args = self->args;
51 	struct winlink		*wl = item->target.wl;
52 	struct window		*w = wl->window;
53 	struct window_pane	*wp = item->target.wp;
54 	struct client           *c = cmd_find_client(item, NULL, 1);
55 	struct session		*s = item->target.s;
56 	struct environ		*env;
57 	const char		*path = NULL, *cp;
58 	char			*cause, *cwd = NULL;
59 	u_int			 idx;
60 	struct environ_entry	*envent;
61 
62 	if (!args_has(self->args, 'k') && wp->fd != -1) {
63 		if (window_pane_index(wp, &idx) != 0)
64 			fatalx("index not found");
65 		cmdq_error(item, "pane still active: %s:%d.%u",
66 		    s->name, wl->idx, idx);
67 		return (CMD_RETURN_ERROR);
68 	}
69 
70 	window_pane_reset_mode(wp);
71 	screen_reinit(&wp->base);
72 	input_init(wp);
73 
74 	if (item->client != NULL && item->client->session == NULL)
75 		envent = environ_find(item->client->environ, "PATH");
76 	else
77 		envent = environ_find(s->environ, "PATH");
78 	if (envent != NULL)
79 		path = envent->value;
80 
81 	if ((cp = args_get(args, 'c')) != NULL)
82 		cwd = format_single(item, cp, c, s, NULL, NULL);
83 
84 	env = environ_for_session(s, 0);
85 	if (window_pane_spawn(wp, args->argc, args->argv, path, NULL, cwd, env,
86 	    s->tio, &cause) != 0) {
87 		cmdq_error(item, "respawn pane failed: %s", cause);
88 		free(cause);
89 		environ_free(env);
90 		free(cwd);
91 		return (CMD_RETURN_ERROR);
92 	}
93 	environ_free(env);
94 	free(cwd);
95 
96 	wp->flags |= PANE_REDRAW;
97 	server_status_window(w);
98 
99 	return (CMD_RETURN_NORMAL);
100 }
101