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