1 /* Licensed under GPLv3+ - see LICENSE file for details */
2 #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].type == 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].type == OPT_SUBTABLE)
48 return first_name(opt_table[*i].names, len);
49 p = next_name(p, len);
56 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 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)
86 unsigned int len = 0 /* GCC bogus warning */;
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 if (entry->type != OPT_HASARG && entry->type != OPT_NOARG
111 && entry->type != (OPT_EARLY|OPT_HASARG)
112 && entry->type != (OPT_EARLY|OPT_NOARG))
113 errx(1, "Option %s: unknown entry type %u",
114 entry->names, entry->type);
117 errx(1, "Option %s: description cannot be NULL", entry->names);
120 if (entry->names[0] != '-')
121 errx(1, "Option %s: does not begin with '-'", entry->names);
123 for (p = first_name(entry->names, &len); p; p = next_name(p, &len)) {
126 errx(1, "Option %s: invalid long option '--'",
131 errx(1, "Option %s: invalid short option"
132 " '%.*s'", entry->names, len+1, p-1);
134 if (entry->type == OPT_HASARG)
137 /* Don't document args unless there are some. */
138 if (entry->type == OPT_NOARG) {
139 if (p[len] == ' ' || p[len] == '=')
140 errx(1, "Option %s: does not take arguments"
141 " '%s'", entry->names, p+len+1);
146 static void add_opt(const struct opt_table *entry)
148 opt_table = realloc(opt_table, sizeof(opt_table[0]) * (opt_count+1));
149 opt_table[opt_count++] = *entry;
152 void _opt_register(const char *names, enum opt_type type,
153 char *(*cb)(void *arg),
154 char *(*cb_arg)(const char *optarg, void *arg),
155 void (*show)(char buf[OPT_SHOW_LEN], const void *arg),
156 const void *arg, const char *desc)
158 struct opt_table opt;
170 void opt_register_table(const struct opt_table entry[], const char *desc)
172 unsigned int i, start = opt_count;
175 struct opt_table heading = OPT_SUBTABLE(NULL, desc);
178 for (i = 0; entry[i].type != OPT_END; i++) {
179 if (entry[i].type == OPT_SUBTABLE)
180 opt_register_table(subtable_of(&entry[i]),
183 check_opt(&entry[i]);
187 /* We store the table length in arg ptr. */
189 opt_table[start].u.tlen = (opt_count - start);
192 /* Parse your arguments. */
193 bool opt_parse(int *argc, char *argv[], void (*errlog)(const char *fmt, ...))
198 /* This helps opt_usage. */
201 while ((ret = parse_one(argc, argv, 0, &offset, errlog)) == 1);
203 /* parse_one returns 0 on finish, -1 on error */
207 bool opt_early_parse(int argc, char *argv[],
208 void (*errlog)(const char *fmt, ...))
212 char **tmpargv = malloc(sizeof(argv[0]) * (argc + 1));
214 /* We could avoid a copy and skip instead, but this is simple. */
215 memcpy(tmpargv, argv, sizeof(argv[0]) * (argc + 1));
217 /* This helps opt_usage. */
220 while ((ret = parse_one(&argc, tmpargv, OPT_EARLY, &off, errlog)) == 1);
224 /* parse_one returns 0 on finish, -1 on error */
228 void opt_free_table(void)
232 opt_count = opt_num_short = opt_num_short_arg = opt_num_long = 0;
235 void opt_log_stderr(const char *fmt, ...)
240 vfprintf(stderr, fmt, ap);
241 fprintf(stderr, "\n");
245 void opt_log_stderr_exit(const char *fmt, ...)
250 vfprintf(stderr, fmt, ap);
251 fprintf(stderr, "\n");
256 char *opt_invalid_argument(const char *arg)
258 char *str = malloc(sizeof("Invalid argument '%s'") + strlen(arg));
259 sprintf(str, "Invalid argument '%s'", arg);