]> git.ozlabs.org Git - ccan/blob - ccan/opt/test/utils.c
2ff04884ebdc0efe6dc4d9dd8fe4ae6af4b9175b
[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 UNNEEDED)
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 bool parse_early_args_incomplete(int *argc, char ***argv, ...)
107 {
108         char **a;
109         va_list ap;
110
111         va_start(ap, argv);
112         *argc = 1;
113         a = malloc(sizeof(*a) * (*argc + 1));
114         a[0] = (*argv)[0];
115         while ((a[*argc] = va_arg(ap, char *)) != NULL) {
116                 (*argc)++;
117                 a = realloc(a, sizeof(*a) * (*argc + 1));
118         }
119
120         if (allocated)
121                 free(*argv);
122
123         *argv = a;
124         allocated = true;
125
126         return opt_early_parse_incomplete(*argc, *argv, save_err_output);
127 }
128
129 struct opt_table short_table[] = {
130         /* Short opts, different args. */
131         OPT_WITHOUT_ARG("-a", test_noarg, "a", "Description of a"),
132         OPT_WITH_ARG("-b", test_arg, show_arg, "b", "Description of b"),
133         OPT_ENDTABLE
134 };
135
136 struct opt_table long_table[] = {
137         /* Long opts, different args. */
138         OPT_WITHOUT_ARG("--ddd", test_noarg, "ddd", "Description of ddd"),
139         OPT_WITH_ARG("--eee <filename>", test_arg, show_arg, "eee", ""),
140         OPT_ENDTABLE
141 };
142
143 struct opt_table long_and_short_table[] = {
144         /* Short and long, different args. */
145         OPT_WITHOUT_ARG("--ggg|-g", test_noarg, "ggg", "Description of ggg"),
146         OPT_WITH_ARG("-h|--hhh", test_arg, NULL, "hhh", "Description of hhh"),
147         OPT_ENDTABLE
148 };
149
150 /* Sub-table test. */
151 struct opt_table subtables[] = {
152         /* Two short, and two long long, no description */
153         OPT_WITH_ARG("--jjj|-j|--lll|-l", test_arg, show_arg, "jjj", ""),
154         /* Hidden option */
155         OPT_WITH_ARG("--mmm|-m", test_arg, show_arg, "mmm", opt_hidden),
156         OPT_SUBTABLE(short_table, NULL),
157         OPT_SUBTABLE(long_table, "long table options"),
158         OPT_SUBTABLE(long_and_short_table, NULL),
159         OPT_ENDTABLE
160 };