Annotation of src/usr.bin/tmux/cmd-respawn-pane.c, Revision 1.26
1.26 ! nicm 1: /* $OpenBSD: cmd-respawn-pane.c,v 1.25 2017/04/25 15:35:10 nicm Exp $ */
1.1 nicm 2:
3: /*
1.20 nicm 4: * Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1 nicm 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:
1.6 nicm 22: #include <stdlib.h>
1.1 nicm 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:
1.22 nicm 31: static enum cmd_retval cmd_respawn_pane_exec(struct cmd *, struct cmdq_item *);
1.1 nicm 32:
33: const struct cmd_entry cmd_respawn_pane_entry = {
1.18 nicm 34: .name = "respawn-pane",
35: .alias = "respawnp",
36:
1.26 ! nicm 37: .args = { "c:kt:", 0, -1 },
! 38: .usage = "[-c start-directory] [-k] " CMD_TARGET_PANE_USAGE
! 39: " [command]",
1.18 nicm 40:
1.24 nicm 41: .target = { 't', CMD_FIND_PANE, 0 },
1.19 nicm 42:
43: .flags = 0,
1.18 nicm 44: .exec = cmd_respawn_pane_exec
1.1 nicm 45: };
46:
1.21 nicm 47: static enum cmd_retval
1.22 nicm 48: cmd_respawn_pane_exec(struct cmd *self, struct cmdq_item *item)
1.1 nicm 49: {
50: struct args *args = self->args;
1.24 nicm 51: struct winlink *wl = item->target.wl;
1.17 nicm 52: struct window *w = wl->window;
1.24 nicm 53: struct window_pane *wp = item->target.wp;
1.26 ! nicm 54: struct client *c = cmd_find_client(item, NULL, 1);
1.24 nicm 55: struct session *s = item->target.s;
1.15 nicm 56: struct environ *env;
1.26 ! nicm 57: const char *path = NULL, *cp;
! 58: char *cause, *cwd = NULL;
1.4 nicm 59: u_int idx;
1.11 nicm 60: struct environ_entry *envent;
1.1 nicm 61:
62: if (!args_has(self->args, 'k') && wp->fd != -1) {
1.4 nicm 63: if (window_pane_index(wp, &idx) != 0)
64: fatalx("index not found");
1.22 nicm 65: cmdq_error(item, "pane still active: %s:%d.%u",
1.4 nicm 66: s->name, wl->idx, idx);
1.7 nicm 67: return (CMD_RETURN_ERROR);
1.1 nicm 68: }
69:
70: window_pane_reset_mode(wp);
1.2 nicm 71: screen_reinit(&wp->base);
1.1 nicm 72: input_init(wp);
73:
1.22 nicm 74: if (item->client != NULL && item->client->session == NULL)
75: envent = environ_find(item->client->environ, "PATH");
1.11 nicm 76: else
1.15 nicm 77: envent = environ_find(s->environ, "PATH");
1.11 nicm 78: if (envent != NULL)
79: path = envent->value;
80:
1.26 ! nicm 81: if ((cp = args_get(args, 'c')) != NULL)
! 82: cwd = format_single(item, cp, c, s, NULL, NULL);
! 83:
1.25 nicm 84: env = environ_for_session(s, 0);
1.26 ! nicm 85: if (window_pane_spawn(wp, args->argc, args->argv, path, NULL, cwd, env,
1.12 nicm 86: s->tio, &cause) != 0) {
1.22 nicm 87: cmdq_error(item, "respawn pane failed: %s", cause);
1.6 nicm 88: free(cause);
1.15 nicm 89: environ_free(env);
1.26 ! nicm 90: free(cwd);
1.7 nicm 91: return (CMD_RETURN_ERROR);
1.1 nicm 92: }
1.23 nicm 93: environ_free(env);
1.26 ! nicm 94: free(cwd);
1.23 nicm 95:
1.1 nicm 96: wp->flags |= PANE_REDRAW;
97: server_status_window(w);
98:
1.7 nicm 99: return (CMD_RETURN_NORMAL);
1.1 nicm 100: }