Author: jimharris Date: Wed Jun 26 23:11:20 2013 New Revision: 252269 URL: http://svnweb.freebsd.org/changeset/base/252269
Log: 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 Modified: head/sbin/nvmecontrol/nvmecontrol.c head/sbin/nvmecontrol/nvmecontrol.h Modified: head/sbin/nvmecontrol/nvmecontrol.c ============================================================================== --- head/sbin/nvmecontrol/nvmecontrol.c Wed Jun 26 23:05:48 2013 (r252268) +++ head/sbin/nvmecontrol/nvmecontrol.c Wed Jun 26 23:11:20 2013 (r252269) @@ -44,14 +44,31 @@ __FBSDID("$FreeBSD$"); #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 usage(void) { + struct nvme_function *f; + + f = funcs; fprintf(stderr, "usage:\n"); - fprintf(stderr, DEVLIST_USAGE); - fprintf(stderr, IDENTIFY_USAGE); - fprintf(stderr, RESET_USAGE); - fprintf(stderr, PERFTEST_USAGE); + while (f->name != NULL) { + fprintf(stderr, "%s", f->usage); + f++; + } exit(EX_USAGE); } @@ -136,18 +153,17 @@ open_dev(const char *str, int *fd, int s int main(int argc, char *argv[]) { + struct nvme_function *f; if (argc < 2) usage(); - if (strcmp(argv[1], "devlist") == 0) - devlist(argc-1, &argv[1]); - else if (strcmp(argv[1], "identify") == 0) - identify(argc-1, &argv[1]); - else if (strcmp(argv[1], "perftest") == 0) - perftest(argc-1, &argv[1]); - else if (strcmp(argv[1], "reset") == 0) - reset(argc-1, &argv[1]); + f = funcs; + while (f->name != NULL) { + if (strcmp(argv[1], f->name) == 0) + f->fn(argc-1, &argv[1]); + f++; + } usage(); Modified: head/sbin/nvmecontrol/nvmecontrol.h ============================================================================== --- head/sbin/nvmecontrol/nvmecontrol.h Wed Jun 26 23:05:48 2013 (r252268) +++ head/sbin/nvmecontrol/nvmecontrol.h Wed Jun 26 23:11:20 2013 (r252269) @@ -46,14 +46,14 @@ #define RESET_USAGE \ " 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 identify(int argc, char *argv[]); void perftest(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 _______________________________________________ svn-src-all@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/svn-src-all To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"