summaryrefslogtreecommitdiff
path: root/lauxlib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2011-11-14 15:10:24 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2011-11-14 15:10:24 -0200
commit6cce5c060186c109d5fc87ac13b5d36d9d3997ea (patch)
treeb2215f8fec6effa7adc45e73614a0694b833016a /lauxlib.c
parenteb70f58279e7bd8581ecf58c763d8065a8e2bfb0 (diff)
downloadlua-6cce5c060186c109d5fc87ac13b5d36d9d3997ea.tar.gz
lua-6cce5c060186c109d5fc87ac13b5d36d9d3997ea.tar.bz2
lua-6cce5c060186c109d5fc87ac13b5d36d9d3997ea.zip
new function 'luaL_loadfilex'
Diffstat (limited to 'lauxlib.c')
-rw-r--r--lauxlib.c34
1 files changed, 27 insertions, 7 deletions
diff --git a/lauxlib.c b/lauxlib.c
index 34a33504..5970525d 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lauxlib.c,v 1.234 2011/07/25 17:18:49 roberto Exp roberto $ 2** $Id: lauxlib.c,v 1.235 2011/11/09 19:08:55 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*/
@@ -331,7 +331,7 @@ LUALIB_API int luaL_checkoption (lua_State *L, int narg, const char *def,
331 331
332LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) { 332LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) {
333 /* keep some extra space to run error routines, if needed */ 333 /* keep some extra space to run error routines, if needed */
334 const int extra = 2 * LUA_MINSTACK; 334 const int extra = LUA_MINSTACK;
335 if (!lua_checkstack(L, space + extra)) { 335 if (!lua_checkstack(L, space + extra)) {
336 if (msg) 336 if (msg)
337 luaL_error(L, "stack overflow (%s)", msg); 337 luaL_error(L, "stack overflow (%s)", msg);
@@ -592,6 +592,16 @@ static int errfile (lua_State *L, const char *what, int fnameindex) {
592} 592}
593 593
594 594
595static int checkmode (lua_State *L, const char *mode, const char *x) {
596 if (mode && strchr(mode, x[0]) == NULL) {
597 lua_pushfstring(L,
598 "attempt to load a %s chunk (mode is " LUA_QS ")", x, mode);
599 return LUA_ERRFILE;
600 }
601 else return LUA_OK;
602}
603
604
595static int skipBOM (LoadF *lf) { 605static int skipBOM (LoadF *lf) {
596 const char *p = "\xEF\xBB\xBF"; /* Utf8 BOM mark */ 606 const char *p = "\xEF\xBB\xBF"; /* Utf8 BOM mark */
597 int c; 607 int c;
@@ -624,7 +634,8 @@ static int skipcomment (LoadF *lf, int *cp) {
624} 634}
625 635
626 636
627LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) { 637LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename,
638 const char *mode) {
628 LoadF lf; 639 LoadF lf;
629 int status, readstatus; 640 int status, readstatus;
630 int c; 641 int c;
@@ -640,14 +651,23 @@ LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
640 } 651 }
641 if (skipcomment(&lf, &c)) /* read initial portion */ 652 if (skipcomment(&lf, &c)) /* read initial portion */
642 lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */ 653 lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */
643 if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */ 654 if (c == LUA_SIGNATURE[0]) { /* binary file? */
644 lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ 655 if ((status = checkmode(L, mode, "binary")) != LUA_OK)
645 if (lf.f == NULL) return errfile(L, "reopen", fnameindex); 656 goto closefile;
646 skipcomment(&lf, &c); /* re-read initial portion */ 657 if (filename) {
658 lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */
659 if (lf.f == NULL) return errfile(L, "reopen", fnameindex);
660 skipcomment(&lf, &c); /* re-read initial portion */
661 }
662 }
663 else { /* text file */
664 if ((status = checkmode(L, mode, "text")) != LUA_OK)
665 goto closefile;
647 } 666 }
648 if (c != EOF) 667 if (c != EOF)
649 lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */ 668 lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */
650 status = lua_load(L, getF, &lf, lua_tostring(L, -1)); 669 status = lua_load(L, getF, &lf, lua_tostring(L, -1));
670 closefile:
651 readstatus = ferror(lf.f); 671 readstatus = ferror(lf.f);
652 if (filename) fclose(lf.f); /* close file (even in case of errors) */ 672 if (filename) fclose(lf.f); /* close file (even in case of errors) */
653 if (readstatus) { 673 if (readstatus) {