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