Skip to content

Commit

Permalink
Ensure ^C gets printed to Windows console
Browse files Browse the repository at this point in the history
  • Loading branch information
jart committed Nov 22, 2024
1 parent e228aa3 commit fd15b2d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 deletions.
6 changes: 0 additions & 6 deletions libc/calls/BUILD.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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 $<

Expand Down
1 change: 1 addition & 0 deletions libc/calls/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
10 changes: 7 additions & 3 deletions libc/calls/read-nt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 6 additions & 0 deletions libc/intrin/BUILD.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions libc/intrin/sig.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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__

Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit fd15b2d

Please sign in to comment.