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)
46 for (; *i < opt_count; (*i)++) {
47 if (opt_table[*i].flags == OPT_SUBTABLE)
50 return first_name(opt_table[*i].names, len);
51 p = next_name(p, len);
58 static const char *first_lopt(unsigned *i, unsigned *len)
61 for (p = first_opt(i, len); p; p = next_opt(p, i, len)) {
63 /* Skip leading "-" */
72 static const char *next_lopt(const char *p, unsigned *i, unsigned *len)
74 for (p = next_opt(p, i, len); p; p = next_opt(p, i, len)) {
76 /* Skip leading "-" */
85 const char *first_sopt(unsigned *i)
90 for (p = first_opt(i, &len); p; p = next_opt(p, i, &len)) {
97 const char *next_sopt(const char *p, unsigned *i)
100 for (p = next_opt(p, i, &len); p; p = next_opt(p, i, &len)) {
107 static void check_opt(const struct opt_table *entry)
112 assert(entry->flags == OPT_HASARG || entry->flags == OPT_NOARG);
114 assert(entry->names[0] == '-');
115 for (p = first_name(entry->names, &len); p; p = next_name(p, &len)) {
123 if (entry->flags == OPT_HASARG) {
125 /* FIXME: -? with ops breaks getopt_long */
132 static void add_opt(const struct opt_table *entry)
134 opt_table = realloc(opt_table, sizeof(opt_table[0]) * (opt_count+1));
135 opt_table[opt_count++] = *entry;
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)
144 struct opt_table opt;
156 void opt_register_table(const struct opt_table entry[], const char *desc)
158 unsigned int i, start = opt_count;
161 struct opt_table heading = OPT_SUBTABLE(NULL, desc);
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]),
169 check_opt(&entry[i]);
173 /* We store the table length in arg ptr. */
175 opt_table[start].arg = (void *)(intptr_t)(opt_count - start);
178 static char *make_optstring(void)
180 char *str = malloc(1 + opt_num_short + opt_num_short_arg + 1);
182 unsigned int i, num = 0;
184 /* This tells getopt_long we want a ':' returned for missing arg. */
186 for (p = first_sopt(&i); p; p = next_sopt(p, &i)) {
188 if (opt_table[i].flags == OPT_HASARG)
192 assert(num == 1 + opt_num_short + opt_num_short_arg + 1);
196 static struct option *make_options(void)
198 struct option *options = malloc(sizeof(*options) * (opt_num_long + 1));
199 unsigned int i, num = 0, len;
202 for (p = first_lopt(&i, &len); p; p = next_lopt(p, &i, &len)) {
203 char *buf = malloc(len + 1);
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;
212 memset(&options[num], 0, sizeof(options[num]));
213 assert(num == opt_num_long);
217 static struct opt_table *find_short(char shortopt)
222 for (p = first_sopt(&i); p; p = next_sopt(p, &i)) {
224 return &opt_table[i];
229 /* We want the index'th long entry. */
230 static struct opt_table *find_long(int index, const char **name)
235 for (p = first_lopt(&i, &len); p; p = next_lopt(p, &i, &len)) {
238 return &opt_table[i];
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'
252 static void parse_fail(void (*errlog)(const char *fmt, ...),
253 char shortopt, const char *longopt, const char *problem)
256 errlog("%s: -%c: %s", opt_argv0, shortopt, problem);
258 errlog("%s: --%.*s: %s", opt_argv0,
259 strcspn(longopt, "/"), longopt, problem);
262 void dump_optstate(void);
263 void dump_optstate(void)
265 printf("opterr = %i, optind = %i, optopt = %i, optarg = %s\n",
266 opterr, optind, optopt, optarg);
269 /* Parse your arguments. */
270 bool opt_parse(int *argc, char *argv[], void (*errlog)(const char *fmt, ...))
272 char *optstring = make_optstring();
273 struct option *options = make_options();
274 int ret, longidx = 0;
277 /* We will do our own error reporting. */
281 /* Reset in case we're called more than once. */
284 while ((ret = getopt_long(*argc, argv, optstring, options, &longidx))
289 /* optopt is 0 if it's an unknown long option, *or* if
290 * -? is a valid short option. */
292 if (optopt || strncmp(argv[optind-1], "--", 2) == 0) {
293 parse_fail(errlog, optopt, argv[optind-1]+2,
294 "unrecognized option");
297 } else if (ret == ':') {
298 /* Missing argument: longidx not updated :( */
299 parse_fail(errlog, optopt, argv[optind-1]+2,
300 "option requires an argument");
307 e = find_long(longidx, &name);
309 if (e->flags == OPT_HASARG)
310 problem = e->cb_arg(optarg, e->arg);
312 problem = e->cb(e->arg);
315 parse_fail(errlog, ret, name, problem);
325 /* We hide everything but remaining arguments. */
326 memmove(&argv[1], &argv[optind], sizeof(argv[1]) * (*argc-optind+1));
329 return ret == -1 ? true : false;
332 void opt_log_stderr(const char *fmt, ...)
337 vfprintf(stderr, fmt, ap);
341 char *opt_invalid_argument(const char *arg)
343 char *str = malloc(sizeof("Invalid argument '%s'") + strlen(arg));
344 sprintf(str, "Invalid argument '%s'", arg);