15494e770Schristos /* $OpenBSD$ */
2698d5317Sjmmv
3698d5317Sjmmv /*
4ed4e6cd4Schristos * Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com>
5698d5317Sjmmv *
6698d5317Sjmmv * Permission to use, copy, modify, and distribute this software for any
7698d5317Sjmmv * purpose with or without fee is hereby granted, provided that the above
8698d5317Sjmmv * copyright notice and this permission notice appear in all copies.
9698d5317Sjmmv *
10698d5317Sjmmv * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11698d5317Sjmmv * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12698d5317Sjmmv * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13698d5317Sjmmv * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14698d5317Sjmmv * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15698d5317Sjmmv * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16698d5317Sjmmv * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17698d5317Sjmmv */
18698d5317Sjmmv
19698d5317Sjmmv #include <sys/types.h>
20698d5317Sjmmv
21928fc495Schristos #include <stdlib.h>
226483eba0Schristos #include <string.h>
23698d5317Sjmmv
24698d5317Sjmmv #include "tmux.h"
25698d5317Sjmmv
26698d5317Sjmmv /*
27698d5317Sjmmv * Respawn a window (restart the command). Kill existing if -k given.
28698d5317Sjmmv */
29698d5317Sjmmv
304e179ddaSchristos static enum cmd_retval cmd_respawn_window_exec(struct cmd *,
314e179ddaSchristos struct cmdq_item *);
32698d5317Sjmmv
33698d5317Sjmmv const struct cmd_entry cmd_respawn_window_entry = {
34ed4e6cd4Schristos .name = "respawn-window",
35ed4e6cd4Schristos .alias = "respawnw",
36ed4e6cd4Schristos
37*6db26757Swiz .args = { "c:e:kt:", 0, -1, NULL },
386483eba0Schristos .usage = "[-k] [-c start-directory] [-e environment] "
39*6db26757Swiz CMD_TARGET_WINDOW_USAGE " [shell-command]",
40ed4e6cd4Schristos
41c9ad075bSchristos .target = { 't', CMD_FIND_WINDOW, 0 },
42ed4e6cd4Schristos
43ed4e6cd4Schristos .flags = 0,
44ed4e6cd4Schristos .exec = cmd_respawn_window_exec
45698d5317Sjmmv };
46698d5317Sjmmv
474e179ddaSchristos static enum cmd_retval
cmd_respawn_window_exec(struct cmd * self,struct cmdq_item * item)484e179ddaSchristos cmd_respawn_window_exec(struct cmd *self, struct cmdq_item *item)
49698d5317Sjmmv {
509fb66d81Schristos struct args *args = cmd_get_args(self);
519fb66d81Schristos struct cmd_find_state *target = cmdq_get_target(item);
52*6db26757Swiz struct spawn_context sc = { 0 };
539fb66d81Schristos struct client *tc = cmdq_get_target_client(item);
549fb66d81Schristos struct session *s = target->s;
559fb66d81Schristos struct winlink *wl = target->wl;
566483eba0Schristos char *cause = NULL;
57*6db26757Swiz struct args_value *av;
58698d5317Sjmmv
596483eba0Schristos sc.item = item;
606483eba0Schristos sc.s = s;
616483eba0Schristos sc.wl = wl;
629fb66d81Schristos sc.tc = tc;
636483eba0Schristos
64*6db26757Swiz args_to_vector(args, &sc.argc, &sc.argv);
656483eba0Schristos sc.environ = environ_create();
666483eba0Schristos
67*6db26757Swiz av = args_first_value(args, 'e');
68*6db26757Swiz while (av != NULL) {
69*6db26757Swiz environ_put(sc.environ, av->string, 0);
70*6db26757Swiz av = args_next_value(av);
71698d5317Sjmmv }
72698d5317Sjmmv
736483eba0Schristos sc.idx = -1;
746483eba0Schristos sc.cwd = args_get(args, 'c');
755494e770Schristos
766483eba0Schristos sc.flags = SPAWN_RESPAWN;
776483eba0Schristos if (args_has(args, 'k'))
786483eba0Schristos sc.flags |= SPAWN_KILL;
795494e770Schristos
806483eba0Schristos if (spawn_window(&sc, &cause) == NULL) {
814e179ddaSchristos cmdq_error(item, "respawn window failed: %s", cause);
82928fc495Schristos free(cause);
83*6db26757Swiz if (sc.argv != NULL)
84*6db26757Swiz cmd_free_argv(sc.argc, sc.argv);
85*6db26757Swiz environ_free(sc.environ);
86928fc495Schristos return (CMD_RETURN_ERROR);
87698d5317Sjmmv }
88c9ad075bSchristos
896483eba0Schristos server_redraw_window(wl->window);
90698d5317Sjmmv
91*6db26757Swiz if (sc.argv != NULL)
92*6db26757Swiz cmd_free_argv(sc.argc, sc.argv);
936483eba0Schristos environ_free(sc.environ);
94928fc495Schristos return (CMD_RETURN_NORMAL);
95698d5317Sjmmv }
96