Skip to content

cmd: Add support for command substitution

Sean Anderson u-boot 1 version New

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest5 patches 0 repliesNew

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/5hush: Print syntax error line with DEBUG_SHELLcommon/cli_hush.c+8 -61 reply
1/5hush: Print syntax error line with DEBUG_SHELL+0 -00 replies
2/5cmd: Add support for (limited) command substitutioncommon/cli_hush.c common/cli_simple.c common/command.c +1 more+50 -41 reply
2/5cmd: Add support for (limited) command substitution+0 -00 replies
3/5cmd: Convert echo to use cmd_resultcmd/echo.c+26 -71 reply
4/5test: hush: Add test for command substitutiontest/cmd/test_echo.c+9 -10 replies
5/5cmd: Convert part uuid to use cmd_resultcmd/part.c+14 -43 replies
5/5cmd: Convert part uuid to use cmd_result+0 -00 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [PATCH 0/5] cmd: Add support for command substitution

This series adds support for command substitution using $(). The motivation for this is that we have many commands which have a version which prints some information, and one which sets an environmental variable with that info. This is a bit clunky, since every time someone wants to grab info from a command (e.g. [1], [2]), they have to add a version which sets a variable. Every command also has subtle differences in the syntax of how the environmental is specified.

The classic way to do this is to redirect the output of the command into a variable using command substitution. Unfortunately, this approach will not work in U-Boot for a few reasons. First, we don't have any jobs, so we can't use pipelines to filter output. In addition, the command output tends to be very "human-oriented" and would be hard to parse, even if something like console recording were used. Instead, commands just set gd->cmd_result. This result is then printed by the caller of cmd_process.

Unfortunately, this means that to support command substitution, commands must be modified to set cmd_result. However, they already would have needed modification to support setting an environmental variable.

Printing out the result of the command may not be the best approach here. If a command which prints out many messages already returns a result, just printing the result on its own does not clearly convey what the result means. Perhaps a prefix should be added to printed results? Though this would require echo to be changed.

This initial series just converts two commands: echo and part uuid. This showcases the manner in which this new functionality could be implemented. I think that some helper functions like for env may be useful so that commands don't have to do some much bookkeeping (like in the current echo command).

This series depends on [3].

[1] https://patchwork.ozlabs.org/project/uboot/patch/20210226181733.19307-1-farhan.ali at broadcom.com/
[2] https://patchwork.ozlabs.org/project/uboot/patch/20201215165439.13165-1-m.szyprowski at samsung.com/
[3] https://patchwork.ozlabs.org/project/uboot/patch/20210228212951.1175231-1-seanga2 at gmail.com/

Sean Anderson (5):
hush: Print syntax error line with DEBUG_SHELL
cmd: Add support for (limited) command substitution
cmd: Convert echo to use cmd_result
test: hush: Add test for command substitution
cmd: Convert part uuid to use cmd_result

 cmd/echo.c                        | 33 +++++++++++++++----
 cmd/part.c                        | 18 ++++++++---
 common/cli_hush.c                 | 53 +++++++++++++++++++++++++------
 common/cli_simple.c               |  8 ++++-
 common/command.c                  |  3 ++
 include/asm-generic/global_data.h |  4 +++
 test/cmd/test_echo.c              | 10 +++++-
 7 files changed, 107 insertions(+), 22 deletions(-)
Signature · 1 line
2.30.1