Expand formats in window and session names.

This commit is contained in:
nicm 2018-03-01 12:53:08 +00:00
parent 508e2f0b3a
commit 182357f24b
5 changed files with 70 additions and 50 deletions

View File

@ -73,14 +73,15 @@ cmd_new_session_exec(struct cmd *self, struct cmdq_item *item)
struct environ *env; struct environ *env;
struct termios tio, *tiop; struct termios tio, *tiop;
struct session_group *sg; struct session_group *sg;
const char *newname, *errstr, *template, *group, *prefix; const char *errstr, *template, *group, *prefix;
const char *path, *cmd, *cwd; const char *path, *cmd, *tmp;
char **argv, *cause, *cp, *to_free = NULL; char **argv, *cause, *cp, *newname, *cwd = NULL;
int detached, already_attached, idx, argc; int detached, already_attached, idx, argc;
int is_control = 0; int is_control = 0;
u_int sx, sy; u_int sx, sy;
struct environ_entry *envent; struct environ_entry *envent;
struct cmd_find_state fs; struct cmd_find_state fs;
enum cmd_retval retval;
if (self->entry == &cmd_has_session_entry) { if (self->entry == &cmd_has_session_entry) {
/* /*
@ -95,20 +96,24 @@ cmd_new_session_exec(struct cmd *self, struct cmdq_item *item)
return (CMD_RETURN_ERROR); return (CMD_RETURN_ERROR);
} }
newname = args_get(args, 's'); newname = NULL;
if (newname != NULL) { if (args_has(args, 's')) {
newname = format_single(item, args_get(args, 's'), c, NULL,
NULL, NULL);
if (!session_check_name(newname)) { if (!session_check_name(newname)) {
cmdq_error(item, "bad session name: %s", newname); cmdq_error(item, "bad session name: %s", newname);
return (CMD_RETURN_ERROR); goto error;
} }
if ((as = session_find(newname)) != NULL) { if ((as = session_find(newname)) != NULL) {
if (args_has(args, 'A')) { if (args_has(args, 'A')) {
return (cmd_attach_session(item, retval = cmd_attach_session(item,
newname, args_has(args, 'D'), newname, args_has(args, 'D'),
0, NULL, args_has(args, 'E'))); 0, NULL, args_has(args, 'E'));
free(newname);
return (retval);
} }
cmdq_error(item, "duplicate session: %s", newname); cmdq_error(item, "duplicate session: %s", newname);
return (CMD_RETURN_ERROR); goto error;
} }
} }
@ -149,14 +154,12 @@ cmd_new_session_exec(struct cmd *self, struct cmdq_item *item)
already_attached = 1; already_attached = 1;
/* Get the new session working directory. */ /* Get the new session working directory. */
if (args_has(args, 'c')) { if ((tmp = args_get(args, 'c')) != NULL)
cwd = args_get(args, 'c'); cwd = format_single(item, tmp, c, NULL, NULL, NULL);
to_free = format_single(item, cwd, c, NULL, NULL, NULL); else if (c != NULL && c->session == NULL && c->cwd != NULL)
cwd = to_free; cwd = xstrdup(c->cwd);
} else if (c != NULL && c->session == NULL && c->cwd != NULL)
cwd = c->cwd;
else else
cwd = "."; cwd = xstrdup(".");
/* /*
* If this is a new client, check for nesting and save the termios * If this is a new client, check for nesting and save the termios
@ -261,10 +264,12 @@ cmd_new_session_exec(struct cmd *self, struct cmdq_item *item)
} }
/* Set the initial window name if one given. */ /* Set the initial window name if one given. */
if (argc >= 0 && args_has(args, 'n')) { if (argc >= 0 && (tmp = args_get(args, 'n')) != NULL) {
cp = format_single(item, tmp, c, s, NULL, NULL);
w = s->curw->window; w = s->curw->window;
window_set_name(w, args_get(args, 'n')); window_set_name(w, cp);
options_set_number(w->options, "automatic-rename", 0); options_set_number(w->options, "automatic-rename", 0);
free(cp);
} }
/* /*
@ -331,10 +336,12 @@ cmd_new_session_exec(struct cmd *self, struct cmdq_item *item)
cmd_find_from_session(&fs, s, 0); cmd_find_from_session(&fs, s, 0);
hooks_insert(s->hooks, item, &fs, "after-new-session"); hooks_insert(s->hooks, item, &fs, "after-new-session");
free(to_free); free(cwd);
free(newname);
return (CMD_RETURN_NORMAL); return (CMD_RETURN_NORMAL);
error: error:
free(to_free); free(cwd);
free(newname);
return (CMD_RETURN_ERROR); return (CMD_RETURN_ERROR);
} }

View File

@ -57,8 +57,8 @@ cmd_new_window_exec(struct cmd *self, struct cmdq_item *item)
struct winlink *wl = item->target.wl; struct winlink *wl = item->target.wl;
struct client *c = cmd_find_client(item, NULL, 1); struct client *c = cmd_find_client(item, NULL, 1);
int idx = item->target.idx; int idx = item->target.idx;
const char *cmd, *path, *template, *cwd; const char *cmd, *path, *template, *tmp;
char **argv, *cause, *cp, *to_free = NULL; char **argv, *cause, *cp, *cwd, *name;
int argc, detached; int argc, detached;
struct environ_entry *envent; struct environ_entry *envent;
struct cmd_find_state fs; struct cmd_find_state fs;
@ -93,14 +93,17 @@ cmd_new_window_exec(struct cmd *self, struct cmdq_item *item)
if (envent != NULL) if (envent != NULL)
path = envent->value; path = envent->value;
if (args_has(args, 'c')) { if ((tmp = args_get(args, 'c')) != NULL)
cwd = args_get(args, 'c'); cwd = format_single(item, tmp, c, s, NULL, NULL);
to_free = format_single(item, cwd, c, s, NULL, NULL); else if (item->client != NULL && item->client->session == NULL)
cwd = to_free; cwd = xstrdup(item->client->cwd);
} else if (item->client != NULL && item->client->session == NULL)
cwd = item->client->cwd;
else else
cwd = s->cwd; cwd = xstrdup(s->cwd);
if ((tmp = args_get(args, 'n')) != NULL)
name = format_single(item, tmp, c, s, NULL, NULL);
else
name = NULL;
wl = NULL; wl = NULL;
if (idx != -1) if (idx != -1)
@ -124,7 +127,7 @@ cmd_new_window_exec(struct cmd *self, struct cmdq_item *item)
if (idx == -1) if (idx == -1)
idx = -1 - options_get_number(s->options, "base-index"); idx = -1 - options_get_number(s->options, "base-index");
wl = session_new(s, args_get(args, 'n'), argc, argv, path, cwd, idx, wl = session_new(s, name, argc, argv, path, cwd, idx,
&cause); &cause);
if (wl == NULL) { if (wl == NULL) {
cmdq_error(item, "create window failed: %s", cause); cmdq_error(item, "create window failed: %s", cause);
@ -149,10 +152,12 @@ cmd_new_window_exec(struct cmd *self, struct cmdq_item *item)
cmd_find_from_winlink(&fs, wl, 0); cmd_find_from_winlink(&fs, wl, 0);
hooks_insert(s->hooks, item, &fs, "after-new-window"); hooks_insert(s->hooks, item, &fs, "after-new-window");
free(to_free); free(name);
free(cwd);
return (CMD_RETURN_NORMAL); return (CMD_RETURN_NORMAL);
error: error:
free(to_free); free(name);
free(cwd);
return (CMD_RETURN_ERROR); return (CMD_RETURN_ERROR);
} }

View File

@ -46,26 +46,31 @@ const struct cmd_entry cmd_rename_session_entry = {
static enum cmd_retval static enum cmd_retval
cmd_rename_session_exec(struct cmd *self, struct cmdq_item *item) cmd_rename_session_exec(struct cmd *self, struct cmdq_item *item)
{ {
struct args *args = self->args; struct args *args = self->args;
struct session *s = item->target.s; struct client *c = cmd_find_client(item, NULL, 0);
const char *newname; struct session *s = item->target.s;
char *newname;
newname = args->argv[0]; newname = format_single(item, args->argv[0], c, s, NULL, NULL);
if (strcmp(newname, s->name) == 0) if (strcmp(newname, s->name) == 0) {
free(newname);
return (CMD_RETURN_NORMAL); return (CMD_RETURN_NORMAL);
}
if (!session_check_name(newname)) { if (!session_check_name(newname)) {
cmdq_error(item, "bad session name: %s", newname); cmdq_error(item, "bad session name: %s", newname);
free(newname);
return (CMD_RETURN_ERROR); return (CMD_RETURN_ERROR);
} }
if (session_find(newname) != NULL) { if (session_find(newname) != NULL) {
cmdq_error(item, "duplicate session: %s", newname); cmdq_error(item, "duplicate session: %s", newname);
free(newname);
return (CMD_RETURN_ERROR); return (CMD_RETURN_ERROR);
} }
RB_REMOVE(sessions, &sessions, s); RB_REMOVE(sessions, &sessions, s);
free(s->name); free(s->name);
s->name = xstrdup(newname); s->name = newname;
RB_INSERT(sessions, &sessions, s); RB_INSERT(sessions, &sessions, s);
server_status_session(s); server_status_session(s);

View File

@ -46,12 +46,17 @@ static enum cmd_retval
cmd_rename_window_exec(struct cmd *self, struct cmdq_item *item) cmd_rename_window_exec(struct cmd *self, struct cmdq_item *item)
{ {
struct args *args = self->args; struct args *args = self->args;
struct client *c = cmd_find_client(item, NULL, 0);
struct session *s = item->target.s;
struct winlink *wl = item->target.wl; struct winlink *wl = item->target.wl;
char *newname;
window_set_name(wl->window, args->argv[0]); newname = format_single(item, args->argv[0], c, s, wl, NULL);
window_set_name(wl->window, newname);
options_set_number(wl->window->options, "automatic-rename", 0); options_set_number(wl->window->options, "automatic-rename", 0);
server_status_window(wl->window); server_status_window(wl->window);
free(newname);
return (CMD_RETURN_NORMAL); return (CMD_RETURN_NORMAL);
} }

View File

@ -61,8 +61,8 @@ cmd_split_window_exec(struct cmd *self, struct cmdq_item *item)
struct window *w = wl->window; struct window *w = wl->window;
struct window_pane *wp = item->target.wp, *new_wp = NULL; struct window_pane *wp = item->target.wp, *new_wp = NULL;
struct environ *env; struct environ *env;
const char *cmd, *path, *shell, *template, *cwd; const char *cmd, *path, *shell, *template, *tmp;
char **argv, *cause, *new_cause, *cp, *to_free = NULL; char **argv, *cause, *new_cause, *cp, *cwd;
u_int hlimit; u_int hlimit;
int argc, size, percentage; int argc, size, percentage;
enum layout_type type; enum layout_type type;
@ -86,14 +86,12 @@ cmd_split_window_exec(struct cmd *self, struct cmdq_item *item)
argv = args->argv; argv = args->argv;
} }
if (args_has(args, 'c')) { if ((tmp = args_get(args, 'c')) != NULL)
cwd = args_get(args, 'c'); cwd = format_single(item, tmp, c, s, NULL, NULL);
to_free = format_single(item, cwd, c, s, NULL, NULL); else if (item->client != NULL && item->client->session == NULL)
cwd = to_free; cwd = xstrdup(item->client->cwd);
} else if (item->client != NULL && item->client->session == NULL)
cwd = item->client->cwd;
else else
cwd = s->cwd; cwd = xstrdup(s->cwd);
type = LAYOUT_TOPBOTTOM; type = LAYOUT_TOPBOTTOM;
if (args_has(args, 'h')) if (args_has(args, 'h'))
@ -175,7 +173,7 @@ cmd_split_window_exec(struct cmd *self, struct cmdq_item *item)
cmd_find_from_winlink_pane(&fs, wl, new_wp, 0); cmd_find_from_winlink_pane(&fs, wl, new_wp, 0);
hooks_insert(s->hooks, item, &fs, "after-split-window"); hooks_insert(s->hooks, item, &fs, "after-split-window");
free(to_free); free(cwd);
return (CMD_RETURN_NORMAL); return (CMD_RETURN_NORMAL);
error: error:
@ -186,6 +184,6 @@ error:
cmdq_error(item, "create pane failed: %s", cause); cmdq_error(item, "create pane failed: %s", cause);
free(cause); free(cause);
free(to_free); free(cwd);
return (CMD_RETURN_ERROR); return (CMD_RETURN_ERROR);
} }