X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Fopt%2Ftest%2Futils.c;h=c2967fca173d51010b810fcaa626eb6ebff420df;hp=9544fa77bd3493382c07e72f77f770d09b3d4cc7;hb=2e26b06e6384c4483dde844cf8991846075e552c;hpb=6fa62e5ae1e29646aa718e99020fdf2efee74373 diff --git a/ccan/opt/test/utils.c b/ccan/opt/test/utils.c index 9544fa77..c2967fca 100644 --- a/ccan/opt/test/utils.c +++ b/ccan/opt/test/utils.c @@ -3,7 +3,6 @@ #include #include #include -#include #include #include #include "utils.h" @@ -49,6 +48,13 @@ void save_err_output(const char *fmt, ...) err_output = p; } +void reset_options(void) +{ + opt_free_table(); + free(err_output); + err_output = NULL; +} + static bool allocated = false; bool parse_args(int *argc, char ***argv, ...) @@ -70,12 +76,33 @@ bool parse_args(int *argc, char ***argv, ...) *argv = a; allocated = true; - /* Re-set before parsing. */ - optind = 0; return opt_parse(argc, *argv, save_err_output); } +bool parse_early_args(int *argc, char ***argv, ...) +{ + char **a; + va_list ap; + + va_start(ap, argv); + *argc = 1; + a = malloc(sizeof(*a) * (*argc + 1)); + a[0] = (*argv)[0]; + while ((a[*argc] = va_arg(ap, char *)) != NULL) { + (*argc)++; + a = realloc(a, sizeof(*a) * (*argc + 1)); + } + + if (allocated) + free(*argv); + + *argv = a; + allocated = true; + + return opt_early_parse(*argc, *argv, save_err_output); +} + struct opt_table short_table[] = { /* Short opts, different args. */ OPT_WITHOUT_ARG("-a", test_noarg, "a", "Description of a"),