Skip to content

Commit

Permalink
cli: update to latest version of adopt
Browse files Browse the repository at this point in the history
  • Loading branch information
ethomson committed Dec 14, 2023
1 parent 1e44d32 commit 50aca17
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
30 changes: 26 additions & 4 deletions cli/ntlm_cli_opt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stdlib.h>
Expand All @@ -22,7 +22,7 @@
#include "ntlm_cli_opt.h"

#ifdef _WIN32
# include <Windows.h>
# include <windows.h>
#else
# include <fcntl.h>
# include <sys/ioctl.h>
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down
20 changes: 19 additions & 1 deletion cli/ntlm_cli_opt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 50aca17

Please sign in to comment.