From eebd48c0aa204031b6e094225545fb15a8dd6ad2 Mon Sep 17 00:00:00 2001
From: vda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277>
Date: Sat, 28 Apr 2007 16:45:59 +0000
Subject: hush: add Ctrl-C handling to nofork case

git-svn-id: svn://busybox.net/trunk/busybox@18523 69ca8d6d-28ef-0310-b511-8ec308f3f277
---
 shell/hush.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/shell/hush.c b/shell/hush.c
index d249dd59e..9e13e4a2e 100644
--- a/shell/hush.c
+++ b/shell/hush.c
@@ -481,6 +481,13 @@ struct nofork_save_area nofork_save;
 static sigjmp_buf nofork_jb;
 static struct pipe *nofork_pipe;
 
+static void handler_ctrl_c(int sig)
+{
+	debug_jobs_printf("got sig %d\n", sig);
+// as usual we can have all kinds of nasty problems with leaked malloc data here
+	siglongjmp(nofork_jb, 1);
+}
+
 static void handler_ctrl_z(int sig)
 {
 	pid_t pid;
@@ -1503,6 +1510,7 @@ static int run_single_fg_nofork(struct pipe *pi, const struct bb_applet *a,
 	save_nofork_data(&nofork_save);
 	if (sigsetjmp(nofork_jb, 1) == 0) {
 		signal_SA_RESTART(SIGTSTP, handler_ctrl_z);
+		signal(SIGINT, handler_ctrl_c);
 		rcode = run_nofork_applet_prime(&nofork_save, a, argv);
 		if (--nofork_save.saved != 0) {
 			/* Ctrl-Z forked, we are child */
@@ -1510,12 +1518,16 @@ static int run_single_fg_nofork(struct pipe *pi, const struct bb_applet *a,
 		}
 		return rcode;
 	}
-	/* Ctrl-Z forked, we are parent.
+	/* Ctrl-Z forked, we are parent; or Ctrl-C.
 	 * Sighandler has longjmped us here */
+	signal(SIGINT, SIG_IGN);
 	signal(SIGTSTP, SIG_IGN);
 	debug_jobs_printf("Exiting nofork early\n");
 	restore_nofork_data(&nofork_save);
-	insert_bg_job(pi);
+	if (nofork_save.saved == 0) /* Ctrl-Z, not Ctrl-C */
+		insert_bg_job(pi);
+	else
+		putchar('\n'); /* bash does this on Ctrl-C */
 	return 0;
 }
 
@@ -2467,7 +2479,7 @@ static int done_command(struct p_context *ctx)
 	prog = pi->progs + pi->num_progs;
 	memset(prog, 0, sizeof(*prog));
 	/*prog->redirects = NULL;*/
-	/*prog->argv = NULL;
+	/*prog->argv = NULL; */
 	/*prog->is_stopped = 0;*/
 	/*prog->group = NULL;*/
 	/*prog->glob_result.gl_pathv = NULL;*/
-- 
cgit v1.2.3-55-g6feb