diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2003-05-15 09:20:24 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2003-05-15 09:20:24 -0300 |
commit | 176cd7d1a394cc9df27a62ab7cd9adba005c5c72 (patch) | |
tree | 1c38193aa7ab82001fd581850cf095735b431ca2 /lauxlib.c | |
parent | 859c7f7ce0ce6dfdbd3586535a47eaf562d60f37 (diff) | |
download | lua-176cd7d1a394cc9df27a62ab7cd9adba005c5c72.tar.gz lua-176cd7d1a394cc9df27a62ab7cd9adba005c5c72.tar.bz2 lua-176cd7d1a394cc9df27a62ab7cd9adba005c5c72.zip |
`#!' is detected by loadfile, not by the lexer
Diffstat (limited to 'lauxlib.c')
-rw-r--r-- | lauxlib.c | 22 |
1 files changed, 19 insertions, 3 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) */ |