source: rtems-libbsd/freebsd/sbin/nvmecontrol/comnd.c @ ddc8c35

5
Last change on this file since ddc8c35 was ddc8c35, checked in by Sebastian Huber <sebastian.huber@…>, on 11/13/19 at 11:57:55

NVMECONTROL(8): Import from FreeBSD

Update #3821.

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