This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git


The following commit(s) were added to refs/heads/master by this push:
     new c03829f56 nshlib: Restore the console after a redirected "source".
c03829f56 is described below

commit c03829f563fb5a2895003a95cd40a639b10ce686
Author: Marco Casaroli <[email protected]>
AuthorDate: Tue Oct 20 11:15:16 2026 +0200

    nshlib: Restore the console after a redirected "source".
    
    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 <[email protected]>
---
 nshlib/nsh_parse.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/nshlib/nsh_parse.c b/nshlib/nsh_parse.c
index 7bbc52ff5..ec8251357 100644
--- a/nshlib/nsh_parse.c
+++ b/nshlib/nsh_parse.c
@@ -659,6 +659,7 @@ static int nsh_execute(FAR struct nsh_vtbl_s *vtbl,
 #endif
     {
       uint8_t save[SAVE_SIZE];
+      bool redirected;
 
       /* Redirected output? */
 
@@ -740,8 +741,9 @@ static int nsh_execute(FAR struct nsh_vtbl_s *vtbl,
 
       /* Handle redirection of stdin/stdout/stderr file descriptor */
 
-      if (vtbl->np.np_redir_out || vtbl->np.np_redir_in ||
-          vtbl->np.np_redir_err)
+      redirected = vtbl->np.np_redir_out || vtbl->np.np_redir_in ||
+                   vtbl->np.np_redir_err;
+      if (redirected)
         {
           nsh_redirect(vtbl, fd_in, fd_out, fd_err, save);
         }
@@ -756,11 +758,11 @@ static int nsh_execute(FAR struct nsh_vtbl_s *vtbl,
       ret = nsh_command(vtbl, argc, argv);
 
       /* Restore the original output.  Undirect will close the redirection
-       * file descriptor.
+       * file descriptor.  Do not test the np_redir_* flags again: a script
+       * ("source") parses its lines with this vtbl, which clears them.
        */
 
-      if (vtbl->np.np_redir_out || vtbl->np.np_redir_in ||
-          vtbl->np.np_redir_err)
+      if (redirected)
         {
           nsh_undirect(vtbl, save);
           fd_out = -1;

Reply via email to