From fe24a6a55e8850298b496c5b9d82f1866eba190e Mon Sep 17 00:00:00 2001
From: Chet Ramey <chet.ramey@case.edu>
Date: Mon, 30 Oct 2023 12:16:07 -0400
Subject: [PATCH] changes to SIGINT handler while waiting for a child; skip
 vertical whitespace after translating an integer

Conflict:partial round, only the modified content of jobs.c is rounded.
Reference:https://git.savannah.gnu.org/cgit/bash.git/commit/?id=fe24a6a55e8850298b496c5b9d82f1866eba190e
---
 jobs.c | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)

diff --git a/jobs.c b/jobs.c
index 7c3b6e8..c22ac9f 100644
--- a/jobs.c
+++ b/jobs.c
@@ -2727,6 +2727,10 @@ wait_for_background_pids (ps)
 #define INVALID_SIGNAL_HANDLER (SigHandler *)wait_for_background_pids
 static SigHandler *old_sigint_handler = INVALID_SIGNAL_HANDLER;
 
+/* The current SIGINT handler as set by restore_sigint_handler. Only valid
+   immediately after restore_sigint_handler, used for continuations. */
+static SigHandler *cur_sigint_handler = INVALID_SIGNAL_HANDLER;   
+
 static int wait_sigint_received;
 static int child_caught_sigint;
 
@@ -2743,6 +2747,7 @@ wait_sigint_cleanup ()
 static void
 restore_sigint_handler ()
 {
+  cur_sigint_handler = old_sigint_handler;
   if (old_sigint_handler != INVALID_SIGNAL_HANDLER)
     {
       set_signal_handler (SIGINT, old_sigint_handler);
@@ -2766,8 +2771,7 @@ wait_sigint_handler (sig)
       restore_sigint_handler ();
       /* If we got a SIGINT while in `wait', and SIGINT is trapped, do
 	 what POSIX.2 says (see builtins/wait.def for more info). */
-      if (this_shell_builtin && this_shell_builtin == wait_builtin &&
-	  signal_is_trapped (SIGINT) &&
+      if (signal_is_trapped (SIGINT) &&
 	  ((sigint_handler = trap_to_sighandler (SIGINT)) == trap_handler))
 	{
 	  trap_handler (SIGINT);	/* set pending_traps[SIGINT] */
@@ -2790,6 +2794,8 @@ wait_sigint_handler (sig)
     {
       set_exit_status (128+SIGINT);
       restore_sigint_handler ();
+      if (cur_sigint_handler == INVALID_SIGNAL_HANDLER)
+	set_sigint_handler ();		/* XXX - only do this in one place */
       kill (getpid (), SIGINT);
     }
 
@@ -2934,15 +2940,15 @@ wait_for (pid, flags)
     {
       SigHandler *temp_sigint_handler;
 
-      temp_sigint_handler = set_signal_handler (SIGINT, wait_sigint_handler);
-      if (temp_sigint_handler == wait_sigint_handler)
+      temp_sigint_handler = old_sigint_handler;
+      old_sigint_handler = set_signal_handler (SIGINT, wait_sigint_handler);
+      if (old_sigint_handler == wait_sigint_handler)
 	{
 #if defined (DEBUG)
 	  internal_warning ("wait_for: recursively setting old_sigint_handler to wait_sigint_handler: running_trap = %d", running_trap);
 #endif
+	  old_sigint_handler = temp_sigint_handler;
 	}
-      else
-	old_sigint_handler = temp_sigint_handler;
       waiting_for_child = 0;
       if (old_sigint_handler == SIG_IGN)
 	set_signal_handler (SIGINT, old_sigint_handler);
@@ -4148,7 +4154,7 @@ set_job_status_and_cleanup (job)
 		 SIGINT (if we reset the sighandler to the default).
 		 In this case, we have to fix things up.  What a crock. */
 	      if (temp_handler == trap_handler && signal_is_trapped (SIGINT) == 0)
-		  temp_handler = trap_to_sighandler (SIGINT);
+		temp_handler = trap_to_sighandler (SIGINT);
 	      restore_sigint_handler ();
 	      if (temp_handler == SIG_DFL)
 		termsig_handler (SIGINT);	/* XXX */
-- 
2.33.0

