1 #include <ccan/opt/opt.h>
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;
17 static const char *first_name(const char *names, unsigned *len)
19 *len = strcspn(names + 1, "/");
23 static const char *next_name(const char *names, unsigned *len)
28 return first_name(names + 1, len);
31 /* FIXME: Combine with next_opt */
32 static const char *first_opt(bool want_long, unsigned *i, unsigned *len)
35 for (*i = 0; *i < opt_count; (*i)++) {
36 if (opt_table[*i].flags == OPT_SUBTABLE)
38 for (p = first_name(opt_table[*i].names, len);
40 p = next_name(p, len)) {
41 if ((p[0] == '-') == want_long) {
43 /* Skip leading "-" */
54 static const char *next_opt(const char *names, bool want_long,
55 unsigned *i, unsigned *len)
57 const char *p = next_name(names, len);
60 if ((p[0] == '-') == want_long) {
62 /* Skip leading "-" */
68 p = next_name(p, len);
72 } while (*i < opt_count && opt_table[*i].flags == OPT_SUBTABLE);
75 p = first_name(opt_table[*i].names, len);
79 static const char *first_lopt(unsigned *i, unsigned *len)
81 return first_opt(true, i, len);
84 static const char *next_lopt(const char *names, unsigned *i, unsigned *len)
86 return next_opt(names, true, i, len);
89 const char *first_sopt(unsigned *i)
92 return first_opt(false, i, &unused_len);
95 const char *next_sopt(const char *names, unsigned *i)
97 unsigned unused_len = 1;
99 return next_opt(names, false, i, &unused_len);
102 static void check_opt(const struct opt_table *entry)
107 assert(entry->flags == OPT_HASARG || entry->flags == OPT_NOARG);
109 assert(entry->names[0] == '-');
110 for (p = first_name(entry->names, &len); p; p = next_name(p, &len)) {
118 if (entry->flags == OPT_HASARG) {
120 /* FIXME: -? with ops breaks getopt_long */
127 static void add_opt(const struct opt_table *entry)
129 opt_table = realloc(opt_table, sizeof(opt_table[0]) * (opt_count+1));
130 opt_table[opt_count++] = *entry;
133 void _opt_register(const char *names, enum opt_flags flags,
134 char *(*cb)(void *arg),
135 char *(*cb_arg)(const char *optarg, void *arg),
136 void (*show)(char buf[OPT_SHOW_LEN], const void *arg),
137 void *arg, const char *desc)
139 struct opt_table opt;
151 void opt_register_table(const struct opt_table entry[], const char *desc)
153 unsigned int i, start = opt_count;
156 struct opt_table heading = OPT_SUBTABLE(NULL, desc);
159 for (i = 0; entry[i].flags != OPT_END; i++) {
160 if (entry[i].flags == OPT_SUBTABLE)
161 opt_register_table(subtable_of(&entry[i]),
164 check_opt(&entry[i]);
168 /* We store the table length in arg ptr. */
170 opt_table[start].arg = (void *)(intptr_t)(opt_count - start);
173 static char *make_optstring(void)
175 char *str = malloc(1 + opt_num_short + opt_num_short_arg + 1);
177 unsigned int i, num = 0;
179 /* This tells getopt_long we want a ':' returned for missing arg. */
181 for (p = first_sopt(&i); p; p = next_sopt(p, &i)) {
183 if (opt_table[i].flags == OPT_HASARG)
187 assert(num == 1 + opt_num_short + opt_num_short_arg + 1);
191 static struct option *make_options(void)
193 struct option *options = malloc(sizeof(*options) * (opt_num_long + 1));
194 unsigned int i, num = 0, len;
197 for (p = first_lopt(&i, &len); p; p = next_lopt(p, &i, &len)) {
198 char *buf = malloc(len + 1);
201 options[num].name = buf;
202 options[num].has_arg = (opt_table[i].flags == OPT_HASARG);
203 options[num].flag = NULL;
204 options[num].val = 0;
207 memset(&options[num], 0, sizeof(options[num]));
208 assert(num == opt_num_long);
212 static struct opt_table *find_short(char shortopt)
217 for (p = first_sopt(&i); p; p = next_sopt(p, &i)) {
219 return &opt_table[i];
224 /* We want the index'th long entry. */
225 static struct opt_table *find_long(int index, const char **name)
230 for (p = first_lopt(&i, &len); p; p = next_lopt(p, &i, &len)) {
233 return &opt_table[i];
240 /* glibc does this as:
241 /tmp/opt-example: invalid option -- 'x'
242 /tmp/opt-example: unrecognized option '--long'
243 /tmp/opt-example: option '--someflag' doesn't allow an argument
244 /tmp/opt-example: option '--s' is ambiguous
245 /tmp/opt-example: option requires an argument -- 's'
247 static void parse_fail(void (*errlog)(const char *fmt, ...),
248 char shortopt, const char *longopt, const char *problem)
251 errlog("%s: -%c: %s", opt_argv0, shortopt, problem);
253 errlog("%s: --%.*s: %s", opt_argv0,
254 strcspn(longopt, "/"), longopt, problem);
257 void dump_optstate(void);
258 void dump_optstate(void)
260 printf("opterr = %i, optind = %i, optopt = %i, optarg = %s\n",
261 opterr, optind, optopt, optarg);
264 /* Parse your arguments. */
265 bool opt_parse(int *argc, char *argv[], void (*errlog)(const char *fmt, ...))
267 char *optstring = make_optstring();
268 struct option *options = make_options();
269 int ret, longidx = 0;
272 /* We will do our own error reporting. */
276 /* Reset in case we're called more than once. */
279 while ((ret = getopt_long(*argc, argv, optstring, options, &longidx))
284 /* optopt is 0 if it's an unknown long option, *or* if
285 * -? is a valid short option. */
287 if (optopt || strncmp(argv[optind-1], "--", 2) == 0) {
288 parse_fail(errlog, optopt, argv[optind-1]+2,
289 "unrecognized option");
292 } else if (ret == ':') {
293 /* Missing argument: longidx not updated :( */
294 parse_fail(errlog, optopt, argv[optind-1]+2,
295 "option requires an argument");
302 e = find_long(longidx, &name);
304 if (e->flags == OPT_HASARG)
305 problem = e->cb_arg(optarg, e->arg);
307 problem = e->cb(e->arg);
310 parse_fail(errlog, ret, name, problem);
320 /* We hide everything but remaining arguments. */
321 memmove(&argv[1], &argv[optind], sizeof(argv[1]) * (*argc-optind+1));
324 return ret == -1 ? true : false;
327 void opt_log_stderr(const char *fmt, ...)
332 vfprintf(stderr, fmt, ap);
336 char *opt_invalid_argument(const char *arg)
338 char *str = malloc(sizeof("Invalid argument '%s'") + strlen(arg));
339 sprintf(str, "Invalid argument '%s'", arg);