Improve linenoise and get it working on Windows

Some progress has been made on introducing completion but there's been
difficulties using the Python C API to get local shell variables.
This commit is contained in:
Justine Tunney 2021-08-15 00:05:27 -07:00
parent 968474d291
commit 5029e20bef
23 changed files with 408 additions and 209 deletions

View file

@ -41,7 +41,8 @@ textwindows int ioctl_tcgets_nt(int ignored, struct termios *tio) {
if (inmode & kNtEnableProcessedInput) tio->c_lflag |= IEXTEN | ISIG;
}
if (outok) {
if (inmode & kNtEnableProcessedInput) tio->c_lflag |= IEXTEN | ISIG;
if (outmode & kNtEnableProcessedOutput) tio->c_oflag |= OPOST;
if (!(outmode & kNtDisableNewlineAutoReturn)) tio->c_oflag |= ONLCR;
}
return 0;
} else {

View file

@ -51,8 +51,8 @@ textwindows int ioctl_tcsets_nt(int ignored, uint64_t request,
}
if (outok) {
outmode |= kNtEnableWrapAtEolOutput;
outmode |= kNtEnableProcessedOutput;
if (!(tio->c_oflag & OPOST)) outmode |= kNtDisableNewlineAutoReturn;
if (tio->c_oflag & OPOST) outmode |= kNtEnableProcessedOutput;
if (!(tio->c_oflag & ONLCR)) outmode |= kNtDisableNewlineAutoReturn;
if (NtGetVersion() >= kNtVersionWindows10) {
outmode |= kNtEnableVirtualTerminalProcessing;
}

View file

@ -102,6 +102,7 @@ ssize_t readansi(int fd, char *buf, size_t size) {
break;
case kCsi:
switch (c) {
case '[':
case ':':
case ';':
case '<':

View file

@ -19,6 +19,7 @@
#include "libc/bits/bits.h"
#include "libc/dce.h"
#include "libc/log/backtrace.internal.h"
#include "libc/log/internal.h"
#include "libc/log/log.h"
/**
@ -29,6 +30,7 @@
relegated wontreturn void __die(void) {
static bool once;
if (cmpxchg(&once, false, true)) {
__restore_tty();
if (IsDebuggerPresent(false)) DebugBreak();
ShowBacktrace(2, NULL);
}

View file

@ -2,16 +2,19 @@
#define COSMOPOLITAN_LIBC_LOG_INTERNAL_H_
#include "libc/calls/struct/sigaction.h"
#include "libc/calls/struct/siginfo.h"
#include "libc/calls/struct/termios.h"
#include "libc/calls/ucontext.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
extern int kCrashSigs[8] hidden;
extern struct termios g_oldtermios hidden;
extern struct sigaction g_oldcrashacts[8] hidden;
void __start_fatal(const char *, int) hidden;
void __start_fatal_ndebug(void) hidden;
void __oncrash(int, struct siginfo *, struct ucontext *) relegated;
void __restore_tty(void);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */

45
libc/log/oldtermios.c Normal file
View file

@ -0,0 +1,45 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2021 Justine Alexandra Roberts Tunney
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.
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.
*/
#include "libc/calls/calls.h"
#include "libc/calls/termios.h"
#include "libc/log/internal.h"
#include "libc/str/str.h"
#define SHOW_CURSOR "\e[?25h"
#define DISABLE_MOUSE "\e[?1000;1002;1015;1006l"
#define ANSI_RESTORE SHOW_CURSOR DISABLE_MOUSE
struct termios g_oldtermios;
static textstartup void g_oldtermios_init() {
if (isatty(1)) {
tcgetattr(1, &g_oldtermios);
}
}
const void *const g_oldtermios_ctor[] initarray = {
g_oldtermios_init,
};
void __restore_tty(void) {
if (isatty(1)) {
write(1, ANSI_RESTORE, strlen(ANSI_RESTORE));
tcsetattr(1, TCSAFLUSH, &g_oldtermios);
}
}

View file

@ -20,7 +20,9 @@
#include "libc/calls/sigbits.h"
#include "libc/calls/struct/sigaction.h"
#include "libc/calls/struct/siginfo.h"
#include "libc/calls/struct/termios.h"
#include "libc/calls/struct/utsname.h"
#include "libc/calls/termios.h"
#include "libc/calls/ucontext.h"
#include "libc/dce.h"
#include "libc/errno.h"
@ -41,6 +43,7 @@
#include "libc/sysv/consts/fileno.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/sig.h"
#include "libc/sysv/consts/termios.h"
/**
* @fileoverview Abnormal termination handling & GUI debugging.
@ -262,6 +265,7 @@ relegated void __oncrash(int sig, struct siginfo *si, ucontext_t *ctx) {
: 0);
}
if (gdbpid > 0 && (sig == SIGTRAP || sig == SIGQUIT)) return;
__restore_tty();
ShowCrashReport(err, STDERR_FILENO, sig, si, ctx);
exit(128 + sig);
unreachable;

View file

@ -20,6 +20,7 @@
#include "libc/bits/bits.h"
#include "libc/calls/calls.h"
#include "libc/calls/sigbits.h"
#include "libc/calls/termios.h"
#include "libc/calls/typedef/sigaction_f.h"
#include "libc/dce.h"
#include "libc/log/check.h"

View file

@ -32,6 +32,7 @@
relegated void __start_fatal(const char *file, int line) {
bool colorful;
char s[16 + 16 + 16 + 16 + PATH_MAX + 16 + NAME_MAX + 16], *p = s;
__restore_tty();
colorful = cancolor();
*p++ = '\r';
if (colorful) p = stpcpy(p, "\e[J\e[30;101m");

View file

@ -29,6 +29,7 @@
*/
relegated void __start_fatal_ndebug(void) {
char s[16 + 16 + 16 + 16 + PATH_MAX + 16], *p = s;
__restore_tty();
*p++ = '\r';
if (cancolor()) p = stpcpy(p, "\e[J");
p = stpcpy(p, "error:");

View file

@ -45,10 +45,11 @@ static bool have_getrandom;
* This random number seed generator blends information from:
*
* - getrandom() on Linux
* - RtlGenRandom() on Windows
* - getentropy() on XNU and OpenBSD
* - sysctl(KERN_ARND) on FreeBSD and NetBSD
* - RDSEED on Broadwell+ and Xen+ unless GRND_NORDRND
* - RDRAND on Ivybridge+ and Xen+ unless GRND_NORDRND|GRND_RANDOM
* - RDRAND on Ivybridge+ and Xen+ unless GRND_NORDRND
*
* The following flags may be specified:
*
@ -75,7 +76,9 @@ ssize_t getrandom(void *p, size_t n, unsigned f) {
sigset_t neu, old;
if (n > 256) n = 256;
if (!IsTiny() &&
(f & ~(GRND_RANDOM | GRND_NONBLOCK | GRND_NORDRND | GRND_NOSYSTEM))) {
((f & ~(GRND_RANDOM | GRND_NONBLOCK | GRND_NORDRND | GRND_NOSYSTEM)) ||
(f & (GRND_NORDRND | GRND_NOSYSTEM)) ==
(GRND_NORDRND | GRND_NOSYSTEM))) {
return einval();
}
if (!(f & GRND_NOSYSTEM)) {

View file

@ -23,8 +23,8 @@
/**
* Fills memory with random bytes, e.g.
*
* char buf[1024];
* rngset(buf, sizeof(buf), rand64, -1);
* char buf[1024];
* rngset(buf, sizeof(buf), rand64, -1);
*
* @param seed can be rand64() and is always called at least once
* @param reseed is bytes between seed() calls and -1 disables it

View file

@ -53,6 +53,7 @@ void *xunbinga(size_t, const char16_t *) attributeallocalign((1)) _XMAL _XRET;
void *xunbing(const char16_t *) _XMAL _XRET;
char16_t *utf8toutf16(const char *, size_t, size_t *) nodiscard;
char *utf16toutf8(const char16_t *, size_t, size_t *) nodiscard;
char *xhomedir(void) nodiscard;
/*───────────────────────────────────────────────────────────────────────────│─╗
cosmopolitan § eXtended apis » files

43
libc/x/xhomedir.c Normal file
View file

@ -0,0 +1,43 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2021 Justine Alexandra Roberts Tunney
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.
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.
*/
#include "libc/dce.h"
#include "libc/runtime/runtime.h"
#include "libc/x/x.h"
/**
* Returns home directory.
*/
char *xhomedir(void) {
int fd;
const char *a, *b;
if ((a = getenv("HOME"))) {
b = "";
} else if (IsWindows()) {
a = getenv("HOMEDRIVE");
b = getenv("HOMEPATH");
if (!a || !b) {
a = "C:";
b = "";
}
} else {
a = ".";
b = "";
}
return xasprintf("%s%s", a, b);
}