Add lua repl interface to redbean

You can now interact with the global web server state on the command
line, which the web server is running. This supports Emacs shortcuts
with history, readline parity, <tab> completions, plus hints. Enjoy!
This commit is contained in:
Justine Tunney 2022-04-16 20:29:08 -07:00
parent f6b6204b9e
commit a6b02ce5a6
24 changed files with 848 additions and 463 deletions

View file

@ -36,13 +36,23 @@ textwindows int ioctl_tcgets_nt(int ignored, struct termios *tio) {
if (inok | outok) {
bzero(tio, sizeof(*tio));
if (inok) {
if (inmode & kNtEnableLineInput) tio->c_lflag |= ICANON;
if (inmode & kNtEnableEchoInput) tio->c_lflag |= ECHO;
if (inmode & kNtEnableProcessedInput) tio->c_lflag |= IEXTEN | ISIG;
if (inmode & kNtEnableLineInput) {
tio->c_lflag |= ICANON;
}
if (inmode & kNtEnableEchoInput) {
tio->c_lflag |= ECHO;
}
if (inmode & kNtEnableProcessedInput) {
tio->c_lflag |= IEXTEN | ISIG;
}
}
if (outok) {
if (outmode & kNtEnableProcessedOutput) tio->c_oflag |= OPOST;
if (!(outmode & kNtDisableNewlineAutoReturn)) tio->c_oflag |= ONLCR;
if (outmode & kNtEnableProcessedOutput) {
tio->c_oflag |= OPOST;
}
if (!(outmode & kNtDisableNewlineAutoReturn)) {
tio->c_oflag |= ONLCR;
}
}
return 0;
} else {

View file

@ -23,11 +23,10 @@
#include "libc/calls/termios.internal.h"
#include "libc/dce.h"
#include "libc/intrin/asan.internal.h"
#include "libc/intrin/nomultics.internal.h"
#include "libc/sysv/consts/termios.h"
#include "libc/sysv/errfuns.h"
extern bool __nomultics;
int ioctl_tcsets_nt(int, uint64_t, const struct termios *);
static int ioctl_tcsets_metal(int fd, uint64_t request,
@ -85,7 +84,9 @@ int ioctl_tcsets(int fd, uint64_t request, ...) {
rc = einval();
}
if (rc != -1) {
__nomultics = !(tio->c_oflag & OPOST);
if (__nomultics == 0 || __nomultics == 1) {
__nomultics = !(tio->c_oflag & OPOST);
}
}
STRACE("ioctl_tcsets(%d, %p, %p) → %d% m", fd, request, tio, rc);
return rc;

View file

@ -20,7 +20,11 @@
#include "libc/calls/internal.h"
#include "libc/calls/strace.internal.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/intrin/asan.internal.h"
#include "libc/intrin/describeflags.internal.h"
#include "libc/intrin/kprintf.h"
#include "libc/macros.internal.h"
#include "libc/sock/internal.h"
#include "libc/sock/sock.h"
#include "libc/sysv/errfuns.h"
@ -62,8 +66,9 @@
* @norestart
*/
int poll(struct pollfd *fds, size_t nfds, int timeout_ms) {
int rc;
int i, rc;
uint64_t millis;
if (IsAsan() && !__asan_is_valid(fds, nfds * sizeof(struct pollfd))) {
rc = efault();
} else if (!IsWindows()) {
@ -76,6 +81,23 @@ int poll(struct pollfd *fds, size_t nfds, int timeout_ms) {
millis = timeout_ms;
rc = sys_poll_nt(fds, nfds, &millis);
}
STRACE("poll(%p, %'lu, %'d) → %d% lm", fds, nfds, timeout_ms, rc);
#if defined(SYSDEBUG) && _POLLTRACE
if (__strace > 0) {
if (rc == -1 && errno == EFAULT) {
STRACE("poll(%p, %'lu, %'d) → %d% lm", fds, nfds, timeout_ms, rc);
} else {
kprintf(STRACE_PROLOGUE "poll({");
for (i = 0; i < MIN(5, nfds); ++i) {
kprintf("%s{%d,%s,%s}", i ? ", " : "", fds[i].fd,
DescribePollFlags(fds[i].events),
DescribePollFlags(fds[i].revents));
}
kprintf("%s}, %'zu, %'d) → %d% lm%n", i == 5 ? "..." : "", nfds,
timeout_ms, rc);
}
}
#endif
return rc;
}

View file

@ -23,6 +23,32 @@
/**
* Obtains the termios struct.
*
* Here are the general defaults you can expect across platforms:
*
* c_iflag = ICRNL IXON
* c_oflag = OPOST ONLCR NL0 CR0 TAB0 BS0 VT0 FF0
* c_cflag = ISIG CREAD CS8
* c_lflag = ISIG ICANON ECHO ECHOE ECHOK IEXTEN ECHOCTL ECHOKE
* c_ispeed = 38400
* c_ospeed = 38400
* c_cc[VINTR] = CTRL-C
* c_cc[VQUIT] = CTRL-\ # ignore this comment
* c_cc[VERASE] = CTRL-?
* c_cc[VKILL] = CTRL-U
* c_cc[VEOF] = CTRL-D
* c_cc[VTIME] = CTRL-@
* c_cc[VMIN] = CTRL-A
* c_cc[VSTART] = CTRL-Q
* c_cc[VSTOP] = CTRL-S
* c_cc[VSUSP] = CTRL-Z
* c_cc[VEOL] = CTRL-@
* c_cc[VSWTC] = CTRL-@
* c_cc[VREPRINT] = CTRL-R
* c_cc[VDISCARD] = CTRL-O
* c_cc[VWERASE] = CTRL-W
* c_cc[VLNEXT] = CTRL-V
* c_cc[VEOL2] = CTRL-@
*
* @param fd open file descriptor that isatty()
* @param tio is where result is stored
* @return -1 w/ errno on error

View file

@ -24,6 +24,7 @@
#include "libc/fmt/fmts.h"
#include "libc/fmt/internal.h"
#include "libc/fmt/itoa.h"
#include "libc/intrin/nomultics.internal.h"
#include "libc/macros.internal.h"
#include "libc/mem/mem.h"
#include "libc/nexgen32e/bsr.h"
@ -40,9 +41,6 @@
} \
} while (0)
extern bool __nomultics;
extern bool __replmode;
static const char kSpecialFloats[2][2][4] = {{"INF", "inf"}, {"NAN", "nan"}};
static void __fmt_free_dtoa(char **mem) {

View file

@ -29,6 +29,7 @@
#include "libc/intrin/asancodes.h"
#include "libc/intrin/kprintf.h"
#include "libc/intrin/lockcmpxchg.h"
#include "libc/intrin/nomultics.internal.h"
#include "libc/log/backtrace.internal.h"
#include "libc/log/internal.h"
#include "libc/log/libfatal.internal.h"
@ -153,7 +154,6 @@ struct ReportOriginHeap {
};
bool __asan_noreentry;
extern bool __nomultics;
static struct AsanMorgue __asan_morgue;
static wontreturn void __asan_unreachable(void) {

View file

@ -14,6 +14,7 @@ const char *DescribeFlags(char *, size_t, struct DescribeFlags *, size_t,
const char *DescribeMapFlags(int);
const char *DescribeProtFlags(int);
const char *DescribePollFlags(int);
const char *DescribeRemapFlags(int);
const char *DescribeNtPageFlags(uint32_t);

View file

@ -0,0 +1,41 @@
/*-*- 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 2022 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/intrin/describeflags.internal.h"
#include "libc/macros.internal.h"
#include "libc/nt/enum/filemapflags.h"
#include "libc/sysv/consts/poll.h"
const char *DescribePollFlags(int x) {
const struct DescribeFlags kPollFlags[] = {
{POLLIN, "IN"}, // order matters
{POLLOUT, "OUT"}, // order matters
{POLLPRI, "PRI"}, //
{POLLHUP, "HUP"}, //
{POLLERR, "ERR"}, //
{POLLNVAL, "NVAL"}, //
{POLLRDBAND, "RDBAND"}, //
{POLLRDHUP, "RDHUP"}, //
{POLLRDNORM, "RDNORM"}, //
{POLLWRBAND, "WRBAND"}, //
{POLLWRNORM, "WRNORM"}, //
};
static char pollflags[64];
return DescribeFlags(pollflags, sizeof(pollflags), kPollFlags,
ARRAYLEN(kPollFlags), "POLL", x);
}

View file

@ -29,6 +29,7 @@
#include "libc/intrin/cmpxchg.h"
#include "libc/intrin/kprintf.h"
#include "libc/intrin/lockcmpxchg.h"
#include "libc/intrin/nomultics.internal.h"
#include "libc/intrin/spinlock.h"
#include "libc/limits.h"
#include "libc/macros.internal.h"
@ -54,8 +55,6 @@ struct Timestamps {
};
extern int __pid;
extern bool __replmode;
extern bool __nomultics;
unsigned long long __kbirth; // see fork-nt.c
privileged static struct Timestamps kenter(void) {

View file

@ -26,5 +26,13 @@
*
* @see kprintf()
*/
bool __nomultics;
bool __replmode;
char __nomultics;
/**
* Controls ANSI prefix for log emissions.
*
* This should be true in raw tty mode repls.
*
* @see kprintf(), vflogf(), linenoise()
*/
char __replmode;

View file

@ -0,0 +1,11 @@
#ifndef COSMOPOLITAN_LIBC_INTRIN_NOMULTICS_INTERNAL_H_
#define COSMOPOLITAN_LIBC_INTRIN_NOMULTICS_INTERNAL_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
extern bool __nomultics;
extern bool __replmode;
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_INTRIN_NOMULTICS_INTERNAL_H_ */

View file

@ -4,6 +4,7 @@
#include "libc/calls/struct/rusage.h"
#include "libc/calls/struct/sigset.h"
#include "libc/calls/struct/winsize.h"
#include "libc/errno.h"
#include "libc/nexgen32e/stackframe.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
@ -200,22 +201,26 @@ extern unsigned __log_level; /* log level for runtime check */
#define LOGIFNEG1(FORM) \
({ \
int e = errno; \
autotype(FORM) Ax = (FORM); \
if (UNLIKELY(Ax == (typeof(Ax))(-1)) && LOGGABLE(kLogWarn)) { \
++g_ftrace; \
__logerrno(__FILE__, __LINE__, #FORM); \
--g_ftrace; \
errno = e; \
} \
Ax; \
})
#define LOGIFNULL(FORM) \
({ \
int e = errno; \
autotype(FORM) Ax = (FORM); \
if (Ax == NULL && LOGGABLE(kLogWarn)) { \
++g_ftrace; \
__logerrno(__FILE__, __LINE__, #FORM); \
--g_ftrace; \
errno = e; \
} \
Ax; \
})

View file

@ -54,7 +54,7 @@ void vflogf_onfail(FILE *f) {
fseek(f, SEEK_SET, 0);
f->beg = f->end = 0;
clearerr(f);
(fprintf)(f, "performed emergency log truncation: %s\n", strerror(err));
(fprintf)(f, "performed emergency log truncation: %s%n", strerror(err));
}
}
@ -104,7 +104,7 @@ void(vflogf)(unsigned level, const char *file, int line, FILE *f,
vflogf_onfail(f);
}
(vfprintf)(f, fmt, va);
fputs("\n", f);
fprintf(f, "%n");
if (bufmode == _IOLBF) {
f->bufmode = _IOLBF;
fflush(f);
@ -113,7 +113,7 @@ void(vflogf)(unsigned level, const char *file, int line, FILE *f,
__start_fatal(file, line);
strcpy(buf32, "unknown");
gethostname(buf32, sizeof(buf32));
(dprintf)(STDERR_FILENO, "fatality %s pid %d\n", buf32, getpid());
(dprintf)(STDERR_FILENO, "fatality %s pid %d%n", buf32, getpid());
__die();
unreachable;
}

View file

@ -19,8 +19,12 @@
#include "libc/calls/calls.h"
#include "libc/calls/strace.internal.h"
#include "libc/calls/struct/sigset.h"
#include "libc/calls/struct/termios.h"
#include "libc/calls/termios.h"
#include "libc/calls/ttydefaults.h"
#include "libc/dce.h"
#include "libc/dns/dns.h"
#include "libc/errno.h"
#include "libc/intrin/describeflags.internal.h"
#include "libc/intrin/kprintf.h"
#include "libc/macros.internal.h"
@ -44,6 +48,7 @@
#include "libc/sysv/consts/f.h"
#include "libc/sysv/consts/poll.h"
#include "libc/sysv/consts/sig.h"
#include "libc/sysv/consts/termios.h"
#include "tool/decode/lib/idname.h"
#include "tool/decode/lib/x86idnames.h"
@ -133,11 +138,13 @@ textstartup void __printargs(const char *prologue) {
uintptr_t *auxp;
struct utsname uts;
char path[PATH_MAX];
int x, st, ft, flags;
struct termios termios;
int e, x, st, ft, flags;
struct pollfd pfds[128];
struct AuxiliaryValue *auxinfo;
st = __strace, __strace = 0;
ft = g_ftrace, g_ftrace = 0;
e = errno;
PRINT("");
PRINT("SYSTEM");
@ -327,6 +334,143 @@ textstartup void __printargs(const char *prologue) {
PRINT("MEMTRACK");
PrintMemoryIntervals(2, &_mmi);
PRINT("");
PRINT("TERMIOS");
for (i = 0; i < 2; ++i) {
if (!tcgetattr(i, &termios)) {
PRINT(" - stdin");
kprintf(prologue);
kprintf(" c_iflag =");
if (termios.c_iflag & IGNBRK) kprintf(" IGNBRK");
if (termios.c_iflag & BRKINT) kprintf(" BRKINT");
if (termios.c_iflag & IGNPAR) kprintf(" IGNPAR");
if (termios.c_iflag & PARMRK) kprintf(" PARMRK");
if (termios.c_iflag & INPCK) kprintf(" INPCK");
if (termios.c_iflag & ISTRIP) kprintf(" ISTRIP");
if (termios.c_iflag & INLCR) kprintf(" INLCR");
if (termios.c_iflag & IGNCR) kprintf(" IGNCR");
if (termios.c_iflag & ICRNL) kprintf(" ICRNL");
if (termios.c_iflag & IXON) kprintf(" IXON");
if (termios.c_iflag & IXANY) kprintf(" IXANY");
if (termios.c_iflag & IXOFF) kprintf(" IXOFF");
if (termios.c_iflag & IMAXBEL) kprintf(" IMAXBEL");
if (termios.c_iflag & IUTF8) kprintf(" IUTF8");
if (termios.c_iflag & IUCLC) kprintf(" IUCLC");
kprintf("%n");
kprintf(prologue);
kprintf(" c_oflag =");
if (termios.c_oflag & OPOST) kprintf(" OPOST");
if (termios.c_oflag & ONLCR) kprintf(" ONLCR");
if (termios.c_oflag & OCRNL) kprintf(" OCRNL");
if (termios.c_oflag & ONOCR) kprintf(" ONOCR");
if (termios.c_oflag & ONLRET) kprintf(" ONLRET");
if (termios.c_oflag & OFILL) kprintf(" OFILL");
if (termios.c_oflag & OFDEL) kprintf(" OFDEL");
if (termios.c_oflag & OLCUC) kprintf(" OLCUC");
if ((termios.c_oflag & NLDLY) == NL0) {
kprintf(" NL0");
} else if ((termios.c_oflag & NLDLY) == NL1) {
kprintf(" NL1");
} else if ((termios.c_oflag & NLDLY) == NL2) {
kprintf(" NL2");
} else if ((termios.c_oflag & NLDLY) == NL3) {
kprintf(" NL3");
}
if ((termios.c_oflag & CRDLY) == CR0) {
kprintf(" CR0");
} else if ((termios.c_oflag & CRDLY) == CR1) {
kprintf(" CR1");
} else if ((termios.c_oflag & CRDLY) == CR2) {
kprintf(" CR2");
} else if ((termios.c_oflag & CRDLY) == CR3) {
kprintf(" CR3");
}
if ((termios.c_oflag & TABDLY) == TAB0) {
kprintf(" TAB0");
} else if ((termios.c_oflag & TABDLY) == TAB1) {
kprintf(" TAB1");
} else if ((termios.c_oflag & TABDLY) == TAB2) {
kprintf(" TAB2");
} else if ((termios.c_oflag & TABDLY) == TAB3) {
kprintf(" TAB3");
}
if ((termios.c_oflag & BSDLY) == BS0) {
kprintf(" BS0");
} else if ((termios.c_oflag & BSDLY) == BS1) {
kprintf(" BS1");
}
if ((termios.c_oflag & VTDLY) == VT0) {
kprintf(" VT0");
} else if ((termios.c_oflag & VTDLY) == VT1) {
kprintf(" VT1");
}
if ((termios.c_oflag & FFDLY) == FF0) {
kprintf(" FF0");
} else if ((termios.c_oflag & FFDLY) == FF1) {
kprintf(" FF1");
}
kprintf("%n");
kprintf(prologue);
kprintf(" c_cflag =");
if (termios.c_cflag & ISIG) kprintf(" ISIG");
if (termios.c_cflag & CSTOPB) kprintf(" CSTOPB");
if (termios.c_cflag & CREAD) kprintf(" CREAD");
if (termios.c_cflag & PARENB) kprintf(" PARENB");
if (termios.c_cflag & PARODD) kprintf(" PARODD");
if (termios.c_cflag & HUPCL) kprintf(" HUPCL");
if (termios.c_cflag & CLOCAL) kprintf(" CLOCAL");
if ((termios.c_cflag & CSIZE) == CS5) {
kprintf(" CS5");
} else if ((termios.c_cflag & CSIZE) == CS6) {
kprintf(" CS6");
} else if ((termios.c_cflag & CSIZE) == CS7) {
kprintf(" CS7");
} else if ((termios.c_cflag & CSIZE) == CS8) {
kprintf(" CS8");
}
kprintf("%n");
kprintf(prologue);
kprintf(" c_lflag =");
if (termios.c_lflag & ISIG) kprintf(" ISIG");
if (termios.c_lflag & ICANON) kprintf(" ICANON");
if (termios.c_lflag & ECHO) kprintf(" ECHO");
if (termios.c_lflag & ECHOE) kprintf(" ECHOE");
if (termios.c_lflag & ECHOK) kprintf(" ECHOK");
if (termios.c_lflag & ECHONL) kprintf(" ECHONL");
if (termios.c_lflag & NOFLSH) kprintf(" NOFLSH");
if (termios.c_lflag & TOSTOP) kprintf(" TOSTOP");
if (termios.c_lflag & IEXTEN) kprintf(" IEXTEN");
if (termios.c_lflag & ECHOCTL) kprintf(" ECHOCTL");
if (termios.c_lflag & ECHOPRT) kprintf(" ECHOPRT");
if (termios.c_lflag & ECHOKE) kprintf(" ECHOKE");
if (termios.c_lflag & FLUSHO) kprintf(" FLUSHO");
if (termios.c_lflag & PENDIN) kprintf(" PENDIN");
if (termios.c_lflag & XCASE) kprintf(" XCASE");
kprintf("%n");
PRINT(" c_ispeed = %u", termios.c_ispeed);
PRINT(" c_ospeed = %u", termios.c_ospeed);
PRINT(" c_cc[VINTR] = CTRL-%c", CTRL(termios.c_cc[VINTR]));
PRINT(" c_cc[VQUIT] = CTRL-%c", CTRL(termios.c_cc[VQUIT]));
PRINT(" c_cc[VERASE] = CTRL-%c", CTRL(termios.c_cc[VERASE]));
PRINT(" c_cc[VKILL] = CTRL-%c", CTRL(termios.c_cc[VKILL]));
PRINT(" c_cc[VEOF] = CTRL-%c", CTRL(termios.c_cc[VEOF]));
PRINT(" c_cc[VTIME] = CTRL-%c", CTRL(termios.c_cc[VTIME]));
PRINT(" c_cc[VMIN] = CTRL-%c", CTRL(termios.c_cc[VMIN]));
PRINT(" c_cc[VSTART] = CTRL-%c", CTRL(termios.c_cc[VSTART]));
PRINT(" c_cc[VSTOP] = CTRL-%c", CTRL(termios.c_cc[VSTOP]));
PRINT(" c_cc[VSUSP] = CTRL-%c", CTRL(termios.c_cc[VSUSP]));
PRINT(" c_cc[VEOL] = CTRL-%c", CTRL(termios.c_cc[VEOL]));
PRINT(" c_cc[VSWTC] = CTRL-%c", CTRL(termios.c_cc[VSWTC]));
PRINT(" c_cc[VREPRINT] = CTRL-%c", CTRL(termios.c_cc[VREPRINT]));
PRINT(" c_cc[VDISCARD] = CTRL-%c", CTRL(termios.c_cc[VDISCARD]));
PRINT(" c_cc[VWERASE] = CTRL-%c", CTRL(termios.c_cc[VWERASE]));
PRINT(" c_cc[VLNEXT] = CTRL-%c", CTRL(termios.c_cc[VLNEXT]));
PRINT(" c_cc[VEOL2] = CTRL-%c", CTRL(termios.c_cc[VEOL2]));
} else {
PRINT(" - tcgetattr(%d) failed %m", i);
}
}
if (IsWindows()) {
struct NtStartupInfo startinfo;
GetStartupInfo(&startinfo);
@ -400,4 +544,5 @@ textstartup void __printargs(const char *prologue) {
PRINT("");
__strace = st;
g_ftrace = ft;
errno = e;
}

View file

@ -26,6 +26,7 @@
#include "libc/errno.h"
#include "libc/fmt/fmt.h"
#include "libc/intrin/kprintf.h"
#include "libc/intrin/nomultics.internal.h"
#include "libc/log/libfatal.internal.h"
#include "libc/macros.internal.h"
#include "libc/nexgen32e/bsr.h"
@ -86,7 +87,6 @@ struct WinArgs {
};
extern int __pid;
extern bool __nomultics;
extern uint32_t __winmainpid;
extern int64_t __wincrashearly;
extern const char kConsoleHandles[3];

View file

@ -22,6 +22,6 @@ bool startswithi(const char *s, const char *prefix) {
for (;;) {
if (!*prefix) return true;
if (!*s) return false;
if (kToLower[*s++ & 255] != (*prefix++ & 255)) return false;
if (kToLower[*s++ & 255] != kToLower[*prefix++ & 255]) return false;
}
}