Move the usage and command name lookup into functions.
This commit is contained in:
parent
29740f4ce1
commit
ccac2ba7ec
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=313190
@ -45,13 +45,8 @@ __FBSDID("$FreeBSD$");
|
|||||||
|
|
||||||
#include "nvmecontrol.h"
|
#include "nvmecontrol.h"
|
||||||
|
|
||||||
typedef void (*nvme_fn_t)(int argc, char *argv[]);
|
|
||||||
|
|
||||||
static struct nvme_function {
|
static struct nvme_function funcs[] = {
|
||||||
const char *name;
|
|
||||||
nvme_fn_t fn;
|
|
||||||
const char *usage;
|
|
||||||
} funcs[] = {
|
|
||||||
{"devlist", devlist, DEVLIST_USAGE},
|
{"devlist", devlist, DEVLIST_USAGE},
|
||||||
{"identify", identify, IDENTIFY_USAGE},
|
{"identify", identify, IDENTIFY_USAGE},
|
||||||
{"perftest", perftest, PERFTEST_USAGE},
|
{"perftest", perftest, PERFTEST_USAGE},
|
||||||
@ -62,12 +57,10 @@ static struct nvme_function {
|
|||||||
{NULL, NULL, NULL},
|
{NULL, NULL, NULL},
|
||||||
};
|
};
|
||||||
|
|
||||||
static void
|
void
|
||||||
usage(void)
|
gen_usage(struct nvme_function *f)
|
||||||
{
|
{
|
||||||
struct nvme_function *f;
|
|
||||||
|
|
||||||
f = funcs;
|
|
||||||
fprintf(stderr, "usage:\n");
|
fprintf(stderr, "usage:\n");
|
||||||
while (f->name != NULL) {
|
while (f->name != NULL) {
|
||||||
fprintf(stderr, "%s", f->usage);
|
fprintf(stderr, "%s", f->usage);
|
||||||
@ -76,6 +69,21 @@ usage(void)
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
dispatch(int argc, char *argv[], struct nvme_function *tbl)
|
||||||
|
{
|
||||||
|
struct nvme_function *f = tbl;
|
||||||
|
|
||||||
|
while (f->name != NULL) {
|
||||||
|
if (strcmp(argv[1], f->name) == 0)
|
||||||
|
f->fn(argc-1, &argv[1]);
|
||||||
|
f++;
|
||||||
|
}
|
||||||
|
|
||||||
|
fprintf(stderr, "Unknown command: %s\n", argv[1]);
|
||||||
|
gen_usage(tbl);
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
print_bytes(void *data, uint32_t length)
|
print_bytes(void *data, uint32_t length)
|
||||||
{
|
{
|
||||||
@ -217,19 +225,11 @@ parse_ns_str(const char *ns_str, char *ctrlr_str, int *nsid)
|
|||||||
int
|
int
|
||||||
main(int argc, char *argv[])
|
main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
struct nvme_function *f;
|
|
||||||
|
|
||||||
if (argc < 2)
|
if (argc < 2)
|
||||||
usage();
|
gen_usage(funcs);
|
||||||
|
|
||||||
f = funcs;
|
dispatch(argc, argv, funcs);
|
||||||
while (f->name != NULL) {
|
|
||||||
if (strcmp(argv[1], f->name) == 0)
|
|
||||||
f->fn(argc-1, &argv[1]);
|
|
||||||
f++;
|
|
||||||
}
|
|
||||||
|
|
||||||
usage();
|
|
||||||
|
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
@ -31,6 +31,14 @@
|
|||||||
|
|
||||||
#include <dev/nvme/nvme.h>
|
#include <dev/nvme/nvme.h>
|
||||||
|
|
||||||
|
typedef void (*nvme_fn_t)(int argc, char *argv[]);
|
||||||
|
|
||||||
|
struct nvme_function {
|
||||||
|
const char *name;
|
||||||
|
nvme_fn_t fn;
|
||||||
|
const char *usage;
|
||||||
|
};
|
||||||
|
|
||||||
#define NVME_CTRLR_PREFIX "nvme"
|
#define NVME_CTRLR_PREFIX "nvme"
|
||||||
#define NVME_NS_PREFIX "ns"
|
#define NVME_NS_PREFIX "ns"
|
||||||
|
|
||||||
@ -73,6 +81,8 @@ void read_namespace_data(int fd, int nsid, struct nvme_namespace_data *nsdata);
|
|||||||
void print_hex(void *data, uint32_t length);
|
void print_hex(void *data, uint32_t length);
|
||||||
void read_logpage(int fd, uint8_t log_page, int nsid, void *payload,
|
void read_logpage(int fd, uint8_t log_page, int nsid, void *payload,
|
||||||
uint32_t payload_size);
|
uint32_t payload_size);
|
||||||
|
void gen_usage(struct nvme_function *);
|
||||||
|
void dispatch(int argc, char *argv[], struct nvme_function *f);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user