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_usage(const struct cmd *f)
56 fprintf(stderr, " %s %-15s - %s\n", getprogname(), f->name, f->descr);
60 gen_usage(const struct cmd *t)
64 fprintf(stderr, "usage:\n");
65 SLIST_FOREACH(walker, &t->subcmd, link) {
72 cmd_dispatch(int argc, char *argv[], const struct cmd *t)
79 if (argv[1] == NULL) {
83 SLIST_FOREACH(walker, &t->subcmd, link) {
84 if (strcmp(argv[1], walker->name) == 0) {
85 walker->fn(walker, argc-1, &argv[1]);
89 fprintf(stderr, "Unknown command: %s\n", argv[1]);
95 arg_suffix(char *buf, size_t len, arg_type at)
101 strlcat(buf, "=<STRING>", len);
104 strlcat(buf, "=<FILE>", len);
107 strlcat(buf, "=<NUM>", len);
113 arg_help(int argc __unused, char * const *argv, const struct cmd *f)
117 const struct opts *opts = f->opts;
118 const struct args *args = f->args;
120 // XXX walk up the cmd list...
122 fprintf(stderr, "Unknown argument: %s\n", argv[optind]);
123 fprintf(stderr, "Usage:\n %s %s", getprogname(), argv[0]);
125 fprintf(stderr, " <args>");
127 while (args->descr != NULL) {
128 fprintf(stderr, " %s", args->descr);
132 fprintf(stderr, "\n\n%s\n", f->descr);
134 fprintf(stderr, "Options:\n");
135 for (i = 0; opts[i].long_arg != NULL; i++) {
137 if (isprint(opts[i].short_arg)) {
138 snprintf(buf, sizeof(buf), " -%c, ", opts[i].short_arg);
140 strlcpy(buf, " ", sizeof(buf));
142 strlcat(buf, "--", sizeof(buf));
143 strlcat(buf, opts[i].long_arg, sizeof(buf));
144 arg_suffix(buf, sizeof(buf), opts[i].at);
145 fprintf(stderr, "%-30.30s - %s\n", buf, opts[i].descr);
152 find_long(struct option *lopts, int ch)
156 for (i = 0; lopts[i].val != ch && lopts[i].name != NULL; i++)
162 arg_parse(int argc, char * const * argv, const struct cmd *f)
166 struct option *lopts;
168 const struct opts *opts = f->opts;
169 const struct args *args = f->args;
174 for (n = 0; opts[n].long_arg != NULL;)
176 lopts = malloc((n + 2) * sizeof(struct option));
178 err(1, "option memory");
179 p = shortopts = malloc((n + 3) * sizeof(char));
180 if (shortopts == NULL)
181 err(1, "shortopts memory");
183 for (i = 0; i < n; i++) {
184 lopts[i].name = opts[i].long_arg;
185 lopts[i].has_arg = opts[i].at == arg_none ? no_argument : required_argument;
186 lopts[i].flag = NULL;
187 lopts[i].val = opts[i].short_arg;
188 if (isprint(opts[i].short_arg)) {
189 *p++ = opts[i].short_arg;
190 if (lopts[i].has_arg)
194 lopts[n].name = "help";
195 lopts[n].has_arg = no_argument;
196 lopts[n].flag = NULL;
200 memset(lopts + n + 1, 0, sizeof(struct option));
201 while ((ch = getopt_long(argc, argv, shortopts, lopts, &idx)) != -1) {
203 * If ch != 0, we've found a short option, and we have to
204 * look it up lopts table. Otherwise idx is valid.
207 idx = find_long(lopts, ch);
209 arg_help(argc, argv, f);
210 switch (opts[idx].at) {
212 *(bool *)opts[idx].ptr = true;
216 *(const char **)opts[idx].ptr = optarg;
219 v = strtoul(optarg, NULL, 0);
222 *(uint8_t *)opts[idx].ptr = v;
225 v = strtoul(optarg, NULL, 0);
228 *(uint16_t *)opts[idx].ptr = v;
231 v = strtoul(optarg, NULL, 0);
234 *(uint32_t *)opts[idx].ptr = v;
237 v = strtoul(optarg, NULL, 0);
238 if (v > 0xffffffffffffffffull)
240 *(uint64_t *)opts[idx].ptr = v;
243 if (expand_number(optarg, &v) < 0)
245 *(uint64_t *)opts[idx].ptr = v;
250 while (args->descr) {
251 if (optind >= argc) {
252 fprintf(stderr, "Missing arg %s\n", args->descr);
253 arg_help(argc, argv, f);
258 *(char **)args->ptr = argv[optind++];
266 fprintf(stderr, "Bad value to --%s: %s\n", opts[idx].long_arg, optarg);
273 * Loads all the .so's from the specified directory.
276 cmd_load_dir(const char *dir __unused, cmd_load_cb_t cb __unused, void *argp __unused)
286 for (dent = readdir(d); dent != NULL; dent = readdir(d)) {
287 if (strcmp(".so", dent->d_name + dent->d_namlen - 3) != 0)
289 asprintf(&path, "%s/%s", dir, dent->d_name);
291 err(1, "Can't malloc for path, giving up.");
292 if ((h = dlopen(path, RTLD_NOW | RTLD_GLOBAL)) == NULL)
293 warnx("Can't load %s: %s", path, dlerror());
305 cmd_register(struct cmd *up, struct cmd *cmd)
307 struct cmd *walker, *last;
311 SLIST_INIT(&cmd->subcmd);
314 SLIST_FOREACH(walker, &up->subcmd, link) {
315 if (strcmp(walker->name, cmd->name) > 0)
320 SLIST_INSERT_HEAD(&up->subcmd, cmd, link);
322 SLIST_INSERT_AFTER(last, cmd, link);