From fdc69a4b0eadfefaf1247f868953a896aa73e2f2 Mon Sep 17 00:00:00 2001 From: Nguyễn Thái Ngọc Duy Date: Thu, 23 Sep 2010 10:14:53 +1000 Subject: win32: ash: implement ^C (and Ctrl+Break) ^C sometimes does not work while Ctrl+Break seems to always work --- BUGS | 3 +++ shell/ash.c | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/BUGS b/BUGS index 3be0808fd..a4bfbf2ac 100644 --- a/BUGS +++ b/BUGS @@ -10,6 +10,9 @@ ^Z can be used while ^D does not work +^C sometimes does not work (Ctrl-Break always works though). +This may have something to do with read_key()? + ** $HOME not set $HOMEPATH should probably be used diff --git a/shell/ash.c b/shell/ash.c index c3b497938..5c24762f2 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -4050,6 +4050,18 @@ sprint_status(char *s, int status, int sigonly) } #if ENABLE_PLATFORM_MINGW32 + +HANDLE hSIGINT; /* Ctrl-C is pressed */ + +static BOOL WINAPI ctrl_handler(DWORD dwCtrlType) +{ + if (dwCtrlType == CTRL_C_EVENT || dwCtrlType == CTRL_BREAK_EVENT) { + SetEvent(hSIGINT); + return TRUE; + } + return FALSE; +} + /* * Windows does not know about parent-child relationship * They don't support waitpid(-1) @@ -4081,7 +4093,9 @@ waitpid_child(int *status) LOOP(pid_nr++); if (!pid_nr) return -1; + pid_nr++; pidp = pidlist = ckmalloc(sizeof(*pidlist)*pid_nr); + *pidp++ = hSIGINT; LOOP(*pidp++ = (HANDLE)ps->ps_pid); #undef LOOP @@ -4090,6 +4104,15 @@ waitpid_child(int *status) free(pidlist); return -1; } + if (!idx) { /* hSIGINT */ + int i; + ResetEvent(hSIGINT); + for (i = 1; i < pid_nr; i++) + TerminateProcess(pidlist[i], 1); + free(pidlist); + *status = 260; /* terminated by a signal */ + return pidlist[1]; + } GetExitCodeProcess(pidlist[idx], &win_status); pid = (int)pidlist[idx]; free(pidlist); @@ -13593,6 +13616,8 @@ int ash_main(int argc UNUSED_PARAM, char **argv) setstackmark(&smark); #if ENABLE_PLATFORM_MINGW32 + hSIGINT = CreateEvent(NULL, TRUE, FALSE, NULL); + SetConsoleCtrlHandler(ctrl_handler, TRUE); if (argc == 3 && !strcmp(argv[1], "--forkshell")) { forkshell_init(argv[2]); -- cgit v1.2.3-55-g6feb