2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (C) 2019 Netflix, Inc
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/ioccom.h>
51 static struct cmd top;
54 print_tree(const struct cmd *f)
57 if (f->parent != NULL)
58 print_tree(f->parent);
60 fprintf(stderr, " %s", f->name);
64 print_usage(const struct cmd *f)
67 fprintf(stderr, " %s", getprogname());
68 print_tree(f->parent);
69 fprintf(stderr, " %-15s - %s\n", f->name, f->descr);
73 gen_usage(const struct cmd *t)
77 fprintf(stderr, "usage:\n");
78 SLIST_FOREACH(walker, &t->subcmd, link) {
85 cmd_dispatch(int argc, char *argv[], const struct cmd *t)
92 if (argv[1] == NULL) {
96 SLIST_FOREACH(walker, &t->subcmd, link) {
97 if (strcmp(argv[1], walker->name) == 0) {
98 walker->fn(walker, argc-1, &argv[1]);
102 fprintf(stderr, "Unknown command: %s\n", argv[1]);
108 arg_suffix(char *buf, size_t len, arg_type at)
114 strlcat(buf, "=<STRING>", len);
117 strlcat(buf, "=<FILE>", len);
120 strlcat(buf, "=<NUM>", len);
126 arg_help(int argc __unused, char * const *argv, const struct cmd *f)
130 const struct opts *opts = f->opts;
131 const struct args *args = f->args;
133 // XXX walk up the cmd list...
135 fprintf(stderr, "Unknown argument: %s\n", argv[optind]);
136 fprintf(stderr, "Usage:\n %s", getprogname());
139 fprintf(stderr, " <args>");
141 while (args->descr != NULL) {
142 fprintf(stderr, " %s", args->descr);
146 fprintf(stderr, "\n\n%s\n", f->descr);
148 fprintf(stderr, "Options:\n");
149 for (i = 0; opts[i].long_arg != NULL; i++) {
151 if (isprint(opts[i].short_arg)) {
152 snprintf(buf, sizeof(buf), " -%c, ", opts[i].short_arg);
154 strlcpy(buf, " ", sizeof(buf));
156 strlcat(buf, "--", sizeof(buf));
157 strlcat(buf, opts[i].long_arg, sizeof(buf));
158 arg_suffix(buf, sizeof(buf), opts[i].at);
159 fprintf(stderr, "%-30.30s - %s\n", buf, opts[i].descr);
166 find_long(struct option *lopts, int ch)
170 for (i = 0; lopts[i].val != ch && lopts[i].name != NULL; i++)
176 arg_parse(int argc, char * const * argv, const struct cmd *f)
180 struct option *lopts;
182 const struct opts *opts = f->opts;
183 const struct args *args = f->args;
188 for (n = 0; opts[n].long_arg != NULL;)
190 lopts = malloc((n + 2) * sizeof(struct option));
192 err(EX_OSERR, "option memory");
193 p = shortopts = malloc((2 * n + 3) * sizeof(char));
194 if (shortopts == NULL)
195 err(EX_OSERR, "shortopts memory");
197 for (i = 0; i < n; i++) {
198 lopts[i].name = opts[i].long_arg;
199 lopts[i].has_arg = opts[i].at == arg_none ? no_argument : required_argument;
200 lopts[i].flag = NULL;
201 lopts[i].val = opts[i].short_arg;
202 if (isprint(opts[i].short_arg)) {
203 *p++ = opts[i].short_arg;
204 if (lopts[i].has_arg)
208 lopts[n].name = "help";
209 lopts[n].has_arg = no_argument;
210 lopts[n].flag = NULL;
214 memset(lopts + n + 1, 0, sizeof(struct option));
215 while ((ch = getopt_long(argc, argv, shortopts, lopts, &idx)) != -1) {
217 * If ch != 0, we've found a short option, and we have to
218 * look it up lopts table. Otherwise idx is valid.
221 idx = find_long(lopts, ch);
223 arg_help(argc, argv, f);
224 switch (opts[idx].at) {
226 *(bool *)opts[idx].ptr = true;
230 *(const char **)opts[idx].ptr = optarg;
233 v = strtoul(optarg, NULL, 0);
236 *(uint8_t *)opts[idx].ptr = v;
239 v = strtoul(optarg, NULL, 0);
242 *(uint16_t *)opts[idx].ptr = v;
245 v = strtoul(optarg, NULL, 0);
248 *(uint32_t *)opts[idx].ptr = v;
251 v = strtoul(optarg, NULL, 0);
252 if (v > 0xffffffffffffffffull)
254 *(uint64_t *)opts[idx].ptr = v;
257 if (expand_number(optarg, &v) < 0)
259 *(uint64_t *)opts[idx].ptr = v;
264 while (args->descr) {
265 if (optind >= argc) {
266 fprintf(stderr, "Missing arg %s\n", args->descr);
267 arg_help(argc, argv, f);
272 *(char **)args->ptr = argv[optind++];
280 fprintf(stderr, "Bad value to --%s: %s\n", opts[idx].long_arg, optarg);
287 * Loads all the .so's from the specified directory.
290 cmd_load_dir(const char *dir, cmd_load_cb_t cb, void *argp)
300 for (dent = readdir(d); dent != NULL; dent = readdir(d)) {
301 if (strcmp(".so", dent->d_name + dent->d_namlen - 3) != 0)
303 asprintf(&path, "%s/%s", dir, dent->d_name);
305 err(EX_OSERR, "Can't malloc for path, giving up.");
306 if ((h = dlopen(path, RTLD_NOW | RTLD_GLOBAL)) == NULL)
307 warnx("Can't load %s: %s", path, dlerror());
319 cmd_register(struct cmd *up, struct cmd *cmd)
321 struct cmd *walker, *last;
325 SLIST_INIT(&cmd->subcmd);
328 SLIST_FOREACH(walker, &up->subcmd, link) {
329 if (strcmp(walker->name, cmd->name) > 0)
334 SLIST_INSERT_HEAD(&up->subcmd, cmd, link);
336 SLIST_INSERT_AFTER(last, cmd, link);