Add an nvme_function structure array, defining the name, C function and

usage message for each nvmecontrol command.  This helps reduce some code
clutter both now and for future commits which will add logpage and
firmware support to nvmecontrol(8).

Also move helper function prototypes to the end of the header file, after
the per-command functions.

Sponsored by:	Intel
MFC after:	3 days
This commit is contained in:
Jim Harris 2013-06-26 23:11:20 +00:00
parent 28c091109b
commit 7cdb43c490
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=252269
2 changed files with 32 additions and 16 deletions

View File

@ -44,14 +44,31 @@ __FBSDID("$FreeBSD$");
#include "nvmecontrol.h" #include "nvmecontrol.h"
typedef void (*nvme_fn_t)(int argc, char *argv[]);
struct nvme_function {
const char *name;
nvme_fn_t fn;
const char *usage;
} funcs[] = {
{"devlist", devlist, DEVLIST_USAGE},
{"identify", identify, IDENTIFY_USAGE},
{"perftest", perftest, PERFTEST_USAGE},
{"reset", reset, RESET_USAGE},
{NULL, NULL, NULL},
};
static void static void
usage(void) usage(void)
{ {
struct nvme_function *f;
f = funcs;
fprintf(stderr, "usage:\n"); fprintf(stderr, "usage:\n");
fprintf(stderr, DEVLIST_USAGE); while (f->name != NULL) {
fprintf(stderr, IDENTIFY_USAGE); fprintf(stderr, "%s", f->usage);
fprintf(stderr, RESET_USAGE); f++;
fprintf(stderr, PERFTEST_USAGE); }
exit(EX_USAGE); exit(EX_USAGE);
} }
@ -136,18 +153,17 @@ open_dev(const char *str, int *fd, int show_error, int exit_on_error)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
struct nvme_function *f;
if (argc < 2) if (argc < 2)
usage(); usage();
if (strcmp(argv[1], "devlist") == 0) f = funcs;
devlist(argc-1, &argv[1]); while (f->name != NULL) {
else if (strcmp(argv[1], "identify") == 0) if (strcmp(argv[1], f->name) == 0)
identify(argc-1, &argv[1]); f->fn(argc-1, &argv[1]);
else if (strcmp(argv[1], "perftest") == 0) f++;
perftest(argc-1, &argv[1]); }
else if (strcmp(argv[1], "reset") == 0)
reset(argc-1, &argv[1]);
usage(); usage();

View File

@ -46,14 +46,14 @@
#define RESET_USAGE \ #define RESET_USAGE \
" nvmecontrol reset <controller id>\n" " nvmecontrol reset <controller id>\n"
int open_dev(const char *str, int *fd, int show_error, int exit_on_error);
void read_controller_data(int fd, struct nvme_controller_data *cdata);
void read_namespace_data(int fd, int nsid, struct nvme_namespace_data *nsdata);
void devlist(int argc, char *argv[]); void devlist(int argc, char *argv[]);
void identify(int argc, char *argv[]); void identify(int argc, char *argv[]);
void perftest(int argc, char *argv[]); void perftest(int argc, char *argv[]);
void reset(int argc, char *argv[]); void reset(int argc, char *argv[]);
int open_dev(const char *str, int *fd, int show_error, int exit_on_error);
void read_controller_data(int fd, struct nvme_controller_data *cdata);
void read_namespace_data(int fd, int nsid, struct nvme_namespace_data *nsdata);
#endif #endif