1 /* $OpenBSD: cmd-respawn-pane.c,v 1.29 2019/04/28 20:05:50 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 <string.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:e:kt:", 0, -1 }, 38 .usage = "[-k] [-c start-directory] [-e environment] " 39 CMD_TARGET_PANE_USAGE " [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 spawn_context sc; 52 struct session *s = item->target.s; 53 struct winlink *wl = item->target.wl; 54 struct window_pane *wp = item->target.wp; 55 char *cause = NULL; 56 const char *add; 57 struct args_value *value; 58 59 memset(&sc, 0, sizeof sc); 60 sc.item = item; 61 sc.s = s; 62 sc.wl = wl; 63 64 sc.wp0 = wp; 65 sc.lc = NULL; 66 67 sc.name = NULL; 68 sc.argc = args->argc; 69 sc.argv = args->argv; 70 sc.environ = environ_create(); 71 72 add = args_first_value(args, 'e', &value); 73 while (add != NULL) { 74 environ_put(sc.environ, add); 75 add = args_next_value(&value); 76 } 77 78 sc.idx = -1; 79 sc.cwd = args_get(args, 'c'); 80 81 sc.flags = SPAWN_RESPAWN; 82 if (args_has(args, 'k')) 83 sc.flags |= SPAWN_KILL; 84 85 if (spawn_pane(&sc, &cause) == NULL) { 86 cmdq_error(item, "respawn pane failed: %s", cause); 87 free(cause); 88 return (CMD_RETURN_ERROR); 89 } 90 91 wp->flags |= PANE_REDRAW; 92 server_status_window(wp->window); 93 94 environ_free(sc.environ); 95 return (CMD_RETURN_NORMAL); 96 } 97