From: Ahmad Fatoum <a.fatoum@barebox.org>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@barebox.org>
Subject: [PATCH] hush: implement PROMPT_COMMAND
Date: Wed, 6 Aug 2025 15:16:12 +0200 [thread overview]
Message-ID: <20250806131612.2823451-1-a.fatoum@barebox.org> (raw)
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@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
next reply other threads:[~2025-08-06 13:16 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-06 13:16 Ahmad Fatoum [this message]
2025-08-13 5:49 ` Sascha Hauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250806131612.2823451-1-a.fatoum@barebox.org \
--to=a.fatoum@barebox.org \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox