[PATCH v2 1/2] common: cli_hush: Restore clear local variable support

From: Stefan Herbrechtsmeier stefan.herbrechtsmeier@weidmueller.com
The u-boot hush shell doesn’t support the unset command to clear a variable and therefore an empty value ("c=") should be a valid value for the set_local_var function to clear the variable. This partial reverts commit aa722529635c ("common: cli_hush: avoid dead code") and only checks for a `=` in the string. Additionally explicit call the unset_local_var function to remove the variable if the value is empty.
Signed-off-by: Stefan Herbrechtsmeier stefan.herbrechtsmeier@weidmueller.com
---
Changes in v2: - Use `!var` instead of `var == NULL`
common/cli_hush.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/common/cli_hush.c b/common/cli_hush.c index 1ad7a509df..171069f5f4 100644 --- a/common/cli_hush.c +++ b/common/cli_hush.c @@ -2171,12 +2171,18 @@ int set_local_var(const char *s, int flg_export) * NAME=VALUE format. So the first order of business is to * split 's' on the '=' into 'name' and 'value' */ value = strchr(name, '='); - if (value == NULL || *(value + 1) == 0) { + if (!value) { free(name); return -1; } *value++ = 0;
+ if (!*value) { + unset_local_var(name); + free(name); + return 0; + } + for(cur = top_vars; cur; cur = cur->next) { if(strcmp(cur->name, name)==0) break; -- 2.30.2
________________________________ Kommanditgesellschaft - Sitz: Detmold - Amtsgericht Lemgo HRA 2790 - Komplementärin: Weidmüller Interface Führungsgesellschaft mbH - Sitz: Detmold - Amtsgericht Lemgo HRB 3924; Geschäftsführer: Dr. Timo Berger, Volker Bibelhausen, Dr. Sebastian Durst, André Sombecki; USt-ID-Nr. DE124599660

From: Stefan Herbrechtsmeier stefan.herbrechtsmeier@weidmueller.com
Add a test for the hush shell variable assignment and clear.
Signed-off-by: Stefan Herbrechtsmeier stefan.herbrechtsmeier@weidmueller.com ---
(no changes since v1)
test/py/tests/test_hush_if_test.py | 13 +++++++++++++ 1 file changed, 13 insertions(+)
diff --git a/test/py/tests/test_hush_if_test.py b/test/py/tests/test_hush_if_test.py index 37c1608bb2..3b4b6fcaf4 100644 --- a/test/py/tests/test_hush_if_test.py +++ b/test/py/tests/test_hush_if_test.py @@ -182,3 +182,16 @@ def test_hush_if_test_host_file_exists(u_boot_console):
expr = 'test -e hostfs - ' + test_file exec_hush_if(u_boot_console, expr, False) + +def test_hush_var(u_boot_console): + """Test the set and unset of variables""" + u_boot_console.run_command('ut_var_nonexistent=') + u_boot_console.run_command('ut_var_exists=1') + u_boot_console.run_command('ut_var_unset=1') + exec_hush_if(u_boot_console, 'test -z "$ut_var_nonexistent"', True) + exec_hush_if(u_boot_console, 'test -z "$ut_var_exists"', False) + exec_hush_if(u_boot_console, 'test -z "$ut_var_unset"', False) + exec_hush_if(u_boot_console, 'ut_var_unset=', True) + exec_hush_if(u_boot_console, 'test -z "$ut_var_unset"', True) + u_boot_console.run_command('ut_var_exists=') + u_boot_console.run_command('ut_var_unset=') -- 2.30.2
________________________________ Kommanditgesellschaft - Sitz: Detmold - Amtsgericht Lemgo HRA 2790 - Komplementärin: Weidmüller Interface Führungsgesellschaft mbH - Sitz: Detmold - Amtsgericht Lemgo HRB 3924; Geschäftsführer: Dr. Timo Berger, Volker Bibelhausen, Dr. Sebastian Durst, André Sombecki; USt-ID-Nr. DE124599660

On Tue, 4 Apr 2023 at 01:50, Stefan Herbrechtsmeier stefan.herbrechtsmeier-oss@weidmueller.com wrote:
From: Stefan Herbrechtsmeier stefan.herbrechtsmeier@weidmueller.com
Add a test for the hush shell variable assignment and clear.
Signed-off-by: Stefan Herbrechtsmeier stefan.herbrechtsmeier@weidmueller.com
(no changes since v1)
test/py/tests/test_hush_if_test.py | 13 +++++++++++++ 1 file changed, 13 insertions(+)
Reviewed-by: Simon Glass sjg@chromium.org

On Mon, Apr 03, 2023 at 03:50:01PM +0200, Stefan Herbrechtsmeier wrote:
From: Stefan Herbrechtsmeier stefan.herbrechtsmeier@weidmueller.com
Add a test for the hush shell variable assignment and clear.
Signed-off-by: Stefan Herbrechtsmeier stefan.herbrechtsmeier@weidmueller.com Reviewed-by: Simon Glass sjg@chromium.org
Applied to u-boot/master, thanks!

On Tue, 4 Apr 2023 at 01:50, Stefan Herbrechtsmeier stefan.herbrechtsmeier-oss@weidmueller.com wrote:
From: Stefan Herbrechtsmeier stefan.herbrechtsmeier@weidmueller.com
The u-boot hush shell doesn’t support the unset command to clear a variable and therefore an empty value ("c=") should be a valid value for the set_local_var function to clear the variable. This partial reverts commit aa722529635c ("common: cli_hush: avoid dead code") and only checks for a `=` in the string. Additionally explicit call the unset_local_var function to remove the variable if the value is empty.
Signed-off-by: Stefan Herbrechtsmeier stefan.herbrechtsmeier@weidmueller.com
Changes in v2:
- Use `!var` instead of `var == NULL`
common/cli_hush.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
Reviewed-by: Simon Glass sjg@chromium.org

On Mon, Apr 03, 2023 at 03:50:00PM +0200, Stefan Herbrechtsmeier wrote:
From: Stefan Herbrechtsmeier stefan.herbrechtsmeier@weidmueller.com
The u-boot hush shell doesn’t support the unset command to clear a variable and therefore an empty value ("c=") should be a valid value for the set_local_var function to clear the variable. This partial reverts commit aa722529635c ("common: cli_hush: avoid dead code") and only checks for a `=` in the string. Additionally explicit call the unset_local_var function to remove the variable if the value is empty.
Signed-off-by: Stefan Herbrechtsmeier stefan.herbrechtsmeier@weidmueller.com Reviewed-by: Simon Glass sjg@chromium.org
Applied to u-boot/master, thanks!
participants (3)
-
Simon Glass
-
Stefan Herbrechtsmeier
-
Tom Rini