]> git.ozlabs.org Git - ccan/blob - ccan/opt/opt.c
aa85325dd339c6b10f166a825aeec2ba4848400b
[ccan] / ccan / opt / opt.c
1 #include <ccan/opt/opt.h>
2 #include <string.h>
3 #include <errno.h>
4 #include <getopt.h>
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <err.h>
8 #include <assert.h>
9 #include <stdarg.h>
10 #include <stdint.h>
11 #include "private.h"
12
13 struct opt_table *opt_table;
14 unsigned int opt_count, opt_num_short, opt_num_short_arg, opt_num_long;
15 const char *opt_argv0;
16
17 /* Returns string after first '-'. */
18 static const char *first_name(const char *names, unsigned *len)
19 {
20         *len = strcspn(names + 1, "/");
21         return names + 1;
22 }
23
24 static const char *next_name(const char *names, unsigned *len)
25 {
26         names += *len;
27         if (!names[0])
28                 return NULL;
29         return first_name(names + 1, len);
30 }
31
32 static const char *first_opt(unsigned *i, unsigned *len)
33 {
34         for (*i = 0; *i < opt_count; (*i)++) {
35                 if (opt_table[*i].flags == OPT_SUBTABLE)
36                         continue;
37                 return first_name(opt_table[*i].names, len);
38         }
39         return NULL;
40 }
41
42 static const char *next_opt(const char *p, unsigned *i, unsigned *len)
43 {
44         if (!p)
45                 (*i)++;
46         for (; *i < opt_count; (*i)++) {
47                 if (opt_table[*i].flags == OPT_SUBTABLE)
48                         continue;
49                 if (!p)
50                         return first_name(opt_table[*i].names, len);
51                 p = next_name(p, len);
52                 if (p)
53                         return p;
54         }
55         return NULL;
56 }
57
58 static const char *first_lopt(unsigned *i, unsigned *len)
59 {
60         const char *p;
61         for (p = first_opt(i, len); p; p = next_opt(p, i, len)) {
62                 if (p[0] == '-') {
63                         /* Skip leading "-" */
64                         (*len)--;
65                         p++;
66                         break;
67                 }
68         }
69         return p;
70 }
71
72 static const char *next_lopt(const char *p, unsigned *i, unsigned *len)
73 {
74         for (p = next_opt(p, i, len); p; p = next_opt(p, i, len)) {
75                 if (p[0] == '-') {
76                         /* Skip leading "-" */
77                         (*len)--;
78                         p++;
79                         break;
80                 }
81         }
82         return p;
83 }
84
85 const char *first_sopt(unsigned *i)
86 {
87         const char *p;
88         unsigned int len;
89
90         for (p = first_opt(i, &len); p; p = next_opt(p, i, &len)) {
91                 if (p[0] != '-')
92                         break;
93         }
94         return p;
95 }
96
97 const char *next_sopt(const char *p, unsigned *i)
98 {
99         unsigned int len = 1;
100         for (p = next_opt(p, i, &len); p; p = next_opt(p, i, &len)) {
101                 if (p[0] != '-')
102                         break;
103         }
104         return p;
105 }
106
107 static void check_opt(const struct opt_table *entry)
108 {
109         const char *p;
110         unsigned len;
111
112         assert(entry->flags == OPT_HASARG || entry->flags == OPT_NOARG);
113
114         assert(entry->names[0] == '-');
115         for (p = first_name(entry->names, &len); p; p = next_name(p, &len)) {
116                 if (*p == '-') {
117                         assert(len > 1);
118                         opt_num_long++;
119                 } else {
120                         assert(len == 1);
121                         assert(*p != ':');
122                         opt_num_short++;
123                         if (entry->flags == OPT_HASARG) {
124                                 opt_num_short_arg++;
125                                 /* FIXME: -? with ops breaks getopt_long */
126                                 assert(*p != '?');
127                         }
128                 }
129         }
130 }
131
132 static void add_opt(const struct opt_table *entry)
133 {
134         opt_table = realloc(opt_table, sizeof(opt_table[0]) * (opt_count+1));
135         opt_table[opt_count++] = *entry;
136 }
137
138 void _opt_register(const char *names, enum opt_flags flags,
139                    char *(*cb)(void *arg),
140                    char *(*cb_arg)(const char *optarg, void *arg),
141                    void (*show)(char buf[OPT_SHOW_LEN], const void *arg),
142                    void *arg, const char *desc)
143 {
144         struct opt_table opt;
145         opt.names = names;
146         opt.flags = flags;
147         opt.cb = cb;
148         opt.cb_arg = cb_arg;
149         opt.show = show;
150         opt.arg = arg;
151         opt.desc = desc;
152         check_opt(&opt);
153         add_opt(&opt);
154 }
155
156 void opt_register_table(const struct opt_table entry[], const char *desc)
157 {
158         unsigned int i, start = opt_count;
159
160         if (desc) {
161                 struct opt_table heading = OPT_SUBTABLE(NULL, desc);
162                 add_opt(&heading);
163         }
164         for (i = 0; entry[i].flags != OPT_END; i++) {
165                 if (entry[i].flags == OPT_SUBTABLE)
166                         opt_register_table(subtable_of(&entry[i]),
167                                            entry[i].desc);
168                 else {
169                         check_opt(&entry[i]);
170                         add_opt(&entry[i]);
171                 }
172         }
173         /* We store the table length in arg ptr. */
174         if (desc)
175                 opt_table[start].arg = (void *)(intptr_t)(opt_count - start);
176 }
177
178 static char *make_optstring(void)
179 {
180         char *str = malloc(1 + opt_num_short + opt_num_short_arg + 1);
181         const char *p;
182         unsigned int i, num = 0;
183
184         /* This tells getopt_long we want a ':' returned for missing arg. */
185         str[num++] = ':';
186         for (p = first_sopt(&i); p; p = next_sopt(p, &i)) {
187                 str[num++] = *p;
188                 if (opt_table[i].flags == OPT_HASARG)
189                         str[num++] = ':';
190         }
191         str[num++] = '\0';
192         assert(num == 1 + opt_num_short + opt_num_short_arg + 1);
193         return str;
194 }
195
196 static struct option *make_options(void)
197 {
198         struct option *options = malloc(sizeof(*options) * (opt_num_long + 1));
199         unsigned int i, num = 0, len;
200         const char *p;
201
202         for (p = first_lopt(&i, &len); p; p = next_lopt(p, &i, &len)) {
203                 char *buf = malloc(len + 1);
204                 memcpy(buf, p, len);
205                 buf[len] = 0;
206                 options[num].name = buf;
207                 options[num].has_arg = (opt_table[i].flags == OPT_HASARG);
208                 options[num].flag = NULL;
209                 options[num].val = 0;
210                 num++;
211         }
212         memset(&options[num], 0, sizeof(options[num]));
213         assert(num == opt_num_long);
214         return options;
215 }
216
217 static struct opt_table *find_short(char shortopt)
218 {
219         unsigned int i;
220         const char *p;
221
222         for (p = first_sopt(&i); p; p = next_sopt(p, &i)) {
223                 if (*p == shortopt)
224                         return &opt_table[i];
225         }
226         abort();
227 }
228
229 /* We want the index'th long entry. */
230 static struct opt_table *find_long(int index, const char **name)
231 {
232         unsigned int i, len;
233         const char *p;
234
235         for (p = first_lopt(&i, &len); p; p = next_lopt(p, &i, &len)) {
236                 if (index == 0) {
237                         *name = p;
238                         return &opt_table[i];
239                 }
240                 index--;
241         }
242         abort();
243 }
244
245 /* glibc does this as:
246 /tmp/opt-example: invalid option -- 'x'
247 /tmp/opt-example: unrecognized option '--long'
248 /tmp/opt-example: option '--someflag' doesn't allow an argument
249 /tmp/opt-example: option '--s' is ambiguous
250 /tmp/opt-example: option requires an argument -- 's'
251 */
252 static void parse_fail(void (*errlog)(const char *fmt, ...),
253                        char shortopt, const char *longopt, const char *problem)
254 {
255         if (shortopt)
256                 errlog("%s: -%c: %s", opt_argv0, shortopt, problem);
257         else
258                 errlog("%s: --%.*s: %s", opt_argv0,
259                        strcspn(longopt, "/"), longopt, problem);
260 }
261
262 void dump_optstate(void);
263 void dump_optstate(void)
264 {
265         printf("opterr = %i, optind = %i, optopt = %i, optarg = %s\n",
266                opterr, optind, optopt, optarg);
267 }
268
269 /* Parse your arguments. */
270 bool opt_parse(int *argc, char *argv[], void (*errlog)(const char *fmt, ...))
271 {
272         char *optstring = make_optstring();
273         struct option *options = make_options();
274         int ret, longidx = 0;
275         struct opt_table *e;
276
277         /* We will do our own error reporting. */
278         opterr = 0;
279         opt_argv0 = argv[0];
280
281         /* Reset in case we're called more than once. */
282         optopt = 0;
283         optind = 0;
284         while ((ret = getopt_long(*argc, argv, optstring, options, &longidx))
285                != -1) {
286                 char *problem;
287                 const char *name;
288
289                 /* optopt is 0 if it's an unknown long option, *or* if
290                  * -? is a valid short option. */
291                 if (ret == '?') {
292                         if (optopt || strncmp(argv[optind-1], "--", 2) == 0) {
293                                 parse_fail(errlog, optopt, argv[optind-1]+2,
294                                            "unrecognized option");
295                                 break;
296                         }
297                 } else if (ret == ':') {
298                         /* Missing argument: longidx not updated :( */
299                         parse_fail(errlog, optopt, argv[optind-1]+2,
300                                    "option requires an argument");
301                         break;
302                 }
303
304                 if (ret != 0)
305                         e = find_short(ret);
306                 else
307                         e = find_long(longidx, &name);
308
309                 if (e->flags == OPT_HASARG)
310                         problem = e->cb_arg(optarg, e->arg);
311                 else
312                         problem = e->cb(e->arg);
313
314                 if (problem) {
315                         parse_fail(errlog, ret, name, problem);
316                         free(problem);
317                         break;
318                 }
319         }
320         free(optstring);
321         free(options);
322         if (ret != -1)
323                 return false;
324
325         /* We hide everything but remaining arguments. */
326         memmove(&argv[1], &argv[optind], sizeof(argv[1]) * (*argc-optind+1));
327         *argc -= optind - 1;
328
329         return ret == -1 ? true : false;
330 }
331
332 void opt_log_stderr(const char *fmt, ...)
333 {
334         va_list ap;
335
336         va_start(ap, fmt);
337         vfprintf(stderr, fmt, ap);
338         va_end(ap);
339 }
340
341 char *opt_invalid_argument(const char *arg)
342 {
343         char *str = malloc(sizeof("Invalid argument '%s'") + strlen(arg));
344         sprintf(str, "Invalid argument '%s'", arg);
345         return str;
346 }