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