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)
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].flags == 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].flags == 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 assert(entry->flags == OPT_HASARG || entry->flags == OPT_NOARG);
112 assert(entry->names[0] == '-');
113 for (p = first_name(entry->names, &len); p; p = next_name(p, &len)) {
121 if (entry->flags == OPT_HASARG) {
123 /* FIXME: -? with ops breaks getopt_long */
130 static void add_opt(const struct opt_table *entry)
132 opt_table = realloc(opt_table, sizeof(opt_table[0]) * (opt_count+1));
133 opt_table[opt_count++] = *entry;
136 void _opt_register(const char *names, enum opt_flags flags,
137 char *(*cb)(void *arg),
138 char *(*cb_arg)(const char *optarg, void *arg),
139 void (*show)(char buf[OPT_SHOW_LEN], const void *arg),
140 void *arg, const char *desc)
142 struct opt_table opt;
154 void opt_register_table(const struct opt_table entry[], const char *desc)
156 unsigned int i, start = opt_count;
159 struct opt_table heading = OPT_SUBTABLE(NULL, desc);
162 for (i = 0; entry[i].flags != OPT_END; i++) {
163 if (entry[i].flags == OPT_SUBTABLE)
164 opt_register_table(subtable_of(&entry[i]),
167 check_opt(&entry[i]);
171 /* We store the table length in arg ptr. */
173 opt_table[start].arg = (void *)(intptr_t)(opt_count - start);
176 static char *make_optstring(void)
178 char *str = malloc(1 + opt_num_short + opt_num_short_arg + 1);
180 unsigned int i, num = 0;
182 /* This tells getopt_long we want a ':' returned for missing arg. */
184 for (p = first_sopt(&i); p; p = next_sopt(p, &i)) {
186 if (opt_table[i].flags == OPT_HASARG)
190 assert(num == 1 + opt_num_short + opt_num_short_arg + 1);
194 static struct option *make_options(void)
196 struct option *options = malloc(sizeof(*options) * (opt_num_long + 1));
197 unsigned int i, num = 0, len;
200 for (p = first_lopt(&i, &len); p; p = next_lopt(p, &i, &len)) {
201 char *buf = malloc(len + 1);
204 options[num].name = buf;
205 options[num].has_arg = (opt_table[i].flags == OPT_HASARG);
206 options[num].flag = NULL;
207 options[num].val = 0;
210 memset(&options[num], 0, sizeof(options[num]));
211 assert(num == opt_num_long);
215 static struct opt_table *find_short(char shortopt)
220 for (p = first_sopt(&i); p; p = next_sopt(p, &i)) {
222 return &opt_table[i];
227 /* We want the index'th long entry. */
228 static struct opt_table *find_long(int index, const char **name)
233 for (p = first_lopt(&i, &len); p; p = next_lopt(p, &i, &len)) {
236 return &opt_table[i];
243 /* glibc does this as:
244 /tmp/opt-example: invalid option -- 'x'
245 /tmp/opt-example: unrecognized option '--long'
246 /tmp/opt-example: option '--someflag' doesn't allow an argument
247 /tmp/opt-example: option '--s' is ambiguous
248 /tmp/opt-example: option requires an argument -- 's'
250 static void parse_fail(void (*errlog)(const char *fmt, ...),
251 char shortopt, const char *longopt, const char *problem)
254 errlog("%s: -%c: %s", opt_argv0, shortopt, problem);
256 errlog("%s: --%.*s: %s", opt_argv0,
257 strcspn(longopt, "/"), longopt, problem);
260 /* Parse your arguments. */
261 bool opt_parse(int *argc, char *argv[], void (*errlog)(const char *fmt, ...))
263 char *optstring = make_optstring();
264 struct option *options = make_options();
265 int ret, longidx = 0;
268 /* We will do our own error reporting. */
272 /* Reset in case we're called more than once. */
275 while ((ret = getopt_long(*argc, argv, optstring, options, &longidx))
280 /* optopt is 0 if it's an unknown long option, *or* if
281 * -? is a valid short option. */
283 if (optopt || strncmp(argv[optind-1], "--", 2) == 0) {
284 parse_fail(errlog, optopt, argv[optind-1]+2,
285 "unrecognized option");
288 } else if (ret == ':') {
289 /* Missing argument: longidx not updated :( */
290 parse_fail(errlog, optopt, argv[optind-1]+2,
291 "option requires an argument");
298 e = find_long(longidx, &name);
300 if (e->flags == OPT_HASARG)
301 problem = e->cb_arg(optarg, e->arg);
303 problem = e->cb(e->arg);
306 parse_fail(errlog, ret, name, problem);
316 /* We hide everything but remaining arguments. */
317 memmove(&argv[1], &argv[optind], sizeof(argv[1]) * (*argc-optind+1));
320 return ret == -1 ? true : false;
323 void opt_log_stderr(const char *fmt, ...)
328 vfprintf(stderr, fmt, ap);
332 char *opt_invalid_argument(const char *arg)
334 char *str = malloc(sizeof("Invalid argument '%s'") + strlen(arg));
335 sprintf(str, "Invalid argument '%s'", arg);