diff options
author | Nicholas Marriott <nicm@cvs.openbsd.org> | 2021-08-20 19:50:18 +0000 |
---|---|---|
committer | Nicholas Marriott <nicm@cvs.openbsd.org> | 2021-08-20 19:50:18 +0000 |
commit | 720f841059a27643e512b0f9ac54c28b316e79d8 (patch) | |
tree | 1fd6c606c266b70bc640fe63d054f44396a2d43d /usr.bin/tmux/arguments.c | |
parent | 4ac13be589b12b0ebd28925105fd98cfb8e400e5 (diff) |
Hide struct args behind a couple of accessor functions.
Diffstat (limited to 'usr.bin/tmux/arguments.c')
-rw-r--r-- | usr.bin/tmux/arguments.c | 64 |
1 files changed, 43 insertions, 21 deletions
diff --git a/usr.bin/tmux/arguments.c b/usr.bin/tmux/arguments.c index aac6acb9ce4..875f04ebd51 100644 --- a/usr.bin/tmux/arguments.c +++ b/usr.bin/tmux/arguments.c @@ -1,4 +1,4 @@ -/* $OpenBSD: arguments.c,v 1.37 2021/08/20 18:59:53 nicm Exp $ */ +/* $OpenBSD: arguments.c,v 1.38 2021/08/20 19:50:16 nicm Exp $ */ /* * Copyright (c) 2010 Nicholas Marriott <nicholas.marriott@gmail.com> @@ -38,6 +38,12 @@ struct args_entry { RB_ENTRY(args_entry) entry; }; +struct args { + struct args_tree tree; + int argc; + char **argv; +}; + static struct args_entry *args_find(struct args *, u_char); static int args_cmp(struct args_entry *, struct args_entry *); @@ -73,7 +79,7 @@ args_create(void) /* Parse an argv and argc into a new argument set. */ struct args * -args_parse(const char *template, int argc, char **argv) +args_parse(const char *template, int argc, char **argv, int lower, int upper) { struct args *args; int opt; @@ -99,6 +105,10 @@ args_parse(const char *template, int argc, char **argv) args->argc = argc; args->argv = cmd_copy_argv(argc, argv); + if ((lower != -1 && argc < lower) || (upper != -1 && argc > upper)) { + args_free(args); + return (NULL); + } return (args); } @@ -126,6 +136,14 @@ args_free(struct args *args) free(args); } +/* Convert arguments to vector. */ +void +args_vector(struct args *args, int *argc, char ***argv) +{ + *argc = args->argc; + *argv = cmd_copy_argv(args->argc, args->argv); +} + /* Add to string. */ static void printflike(3, 4) args_print_add(char **buf, size_t *len, const char *fmt, ...) @@ -145,23 +163,6 @@ args_print_add(char **buf, size_t *len, const char *fmt, ...) free(s); } -/* Add value to string. */ -static void -args_print_add_value(char **buf, size_t *len, struct args_entry *entry, - struct args_value *value) -{ - char *escaped; - - if (**buf != '\0') - args_print_add(buf, len, " -%c ", entry->flag); - else - args_print_add(buf, len, "-%c ", entry->flag); - - escaped = args_escape(value->value); - args_print_add(buf, len, "%s", escaped); - free(escaped); -} - /* Add argument to string. */ static void args_print_add_argument(char **buf, size_t *len, const char *argument) @@ -203,8 +204,13 @@ args_print(struct args *args) /* Then the flags with arguments. */ RB_FOREACH(entry, args_tree, &args->tree) { - TAILQ_FOREACH(value, &entry->values, entry) - args_print_add_value(&buf, &len, entry, value); + TAILQ_FOREACH(value, &entry->values, entry) { + if (*buf != '\0') + args_print_add(&buf, &len, " -%c", entry->flag); + else + args_print_add(&buf, &len, "-%c", entry->flag); + args_print_add_argument(&buf, &len, value->value); + } } /* And finally the argument vector. */ @@ -330,6 +336,22 @@ args_next(struct args_entry **entry) return ((*entry)->flag); } +/* Get argument count. */ +u_int +args_count(struct args *args) +{ + return (args->argc); +} + +/* Return argument as string. */ +const char * +args_string(struct args *args, u_int idx) +{ + if (idx >= (u_int)args->argc) + return (NULL); + return (args->argv[idx]); +} + /* Get first value in argument. */ struct args_value * args_first_value(struct args *args, u_char flag) |