diff --git a/libc/calls/BUILD.mk b/libc/calls/BUILD.mk index ea3b8b75d..75ac5a00a 100644 --- a/libc/calls/BUILD.mk +++ b/libc/calls/BUILD.mk @@ -216,12 +216,6 @@ o//libc/calls/writev.o: private \ -mgeneral-regs-only # these assembly files are safe to build on aarch64 -o/$(MODE)/libc/calls/getcontext.o: libc/calls/getcontext.S - @$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) -c $< -o/$(MODE)/libc/calls/swapcontext.o: libc/calls/swapcontext.S - @$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) -c $< -o/$(MODE)/libc/calls/tailcontext.o: libc/calls/tailcontext.S - @$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) -c $< o/$(MODE)/libc/calls/stackjump.o: libc/calls/stackjump.S @$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) -c $< diff --git a/libc/calls/internal.h b/libc/calls/internal.h index 010215788..1529418a8 100644 --- a/libc/calls/internal.h +++ b/libc/calls/internal.h @@ -30,6 +30,7 @@ int CountConsoleInputBytes(void); int FlushConsoleInputBytes(void); int64_t GetConsoleInputHandle(void); int64_t GetConsoleOutputHandle(void); +void EchoConsoleNt(const char *, size_t, bool); int IsWindowsExecutable(int64_t, const char16_t *); void InterceptTerminalCommands(const char *, size_t); diff --git a/libc/calls/read-nt.c b/libc/calls/read-nt.c index c4da9d610..9ac353a63 100644 --- a/libc/calls/read-nt.c +++ b/libc/calls/read-nt.c @@ -320,9 +320,12 @@ textwindows static int ProcessKeyEvent(const struct NtInputRecord *r, char *p) { // note we define _POSIX_VDISABLE as zero // tcsetattr() lets anyone reconfigure these keybindings if (c && !(__ttyconf.magic & kTtyNoIsigs) && !__keystroke.bypass_mode) { + char b[] = {c}; if (c == __ttyconf.vintr) { + EchoConsoleNt(b, 1, false); return AddSignal(SIGINT); } else if (c == __ttyconf.vquit) { + EchoConsoleNt(b, 1, false); return AddSignal(SIGQUIT); } } @@ -457,7 +460,8 @@ textwindows static void WriteCtl(const char *p, size_t n, bool escape_harder) { } } -textwindows static void EchoTty(const char *p, size_t n, bool escape_harder) { +textwindows void EchoConsoleNt(const char *p, size_t n, bool escape_harder) { + InitConsole(); if (!(__ttyconf.magic & kTtySilence)) { if (__ttyconf.magic & kTtyEchoRaw) { WriteTty(p, n); @@ -517,7 +521,7 @@ textwindows static void IngestConsoleInputRecord(struct NtInputRecord *r) { memcpy(k->buf, buf, sizeof(k->buf)); k->buflen = len; dll_make_last(&__keystroke.line, &k->elem); - EchoTty(buf, len, true); + EchoConsoleNt(buf, len, true); if (!__keystroke.freekeys) { dll_make_last(&__keystroke.list, __keystroke.line); __keystroke.line = 0; @@ -616,7 +620,7 @@ textwindows static void IngestConsoleInputRecord(struct NtInputRecord *r) { // echo input if it was successfully recorded // assuming the win32 console isn't doing it already - EchoTty(buf, len, false); + EchoConsoleNt(buf, len, false); // save keystroke to appropriate list if (__ttyconf.magic & kTtyUncanon) { diff --git a/libc/intrin/BUILD.mk b/libc/intrin/BUILD.mk index dfef72d12..824c0edaf 100644 --- a/libc/intrin/BUILD.mk +++ b/libc/intrin/BUILD.mk @@ -119,6 +119,12 @@ o/$(MODE)/libc/intrin/windowstimetotimespec.o: private \ -O2 # these assembly files are safe to build on aarch64 +o/$(MODE)/libc/intrin/getcontext.o: libc/intrin/getcontext.S + @$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) -c $< +o/$(MODE)/libc/intrin/swapcontext.o: libc/intrin/swapcontext.S + @$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) -c $< +o/$(MODE)/libc/intrin/tailcontext.o: libc/intrin/tailcontext.S + @$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) -c $< o/$(MODE)/libc/intrin/aarch64/%.o: libc/intrin/aarch64/%.S @$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) -c $< o/$(MODE)/libc/intrin/fenv.o: libc/intrin/fenv.S diff --git a/libc/intrin/sig.c b/libc/intrin/sig.c index 7292b6701..3303a8378 100644 --- a/libc/intrin/sig.c +++ b/libc/intrin/sig.c @@ -19,6 +19,7 @@ #include "libc/sysv/consts/sig.h" #include "ape/sections.internal.h" #include "libc/calls/calls.h" +#include "libc/calls/internal.h" #include "libc/calls/sig.internal.h" #include "libc/calls/state.internal.h" #include "libc/calls/struct/sigaction.h" @@ -34,6 +35,7 @@ #include "libc/intrin/describebacktrace.h" #include "libc/intrin/dll.h" #include "libc/intrin/maps.h" +#include "libc/intrin/nomultics.h" #include "libc/intrin/strace.h" #include "libc/intrin/weaken.h" #include "libc/nt/console.h" @@ -54,6 +56,7 @@ #include "libc/sysv/consts/sa.h" #include "libc/sysv/consts/sicode.h" #include "libc/sysv/consts/ss.h" +#include "libc/sysv/consts/termios.h" #include "libc/thread/posixthread.internal.h" #ifdef __x86_64__ @@ -622,12 +625,32 @@ static textwindows int __sig_console_sig(uint32_t dwCtrlType) { } } +static textwindows int __sig_console_char(uint32_t dwCtrlType) { + switch (dwCtrlType) { + case kNtCtrlCEvent: + return __ttyconf.vintr; + case kNtCtrlBreakEvent: + return __ttyconf.vquit; + default: + return _POSIX_VDISABLE; + } +} + __msabi textwindows dontinstrument bool32 __sig_console(uint32_t dwCtrlType) { // win32 launches a thread to deliver ctrl-c and ctrl-break when typed // it only happens when kNtEnableProcessedInput is in play on console. // otherwise we need to wait until read-nt.c discovers that keystroke. struct CosmoTib tls; __bootstrap_tls(&tls, __builtin_frame_address(0)); + + // ensure that ^C or ^\ gets printed to console appropriately + if (_weaken(EchoConsoleNt)) { + char c; + if ((c = __sig_console_char(dwCtrlType)) != _POSIX_VDISABLE) + _weaken(EchoConsoleNt)(&c, sizeof(c), false); + } + + // take control of random thread and inject call to signal handler __sig_generate(__sig_console_sig(dwCtrlType), SI_KERNEL); return true; }