2 * SPDX-License-Identifier: BSD-2-Clause
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/param.h>
29 #include <sys/ioccom.h>
48 static struct cmd top;
51 print_tree(const struct cmd *f)
54 if (f->parent != NULL)
55 print_tree(f->parent);
57 fprintf(stderr, " %s", f->name);
61 print_usage(const struct cmd *f)
64 fprintf(stderr, " %s", getprogname());
65 print_tree(f->parent);
66 fprintf(stderr, " %-15s - %s\n", f->name, f->descr);
70 gen_usage(const struct cmd *t)
74 fprintf(stderr, "usage:\n");
75 SLIST_FOREACH(walker, &t->subcmd, link) {
82 cmd_dispatch(int argc, char *argv[], const struct cmd *t)
89 if (argv[1] == NULL) {
93 SLIST_FOREACH(walker, &t->subcmd, link) {
94 if (strcmp(argv[1], walker->name) == 0) {
95 walker->fn(walker, argc-1, &argv[1]);
99 fprintf(stderr, "Unknown command: %s\n", argv[1]);
105 arg_suffix(char *buf, size_t len, arg_type at)
111 strlcat(buf, "=<STRING>", len);
114 strlcat(buf, "=<FILE>", len);
117 strlcat(buf, "=<NUM>", len);
123 arg_help(int argc __unused, char * const *argv, const struct cmd *f)
127 const struct opts *opts = f->opts;
128 const struct args *args = f->args;
130 // XXX walk up the cmd list...
132 fprintf(stderr, "Unknown argument: %s\n", argv[optind]);
133 fprintf(stderr, "Usage:\n %s", getprogname());
136 fprintf(stderr, " <args>");
138 while (args->descr != NULL) {
139 fprintf(stderr, " %s", args->descr);
143 fprintf(stderr, "\n\n%s\n", f->descr);
145 fprintf(stderr, "Options:\n");
146 for (i = 0; opts[i].long_arg != NULL; i++) {
148 if (isprint(opts[i].short_arg)) {
149 snprintf(buf, sizeof(buf), " -%c, ", opts[i].short_arg);
151 strlcpy(buf, " ", sizeof(buf));
153 strlcat(buf, "--", sizeof(buf));
154 strlcat(buf, opts[i].long_arg, sizeof(buf));
155 arg_suffix(buf, sizeof(buf), opts[i].at);
156 fprintf(stderr, "%-30.30s - %s\n", buf, opts[i].descr);
163 find_long(struct option *lopts, int ch)
167 for (i = 0; lopts[i].val != ch && lopts[i].name != NULL; i++)
173 arg_parse(int argc, char * const * argv, const struct cmd *f)
177 struct option *lopts;
179 const struct opts *opts = f->opts;
180 const struct args *args = f->args;
185 for (n = 0; opts[n].long_arg != NULL;)
187 lopts = malloc((n + 2) * sizeof(struct option));
189 err(EX_OSERR, "option memory");
190 p = shortopts = malloc((2 * n + 3) * sizeof(char));
191 if (shortopts == NULL)
192 err(EX_OSERR, "shortopts memory");
194 for (i = 0; i < n; i++) {
195 lopts[i].name = opts[i].long_arg;
196 lopts[i].has_arg = opts[i].at == arg_none ? no_argument : required_argument;
197 lopts[i].flag = NULL;
198 lopts[i].val = opts[i].short_arg;
199 if (isprint(opts[i].short_arg)) {
200 *p++ = opts[i].short_arg;
201 if (lopts[i].has_arg)
205 lopts[n].name = "help";
206 lopts[n].has_arg = no_argument;
207 lopts[n].flag = NULL;
211 memset(lopts + n + 1, 0, sizeof(struct option));
212 while ((ch = getopt_long(argc, argv, shortopts, lopts, &idx)) != -1) {
214 * If ch != 0, we've found a short option, and we have to
215 * look it up lopts table. Otherwise idx is valid.
218 idx = find_long(lopts, ch);
220 arg_help(argc, argv, f);
221 switch (opts[idx].at) {
223 *(bool *)opts[idx].ptr = true;
227 *(const char **)opts[idx].ptr = optarg;
230 v = strtoul(optarg, NULL, 0);
233 *(uint8_t *)opts[idx].ptr = v;
236 v = strtoul(optarg, NULL, 0);
239 *(uint16_t *)opts[idx].ptr = v;
242 v = strtoul(optarg, NULL, 0);
245 *(uint32_t *)opts[idx].ptr = v;
248 v = strtoul(optarg, NULL, 0);
249 if (v > 0xffffffffffffffffull)
251 *(uint64_t *)opts[idx].ptr = v;
254 if (expand_number(optarg, &v) < 0)
256 *(uint64_t *)opts[idx].ptr = v;
261 while (args->descr) {
262 if (optind >= argc) {
263 fprintf(stderr, "Missing arg %s\n", args->descr);
264 arg_help(argc, argv, f);
269 *(char **)args->ptr = argv[optind++];
277 fprintf(stderr, "Bad value to --%s: %s\n", opts[idx].long_arg, optarg);
284 * Loads all the .so's from the specified directory.
287 cmd_load_dir(const char *dir, cmd_load_cb_t cb, void *argp)
297 for (dent = readdir(d); dent != NULL; dent = readdir(d)) {
298 if (strcmp(".so", dent->d_name + dent->d_namlen - 3) != 0)
300 asprintf(&path, "%s/%s", dir, dent->d_name);
302 err(EX_OSERR, "Can't malloc for path, giving up.");
303 if ((h = dlopen(path, RTLD_NOW | RTLD_GLOBAL)) == NULL)
304 warnx("Can't load %s: %s", path, dlerror());
316 cmd_register(struct cmd *up, struct cmd *cmd)
318 struct cmd *walker, *last;
322 SLIST_INIT(&cmd->subcmd);
325 SLIST_FOREACH(walker, &up->subcmd, link) {
326 if (strcmp(walker->name, cmd->name) > 0)
331 SLIST_INSERT_HEAD(&up->subcmd, cmd, link);
333 SLIST_INSERT_AFTER(last, cmd, link);