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 bool opt_unregister(const char *names)
183 for (i = 0; i < opt_count; i++) {
184 if (opt_table[i].type == OPT_SUBTABLE)
186 if (strcmp(opt_table[i].names, names) == 0)
192 memmove(&opt_table[found], &opt_table[found+1],
193 (opt_count - found) * sizeof(opt_table[found]));
197 void opt_register_table(const struct opt_table entry[], const char *desc)
199 unsigned int i, start = opt_count;
202 struct opt_table heading = OPT_SUBTABLE(NULL, desc);
205 for (i = 0; entry[i].type != OPT_END; i++) {
206 if (entry[i].type == OPT_SUBTABLE)
207 opt_register_table(subtable_of(&entry[i]),
210 check_opt(&entry[i]);
214 /* We store the table length in arg ptr. */
216 opt_table[start].u.tlen = (opt_count - start);
219 /* Parse your arguments. */
220 bool opt_parse(int *argc, char *argv[], void (*errlog)(const char *fmt, ...))
225 /* This helps opt_usage. */
228 while ((ret = parse_one(argc, argv, 0, &offset, errlog, false)) == 1);
230 /* parse_one returns 0 on finish, -1 on error */
234 static bool early_parse(int argc, char *argv[],
235 void (*errlog)(const char *fmt, ...),
240 char **tmpargv = opt_alloc.alloc(sizeof(argv[0]) * (argc + 1));
242 /* We could avoid a copy and skip instead, but this is simple. */
243 memcpy(tmpargv, argv, sizeof(argv[0]) * (argc + 1));
245 /* This helps opt_usage. */
248 while ((ret = parse_one(&argc, tmpargv, OPT_EARLY, &off, errlog, ignore_unknown)) == 1);
250 opt_alloc.free(tmpargv);
252 /* parse_one returns 0 on finish, -1 on error */
256 bool opt_early_parse(int argc, char *argv[],
257 void (*errlog)(const char *fmt, ...))
259 return early_parse(argc, argv, errlog, false);
262 bool opt_early_parse_incomplete(int argc, char *argv[],
263 void (*errlog)(const char *fmt, ...))
265 return early_parse(argc, argv, errlog, true);
268 void opt_free_table(void)
270 opt_alloc.free(opt_table);
272 opt_count = opt_num_short = opt_num_short_arg = opt_num_long = 0;
275 void opt_log_stderr(const char *fmt, ...)
280 vfprintf(stderr, fmt, ap);
281 fprintf(stderr, "\n");
285 void opt_log_stderr_exit(const char *fmt, ...)
290 vfprintf(stderr, fmt, ap);
291 fprintf(stderr, "\n");
296 char *opt_invalid_argument(const char *arg)
298 char *str = opt_alloc.alloc(sizeof("Invalid argument '%s'") + strlen(arg));
299 sprintf(str, "Invalid argument '%s'", arg);
303 void opt_set_alloc(void *(*allocfn)(size_t size),
304 void *(*reallocfn)(void *ptr, size_t size),
305 void (*freefn)(void *ptr))
307 opt_alloc.alloc = allocfn;
308 opt_alloc.realloc = reallocfn;
309 opt_alloc.free = freefn;