54 lines
1.4 KiB
C
54 lines
1.4 KiB
C
#include <lua.h>
|
|
#include <lualib.h>
|
|
#include <lauxlib.h>
|
|
#include <curl/curl.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdbool.h>
|
|
|
|
#include "lib.h"
|
|
|
|
#ifndef BYTECODE
|
|
static uint8_t entry_bytecode[] = "error \"bad build\"";
|
|
#define BYTECODE entry_bytecode
|
|
#define BYTECODE_SIZE (sizeof entry_bytecode - 1)
|
|
#endif
|
|
|
|
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_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_loadbuffer(ctx, (char*)BYTECODE, BYTECODE_SIZE, "<main>")) {
|
|
fprintf(stderr, "error while loading lua bytecode for main: %s", lua_tostring(ctx, -1));
|
|
return 1;
|
|
}
|
|
|
|
for (size_t i = 1; i < argc; i++) {
|
|
lua_pushstring(ctx, argv[i]);
|
|
}
|
|
|
|
if (lua_pcall(ctx, argc - 1, 0, errfunc_i)) return 1;
|
|
|
|
lua_close(ctx);
|
|
return 0;
|
|
}
|