cosmopolitan/dsp/tty/ttyraw.c

164 lines
5.6 KiB
C
Raw Normal View History

2020-06-15 14:18:57 +00:00
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
2020-06-15 14:18:57 +00:00
Copyright 2020 Justine Alexandra Roberts Tunney
2020-12-28 01:18:44 +00:00
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
2020-06-15 14:18:57 +00:00
2020-12-28 01:18:44 +00:00
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
2020-06-15 14:18:57 +00:00
*/
#include "dsp/tty/tty.h"
#include "libc/assert.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/sigaction.h"
#include "libc/calls/struct/siginfo.h"
#include "libc/calls/termios.h"
#include "libc/calls/ucontext.h"
#include "libc/log/log.h"
#include "libc/macros.internal.h"
#include "libc/mem/gc.h"
2020-06-15 14:18:57 +00:00
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/fileno.h"
#include "libc/sysv/consts/sa.h"
#include "libc/sysv/consts/sig.h"
#include "libc/sysv/errfuns.h"
#include "libc/x/x.h"
#include "libc/x/xsigaction.h"
2020-06-15 14:18:57 +00:00
/* TODO(jart): DELETE */
2020-06-15 14:18:57 +00:00
#define FD STDOUT_FILENO
static struct TtyRaw {
bool setup;
bool hasold;
2020-06-15 14:18:57 +00:00
bool noreentry;
bool initialized;
enum TtyRawFlags flags;
sigaction_f next[10];
unsigned char sigs[10];
struct termios old;
} g_ttyraw;
static textstartup int ttyraw_setup(void) {
struct termios *old;
if (isatty(FD)) {
old = !g_ttyraw.hasold ? &g_ttyraw.old : NULL;
if (ttyconfig(FD, ttysetrawmode, g_ttyraw.flags, old) != -1) {
g_ttyraw.hasold = true;
return 0;
}
2020-06-15 14:18:57 +00:00
}
return -1;
2020-06-15 14:18:57 +00:00
}
static textstartup int ttyraw_enable(void) {
int rc;
g_ttyraw.setup = (rc = ttyraw_setup()) != -1 || g_ttyraw.setup;
return rc;
}
static textstartup void ttyraw_hidecursor(void) {
if (!g_ttyraw.setup)
return;
if (g_ttyraw.flags & kTtyCursor)
return;
2020-06-15 14:18:57 +00:00
ttyhidecursor(FD);
}
static textexit int ttyraw_disable(void) {
if (!g_ttyraw.setup)
return 0;
2020-06-15 14:18:57 +00:00
ttyshowcursor(FD);
return ttyrestore(FD, &g_ttyraw.old);
}
static textexit void ttyraw_onexit(void) {
ttyraw_disable();
}
static relegated void ttyraw_onsig(int sig, struct siginfo *info,
struct ucontext *ctx) {
size_t i;
if (g_ttyraw.noreentry)
_Exit(128 + sig);
2020-06-15 14:18:57 +00:00
g_ttyraw.noreentry = true;
if (g_ttyraw.flags != -1) {
if (sig == SIGCONT) {
ttyraw_enable();
} else {
ttyraw_disable();
}
}
for (i = 0; i < ARRAYLEN(g_ttyraw.sigs); ++i) {
if (g_ttyraw.sigs[i] == sig) {
2022-09-05 15:43:52 +00:00
if (g_ttyraw.next[i] != (void *)SIG_IGN) {
if (g_ttyraw.next[i] != (void *)SIG_DFL) {
if (g_ttyraw.next[i]) {
g_ttyraw.next[i](sig, info, ctx);
}
2020-06-15 14:18:57 +00:00
} else if (sig != SIGCONT) {
_Exit(128 + sig);
}
}
break;
}
}
g_ttyraw.noreentry = false;
}
static textstartup void ttyraw_initsig(int sig, unsigned flags, unsigned mask) {
static unsigned i;
struct sigaction old;
g_ttyraw.next[i] = xsigaction(sig, ttyraw_onsig, flags, mask, &old) != -1
? old.sa_sigaction
2022-09-05 15:43:52 +00:00
: (void *)SIG_DFL;
2020-06-15 14:18:57 +00:00
g_ttyraw.sigs[i++] = sig;
}
static textstartup void ttyraw_init(void) {
unsigned crashmask = ~(SIGILL | SIGBUS | SIGSEGV | SIGABRT);
ttyraw_initsig(SIGILL, SA_SIGINFO | SA_NODEFER | SA_ONSTACK, crashmask);
ttyraw_initsig(SIGSEGV, SA_SIGINFO | SA_NODEFER | SA_ONSTACK, crashmask);
ttyraw_initsig(SIGBUS, SA_SIGINFO | SA_NODEFER | SA_ONSTACK, crashmask);
ttyraw_initsig(SIGABRT, SA_SIGINFO | SA_NODEFER, crashmask);
ttyraw_initsig(SIGFPE, SA_SIGINFO | SA_RESTART, crashmask);
ttyraw_initsig(SIGTRAP, SA_SIGINFO | SA_RESTART, crashmask);
ttyraw_initsig(SIGHUP, SA_SIGINFO | SA_RESTART, crashmask);
ttyraw_initsig(SIGINT, SA_SIGINFO | SA_RESTART, crashmask);
ttyraw_initsig(SIGQUIT, SA_SIGINFO | SA_RESTART, crashmask);
ttyraw_initsig(SIGCONT, SA_SIGINFO | SA_RESTART, crashmask);
atexit(ttyraw_onexit);
}
/**
* Sets raw mode, safely, the easy way.
*
* This should be called after your signal handlers have been installed.
*/
textstartup int ttyraw(enum TtyRawFlags flags) {
int rc;
if ((g_ttyraw.flags = flags) != -1) {
if (!g_ttyraw.initialized) {
g_ttyraw.initialized = true;
ttyraw_init();
}
if ((rc = ttyraw_enable()) != -1) {
ttyraw_hidecursor();
}
} else {
rc = ttyraw_disable();
}
return rc;
}