1/* $OpenBSD$ */
2
3/*
4 * Copyright (c) 2008 Nicholas Marriott <[email protected]>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include <sys/types.h>
20
21#include <stdlib.h>
22#include <string.h>
23
24#include "tmux.h"
25
26/*
27 * Respawn a window (restart the command). Kill existing if -k given.
28 */
29
30static enum cmd_retval cmd_respawn_window_exec(struct cmd *,
31 struct cmdq_item *);
32
33const struct cmd_entry cmd_respawn_window_entry = {
34 .name = "respawn-window",
35 .alias = "respawnw",
36
37 .args = { "c:e:kt:", 0, -1 },
38 .usage = "[-k] [-c start-directory] [-e environment] "
39 CMD_TARGET_WINDOW_USAGE " [command]",
40
41 .target = { 't', CMD_FIND_WINDOW, 0 },
42
43 .flags = 0,
44 .exec = cmd_respawn_window_exec
45};
46
47static enum cmd_retval
48cmd_respawn_window_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 char *cause = NULL;
55 const char *add;
56 struct args_value *value;
57
58 memset(&sc, 0, sizeof sc);
59 sc.item = item;
60 sc.s = s;
61 sc.wl = wl;
62 sc.c = cmd_find_client(item, NULL, 1);
63
64 sc.name = NULL;
65 sc.argc = args->argc;
66 sc.argv = args->argv;
67 sc.environ = environ_create();
68
69 add = args_first_value(args, 'e', &value);
70 while (add != NULL) {
71 environ_put(sc.environ, add);
72 add = args_next_value(&value);
73 }
74
75 sc.idx = -1;
76 sc.cwd = args_get(args, 'c');
77
78 sc.flags = SPAWN_RESPAWN;
79 if (args_has(args, 'k'))
80 sc.flags |= SPAWN_KILL;
81
82 if (spawn_window(&sc, &cause) == NULL) {
83 cmdq_error(item, "respawn window failed: %s", cause);
84 free(cause);
85 return (CMD_RETURN_ERROR);
86 }
87
88 server_redraw_window(wl->window);
89
90 environ_free(sc.environ);
91 return (CMD_RETURN_NORMAL);
92}
93