[RFC PATCH v5 15/24] cli: add hush 2021 as parser for run_command*()

Francis Laniel francis.laniel at amarulasolutions.com
Tue Nov 1 20:20:20 CET 2022


Enables using, in code, hush 2021 as parser for run_command function family.
It also enables the command run to be used by CLI user of hush 2021.

Signed-off-by: Francis Laniel <francis.laniel at amarulasolutions.com>
---
 common/cli.c               | 63 ++++++++++++++++++++++++++++----------
 common/cli_hush_upstream.c |  2 +-
 2 files changed, 48 insertions(+), 17 deletions(-)

diff --git a/common/cli.c b/common/cli.c
index c2287443d4..740a6919ff 100644
--- a/common/cli.c
+++ b/common/cli.c
@@ -40,14 +40,31 @@ int run_command(const char *cmd, int flag)
 		return 1;
 
 	return 0;
-#elif CONFIG_IS_ENABLED(HUSH_OLD_PARSER)
-	int hush_flags = FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP;
-
-	if (flag & CMD_FLAG_ENV)
-		hush_flags |= FLAG_CONT_ON_NEWLINE;
-	return parse_string_outer(cmd, hush_flags);
-#else /* HUSH_2021_PARSER */
-	/* Not yet implemented. */
+#elif CONFIG_IS_ENABLED(HUSH_PARSER)
+	if (gd->flags & GD_FLG_HUSH_OLD_PARSER) {
+		int hush_flags = FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP;
+
+		if (flag & CMD_FLAG_ENV)
+			hush_flags |= FLAG_CONT_ON_NEWLINE;
+		return parse_string_outer(cmd, hush_flags);
+	} else if (gd->flags & GD_FLG_HUSH_2021_PARSER) {
+		/*
+		 * Possible values for flags are the following:
+		 * FLAG_EXIT_FROM_LOOP: This flags ensures we exit from loop in
+		 * parse_and_run_stream() after first iteration while normal
+		 * behavior, * i.e. when called from cli_loop(), is to loop
+		 * infinitely.
+		 * FLAG_PARSE_SEMICOLON: Hush 2021 parses ';' and does not stop
+		 * first time it sees one. So, I think we do not need this flag.
+		 * FLAG_REPARSING: For the moment, I do not understand the goal
+		 * of this flag.
+		 * FLAG_CONT_ON_NEWLINE: This flag seems to be used to continue
+		 * parsing even when reading '\n' when coming from
+		 * run_command(). In this case, Hush 2021 reads until it finds
+		 * '\0'. So, I think we do not need this flag.
+		 */
+		return parse_string_outer_2021(cmd, FLAG_EXIT_FROM_LOOP);
+	}
 	return 1;
 #endif
 }
@@ -64,12 +81,25 @@ int run_command_repeatable(const char *cmd, int flag)
 #ifndef CONFIG_HUSH_PARSER
 	return cli_simple_run_command(cmd, flag);
 #else
+	int ret;
+
+	if (gd->flags & GD_FLG_HUSH_OLD_PARSER) {
+		ret = parse_string_outer(cmd,
+					 FLAG_PARSE_SEMICOLON
+					 | FLAG_EXIT_FROM_LOOP);
+	} else if (gd->flags & GD_FLG_HUSH_2021_PARSER) {
+		ret = parse_string_outer_2021(cmd,
+					      FLAG_PARSE_SEMICOLON
+					      | FLAG_EXIT_FROM_LOOP);
+	} else {
+		ret = 1;
+	}
+
 	/*
 	 * parse_string_outer() returns 1 for failure, so clean up
 	 * its result.
 	 */
-	if (parse_string_outer(cmd,
-			       FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP))
+	if (ret)
 		return -1;
 
 	return 0;
@@ -108,12 +138,13 @@ int run_command_list(const char *cmd, int len, int flag)
 		buff[len] = '\0';
 	}
 #ifdef CONFIG_HUSH_PARSER
-#if CONFIG_IS_ENABLED(HUSH_OLD_PARSER)
-	rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
-#else /* HUSH_2021_PARSER */
-	/* Not yet implemented. */
-	rcode = 1;
-#endif
+	if (gd->flags & GD_FLG_HUSH_OLD_PARSER) {
+		rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
+	} else if (gd->flags & GD_FLG_HUSH_2021_PARSER) {
+		rcode = parse_string_outer_2021(buff, FLAG_PARSE_SEMICOLON);
+	} else {
+		rcode = 1;
+	}
 #else
 	/*
 	 * This function will overwrite any \n it sees with a \0, which
diff --git a/common/cli_hush_upstream.c b/common/cli_hush_upstream.c
index 6c023b679a..baa36b16ee 100644
--- a/common/cli_hush_upstream.c
+++ b/common/cli_hush_upstream.c
@@ -8020,7 +8020,7 @@ static int parse_and_run_string(const char *s)
 }
 
 #ifdef __U_BOOT__
-int parse_string_outer(const char *cmd, int flags)
+int parse_string_outer_2021(const char *cmd, int flags)
 {
 	int ret;
 	int old_flags;
-- 
2.25.1



More information about the U-Boot mailing list