Update redbean lua example code

This commit is contained in:
Justine Tunney 2022-04-25 09:31:28 -07:00
parent 451e3f73d9
commit 72e9be5c20
7 changed files with 56 additions and 51 deletions

View file

@ -781,7 +781,9 @@ static ssize_t linenoiseRead(int fd, char *buf, size_t size,
if (l && gotwinch) refreshme = 1;
if (refreshme) linenoiseRefreshLine(l);
if (!block && linenoisePoll(l, fd) == -1) return -1;
--__strace;
rc = readansi(fd, buf, size);
++__strace;
if (rc == -1 && errno == EINTR) {
if (!block) break;
} else {
@ -1277,11 +1279,15 @@ StartOver:
}
void linenoiseRefreshLine(struct linenoiseState *l) {
--__strace;
linenoiseRefreshLineImpl(l, 0);
++__strace;
}
static void linenoiseRefreshLineForce(struct linenoiseState *l) {
--__strace;
linenoiseRefreshLineImpl(l, 1);
++__strace;
}
static void linenoiseEditInsert(struct linenoiseState *l, const char *p,

View file

@ -35,7 +35,7 @@ Write('<dt>unix.getcwd()\r\n')
Write('<dd>%s\r\n' % {EscapeHtml(unix.getcwd())})
function PrintResourceLimit(name, id)
soft, hard, errno = unix.getrlimit(id)
soft, errno, hard = unix.getrlimit(id)
Write('<dt>getrlimit(%s)\r\n' % {name})
if soft then
Write('<dd>')
@ -80,7 +80,7 @@ else
Write('%s\r\n' % {EscapeHtml(unix.strerrno(errno))})
end
errno, enabled = unix.getsockopt(GetClientFd(), unix.SOL_SOCKET, unix.SO_DEBUG)
enabled, errno = unix.getsockopt(GetClientFd(), unix.SOL_SOCKET, unix.SO_DEBUG)
Write('<dt>unix.getsockopt(GetClientFd(), unix.SOL_SOCKET, unix.SO_DEBUG)\r\n')
if errno then
Write('<dd>%s\r\n' % {EscapeHtml(unix.strerrno(errno))})
@ -88,7 +88,7 @@ else
Write('<dd>%s\r\n' % {enabled})
end
errno, enabled = unix.getsockopt(GetClientFd(), unix.SOL_SOCKET, unix.SO_ACCEPTCONN)
enabled, errno = unix.getsockopt(GetClientFd(), unix.SOL_SOCKET, unix.SO_ACCEPTCONN)
Write('<dt>unix.getsockopt(GetClientFd(), unix.SOL_SOCKET, unix.SO_ACCEPTCONN)\r\n')
if errno then
Write('<dd>%s\r\n' % {EscapeHtml(unix.strerrno(errno))})
@ -96,7 +96,7 @@ else
Write('<dd>%s\r\n' % {enabled})
end
errno, enabled = unix.getsockopt(GetClientFd(), unix.SOL_SOCKET, unix.SO_REUSEADDR)
enabled, errno = unix.getsockopt(GetClientFd(), unix.SOL_SOCKET, unix.SO_REUSEADDR)
Write('<dt>unix.getsockopt(GetClientFd(), unix.SOL_SOCKET, unix.SO_REUSEADDR)\r\n')
if errno then
Write('<dd>%s\r\n' % {EscapeHtml(unix.strerrno(errno))})
@ -104,7 +104,7 @@ else
Write('<dd>%s\r\n' % {enabled})
end
errno, enabled = unix.getsockopt(GetClientFd(), unix.SOL_SOCKET, unix.SO_REUSEPORT)
enabled, errno = unix.getsockopt(GetClientFd(), unix.SOL_SOCKET, unix.SO_REUSEPORT)
Write('<dt>unix.getsockopt(GetClientFd(), unix.SOL_SOCKET, unix.SO_REUSEPORT)\r\n')
if errno then
Write('<dd>%s\r\n' % {EscapeHtml(unix.strerrno(errno))})
@ -112,7 +112,7 @@ else
Write('<dd>%s\r\n' % {enabled})
end
errno, enabled = unix.getsockopt(GetClientFd(), unix.SOL_SOCKET, unix.SO_KEEPALIVE)
enabled, errno = unix.getsockopt(GetClientFd(), unix.SOL_SOCKET, unix.SO_KEEPALIVE)
Write('<dt>unix.getsockopt(GetClientFd(), unix.SOL_SOCKET, unix.SO_KEEPALIVE)\r\n')
if errno then
Write('<dd>%s\r\n' % {EscapeHtml(unix.strerrno(errno))})
@ -120,7 +120,7 @@ else
Write('<dd>%s\r\n' % {enabled})
end
errno, enabled = unix.getsockopt(GetClientFd(), unix.SOL_TCP, unix.TCP_NODELAY)
enabled, errno = unix.getsockopt(GetClientFd(), unix.SOL_TCP, unix.TCP_NODELAY)
Write('<dt>unix.getsockopt(GetClientFd(), unix.SOL_TCP, unix.TCP_NODELAY)\r\n')
if errno then
Write('<dd>%s\r\n' % {EscapeHtml(unix.strerrno(errno))})
@ -128,7 +128,7 @@ else
Write('<dd>%s\r\n' % {enabled})
end
errno, secs, micros = unix.getsockopt(GetClientFd(), unix.SOL_SOCKET, unix.SO_RCVTIMEO)
secs, errno, micros = unix.getsockopt(GetClientFd(), unix.SOL_SOCKET, unix.SO_RCVTIMEO)
Write('<dt>unix.getsockopt(GetClientFd(), unix.SOL_SOCKET, unix.SO_RCVTIMEO)\r\n')
if errno then
Write('<dd>%s\r\n' % {EscapeHtml(unix.strerrno(errno))})
@ -136,7 +136,7 @@ else
Write('<dd>%d sec %d µs\r\n' % {secs, micros})
end
errno, secs, micros = unix.getsockopt(GetClientFd(), unix.SOL_SOCKET, unix.SO_SNDTIMEO)
secs, errno, micros = unix.getsockopt(GetClientFd(), unix.SOL_SOCKET, unix.SO_SNDTIMEO)
Write('<dt>unix.getsockopt(GetClientFd(), unix.SOL_SOCKET, unix.SO_SNDTIMEO)\r\n')
if errno then
Write('<dd>%s\r\n' % {EscapeHtml(unix.strerrno(errno))})
@ -144,7 +144,7 @@ else
Write('<dd>%d sec %d µs\r\n' % {secs, micros})
end
errno, enabled = unix.getsockopt(GetClientFd(), unix.SOL_SOCKET, unix.SO_DONTROUTE)
enabled, errno = unix.getsockopt(GetClientFd(), unix.SOL_SOCKET, unix.SO_DONTROUTE)
Write('<dt>unix.getsockopt(GetClientFd(), unix.SOL_SOCKET, unix.SO_DONTROUTE)\r\n')
if errno then
Write('<dd>%s\r\n' % {EscapeHtml(unix.strerrno(errno))})
@ -152,7 +152,7 @@ else
Write('<dd>%s\r\n' % {enabled})
end
errno, bytes = unix.getsockopt(GetClientFd(), unix.SOL_SOCKET, unix.SO_SNDBUF)
bytes, errno = unix.getsockopt(GetClientFd(), unix.SOL_SOCKET, unix.SO_SNDBUF)
Write('<dt>unix.getsockopt(GetClientFd(), unix.SOL_SOCKET, unix.SO_SNDBUF)\r\n')
if errno then
Write('<dd>%s\r\n' % {EscapeHtml(unix.strerrno(errno))})
@ -160,7 +160,7 @@ else
Write('<dd>%d\r\n' % {bytes})
end
errno, bytes = unix.getsockopt(GetClientFd(), unix.SOL_SOCKET, unix.SO_RCVBUF)
bytes, errno = unix.getsockopt(GetClientFd(), unix.SOL_SOCKET, unix.SO_RCVBUF)
Write('<dt>unix.getsockopt(GetClientFd(), unix.SOL_SOCKET, unix.SO_RCVBUF)\r\n')
if errno then
Write('<dd>%s\r\n' % {EscapeHtml(unix.strerrno(errno))})
@ -168,7 +168,7 @@ else
Write('<dd>%d\r\n' % {bytes})
end
errno, bytes = unix.getsockopt(GetClientFd(), unix.SOL_TCP, unix.TCP_FASTOPEN)
bytes, errno = unix.getsockopt(GetClientFd(), unix.SOL_TCP, unix.TCP_FASTOPEN)
Write('<dt>unix.getsockopt(GetClientFd(), unix.SOL_TCP, unix.TCP_FASTOPEN)\r\n')
if errno then
Write('<dd>%s\r\n' % {EscapeHtml(unix.strerrno(errno))})
@ -176,7 +176,7 @@ else
Write('<dd>%d\r\n' % {bytes})
end
errno, enabled = unix.getsockopt(GetClientFd(), unix.SOL_SOCKET, unix.SO_BROADCAST)
enabled, errno = unix.getsockopt(GetClientFd(), unix.SOL_SOCKET, unix.SO_BROADCAST)
Write('<dt>unix.getsockopt(GetClientFd(), unix.SOL_SOCKET, unix.SO_BROADCAST)\r\n')
if errno then
Write('<dd>%s\r\n' % {EscapeHtml(unix.strerrno(errno))})
@ -184,7 +184,7 @@ else
Write('<dd>%s\r\n' % {enabled})
end
errno, enabled = unix.getsockopt(GetClientFd(), unix.SOL_TCP, unix.TCP_CORK)
enabled, errno = unix.getsockopt(GetClientFd(), unix.SOL_TCP, unix.TCP_CORK)
Write('<dt>unix.getsockopt(GetClientFd(), unix.SOL_TCP, unix.TCP_CORK)\r\n')
if errno then
Write('<dd>%s\r\n' % {EscapeHtml(unix.strerrno(errno))})
@ -192,7 +192,7 @@ else
Write('<dd>%s\r\n' % {enabled})
end
errno, enabled = unix.getsockopt(GetClientFd(), unix.SOL_TCP, unix.TCP_QUICKACK)
enabled, errno = unix.getsockopt(GetClientFd(), unix.SOL_TCP, unix.TCP_QUICKACK)
Write('<dt>unix.getsockopt(GetClientFd(), unix.SOL_TCP, unix.TCP_QUICKACK)\r\n')
if errno then
Write('<dd>%s\r\n' % {EscapeHtml(unix.strerrno(errno))})
@ -200,7 +200,7 @@ else
Write('<dd>%s\r\n' % {enabled})
end
errno, enabled = unix.getsockopt(GetClientFd(), unix.SOL_TCP, unix.TCP_DEFER_ACCEPT)
enabled, errno = unix.getsockopt(GetClientFd(), unix.SOL_TCP, unix.TCP_DEFER_ACCEPT)
Write('<dt>unix.getsockopt(GetClientFd(), unix.SOL_TCP, unix.TCP_DEFER_ACCEPT)\r\n')
if errno then
Write('<dd>%s\r\n' % {EscapeHtml(unix.strerrno(errno))})
@ -208,7 +208,7 @@ else
Write('<dd>%s\r\n' % {enabled})
end
errno, enabled = unix.getsockopt(GetClientFd(), unix.SOL_TCP, unix.TCP_FASTOPEN_CONNECT)
enabled, errno = unix.getsockopt(GetClientFd(), unix.SOL_TCP, unix.TCP_FASTOPEN_CONNECT)
Write('<dt>unix.getsockopt(GetClientFd(), unix.SOL_TCP, unix.TCP_FASTOPEN_CONNECT)\r\n')
if errno then
Write('<dd>%s\r\n' % {EscapeHtml(unix.strerrno(errno))})
@ -216,7 +216,7 @@ else
Write('<dd>%s\r\n' % {enabled})
end
errno, bytes = unix.getsockopt(GetClientFd(), unix.SOL_SOCKET, unix.SO_SNDLOWAT)
bytes, errno = unix.getsockopt(GetClientFd(), unix.SOL_SOCKET, unix.SO_SNDLOWAT)
Write('<dt>unix.getsockopt(GetClientFd(), unix.SOL_SOCKET, unix.SO_SNDLOWAT)\r\n')
if errno then
Write('<dd>%s\r\n' % {EscapeHtml(unix.strerrno(errno))})
@ -224,7 +224,7 @@ else
Write('<dd>%d\r\n' % {bytes})
end
errno, bytes = unix.getsockopt(GetClientFd(), unix.SOL_SOCKET, unix.SO_RCVLOWAT)
bytes, errno = unix.getsockopt(GetClientFd(), unix.SOL_SOCKET, unix.SO_RCVLOWAT)
Write('<dt>unix.getsockopt(GetClientFd(), unix.SOL_SOCKET, unix.SO_RCVLOWAT)\r\n')
if errno then
Write('<dd>%s\r\n' % {EscapeHtml(unix.strerrno(errno))})
@ -232,7 +232,7 @@ else
Write('<dd>%d\r\n' % {bytes})
end
errno, bytes = unix.getsockopt(GetClientFd(), unix.SOL_TCP, unix.TCP_KEEPCNT)
bytes, errno = unix.getsockopt(GetClientFd(), unix.SOL_TCP, unix.TCP_KEEPCNT)
Write('<dt>unix.getsockopt(GetClientFd(), unix.SOL_TCP, unix.TCP_KEEPCNT)\r\n')
if errno then
Write('<dd>%s\r\n' % {EscapeHtml(unix.strerrno(errno))})
@ -240,7 +240,7 @@ else
Write('<dd>%d\r\n' % {bytes})
end
errno, bytes = unix.getsockopt(GetClientFd(), unix.SOL_TCP, unix.TCP_MAXSEG)
bytes, errno = unix.getsockopt(GetClientFd(), unix.SOL_TCP, unix.TCP_MAXSEG)
Write('<dt>unix.getsockopt(GetClientFd(), unix.SOL_TCP, unix.TCP_MAXSEG)\r\n')
if errno then
Write('<dd>%s\r\n' % {EscapeHtml(unix.strerrno(errno))})
@ -248,7 +248,7 @@ else
Write('<dd>%d\r\n' % {bytes})
end
errno, bytes = unix.getsockopt(GetClientFd(), unix.SOL_TCP, unix.TCP_SYNCNT)
bytes, errno = unix.getsockopt(GetClientFd(), unix.SOL_TCP, unix.TCP_SYNCNT)
Write('<dt>unix.getsockopt(GetClientFd(), unix.SOL_TCP, unix.TCP_SYNCNT)\r\n')
if errno then
Write('<dd>%s\r\n' % {EscapeHtml(unix.strerrno(errno))})
@ -256,7 +256,7 @@ else
Write('<dd>%d\r\n' % {bytes})
end
errno, bytes = unix.getsockopt(GetClientFd(), unix.SOL_TCP, unix.TCP_NOTSENT_LOWAT)
bytes, errno = unix.getsockopt(GetClientFd(), unix.SOL_TCP, unix.TCP_NOTSENT_LOWAT)
Write('<dt>unix.getsockopt(GetClientFd(), unix.SOL_TCP, unix.TCP_NOTSENT_LOWAT)\r\n')
if errno then
Write('<dd>%s\r\n' % {EscapeHtml(unix.strerrno(errno))})
@ -264,7 +264,7 @@ else
Write('<dd>%d\r\n' % {bytes})
end
errno, bytes = unix.getsockopt(GetClientFd(), unix.SOL_TCP, unix.TCP_WINDOW_CLAMP)
bytes, errno = unix.getsockopt(GetClientFd(), unix.SOL_TCP, unix.TCP_WINDOW_CLAMP)
Write('<dt>unix.getsockopt(GetClientFd(), unix.SOL_TCP, unix.TCP_WINDOW_CLAMP)\r\n')
if errno then
Write('<dd>%s\r\n' % {EscapeHtml(unix.strerrno(errno))})
@ -272,7 +272,7 @@ else
Write('<dd>%d\r\n' % {bytes})
end
errno, bytes = unix.getsockopt(GetClientFd(), unix.SOL_TCP, unix.TCP_KEEPIDLE)
bytes, errno = unix.getsockopt(GetClientFd(), unix.SOL_TCP, unix.TCP_KEEPIDLE)
Write('<dt>unix.getsockopt(GetClientFd(), unix.SOL_TCP, unix.TCP_KEEPIDLE)\r\n')
if errno then
Write('<dd>%s\r\n' % {EscapeHtml(unix.strerrno(errno))})
@ -280,7 +280,7 @@ else
Write('<dd>%d\r\n' % {bytes})
end
errno, bytes = unix.getsockopt(GetClientFd(), unix.SOL_TCP, unix.TCP_KEEPINTVL)
bytes, errno = unix.getsockopt(GetClientFd(), unix.SOL_TCP, unix.TCP_KEEPINTVL)
Write('<dt>unix.getsockopt(GetClientFd(), unix.SOL_TCP, unix.TCP_KEEPINTVL)\r\n')
if errno then
Write('<dd>%s\r\n' % {EscapeHtml(unix.strerrno(errno))})

View file

@ -111,7 +111,7 @@ local function main()
if dir then
unix.write(fd, '<ul>\r\n')
while true do
name, kind, ino, off = dir:read()
name, errno, kind, ino, off = dir:read()
if not name then
break
end

View file

@ -8,14 +8,14 @@ function main()
cmd = 'ls'
end
syscall = 'commandv'
ls, errno = unix.commandv(cmd)
ls = assert(unix.commandv(cmd))
if ls then
syscall = 'pipe'
reader, writer, errno = unix.pipe()
if reader then
oldint = unix.sigaction(unix.SIGINT, unix.SIG_IGN)
oldquit = unix.sigaction(unix.SIGQUIT, unix.SIG_IGN)
oldmask = unix.sigprocmask(unix.SIG_BLOCK, (1 << (unix.SIGCHLD - 1)))
-- oldint = assert(unix.sigaction(unix.SIGINT, unix.SIG_IGN))
-- oldquit = assert(unix.sigaction(unix.SIGQUIT, unix.SIG_IGN))
-- oldmask = assert(unix.sigprocmask(unix.SIG_BLOCK, (1 << (unix.SIGCHLD - 1))))
syscall = 'fork'
child, errno = unix.fork()
if child then

View file

@ -33,7 +33,7 @@ function main()
pollfds = {}
pollfds[mainfd] = unix.POLLIN
ifs, errno = unix.siocgifconf()
ifs = assert(unix.siocgifconf())
for i = 1,#ifs do
if (IsLoopbackIp(mainip) and (IsPublicIp(ifs[i].ip) or
IsPrivateIp(ifs[i].ip) or
@ -45,7 +45,7 @@ function main()
server = unix.socket()
unix.bind(server, ifs[i].ip)
unix.listen(server)
ip, port = unix.getsockname(server)
ip, errno, port = unix.getsockname(server)
addr = '%s:%d' % {FormatIp(ip), port}
url = 'http://%s' % {addr}
Log(kLogInfo, 'listening on %s' % {addr})
@ -79,7 +79,7 @@ function main()
unix.write(mainfd, data)
elseif servers[fd] then
unix.write(mainfd, 'preparing to accept from %d<br>\r\n' % {fd})
client, clientip, clientport = unix.accept(fd)
client, errno, clientip, clientport = unix.accept(fd)
unix.write(mainfd, 'preparing to accept from %d<br>\r\n' % {fd})
addr = '%s:%d' % {FormatIp(clientip), clientport}
addrs[client] = addr

View file

@ -806,7 +806,7 @@ static int ReturnDir(lua_State *L, struct UnixDir *udir) {
struct UnixDir **udirp;
udir->refs = 1;
udirp = lua_newuserdatauv(L, sizeof(*udirp), 1);
luaL_setmetatable(L, "unix.UnixDir");
luaL_setmetatable(L, "unix.Dir");
*udirp = udir;
return 1;
}
@ -943,9 +943,8 @@ static int LuaUnixGetsockopt(lua_State *L) {
optvalsize = sizeof(optval);
if (getsockopt(fd, level, optname, &optval, &optvalsize) != -1) {
CheckOptvalsize(L, sizeof(optval), optvalsize);
lua_pushnil(L);
lua_pushboolean(L, optval);
return 2;
return 1;
} else {
return SysretErrnoNil(L, olderr);
}
@ -953,9 +952,8 @@ static int LuaUnixGetsockopt(lua_State *L) {
optvalsize = sizeof(optval);
if (getsockopt(fd, level, optname, &optval, &optvalsize) != -1) {
CheckOptvalsize(L, sizeof(optval), optvalsize);
lua_pushnil(L);
lua_pushinteger(L, optval);
return 2;
return 1;
} else {
return SysretErrnoNil(L, olderr);
}
@ -963,8 +961,8 @@ static int LuaUnixGetsockopt(lua_State *L) {
tvsize = sizeof(tv);
if (getsockopt(fd, level, optname, &tv, &tvsize) != -1) {
CheckOptvalsize(L, sizeof(tv), tvsize);
lua_pushnil(L);
lua_pushinteger(L, tv.tv_sec);
lua_pushnil(L);
lua_pushinteger(L, tv.tv_usec);
return 3;
} else {
@ -974,8 +972,8 @@ static int LuaUnixGetsockopt(lua_State *L) {
lsize = sizeof(l);
if (getsockopt(fd, level, optname, &l, &lsize) != -1) {
CheckOptvalsize(L, sizeof(l), lsize);
lua_pushnil(L);
lua_pushinteger(L, l.l_onoff);
lua_pushnil(L);
lua_pushinteger(L, l.l_linger);
return 3;
} else {
@ -1745,9 +1743,8 @@ static int LuaUnixDirClose(lua_State *L) {
// unix.Dir:read() → name:str, unix.Errno, kind:int, ino:int, off:int
static int LuaUnixDirRead(lua_State *L) {
int olderr;
int olderr = errno;
struct dirent *ent;
olderr = errno;
errno = 0;
if ((ent = readdir(GetDirOrDie(L)))) {
lua_pushlstring(L, ent->d_name, strnlen(ent->d_name, sizeof(ent->d_name)));
@ -1755,7 +1752,7 @@ static int LuaUnixDirRead(lua_State *L) {
lua_pushinteger(L, ent->d_type);
lua_pushinteger(L, ent->d_ino);
lua_pushinteger(L, ent->d_off);
return 4;
return 5;
} else if (!ent && !errno) {
return 0; // end of listing
} else {

View file

@ -5178,13 +5178,15 @@ static void LuaPrint(lua_State *L) {
char *b = 0;
const char *s;
n = lua_gettop(L);
for (i = 1; i <= n; i++) {
if (i > 1) appendw(&b, '\t');
LuaEncodeLuaData(L, &b, 64, "g", i);
if (n > 0) {
for (i = 1; i <= n; i++) {
if (i > 1) appendw(&b, '\t');
LuaEncodeLuaData(L, &b, 64, "g", i);
}
appendw(&b, '\n');
WRITE(1, b, appendz(b).i);
free(b);
}
appendw(&b, '\n');
WRITE(1, b, appendz(b).i);
free(b);
}
static void LuaInterpreter(lua_State *L) {