diff --git a/cmd-new-session.c b/cmd-new-session.c index efddc228..81c2f467 100644 --- a/cmd-new-session.c +++ b/cmd-new-session.c @@ -74,8 +74,8 @@ cmd_new_session_exec(struct cmd *self, struct cmdq_item *item) struct termios tio, *tiop; struct session_group *sg; const char *newname, *errstr, *template, *group, *prefix; - const char *path, *cmd, *cwd, *to_free = NULL; - char **argv, *cause, *cp; + const char *path, *cmd, *cwd; + char **argv, *cause, *cp, *to_free = NULL; int detached, already_attached, idx, argc; int is_control = 0; u_int sx, sy; @@ -151,7 +151,8 @@ cmd_new_session_exec(struct cmd *self, struct cmdq_item *item) /* Get the new session working directory. */ if (args_has(args, 'c')) { cwd = args_get(args, 'c'); - to_free = cwd = format_single(item, cwd, c, NULL, NULL, NULL); + to_free = format_single(item, cwd, c, NULL, NULL, NULL); + cwd = to_free; } else if (c != NULL && c->session == NULL && c->cwd != NULL) cwd = c->cwd; else @@ -327,16 +328,13 @@ cmd_new_session_exec(struct cmd *self, struct cmdq_item *item) cmd_find_from_session(&item->shared->current, s); } - if (to_free != NULL) - free((void *)to_free); - cmd_find_from_session(&fs, s); hooks_insert(s->hooks, item, &fs, "after-new-session"); + free(to_free); return (CMD_RETURN_NORMAL); error: - if (to_free != NULL) - free((void *)to_free); + free(to_free); return (CMD_RETURN_ERROR); } diff --git a/cmd-new-window.c b/cmd-new-window.c index 82f1d745..7cb83f58 100644 --- a/cmd-new-window.c +++ b/cmd-new-window.c @@ -57,8 +57,8 @@ cmd_new_window_exec(struct cmd *self, struct cmdq_item *item) struct winlink *wl = item->target.wl; struct client *c = cmd_find_client(item, NULL, 1); int idx = item->target.idx; - const char *cmd, *path, *template, *cwd, *to_free; - char **argv, *cause, *cp; + const char *cmd, *path, *template, *cwd; + char **argv, *cause, *cp, *to_free = NULL; int argc, detached; struct environ_entry *envent; struct cmd_find_state fs; @@ -93,10 +93,10 @@ cmd_new_window_exec(struct cmd *self, struct cmdq_item *item) if (envent != NULL) path = envent->value; - to_free = NULL; if (args_has(args, 'c')) { cwd = args_get(args, 'c'); - to_free = cwd = format_single(item, cwd, c, s, NULL, NULL); + to_free = format_single(item, cwd, c, s, NULL, NULL); + cwd = to_free; } else if (item->client != NULL && item->client->session == NULL) cwd = item->client->cwd; else @@ -146,16 +146,13 @@ cmd_new_window_exec(struct cmd *self, struct cmdq_item *item) free(cp); } - if (to_free != NULL) - free((void *)to_free); - cmd_find_from_winlink(&fs, wl); hooks_insert(s->hooks, item, &fs, "after-new-window"); + free(to_free); return (CMD_RETURN_NORMAL); error: - if (to_free != NULL) - free((void *)to_free); + free(to_free); return (CMD_RETURN_ERROR); } diff --git a/cmd-respawn-pane.c b/cmd-respawn-pane.c index a1178147..3d78c495 100644 --- a/cmd-respawn-pane.c +++ b/cmd-respawn-pane.c @@ -34,8 +34,9 @@ const struct cmd_entry cmd_respawn_pane_entry = { .name = "respawn-pane", .alias = "respawnp", - .args = { "kt:", 0, -1 }, - .usage = "[-k] " CMD_TARGET_PANE_USAGE " [command]", + .args = { "c:kt:", 0, -1 }, + .usage = "[-c start-directory] [-k] " CMD_TARGET_PANE_USAGE + " [command]", .target = { 't', CMD_FIND_PANE, 0 }, @@ -50,10 +51,11 @@ cmd_respawn_pane_exec(struct cmd *self, struct cmdq_item *item) struct winlink *wl = item->target.wl; struct window *w = wl->window; struct window_pane *wp = item->target.wp; + struct client *c = cmd_find_client(item, NULL, 1); struct session *s = item->target.s; struct environ *env; - const char *path; - char *cause; + const char *path = NULL, *cp; + char *cause, *cwd = NULL; u_int idx; struct environ_entry *envent; @@ -69,7 +71,6 @@ cmd_respawn_pane_exec(struct cmd *self, struct cmdq_item *item) screen_reinit(&wp->base); input_init(wp); - path = NULL; if (item->client != NULL && item->client->session == NULL) envent = environ_find(item->client->environ, "PATH"); else @@ -77,15 +78,20 @@ cmd_respawn_pane_exec(struct cmd *self, struct cmdq_item *item) if (envent != NULL) path = envent->value; + if ((cp = args_get(args, 'c')) != NULL) + cwd = format_single(item, cp, c, s, NULL, NULL); + env = environ_for_session(s, 0); - if (window_pane_spawn(wp, args->argc, args->argv, path, NULL, NULL, env, + if (window_pane_spawn(wp, args->argc, args->argv, path, NULL, cwd, env, s->tio, &cause) != 0) { cmdq_error(item, "respawn pane failed: %s", cause); free(cause); environ_free(env); + free(cwd); return (CMD_RETURN_ERROR); } environ_free(env); + free(cwd); wp->flags |= PANE_REDRAW; server_status_window(w); diff --git a/cmd-respawn-window.c b/cmd-respawn-window.c index 48e6a97f..a1e26117 100644 --- a/cmd-respawn-window.c +++ b/cmd-respawn-window.c @@ -34,8 +34,9 @@ const struct cmd_entry cmd_respawn_window_entry = { .name = "respawn-window", .alias = "respawnw", - .args = { "kt:", 0, -1 }, - .usage = "[-k] " CMD_TARGET_WINDOW_USAGE " [command]", + .args = { "c:kt:", 0, -1 }, + .usage = "[-c start-directory] [-k] " CMD_TARGET_WINDOW_USAGE + " [command]", .target = { 't', CMD_FIND_WINDOW, 0 }, @@ -51,9 +52,10 @@ cmd_respawn_window_exec(struct cmd *self, struct cmdq_item *item) struct winlink *wl = item->target.wl; struct window *w = wl->window; struct window_pane *wp; + struct client *c = cmd_find_client(item, NULL, 1); struct environ *env; - const char *path; - char *cause; + const char *path = NULL, *cp; + char *cause, *cwd = NULL; struct environ_entry *envent; if (!args_has(self->args, 'k')) { @@ -73,7 +75,6 @@ cmd_respawn_window_exec(struct cmd *self, struct cmdq_item *item) TAILQ_INSERT_HEAD(&w->panes, wp, entry); window_pane_resize(wp, w->sx, w->sy); - path = NULL; if (item->client != NULL && item->client->session == NULL) envent = environ_find(item->client->environ, "PATH"); else @@ -81,16 +82,22 @@ cmd_respawn_window_exec(struct cmd *self, struct cmdq_item *item) if (envent != NULL) path = envent->value; + if ((cp = args_get(args, 'c')) != NULL) + cwd = format_single(item, cp, c, s, NULL, NULL); + env = environ_for_session(s, 0); - if (window_pane_spawn(wp, args->argc, args->argv, path, NULL, NULL, env, + if (window_pane_spawn(wp, args->argc, args->argv, path, NULL, cwd, env, s->tio, &cause) != 0) { cmdq_error(item, "respawn window failed: %s", cause); free(cause); environ_free(env); + free(cwd); server_destroy_pane(wp, 0); return (CMD_RETURN_ERROR); } environ_free(env); + free(cwd); + layout_init(w, wp); window_pane_reset_mode(wp); screen_reinit(&wp->base); diff --git a/cmd-split-window.c b/cmd-split-window.c index 4d94cd91..ad403196 100644 --- a/cmd-split-window.c +++ b/cmd-split-window.c @@ -61,8 +61,8 @@ cmd_split_window_exec(struct cmd *self, struct cmdq_item *item) struct window *w = wl->window; struct window_pane *wp = item->target.wp, *new_wp = NULL; struct environ *env; - const char *cmd, *path, *shell, *template, *cwd, *to_free; - char **argv, *cause, *new_cause, *cp; + const char *cmd, *path, *shell, *template, *cwd; + char **argv, *cause, *new_cause, *cp, *to_free = NULL; u_int hlimit; int argc, size, percentage; enum layout_type type; @@ -86,10 +86,10 @@ cmd_split_window_exec(struct cmd *self, struct cmdq_item *item) argv = args->argv; } - to_free = NULL; if (args_has(args, 'c')) { cwd = args_get(args, 'c'); - to_free = cwd = format_single(item, cwd, c, s, NULL, NULL); + to_free = format_single(item, cwd, c, s, NULL, NULL); + cwd = to_free; } else if (item->client != NULL && item->client->session == NULL) cwd = item->client->cwd; else @@ -172,12 +172,10 @@ cmd_split_window_exec(struct cmd *self, struct cmdq_item *item) } notify_window("window-layout-changed", w); - if (to_free != NULL) - free((void *)to_free); - cmd_find_from_winlink_pane(&fs, wl, new_wp); hooks_insert(s->hooks, item, &fs, "after-split-window"); + free(to_free); return (CMD_RETURN_NORMAL); error: @@ -188,7 +186,6 @@ error: cmdq_error(item, "create pane failed: %s", cause); free(cause); - if (to_free != NULL) - free((void *)to_free); + free(to_free); return (CMD_RETURN_ERROR); } diff --git a/tmux.1 b/tmux.1 index 4f9e3e5d..c9a181e6 100644 --- a/tmux.1 +++ b/tmux.1 @@ -1834,6 +1834,7 @@ and unzoomed (its normal position in the layout). begins mouse resizing (only valid if bound to a mouse key binding, see .Sx MOUSE SUPPORT ) . .It Xo Ic respawn-pane +.Op Fl c Ar start-directory .Op Fl k .Op Fl t Ar target-pane .Op Ar shell-command @@ -1848,7 +1849,10 @@ is not given, the command used when the pane was created is executed. The pane must be already inactive, unless .Fl k is given, in which case any existing command is killed. +.Fl c +specifies a new working directory for the pane. .It Xo Ic respawn-window +.Op Fl c Ar start-directory .Op Fl k .Op Fl t Ar target-window .Op Ar shell-command @@ -1863,6 +1867,8 @@ is not given, the command used when the window was created is executed. The window must be already inactive, unless .Fl k is given, in which case any existing command is killed. +.Fl c +specifies a new working directory for the window. .It Xo Ic rotate-window .Op Fl DU .Op Fl t Ar target-window