[PATCH] hush: implement PROMPT_COMMAND

Ahmad Fatoum a.fatoum at barebox.org
Wed Aug 6 06:16:12 PDT 2025


This allows dynamically updating the prompt to reflect new contextual or
global information (e.g. which security mode barebox is in).

Signed-off-by: Ahmad Fatoum <a.fatoum at barebox.org>
---
 common/Kconfig |  1 +
 common/hush.c  | 12 +++++++++++-
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/common/Kconfig b/common/Kconfig
index b2449207eff9..1d9570ef67b5 100644
--- a/common/Kconfig
+++ b/common/Kconfig
@@ -515,6 +515,7 @@ config HUSH_FANCY_PROMPT
 	help
 	  Allow to set PS1 from the command line. PS1 can have several escaped commands
 	  like \h for the 'model' string or \w for the current working directory.
+	  PS1 can be set statically or computed on demand by executing PROMPT_COMMAND.
 
 config CMDLINE_EDITING
 	depends on !SHELL_NONE
diff --git a/common/hush.c b/common/hush.c
index 086cbc68b891..552e7327de92 100644
--- a/common/hush.c
+++ b/common/hush.c
@@ -426,8 +426,17 @@ static char *getprompt(void)
 	static char prompt[PATH_MAX + 32];
 
 #ifdef CONFIG_HUSH_FANCY_PROMPT
-	const char *ps1 = getenv("PS1");
+	const char *ps1, *prompt_command;
+	struct p_context ctx = {};
 
+	prompt_command = getenv("PROMPT_COMMAND");
+	if (prompt_command) {
+		initialize_context(&ctx);
+		parse_string_outer(&ctx, prompt_command, FLAG_PARSE_SEMICOLON);
+		release_context(&ctx);
+	}
+
+	ps1 = getenv("PS1");
 	if (ps1)
 		process_escape_sequence(ps1, prompt, PATH_MAX + 32);
 	else
@@ -2065,6 +2074,7 @@ BAREBOX_CMD_END
 BAREBOX_MAGICVAR(PATH, "colon separated list of paths to search for executables");
 #ifdef CONFIG_HUSH_FANCY_PROMPT
 BAREBOX_MAGICVAR(PS1, "hush prompt");
+BAREBOX_MAGICVAR(PROMPT_COMMAND, "command to execute prior to each primary prompt");
 #endif
 
 static int binfmt_sh_excute(struct binfmt_hook *b, char *file, int argc, char **argv)
-- 
2.39.5




More information about the barebox mailing list