diff options
Diffstat (limited to 'lapi.c')
-rw-r--r-- | lapi.c | 48 |
1 files changed, 27 insertions, 21 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lapi.c,v 1.190 2002/05/07 17:36:56 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 1.191 2002/05/15 18:57:44 roberto Exp roberto $ |
3 | ** Lua API | 3 | ** Lua API |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -355,10 +355,23 @@ LUA_API void lua_pushstring (lua_State *L, const char *s) { | |||
355 | } | 355 | } |
356 | 356 | ||
357 | 357 | ||
358 | LUA_API void lua_vpushstr (lua_State *L, const char *fmt, va_list argp) { | 358 | LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt, |
359 | va_list argp) { | ||
360 | const char *ret; | ||
359 | lua_lock(L); | 361 | lua_lock(L); |
360 | luaO_vpushstr(L, fmt, argp); | 362 | ret = luaO_pushvfstring(L, fmt, argp); |
361 | lua_unlock(L); | 363 | lua_unlock(L); |
364 | return ret; | ||
365 | } | ||
366 | |||
367 | |||
368 | LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) { | ||
369 | const char *ret; | ||
370 | va_list argp; | ||
371 | va_start(argp, fmt); | ||
372 | ret = lua_pushvfstring(L, fmt, argp); | ||
373 | va_end(argp); | ||
374 | return ret; | ||
362 | } | 375 | } |
363 | 376 | ||
364 | 377 | ||
@@ -556,21 +569,16 @@ LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errf) { | |||
556 | 569 | ||
557 | static int errfile (lua_State *L, const char *filename) { | 570 | static int errfile (lua_State *L, const char *filename) { |
558 | if (filename == NULL) filename = "stdin"; | 571 | if (filename == NULL) filename = "stdin"; |
559 | lua_pushliteral(L, "cannot read "); | 572 | lua_pushfstring(L, "cannot read %s: %s", filename, lua_fileerror); |
560 | lua_pushstring(L, filename); | ||
561 | lua_pushliteral(L, ": "); | ||
562 | lua_pushstring(L, lua_fileerror); | ||
563 | lua_concat(L, 4); | ||
564 | return LUA_ERRFILE; | 573 | return LUA_ERRFILE; |
565 | } | 574 | } |
566 | 575 | ||
567 | 576 | ||
568 | LUA_API int lua_loadfile (lua_State *L, const char *filename) { | 577 | LUA_API int lua_loadfile (lua_State *L, const char *filename) { |
569 | ZIO z; | 578 | ZIO z; |
570 | const char *luafname; /* name used by lua */ | 579 | int fnindex; |
571 | int status; | 580 | int status; |
572 | int bin; /* flag for file mode */ | 581 | int bin; /* flag for file mode */ |
573 | int nlevel; /* level on the stack of filename */ | ||
574 | FILE *f = (filename == NULL) ? stdin : fopen(filename, "r"); | 582 | FILE *f = (filename == NULL) ? stdin : fopen(filename, "r"); |
575 | if (f == NULL) return errfile(L, filename); /* unable to open file */ | 583 | if (f == NULL) return errfile(L, filename); /* unable to open file */ |
576 | bin = (ungetc(getc(f), f) == LUA_SIGNATURE[0]); | 584 | bin = (ungetc(getc(f), f) == LUA_SIGNATURE[0]); |
@@ -580,19 +588,17 @@ LUA_API int lua_loadfile (lua_State *L, const char *filename) { | |||
580 | if (f == NULL) return errfile(L, filename); /* unable to reopen file */ | 588 | if (f == NULL) return errfile(L, filename); /* unable to reopen file */ |
581 | } | 589 | } |
582 | if (filename == NULL) | 590 | if (filename == NULL) |
583 | lua_pushstring(L, "=stdin"); | 591 | lua_pushliteral(L, "=stdin"); |
584 | else { | 592 | else |
585 | lua_pushliteral(L, "@"); | 593 | lua_pushfstring(L, "@%s", filename); |
586 | lua_pushstring(L, filename); | 594 | fnindex = lua_gettop(L); /* stack index of file name */ |
587 | lua_concat(L, 2); | 595 | luaZ_Fopen(&z, f, lua_tostring(L, fnindex)); |
588 | } | ||
589 | nlevel = lua_gettop(L); | ||
590 | luafname = lua_tostring(L, -1); /* luafname = `@'..filename */ | ||
591 | luaZ_Fopen(&z, f, luafname); | ||
592 | status = luaD_protectedparser(L, &z, bin); | 596 | status = luaD_protectedparser(L, &z, bin); |
593 | if (ferror(f)) | 597 | lua_remove(L, fnindex); |
598 | if (ferror(f)) { | ||
599 | if (status == 0) lua_pop(L, 1); /* remove chunk */ | ||
594 | return errfile(L, filename); | 600 | return errfile(L, filename); |
595 | lua_remove(L, nlevel); /* remove filename */ | 601 | } |
596 | if (f != stdin) | 602 | if (f != stdin) |
597 | fclose(f); | 603 | fclose(f); |
598 | return status; | 604 | return status; |