Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: update to latest version of adopt #19

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading