1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (C) 2019 Netflix, Inc
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
14 *
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
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 #include <sys/param.h>
30 #include <sys/ioccom.h>
31
32 #include <ctype.h>
33 #include <dirent.h>
34 #include <dlfcn.h>
35 #include <err.h>
36 #include <fcntl.h>
37 #include <getopt.h>
38 #include <libutil.h>
39 #include <stdbool.h>
40 #include <stddef.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <sysexits.h>
45 #include <unistd.h>
46
47 #include "comnd.h"
48
49 static struct cmd top;
50
51 static void
print_tree(const struct cmd * f)52 print_tree(const struct cmd *f)
53 {
54
55 if (f->parent != NULL)
56 print_tree(f->parent);
57 if (f->name != NULL)
58 fprintf(stderr, " %s", f->name);
59 }
60
61 static void
print_usage(const struct cmd * f)62 print_usage(const struct cmd *f)
63 {
64
65 fprintf(stderr, " %s", getprogname());
66 print_tree(f->parent);
67 fprintf(stderr, " %-15s - %s\n", f->name, f->descr);
68 }
69
70 static void
gen_usage(const struct cmd * t)71 gen_usage(const struct cmd *t)
72 {
73 struct cmd *walker;
74
75 fprintf(stderr, "usage:\n");
76 SLIST_FOREACH(walker, &t->subcmd, link) {
77 print_usage(walker);
78 }
79 exit(EX_USAGE);
80 }
81
82 int
cmd_dispatch(int argc,char * argv[],const struct cmd * t)83 cmd_dispatch(int argc, char *argv[], const struct cmd *t)
84 {
85 struct cmd *walker;
86
87 if (t == NULL)
88 t = ⊤
89
90 if (argv[1] == NULL) {
91 gen_usage(t);
92 return (1);
93 }
94 SLIST_FOREACH(walker, &t->subcmd, link) {
95 if (strcmp(argv[1], walker->name) == 0) {
96 walker->fn(walker, argc-1, &argv[1]);
97 return (0);
98 }
99 }
100 fprintf(stderr, "Unknown command: %s\n", argv[1]);
101 gen_usage(t);
102 return (1);
103 }
104
105 static void
arg_suffix(char * buf,size_t len,arg_type at)106 arg_suffix(char *buf, size_t len, arg_type at)
107 {
108 switch (at) {
109 case arg_none:
110 break;
111 case arg_string:
112 strlcat(buf, "=<STRING>", len);
113 break;
114 case arg_path:
115 strlcat(buf, "=<FILE>", len);
116 break;
117 default:
118 strlcat(buf, "=<NUM>", len);
119 break;
120 }
121 }
122
123 void
arg_help(int argc __unused,char * const * argv,const struct cmd * f)124 arg_help(int argc __unused, char * const *argv, const struct cmd *f)
125 {
126 int i;
127 char buf[31];
128 const struct opts *opts = f->opts;
129 const struct args *args = f->args;
130
131 // XXX walk up the cmd list...
132 if (argv[optind])
133 fprintf(stderr, "Unknown argument: %s\n", argv[optind]);
134 fprintf(stderr, "Usage:\n %s", getprogname());
135 print_tree(f);
136 if (opts)
137 fprintf(stderr, " <args>");
138 if (args) {
139 while (args->descr != NULL) {
140 fprintf(stderr, " %s", args->descr);
141 args++;
142 }
143 }
144 fprintf(stderr, "\n\n%s\n", f->descr);
145 if (opts != NULL) {
146 fprintf(stderr, "Options:\n");
147 for (i = 0; opts[i].long_arg != NULL; i++) {
148 *buf = '\0';
149 if (isprint(opts[i].short_arg)) {
150 snprintf(buf, sizeof(buf), " -%c, ", opts[i].short_arg);
151 } else {
152 strlcpy(buf, " ", sizeof(buf));
153 }
154 strlcat(buf, "--", sizeof(buf));
155 strlcat(buf, opts[i].long_arg, sizeof(buf));
156 arg_suffix(buf, sizeof(buf), opts[i].at);
157 fprintf(stderr, "%-30.30s - %s\n", buf, opts[i].descr);
158 }
159 }
160 exit(EX_USAGE);
161 }
162
163 static int
find_long(struct option * lopts,int ch)164 find_long(struct option *lopts, int ch)
165 {
166 int i;
167
168 for (i = 0; lopts[i].val != ch && lopts[i].name != NULL; i++)
169 continue;
170 return (i);
171 }
172
173 int
arg_parse(int argc,char * const * argv,const struct cmd * f)174 arg_parse(int argc, char * const * argv, const struct cmd *f)
175 {
176 int i, n, idx, ch;
177 uint64_t v;
178 struct option *lopts;
179 char *shortopts, *p;
180 const struct opts *opts = f->opts;
181 const struct args *args = f->args;
182
183 if (opts == NULL)
184 n = 0;
185 else
186 for (n = 0; opts[n].long_arg != NULL;)
187 n++;
188 lopts = malloc((n + 2) * sizeof(struct option));
189 if (lopts == NULL)
190 err(EX_OSERR, "option memory");
191 p = shortopts = malloc((2 * n + 3) * sizeof(char));
192 if (shortopts == NULL)
193 err(EX_OSERR, "shortopts memory");
194 idx = 0;
195 for (i = 0; i < n; i++) {
196 lopts[i].name = opts[i].long_arg;
197 lopts[i].has_arg = opts[i].at == arg_none ? no_argument : required_argument;
198 lopts[i].flag = NULL;
199 lopts[i].val = opts[i].short_arg;
200 if (isprint(opts[i].short_arg)) {
201 *p++ = opts[i].short_arg;
202 if (lopts[i].has_arg)
203 *p++ = ':';
204 }
205 }
206 lopts[n].name = "help";
207 lopts[n].has_arg = no_argument;
208 lopts[n].flag = NULL;
209 lopts[n].val = '?';
210 *p++ = '?';
211 *p++ = '\0';
212 memset(lopts + n + 1, 0, sizeof(struct option));
213 while ((ch = getopt_long(argc, argv, shortopts, lopts, &idx)) != -1) {
214 /*
215 * If ch != 0, we've found a short option, and we have to
216 * look it up lopts table. Otherwise idx is valid.
217 */
218 if (ch != 0)
219 idx = find_long(lopts, ch);
220 if (idx == n)
221 arg_help(argc, argv, f);
222 switch (opts[idx].at) {
223 case arg_none:
224 *(bool *)opts[idx].ptr = true;
225 break;
226 case arg_string:
227 case arg_path:
228 *(const char **)opts[idx].ptr = optarg;
229 break;
230 case arg_uint8:
231 v = strtoul(optarg, NULL, 0);
232 if (v > 0xff)
233 goto bad_arg;
234 *(uint8_t *)opts[idx].ptr = v;
235 break;
236 case arg_uint16:
237 v = strtoul(optarg, NULL, 0);
238 if (v > 0xffff)
239 goto bad_arg;
240 *(uint16_t *)opts[idx].ptr = v;
241 break;
242 case arg_uint32:
243 v = strtoul(optarg, NULL, 0);
244 if (v > 0xffffffffu)
245 goto bad_arg;
246 *(uint32_t *)opts[idx].ptr = v;
247 break;
248 case arg_uint64:
249 v = strtoul(optarg, NULL, 0);
250 if (v > 0xffffffffffffffffull)
251 goto bad_arg;
252 *(uint64_t *)opts[idx].ptr = v;
253 break;
254 case arg_size:
255 if (expand_number(optarg, &v) < 0)
256 goto bad_arg;
257 *(uint64_t *)opts[idx].ptr = v;
258 break;
259 }
260 }
261 if (args) {
262 while (args->descr) {
263 if (optind >= argc) {
264 fprintf(stderr, "Missing arg %s\n", args->descr);
265 arg_help(argc, argv, f);
266 free(lopts);
267 free(shortopts);
268 return (1);
269 }
270 *(char **)args->ptr = argv[optind++];
271 args++;
272 }
273 }
274 free(lopts);
275 free(shortopts);
276 return (0);
277 bad_arg:
278 fprintf(stderr, "Bad value to --%s: %s\n", opts[idx].long_arg, optarg);
279 free(lopts);
280 free(shortopts);
281 exit(EX_USAGE);
282 }
283
284 /*
285 * Loads all the .so's from the specified directory.
286 */
287 void
cmd_load_dir(const char * dir,cmd_load_cb_t cb,void * argp)288 cmd_load_dir(const char *dir, cmd_load_cb_t cb, void *argp)
289 {
290 DIR *d;
291 struct dirent *dent;
292 char *path = NULL;
293 void *h;
294
295 d = opendir(dir);
296 if (d == NULL)
297 return;
298 for (dent = readdir(d); dent != NULL; dent = readdir(d)) {
299 if (strcmp(".so", dent->d_name + dent->d_namlen - 3) != 0)
300 continue;
301 asprintf(&path, "%s/%s", dir, dent->d_name);
302 if (path == NULL)
303 err(EX_OSERR, "Can't malloc for path, giving up.");
304 if ((h = dlopen(path, RTLD_NOW | RTLD_GLOBAL)) == NULL)
305 warnx("Can't load %s: %s", path, dlerror());
306 else {
307 if (cb != NULL)
308 cb(argp, h);
309 }
310 free(path);
311 path = NULL;
312 }
313 closedir(d);
314 }
315
316 void
cmd_register(struct cmd * up,struct cmd * cmd)317 cmd_register(struct cmd *up, struct cmd *cmd)
318 {
319 struct cmd *walker, *last;
320
321 if (up == NULL)
322 up = ⊤
323 SLIST_INIT(&cmd->subcmd);
324 cmd->parent = up;
325 last = NULL;
326 SLIST_FOREACH(walker, &up->subcmd, link) {
327 if (strcmp(walker->name, cmd->name) > 0)
328 break;
329 last = walker;
330 }
331 if (last == NULL) {
332 SLIST_INSERT_HEAD(&up->subcmd, cmd, link);
333 } else {
334 SLIST_INSERT_AFTER(last, cmd, link);
335 }
336 }
337
338 void
cmd_init(void)339 cmd_init(void)
340 {
341
342 }
343