diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2010-10-01 15:53:00 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2010-10-01 15:53:00 -0300 |
| commit | c3eb89544fd6c435443ae0da7aa5b12491ed9689 (patch) | |
| tree | e08570b65804c0665b3ecad31c16efd0968ebb7c | |
| parent | ffff9a49da35b8b90d48f5419c9c5e0402f803af (diff) | |
| download | lua-c3eb89544fd6c435443ae0da7aa5b12491ed9689.tar.gz lua-c3eb89544fd6c435443ae0da7aa5b12491ed9689.tar.bz2 lua-c3eb89544fd6c435443ae0da7aa5b12491ed9689.zip | |
'luaL_loadlib' accepts Utf8 BOM in the beginning of files
| -rw-r--r-- | lauxlib.c | 39 |
1 files changed, 26 insertions, 13 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lauxlib.c,v 1.219 2010/07/28 15:51:59 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.220 2010/08/03 20:21:16 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 | */ |
| @@ -511,17 +511,32 @@ static int errfile (lua_State *L, const char *what, int fnameindex) { | |||
| 511 | } | 511 | } |
| 512 | 512 | ||
| 513 | 513 | ||
| 514 | static int skipBOM (LoadF *lf) { | ||
| 515 | const char *p = "\xEF\xBB\xBF"; /* Utf8 BOM mark */ | ||
| 516 | int c; | ||
| 517 | lf->n = 0; | ||
| 518 | do { | ||
| 519 | c = getc(lf->f); | ||
| 520 | if (c == EOF || c != *(unsigned char *)p++) return c; | ||
| 521 | lf->buff[lf->n++] = c; /* to be read by the parser */ | ||
| 522 | } while (*p != '\0'); | ||
| 523 | lf->n = 0; /* prefix matched; discard it */ | ||
| 524 | return getc(lf->f); /* return next character */ | ||
| 525 | } | ||
| 526 | |||
| 527 | |||
| 514 | /* | 528 | /* |
| 515 | ** reads the first character of file 'f' and skips its first line | 529 | ** reads the first character of file 'f' and skips an optional BOM mark |
| 516 | ** if it starts with '#'. Returns true if it skipped the first line. | 530 | ** in its beginning plus its first line if it starts with '#'. Returns |
| 517 | ** In any case, '*cp' has the first "valid" character of the file | 531 | ** true if it skipped the first line. In any case, '*cp' has the |
| 518 | ** (after the optional first-line comment). | 532 | ** first "valid" character of the file (after the optional BOM and |
| 533 | ** a first-line comment). | ||
| 519 | */ | 534 | */ |
| 520 | static int skipcomment (FILE *f, int *cp) { | 535 | static int skipcomment (LoadF *lf, int *cp) { |
| 521 | int c = *cp = getc(f); | 536 | int c = *cp = skipBOM(lf); |
| 522 | if (c == '#') { /* first line is a comment (Unix exec. file)? */ | 537 | if (c == '#') { /* first line is a comment (Unix exec. file)? */ |
| 523 | while ((c = getc(f)) != EOF && c != '\n') ; /* skip first line */ | 538 | while ((c = getc(lf->f)) != EOF && c != '\n') ; /* skip first line */ |
| 524 | *cp = getc(f); /* skip end-of-line */ | 539 | *cp = getc(lf->f); /* skip end-of-line */ |
| 525 | return 1; /* there was a comment */ | 540 | return 1; /* there was a comment */ |
| 526 | } | 541 | } |
| 527 | else return 0; /* no comment */ | 542 | else return 0; /* no comment */ |
| @@ -542,14 +557,12 @@ LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) { | |||
| 542 | lf.f = fopen(filename, "r"); | 557 | lf.f = fopen(filename, "r"); |
| 543 | if (lf.f == NULL) return errfile(L, "open", fnameindex); | 558 | if (lf.f == NULL) return errfile(L, "open", fnameindex); |
| 544 | } | 559 | } |
| 545 | lf.n = 0; | 560 | if (skipcomment(&lf, &c)) /* read initial portion */ |
| 546 | if (skipcomment(lf.f, &c)) /* read initial portion */ | ||
| 547 | lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */ | 561 | lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */ |
| 548 | if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */ | 562 | if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */ |
| 549 | lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ | 563 | lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ |
| 550 | if (lf.f == NULL) return errfile(L, "reopen", fnameindex); | 564 | if (lf.f == NULL) return errfile(L, "reopen", fnameindex); |
| 551 | lf.n = 0; | 565 | skipcomment(&lf, &c); /* re-read initial portion */ |
| 552 | skipcomment(lf.f, &c); /* re-read initial portion */ | ||
| 553 | } | 566 | } |
| 554 | if (c != EOF) | 567 | if (c != EOF) |
| 555 | lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */ | 568 | lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */ |
