Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
269 lines (198 loc) · 5.61 KB

File metadata and controls

269 lines (198 loc) · 5.61 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/****************************
* DreamShell ##version## *
* lua.c *
* DreamShell LUA *
* Created by SWAT *
****************************/
#include "lua.h"
#include "list.h"
#include "module.h"
#include "utils.h"
#include "console.h"
#include "exceptions.h"
static lua_State *DSLua = NULL;
static Item_list_t *lua_libs;
void LuaOpenlibs(lua_State *L) {
/* Open stock libs */
luaL_openlibs(L);
/* Open additional libs */
lua_packlibopen(L);
/* Custom functions */
RegisterLuaFunctions(L);
Item_t *i;
LuaRegLibOpen *openFunc;
SLIST_FOREACH(i, lua_libs, list) {
openFunc = (LuaRegLibOpen *) i->data;
if(openFunc != NULL) {
//ds_printf("DS_PROCESS: Opening lua library: %s\n", i->name);
EXPT_GUARD_BEGIN;
openFunc(L);
EXPT_GUARD_CATCH;
ds_printf("DS_ERROR: Can't open lua library: %s\n", i->name);
EXPT_GUARD_END;
}
}
}
int InitLua() {
//ds_printf("DS_PROCESS: Init lua library...\n");
luaB_set_fputs((void (*)(const char *))ds_printf);
DSLua = luaL_newstate();//lua_open();
if (DSLua == NULL) {
ds_printf("DS_ERROR_LUA: Invalid state.. giving up\n");
return -1;
}
if((lua_libs = listMake()) == NULL) {
return -1;
}
LuaOpenlibs(DSLua);
//ds_printf("DS_OK: Lua library inited.\n");
return 0;
}
void ShutdownLua() {
listDestroy(lua_libs, (listFreeItemFunc *) free);
lua_close(DSLua);
}
void ResetLua() {
ds_printf("DS_PROCESS: Reset lua state...\n");
lua_close(DSLua);
DSLua = luaL_newstate();
LuaOpenlibs(DSLua);
}
int RegisterLuaLib(const char *name, LuaRegLibOpen *func) {
if(listAddItem(lua_libs, LIST_ITEM_LUA_LIB, name, func, sizeof(LuaRegLibOpen)) != NULL){
return 1;
}
return 0;
}
int UnregisterLuaLib(const char *name) {
Item_t *i = listGetItemByName(lua_libs, name);
if(i == NULL) {
return 0;
}
listRemoveItem(lua_libs, i, NULL);
return 1;
}
int lua_report (lua_State *L, int status) {
if (status && !lua_isnil(L, -1)) {
const char *msg = lua_tostring(L, -1);
if (msg == NULL) msg = "(error object is not a string)";
ds_printf(msg);
lua_pop(L, 1);
}
return status;
}
int lua_traceback(lua_State *L) {
if (!lua_isstring(L, 1)) /* 'message' not a string? */
return 1; /* keep it intact */
lua_getfield(L, LUA_GLOBALSINDEX, "debug");
if (!lua_istable(L, -1)) {
lua_pop(L, 1);
return 1;
}
lua_getfield(L, -1, "traceback");
if (!lua_isfunction(L, -1)) {
lua_pop(L, 2);
return 1;
}
lua_pushvalue(L, 1); /* pass error message */
lua_pushinteger(L, 2); /* skip this function and traceback */
lua_call(L, 2, 1); /* call debug.traceback */
return 1;
}
int lua_docall (lua_State *L, int narg, int clear) {
int status;
int base = lua_gettop(L) - narg; /* function index */
lua_pushcfunction(L, lua_traceback); /* push traceback function */
lua_insert(L, base); /* put it under chunk and args */
status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);
lua_remove(L, base); /* remove traceback function */
/* force a complete garbage collection in case of errors */
if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0);
return status;
}
static int dofile (lua_State *L, const char *name) {
int status = luaL_loadfile(L, name) || lua_docall(L, 0, 1);
return lua_report(L, status);
}
static int dostring (lua_State *L, const char *s, const char *name) {
int status = luaL_loadbuffer(L, s, strlen(s), name) || lua_docall(L, 0, 1);
return lua_report(L, status);
}
static int dolibrary (lua_State *L, const char *name) {
lua_getglobal(L, "require");
lua_pushstring(L, name);
return lua_report(L, lua_docall(L, 1, 1));
}
int LuaDo(int type, const char *str_or_file, lua_State *lu) {
int res = LUA_ERRRUN;
EXPT_GUARD_BEGIN;
if(type == LUA_DO_FILE) {
res = dofile(lu, str_or_file);
} else if(type == LUA_DO_STRING) {
res = dostring(lu, str_or_file, "=(DreamShell)");
} else if(type == LUA_DO_LIBRARY) {
res = dolibrary(lu, str_or_file);
} else {
ds_printf("DS_ERROR: LuaDo type error: %d\n", type);
}
EXPT_GUARD_CATCH;
res = LUA_ERRRUN;
ds_printf("DS_ERROR: Exception in lua script: %s\n", str_or_file);
EXPT_GUARD_END;
/* Lua gives no message in such cases, so lua.c provides one */
if (res == LUA_ERRMEM) {
ds_printf("LUA: Memory allocation error!\n");
} else if (res == LUA_ERRFILE)
ds_printf("LUA: Can't open lua file %s\n", str_or_file);
else if (res == LUA_ERRSYNTAX)
ds_printf("LUA: Syntax error in lua script %s\n", str_or_file);
else if (res == LUA_ERRRUN)
ds_printf("LUA: Error at management chunk!\n");
else if (res == LUA_ERRERR)
ds_printf("LUA: Error in error message!\n");
return res;
}
void LuaPushArgs(lua_State *L, char *argv[]) {
int i;
lua_newtable(L);
for (i=0; argv[i]; i++) {
/* arg[i] = argv[i] */
lua_pushnumber(L, i);
lua_pushstring(L, argv[i]);
lua_settable(L, -3);
}
/* argv.n = maximum index in table `argv' */
lua_pushstring(L, "n");
lua_pushnumber(L, i-1);
lua_settable(L, -3);
}
void LuaAddArgs(lua_State *L, char *argv[]) {
LuaPushArgs(L, argv);
lua_setglobal(L, "argv");
}
int RunLuaScript(char *fn, char *argv[]) {
lua_State *L = NewLuaThread();
if(L == NULL) {
ds_printf("DS_ERROR: LUA: Invalid state.. giving up\n");
return 0;
}
if(argv != NULL) {
LuaPushArgs(L, argv);
lua_setglobal(L, "argv");
}
LuaDo(LUA_DO_FILE, fn, L);
lua_close(L);
return 1;
}
lua_State *GetLuaState() {
return DSLua;
}
void SetLuaState(lua_State *l) {
DSLua = l;
}
lua_State *NewLuaThread() {
return lua_newthread(DSLua);
//lua_State *L = lua_open();
//LuaOpenlibs(L);
//return L;
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.