-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.c
55 lines (45 loc) · 1.25 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
int luaopen_buffer(lua_State *L);
int luaopen_MPI(lua_State *L);
int main(int argc, char **argv)
{
int n;
lua_State *L = luaL_newstate();
luaL_openlibs(L);
luaL_requiref(L, "MPI", luaopen_MPI, 0); lua_pop(L, 1);
luaL_requiref(L, "buffer", luaopen_buffer, 0); lua_pop(L, 1);
// Create the global `arg` table
// ---------------------------------------------------------------------------
lua_newtable(L);
for (n=0; n<argc; ++n) {
lua_pushstring(L, argv[n]);
lua_rawseti(L, -2, n);
}
lua_setglobal(L, "arg");
// Run the script
// ---------------------------------------------------------------------------
if (argc == 1) {
printf("usage: %s script.lua [arg1=val1 arg2=val2]\n", argv[0]);
}
else {
char luacode[4096];
snprintf(luacode, 4096, "\
local f, err = loadfile('%s')\n \
if not f then\n \
print(err)\n \
else \
local success, msg = xpcall(f, debug.traceback)\n \
if not success then\n \
print(msg)\n \
end\n \
end\n", argv[1]);
int err = luaL_dostring(L, luacode);
if (err) {
printf("%s\n", lua_tostring(L, -1));
}
}
lua_close(L);
return 0;
}