xref: /netbsd-src/external/bsd/tmux/dist/cmd-respawn-pane.c (revision d909946ca08dceb44d7d0f22ec9488679695d976)
1 /* $OpenBSD$ */
2 
3 /*
4  * Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net>
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 enum cmd_retval	 cmd_respawn_pane_exec(struct cmd *, struct cmd_q *);
32 
33 const struct cmd_entry cmd_respawn_pane_entry = {
34 	"respawn-pane", "respawnp",
35 	"kt:", 0, -1,
36 	"[-k] " CMD_TARGET_PANE_USAGE " [command]",
37 	0,
38 	cmd_respawn_pane_exec
39 };
40 
41 enum cmd_retval
42 cmd_respawn_pane_exec(struct cmd *self, struct cmd_q *cmdq)
43 {
44 	struct args		*args = self->args;
45 	struct winlink		*wl;
46 	struct window		*w;
47 	struct window_pane	*wp;
48 	struct session		*s;
49 	struct environ		 env;
50 	const char		*path;
51 	char			*cause;
52 	u_int			 idx;
53 	struct environ_entry	*envent;
54 
55 	if ((wl = cmd_find_pane(cmdq, args_get(args, 't'), &s, &wp)) == NULL)
56 		return (CMD_RETURN_ERROR);
57 	w = wl->window;
58 
59 	if (!args_has(self->args, 'k') && wp->fd != -1) {
60 		if (window_pane_index(wp, &idx) != 0)
61 			fatalx("index not found");
62 		cmdq_error(cmdq, "pane still active: %s:%d.%u",
63 		    s->name, wl->idx, idx);
64 		return (CMD_RETURN_ERROR);
65 	}
66 
67 	environ_init(&env);
68 	environ_copy(&global_environ, &env);
69 	environ_copy(&s->environ, &env);
70 	server_fill_environ(s, &env);
71 
72 	window_pane_reset_mode(wp);
73 	screen_reinit(&wp->base);
74 	input_init(wp);
75 
76 	path = NULL;
77 	if (cmdq->client != NULL && cmdq->client->session == NULL)
78 		envent = environ_find(&cmdq->client->environ, "PATH");
79 	else
80 		envent = environ_find(&s->environ, "PATH");
81 	if (envent != NULL)
82 		path = envent->value;
83 
84 	if (window_pane_spawn(wp, args->argc, args->argv, path, NULL, -1, &env,
85 	    s->tio, &cause) != 0) {
86 		cmdq_error(cmdq, "respawn pane failed: %s", cause);
87 		free(cause);
88 		environ_free(&env);
89 		return (CMD_RETURN_ERROR);
90 	}
91 	wp->flags |= PANE_REDRAW;
92 	server_status_window(w);
93 
94 	environ_free(&env);
95 	return (CMD_RETURN_NORMAL);
96 }
97