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>
50 static struct cmd top;
53 print_tree(const struct cmd *f)
56 if (f->parent != NULL)
57 print_tree(f->parent);
59 fprintf(stderr, " %s", f->name);
63 print_usage(const struct cmd *f)
66 fprintf(stderr, " %s", getprogname());
67 print_tree(f->parent);
68 fprintf(stderr, " %-15s - %s\n", f->name, f->descr);
72 gen_usage(const struct cmd *t)
76 fprintf(stderr, "usage:\n");
77 SLIST_FOREACH(walker, &t->subcmd, link) {
84 cmd_dispatch(int argc, char *argv[], const struct cmd *t)
91 if (argv[1] == NULL) {
95 SLIST_FOREACH(walker, &t->subcmd, link) {
96 if (strcmp(argv[1], walker->name) == 0) {
97 walker->fn(walker, argc-1, &argv[1]);
101 fprintf(stderr, "Unknown command: %s\n", argv[1]);
107 arg_suffix(char *buf, size_t len, arg_type at)
113 strlcat(buf, "=<STRING>", len);
116 strlcat(buf, "=<FILE>", len);
119 strlcat(buf, "=<NUM>", len);
125 arg_help(int argc __unused, char * const *argv, const struct cmd *f)
129 const struct opts *opts = f->opts;
130 const struct args *args = f->args;
132 // XXX walk up the cmd list...
134 fprintf(stderr, "Unknown argument: %s\n", argv[optind]);
135 fprintf(stderr, "Usage:\n %s", getprogname());
138 fprintf(stderr, " <args>");
140 while (args->descr != NULL) {
141 fprintf(stderr, " %s", args->descr);
145 fprintf(stderr, "\n\n%s\n", f->descr);
147 fprintf(stderr, "Options:\n");
148 for (i = 0; opts[i].long_arg != NULL; i++) {
150 if (isprint(opts[i].short_arg)) {
151 snprintf(buf, sizeof(buf), " -%c, ", opts[i].short_arg);
153 strlcpy(buf, " ", sizeof(buf));
155 strlcat(buf, "--", sizeof(buf));
156 strlcat(buf, opts[i].long_arg, sizeof(buf));
157 arg_suffix(buf, sizeof(buf), opts[i].at);
158 fprintf(stderr, "%-30.30s - %s\n", buf, opts[i].descr);
165 find_long(struct option *lopts, int ch)
169 for (i = 0; lopts[i].val != ch && lopts[i].name != NULL; i++)
175 arg_parse(int argc, char * const * argv, const struct cmd *f)
179 struct option *lopts;
181 const struct opts *opts = f->opts;
182 const struct args *args = f->args;
187 for (n = 0; opts[n].long_arg != NULL;)
189 lopts = malloc((n + 2) * sizeof(struct option));
191 err(1, "option memory");
192 p = shortopts = malloc((2 * n + 3) * sizeof(char));
193 if (shortopts == NULL)
194 err(1, "shortopts memory");
196 for (i = 0; i < n; i++) {
197 lopts[i].name = opts[i].long_arg;
198 lopts[i].has_arg = opts[i].at == arg_none ? no_argument : required_argument;
199 lopts[i].flag = NULL;
200 lopts[i].val = opts[i].short_arg;
201 if (isprint(opts[i].short_arg)) {
202 *p++ = opts[i].short_arg;
203 if (lopts[i].has_arg)
207 lopts[n].name = "help";
208 lopts[n].has_arg = no_argument;
209 lopts[n].flag = NULL;
213 memset(lopts + n + 1, 0, sizeof(struct option));
214 while ((ch = getopt_long(argc, argv, shortopts, lopts, &idx)) != -1) {
216 * If ch != 0, we've found a short option, and we have to
217 * look it up lopts table. Otherwise idx is valid.
220 idx = find_long(lopts, ch);
222 arg_help(argc, argv, f);
223 switch (opts[idx].at) {
225 *(bool *)opts[idx].ptr = true;
229 *(const char **)opts[idx].ptr = optarg;
232 v = strtoul(optarg, NULL, 0);
235 *(uint8_t *)opts[idx].ptr = v;
238 v = strtoul(optarg, NULL, 0);
241 *(uint16_t *)opts[idx].ptr = v;
244 v = strtoul(optarg, NULL, 0);
247 *(uint32_t *)opts[idx].ptr = v;
250 v = strtoul(optarg, NULL, 0);
251 if (v > 0xffffffffffffffffull)
253 *(uint64_t *)opts[idx].ptr = v;
256 if (expand_number(optarg, &v) < 0)
258 *(uint64_t *)opts[idx].ptr = v;
263 while (args->descr) {
264 if (optind >= argc) {
265 fprintf(stderr, "Missing arg %s\n", args->descr);
266 arg_help(argc, argv, f);
271 *(char **)args->ptr = argv[optind++];
279 fprintf(stderr, "Bad value to --%s: %s\n", opts[idx].long_arg, optarg);
286 * Loads all the .so's from the specified directory.
289 cmd_load_dir(const char *dir __unused, cmd_load_cb_t cb __unused, void *argp __unused)
299 for (dent = readdir(d); dent != NULL; dent = readdir(d)) {
300 if (strcmp(".so", dent->d_name + dent->d_namlen - 3) != 0)
302 asprintf(&path, "%s/%s", dir, dent->d_name);
304 err(1, "Can't malloc for path, giving up.");
305 if ((h = dlopen(path, RTLD_NOW | RTLD_GLOBAL)) == NULL)
306 warnx("Can't load %s: %s", path, dlerror());
318 cmd_register(struct cmd *up, struct cmd *cmd)
320 struct cmd *walker, *last;
324 SLIST_INIT(&cmd->subcmd);
327 SLIST_FOREACH(walker, &up->subcmd, link) {
328 if (strcmp(walker->name, cmd->name) > 0)
333 SLIST_INSERT_HEAD(&up->subcmd, cmd, link);
335 SLIST_INSERT_AFTER(last, cmd, link);