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 /* Returns string after first '-'. */
18 static const char *first_name(const char *names, unsigned *len)
20 *len = strcspn(names + 1, "/= ");
24 static const char *next_name(const char *names, unsigned *len)
27 if (names[0] == ' ' || names[0] == '=' || names[0] == '\0')
29 return first_name(names + 1, len);
32 static const char *first_opt(unsigned *i, unsigned *len)
34 for (*i = 0; *i < opt_count; (*i)++) {
35 if (opt_table[*i].type == OPT_SUBTABLE)
37 return first_name(opt_table[*i].names, len);
42 static const char *next_opt(const char *p, unsigned *i, unsigned *len)
44 for (; *i < opt_count; (*i)++) {
45 if (opt_table[*i].type == OPT_SUBTABLE)
48 return first_name(opt_table[*i].names, len);
49 p = next_name(p, len);
56 static const char *first_lopt(unsigned *i, unsigned *len)
59 for (p = first_opt(i, len); p; p = next_opt(p, i, len)) {
61 /* Skip leading "-" */
70 static const char *next_lopt(const char *p, unsigned *i, unsigned *len)
72 for (p = next_opt(p, i, len); p; p = next_opt(p, i, len)) {
74 /* Skip leading "-" */
83 const char *first_sopt(unsigned *i)
88 for (p = first_opt(i, &len); p; p = next_opt(p, i, &len)) {
95 const char *next_sopt(const char *p, unsigned *i)
98 for (p = next_opt(p, i, &len); p; p = next_opt(p, i, &len)) {
105 static void check_opt(const struct opt_table *entry)
110 if (entry->type != OPT_HASARG && entry->type != OPT_NOARG)
111 errx(1, "Option %s: unknown entry type %u",
112 entry->names, entry->type);
115 errx(1, "Option %s: description cannot be NULL", entry->names);
118 if (entry->names[0] != '-')
119 errx(1, "Option %s: does not begin with '-'", entry->names);
121 for (p = first_name(entry->names, &len); p; p = next_name(p, &len)) {
124 errx(1, "Option %s: invalid long option '--'",
129 errx(1, "Option %s: invalid short option"
130 " '%.*s'", entry->names, len+1, p-1);
132 errx(1, "Option %s: invalid short option '-:'",
135 if (entry->type == OPT_HASARG) {
138 errx(1, "Option %s: '-?' cannot take"
139 " an argument", entry->names);
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);
151 static void add_opt(const struct opt_table *entry)
153 opt_table = realloc(opt_table, sizeof(opt_table[0]) * (opt_count+1));
154 opt_table[opt_count++] = *entry;
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)
163 struct opt_table opt;
175 void opt_register_table(const struct opt_table entry[], const char *desc)
177 unsigned int i, start = opt_count;
180 struct opt_table heading = OPT_SUBTABLE(NULL, desc);
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]),
188 check_opt(&entry[i]);
192 /* We store the table length in arg ptr. */
194 opt_table[start].arg = (void *)(intptr_t)(opt_count - start);
197 static char *make_optstring(void)
199 char *str = malloc(1 + opt_num_short + opt_num_short_arg + 1);
201 unsigned int i, num = 0;
203 /* This tells getopt_long we want a ':' returned for missing arg. */
205 for (p = first_sopt(&i); p; p = next_sopt(p, &i)) {
207 if (opt_table[i].type == OPT_HASARG)
211 assert(num == 1 + opt_num_short + opt_num_short_arg + 1);
215 static struct option *make_options(void)
217 struct option *options = malloc(sizeof(*options) * (opt_num_long + 1));
218 unsigned int i, num = 0, len;
221 for (p = first_lopt(&i, &len); p; p = next_lopt(p, &i, &len)) {
222 char *buf = malloc(len + 1);
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;
231 memset(&options[num], 0, sizeof(options[num]));
232 assert(num == opt_num_long);
236 static struct opt_table *find_short(char shortopt)
241 for (p = first_sopt(&i); p; p = next_sopt(p, &i)) {
243 return &opt_table[i];
248 /* We want the index'th long entry. */
249 static struct opt_table *find_long(int index, const char **name)
254 for (p = first_lopt(&i, &len); p; p = next_lopt(p, &i, &len)) {
257 return &opt_table[i];
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'
271 static void parse_fail(void (*errlog)(const char *fmt, ...),
272 char shortopt, const char *longopt, const char *problem)
275 errlog("%s: -%c: %s", opt_argv0, shortopt, problem);
277 errlog("%s: --%.*s: %s", opt_argv0,
278 strcspn(longopt, "/"), longopt, problem);
281 /* Parse your arguments. */
282 bool opt_parse(int *argc, char *argv[], void (*errlog)(const char *fmt, ...))
284 char *optstring = make_optstring();
285 struct option *options = make_options();
286 int ret, longidx = 0;
289 /* We will do our own error reporting. */
293 /* Reset in case we're called more than once. */
296 while ((ret = getopt_long(*argc, argv, optstring, options, &longidx))
301 /* optopt is 0 if it's an unknown long option, *or* if
302 * -? is a valid short option. */
304 if (optopt || strncmp(argv[optind-1], "--", 2) == 0) {
305 parse_fail(errlog, optopt, argv[optind-1]+2,
306 "unrecognized option");
309 } else if (ret == ':') {
310 /* Missing argument: longidx not updated :( */
311 parse_fail(errlog, optopt, argv[optind-1]+2,
312 "option requires an argument");
319 e = find_long(longidx, &name);
321 if (e->type == OPT_HASARG)
322 problem = e->cb_arg(optarg, e->arg);
324 problem = e->cb(e->arg);
327 parse_fail(errlog, ret, name, problem);
337 /* We hide everything but remaining arguments. */
338 memmove(&argv[1], &argv[optind], sizeof(argv[1]) * (*argc-optind+1));
341 return ret == -1 ? true : false;
344 void opt_log_stderr(const char *fmt, ...)
349 vfprintf(stderr, fmt, ap);
353 char *opt_invalid_argument(const char *arg)
355 char *str = malloc(sizeof("Invalid argument '%s'") + strlen(arg));
356 sprintf(str, "Invalid argument '%s'", arg);