From 50aca17ea01a881f6298b4efabd0131ca0132911 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Thu, 14 Dec 2023 14:30:11 +0000 Subject: [PATCH] cli: update to latest version of adopt --- cli/ntlm_cli_opt.c | 30 ++++++++++++++++++++++++++---- cli/ntlm_cli_opt.h | 20 +++++++++++++++++++- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/cli/ntlm_cli_opt.c b/cli/ntlm_cli_opt.c index 6c6d961..6626b53 100644 --- a/cli/ntlm_cli_opt.c +++ b/cli/ntlm_cli_opt.c @@ -10,7 +10,7 @@ * This file was produced by using the `rename.pl` script included with * adopt. The command-line specified was: * - * ./rename.pl --out=../ntlmclient/cli/ --filename=ntlm_cli_opt ntlm_opt + * ./rename.pl --filename=ntlm_cli_opt ntlm_opt */ #include @@ -22,7 +22,7 @@ #include "ntlm_cli_opt.h" #ifdef _WIN32 -# include +# include #else # include # include @@ -78,7 +78,7 @@ INLINE(const ntlm_opt_spec *) spec_for_long( /* Handle --option=value arguments */ if (spec->type == NTLM_OPT_TYPE_VALUE && - eql && + spec->name && eql && strncmp(arg, spec->name, eql_pos) == 0 && spec->name[eql_pos] == '\0') { *has_value = 1; @@ -580,6 +580,28 @@ ntlm_opt_status_t ntlm_opt_parse( return validate_required(opt, specs, given_specs); } +int ntlm_opt_foreach( + const ntlm_opt_spec specs[], + char **args, + size_t args_len, + unsigned int flags, + int (*callback)(ntlm_opt *, void *), + void *callback_data) +{ + ntlm_opt_parser parser; + ntlm_opt opt; + int ret; + + ntlm_opt_parser_init(&parser, specs, args, args_len, flags); + + while (ntlm_opt_parser_next(&opt, &parser)) { + if ((ret = callback(&opt, callback_data)) != 0) + return ret; + } + + return 0; +} + static int spec_name_fprint(FILE *file, const ntlm_opt_spec *spec) { int error; @@ -621,7 +643,7 @@ int ntlm_opt_status_fprint( if ((error = fprintf(file, "argument '")) < 0 || (error = spec_name_fprint(file, opt->spec)) < 0 || (error = fprintf(file, "' requires a value.\n")) < 0) - ; + break; break; case NTLM_OPT_STATUS_MISSING_ARGUMENT: if (spec_is_choice(opt->spec)) { diff --git a/cli/ntlm_cli_opt.h b/cli/ntlm_cli_opt.h index a41c9b0..c3aca99 100644 --- a/cli/ntlm_cli_opt.h +++ b/cli/ntlm_cli_opt.h @@ -10,7 +10,7 @@ * This file was produced by using the `rename.pl` script included with * adopt. The command-line specified was: * - * ./rename.pl --out=../ntlmclient/cli/ --filename=ntlm_cli_opt ntlm_opt + * ./rename.pl --filename=ntlm_cli_opt ntlm_opt */ #ifndef NTLM_CLI_OPT_H @@ -300,6 +300,24 @@ ntlm_opt_status_t ntlm_opt_parse( size_t args_len, unsigned int flags); +/** + * Quickly executes the given callback for each argument. + * + * @param specs A NULL-terminated array of `ntlm_opt_spec`s that can be parsed + * @param args The arguments that will be parsed + * @param args_len The length of arguments to be parsed + * @param flags The `ntlm_opt_flag_t flags for parsing + * @param callback The callback to invoke for each specified option + * @param callback_data Data to be provided to the callback + */ +int ntlm_opt_foreach( + const ntlm_opt_spec specs[], + char **args, + size_t args_len, + unsigned int flags, + int (*callback)(ntlm_opt *, void *), + void *callback_data); + /** * Initializes a parser that parses the given arguments according to the * given specifications.