]> git.ozlabs.org Git - ccan/blob - ccan/opt/test/utils.c
opt: fix up outdated comments in documentation.
[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 struct opt_table short_table[] = {
84         /* Short opts, different args. */
85         OPT_WITHOUT_ARG("-a", test_noarg, "a", "Description of a"),
86         OPT_WITH_ARG("-b", test_arg, show_arg, "b", "Description of b"),
87         OPT_ENDTABLE
88 };
89
90 struct opt_table long_table[] = {
91         /* Long opts, different args. */
92         OPT_WITHOUT_ARG("--ddd", test_noarg, "ddd", "Description of ddd"),
93         OPT_WITH_ARG("--eee <filename>", test_arg, show_arg, "eee", ""),
94         OPT_ENDTABLE
95 };
96
97 struct opt_table long_and_short_table[] = {
98         /* Short and long, different args. */
99         OPT_WITHOUT_ARG("--ggg|-g", test_noarg, "ggg", "Description of ggg"),
100         OPT_WITH_ARG("-h|--hhh", test_arg, NULL, "hhh", "Description of hhh"),
101         OPT_ENDTABLE
102 };
103
104 /* Sub-table test. */
105 struct opt_table subtables[] = {
106         /* Two short, and two long long, no description */
107         OPT_WITH_ARG("--jjj|-j|--lll|-l", test_arg, show_arg, "jjj", ""),
108         /* Hidden option */
109         OPT_WITH_ARG("--mmm|-m", test_arg, show_arg, "mmm", opt_hidden),
110         OPT_SUBTABLE(short_table, NULL),
111         OPT_SUBTABLE(long_table, "long table options"),
112         OPT_SUBTABLE(long_and_short_table, NULL),
113         OPT_ENDTABLE
114 };