-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathluamain.c
156 lines (124 loc) · 3.98 KB
/
luamain.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include "lua.h"
#include "lauxlib.h"
#include "preload.h"
#define FAIL_INIT -125
#define FAIL_ALLOC -126
#define FAIL_EXECUTION -127
#define ALL_IS_RIGHT 0
#ifdef LUA_OK
#define is_lua_ok(status_code) (LUA_OK == status_code)
#else
#define is_lua_ok(status_code) (!status_code)
#endif
#ifdef LUA_OK
#define lua_is_bad() (!LUA_OK)
#else
#define lua_is_bad() (is_lua_ok(0) ? 1 : 0)
#endif
void luaL_openlibs (lua_State *L); // Lua internal - not part of the lua API
const char* arg0 = 0;
static int msghandler (lua_State *L) {
// is error object not a string?
const char *msg = lua_tostring(L, 1);
if (msg == NULL) {
// call a tostring metamethod if any
if (luaL_callmeta(L, 1, "__tostring") && lua_type(L, -1) == LUA_TSTRING){
msg = lua_tostring(L, -1);
lua_remove(L, -1);
// else push a standard error message
} else {
msg = lua_pushfstring(L, "(error object is a %s value)", luaL_typename(L, 1));}
}
// append a traceback
luaL_traceback(L, L, msg, 1);
return 1;
}
// Signal hook: stop the interpreter. Just like standard lua interpreter.
static void clear_and_stop(lua_State *L, lua_Debug *ar) {
(void)ar; // unused arg.
lua_sethook(L, NULL, 0, 0);
luaL_error(L, "interrupted!");
}
static lua_State *globalL = NULL;
static void sigint_handler (int i) {
signal(i, SIG_DFL); // if another SIGINT happens, terminate process
lua_State *L = globalL;
globalL = NULL;
lua_sethook(L, clear_and_stop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1); // Run 'clear_and_stop' before any other lua code
}
static void report_error(lua_State *L, const char * title){
if (!title || title[0] == '\0')
title = "An error occurred somewhere.\n";
char * data = "No other information are avaiable\n";
if (lua_isstring(L, -1)) data = (char *) lua_tostring(L, -1);
lua_getglobal(L, "print");
lua_pushstring(L, title);
lua_pushstring(L, data);
lua_pcall(L, 2, 1, 0);
}
int luamain_start(lua_State *L, char* script, int size, int argc, char **argv) {
int status;
int create_lua = 0;
int base = 0;
arg0 = argv[0];
// create state as needed
if (L == NULL) {
create_lua = 1;
L = luaL_newstate();
if (L == NULL) return FAIL_ALLOC;
}
// os signal handler
void (*previous_sighandl)(int) = signal(SIGINT, sigint_handler);
if (SIG_DFL == previous_sighandl) {
globalL = L; // to be available to 'sigint_handler'
}
luaL_openlibs(L); // open standard libraries
preload_all(L); // open extra libraries
// Prepare the stack with the error handler
lua_pushcfunction(L, msghandler);
base = lua_gettop(L);
// Create a table to store the command line arguments
lua_createtable(L, argc-1, 1);
// Arg 0 : command-line-like path to the executable:
// it may be a link and/or be relative to the current directory
lua_pushstring(L, argv[0]);
lua_rawseti(L, -2, 0);
// Args N... : command line arguments
for (int i = 1; i < argc; i++) {
lua_pushstring(L, argv[i]);
lua_rawseti(L, -2, i);
}
// Save the table in the global namespace
lua_setglobal(L, "arg");
// Load the script in the stack
if (size < 0) size = strlen(script);
status = luaL_loadbuffer(L, script, size, "embedded");
if (!is_lua_ok(status)) {
report_error(L, "An error occurred during the script load.");
status = FAIL_EXECUTION;
goto luamain_end;
}
// Run the script with the signal handler
status = lua_is_bad();
status = lua_pcall(L, 0, LUA_MULTRET, base);
if (is_lua_ok(status)) {
status = ALL_IS_RIGHT;
goto luamain_end;
}
// Report error
report_error(L, "An error accurred during the script execution.");
if (lua_isnumber(L, -1)) status = lua_tonumber(L, -1);
else status = FAIL_EXECUTION;
luamain_end:
// clear C-signal handler
if (SIG_DFL == previous_sighandl) {
signal(SIGINT, SIG_DFL);
globalL = NULL;
}
if (base>0) lua_remove(L, base); // remove lua message handler
if (create_lua) lua_close(L);
return status;
}