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)
117 enum opt_type type = entry->type & (OPT_USER_MIN-1);
119 if (type != OPT_HASARG && type != OPT_NOARG
120 && type != (OPT_EARLY|OPT_HASARG)
121 && type != (OPT_EARLY|OPT_NOARG))
122 failmsg("Option %s: unknown entry type %u",
123 entry->names, entry->type);
126 failmsg("Option %s: description cannot be NULL", entry->names);
129 if (entry->names[0] != '-')
130 failmsg("Option %s: does not begin with '-'", entry->names);
132 for (p = first_name(entry->names, &len); p; p = next_name(p, &len)) {
135 failmsg("Option %s: invalid long option '--'",
140 failmsg("Option %s: invalid short option"
141 " '%.*s'", entry->names, len+1, p-1);
143 if (entry->type == OPT_HASARG)
146 /* Don't document args unless there are some. */
147 if (entry->type == OPT_NOARG) {
148 if (p[len] == ' ' || p[len] == '=')
149 failmsg("Option %s: does not take arguments"
150 " '%s'", entry->names, p+len+1);
155 static void add_opt(const struct opt_table *entry)
157 opt_table = opt_alloc.realloc(opt_table,
158 sizeof(opt_table[0]) * (opt_count+1));
159 opt_table[opt_count++] = *entry;
162 void _opt_register(const char *names, enum opt_type type,
163 char *(*cb)(void *arg),
164 char *(*cb_arg)(const char *optarg, void *arg),
165 bool (*show)(char *buf, size_t len, const void *arg),
166 const void *arg, const char *desc)
168 struct opt_table opt;
180 bool opt_unregister(const char *names)
184 for (i = 0; i < opt_count; i++) {
185 if (opt_table[i].type & OPT_SUBTABLE)
187 if (strcmp(opt_table[i].names, names) == 0)
193 memmove(&opt_table[found], &opt_table[found+1],
194 (opt_count - found) * sizeof(opt_table[found]));
198 void opt_register_table(const struct opt_table entry[], const char *desc)
200 unsigned int i, start = opt_count;
203 struct opt_table heading = OPT_SUBTABLE(NULL, desc);
206 for (i = 0; entry[i].type != OPT_END; i++) {
207 if (entry[i].type & OPT_SUBTABLE)
208 opt_register_table(subtable_of(&entry[i]),
211 check_opt(&entry[i]);
215 /* We store the table length in arg ptr. */
217 opt_table[start].u.tlen = (opt_count - start);
220 /* Parse your arguments. */
221 bool opt_parse(int *argc, char *argv[], void (*errlog)(const char *fmt, ...))
226 /* This helps opt_usage. */
229 while ((ret = parse_one(argc, argv, 0, &offset, errlog, false)) == 1);
231 /* parse_one returns 0 on finish, -1 on error */
235 static bool early_parse(int argc, char *argv[],
236 void (*errlog)(const char *fmt, ...),
241 char **tmpargv = opt_alloc.alloc(sizeof(argv[0]) * (argc + 1));
243 /* We could avoid a copy and skip instead, but this is simple. */
244 memcpy(tmpargv, argv, sizeof(argv[0]) * (argc + 1));
246 /* This helps opt_usage. */
249 while ((ret = parse_one(&argc, tmpargv, OPT_EARLY, &off, errlog, ignore_unknown)) == 1);
251 opt_alloc.free(tmpargv);
253 /* parse_one returns 0 on finish, -1 on error */
257 bool opt_early_parse(int argc, char *argv[],
258 void (*errlog)(const char *fmt, ...))
260 return early_parse(argc, argv, errlog, false);
263 bool opt_early_parse_incomplete(int argc, char *argv[],
264 void (*errlog)(const char *fmt, ...))
266 return early_parse(argc, argv, errlog, true);
269 void opt_free_table(void)
271 opt_alloc.free(opt_table);
273 opt_count = opt_num_short = opt_num_short_arg = opt_num_long = 0;
276 void opt_log_stderr(const char *fmt, ...)
281 vfprintf(stderr, fmt, ap);
282 fprintf(stderr, "\n");
286 void opt_log_stderr_exit(const char *fmt, ...)
291 vfprintf(stderr, fmt, ap);
292 fprintf(stderr, "\n");
297 char *opt_invalid_argument(const char *arg)
299 char *str = opt_alloc.alloc(sizeof("Invalid argument '%s'") + strlen(arg));
300 sprintf(str, "Invalid argument '%s'", arg);
304 void opt_set_alloc(void *(*allocfn)(size_t size),
305 void *(*reallocfn)(void *ptr, size_t size),
306 void (*freefn)(void *ptr))
308 opt_alloc.alloc = allocfn;
309 opt_alloc.realloc = reallocfn;
310 opt_alloc.free = freefn;