From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr0-f194.google.com (mail-wr0-f194.google.com [209.85.128.194]) by dpdk.org (Postfix) with ESMTP id 766607CE1 for ; Thu, 15 Mar 2018 18:50:16 +0100 (CET) Received: by mail-wr0-f194.google.com with SMTP id m12so9116132wrm.13 for ; Thu, 15 Mar 2018 10:50:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=6wind-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=6SjHYFp2UmNXSHWd7rqzlnjp+TP5H4pyim5hgvUbVTU=; b=b9Pmfqh5WS9RW/QU5b3ym8IYOGNQlBac6VgamWac3kja3Y2iHB1ZA31vVzlOO2suUu U14m4uteu8hv1UX0f51IrVfhbnXc9OBJa81ke2EPyUXgF4iDEzK/ZGdNwx2E5TdbC89f +CBX+MTWUJkVTDAsgBc8U7H2cV71Y28f52cSmYljjbopMRPQtqirLbSoKK/WbdIFxOQF sIXuc+Nh+YQ60PWf7o3EISlbpBURRS+vTfw6GLw4ex2FEYKrEZAYCxeMLHTiZdMWK3pn heTtqNAyaLP2WRy5WxIT+CCpmcXWFj+NqmjieiFqUqLOfse6q8o25gqjiJVuqd00/YOb ZzZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=6SjHYFp2UmNXSHWd7rqzlnjp+TP5H4pyim5hgvUbVTU=; b=twV16xMYsH06qbfuaqA+XN9VViStHzm7kqkZTV+GfXFud03LPaY9UYbRpRyViEGb9F f5yUtPsxqt3y+7Vc4f3UNiNHsSHmkhfnw1XAnwh8yLydnFxwK0Ym/DFJG+AuUpWUeeSU qA3Kqjbm7NSrVmW9MnjRJ6bW7bPGAQFlTTaRGfpcA3z03Ib8T1hryZdC5jdbKhfQRdDt wfdrMqffiY0QssDRnWGgF48WfeBVpqwJfycQc/ZcMmudPesv4Dkssc3308pPpwUUDNsJ f3QfdsF8OAw2DLbLnRR69HEymyuh0SWWs9bySgqwQJHGDCYsPlUOxEMHEmrts0e4KDpW ne5w== X-Gm-Message-State: AElRT7GCJQy7Fck/ewrK3VcrQSQUtx9+D1jZGg4jG+PV2fNHOQwu0uRH cyGLs/ejhkux4P8BKzGIvdIsvUi7 X-Google-Smtp-Source: AG47ELtRgywn/4UfN7I1wWWQeOWBxWLCai9nrRZgKjLvl3Ir3/XnjPgIQr4+kbRqke4iIrOi5bvEFA== X-Received: by 10.223.160.110 with SMTP id l43mr7563930wrl.264.1521136215358; Thu, 15 Mar 2018 10:50:15 -0700 (PDT) Received: from bidouze.dev.6wind.com. (host.78.145.23.62.rev.coltfrance.com. [62.23.145.78]) by smtp.gmail.com with ESMTPSA id 69sm4756596wmp.36.2018.03.15.10.50.14 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 15 Mar 2018 10:50:14 -0700 (PDT) From: Gaetan Rivet To: dev@dpdk.org Cc: Gaetan Rivet Date: Thu, 15 Mar 2018 18:49:34 +0100 Message-Id: <0c0ddcb98c0230c5925fa53ff7fe7b9192d5f96f.1521124599.git.gaetan.rivet@6wind.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: References: In-Reply-To: References: Subject: [dpdk-dev] [PATCH v1 04/18] eal: add lightweight kvarg parsing utility X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 15 Mar 2018 17:50:16 -0000 This library offers a quick way to parse parameters passed with a key=value syntax. A single function is needed and finds the relevant element within the text. No dynamic allocation is performed. It is possible to chain the parsing of each pairs for quickly scanning a list. This utility is private to the EAL and should allow avoiding having to move around the more complete librte_kvargs. Signed-off-by: Gaetan Rivet --- lib/librte_eal/common/eal_common_dev.c | 38 ++++++++++++++++++++++++++++++++++ lib/librte_eal/common/eal_private.h | 34 ++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/lib/librte_eal/common/eal_common_dev.c b/lib/librte_eal/common/eal_common_dev.c index cd071442f..4032f1bd8 100644 --- a/lib/librte_eal/common/eal_common_dev.c +++ b/lib/librte_eal/common/eal_common_dev.c @@ -13,10 +13,48 @@ #include #include #include +#include #include #include "eal_private.h" +/* EAL-private function. */ +int +rte_parse_kv(const char *str, struct rte_kvarg *kv) +{ + const char *equal; + const char *end; + + if (str == NULL || str[0] == '\0') + return 1; + equal = strchr(str, '='); + if (equal == NULL) { + rte_errno = EINVAL; + return -1; + } + end = strchr(equal + 1, ','); + end = end ? end : strchr(equal + 1, '/'); + end = end ? end : strchr(equal + 1, '\0'); + if (end == NULL) { + rte_errno = ENODEV; + return -1; + } + if (kv == NULL) + return 0; + snprintf(kv->data, sizeof(kv->data), "%s", str); + kv->key = &kv->data[0]; + strchr(kv->data, end[0])[0] = '\0'; + if (strchr(kv->data, '=')) { + kv->value = strchr(kv->data, '=') + 1; + strchr(kv->data, '=')[0] = '\0'; + } + if (end[0] == '\0') + kv->next = NULL; + else + kv->next = end + 1; + return 0; +} + static int cmp_detached_dev_name(const struct rte_device *dev, const void *_name) { diff --git a/lib/librte_eal/common/eal_private.h b/lib/librte_eal/common/eal_private.h index 0b2877000..d2774a3ad 100644 --- a/lib/librte_eal/common/eal_private.h +++ b/lib/librte_eal/common/eal_private.h @@ -205,4 +205,38 @@ struct rte_bus *rte_bus_find_by_device_name(const char *str); int rte_mp_channel_init(void); +/* + * Lightweight kvarg parsing library. + */ + +#define RTE_MAX_KVARG_LEN 64 + +/** + * Kvarg representation. + */ +struct rte_kvarg { + char *key; /**< points the key in the data. */ + char *value; /**< points the value in the data. */ + const char *next; /**< next token to parse, if any. */ + char data[RTE_MAX_KVARG_LEN + 1]; /**< local copy of key and value. */ +}; + +/** + * Parse one kvarg. + * + * The key-value pair must be shorter than the rte_kvarg data size. + * + * @param[in] str + * text to parse. + * + * @param[out] kv + * kvarg structure to fill. + * + * @return + * 0 if parsing succeeded. + * >0 if there was nothing to parse. + * <0 on error, rte_errno is set. + */ +int rte_parse_kv(const char *str, struct rte_kvarg *kv); + #endif /* _EAL_PRIVATE_H_ */ -- 2.11.0