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.c95
1 files changed, 49 insertions, 46 deletions
diff --git a/apps/plugins/lua/lauxlib.c b/apps/plugins/lua/lauxlib.c
index a755266bde..88abc3cde0 100644
--- a/apps/plugins/lua/lauxlib.c
+++ b/apps/plugins/lua/lauxlib.c
@@ -547,60 +547,62 @@ static int errfile (lua_State *L, const char *what, int fnameindex) {
547 return LUA_ERRFILE; 547 return LUA_ERRFILE;
548} 548}
549 549
550static void make_path(char* dest, size_t dest_size, char* curfile, char* newfile)
551{
552 char* pos = rb->strrchr(curfile, '/');
553 if(pos != NULL)
554 {
555 unsigned int len = (unsigned int)(pos - curfile);
556 len = len + 1 > dest_size ? dest_size - 1 : len;
557
558 if(len > 0)
559 memcpy(dest, curfile, len);
560
561 dest[len] = '/';
562 dest[len+1] = '\0';
563 }
564 else
565 dest[0] = '\0';
566
567 strncat(dest, newfile, dest_size - strlen(dest));
568}
550 569
551LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) { 570LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
552 LoadF lf; 571 LoadF lf;
553 int status; //, readstatus; 572 int status;
554 char buffer[MAX_PATH]; 573 char buffer[MAX_PATH];
555// int c;
556 int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ 574 int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */
557 lf.extraline = 0; 575 lf.extraline = 0;
558// if (filename == NULL) { 576 lf.f = rb->open(filename, O_RDONLY);
559// lua_pushliteral(L, "=stdin"); 577 if(lf.f < 0) {
560// lf.f = stdin; 578 /* Fallback */
561// } 579
562// else { 580 lua_Debug ar;
563 lua_pushfstring(L, "@%s", filename); 581 if(lua_getstack(L, 1, &ar)) {
564 lf.f = rb->open(filename, O_RDONLY); 582 lua_getinfo(L, "S", &ar);
565 if (lf.f < 0) 583
566 { 584 /* Try determining the base path of the current Lua chunk
567 /* Fallback */ 585 and prepend it to filename in buffer. */
568 snprintf(buffer, sizeof(buffer), "%s/%s", curpath, filename); 586 make_path(buffer, sizeof(buffer), (char*)&ar.source[1], (char*)filename);
569 lf.f = rb->open(buffer, O_RDONLY); 587 lf.f = rb->open(buffer, O_RDONLY);
570
571 if(lf.f < 0)
572 {
573 snprintf(buffer, sizeof(buffer), "%s/%s", VIEWERS_DIR, filename);
574 lf.f = rb->open(buffer, O_RDONLY);
575
576 if(lf.f < 0)
577 return errfile(L, "open", fnameindex);
578 }
579 } 588 }
580// } 589
581// c = getc(lf.f); 590 if(lf.f < 0) {
582// if (c == '#') { /* Unix exec. file? */ 591 snprintf(buffer, sizeof(buffer), "%s/%s", VIEWERS_DIR, filename);
583// lf.extraline = 1; 592 lf.f = rb->open(buffer, O_RDONLY);
584// while ((c = getc(lf.f)) != EOF && c != '\n') ; /* skip first line */ 593
585// if (c == '\n') c = getc(lf.f); 594 if(lf.f < 0)
586// } 595 return errfile(L, "open", fnameindex);
587// if (c == LUA_SIGNATURE[0]) { // && lf.f != stdin) { /* binary file? */ 596 }
588// rb->close(lf.f); 597
589// lf.f = rb->open(filename, O_RDONLY); /* reopen in binary mode */ 598 if(lf.f >= 0)
590// if (lf.f < 0) return errfile(L, "reopen", fnameindex); 599 lua_pushfstring(L, "@%s", buffer);
591 /* skip eventual `#!...' */ 600 }
592// while ((c = getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) ; 601 else
593// lf.extraline = 0; 602 lua_pushfstring(L, "@%s", filename);
594// } 603
595// ungetc(c, lf.f);
596 status = lua_load(L, getF, &lf, lua_tostring(L, -1)); 604 status = lua_load(L, getF, &lf, lua_tostring(L, -1));
597// readstatus = ferror(lf.f);
598 //if (lf.f != stdin) rb->close(lf.f); /* close file (even in case of errors) */
599 rb->close(lf.f); 605 rb->close(lf.f);
600// if (readstatus) {
601// lua_settop(L, fnameindex); /* ignore results from `lua_load' */
602// return errfile(L, "read", fnameindex);
603// }
604 lua_remove(L, fnameindex); 606 lua_remove(L, fnameindex);
605 return status; 607 return status;
606} 608}
@@ -653,9 +655,10 @@ static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
653 655
654 656
655static int panic (lua_State *L) { 657static int panic (lua_State *L) {
656 (void)L; /* to avoid warnings */
657 DEBUGF("PANIC: unprotected error in call to Lua API (%s)\n", 658 DEBUGF("PANIC: unprotected error in call to Lua API (%s)\n",
658 lua_tostring(L, -1)); 659 lua_tostring(L, -1));
660 rb->splashf(5 * HZ, "PANIC: unprotected error in call to Lua API (%s)",
661 lua_tostring(L, -1));
659 662
660 return 0; 663 return 0;
661} 664}