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

@ -103,22 +103,21 @@ static void AppendArg(struct Args *l, char *s) {
static void MakeHeader(struct Header *h, const char *name, int ref, int mode,
int size) {
size_t n;
char buf[24];
memset(h, ' ', sizeof(*h));
n = strlen(name);
memcpy(h->name, name, n);
if (ref != -1) {
memcpy(h->name + n, buf, uint64toarray_radix10(ref, buf));
FormatUint32(h->name + n, ref);
}
if (strcmp(name, "//") != 0) {
h->date[0] = '0';
h->uid[0] = '0';
h->gid[0] = '0';
memcpy(h->mode, buf, uint64toarray_radix8(mode & 0777, buf));
FormatOctal32(h->mode, mode & 0777, false);
}
h->fmag[0] = '`';
h->fmag[1] = '\n';
memcpy(h->size, buf, uint64toarray_radix10(size, buf));
FormatUint32(h->size, size);
}
int main(int argc, char *argv[]) {

View file

@ -1271,7 +1271,7 @@ static void DrawXmm(struct Panel *p, long i, long r) {
uint64toarray_fixed16(ival, buf, xmmtype.size[r] * 8);
}
} else {
int64toarray_radix10(SignExtend(ival, xmmtype.size[r] * 8), buf);
FormatInt64(buf, SignExtend(ival, xmmtype.size[r] * 8));
}
break;
default:
@ -2270,7 +2270,8 @@ static void OnVidyaServiceTeletypeOutput(void) {
char buf[12];
n = 0 /* FormatCga(m->bx[0], buf) */;
w = tpenc(VidyaServiceXlatTeletype(m->ax[0]));
do buf[n++] = w;
do
buf[n++] = w;
while ((w >>= 8));
PtyWrite(pty, buf, n);
}

View file

@ -30,9 +30,9 @@ size_t FormatCga(uint8_t bgfg, char buf[hasatleast 11]) {
char *p = buf;
*p++ = '\e';
*p++ = '[';
p += uint64toarray_radix10(kCgaToAnsi[(bgfg & 0xF0) >> 4] + 10, p);
p = FormatUint32(p, kCgaToAnsi[(bgfg & 0xF0) >> 4] + 10);
*p++ = ';';
p += uint64toarray_radix10(kCgaToAnsi[bgfg & 0x0F], p);
p = FormatUint32(p, kCgaToAnsi[bgfg & 0x0F]);
*p++ = 'm';
*p = '\0';
return p - buf;

View file

@ -128,7 +128,7 @@ static char *DisRegisterWord(struct Dis *d, uint32_t rde, char *p, bool g,
static char *DisInt(char *p, int64_t x) {
if (-15 <= x && x <= 15) {
p += int64toarray_radix10(x, p);
p = FormatInt64(p, x);
} else if (x == INT64_MIN) {
p = stpcpy(p, "-0x");
p += uint64toarray_radix16(INT64_MIN, p);
@ -319,7 +319,8 @@ static char *DisRegMem(struct Dis *d, uint32_t rde, char *p,
}
static dontinline char *DisE(struct Dis *d, uint32_t rde, char *p,
char *f(struct Dis *, uint32_t, char *, bool, int)) {
char *f(struct Dis *, uint32_t, char *, bool,
int)) {
if (IsModrmRegister(rde)) {
return f(d, rde, p, Rexb(rde), ModrmRm(rde));
} else {
@ -471,13 +472,13 @@ static char *DisOne(struct Dis *d, uint32_t rde, char *p) {
static char *DisJbs(struct Dis *d, uint32_t rde, char *p) {
if (d->xedd->op.disp > 0) *p++ = '+';
p += int64toarray_radix10(d->xedd->op.disp, p);
p = FormatInt64(p, d->xedd->op.disp);
return p;
}
static char *DisJb(struct Dis *d, uint32_t rde, char *p) {
if (d->xedd->op.disp > 0) *p++ = '+';
p += uint64toarray_radix10(d->xedd->op.disp & 0xff, p);
p = FormatUint32(p, d->xedd->op.disp & 0xff);
return p;
}
@ -522,7 +523,7 @@ static char *DisXmm(struct Dis *d, uint32_t rde, char *p, const char *s,
p = HighStart(p, g_high.reg);
*p++ = '%';
p = stpcpy(p, s);
p += uint64toarray_radix10(reg, p);
p = FormatUint32(p, reg);
p = HighEnd(p);
return p;
}

View file

@ -25,7 +25,7 @@ struct High g_high;
char *HighStart(char *p, int h) {
if (h) {
p = stpcpy(p, "\e[38;5;");
p += uint64toarray_radix10(h, p);
p = FormatUint32(p, h);
p = stpcpy(p, "m");
g_high.active = true;
}

View file

@ -693,9 +693,9 @@ static void PtyReportCursorPosition(struct Pty *pty) {
p = buf;
*p++ = '\e';
*p++ = '[';
p += uint64toarray_radix10((pty->y + 1) & 0x7fff, p);
p = FormatInt32(p, (pty->y + 1) & 0x7fff);
*p++ = ';';
p += uint64toarray_radix10((pty->x + 1) & 0x7fff, p);
p = FormatInt32(p, (pty->x + 1) & 0x7fff);
*p++ = 'R';
PtyWriteInput(pty, buf, p - buf);
}
@ -1178,7 +1178,8 @@ ssize_t PtyWriteInput(struct Pty *pty, const void *data, size_t n) {
m = pty->input.n;
if (i + n * 2 + 1 > m) {
m = MAX(m, 8);
do m += m >> 1;
do
m += m >> 1;
while (i + n * 2 + 1 > m);
if (!(p = realloc(p, m))) {
return -1;
@ -1229,18 +1230,18 @@ ssize_t PtyRead(struct Pty *pty, void *buf, size_t size) {
static char *PtyEncodeRgb(char *p, int rgb) {
*p++ = '2';
*p++ = ';';
p += uint64toarray_radix10((rgb & 0x0000ff) >> 000, p);
p = FormatUint32(p, (rgb & 0x0000ff) >> 000);
*p++ = ';';
p += uint64toarray_radix10((rgb & 0x00ff00) >> 010, p);
p = FormatUint32(p, (rgb & 0x00ff00) >> 010);
*p++ = ';';
p += uint64toarray_radix10((rgb & 0xff0000) >> 020, p);
p = FormatUint32(p, (rgb & 0xff0000) >> 020);
return p;
}
static char *PtyEncodeXterm256(char *p, int xt) {
*p++ = '5';
*p++ = ';';
p += uint64toarray_radix10(xt, p);
p = FormatUint32(p, xt);
return p;
}

View file

@ -661,7 +661,7 @@ static const char *GetErrnoName(int x) {
const char *s;
static char buf[16];
if ((s = strerror_short(x))) return s;
int64toarray_radix10(x, buf);
FormatInt64(buf, x);
return buf;
}

View file

@ -58,7 +58,7 @@ void PrintClosure(struct Closure *c, const char *name, int indent, FILE *f) {
fputs(": ", f);
Print(c->term, 0, GetDepth(c->envp), f);
fputs(" +", f);
int64toarray_radix10(c->refs, ibuf);
FormatInt64(ibuf, c->refs);
fputs(ibuf, f);
fputc('\n', f);
PrintClosure(c->envp, "envp", indent + 1, f);

View file

@ -632,7 +632,7 @@ void PrintVar(int i, FILE* f) {
char ibuf[22];
switch (style) {
case 0:
int64toarray_radix10(i, ibuf);
FormatInt64(ibuf, i);
fputs(ibuf, f);
break;
case 1:
@ -642,7 +642,7 @@ void PrintVar(int i, FILE* f) {
fputwc(FREEBIES[~i], f);
} else {
ibuf[0] = '?';
int64toarray_radix10(i, ibuf + 1);
FormatInt64(ibuf + 1, i);
fputs(ibuf, f);
}
break;
@ -889,7 +889,7 @@ void PrintDebruijn(int x, int head, int depth, FILE* f) {
PrintVar(mem[x + 1], f);
} else {
fputc(L'', f);
int64toarray_radix10(x, ibuf);
FormatInt64(ibuf, x);
fputs(ibuf, f);
}
} else if (mem[x] == IOP) {
@ -905,19 +905,19 @@ void PrintDebruijn(int x, int head, int depth, FILE* f) {
fputs(asciiname ? "gro" : "", f);
} else {
fputc(L'!', f);
int64toarray_radix10(x, ibuf);
FormatInt64(ibuf, x);
fputs(ibuf, f);
}
} else {
fputc(L'!', f);
int64toarray_radix10(x, ibuf);
FormatInt64(ibuf, x);
fputs(ibuf, f);
}
return;
}
Overflow:
fputc(L'', f);
int64toarray_radix10(x, ibuf);
FormatInt64(ibuf, x);
fputs(ibuf, f);
}
@ -1172,7 +1172,7 @@ void PrintLambda(int x, int head, int depth, int apps, FILE* f) {
PrintVar(depth - 1 - mem[x + 1], f);
} else {
fputc(L'', f);
int64toarray_radix10(x, ibuf);
FormatInt64(ibuf, x);
fputs(ibuf, f);
}
} else if (mem[x] == APP) {
@ -1198,12 +1198,12 @@ void PrintLambda(int x, int head, int depth, int apps, FILE* f) {
fputs(asciiname ? "gro" : "", f);
} else {
fputc(L'!', f);
int64toarray_radix10(x, ibuf);
FormatInt64(ibuf, x);
fputs(ibuf, f);
}
} else {
fputc(L'!', f);
int64toarray_radix10(x, ibuf);
FormatInt64(ibuf, x);
fputs(ibuf, f);
}
if (close) {
@ -1213,7 +1213,7 @@ void PrintLambda(int x, int head, int depth, int apps, FILE* f) {
}
Overflow:
fputc(L'', f);
int64toarray_radix10(x, ibuf);
FormatInt64(ibuf, x);
fputs(ibuf, f);
}
@ -1239,7 +1239,7 @@ void PrintBinary(int x, int head, int depth, FILE* f) {
PrintVar(mem[x + 1], f);
} else {
fputc(L'', f);
int64toarray_radix10(x, ibuf);
FormatInt64(ibuf, x);
fputs(ibuf, f);
}
} else if (mem[x] == APP) {
@ -1263,14 +1263,14 @@ void PrintBinary(int x, int head, int depth, FILE* f) {
fputwc(L'', f);
} else {
fputc(L'!', f);
int64toarray_radix10(x, ibuf);
FormatInt64(ibuf, x);
fputs(ibuf, f);
}
return;
}
Overflow:
fputc(L'', f);
int64toarray_radix10(x, ibuf);
FormatInt64(ibuf, x);
fputs(ibuf, f);
}

View file

@ -20,17 +20,17 @@ db:exec[[
INSERT INTO test (content) VALUES ('Hello Sqlite3');
]]
-- this intercepts all requests if it's defined
function OnHttpRequest()
if HasParam('magic') then
Write('<p>\r\n')
Write('OnHttpRequest() has intercepted your request<br>\r\n')
Write('because you specified the magic parameter\r\n')
Write('<pre>\r\n')
Write(EscapeHtml(LoadAsset('/.init.lua')))
Write('</pre>\r\n')
else
Route() -- this asks redbean to do the default thing
end
SetHeader('Server', 'redbean!')
end
-- -- this intercepts all requests if it's defined
-- function OnHttpRequest()
-- if HasParam('magic') then
-- Write('<p>\r\n')
-- Write('OnHttpRequest() has intercepted your request<br>\r\n')
-- Write('because you specified the magic parameter\r\n')
-- Write('<pre>\r\n')
-- Write(EscapeHtml(LoadAsset('/.init.lua')))
-- Write('</pre>\r\n')
-- else
-- Route() -- this asks redbean to do the default thing
-- end
-- SetHeader('Server', 'redbean!')
-- end

View file

@ -90,7 +90,7 @@ o/$(MODE)/tool/net/%.com.dbg: \
o/$(MODE)/tool/net/redbean.com.dbg: \
$(TOOL_NET_DEPS) \
o/$(MODE)/tool/net/redbean.o \
o/$(MODE)/tool/net/redbean.o \
o/$(MODE)/tool/net/lre.o \
o/$(MODE)/tool/net/lunix.o \
o/$(MODE)/tool/net/lmaxmind.o \
@ -208,7 +208,7 @@ o/$(MODE)/tool/net/demo/virtualbean.html.zip.o: \
o/$(MODE)/tool/net/redbean-demo.com.dbg: \
$(TOOL_NET_DEPS) \
o/$(MODE)/tool/net/redbean.o \
o/$(MODE)/tool/net/redbean.o \
o/$(MODE)/tool/net/lre.o \
o/$(MODE)/tool/net/lunix.o \
o/$(MODE)/tool/net/lmaxmind.o \

View file

@ -39,6 +39,7 @@
#include "libc/fmt/conv.h"
#include "libc/fmt/itoa.h"
#include "libc/intrin/nomultics.internal.h"
#include "libc/intrin/spinlock.h"
#include "libc/log/backtrace.internal.h"
#include "libc/log/check.h"
#include "libc/log/log.h"
@ -50,10 +51,12 @@
#include "libc/nexgen32e/bsf.h"
#include "libc/nexgen32e/bsr.h"
#include "libc/nexgen32e/crc32.h"
#include "libc/nexgen32e/nt2sysv.h"
#include "libc/nexgen32e/rdtsc.h"
#include "libc/nexgen32e/rdtscp.h"
#include "libc/nt/enum/fileflagandattributes.h"
#include "libc/nt/runtime.h"
#include "libc/nt/thread.h"
#include "libc/rand/rand.h"
#include "libc/runtime/clktck.h"
#include "libc/runtime/directmap.internal.h"
@ -362,6 +365,7 @@ static bool printport;
static bool daemonize;
static bool logrusage;
static bool logbodies;
static bool isterminal;
static bool sslcliused;
static bool loglatency;
static bool terminated;
@ -835,12 +839,13 @@ static void DescribeAddress(char buf[40], uint32_t addr, uint16_t port) {
char *p;
const char *s;
p = buf;
p += uint64toarray_radix10((addr & 0xFF000000) >> 030, p), *p++ = '.';
p += uint64toarray_radix10((addr & 0x00FF0000) >> 020, p), *p++ = '.';
p += uint64toarray_radix10((addr & 0x0000FF00) >> 010, p), *p++ = '.';
p += uint64toarray_radix10((addr & 0x000000FF) >> 000, p), *p++ = ':';
p += uint64toarray_radix10(port, p);
*p++ = '\0';
p = FormatUint64(p, (addr & 0xFF000000) >> 030), *p++ = '.';
p = FormatUint64(p, (addr & 0x00FF0000) >> 020), *p++ = '.';
p = FormatUint64(p, (addr & 0x0000FF00) >> 010), *p++ = '.';
p = FormatUint64(p, (addr & 0x000000FF) >> 000), *p++ = ':';
p = FormatUint64(p, port);
*p = '\0';
assert(p - buf < 40);
}
static inline void GetServerAddr(uint32_t *ip, uint16_t *port) {
@ -1029,7 +1034,7 @@ static void Daemonize(void) {
umask(0);
if (pidpath) {
fd = open(pidpath, O_CREAT | O_WRONLY, 0644);
WRITE(fd, ibuf, uint64toarray_radix10(getpid(), ibuf));
WRITE(fd, ibuf, FormatInt32(ibuf, getpid()) - ibuf);
close(fd);
}
if (!logpath) ProgramLogPath("/dev/null");
@ -1486,6 +1491,7 @@ static void CertsDestroy(void) {
static void WipeServingKeys(void) {
size_t i;
long double t = nowl();
if (uniprocess) return;
mbedtls_ssl_ticket_free(&ssltick);
mbedtls_ssl_key_cert_free(conf.key_cert), conf.key_cert = 0;
@ -2083,7 +2089,7 @@ static char *AppendExpires(char *p, int64_t t) {
static char *AppendCache(char *p, int64_t seconds) {
if (seconds < 0) return p;
p = stpcpy(p, "Cache-Control: max-age=");
p += uint64toarray_radix10(seconds, p);
p = FormatUint64(p, seconds);
if (seconds) {
p = stpcpy(p, ", public");
} else {
@ -2095,21 +2101,21 @@ static char *AppendCache(char *p, int64_t seconds) {
static inline char *AppendContentLength(char *p, size_t n) {
p = stpcpy(p, "Content-Length: ");
p += uint64toarray_radix10(n, p);
p = FormatUint64(p, n);
return AppendCrlf(p);
}
static char *AppendContentRange(char *p, long a, long b, long c) {
p = stpcpy(p, "Content-Range: bytes ");
if (a >= 0 && b > 0) {
p += uint64toarray_radix10(a, p);
p = FormatUint64(p, a);
*p++ = '-';
p += uint64toarray_radix10(a + b - 1, p);
p = FormatUint64(p, a + b - 1);
} else {
*p++ = '*';
}
*p++ = '/';
p += uint64toarray_radix10(c, p);
p = FormatUint64(p, c);
return AppendCrlf(p);
}
@ -6396,6 +6402,7 @@ static bool StreamResponse(char *p) {
static bool HandleMessageActual(void) {
int rc;
char *p;
long double now;
if ((rc = ParseHttpMessage(&msg, inbuf.p, amtread)) != -1) {
if (!rc) return false;
hdrsize = rc;
@ -6437,8 +6444,11 @@ static bool HandleMessageActual(void) {
LockInc(&shared->c.messageshandled);
++messageshandled;
if (loglatency || LOGGABLE(kLogDebug)) {
LOGF(kLogDebug, "(stat) %`'.*s latency %,ldµs", msg.uri.b - msg.uri.a,
inbuf.p + msg.uri.a, (long)((nowl() - startrequest) * 1e6L));
now = nowl();
LOGF(kLogDebug, "(stat) %`'.*s latency r: %,ldµs c: %,ldµs",
msg.uri.b - msg.uri.a, inbuf.p + msg.uri.a,
(long)((now - startrequest) * 1e6L),
(long)((now - startconnection) * 1e6L));
}
if (!generator) {
return TransmitResponse(p);
@ -6702,7 +6712,6 @@ static int EnableSandbox(void) {
}
}
// returns 0 otherwise -1 if worker needs to unwind stack and exit
static int HandleConnection(size_t i) {
int pid, rc = 0;
clientaddrsize = sizeof(clientaddr);
@ -6731,10 +6740,16 @@ static int HandleConnection(size_t i) {
__kbirth = rdtsc();
}
if (funtrace) {
ftrace_install();
if (ftrace_install() != -1) {
g_ftrace = 1;
} else {
WARNF("ftrace failed to install %m");
}
}
}
CHECK_NE(-1, EnableSandbox());
if (sandboxed) {
CHECK_NE(-1, EnableSandbox());
}
if (hasonworkerstart) {
CallSimpleHook("OnWorkerStart");
}
@ -6824,45 +6839,6 @@ static int HandleConnection(size_t i) {
return rc;
}
// returns 2 if we should stay in the redbean event loop
// returns 1 if poll() says stdin has user input available
// returns 0 if poll() timed out after ms
// returns -1 if worker is unwinding exit
static int HandlePoll(int ms) {
size_t i;
int nfds;
if ((nfds = poll(polls, 1 + servers.n, ms)) != -1) {
for (i = 0; i < servers.n; ++i) {
if (polls[1 + i].revents) {
serveraddr = &servers.p[i].addr;
ishandlingconnection = true;
if (HandleConnection(i) == -1) return -1;
ishandlingconnection = false;
}
}
// are we polling stdin for the repl?
if (polls[0].fd >= 0) {
if (polls[0].revents) {
return 1; // user entered a keystroke
} else if (!nfds) {
return 0; // let linenoise know it timed out
}
}
} else {
if (errno == EINTR || errno == EAGAIN) {
LockInc(&shared->c.pollinterrupts);
} else if (errno == ENOMEM) {
LockInc(&shared->c.enomems);
WARNF("(srvr) %s ran out of memory");
meltdown = true;
} else {
DIEF("(srvr) poll error: %m");
}
errno = 0;
}
return 2;
}
static void RestoreApe(void) {
char *p;
size_t n;
@ -6883,6 +6859,97 @@ static void RestoreApe(void) {
}
}
static int HandleReadline(void) {
int status;
for (;;) {
status = lua_loadline(GL);
if (status < 0) {
if (status == -1) {
OnTerm(SIGHUP); // eof
INFOF("got repl eof");
write(1, "\r\n", 2);
return -1;
} else if (errno == EINTR) {
errno = 0;
OnInt(SIGINT);
INFOF("got repl interrupt");
return 0;
} else if (errno == EAGAIN) {
errno = 0;
return 0;
} else {
OnTerm(SIGIO); // error
return -1;
}
}
write(1, "\r\n", 2);
linenoiseDisableRawMode();
_spinlock(&lualock);
if (status == LUA_OK) {
status = lua_runchunk(GL, 0, LUA_MULTRET);
}
if (status == LUA_OK) {
lua_l_print(GL);
} else {
lua_report(GL, status);
}
_spunlock(&lualock);
if (lua_repl_isterminal) {
linenoiseEnableRawMode(0);
}
}
}
static int HandlePoll(int ms) {
int rc, nfds;
size_t pollid, serverid;
if ((nfds = poll(polls, 1 + servers.n, ms)) != -1) {
if (nfds) {
// handle pollid/o events
for (pollid = 0; pollid < 1 + servers.n; ++pollid) {
if (!polls[pollid].revents) continue;
if (polls[pollid].fd < 0) continue;
if (polls[pollid].fd) {
// handle listen socket
serverid = pollid - 1;
assert(0 <= serverid && serverid < servers.n);
serveraddr = &servers.p[serverid].addr;
ishandlingconnection = true;
_spinlock(&lualock);
rc = HandleConnection(serverid);
_spunlock(&lualock);
ishandlingconnection = false;
if (rc == -1) return -1;
} else {
// handle standard input
rc = HandleReadline();
if (rc == -1) return rc;
}
}
} else if (__replmode) {
// handle refresh repl line
if (!IsWindows()) {
rc = HandleReadline();
if (rc < 0) return rc;
} else {
linenoiseRefreshLine(lua_repl_linenoise);
}
}
} else {
if (errno == EINTR || errno == EAGAIN) {
LockInc(&shared->c.pollinterrupts);
} else if (errno == ENOMEM) {
LockInc(&shared->c.enomems);
WARNF("(srvr) %s ran out of memory");
meltdown = true;
} else {
DIEF("(srvr) poll error: %m");
}
errno = 0;
}
return 0;
}
static void Listen(void) {
char ipbuf[16];
size_t i, j, n;
@ -6960,10 +7027,9 @@ static void HandleShutdown(void) {
}
// this function coroutines with linenoise
static int EventLoop(int fd, int ms) {
int rc;
static int EventLoop(int ms) {
long double t;
rc = -1;
VERBOSEF("EventLoop()");
while (!terminated) {
errno = 0;
if (zombied) {
@ -6977,54 +7043,47 @@ static int EventLoop(int fd, int ms) {
} else if ((t = nowl()) - lastheartbeat > HEARTBEAT / 1000.) {
lastheartbeat = t;
HandleHeartbeat();
} else if ((rc = HandlePoll(ms)) != 2) {
break; // return control to linenoise
} else if (HandlePoll(ms) == -1) {
break;
}
}
return rc;
return -1;
}
static void ReplEventLoop(void) {
int status;
long double t;
lua_State *L = GL;
DEBUGF("ReplEventLoop()");
polls[0].fd = 0;
__nomultics = 2;
__replmode = true;
lua_initrepl("redbean");
linenoiseSetPollCallback(EventLoop);
for (;;) {
if ((status = lua_loadline(L)) == -1) {
if (errno == EINTR) {
LockInc(&shared->c.pollinterrupts);
if (terminated) {
break;
} else {
continue;
}
} else {
break;
}
}
if (status == LUA_OK) {
status = lua_runchunk(L, 0, LUA_MULTRET);
}
if (status == LUA_OK) {
lua_l_print(L);
} else {
lua_report(L, status);
}
lua_initrepl(GL, "redbean");
if (lua_repl_isterminal) {
linenoiseEnableRawMode(0);
}
if (!terminated && !isexitingworker) {
OnTerm(SIGHUP); // eof event
}
lua_settop(L, 0); // clear stack
lua_writeline();
__replmode = false;
__nomultics = 0;
EventLoop(100);
linenoiseDisableRawMode();
lua_freerepl();
lua_settop(GL, 0); // clear stack
polls[0].fd = -1;
}
static uint32_t WindowsReplThread(void *arg) {
DEBUGF("WindowsReplThread()");
lua_repl_blocking = true;
lua_initrepl(GL, "redbean");
if (lua_repl_isterminal) {
linenoiseEnableRawMode(0);
}
for (;;) {
if (HandleReadline() == -1) {
break;
}
}
linenoiseDisableRawMode();
lua_freerepl();
_spinlock(&lualock);
lua_settop(GL, 0); // clear stack
_spunlock(&lualock);
return 0;
}
static void SigInit(void) {
xsigaction(SIGINT, OnInt, 0, 0, 0);
xsigaction(SIGHUP, OnHup, 0, 0, 0);
@ -7217,13 +7276,6 @@ void RedBean(int argc, char *argv[]) {
if (daemonize) {
Daemonize();
} else {
// xxx: create process group to make it easier to propagate SIGTERM
// to children. the downside to doing this seems to be that
// ctrl-c isn't propagating as expected when running redbean
// underneath strace.com :|
if (!IsWindows()) {
setpgrp();
}
if (logpath) {
close(2);
open(logpath, O_APPEND | O_WRONLY | O_CREAT, 0640);
@ -7240,14 +7292,18 @@ void RedBean(int argc, char *argv[]) {
isinitialized = true;
CallSimpleHookIfDefined("OnServerStart");
#ifdef STATIC
EventLoop(-1, HEARTBEAT);
EventLoop(HEARTBEAT);
#else
GetResolvConf(); // for effect
GetHostsTxt(); // for effect
if (!IsWindows() && isatty(0)) {
ReplEventLoop();
GetResolvConf(); // for effect
if (daemonize || !linenoiseIsTerminal()) {
EventLoop(HEARTBEAT);
} else if (IsWindows()) {
uint32_t tid;
CreateThread(0, 0, NT2SYSV(WindowsReplThread), 0, 0, &tid);
EventLoop(100);
} else {
EventLoop(-1, HEARTBEAT);
ReplEventLoop();
}
#endif
if (!isexitingworker) {

View file

@ -83,7 +83,7 @@ void bf(int fd) {
obuf[n++] = ';';
obuf[n++] = '5';
obuf[n++] = ';';
n += int64toarray_radix10(fg, obuf + n);
n = FormatInt64(obuf + n, fg) - (obuf + n);
obuf[n++] = 'm';
}
obuf[n++] = "0123456789abcdef"[c >> 4];

View file

@ -79,7 +79,7 @@ static const char *GetStorageSpecifier(const char *type, int *out_width,
static void EmitSection(long yn, long xn, int w, int arrayalign, int emit(),
void *a) {
char alignstr[21];
uint64toarray_radix10(arrayalign, alignstr);
FormatUint32(alignstr, arrayalign);
if (arrayalign <= 8 && yn * xn * w == 8) {
emit("\t.rodata.cst", a);
emit("8\n", a);
@ -108,8 +108,8 @@ void *FormatStringTableAsAssembly(long yn, long xn, const char *const T[yn][xn],
char ynstr[21], xnstr[21];
name = firstnonnull(name, "M");
storage = GetStorageSpecifier(firstnonnull(type, "long"), &w, &align);
uint64toarray_radix10(yn, ynstr);
uint64toarray_radix10(xn, xnstr);
FormatUint64(ynstr, yn);
FormatUint64(xnstr, xn);
EmitSection(yn, xn, w, GetArrayAlignment(yn, xn, w, align), emit, a);
emit(name, a);
emit(":", a);

View file

@ -24,8 +24,8 @@ void *FormatStringTableAsCode(long yn, long xn, const char *const T[yn][xn],
int emit(), void *arg, const char *type,
const char *name, const char *ignored) {
char ynstr[21], xnstr[21];
uint64toarray_radix10(yn, ynstr);
uint64toarray_radix10(xn, xnstr);
FormatUint64(ynstr, yn);
FormatUint64(xnstr, xn);
emit(type, arg);
emit(" ", arg);
emit(firstnonnull(name, "M"), arg);

View file

@ -341,7 +341,7 @@ static void AppendChar(char c) {
static void AppendInt(long x) {
char ibuf[21];
AppendData(ibuf, int64toarray_radix10(x, ibuf));
AppendData(ibuf, FormatInt64(ibuf, x) - ibuf);
}
/*───────────────────────────────────────────────────────────────────────────│─╗

View file

@ -743,7 +743,7 @@ static void Render(void) {
fg = InvertXtermGreyscale(fg);
}
p = stpcpy(p, "\e[38;5;");
p += int64toarray_radix10(fg, p);
p = FormatInt64(p, fg);
*p++ = 'm';
}
w = tpenc(kCp437[c]);
@ -769,14 +769,14 @@ static void Render(void) {
}
p = stpcpy(p, " memzoom\e[0m ");
if (!pid) {
p += uint64toarray_radix10(MIN(offset / (long double)size * 100, 100), p);
p = FormatUint32(p, MIN(offset / (long double)size * 100, 100));
p = stpcpy(p, "%-");
p += uint64toarray_radix10(
MIN((offset + ((tyn * txn) << zoom)) / (long double)size * 100, 100),
p);
p = FormatUint32(
p,
MIN((offset + ((tyn * txn) << zoom)) / (long double)size * 100, 100));
p = stpcpy(p, "% ");
}
p += uint64toarray_radix10(1L << zoom, p);
p = FormatUint32(p, 1L << zoom);
p = stpcpy(p, "x\e[J");
PreventBufferbloat();
for (i = 0, n = p - buffer; i < n; i += got) {
@ -910,10 +910,10 @@ static void GetOpts(int argc, char *argv[]) {
}
if (pid) {
p = stpcpy(path, "/proc/");
p += int64toarray_radix10(pid, p);
p = FormatInt64(p, pid);
stpcpy(p, "/mem");
p = stpcpy(mapspath, "/proc/");
p += int64toarray_radix10(pid, p);
p = FormatInt64(p, pid);
stpcpy(p, "/maps");
} else {
if (optind == argc) {

View file

@ -857,8 +857,8 @@ static void OpenVideo(void) {
plm_set_video_decode_callback(plm_, OnVideo, NULL);
plm_set_audio_decode_callback(plm_, OnAudio, NULL);
plm_set_loop(plm_, false);
int64toarray_radix10((chans_ = 2), chansstr_);
int64toarray_radix10((srate_ = plm_get_samplerate(plm_)), sratestr_);
FormatInt64(chansstr_, (chans_ = 2));
FormatInt64(sratestr_, (srate_ = plm_get_samplerate(plm_)));
if (plm_get_num_audio_streams(plm_) && OpenSpeaker()) {
plm_set_audio_enabled(plm_, true, 0);
} else {