summaryrefslogtreecommitdiff
path: root/apps/plugins/lua/lauxlib.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/lua/lauxlib.c')
-rw-r--r--apps/plugins/lua/lauxlib.c50
1 files changed, 1 insertions, 49 deletions
diff --git a/apps/plugins/lua/lauxlib.c b/apps/plugins/lua/lauxlib.c
index 0c987cfa30..b8020b7475 100644
--- a/apps/plugins/lua/lauxlib.c
+++ b/apps/plugins/lua/lauxlib.c
@@ -560,62 +560,14 @@ static int errfile (lua_State *L, const char *what, int fnameindex) {
560 return LUA_ERRFILE; 560 return LUA_ERRFILE;
561} 561}
562 562
563bool get_cur_path(lua_State *L, char* dest, size_t dest_size)
564{
565 lua_Debug ar;
566 if(lua_getstack(L, 1, &ar))
567 {
568 /* Try determining the base path of the current Lua chunk
569 and write it to dest. */
570 lua_getinfo(L, "S", &ar);
571
572 char* curfile = (char*) &ar.source[1];
573 char* pos = rb->strrchr(curfile, '/');
574 if(pos != NULL)
575 {
576 unsigned int len = (unsigned int)(pos - curfile);
577 len = len + 1 > dest_size ? dest_size - 1 : len;
578
579 if(len > 0)
580 memcpy(dest, curfile, len);
581
582 dest[len] = '/';
583 dest[len+1] = '\0';
584
585 return true;
586 }
587 else
588 return false;
589 }
590 else
591 return false;
592}
593
594LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) { 563LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
595 LoadF lf; 564 LoadF lf;
596 int status; 565 int status;
597 char buffer[MAX_PATH];
598 int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ 566 int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */
599 lf.extraline = 0; 567 lf.extraline = 0;
600 lf.f = rb->open(filename, O_RDONLY); 568 lf.f = rb->open(filename, O_RDONLY);
601 lua_pushfstring(L, "@%s", filename); 569 lua_pushfstring(L, "@%s", filename);
602 if(lf.f < 0) { 570 if (lf.f < 0) return errfile(L, "open", fnameindex);
603 /* Fallback */
604
605 if(get_cur_path(L, buffer, sizeof(buffer))) {
606 strncat(buffer, filename, sizeof(buffer) - strlen(buffer));
607 lf.f = rb->open(buffer, O_RDONLY);
608 }
609
610 if(lf.f < 0) {
611 snprintf(buffer, sizeof(buffer), "%s/%s", VIEWERS_DIR, filename);
612 lf.f = rb->open(buffer, O_RDONLY);
613
614 if(lf.f < 0)
615 return errfile(L, "open", fnameindex);
616 }
617 }
618
619 status = lua_load(L, getF, &lf, lua_tostring(L, -1)); 571 status = lua_load(L, getF, &lf, lua_tostring(L, -1));
620 rb->close(lf.f); 572 rb->close(lf.f);
621 lua_remove(L, fnameindex); 573 lua_remove(L, fnameindex);