From owner-svn-src-stable@freebsd.org Thu Feb 1 19:37:19 2018 Return-Path: Delivered-To: svn-src-stable@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 51629EE7EFA; Thu, 1 Feb 2018 19:37:19 +0000 (UTC) (envelope-from mav@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id F310E809E7; Thu, 1 Feb 2018 19:37:18 +0000 (UTC) (envelope-from mav@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id D5C46115D6; Thu, 1 Feb 2018 19:37:18 +0000 (UTC) (envelope-from mav@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id w11JbIM4026170; Thu, 1 Feb 2018 19:37:18 GMT (envelope-from mav@FreeBSD.org) Received: (from mav@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w11JbIxA026168; Thu, 1 Feb 2018 19:37:18 GMT (envelope-from mav@FreeBSD.org) Message-Id: <201802011937.w11JbIxA026168@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: mav set sender to mav@FreeBSD.org using -f From: Alexander Motin Date: Thu, 1 Feb 2018 19:37:18 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-11@freebsd.org Subject: svn commit: r328715 - stable/11/sbin/nvmecontrol X-SVN-Group: stable-11 X-SVN-Commit-Author: mav X-SVN-Commit-Paths: stable/11/sbin/nvmecontrol X-SVN-Commit-Revision: 328715 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-stable@freebsd.org X-Mailman-Version: 2.1.25 Precedence: list List-Id: SVN commit messages for all the -stable branches of the src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 01 Feb 2018 19:37:19 -0000 Author: mav Date: Thu Feb 1 19:37:18 2018 New Revision: 328715 URL: https://svnweb.freebsd.org/changeset/base/328715 Log: MFC r313190 (by imp): Move the usage and command name lookup into functions. Modified: stable/11/sbin/nvmecontrol/nvmecontrol.c stable/11/sbin/nvmecontrol/nvmecontrol.h Directory Properties: stable/11/ (props changed) Modified: stable/11/sbin/nvmecontrol/nvmecontrol.c ============================================================================== --- stable/11/sbin/nvmecontrol/nvmecontrol.c Thu Feb 1 19:36:42 2018 (r328714) +++ stable/11/sbin/nvmecontrol/nvmecontrol.c Thu Feb 1 19:37:18 2018 (r328715) @@ -45,13 +45,8 @@ __FBSDID("$FreeBSD$"); #include "nvmecontrol.h" -typedef void (*nvme_fn_t)(int argc, char *argv[]); -static struct nvme_function { - const char *name; - nvme_fn_t fn; - const char *usage; -} funcs[] = { +static struct nvme_function funcs[] = { {"devlist", devlist, DEVLIST_USAGE}, {"identify", identify, IDENTIFY_USAGE}, {"perftest", perftest, PERFTEST_USAGE}, @@ -62,12 +57,10 @@ static struct nvme_function { {NULL, NULL, NULL}, }; -static void -usage(void) +void +gen_usage(struct nvme_function *f) { - struct nvme_function *f; - f = funcs; fprintf(stderr, "usage:\n"); while (f->name != NULL) { fprintf(stderr, "%s", f->usage); @@ -76,6 +69,21 @@ usage(void) 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 print_bytes(void *data, uint32_t length) { @@ -217,19 +225,11 @@ parse_ns_str(const char *ns_str, char *ctrlr_str, int int main(int argc, char *argv[]) { - struct nvme_function *f; if (argc < 2) - usage(); + gen_usage(funcs); - f = funcs; - while (f->name != NULL) { - if (strcmp(argv[1], f->name) == 0) - f->fn(argc-1, &argv[1]); - f++; - } - - usage(); + dispatch(argc, argv, funcs); return (0); } Modified: stable/11/sbin/nvmecontrol/nvmecontrol.h ============================================================================== --- stable/11/sbin/nvmecontrol/nvmecontrol.h Thu Feb 1 19:36:42 2018 (r328714) +++ stable/11/sbin/nvmecontrol/nvmecontrol.h Thu Feb 1 19:37:18 2018 (r328715) @@ -31,6 +31,14 @@ #include +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_NS_PREFIX "ns" @@ -73,6 +81,8 @@ void read_namespace_data(int fd, int nsid, struct nvme void print_hex(void *data, uint32_t length); void read_logpage(int fd, uint8_t log_page, int nsid, void *payload, uint32_t payload_size); +void gen_usage(struct nvme_function *); +void dispatch(int argc, char *argv[], struct nvme_function *f); #endif