127 lines
4.7 KiB
C
127 lines
4.7 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 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,
|
|
};
|
|
#define BYTECODE entry_bytecode
|
|
#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 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);
|
|
}
|
|
|
|
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")) {
|
|
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")) {
|
|
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;
|
|
}
|