Skip to content

nshlib: Restore the console after a redirected "source". - #3915

Merged
xiaoxiang781216 merged 1 commit into
apache:masterfrom
casaroli:fb-nsh-source-redirect-up
Oct 26, 2026
Merged

xiaoxiang781216 merged 1 commit into
apache:masterfrom
casaroli:fb-nsh-source-redirect-up

Conversation

@casaroli

Copy link
Copy Markdown
Contributor

Summary

nsh_execute() redirects the output of an NSH command, runs it, and then restores the output if one of the np_redir_* flags is set. source and . parse each line of the script with the same vtbl, and nsh_parse_command() clears these flags for each line. So after source script > /dev/null NSH did not call nsh_undirect(). The console kept the closed redirection descriptor as its output: the prompt and all later output were lost, and NSH looked hung.

This PR keeps the redirection state in a local variable and restores the output when it is set.

Impact

Only NSH commands that parse more lines with the same vtbl (source, .) and have a redirection. Other commands do not change.

Testing

qemu-armv8a:knsh under QEMU with HVF, NSH from this branch, a script e4.sh with one line echo E4-INNER:

Command master this PR
source e4.sh prints E4-INNER prints E4-INNER
source e4.sh > /dev/null, then echo E4-AFTER1 no prompt, no output any more prompt, E4-AFTER1
source e4.sh > e4.out, then cat e4.out (not reached) E4-INNER
. e4.sh 2> /dev/null, then echo E4-AFTER2 (not reached) E4-INNER, E4-AFTER2

ostest exits with status 0 on qemu-armv8a:knsh and qemu-armv8a:nsh (NSH from this branch).

Build with NuttX master, each OK: qemu-armv8a:nsh (flat, arm64), qemu-armv8a:knsh (kernel, arm64), rv-virt:nsh64 (flat, rv64), rv-virt:pnsh64 (protected, rv64).

tools/checkpatch.sh -c -u -m -g passes. No documentation change.

nsh_execute() redirects the output of an NSH command, runs it, and then
restores the output if one of the np_redir_* flags is set.  "source"
and "." parse each line of the script with the same vtbl, and
nsh_parse_command() clears these flags for each line.  After a script
with a redirection, for example "source script > /dev/null", NSH did
not call nsh_undirect(): the console kept the closed redirection file
descriptor as its output, so the prompt and all later output were lost
and NSH looked hung.

Keep the redirection state in a local variable and restore the output
when it is set.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
@xiaoxiang781216

Copy link
Copy Markdown
Contributor

ci break fixed by #3914

@cederom cederom left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @casaroli good catch :-)

@xiaoxiang781216
xiaoxiang781216 merged commit c03829f into apache:master Oct 26, 2026
27 of 40 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants