X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Fopt%2Ftest%2Futils.c;fp=ccan%2Fopt%2Ftest%2Futils.c;h=899056ee84f0043f8b28fde8deaeeb5d0c775261;hp=0000000000000000000000000000000000000000;hb=d7d5abe98caeec82d784ce525e0444ff438acd46;hpb=c4c5fed020ba44b9930119672a36a1cb33aff090 diff --git a/ccan/opt/test/utils.c b/ccan/opt/test/utils.c new file mode 100644 index 00000000..899056ee --- /dev/null +++ b/ccan/opt/test/utils.c @@ -0,0 +1,96 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include "utils.h" + +unsigned int test_cb_called; +char *test_noarg(void *arg) +{ + test_cb_called++; + return NULL; +} + +char *test_arg(const char *optarg, void *arg) +{ + test_cb_called++; + ok1(strcmp(optarg, arg) == 0); + return NULL; +} + +char *err_output = NULL; + +static void save_err_output(const char *fmt, ...) +{ + va_list ap; + char *p; + + va_start(ap, fmt); + vasprintf(&p, fmt, ap); + va_end(ap); + + if (err_output) { + err_output = realloc(err_output, + strlen(err_output) + strlen(p) + 1); + strcat(err_output, p); + free(p); + } else + err_output = p; +} + +/* FIXME: This leaks, BTW. */ +bool parse_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)); + } + *argv = a; + /* Re-set before parsing. */ + optind = 0; + + return opt_parse(argc, *argv, save_err_output); +} + +struct opt_table short_table[] = { + /* Short opts, different args. */ + { OPT_WITHOUT_ARG(NULL, 'a', test_noarg, "a"), "Description of a" }, + { OPT_WITH_ARG(NULL, 'b', test_arg, "b"), "Description of b" }, + OPT_ENDTABLE +}; + +struct opt_table long_table[] = { + /* Long opts, different args. */ + { OPT_WITHOUT_ARG("ddd", 0, test_noarg, "ddd"), "Description of ddd" }, + { OPT_WITH_ARG("eee", 0, test_arg, "eee"), "Description of eee" }, + OPT_ENDTABLE +}; + +struct opt_table long_and_short_table[] = { + /* Short and long, different args. */ + { OPT_WITHOUT_ARG("ggg", 'g', test_noarg, "ggg"), + "Description of ggg" }, + { OPT_WITH_ARG("hhh", 'h', test_arg, "hhh"), "Description of hhh"}, + OPT_ENDTABLE +}; + +/* Sub-table test. */ +struct opt_table subtables[] = { + /* Short and long, no description */ + { OPT_WITH_ARG("jjj", 'j', test_arg, "jjj") }, + OPT_SUBTABLE(short_table, NULL), + OPT_SUBTABLE(long_table, "long table options"), + OPT_SUBTABLE(long_and_short_table, opt_table_hidden), + OPT_ENDTABLE +};