From patchwork Tue Apr 17 06:38:47 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: QI Fuli X-Patchwork-Id: 10344411 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id D838E6039A for ; Tue, 17 Apr 2018 06:40:18 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CBA3F289C5 for ; Tue, 17 Apr 2018 06:40:18 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C9EE1289EE; Tue, 17 Apr 2018 06:40:18 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from ml01.01.org (ml01.01.org [198.145.21.10]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 50346289EC for ; Tue, 17 Apr 2018 06:40:18 +0000 (UTC) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 498632263DD68; Mon, 16 Apr 2018 23:40:18 -0700 (PDT) X-Original-To: linux-nvdimm@lists.01.org Delivered-To: linux-nvdimm@lists.01.org Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=202.219.69.171; helo=mgwkm04.jp.fujitsu.com; envelope-from=qi.fuli@jp.fujitsu.com; receiver=linux-nvdimm@lists.01.org Received: from mgwkm04.jp.fujitsu.com (mgwkm04.jp.fujitsu.com [202.219.69.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 194F02263DD64 for ; Mon, 16 Apr 2018 23:40:15 -0700 (PDT) Received: from kw-mxauth.gw.nic.fujitsu.com (unknown [192.168.231.132]) by mgwkm04.jp.fujitsu.com with smtp id 457d_b73f_54181bda_b690_4662_bed7_13d356c355a9; Tue, 17 Apr 2018 15:40:11 +0900 Received: from m3051.s.css.fujitsu.com (m3051.s.css.fujitsu.com [10.134.21.209]) by kw-mxauth.gw.nic.fujitsu.com (Postfix) with ESMTP id 9DF04AC019E for ; Tue, 17 Apr 2018 15:40:10 +0900 (JST) Received: from qi-fedora.fujitsu.com (unknown [10.124.196.110]) by m3051.s.css.fujitsu.com (Postfix) with ESMTP id 7D56744D; Tue, 17 Apr 2018 15:40:10 +0900 (JST) From: QI Fuli To: linux-nvdimm@lists.01.org Subject: [PATCH 1/2] ndctl, util: add OPT_STRING_LIST to parse_option Date: Tue, 17 Apr 2018 15:38:47 +0900 Message-Id: <20180417063848.5585-2-qi.fuli@jp.fujitsu.com> X-Mailer: git-send-email 2.17.0.140.g0b0cc9f86 In-Reply-To: <20180417063848.5585-1-qi.fuli@jp.fujitsu.com> References: <20180417063848.5585-1-qi.fuli@jp.fujitsu.com> X-TM-AS-MML: disable X-BeenThere: linux-nvdimm@lists.01.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: "Linux-nvdimm developer list." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: linux-nvdimm-bounces@lists.01.org Sender: "Linux-nvdimm" X-Virus-Scanned: ClamAV using ClamSMTP This patch adds OPT_STRING_LIST to parse_option in order to support multiple space seperated string objects in one option. Signed-off-by: QI Fuli --- ccan/list/list.h | 6 ++++++ util/parse-options.c | 25 +++++++++++++++++++++++++ util/parse-options.h | 3 +++ 3 files changed, 34 insertions(+) diff --git a/ccan/list/list.h b/ccan/list/list.h index 4d1d34e..f6c927f 100644 --- a/ccan/list/list.h +++ b/ccan/list/list.h @@ -26,6 +26,12 @@ struct list_node struct list_node *next, *prev; }; +struct string_list_node +{ + char *str; + struct list_node list; +}; + /** * struct list_head - the head of a doubly-linked list * @h: the list_head (containing next and prev pointers) diff --git a/util/parse-options.c b/util/parse-options.c index 751c091..cac18f0 100644 --- a/util/parse-options.c +++ b/util/parse-options.c @@ -20,6 +20,7 @@ #include #include #include +#include #define OPT_SHORT 1 #define OPT_UNSET 2 @@ -695,3 +696,27 @@ int parse_opt_verbosity_cb(const struct option *opt, } return 0; } + +int parse_opt_string_list(const struct option *opt, const char *arg, int unset) +{ + if (unset) + return 0; + + if (!arg) + return -1; + + struct list_head *v = opt->value; + char *temp = strdup(arg); + const char *deli = " "; + + temp = strtok(temp, deli); + while (temp != NULL) { + struct string_list_node *sln = malloc(sizeof(*sln)); + sln->str = temp; + list_add_tail(v, &sln->list); + temp = strtok(NULL, deli); + } + + free(temp); + return 0; +} diff --git a/util/parse-options.h b/util/parse-options.h index 6fd6b24..10b79cb 100644 --- a/util/parse-options.h +++ b/util/parse-options.h @@ -135,6 +135,8 @@ struct option { #define OPT_LONG(s, l, v, h) { .type = OPTION_LONG, .short_name = (s), .long_name = (l), .value = check_vtype(v, long *), .help = (h) } #define OPT_U64(s, l, v, h) { .type = OPTION_U64, .short_name = (s), .long_name = (l), .value = check_vtype(v, u64 *), .help = (h) } #define OPT_STRING(s, l, v, a, h) { .type = OPTION_STRING, .short_name = (s), .long_name = (l), .value = check_vtype(v, const char **), (a), .help = (h) } +#define OPT_STRING_LIST(s, l, v, a, h) \ + { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), .value = check_vtype(v, struct list_head *), (a), .help = (h) , 0, &parse_opt_string_list } #define OPT_DATE(s, l, v, h) \ { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), .value = (v), .argh = "time", .help = (h), .callback = parse_opt_approxidate_cb } #define OPT_CALLBACK(s, l, v, a, h, f) \ @@ -206,6 +208,7 @@ extern int parse_options_end(struct parse_opt_ctx_t *ctx); extern int parse_opt_abbrev_cb(const struct option *, const char *, int); extern int parse_opt_approxidate_cb(const struct option *, const char *, int); extern int parse_opt_verbosity_cb(const struct option *, const char *, int); +extern int parse_opt_string_list(const struct option *, const char *, int); #define OPT__VERBOSE(var) OPT_BOOLEAN('v', "verbose", (var), "be verbose") #define OPT__QUIET(var) OPT_BOOLEAN('q', "quiet", (var), "be quiet")