summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--apps/plugins/lua/rocklua.c54
1 files changed, 47 insertions, 7 deletions
diff --git a/apps/plugins/lua/rocklua.c b/apps/plugins/lua/rocklua.c
index ace73ee449..79f6bae953 100644
--- a/apps/plugins/lua/rocklua.c
+++ b/apps/plugins/lua/rocklua.c
@@ -146,12 +146,28 @@ static int docall (lua_State *L) {
146} 146}
147 147
148static void lua_atexit(void); 148static void lua_atexit(void);
149static int lua_split_arguments(lua_State *L, const char *filename);
150
149static int loadfile_newstate(lua_State **L, const char *filename) 151static int loadfile_newstate(lua_State **L, const char *filename)
150{ 152{
151 *L = luaL_newstate(); 153 const char *file;
152 rb_atexit(lua_atexit); 154 int ret;
153 rocklua_openlibs(*L); 155
154 return luaL_loadfile(*L, filename); 156 *L = luaL_newstate();
157 rb_atexit(lua_atexit);
158
159 lua_gc(*L, LUA_GCSTOP, 0); /* stop collector during initialization */
160 rocklua_openlibs(*L);
161
162 lua_split_arguments(*L, filename);
163 lua_setglobal (*L, "_arguments");
164 file = lua_tostring (*L, -1);
165 lua_setglobal (*L, "_fullpath");
166 /* lua manual -> no guarantee pointer valid after value is removed from stack */
167 ret = luaL_loadfile(*L, file);
168 lua_gc(*L, LUA_GCRESTART, 0);
169
170 return ret;
155} 171}
156 172
157static void lua_atexit(void) 173static void lua_atexit(void)
@@ -162,10 +178,14 @@ static void lua_atexit(void)
162 { 178 {
163 if (Ls == lua_touserdata(Ls, -1)) /* signal from restart_lua */ 179 if (Ls == lua_touserdata(Ls, -1)) /* signal from restart_lua */
164 { 180 {
165 filename = (char *) malloc(MAX_PATH); 181 filename = (char *) malloc((MAX_PATH * 2) + 1);
166 182
167 if (filename) /* out of memory? */ 183 if (filename) {/* out of memory? */
168 rb->strlcpy(filename, lua_tostring(Ls, -2), MAX_PATH); 184 filename[MAX_PATH * 2] = '\0';
185 rb->strlcpy(filename, lua_tostring(Ls, -2), MAX_PATH * 2);
186 } else {
187 goto ERR_RUN;
188 }
169 lua_close(Ls); /* close old state */ 189 lua_close(Ls); /* close old state */
170 190
171 lu_status = loadfile_newstate(&Ls, filename); 191 lu_status = loadfile_newstate(&Ls, filename);
@@ -175,6 +195,7 @@ static void lua_atexit(void)
175 } 195 }
176 else if (lua_tointeger(Ls, -1) != 0) /* os.exit */ 196 else if (lua_tointeger(Ls, -1) != 0) /* os.exit */
177 { 197 {
198ERR_RUN:
178 lu_status = LUA_ERRRUN; 199 lu_status = LUA_ERRRUN;
179 lua_pop(Ls, 1); /* put exit string on top of stack */ 200 lua_pop(Ls, 1); /* put exit string on top of stack */
180 plugin_start(NULL); 201 plugin_start(NULL);
@@ -185,6 +206,25 @@ static void lua_atexit(void)
185 _exit(0); /* don't call exit handler */ 206 _exit(0); /* don't call exit handler */
186} 207}
187 208
209/* split filename at argchar
210 * remainder of filename pushed on stack (-1)
211* argument string pushed on stack or nil if doesn't exist (-2)
212 */
213static int lua_split_arguments(lua_State *L, const char *filename)
214{
215 const char argchar = '?';
216 const char* arguments = strchr(filename, argchar);
217 if(arguments) {
218 lua_pushstring(L, (arguments + 1));
219 } else {
220 arguments = strlen(filename) + filename;
221 lua_pushnil(L);
222 }
223 lua_pushlstring(L, filename, arguments - filename);
224 lua_insert(L, -2); /* swap filename and argument */
225 return 2;
226}
227
188/***************** Plugin Entry Point *****************/ 228/***************** Plugin Entry Point *****************/
189enum plugin_status plugin_start(const void* parameter) 229enum plugin_status plugin_start(const void* parameter)
190{ 230{