]> git.ozlabs.org Git - ccan/blobdiff - ccan/opt/test/utils.c
various: make the _info License: wording uniform for GPL variants.
[ccan] / ccan / opt / test / utils.c
index 899056ee84f0043f8b28fde8deaeeb5d0c775261..9544fa77bd3493382c07e72f77f770d09b3d4cc7 100644 (file)
@@ -1,4 +1,4 @@
-#define _GNU_SOURCE
+#include "config.h"
 #include <ccan/tap/tap.h>
 #include <stdarg.h>
 #include <stdlib.h>
@@ -15,22 +15,29 @@ char *test_noarg(void *arg)
        return NULL;
 }
 
-char *test_arg(const char *optarg, void *arg)
+char *test_arg(const char *optarg, const char *arg)
 {
        test_cb_called++;
        ok1(strcmp(optarg, arg) == 0);
        return NULL;
 }
 
+void show_arg(char buf[OPT_SHOW_LEN], const char *arg)
+{
+       strncpy(buf, arg, OPT_SHOW_LEN);
+}
+
 char *err_output = NULL;
 
-static void save_err_output(const char *fmt, ...)
+void save_err_output(const char *fmt, ...)
 {
        va_list ap;
        char *p;
 
        va_start(ap, fmt);
-       vasprintf(&p, fmt, ap);
+       /* Check return, for fascist gcc */
+       if (vasprintf(&p, fmt, ap) == -1)
+               p = NULL;
        va_end(ap);
 
        if (err_output) {
@@ -42,7 +49,8 @@ static void save_err_output(const char *fmt, ...)
                err_output = p;
 }      
 
-/* FIXME: This leaks, BTW. */
+static bool allocated = false;
+
 bool parse_args(int *argc, char ***argv, ...)
 {
        char **a;
@@ -56,7 +64,12 @@ bool parse_args(int *argc, char ***argv, ...)
                (*argc)++;
                a = realloc(a, sizeof(*a) * (*argc + 1));
        }
+
+       if (allocated)
+               free(*argv);
+
        *argv = a;
+       allocated = true;
        /* Re-set before parsing. */
        optind = 0;
 
@@ -65,32 +78,33 @@ bool parse_args(int *argc, char ***argv, ...)
 
 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_WITHOUT_ARG("-a", test_noarg, "a", "Description of a"),
+       OPT_WITH_ARG("-b", test_arg, show_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_WITHOUT_ARG("--ddd", test_noarg, "ddd", "Description of ddd"),
+       OPT_WITH_ARG("--eee <filename>", test_arg, show_arg, "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_WITHOUT_ARG("--ggg|-g", test_noarg, "ggg", "Description of ggg"),
+       OPT_WITH_ARG("-h|--hhh", test_arg, NULL, "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") },
+       /* Two short, and two long long, no description */
+       OPT_WITH_ARG("--jjj|-j|--lll|-l", test_arg, show_arg, "jjj", ""),
+       /* Hidden option */
+       OPT_WITH_ARG("--mmm|-m", test_arg, show_arg, "mmm", opt_hidden),
        OPT_SUBTABLE(short_table, NULL),
        OPT_SUBTABLE(long_table, "long table options"),
-       OPT_SUBTABLE(long_and_short_table, opt_table_hidden),
+       OPT_SUBTABLE(long_and_short_table, NULL),
        OPT_ENDTABLE
 };