]> git.ozlabs.org Git - ccan/blob - ccan/opt/test/utils.c
9544fa77bd3493382c07e72f77f770d09b3d4cc7
[ccan] / ccan / opt / test / utils.c
1 #include "config.h"
2 #include <ccan/tap/tap.h>
3 #include <stdarg.h>
4 #include <stdlib.h>
5 #include <ccan/opt/opt.h>
6 #include <getopt.h>
7 #include <string.h>
8 #include <stdio.h>
9 #include "utils.h"
10
11 unsigned int test_cb_called;
12 char *test_noarg(void *arg)
13 {
14         test_cb_called++;
15         return NULL;
16 }
17
18 char *test_arg(const char *optarg, const char *arg)
19 {
20         test_cb_called++;
21         ok1(strcmp(optarg, arg) == 0);
22         return NULL;
23 }
24
25 void show_arg(char buf[OPT_SHOW_LEN], const char *arg)
26 {
27         strncpy(buf, arg, OPT_SHOW_LEN);
28 }
29
30 char *err_output = NULL;
31
32 void save_err_output(const char *fmt, ...)
33 {
34         va_list ap;
35         char *p;
36
37         va_start(ap, fmt);
38         /* Check return, for fascist gcc */
39         if (vasprintf(&p, fmt, ap) == -1)
40                 p = NULL;
41         va_end(ap);
42
43         if (err_output) {
44                 err_output = realloc(err_output,
45                                      strlen(err_output) + strlen(p) + 1);
46                 strcat(err_output, p);
47                 free(p);
48         } else
49                 err_output = p;
50 }       
51
52 static bool allocated = false;
53
54 bool parse_args(int *argc, char ***argv, ...)
55 {
56         char **a;
57         va_list ap;
58
59         va_start(ap, argv);
60         *argc = 1;
61         a = malloc(sizeof(*a) * (*argc + 1));
62         a[0] = (*argv)[0];
63         while ((a[*argc] = va_arg(ap, char *)) != NULL) {
64                 (*argc)++;
65                 a = realloc(a, sizeof(*a) * (*argc + 1));
66         }
67
68         if (allocated)
69                 free(*argv);
70
71         *argv = a;
72         allocated = true;
73         /* Re-set before parsing. */
74         optind = 0;
75
76         return opt_parse(argc, *argv, save_err_output);
77 }
78
79 struct opt_table short_table[] = {
80         /* Short opts, different args. */
81         OPT_WITHOUT_ARG("-a", test_noarg, "a", "Description of a"),
82         OPT_WITH_ARG("-b", test_arg, show_arg, "b", "Description of b"),
83         OPT_ENDTABLE
84 };
85
86 struct opt_table long_table[] = {
87         /* Long opts, different args. */
88         OPT_WITHOUT_ARG("--ddd", test_noarg, "ddd", "Description of ddd"),
89         OPT_WITH_ARG("--eee <filename>", test_arg, show_arg, "eee", ""),
90         OPT_ENDTABLE
91 };
92
93 struct opt_table long_and_short_table[] = {
94         /* Short and long, different args. */
95         OPT_WITHOUT_ARG("--ggg|-g", test_noarg, "ggg", "Description of ggg"),
96         OPT_WITH_ARG("-h|--hhh", test_arg, NULL, "hhh", "Description of hhh"),
97         OPT_ENDTABLE
98 };
99
100 /* Sub-table test. */
101 struct opt_table subtables[] = {
102         /* Two short, and two long long, no description */
103         OPT_WITH_ARG("--jjj|-j|--lll|-l", test_arg, show_arg, "jjj", ""),
104         /* Hidden option */
105         OPT_WITH_ARG("--mmm|-m", test_arg, show_arg, "mmm", opt_hidden),
106         OPT_SUBTABLE(short_table, NULL),
107         OPT_SUBTABLE(long_table, "long table options"),
108         OPT_SUBTABLE(long_and_short_table, NULL),
109         OPT_ENDTABLE
110 };