Do code cleanup use duff device linenoise i/o

This commit is contained in:
Justine Tunney 2022-04-22 18:55:28 -07:00
parent 6ff46ca373
commit 2f56ebfe78
79 changed files with 1393 additions and 1484 deletions

View file

@ -1,6 +1,9 @@
#define lua_c
#include "libc/calls/calls.h"
#include "libc/calls/sigbits.h"
#include "libc/intrin/kprintf.h"
#include "libc/intrin/nomultics.internal.h"
#include "libc/intrin/spinlock.h"
#include "libc/log/check.h"
#include "libc/runtime/gc.h"
#include "libc/runtime/runtime.h"
@ -13,8 +16,13 @@
#include "third_party/lua/lualib.h"
// clang-format off
bool lua_repl_blocking;
bool lua_repl_isterminal;
_Alignas(64) char lualock;
struct linenoiseState *lua_repl_linenoise;
static lua_State *globalL;
static const char *g_progname;
static const char *g_historypath;
/*
** {==================================================================
@ -111,14 +119,35 @@ static int incomplete (lua_State *L, int status) {
/*
** Prompt the user, read a line, and push it into the Lua stack.
*/
static int pushline (lua_State *L, int firstline) {
static ssize_t pushline (lua_State *L, int firstline) {
char *b;
size_t l;
ssize_t rc;
char *prmt;
globalL = L;
const char *prmt = get_prompt(L, firstline);
if (!(b = linenoiseWithHistory(prmt, g_progname)))
return 0; /* no input (prompt will be popped by caller) */
lua_pop(L, 1); /* remove prompt */
if (lua_repl_isterminal) {
prmt = strdup(get_prompt(L, firstline));
lua_pop(L, 1); /* remove prompt */
_spunlock(&lualock);
rc = linenoiseEdit(lua_repl_linenoise, prmt, &b, !firstline || lua_repl_blocking);
free(prmt);
if (rc != -1) {
if (b && *b) {
linenoiseHistoryLoad(g_historypath);
linenoiseHistoryAdd(b);
linenoiseHistorySave(g_historypath);
}
}
_spinlock(&lualock);
} else {
_spunlock(&lualock);
b = linenoiseGetLine(stdin);
_spinlock(&lualock);
rc = b ? 1 : -1;
}
if (rc == -1 || (!rc && !b)) {
return rc;
}
l = strlen(b);
if (l > 0 && b[l-1] == '\n') /* line ends with newline? */
b[--l] = '\0'; /* remove it */
@ -164,9 +193,10 @@ static void lstop (lua_State *L, lua_Debug *ar) {
static int multiline (lua_State *L) {
for (;;) { /* repeat until gets a complete statement */
size_t len;
ssize_t rc;
const char *line = lua_tolstring(L, 1, &len); /* get what it has */
int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */
if (!incomplete(L, status) || !pushline(L, 0)) {
if (!incomplete(L, status) || pushline(L, 0) != 1) {
return status; /* cannot or should not try to add continuation line */
}
lua_pushliteral(L, "\n"); /* add newline... */
@ -176,11 +206,38 @@ static int multiline (lua_State *L) {
}
void lua_initrepl(const char *progname) {
void lua_initrepl(lua_State *L, const char *progname) {
const char *prompt;
_spinlock(&lualock);
g_progname = progname;
linenoiseSetCompletionCallback(lua_readline_completions);
linenoiseSetHintsCallback(lua_readline_hint);
linenoiseSetFreeHintsCallback(free);
if ((lua_repl_isterminal = linenoiseIsTerminal())) {
linenoiseSetCompletionCallback(lua_readline_completions);
linenoiseSetHintsCallback(lua_readline_hint);
linenoiseSetFreeHintsCallback(free);
prompt = get_prompt(L, 1);
if ((g_historypath = linenoiseGetHistoryPath(progname))) {
if (linenoiseHistoryLoad(g_historypath) == -1) {
kprintf("%r%s: failed to load history: %m%n", g_historypath);
free(g_historypath);
g_historypath = 0;
}
}
lua_repl_linenoise = linenoiseBegin(prompt, 0, 1);
lua_pop(L, 1); /* remove prompt */
__nomultics = 2;
__replmode = true;
}
_spunlock(&lualock);
}
void lua_freerepl(void) {
_spinlock(&lualock);
__nomultics = false;
__replmode = false;
linenoiseEnd(lua_repl_linenoise);
free(g_historypath);
_spunlock(&lualock);
}
@ -189,16 +246,24 @@ void lua_initrepl(const char *progname) {
** adding "return " in front of it) and second as a statement. Return
** the final status of load/call with the resulting function (if any)
** in the top of the stack.
**
** returns -1 on eof
** returns -2 on error
*/
int lua_loadline (lua_State *L) {
ssize_t rc;
int status;
lua_settop(L, 0);
if (!pushline(L, 1))
return -1; /* no input */
_spinlock(&lualock);
if ((rc = pushline(L, 1)) != 1) {
_spunlock(&lualock);
return rc - 1; /* eof or error */
}
if ((status = addreturn(L)) != LUA_OK) /* 'return ...' did not work? */
status = multiline(L); /* try as command, maybe with continuation lines */
lua_remove(L, 1); /* remove line from the stack */
lua_assert(lua_gettop(L) == 1);
_spunlock(&lualock);
return status;
}

View file

@ -5,9 +5,15 @@
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
extern char lualock;
extern bool lua_repl_blocking;
extern bool lua_repl_isterminal;
extern struct linenoiseState *lua_repl_linenoise;
void lua_freerepl(void);
int lua_loadline(lua_State *);
void lua_l_print(lua_State *);
void lua_initrepl(const char *);
void lua_initrepl(lua_State *, const char *);
int lua_report(lua_State *, int);
int lua_runchunk(lua_State *, int, int);
void lua_l_message(const char *, const char *);

View file

@ -10,14 +10,19 @@
#include "libc/calls/sigbits.h"
#include "libc/calls/struct/sigaction.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/intrin/kprintf.h"
#include "libc/log/log.h"
#include "libc/runtime/gc.h"
#include "libc/runtime/stack.h"
#include "libc/sock/sock.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/exit.h"
#include "libc/sysv/consts/poll.h"
#include "libc/sysv/consts/sa.h"
#include "libc/x/x.h"
#include "third_party/linenoise/linenoise.h"
#include "third_party/lua/cosmo.h"
#include "third_party/lua/lauxlib.h"
#include "third_party/lua/lprefix.h"
#include "third_party/lua/lrepl.h"
@ -275,8 +280,27 @@ static void doREPL (lua_State *L) {
int status;
const char *oldprogname = progname;
progname = NULL; /* no 'progname' on errors in interactive mode */
lua_initrepl(LUA_PROGNAME);
while ((status = lua_loadline(L)) != -1) {
lua_initrepl(L, LUA_PROGNAME);
for (;;) {
linenoiseEnableRawMode(0);
TryAgain:
status = lua_loadline(L);
if (status == -2 && errno == EAGAIN) {
errno = 0;
poll(&(struct pollfd){0, POLLIN}, 1, -1);
goto TryAgain;
}
linenoiseDisableRawMode();
if (status == -1) {
break;
} else if (status == -2) {
lua_pushfstring(L, "read error: %s", strerror(errno));
lua_report(L, status);
lua_freerepl();
progname = oldprogname;
return;
}
lua_writeline();
if (status == LUA_OK)
status = lua_runchunk(L, 0, LUA_MULTRET);
if (status == LUA_OK) {
@ -285,6 +309,7 @@ static void doREPL (lua_State *L) {
lua_report(L, status);
}
}
lua_freerepl();
lua_settop(L, 0); /* clear stack */
lua_writeline();
progname = oldprogname;
@ -343,7 +368,7 @@ static int pmain (lua_State *L) {
int main (int argc, char **argv) {
int status, result;
lua_State *L;
if (!IsModeDbg()) {
if (IsModeDbg()) {
ShowCrashReports();
}
/* if (IsModeDbg()) ShowCrashReports(); */