diff options
| author | dev <dev@example> | 2026-11-14 17:42:44 +0000 |
|---|---|---|
| committer | dev <dev@example> | 2026-11-14 17:42:44 +0000 |
| commit | 12dcb09794c48a333bb850d353540c688c503b76 (patch) | |
| tree | f1538ec1b1c794790b16b2a4f3b6f1cd4cb99231 /src/util.c | |
| parent | a2a702cb81eaf73a8989cc31c682592c8acb769a (diff) | |
| download | access-control-12dcb09794c48a333bb850d353540c688c503b76.tar.gz access-control-12dcb09794c48a333bb850d353540c688c503b76.zip | |
Options: getopt_long instead of three hand-rolled parsers
option() in hook.c and one_option() in cmd/main.c were the same function
under two names, and validate's seven options were a chain of seven calls
to one of them. getopt_long does all of it - both --opt value and
--opt=value, the lot - so the parsers are gone and what is left is a table
and what each entry sets.
Two small helpers carry what getopt_long cannot: the name behind a long
option's val, for the 'wants a value' message, and the optind reset that
lets a run parse options twice on two different argv slices.
A command now gets argv[0] as its own name, which is where getopt_long
expects argv[0]; the commands that take names step over it.
strip() in validate.c was authz_trim() again, and cmd_authorize freed the
same three pointers on four exit paths.
One behaviour change: 'authorize --' is now the end-of-options marker
rather than an unknown argument. Anything after it is still refused.
Diffstat (limited to 'src/util.c')
| -rw-r--r-- | src/util.c | 21 |
1 files changed, 21 insertions, 0 deletions
@@ -114,3 +114,24 @@ char *xsubstr(const char *s, size_t len) p[len] = '\0'; return p; } + +// The two things every caller of getopt_long otherwise writes by hand. + +const char *authz_optname(int val, const struct option *lo) +{ + for (; lo->name != NULL; lo++) { + if (lo->val == val) + return lo->name; + } + return "?"; +} + +// optind = 1 is how a second getopt_long call is told to begin again; opterr = 0 +// is so the messages come out in this program's words and not glibc's. + +void authz_opt_reset(void) +{ + optind = 1; + opterr = 0; +} + |
