summaryrefslogtreecommitdiffstats
path: root/freebsd/sbin/nvmecontrol/comnd.c
blob: 3074cfb962730f7ddbf1b4e859a2aa659366c177 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#include <machine/rtems-bsd-user-space.h>

/*-
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
 * Copyright (C) 2019 Netflix, Inc
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/ioccom.h>

#include <ctype.h>
#include <dirent.h>
#include <dlfcn.h>
#include <err.h>
#include <fcntl.h>
#include <getopt.h>
#include <libutil.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "comnd.h"

static struct cmd top;

static void
print_tree(const struct cmd *f)
{

	if (f->parent != NULL)
		print_tree(f->parent);
	if (f->name != NULL)
		fprintf(stderr, " %s", f->name);
}

static void
print_usage(const struct cmd *f)
{

	fprintf(stderr, "    %s", getprogname());
	print_tree(f->parent);
	fprintf(stderr, " %-15s - %s\n", f->name, f->descr);
}

static void
gen_usage(const struct cmd *t)
{
	struct cmd *walker;

	fprintf(stderr, "usage:\n");
	SLIST_FOREACH(walker, &t->subcmd, link) {
		print_usage(walker);
	}
	exit(1);
}

int
cmd_dispatch(int argc, char *argv[], const struct cmd *t)
{
	struct cmd *walker;

	if (t == NULL)
		t = &top;

	if (argv[1] == NULL) {
		gen_usage(t);
		return (1);
	}
	SLIST_FOREACH(walker, &t->subcmd, link) {
		if (strcmp(argv[1], walker->name) == 0) {
			walker->fn(walker, argc-1, &argv[1]);
			return (0);
		}
	}
	fprintf(stderr, "Unknown command: %s\n", argv[1]);
	gen_usage(t);
	return (1);
}

static void
arg_suffix(char *buf, size_t len, arg_type at)
{
	switch (at) {
	case arg_none:
		break;
	case arg_string:
		strlcat(buf, "=<STRING>", len);
		break;
	case arg_path:
		strlcat(buf, "=<FILE>", len);
		break;
	default:
		strlcat(buf, "=<NUM>", len);
		break;
	}
}

void
arg_help(int argc __unused, char * const *argv, const struct cmd *f)
{
	int i;
	char buf[31];
	const struct opts *opts = f->opts;
	const struct args *args = f->args;

	// XXX walk up the cmd list...
	if (argv[optind])
		fprintf(stderr, "Unknown argument: %s\n", argv[optind]);
	fprintf(stderr, "Usage:\n    %s", getprogname());
	print_tree(f);
	if (opts)
		fprintf(stderr, " <args>");
	if (args) {
		while (args->descr != NULL) {
			fprintf(stderr, " %s", args->descr);
			args++;
		}
	}
	fprintf(stderr, "\n\n%s\n", f->descr);
	if (opts != NULL) {
		fprintf(stderr, "Options:\n");
		for (i = 0; opts[i].long_arg != NULL; i++) {
			*buf = '\0';
			if (isprint(opts[i].short_arg)) {
				snprintf(buf, sizeof(buf), " -%c, ", opts[i].short_arg);
			} else {
				strlcpy(buf, "    ", sizeof(buf));
			}
			strlcat(buf, "--", sizeof(buf));
			strlcat(buf, opts[i].long_arg, sizeof(buf));
			arg_suffix(buf, sizeof(buf), opts[i].at);
			fprintf(stderr, "%-30.30s - %s\n", buf, opts[i].descr);
		}
	}
	exit(1);
}

static int
find_long(struct option *lopts, int ch)
{
	int i;

	for (i = 0; lopts[i].val != ch && lopts[i].name != NULL; i++)
		continue;
	return (i);
}

int
arg_parse(int argc, char * const * argv, const struct cmd *f)
{
	int i, n, idx, ch;
	uint64_t v;
	struct option *lopts;
	char *shortopts, *p;
	const struct opts *opts = f->opts;
	const struct args *args = f->args;

	if (opts == NULL)
		n = 0;
	else
		for (n = 0; opts[n].long_arg != NULL;)
			n++;
	lopts = malloc((n + 2) * sizeof(struct option));
	if (lopts == NULL)
		err(1, "option memory");
	p = shortopts = malloc((n + 3) * sizeof(char));
	if (shortopts == NULL)
		err(1, "shortopts memory");
	idx = 0;
	for (i = 0; i < n; i++) {
		lopts[i].name = opts[i].long_arg;
		lopts[i].has_arg = opts[i].at == arg_none ? no_argument : required_argument;
		lopts[i].flag = NULL;
		lopts[i].val = opts[i].short_arg;
		if (isprint(opts[i].short_arg)) {
			*p++ = opts[i].short_arg;
			if (lopts[i].has_arg)
				*p++ = ':';
		}
	}
	lopts[n].name = "help";
	lopts[n].has_arg = no_argument;
	lopts[n].flag = NULL;
	lopts[n].val = '?';
	*p++ = '?';
	*p++ = '\0';
	memset(lopts + n + 1, 0, sizeof(struct option));
	while ((ch = getopt_long(argc, argv, shortopts, lopts, &idx)) != -1) {
		/*
		 * If ch != 0, we've found a short option, and we have to
		 * look it up lopts table. Otherwise idx is valid.
		 */
		if (ch != 0)
			idx = find_long(lopts, ch);
		if (idx == n)
			arg_help(argc, argv, f);
		switch (opts[idx].at) {
		case arg_none:
			*(bool *)opts[idx].ptr = true;
			break;
		case arg_string:
		case arg_path:
			*(const char **)opts[idx].ptr = optarg;
			break;
		case arg_uint8:
			v = strtoul(optarg, NULL, 0);
			if (v > 0xff)
				goto bad_arg;
			*(uint8_t *)opts[idx].ptr = v;
			break;
		case arg_uint16:
			v = strtoul(optarg, NULL, 0);
			if (v > 0xffff)
				goto bad_arg;
			*(uint16_t *)opts[idx].ptr = v;
			break;
		case arg_uint32:
			v = strtoul(optarg, NULL, 0);
			if (v > 0xffffffffu)
				goto bad_arg;
			*(uint32_t *)opts[idx].ptr = v;
			break;
		case arg_uint64:
			v = strtoul(optarg, NULL, 0);
			if (v > 0xffffffffffffffffull)
				goto bad_arg;
			*(uint64_t *)opts[idx].ptr = v;
			break;
		case arg_size:
			if (expand_number(optarg, &v) < 0)
				goto bad_arg;
			*(uint64_t *)opts[idx].ptr = v;
			break;
		}
	}
	if (args) {
		while (args->descr) {
			if (optind >= argc) {
				fprintf(stderr, "Missing arg %s\n", args->descr);
				arg_help(argc, argv, f);
				free(lopts);
				free(shortopts);
				return (1);
			}
			*(char **)args->ptr = argv[optind++];
			args++;
		}
	}
	free(lopts);
	free(shortopts);
	return (0);
bad_arg:
	fprintf(stderr, "Bad value to --%s: %s\n", opts[idx].long_arg, optarg);
	free(lopts);
	free(shortopts);
	exit(1);
}

/*
 * Loads all the .so's from the specified directory.
 */
void
cmd_load_dir(const char *dir __unused, cmd_load_cb_t cb __unused, void *argp __unused)
{
	DIR *d;
	struct dirent *dent;
	char *path = NULL;
	void *h;

	d = opendir(dir);
	if (d == NULL)
		return;
	for (dent = readdir(d); dent != NULL; dent = readdir(d)) {
		if (strcmp(".so", dent->d_name + dent->d_namlen - 3) != 0)
			continue;
		asprintf(&path, "%s/%s", dir, dent->d_name);
		if (path == NULL)
			err(1, "Can't malloc for path, giving up.");
		if ((h = dlopen(path, RTLD_NOW | RTLD_GLOBAL)) == NULL)
			warnx("Can't load %s: %s", path, dlerror());
		else {
			if (cb != NULL)
				cb(argp, h);
		}
		free(path);
		path = NULL;
	}
	closedir(d);
}

void
cmd_register(struct cmd *up, struct cmd *cmd)
{
	struct cmd *walker, *last;

	if (up == NULL)
		up = &top;
	SLIST_INIT(&cmd->subcmd);
	cmd->parent = up;
	last = NULL;
	SLIST_FOREACH(walker, &up->subcmd, link) {
		if (strcmp(walker->name, cmd->name) > 0)
			break;
		last = walker;
	}
	if (last == NULL) {
		SLIST_INSERT_HEAD(&up->subcmd, cmd, link);
	} else {
		SLIST_INSERT_AFTER(last, cmd, link);
	}
}

void
cmd_init(void)
{

}