1 /* Licensed under GPLv2+ - see LICENSE file for details */
2 #include <ccan/opt/opt.h>
12 struct opt_table *opt_table;
13 unsigned int opt_count, opt_num_short, opt_num_short_arg, opt_num_long;
14 const char *opt_argv0;
15 struct opt_alloc opt_alloc = {
19 /* Returns string after first '-'. */
20 static const char *first_name(const char *names, unsigned *len)
22 *len = strcspn(names + 1, "|= ");
26 static const char *next_name(const char *names, unsigned *len)
29 if (names[0] == ' ' || names[0] == '=' || names[0] == '\0')
31 return first_name(names + 1, len);
34 static const char *first_opt(unsigned *i, unsigned *len)
36 for (*i = 0; *i < opt_count; (*i)++) {
37 if (opt_table[*i].type == OPT_SUBTABLE)
39 return first_name(opt_table[*i].names, len);
44 static const char *next_opt(const char *p, unsigned *i, unsigned *len)
46 for (; *i < opt_count; (*i)++) {
47 if (opt_table[*i].type == OPT_SUBTABLE)
50 return first_name(opt_table[*i].names, len);
51 p = next_name(p, len);
58 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 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)
88 unsigned int len = 0 /* GCC bogus warning */;
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 /* Avoids dependency on err.h or ccan/err */
109 #define failmsg(fmt, ...) \
110 do { fprintf(stderr, fmt, __VA_ARGS__); exit(1); } while(0)
113 static void check_opt(const struct opt_table *entry)
118 if (entry->type != OPT_HASARG && entry->type != OPT_NOARG
119 && entry->type != (OPT_EARLY|OPT_HASARG)
120 && entry->type != (OPT_EARLY|OPT_NOARG))
121 failmsg("Option %s: unknown entry type %u",
122 entry->names, entry->type);
125 failmsg("Option %s: description cannot be NULL", entry->names);
128 if (entry->names[0] != '-')
129 failmsg("Option %s: does not begin with '-'", entry->names);
131 for (p = first_name(entry->names, &len); p; p = next_name(p, &len)) {
134 failmsg("Option %s: invalid long option '--'",
139 failmsg("Option %s: invalid short option"
140 " '%.*s'", entry->names, len+1, p-1);
142 if (entry->type == OPT_HASARG)
145 /* Don't document args unless there are some. */
146 if (entry->type == OPT_NOARG) {
147 if (p[len] == ' ' || p[len] == '=')
148 failmsg("Option %s: does not take arguments"
149 " '%s'", entry->names, p+len+1);
154 static void add_opt(const struct opt_table *entry)
156 opt_table = opt_alloc.realloc(opt_table,
157 sizeof(opt_table[0]) * (opt_count+1));
158 opt_table[opt_count++] = *entry;
161 void _opt_register(const char *names, enum opt_type type,
162 char *(*cb)(void *arg),
163 char *(*cb_arg)(const char *optarg, void *arg),
164 void (*show)(char buf[OPT_SHOW_LEN], const void *arg),
165 const void *arg, const char *desc)
167 struct opt_table opt;
179 void opt_register_table(const struct opt_table entry[], const char *desc)
181 unsigned int i, start = opt_count;
184 struct opt_table heading = OPT_SUBTABLE(NULL, desc);
187 for (i = 0; entry[i].type != OPT_END; i++) {
188 if (entry[i].type == OPT_SUBTABLE)
189 opt_register_table(subtable_of(&entry[i]),
192 check_opt(&entry[i]);
196 /* We store the table length in arg ptr. */
198 opt_table[start].u.tlen = (opt_count - start);
201 /* Parse your arguments. */
202 bool opt_parse(int *argc, char *argv[], void (*errlog)(const char *fmt, ...))
207 /* This helps opt_usage. */
210 while ((ret = parse_one(argc, argv, 0, &offset, errlog)) == 1);
212 /* parse_one returns 0 on finish, -1 on error */
216 bool opt_early_parse(int argc, char *argv[],
217 void (*errlog)(const char *fmt, ...))
221 char **tmpargv = opt_alloc.alloc(sizeof(argv[0]) * (argc + 1));
223 /* We could avoid a copy and skip instead, but this is simple. */
224 memcpy(tmpargv, argv, sizeof(argv[0]) * (argc + 1));
226 /* This helps opt_usage. */
229 while ((ret = parse_one(&argc, tmpargv, OPT_EARLY, &off, errlog)) == 1);
231 opt_alloc.free(tmpargv);
233 /* parse_one returns 0 on finish, -1 on error */
237 void opt_free_table(void)
239 opt_alloc.free(opt_table);
241 opt_count = opt_num_short = opt_num_short_arg = opt_num_long = 0;
244 void opt_log_stderr(const char *fmt, ...)
249 vfprintf(stderr, fmt, ap);
250 fprintf(stderr, "\n");
254 void opt_log_stderr_exit(const char *fmt, ...)
259 vfprintf(stderr, fmt, ap);
260 fprintf(stderr, "\n");
265 char *opt_invalid_argument(const char *arg)
267 char *str = opt_alloc.alloc(sizeof("Invalid argument '%s'") + strlen(arg));
268 sprintf(str, "Invalid argument '%s'", arg);
272 void opt_set_alloc(void *(*allocfn)(size_t size),
273 void *(*reallocfn)(void *ptr, size_t size),
274 void (*freefn)(void *ptr))
276 opt_alloc.alloc = allocfn;
277 opt_alloc.realloc = reallocfn;
278 opt_alloc.free = freefn;