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.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.c')
-rw-r--r-- | cmd.c | 58 |
1 files changed, 30 insertions, 28 deletions
@@ -167,7 +167,7 @@ cmd_unpack_argv(char *buf, size_t len, int argc, char ***argv) } char ** -cmd_copy_argv(int argc, char **argv) +cmd_copy_argv(int argc, char *const *argv) { char **new_argv; int i; @@ -201,8 +201,9 @@ cmd_parse(int argc, char **argv, char **cause) { const struct cmd_entry **entryp, *entry; struct cmd *cmd; + struct args *args; char s[BUFSIZ]; - int opt, ambiguous = 0; + int ambiguous = 0; *cause = NULL; if (argc == 0) { @@ -236,30 +237,19 @@ cmd_parse(int argc, char **argv, char **cause) return (NULL); } - optreset = 1; - optind = 1; - if (entry->parse == NULL) { - while ((opt = getopt(argc, argv, "")) != -1) { - switch (opt) { - default: - goto usage; - } - } - argc -= optind; - argv += optind; - if (argc != 0) - goto usage; - } + args = args_parse(entry->args_template, argc, argv); + if (args == NULL) + goto usage; + if (entry->args_lower != -1 && args->argc < entry->args_lower) + goto usage; + if (entry->args_upper != -1 && args->argc > entry->args_upper) + goto usage; + if (entry->check != NULL && entry->check(args) != 0) + goto usage; cmd = xmalloc(sizeof *cmd); cmd->entry = entry; - cmd->data = NULL; - if (entry->parse != NULL) { - if (entry->parse(cmd, argc, argv, cause) != 0) { - xfree(cmd); - return (NULL); - } - } + cmd->args = args; return (cmd); ambiguous: @@ -277,6 +267,8 @@ ambiguous: return (NULL); usage: + if (args != NULL) + args_free(args); xasprintf(cause, "usage: %s %s", entry->name, entry->usage); return (NULL); } @@ -290,17 +282,27 @@ cmd_exec(struct cmd *cmd, struct cmd_ctx *ctx) void cmd_free(struct cmd *cmd) { - if (cmd->data != NULL && cmd->entry->free != NULL) - cmd->entry->free(cmd); + if (cmd->args != NULL) + args_free(cmd->args); xfree(cmd); } size_t cmd_print(struct cmd *cmd, char *buf, size_t len) { - if (cmd->entry->print == NULL) - return (xsnprintf(buf, len, "%s", cmd->entry->name)); - return (cmd->entry->print(cmd, buf, len)); + size_t off, used; + + off = xsnprintf(buf, len, "%s ", cmd->entry->name); + if (off < len) { + used = args_print(cmd->args, buf + off, len - off); + if (used == 0) + buf[off - 1] = '\0'; + else { + off += used; + buf[off] = '\0'; + } + } + return (off); } /* |