From patchwork Mon Jan 22 03:57:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: fengchengwen X-Patchwork-Id: 136026 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 6733B43922; Mon, 22 Jan 2024 05:02:59 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 361D140A87; Mon, 22 Jan 2024 05:02:53 +0100 (CET) Received: from szxga04-in.huawei.com (szxga04-in.huawei.com [45.249.212.190]) by mails.dpdk.org (Postfix) with ESMTP id 6805540A77 for ; Mon, 22 Jan 2024 05:02:51 +0100 (CET) Received: from mail.maildlp.com (unknown [172.19.88.214]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4TJGjb3ftPz29kc3; Mon, 22 Jan 2024 12:01:07 +0800 (CST) Received: from dggpeml500024.china.huawei.com (unknown [7.185.36.10]) by mail.maildlp.com (Postfix) with ESMTPS id CA90A1A016E; Mon, 22 Jan 2024 12:02:34 +0800 (CST) Received: from localhost.localdomain (10.50.165.33) by dggpeml500024.china.huawei.com (7.185.36.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Mon, 22 Jan 2024 12:01:49 +0800 From: Chengwen Feng To: , , , CC: Subject: [PATCH 09/12] argparse: support parse unsigned base type Date: Mon, 22 Jan 2024 03:57:59 +0000 Message-ID: <20240122035802.31491-10-fengchengwen@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20240122035802.31491-1-fengchengwen@huawei.com> References: <20231121122651.7078-1-fengchengwen@huawei.com> <20240122035802.31491-1-fengchengwen@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.50.165.33] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To dggpeml500024.china.huawei.com (7.185.36.10) X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org This commit supports parsing unsigned base type (u8/u16/u32/u64). Signed-off-by: Chengwen Feng --- lib/argparse/rte_argparse.c | 116 ++++++++++++++++++++++++++++++++++++ lib/argparse/rte_argparse.h | 10 +++- 2 files changed, 125 insertions(+), 1 deletion(-) diff --git a/lib/argparse/rte_argparse.c b/lib/argparse/rte_argparse.c index f536a7f92b..cfd9bcf5f6 100644 --- a/lib/argparse/rte_argparse.c +++ b/lib/argparse/rte_argparse.c @@ -397,6 +397,118 @@ parse_arg_int(struct rte_argparse_arg *arg, const char *value) return 0; } +static int +parse_arg_u8(struct rte_argparse_arg *arg, const char *value) +{ + unsigned long val; + char *s = NULL; + + if (value == NULL) { + *(uint8_t *)arg->val_saver = (uint8_t)(intptr_t)arg->val_set; + return 0; + } + + errno = 0; + val = strtoul(value, &s, 0); + if (errno == ERANGE || val > UINT8_MAX) { + ARGPARSE_LOG(ERR, "argument %s numerical out of range!", arg->name_long); + return -EINVAL; + } + + if (s[0] != '\0') { + ARGPARSE_LOG(ERR, "argument %s expect an uint8 value!", arg->name_long); + return -EINVAL; + } + + *(uint8_t *)arg->val_saver = val; + + return 0; +} + +static int +parse_arg_u16(struct rte_argparse_arg *arg, const char *value) +{ + unsigned long val; + char *s = NULL; + + if (value == NULL) { + *(uint16_t *)arg->val_saver = (uint16_t)(intptr_t)arg->val_set; + return 0; + } + + errno = 0; + val = strtoul(value, &s, 0); + if (errno == ERANGE || val > UINT16_MAX) { + ARGPARSE_LOG(ERR, "argument %s numerical out of range!", arg->name_long); + return -EINVAL; + } + + if (s[0] != '\0') { + ARGPARSE_LOG(ERR, "argument %s expect an uint16 value!", arg->name_long); + return -EINVAL; + } + + *(uint16_t *)arg->val_saver = val; + + return 0; +} + +static int +parse_arg_u32(struct rte_argparse_arg *arg, const char *value) +{ + unsigned long val; + char *s = NULL; + + if (value == NULL) { + *(uint32_t *)arg->val_saver = (uint32_t)(intptr_t)arg->val_set; + return 0; + } + + errno = 0; + val = strtoul(value, &s, 0); + if (errno == ERANGE || val > UINT32_MAX) { + ARGPARSE_LOG(ERR, "argument %s numerical out of range!", arg->name_long); + return -EINVAL; + } + + if (s[0] != '\0') { + ARGPARSE_LOG(ERR, "argument %s expect an uint32 value!", arg->name_long); + return -EINVAL; + } + + *(uint32_t *)arg->val_saver = val; + + return 0; +} + +static int +parse_arg_u64(struct rte_argparse_arg *arg, const char *value) +{ + unsigned long val; + char *s = NULL; + + if (value == NULL) { + *(uint64_t *)arg->val_saver = (uint64_t)(intptr_t)arg->val_set; + return 0; + } + + errno = 0; + val = strtoull(value, &s, 0); + if (errno == ERANGE) { + ARGPARSE_LOG(ERR, "argument %s numerical out of range!", arg->name_long); + return -EINVAL; + } + + if (s[0] != '\0') { + ARGPARSE_LOG(ERR, "argument %s expect an uint64 value!", arg->name_long); + return -EINVAL; + } + + *(uint64_t *)arg->val_saver = val; + + return 0; +} + static int parse_arg_autosave(struct rte_argparse_arg *arg, const char *value) { @@ -406,6 +518,10 @@ parse_arg_autosave(struct rte_argparse_arg *arg, const char *value) /* Sort by RTE_ARGPARSE_ARG_VALUE_XXX. */ { NULL }, { parse_arg_int }, + { parse_arg_u8 }, + { parse_arg_u16 }, + { parse_arg_u32 }, + { parse_arg_u64 }, }; uint32_t index = arg_attr_val_type(arg); int ret = -EINVAL; diff --git a/lib/argparse/rte_argparse.h b/lib/argparse/rte_argparse.h index d4e074d3d7..2059fe11da 100644 --- a/lib/argparse/rte_argparse.h +++ b/lib/argparse/rte_argparse.h @@ -59,8 +59,16 @@ enum rte_argparse_flag { /** The argument's value is int type. */ RTE_ARGPARSE_ARG_VALUE_INT = RTE_MBIT64(1, 2), + /** The argument's value is uint8 type. */ + RTE_ARGPARSE_ARG_VALUE_U8 = RTE_MBIT64(2, 2), + /** The argument's value is uint16 type. */ + RTE_ARGPARSE_ARG_VALUE_U16 = RTE_MBIT64(3, 2), + /** The argument's value is uint32 type. */ + RTE_ARGPARSE_ARG_VALUE_U32 = RTE_MBIT64(4, 2), + /** The argument's value is uint64 type. */ + RTE_ARGPARSE_ARG_VALUE_U64 = RTE_MBIT64(5, 2), /** Max value type. */ - RTE_ARGPARSE_ARG_VALUE_MAX = RTE_MBIT64(2, 2), + RTE_ARGPARSE_ARG_VALUE_MAX = RTE_MBIT64(6, 2), /**