diff options
| -rw-r--r-- | lauxlib.c | 22 | ||||
| -rw-r--r-- | llex.c | 7 |
2 files changed, 20 insertions, 9 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lauxlib.c,v 1.99 2003/04/03 13:35:34 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.100 2003/04/07 14:35:00 roberto Exp roberto $ |
| 3 | ** Auxiliary functions for building Lua libraries | 3 | ** Auxiliary functions for building Lua libraries |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -461,6 +461,7 @@ LUALIB_API void luaL_unref (lua_State *L, int t, int ref) { | |||
| 461 | */ | 461 | */ |
| 462 | 462 | ||
| 463 | typedef struct LoadF { | 463 | typedef struct LoadF { |
| 464 | int extraline; | ||
| 464 | FILE *f; | 465 | FILE *f; |
| 465 | char buff[LUAL_BUFFERSIZE]; | 466 | char buff[LUAL_BUFFERSIZE]; |
| 466 | } LoadF; | 467 | } LoadF; |
| @@ -469,6 +470,11 @@ typedef struct LoadF { | |||
| 469 | static const char *getF (lua_State *L, void *ud, size_t *size) { | 470 | static const char *getF (lua_State *L, void *ud, size_t *size) { |
| 470 | LoadF *lf = (LoadF *)ud; | 471 | LoadF *lf = (LoadF *)ud; |
| 471 | (void)L; | 472 | (void)L; |
| 473 | if (lf->extraline) { | ||
| 474 | lf->extraline = 0; | ||
| 475 | *size = 1; | ||
| 476 | return "\n"; | ||
| 477 | } | ||
| 472 | if (feof(lf->f)) return NULL; | 478 | if (feof(lf->f)) return NULL; |
| 473 | *size = fread(lf->buff, 1, LUAL_BUFFERSIZE, lf->f); | 479 | *size = fread(lf->buff, 1, LUAL_BUFFERSIZE, lf->f); |
| 474 | return (*size > 0) ? lf->buff : NULL; | 480 | return (*size > 0) ? lf->buff : NULL; |
| @@ -488,6 +494,7 @@ LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) { | |||
| 488 | int status, readstatus; | 494 | int status, readstatus; |
| 489 | int c; | 495 | int c; |
| 490 | int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ | 496 | int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ |
| 497 | lf.extraline = 0; | ||
| 491 | if (filename == NULL) { | 498 | if (filename == NULL) { |
| 492 | lua_pushliteral(L, "=stdin"); | 499 | lua_pushliteral(L, "=stdin"); |
| 493 | lf.f = stdin; | 500 | lf.f = stdin; |
| @@ -497,12 +504,21 @@ LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) { | |||
| 497 | lf.f = fopen(filename, "r"); | 504 | lf.f = fopen(filename, "r"); |
| 498 | } | 505 | } |
| 499 | if (lf.f == NULL) return errfile(L, fnameindex); /* unable to open file */ | 506 | if (lf.f == NULL) return errfile(L, fnameindex); /* unable to open file */ |
| 500 | c = ungetc(getc(lf.f), lf.f); | 507 | c = getc(lf.f); |
| 501 | if (!(isspace(c) || isprint(c)) && lf.f != stdin) { /* binary file? */ | 508 | if (c == '#') { /* Unix exec. file? */ |
| 509 | lf.extraline = 1; | ||
| 510 | while ((c = getc(lf.f)) != EOF && c != '\n') ; /* skip first line */ | ||
| 511 | if (c == '\n') c = getc(lf.f); | ||
| 512 | } | ||
| 513 | if (c == LUA_SIGNATURE[0] && lf.f != stdin) { /* binary file? */ | ||
| 502 | fclose(lf.f); | 514 | fclose(lf.f); |
| 503 | lf.f = fopen(filename, "rb"); /* reopen in binary mode */ | 515 | lf.f = fopen(filename, "rb"); /* reopen in binary mode */ |
| 504 | if (lf.f == NULL) return errfile(L, fnameindex); /* unable to reopen file */ | 516 | if (lf.f == NULL) return errfile(L, fnameindex); /* unable to reopen file */ |
| 517 | /* skip eventual `#!...' */ | ||
| 518 | while ((c = getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) ; | ||
| 519 | lf.extraline = 0; | ||
| 505 | } | 520 | } |
| 521 | ungetc(c, lf.f); | ||
| 506 | status = lua_load(L, getF, &lf, lua_tostring(L, -1)); | 522 | status = lua_load(L, getF, &lf, lua_tostring(L, -1)); |
| 507 | readstatus = ferror(lf.f); | 523 | readstatus = ferror(lf.f); |
| 508 | if (lf.f != stdin) fclose(lf.f); /* close file (even in case of errors) */ | 524 | if (lf.f != stdin) fclose(lf.f); /* close file (even in case of errors) */ |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: llex.c,v 1.118 2003/02/28 17:19:47 roberto Exp roberto $ | 2 | ** $Id: llex.c,v 1.119 2003/03/24 12:39:34 roberto Exp roberto $ |
| 3 | ** Lexical Analyzer | 3 | ** Lexical Analyzer |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -125,11 +125,6 @@ void luaX_setinput (lua_State *L, LexState *LS, ZIO *z, TString *source) { | |||
| 125 | LS->lastline = 1; | 125 | LS->lastline = 1; |
| 126 | LS->source = source; | 126 | LS->source = source; |
| 127 | next(LS); /* read first char */ | 127 | next(LS); /* read first char */ |
| 128 | if (LS->current == '#') { | ||
| 129 | do { /* skip first line */ | ||
| 130 | next(LS); | ||
| 131 | } while (LS->current != '\n' && LS->current != EOZ); | ||
| 132 | } | ||
| 133 | } | 128 | } |
| 134 | 129 | ||
| 135 | 130 | ||
