-#define _GNU_SOURCE
+#include "config.h"
#include <ccan/tap/tap.h>
#include <stdarg.h>
#include <stdlib.h>
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) {
err_output = p;
}
-/* FIXME: This leaks, BTW. */
+void reset_options(void)
+{
+ opt_free_table();
+ free(err_output);
+ err_output = NULL;
+}
+
+static bool allocated = false;
+
bool parse_args(int *argc, char ***argv, ...)
{
char **a;
(*argc)++;
a = realloc(a, sizeof(*a) * (*argc + 1));
}
+
+ if (allocated)
+ free(*argv);
+
*argv = a;
+ allocated = true;
/* Re-set before parsing. */
optind = 0;
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("-h/--hhh", test_arg, NULL, "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[] = {
/* Two short, and two long long, no description */
- OPT_WITH_ARG("--jjj/-j/--lll/-l", test_arg, show_arg, "jjj", ""),
+ 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_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, NULL),