From patchwork Fri Nov 19 04:36:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Art Nikpal X-Patchwork-Id: 1556894 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20210112 header.b=TQn416Fk; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4HwP644gdLz9s1l for ; Fri, 19 Nov 2021 15:37:32 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id ABF3382FE1; Fri, 19 Nov 2021 05:37:27 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="TQn416Fk"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id C415882FE6; Fri, 19 Nov 2021 05:37:18 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-pg1-x531.google.com (mail-pg1-x531.google.com [IPv6:2607:f8b0:4864:20::531]) (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 0C5CF82FD6 for ; Fri, 19 Nov 2021 05:37:12 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=email2tema@gmail.com Received: by mail-pg1-x531.google.com with SMTP id 206so2524715pgb.4 for ; Thu, 18 Nov 2021 20:37:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=0zH3hY0XQAcafuLRq0CqQB4sltIDy4taFP06YKl4Z8U=; b=TQn416FkFvT0om2kWAw82ekrCNTCAHXh5XQHOtvD0l7hb67juedDhtseqkp1VeXrqA i2H2eQQsTb99qym0lh6bUKSQZDo9dvdgnL+t7mUD5K3os8DxCZHV/1Ga3DcwvgV1WLGq qjnQ/FWq9gsuKSQIVmQ9B1cXk2gl4W7+ZUuPXXSTlD/7M4qN/Vk8zHiQg/0+bHQKnYvq OhR8JibOTaaECMT/zVReEAdWLHsiW2vOPUe5NWE1CDZhhm14XsHtplLLnnbYWpjn/PD2 FX+3ezNzV+YYkOtdgsIvQJc/05bnImg+iXte4NcBB2U6JqY4vVEYJxmRoNVqGG3+lBBY b34Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=0zH3hY0XQAcafuLRq0CqQB4sltIDy4taFP06YKl4Z8U=; b=HjhR1Mo6bEzXVqq9TfI37YOw9K9O2N11DoBYxCJau0Mqcl6xNg1QI8KwD6WpU5YjZx XEMVRiOmju8xVzkRDAd039INDjPro7QrXzeZmnxd0jQ6I62V1N+AP9WAUq6gpVwJa6P3 IfaH6nSonbNiUQMRET88K191W0rC8g4cvfLNdBFxFeGNp5+IRtpb0iW5GHhe24dVsduJ snvhgrQNByXvx0/G5cGB1j8qpi7FWV0hIjcYmD2KKMowufPf8c0l2fdFWimK7WdiaAna zAHLt99Vt4l9hyuu5MhHL7CI26HuGZYVigJaiH2OFNSBic8aAGVPCDdbJ7TbK8phvw1c dlYw== X-Gm-Message-State: AOAM5328QkaPKS24dBv9E6aaqu3hPkdfMiw4uzhk6qd3L5U262tXqyUk 4CxhAraUpjuh0dKTJ3piXxM= X-Google-Smtp-Source: ABdhPJxxVwagtoVPLHdf7uNjfDWPhWkZtB7hZ6dSasFi13vPES7QJQ5OcxBM+VkdP5jobOuCGhZLBw== X-Received: by 2002:a63:6ece:: with SMTP id j197mr15035752pgc.11.1637296630272; Thu, 18 Nov 2021 20:37:10 -0800 (PST) Received: from localhost.localdomain ([2409:8929:1a0b:9314:3a1e:44db:1de0:ce09]) by smtp.gmail.com with ESMTPSA id u23sm1113089pfl.185.2021.11.18.20.37.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Nov 2021 20:37:10 -0800 (PST) From: Artem Lapkin X-Google-Original-From: Artem Lapkin To: sjg@chromium.org Cc: trini@konsulko.com, marek.behun@nic.cz, narmstrong@baylibre.com, twarren@nvidia.com, andre.przywara@arm.com, u-boot@lists.denx.de, u-boot-amlogic@groups.io, christianshewitt@gmail.com, art@khadas.com, nick@khadas.com, gouwa@khadas.com Subject: [PATCH v2 1/2] env: setenv add resolve value option Date: Fri, 19 Nov 2021 12:36:46 +0800 Message-Id: <20211119043647.1251416-2-art@khadas.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211119043647.1251416-1-art@khadas.com> References: <20211119043647.1251416-1-art@khadas.com> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.35 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.2 at phobos.denx.de X-Virus-Status: Clean Add possibility setup env variable with additional resolving vars inside value. Usage examples: => setenv a hello; setenv b world; setenv c '${a} ${b}' => setenv -r d '${c}! ${a}...' => printenv d d=hello world! hello... /* internal usage example */ env_resolve("d", "${c}! ${a}..."); /* d="hello world! hello..." */ Signed-off-by: Artem Lapkin Reviewed-by: Simon Glass --- V2 changes: _ fix comments style _ add comment include/exports.h _ remake strcpy to strdup _ env_resolve minimize --- cmd/nvedit.c | 43 ++++++++++++++++++++++++++++++++++++++++++- include/_exports.h | 1 + include/env.h | 11 +++++++++++ include/exports.h | 2 ++ 4 files changed, 56 insertions(+), 1 deletion(-) diff --git a/cmd/nvedit.c b/cmd/nvedit.c index 3bb6e764c0..6e1237df81 100644 --- a/cmd/nvedit.c +++ b/cmd/nvedit.c @@ -229,6 +229,7 @@ static int _do_env_set(int flag, int argc, char *const argv[], int env_flag) int i, len; char *name, *value, *s; struct env_entry e, *ep; + bool resolve = 0; debug("Initial value for argc=%d\n", argc); @@ -246,6 +247,9 @@ static int _do_env_set(int flag, int argc, char *const argv[], int env_flag) case 'f': /* force */ env_flag |= H_FORCE; break; + case 'r': /* resolve */ + resolve = 1; + break; default: return CMD_RET_USAGE; } @@ -291,6 +295,29 @@ static int _do_env_set(int flag, int argc, char *const argv[], int env_flag) if (s != value) *--s = '\0'; + /* deep resolve value vars */ + if (resolve) { + int max_loop = 32; + char value2[CONFIG_SYS_CBSIZE]; + char *v = NULL; + + do { + cli_simple_process_macros(value, value2, CONFIG_SYS_CBSIZE); + + if (!strcmp(value, value2)) + break; + + v = strdup(value2); + if (!v) { + printf("## Can't allocate memory\n"); + return 1; + } + + free(value); + value = v; + } while (max_loop--); + } + e.key = name; e.data = value; hsearch_r(e, ENV_ENTER, &ep, &env_htab, env_flag); @@ -304,6 +331,18 @@ static int _do_env_set(int flag, int argc, char *const argv[], int env_flag) return 0; } +int env_resolve(const char *varname, const char *varvalue) +{ + const char * const argv[5] = { "setenv", "-r", varname, varvalue, NULL }; + + /* before import into hashtable */ + if (!(gd->flags & GD_FLG_ENV_READY)) + return 1; + + return _do_env_set(0, !varvalue || varvalue[0] == '\0' ? 3 : 4, + (char * const *)argv, H_PROGRAMMATIC); +} + int env_set(const char *varname, const char *varvalue) { const char * const argv[4] = { "setenv", varname, varvalue, NULL }; @@ -1371,7 +1410,9 @@ U_BOOT_CMD_COMPLETE( "setenv [-f] name value ...\n" " - [forcibly] set environment variable 'name' to 'value ...'\n" "setenv [-f] name\n" - " - [forcibly] delete environment variable 'name'", + " - [forcibly] delete environment variable 'name'\n" + "setenv [-r] name value ...\n" + " - [resolve] resolve 'value ...' to environment variable\n", var_complete ); diff --git a/include/_exports.h b/include/_exports.h index 8030d70c0b..86bc07f051 100644 --- a/include/_exports.h +++ b/include/_exports.h @@ -32,6 +32,7 @@ EXPORT_FUNC(do_reset, int, do_reset, struct cmd_tbl *, int , int , char * const []) EXPORT_FUNC(env_get, char *, env_get, const char*) + EXPORT_FUNC(env_resolve, int, env_resolve, const char *, const char *) EXPORT_FUNC(env_set, int, env_set, const char *, const char *) EXPORT_FUNC(simple_strtoul, unsigned long, simple_strtoul, const char *, char **, unsigned int) diff --git a/include/env.h b/include/env.h index ee5e30d036..73d1aca9ac 100644 --- a/include/env.h +++ b/include/env.h @@ -133,6 +133,17 @@ int env_get_f(const char *name, char *buf, unsigned int len); */ int env_get_yesno(const char *var); +/** + * env_resolve() - resolve to environment variable + * + * Same as env_set but make deep resolve for value + * + * @varname: Variable to adjust + * @value: Value to resolve for the variable, or NULL or "" to delete the variable + * @return 0 if OK, 1 on error + */ +int env_resolve(const char *varname, const char *value); + /** * env_set() - set an environment variable * diff --git a/include/exports.h b/include/exports.h index 550cafdc7a..02aac8047c 100644 --- a/include/exports.h +++ b/include/exports.h @@ -44,6 +44,8 @@ int vprintf(const char *, va_list); unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base); int strict_strtoul(const char *cp, unsigned int base, unsigned long *res); char *env_get(const char *name); +/* deep resolve value to environment variable */ +int env_resolve(const char *varname, const char *value); int env_set(const char *varname, const char *value); long simple_strtol(const char *cp, char **endp, unsigned int base); int strcmp(const char *cs, const char *ct);