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].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 */
127 /* Don't document args unless there are some. */
128 if (entry->flags == OPT_NOARG)
129 assert(p[len] != ' ' && p[len] != '=');
133 static void add_opt(const struct opt_table *entry)
135 opt_table = realloc(opt_table, sizeof(opt_table[0]) * (opt_count+1));
136 opt_table[opt_count++] = *entry;
139 void _opt_register(const char *names, enum opt_flags flags,
140 char *(*cb)(void *arg),
141 char *(*cb_arg)(const char *optarg, void *arg),
142 void (*show)(char buf[OPT_SHOW_LEN], const void *arg),
143 void *arg, const char *desc)
145 struct opt_table opt;
157 void opt_register_table(const struct opt_table entry[], const char *desc)
159 unsigned int i, start = opt_count;
162 struct opt_table heading = OPT_SUBTABLE(NULL, desc);
165 for (i = 0; entry[i].flags != OPT_END; i++) {
166 if (entry[i].flags == OPT_SUBTABLE)
167 opt_register_table(subtable_of(&entry[i]),
170 check_opt(&entry[i]);
174 /* We store the table length in arg ptr. */
176 opt_table[start].arg = (void *)(intptr_t)(opt_count - start);
179 static char *make_optstring(void)
181 char *str = malloc(1 + opt_num_short + opt_num_short_arg + 1);
183 unsigned int i, num = 0;
185 /* This tells getopt_long we want a ':' returned for missing arg. */
187 for (p = first_sopt(&i); p; p = next_sopt(p, &i)) {
189 if (opt_table[i].flags == OPT_HASARG)
193 assert(num == 1 + opt_num_short + opt_num_short_arg + 1);
197 static struct option *make_options(void)
199 struct option *options = malloc(sizeof(*options) * (opt_num_long + 1));
200 unsigned int i, num = 0, len;
203 for (p = first_lopt(&i, &len); p; p = next_lopt(p, &i, &len)) {
204 char *buf = malloc(len + 1);
207 options[num].name = buf;
208 options[num].has_arg = (opt_table[i].flags == OPT_HASARG);
209 options[num].flag = NULL;
210 options[num].val = 0;
213 memset(&options[num], 0, sizeof(options[num]));
214 assert(num == opt_num_long);
218 static struct opt_table *find_short(char shortopt)
223 for (p = first_sopt(&i); p; p = next_sopt(p, &i)) {
225 return &opt_table[i];
230 /* We want the index'th long entry. */
231 static struct opt_table *find_long(int index, const char **name)
236 for (p = first_lopt(&i, &len); p; p = next_lopt(p, &i, &len)) {
239 return &opt_table[i];
246 /* glibc does this as:
247 /tmp/opt-example: invalid option -- 'x'
248 /tmp/opt-example: unrecognized option '--long'
249 /tmp/opt-example: option '--someflag' doesn't allow an argument
250 /tmp/opt-example: option '--s' is ambiguous
251 /tmp/opt-example: option requires an argument -- 's'
253 static void parse_fail(void (*errlog)(const char *fmt, ...),
254 char shortopt, const char *longopt, const char *problem)
257 errlog("%s: -%c: %s", opt_argv0, shortopt, problem);
259 errlog("%s: --%.*s: %s", opt_argv0,
260 strcspn(longopt, "/"), longopt, problem);
263 /* Parse your arguments. */
264 bool opt_parse(int *argc, char *argv[], void (*errlog)(const char *fmt, ...))
266 char *optstring = make_optstring();
267 struct option *options = make_options();
268 int ret, longidx = 0;
271 /* We will do our own error reporting. */
275 /* Reset in case we're called more than once. */
278 while ((ret = getopt_long(*argc, argv, optstring, options, &longidx))
283 /* optopt is 0 if it's an unknown long option, *or* if
284 * -? is a valid short option. */
286 if (optopt || strncmp(argv[optind-1], "--", 2) == 0) {
287 parse_fail(errlog, optopt, argv[optind-1]+2,
288 "unrecognized option");
291 } else if (ret == ':') {
292 /* Missing argument: longidx not updated :( */
293 parse_fail(errlog, optopt, argv[optind-1]+2,
294 "option requires an argument");
301 e = find_long(longidx, &name);
303 if (e->flags == OPT_HASARG)
304 problem = e->cb_arg(optarg, e->arg);
306 problem = e->cb(e->arg);
309 parse_fail(errlog, ret, name, problem);
319 /* We hide everything but remaining arguments. */
320 memmove(&argv[1], &argv[optind], sizeof(argv[1]) * (*argc-optind+1));
323 return ret == -1 ? true : false;
326 void opt_log_stderr(const char *fmt, ...)
331 vfprintf(stderr, fmt, ap);
335 char *opt_invalid_argument(const char *arg)
337 char *str = malloc(sizeof("Invalid argument '%s'") + strlen(arg));
338 sprintf(str, "Invalid argument '%s'", arg);