aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrancis Laniel2023-12-22 22:02:38 +0100
committerTom Rini2023-12-28 12:02:56 -0500
commitd8b256308d2134ffe8704f26309897ebb9b56b64 (patch)
tree15303acb86251c5b11680c865df8f07e34167e2f
parent410e78df06372507d1b836d8d825d3e3cdd11e93 (diff)
cli: hush_modern: Enable using < and > as string compare operators
In Busybox hush, '<' and '>' are used as redirection operators. For example, cat foo > bar will write content of file foo inside file bar. In U-Boot, we do not have file system, so we can hardly redirect command output inside a file. But, in actual U-Boot hush, these operators ('<' and '>') are used as string compare operators. For example, test aaa < bbb returns 0 as aaa is before bbb in the dictionary. Busybox hush also permits this, but operators need to be escaped ('\<' and '\>'). Indeed, if escaping is needed it permits the developer to think about its code, as in a lot of case, we want to compare integers (using '-lt' or '-gt') rather than strings. As testing in U-Boot is handled by the test command, we will stick with the original behaviour and not adapt to Busybox one. Nonetheless, if one day we decide to implement test with '[[ ]]', we will then stick to upstream Busybox behavior. Signed-off-by: Francis Laniel <francis.laniel@amarulasolutions.com> Reviewed-by: Simon Glass <sjg@chromium.org>
-rw-r--r--common/cli_hush_upstream.c23
1 files changed, 22 insertions, 1 deletions
diff --git a/common/cli_hush_upstream.c b/common/cli_hush_upstream.c
index 11870c51e80..a56ea6c5c8f 100644
--- a/common/cli_hush_upstream.c
+++ b/common/cli_hush_upstream.c
@@ -6159,7 +6159,28 @@ static struct pipe *parse_stream(char **pstring,
if (parse_redirect(&ctx, redir_fd, redir_style, input))
goto parse_error_exitcode1;
continue; /* get next char */
-#endif /* !__U_BOOT__ */
+#else /* __U_BOOT__ */
+ /*
+ * In U-Boot, '<' and '>' can be used in test command to test if
+ * a string is, alphabetically, before or after another.
+ * In 2021 Busybox hush, we will keep the same behavior and so not treat
+ * them as redirection operator.
+ *
+ * Indeed, in U-Boot, tests are handled by the test command and not by the
+ * shell code.
+ * So, better to give this character as input to test command.
+ *
+ * NOTE In my opinion, when you use '<' or '>' I am almost sure
+ * you wanted to use "-gt" or "-lt" in place, so thinking to
+ * escape these will make you should check your code (sh syntax
+ * at this level is, for me, error prone).
+ */
+ case '>':
+ fallthrough;
+ case '<':
+ o_addQchr(&ctx.word, ch);
+ continue;
+#endif /* __U_BOOT__ */
case '#':
if (ctx.word.length == 0 && !ctx.word.has_quoted_part) {
/* skip "#comment" */