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