a lot of shit
This commit is contained in:
@@ -11,17 +11,35 @@ end
|
||||
|
||||
local function all_loader(paths, ...)
|
||||
local files = { ... };
|
||||
local function coro_entry()
|
||||
|
||||
return coroutine.wrap(function ()
|
||||
for _, f in ipairs(files) do
|
||||
local name = unresolve_require(paths, f);
|
||||
local bytecode = string.dump(assert(load(io.lines(f, 1024), "@" .. f, "t", nil)));
|
||||
|
||||
coroutine.yield(("package.preload[%q] = load(%q, %q, 'b');"):format(name, bytecode, f));
|
||||
end
|
||||
coroutine.yield("require 'init' (...);");
|
||||
end
|
||||
|
||||
return coroutine.wrap(coro_entry);
|
||||
coroutine.yield("require 'init' (...);");
|
||||
end);
|
||||
end
|
||||
|
||||
local function escape(str)
|
||||
return "\"" .. str:gsub(".", function (c)
|
||||
local b = string.byte(c);
|
||||
|
||||
if c == "\n" then
|
||||
return "\\n";
|
||||
elseif c == "\\" then
|
||||
return "\\\\";
|
||||
elseif c == "\"" then
|
||||
return "\\\"";
|
||||
elseif b >= 32 and b <= 126 then
|
||||
return c;
|
||||
else
|
||||
return ("\\%.3o"):format(b);
|
||||
end
|
||||
end) .. "\"";
|
||||
end
|
||||
|
||||
local function main(root, out, ...)
|
||||
@@ -36,13 +54,15 @@ local function main(root, out, ...)
|
||||
local f = assert(io.open(out, "w"));
|
||||
f:write "#include <stdint.h>\n";
|
||||
f:write "#define BYTECODE entry_bytecode\n";
|
||||
f:write "static const uint8_t entry_bytecode[] = { ";
|
||||
f:write "#define BYTECODE_SIZE (sizeof entry_bytecode - 1)\n";
|
||||
f:write "static const uint8_t entry_bytecode[] = ";
|
||||
|
||||
for i = 1, #res do
|
||||
f:write(string.byte(res, i), ", ");
|
||||
for i = 1, math.ceil(#res / 64) do
|
||||
f:write(escape(res:sub((i - 1) * 64 + 1, i * 64)));
|
||||
f:write"\n";
|
||||
end
|
||||
|
||||
f:write "};";
|
||||
f:write ";";
|
||||
|
||||
assert(f:close());
|
||||
end
|
||||
|
||||
34
lib/errfunc.lua
Normal file
34
lib/errfunc.lua
Normal file
@@ -0,0 +1,34 @@
|
||||
local err = ...;
|
||||
local i = 2;
|
||||
|
||||
io.stderr:write("unhandled error: ", tostring(err), "\n");
|
||||
|
||||
while true do
|
||||
local info = debug.getinfo(i, "Snl");
|
||||
if info == nil then break end
|
||||
|
||||
local name = info.name;
|
||||
local location = info.short_src;
|
||||
|
||||
if location == "[C]" then
|
||||
location = "at <C>";
|
||||
elseif location:find "%[string" then
|
||||
location = "at <string>";
|
||||
else
|
||||
location = "at " .. location;
|
||||
end
|
||||
|
||||
if info.currentline > 0 then
|
||||
location = location .. ":" .. info.currentline;
|
||||
end
|
||||
|
||||
if name ~= nil then
|
||||
io.stderr:write(" ", location, " in ", name, "\n");
|
||||
else
|
||||
io.stderr:write(" ", location, "\n");
|
||||
end
|
||||
|
||||
i = i + 1;
|
||||
end
|
||||
|
||||
return err;
|
||||
125
lib/fs.c
Normal file
125
lib/fs.c
Normal file
@@ -0,0 +1,125 @@
|
||||
#define _GNU_SOURCE
|
||||
|
||||
#include <lua.h>
|
||||
#include <lualib.h>
|
||||
#include <lauxlib.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <err.h>
|
||||
|
||||
#include "lib.h"
|
||||
|
||||
static const char *fs_check_path(lua_State *ctx, int i, size_t *psize) {
|
||||
size_t n;
|
||||
const char *path = luaL_checklstring(ctx, 1, &n);
|
||||
|
||||
size_t real_len = strlen(path);
|
||||
if (n != real_len) {
|
||||
luaL_error(ctx, "path may not contain \\0");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (psize) *psize = n;
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
static int lib_fs_mkdir(lua_State *ctx) {
|
||||
size_t path_size;
|
||||
const char *ro_path = fs_check_path(ctx, 1, &path_size);
|
||||
bool recursive = lua_toboolean(ctx, 2);
|
||||
|
||||
if (recursive) {
|
||||
char path[path_size + 1];
|
||||
memcpy(path, ro_path, path_size + 1);
|
||||
|
||||
for (size_t i = 0; i <= path_size ; i++) {
|
||||
if (path[i] == '/' || path[i] == '\0') {
|
||||
path[i] = '\0';
|
||||
|
||||
if (mkdir(path, 0755) < 0 && errno != EEXIST) {
|
||||
lua_pushnil(ctx);
|
||||
lua_pushfstring(ctx, "can't make directory: %s", strerror(errno));
|
||||
return 12;
|
||||
}
|
||||
|
||||
if (i < path_size) path[i] = '/';
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (mkdir(ro_path, 0755) < 0) {
|
||||
lua_pushnil(ctx);
|
||||
lua_pushfstring(ctx, "can't make directory: %s", strerror(errno));
|
||||
return 12;
|
||||
}
|
||||
}
|
||||
|
||||
lua_pushboolean(ctx, true);
|
||||
return 1;
|
||||
}
|
||||
static int lib_fs_symlink(lua_State *ctx) {
|
||||
const char *src = fs_check_path(ctx, 1, NULL);
|
||||
const char *dst = fs_check_path(ctx, 2, NULL);
|
||||
|
||||
if (symlink(src, dst) < 0) {
|
||||
lua_pushnil(ctx);
|
||||
lua_pushfstring(ctx, "failed to chmod: %s", strerror(errno));
|
||||
return 2;
|
||||
}
|
||||
|
||||
lua_pushboolean(ctx, true);
|
||||
return 1;
|
||||
}
|
||||
static int lib_fs_chmod(lua_State *ctx) {
|
||||
const char *path = fs_check_path(ctx, 1, NULL);
|
||||
int mode;
|
||||
|
||||
if (lua_isinteger(ctx, 2)) {
|
||||
mode = lua_tointeger(ctx, 2);
|
||||
mode = mode & 0xFFF;
|
||||
}
|
||||
else {
|
||||
const char *strmode = luaL_checkstring(ctx, 2);
|
||||
|
||||
mode = 0;
|
||||
|
||||
for (size_t i = 0; strmode[i]; i++) {
|
||||
char c = strmode[i];
|
||||
if (c >= '0' && c <= '7') {
|
||||
mode <<= 3;
|
||||
mode |= c - '0';
|
||||
}
|
||||
else {
|
||||
return luaL_error(ctx, "invalid mode '%s' - must be an octal integer", strmode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (chmod(path, mode) < 0) {
|
||||
lua_pushnil(ctx);
|
||||
lua_pushfstring(ctx, "failed to chmod: %s", strerror(errno));
|
||||
return 2;
|
||||
}
|
||||
|
||||
lua_pushboolean(ctx, true);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int fs_open_lib(lua_State *ctx) {
|
||||
luaL_newlib(ctx, ((luaL_Reg[]) {
|
||||
{ "mkdir", lib_fs_mkdir },
|
||||
{ "chmod", lib_fs_chmod },
|
||||
{ "symlink", lib_fs_symlink },
|
||||
{ NULL, NULL },
|
||||
}));
|
||||
|
||||
return 1;
|
||||
}
|
||||
@@ -87,6 +87,7 @@ static int lib_http_get(lua_State *ctx) {
|
||||
char *body = body_compress(buff, &size);
|
||||
lua_pushlstring(ctx, body, size);
|
||||
free(body);
|
||||
// fprintf(stderr, "HTTP %s\n", url);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -3,3 +3,4 @@
|
||||
int http_open_lib(lua_State *ctx);
|
||||
int fmt_open_lib(lua_State *ctx);
|
||||
int zlib_open_lib(lua_State *ctx);
|
||||
int fs_open_lib(lua_State *ctx);
|
||||
|
||||
91
lib/main.c
91
lib/main.c
@@ -9,108 +9,35 @@
|
||||
#include "lib.h"
|
||||
|
||||
#ifndef BYTECODE
|
||||
static char entry_bytecode[] = {
|
||||
0x1b, 0x4c, 0x75, 0x61, 0x54, 0x00, 0x19, 0x93,
|
||||
0x0d, 0x0a, 0x1a, 0x0a, 0x04, 0x08, 0x08, 0x78,
|
||||
0x56, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x28, 0x77, 0x40, 0x01,
|
||||
0x80, 0x80, 0x80, 0x00, 0x01, 0x02, 0x85, 0x51,
|
||||
0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x83,
|
||||
0x80, 0x00, 0x00, 0x44, 0x00, 0x02, 0x01, 0x46,
|
||||
0x00, 0x01, 0x01, 0x82, 0x04, 0x86, 0x65, 0x72,
|
||||
0x72, 0x6f, 0x72, 0x04, 0xa4, 0x70, 0x6c, 0x65,
|
||||
0x61, 0x73, 0x65, 0x2c, 0x20, 0x63, 0x6f, 0x6d,
|
||||
0x70, 0x69, 0x6c, 0x65, 0x20, 0x77, 0x69, 0x74,
|
||||
0x68, 0x20, 0x2d, 0x44, 0x42, 0x59, 0x54, 0x45,
|
||||
0x43, 0x4f, 0x44, 0x45, 0x3d, 0x2e, 0x2e, 0x2e,
|
||||
0x81, 0x01, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80,
|
||||
0x80,
|
||||
};
|
||||
static uint8_t entry_bytecode[] = "error \"bad build\"";
|
||||
#define BYTECODE entry_bytecode
|
||||
#define BYTECODE_SIZE (sizeof entry_bytecode - 1)
|
||||
#endif
|
||||
|
||||
static char err_bytecode[] = {
|
||||
0x1b, 0x4c, 0x75, 0x61, 0x54, 0x00, 0x19, 0x93,
|
||||
0x0d, 0x0a, 0x1a, 0x0a, 0x04, 0x08, 0x08, 0x78,
|
||||
0x56, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x28, 0x77, 0x40, 0x01,
|
||||
0x80, 0x80, 0x80, 0x00, 0x01, 0x0c, 0xc4, 0x51,
|
||||
0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x02, 0x81,
|
||||
0x80, 0x00, 0x80, 0x0b, 0x01, 0x00, 0x00, 0x0e,
|
||||
0x01, 0x02, 0x01, 0x14, 0x81, 0x02, 0x02, 0x03,
|
||||
0x82, 0x01, 0x00, 0x8b, 0x02, 0x00, 0x04, 0x00,
|
||||
0x03, 0x00, 0x00, 0xc4, 0x02, 0x02, 0x02, 0x03,
|
||||
0x83, 0x02, 0x00, 0x44, 0x01, 0x05, 0x01, 0x0b,
|
||||
0x01, 0x00, 0x06, 0x0e, 0x01, 0x02, 0x07, 0x80,
|
||||
0x01, 0x01, 0x00, 0x03, 0x02, 0x04, 0x00, 0x44,
|
||||
0x01, 0x03, 0x02, 0x3c, 0x81, 0x09, 0x00, 0x38,
|
||||
0x17, 0x00, 0x80, 0x8e, 0x01, 0x02, 0x0a, 0x0e,
|
||||
0x02, 0x02, 0x0b, 0x3c, 0x02, 0x0c, 0x00, 0xb8,
|
||||
0x00, 0x00, 0x80, 0x03, 0x82, 0x06, 0x00, 0x38,
|
||||
0x05, 0x00, 0x80, 0x94, 0x82, 0x04, 0x0e, 0x83,
|
||||
0x83, 0x07, 0x00, 0xc4, 0x02, 0x03, 0x02, 0xc2,
|
||||
0x02, 0x00, 0x00, 0xb8, 0x00, 0x00, 0x80, 0x03,
|
||||
0x02, 0x08, 0x00, 0xb8, 0x01, 0x00, 0x80, 0x83,
|
||||
0x82, 0x08, 0x00, 0x00, 0x03, 0x04, 0x00, 0xb5,
|
||||
0x02, 0x02, 0x00, 0x00, 0x02, 0x05, 0x00, 0x8e,
|
||||
0x02, 0x02, 0x12, 0xc0, 0x02, 0x7f, 0x00, 0x38,
|
||||
0x02, 0x00, 0x80, 0x80, 0x02, 0x04, 0x00, 0x03,
|
||||
0x83, 0x09, 0x00, 0x8e, 0x03, 0x02, 0x12, 0xb5,
|
||||
0x02, 0x03, 0x00, 0x00, 0x02, 0x05, 0x00, 0xbc,
|
||||
0x81, 0x09, 0x00, 0xb8, 0x04, 0x00, 0x80, 0x8b,
|
||||
0x02, 0x00, 0x00, 0x8e, 0x02, 0x05, 0x01, 0x94,
|
||||
0x82, 0x05, 0x02, 0x83, 0x03, 0x0a, 0x00, 0x00,
|
||||
0x04, 0x04, 0x00, 0x83, 0x84, 0x0a, 0x00, 0x00,
|
||||
0x05, 0x03, 0x00, 0x83, 0x85, 0x02, 0x00, 0xc4,
|
||||
0x02, 0x07, 0x01, 0x38, 0x03, 0x00, 0x80, 0x8b,
|
||||
0x02, 0x00, 0x00, 0x8e, 0x02, 0x05, 0x01, 0x94,
|
||||
0x82, 0x05, 0x02, 0x83, 0x03, 0x0a, 0x00, 0x00,
|
||||
0x04, 0x04, 0x00, 0x83, 0x84, 0x02, 0x00, 0xc4,
|
||||
0x02, 0x05, 0x01, 0x95, 0x00, 0x01, 0x80, 0xaf,
|
||||
0x00, 0x80, 0x06, 0xb8, 0xe4, 0xff, 0x7f, 0x46,
|
||||
0x00, 0x02, 0x01, 0x46, 0x01, 0x01, 0x01, 0x96,
|
||||
0x04, 0x83, 0x69, 0x6f, 0x04, 0x87, 0x73, 0x74,
|
||||
0x64, 0x65, 0x72, 0x72, 0x04, 0x86, 0x77, 0x72,
|
||||
0x69, 0x74, 0x65, 0x04, 0x92, 0x75, 0x6e, 0x68,
|
||||
0x61, 0x6e, 0x64, 0x6c, 0x65, 0x64, 0x20, 0x65,
|
||||
0x72, 0x72, 0x6f, 0x72, 0x3a, 0x20, 0x04, 0x89,
|
||||
0x74, 0x6f, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
|
||||
0x04, 0x82, 0x0a, 0x04, 0x86, 0x64, 0x65, 0x62,
|
||||
0x75, 0x67, 0x04, 0x88, 0x67, 0x65, 0x74, 0x69,
|
||||
0x6e, 0x66, 0x6f, 0x04, 0x84, 0x53, 0x6e, 0x6c,
|
||||
0x00, 0x04, 0x85, 0x6e, 0x61, 0x6d, 0x65, 0x04,
|
||||
0x8a, 0x73, 0x68, 0x6f, 0x72, 0x74, 0x5f, 0x73,
|
||||
0x72, 0x63, 0x04, 0x84, 0x5b, 0x43, 0x5d, 0x04,
|
||||
0x87, 0x61, 0x74, 0x20, 0x3c, 0x43, 0x3e, 0x04,
|
||||
0x85, 0x66, 0x69, 0x6e, 0x64, 0x04, 0x89, 0x25,
|
||||
0x5b, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x04,
|
||||
0x8c, 0x61, 0x74, 0x20, 0x3c, 0x73, 0x74, 0x72,
|
||||
0x69, 0x6e, 0x67, 0x3e, 0x04, 0x84, 0x61, 0x74,
|
||||
0x20, 0x04, 0x8c, 0x63, 0x75, 0x72, 0x72, 0x65,
|
||||
0x6e, 0x74, 0x6c, 0x69, 0x6e, 0x65, 0x04, 0x82,
|
||||
0x3a, 0x04, 0x83, 0x20, 0x20, 0x04, 0x85, 0x20,
|
||||
0x69, 0x6e, 0x20, 0x81, 0x01, 0x00, 0x00, 0x80,
|
||||
0x80, 0x80, 0x80, 0x80,
|
||||
};
|
||||
static char err_bytecode[] =
|
||||
"local err = ...;"
|
||||
"local trace = debug.traceback(nil, 2):match \"^[^\\n]+\\n(.+)$\""
|
||||
"print(table.concat { \"unhandled error: \", err, \"\\n\", trace })";
|
||||
|
||||
static void load_modules(lua_State *ctx) {
|
||||
luaL_openlibs(ctx);
|
||||
luaL_requiref(ctx, "http", http_open_lib, false);
|
||||
luaL_requiref(ctx, "fmt", fmt_open_lib, false);
|
||||
luaL_requiref(ctx, "zlib", zlib_open_lib, false);
|
||||
luaL_requiref(ctx, "fs", fs_open_lib, false);
|
||||
}
|
||||
|
||||
int main(int argc, const char **argv) {
|
||||
lua_State *ctx = luaL_newstate();
|
||||
load_modules(ctx);
|
||||
|
||||
if (luaL_loadbufferx(ctx, err_bytecode, sizeof(err_bytecode), "<main>", "b")) {
|
||||
if (luaL_loadbuffer(ctx, err_bytecode, sizeof err_bytecode - 1, "<main>")) {
|
||||
fprintf(stderr, "error while loading lua bytecode for errfunc: %s", lua_tostring(ctx, -1));
|
||||
return 1;
|
||||
}
|
||||
int errfunc_i = lua_gettop(ctx);
|
||||
|
||||
if (luaL_loadbufferx(ctx, (char*)BYTECODE, sizeof(BYTECODE), "<main>", "b")) {
|
||||
if (luaL_loadbuffer(ctx, (char*)BYTECODE, BYTECODE_SIZE, "<main>")) {
|
||||
fprintf(stderr, "error while loading lua bytecode for main: %s", lua_tostring(ctx, -1));
|
||||
return 1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user