Implement raw system call for redbean lua code

You can now call functions like fork() from Lua and it'll work across
all supported platforms, including Windows. This gives you a level of
control of the system that Lua traditionally hasn't been able to have
due to its focus on old portable stdio rather modern POSIX APIs. Demo
code has been added to redbean-demo.com to show how it works.

This change also modifies Lua so that integer literals with a leading
zero will be interpreted as octal. That should help avoid shooting in
the foot with POSIX APIs that frequently use octal mode bits.

This change fixes a bug in opendir(".") on New Technology.

Lastly, redbean will now serve crash reports to private network IPs.
This is consistent with other frameworks. However that isn't served
to public IPs unless the -E flag is passed to redbean at startup.
This commit is contained in:
Justine Tunney 2022-04-13 08:49:17 -07:00
parent f684e348d4
commit 281a0f2730
39 changed files with 2044 additions and 84 deletions

View file

@ -7,6 +7,7 @@
#define lobject_c
#define LUA_CORE
#include "libc/intrin/kprintf.h"
#include "third_party/lua/lctype.h"
#include "third_party/lua/ldebug.h"
#include "third_party/lua/ldo.h"
@ -242,7 +243,9 @@ static const char *l_str2d (const char *s, lua_Number *result) {
#define MAXBY10 cast(lua_Unsigned, LUA_MAXINTEGER / 10)
#define MAXBY8 cast(lua_Unsigned, LUA_MAXINTEGER / 8)
#define MAXLASTD cast_int(LUA_MAXINTEGER % 10)
#define MAXLASTD8 cast_int(LUA_MAXINTEGER % 8)
static const char *l_str2int (const char *s, lua_Integer *result) {
lua_Unsigned a = 0;
@ -258,6 +261,15 @@ static const char *l_str2int (const char *s, lua_Integer *result) {
empty = 0;
}
}
else if (s[0] == '0') { /* [jart] octal is the best radix */
for (s += 1; lisdigit(cast_uchar(*s)); s++) {
int d = *s - '0';
if (a >= MAXBY8 && (a > MAXBY8 || d > MAXLASTD8 + neg)) /* overflow? */
return NULL; /* do not accept it (as integer) */
a = a * 8 + d;
empty = 0;
}
}
else { /* decimal */
for (; lisdigit(cast_uchar(*s)); s++) {
int d = *s - '0';