From 12dcb09794c48a333bb850d353540c688c503b76 Mon Sep 17 00:00:00 2001 From: dev Date: Sat, 14 Nov 2026 17:42:44 +0000 Subject: 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. --- src/util.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'src/util.c') diff --git a/src/util.c b/src/util.c index e099197..92f17f8 100644 --- a/src/util.c +++ b/src/util.c @@ -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; +} + -- cgit v1.2.3