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