Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id DB5F7C352A1 for ; Wed, 7 Dec 2022 08:54:43 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 184548555E; Wed, 7 Dec 2022 09:54:07 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="PTnFjMCi"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id AA11085527; Wed, 7 Dec 2022 09:54:05 +0100 (CET) Received: from mail-pf1-x42a.google.com (mail-pf1-x42a.google.com [IPv6:2607:f8b0:4864:20::42a]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id DB87485161 for ; Wed, 7 Dec 2022 09:54:01 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sjg@chromium.org Received: by mail-pf1-x42a.google.com with SMTP id g1so8912150pfk.2 for ; Wed, 07 Dec 2022 00:54:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=hlIj52Im9ObWBi5o47IvRY+RmbQnLKBxhV9BNh4tfgg=; b=PTnFjMCiv5Qbc8wFsb6liHqznwOM7uWb+yWx4O/78YuT89CS2QnhjAcbN5aeYOT41S DuEkv8SeerLIkSu/kXLPkYiRF2500ncJn7of09EQ7+A6y3nUrrj1Jj7B96cBa4YHpueo +017AM+nXHoPR27Jl8WtDPGTiQxjUI4tgBrtA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hlIj52Im9ObWBi5o47IvRY+RmbQnLKBxhV9BNh4tfgg=; b=vtpYJ9XyOjTjFw//EPMC5N3gkaJXkb+YmhzojPVxd1GqTOw8iwxDH7NGJ58+hGy3I8 Z2lOZPy9ixGcvjMPw7wurdKob44C660LxD+ax5naNCk52r+xKAOoaYPwxTKezqFYT3PT UyK7rud3E8coHh3X5PYil4WgqIHFLIdWHagRGhPmIOKhCw6zxQbaSnKPWywj+tYdAX9s y0ZVS8MU/DtFi2JeV725nXcgAA415uc0uAiBb6mgLejTjMnEfwRIFvih8exRlMALfW4Y cYo53Cb2sil1aF/rPoLUO4IqiEUQrj6Bo8/1Slr0t+AGfvj6gQXBYB6f0AO3gAnC7wbQ QM6g== X-Gm-Message-State: ANoB5pm1o+9mKDDkmacu094PSOXdTCMOkYWlgn7SNRIpmxwnvZbCb2eq UdLhpymKzc0Mmo8E281txzTULZ3Bb9h0M06f X-Google-Smtp-Source: AA0mqf4P7TlfVzGAffP7FMVpdxgUS3M7OFsgjvwaPQkmvg7zBIE32kqQEAlyRQv03+yR6O/JfZC+vw== X-Received: by 2002:a63:fd01:0:b0:478:c026:d059 with SMTP id d1-20020a63fd01000000b00478c026d059mr11468129pgh.447.1670403239837; Wed, 07 Dec 2022 00:53:59 -0800 (PST) Received: from localhost.localdomain ([27.110.126.54]) by smtp.gmail.com with ESMTPSA id l16-20020a170903121000b0018157b415dbsm14145514plh.63.2022.12.07.00.53.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Dec 2022 00:53:59 -0800 (PST) From: Simon Glass To: U-Boot Mailing List Cc: U-Boot Custodians , Simon Glass , Heinrich Schuchardt , Samuel Dionne-Riel Subject: [PATCH 04/71] lib: Add a function to split a string into substrings Date: Wed, 7 Dec 2022 21:50:30 +1300 Message-Id: <20221207085137.445249-5-sjg@chromium.org> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog In-Reply-To: <20221207085137.445249-1-sjg@chromium.org> References: <20221207085137.445249-1-sjg@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean Some environment variables provide a space-separated list of strings. It is easier to process these when they are broken out into an array of strings. Add a utility function to handle this. Signed-off-by: Simon Glass --- include/vsprintf.h | 24 ++++++++++++++ lib/strto.c | 41 +++++++++++++++++++++++ test/str_ut.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 147 insertions(+) diff --git a/include/vsprintf.h b/include/vsprintf.h index e006af200fd..ed8a060ee17 100644 --- a/include/vsprintf.h +++ b/include/vsprintf.h @@ -328,6 +328,30 @@ char *strmhz(char *buf, unsigned long hz); */ void str_to_upper(const char *in, char *out, size_t len); +/** + * str_to_list() - Convert a string to a list of string pointers + * + * Splits a string containing space-delimited substrings into a number of + * separate strings, e.g. "this is" becomes {"this", "is", NULL}. If @instr is + * empty then this returns just {NULL}. The string should have only a single + * space between items, with no leading or trailing spaces. + * + * @instr: String to process (this is alloced by this function) + * Returns: List of string pointers, terminated by NULL. Each entry points to + * a string. If @instr is empty, the list consists just of a single NULL entry. + * Note that the first entry points to the alloced string. + * Returns NULL if out of memory + */ +const char **str_to_list(const char *instr); + +/** + * str_free_list() - Free a string list + * + * @ptr: String list to free, as created by str_to_list(). This can also be + * NULL, in which case the function does nothing + */ +void str_free_list(const char **ptr); + /** * vsscanf - Unformat a buffer into a list of arguments * @inp: input buffer diff --git a/lib/strto.c b/lib/strto.c index 6462d4fddf1..154921165cb 100644 --- a/lib/strto.c +++ b/lib/strto.c @@ -11,6 +11,7 @@ #include #include +#include #include /* from lib/kstrtox.c */ @@ -222,3 +223,43 @@ void str_to_upper(const char *in, char *out, size_t len) if (len) *out = '\0'; } + +const char **str_to_list(const char *instr) +{ + const char **ptr; + char *str, *p; + int count, i; + + /* don't allocate if the string is empty */ + str = *instr ? strdup(instr) : (char *)instr; + if (!str) + return NULL; + + /* count the number of space-separated strings */ + for (count = *str != '\0', p = str; *p; p++) { + if (*p == ' ') { + count++; + *p = '\0'; + } + } + + /* allocate the pointer array, allowing for a NULL terminator */ + ptr = calloc(count + 1, sizeof(char *)); + if (!ptr) { + if (*str) + free(str); + return NULL; + } + + for (i = 0, p = str; i < count; p += strlen(p) + 1, i++) + ptr[i] = p; + + return ptr; +} + +void str_free_list(const char **ptr) +{ + if (ptr) + free((char *)ptr[0]); + free(ptr); +} diff --git a/test/str_ut.c b/test/str_ut.c index 5a844347c2b..fa9328ede50 100644 --- a/test/str_ut.c +++ b/test/str_ut.c @@ -274,6 +274,88 @@ static int str_trailing(struct unit_test_state *uts) } STR_TEST(str_trailing, 0); +static int test_str_to_list(struct unit_test_state *uts) +{ + const char **ptr; + ulong start; + + /* check out of memory */ + start = ut_check_delta(0); + malloc_enable_testing(0); + ut_assertnull(str_to_list("")); + ut_assertok(ut_check_delta(start)); + + ut_assertnull(str_to_list("this is a test")); + ut_assertok(ut_check_delta(start)); + + malloc_enable_testing(1); + ut_assertnull(str_to_list("this is a test")); + ut_assertok(ut_check_delta(start)); + + /* for an empty string, only one nalloc is needed */ + malloc_enable_testing(1); + ptr = str_to_list(""); + ut_assertnonnull(ptr); + ut_assertnull(ptr[0]); + str_free_list(ptr); + ut_assertok(ut_check_delta(start)); + + malloc_disable_testing(); + + /* test the same again, without any nalloc restrictions */ + ptr = str_to_list(""); + ut_assertnonnull(ptr); + ut_assertnull(ptr[0]); + str_free_list(ptr); + ut_assertok(ut_check_delta(start)); + + /* test a single string */ + start = ut_check_delta(0); + ptr = str_to_list("hi"); + ut_assertnonnull(ptr); + ut_assertnonnull(ptr[0]); + ut_asserteq_str("hi", ptr[0]); + ut_assertnull(ptr[1]); + str_free_list(ptr); + ut_assertok(ut_check_delta(start)); + + /* test two strings */ + ptr = str_to_list("hi there"); + ut_assertnonnull(ptr); + ut_assertnonnull(ptr[0]); + ut_asserteq_str("hi", ptr[0]); + ut_assertnonnull(ptr[1]); + ut_asserteq_str("there", ptr[1]); + ut_assertnull(ptr[2]); + str_free_list(ptr); + ut_assertok(ut_check_delta(start)); + + /* test leading, trailing and multiple spaces */ + ptr = str_to_list(" more space "); + ut_assertnonnull(ptr); + ut_assertnonnull(ptr[0]); + ut_asserteq_str("", ptr[0]); + ut_assertnonnull(ptr[1]); + ut_asserteq_str("more", ptr[1]); + ut_assertnonnull(ptr[2]); + ut_asserteq_str("", ptr[2]); + ut_assertnonnull(ptr[3]); + ut_asserteq_str("space", ptr[3]); + ut_assertnonnull(ptr[4]); + ut_asserteq_str("", ptr[4]); + ut_assertnonnull(ptr[5]); + ut_asserteq_str("", ptr[5]); + ut_assertnull(ptr[6]); + str_free_list(ptr); + ut_assertok(ut_check_delta(start)); + + /* test freeing a NULL pointer */ + str_free_list(NULL); + + return 0; +} +STR_TEST(test_str_to_list, 0); + int do_ut_str(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) { struct unit_test *tests = UNIT_TEST_SUITE_START(str_test); -- 2.39.0.rc0.267.gcb52ba06e7-goog