diff options
author | Nicholas Marriott <nicm@openbsd.org> | 2011-01-04 00:42:46 +0000 |
---|---|---|
committer | Nicholas Marriott <nicm@openbsd.org> | 2011-01-04 00:42:46 +0000 |
commit | 7502cb3adbb26a2f94445a35626e64041d6769f9 (patch) | |
tree | 6ae4f33f69dc0a470dbe905b5140216fb19b8f01 /cmd-new-session.c | |
parent | ac3b78a84178a308536a56ea114b0f6f8ce6fb47 (diff) | |
download | rtmux-7502cb3adbb26a2f94445a35626e64041d6769f9.tar.gz rtmux-7502cb3adbb26a2f94445a35626e64041d6769f9.tar.bz2 rtmux-7502cb3adbb26a2f94445a35626e64041d6769f9.zip |
Clean up and simplify tmux command argument parsing.
Originally, tmux commands were parsed in the client process into a
struct with the command data which was then serialised and sent to the
server to be executed. The parsing was later moved into the server (an
argv was sent from the client), but the parse step and intermediate
struct was kept.
This change removes that struct and the separate parse step. Argument
parsing and printing is now common to all commands (in arguments.c) with
each command left with just an optional check function (to validate the
arguments at parse time), the exec function and a function to set up any
key bindings (renamed from the old init function).
This is overall more simple and consistent.
There should be no changes to any commands behaviour or syntax although
as this touches every command please watch for any unexpected changes.
Diffstat (limited to 'cmd-new-session.c')
-rw-r--r-- | cmd-new-session.c | 177 |
1 files changed, 38 insertions, 139 deletions
diff --git a/cmd-new-session.c b/cmd-new-session.c index 53a50bbb..d53e2692 100644 --- a/cmd-new-session.c +++ b/cmd-new-session.c @@ -29,119 +29,55 @@ * Create a new session and attach to the current terminal unless -d is given. */ -int cmd_new_session_parse(struct cmd *, int, char **, char **); +int cmd_new_session_check(struct args *); int cmd_new_session_exec(struct cmd *, struct cmd_ctx *); -void cmd_new_session_free(struct cmd *); -void cmd_new_session_init(struct cmd *, int); -size_t cmd_new_session_print(struct cmd *, char *, size_t); - -struct cmd_new_session_data { - char *target; - char *newname; - char *winname; - char *cmd; - int flag_detached; -}; const struct cmd_entry cmd_new_session_entry = { "new-session", "new", + "dn:s:t:", 0, 1, "[-d] [-n window-name] [-s session-name] [-t target-session] [command]", - CMD_STARTSERVER|CMD_CANTNEST|CMD_SENDENVIRON, "", - cmd_new_session_init, - cmd_new_session_parse, - cmd_new_session_exec, - cmd_new_session_free, - cmd_new_session_print + CMD_STARTSERVER|CMD_CANTNEST|CMD_SENDENVIRON, + NULL, + cmd_new_session_check, + cmd_new_session_exec }; -/* ARGSUSED */ -void -cmd_new_session_init(struct cmd *self, unused int arg) -{ - struct cmd_new_session_data *data; - - self->data = data = xmalloc(sizeof *data); - data->flag_detached = 0; - data->target = NULL; - data->newname = NULL; - data->winname = NULL; - data->cmd = NULL; -} - int -cmd_new_session_parse(struct cmd *self, int argc, char **argv, char **cause) +cmd_new_session_check(struct args *args) { - struct cmd_new_session_data *data; - int opt; - - self->entry->init(self, KEYC_NONE); - data = self->data; - - while ((opt = getopt(argc, argv, "ds:t:n:")) != -1) { - switch (opt) { - case 'd': - data->flag_detached = 1; - break; - case 's': - if (data->newname == NULL) - data->newname = xstrdup(optarg); - break; - case 't': - if (data->target == NULL) - data->target = xstrdup(optarg); - break; - case 'n': - if (data->winname == NULL) - data->winname = xstrdup(optarg); - break; - default: - goto usage; - } - } - argc -= optind; - argv += optind; - if (argc != 0 && argc != 1) - goto usage; - - if (data->target != NULL && (argc == 1 || data->winname != NULL)) - goto usage; - - if (argc == 1) - data->cmd = xstrdup(argv[0]); - + if (args_has(args, 't') && (args->argc != 0 || args_has(args, 'n'))) + return (-1); return (0); - -usage: - xasprintf(cause, "usage: %s %s", self->entry->name, self->entry->usage); - - self->entry->free(self); - return (-1); } int cmd_new_session_exec(struct cmd *self, struct cmd_ctx *ctx) { - struct cmd_new_session_data *data = self->data; - struct session *s, *old_s, *groupwith; - struct window *w; - struct window_pane *wp; - struct environ env; - struct termios tio, *tiop; - struct passwd *pw; - const char *update, *cwd; - char *overrides, *cmd, *cause; - int detached, idx; - u_int sx, sy, i; - - if (data->newname != NULL && session_find(data->newname) != NULL) { - ctx->error(ctx, "duplicate session: %s", data->newname); + struct args *args = self->args; + struct session *s, *old_s, *groupwith; + struct window *w; + struct window_pane *wp; + struct environ env; + struct termios tio, *tiop; + struct passwd *pw; + const char *newname, *target, *update, *cwd; + char *overrides, *cmd, *cause; + int detached, idx; + u_int sx, sy, i; + + newname = args_get(args, 's'); + if (newname != NULL && session_find(newname) != NULL) { + ctx->error(ctx, "duplicate session: %s", newname); return (-1); } - groupwith = NULL; - if (data->target != NULL && - (groupwith = cmd_find_session(ctx, data->target)) == NULL) - return (-1); + target = args_get(args, 't'); + if (target != NULL) { + groupwith = cmd_find_session(ctx, target); + if (groupwith == NULL) + return (-1); + } else + groupwith = NULL; /* * There are three cases: @@ -162,7 +98,7 @@ cmd_new_session_exec(struct cmd *self, struct cmd_ctx *ctx) */ /* Set -d if no client. */ - detached = data->flag_detached; + detached = args_has(args, 'd'); if (ctx->cmdclient == NULL && ctx->curclient == NULL) detached = 1; @@ -228,10 +164,10 @@ cmd_new_session_exec(struct cmd *self, struct cmd_ctx *ctx) sy = 1; /* Figure out the command for the new window. */ - if (data->target != NULL) + if (target != NULL) cmd = NULL; - else if (data->cmd != NULL) - cmd = data->cmd; + else if (args->argc != 0) + cmd = args->argv[0]; else cmd = options_get_string(&global_s_options, "default-command"); @@ -243,8 +179,7 @@ cmd_new_session_exec(struct cmd *self, struct cmd_ctx *ctx) /* Create the new session. */ idx = -1 - options_get_number(&global_s_options, "base-index"); - s = session_create( - data->newname, cmd, cwd, &env, tiop, idx, sx, sy, &cause); + s = session_create(newname, cmd, cwd, &env, tiop, idx, sx, sy, &cause); if (s == NULL) { ctx->error(ctx, "create session failed: %s", cause); xfree(cause); @@ -253,11 +188,11 @@ cmd_new_session_exec(struct cmd *self, struct cmd_ctx *ctx) environ_free(&env); /* Set the initial window name if one given. */ - if (cmd != NULL && data->winname != NULL) { + if (cmd != NULL && args_has(args, 'n')) { w = s->curw->window; xfree(w->name); - w->name = xstrdup(data->winname); + w->name = xstrdup(args_get(args, 'n')); options_set_number(&w->options, "automatic-rename", 0); } @@ -316,39 +251,3 @@ cmd_new_session_exec(struct cmd *self, struct cmd_ctx *ctx) return (!detached); /* 1 means don't tell command client to exit */ } - -void -cmd_new_session_free(struct cmd *self) -{ - struct cmd_new_session_data *data = self->data; - - if (data->newname != NULL) - xfree(data->newname); - if (data->winname != NULL) - xfree(data->winname); - if (data->cmd != NULL) - xfree(data->cmd); - xfree(data); -} - -size_t -cmd_new_session_print(struct cmd *self, char *buf, size_t len) -{ - struct cmd_new_session_data *data = self->data; - size_t off = 0; - - off += xsnprintf(buf, len, "%s", self->entry->name); - if (data == NULL) - return (off); - if (off < len && data->flag_detached) - off += xsnprintf(buf + off, len - off, " -d"); - if (off < len && data->winname != NULL) - off += cmd_prarg(buf + off, len - off, " -n ", data->winname); - if (off < len && data->newname != NULL) - off += cmd_prarg(buf + off, len - off, " -s ", data->newname); - if (off < len && data->target != NULL) - off += cmd_prarg(buf + off, len - off, " -t ", data->target); - if (off < len && data->cmd != NULL) - off += cmd_prarg(buf + off, len - off, " ", data->cmd); - return (off); -} |