diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-06-03 17:11:41 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-06-03 17:11:41 -0300 |
commit | ad7103ea3aed7f40a5cf7055af253b34320134bc (patch) | |
tree | 978a750ba9c4cb990ece1fe6280f3806acbe6fce /lauxlib.c | |
parent | 0079e0f57ce2bd4dc40b9b7c5831c58764a7938f (diff) | |
download | lua-ad7103ea3aed7f40a5cf7055af253b34320134bc.tar.gz lua-ad7103ea3aed7f40a5cf7055af253b34320134bc.tar.bz2 lua-ad7103ea3aed7f40a5cf7055af253b34320134bc.zip |
lua_load* defined in auxlib (and so renamed to luaL_load*)
Diffstat (limited to 'lauxlib.c')
-rw-r--r-- | lauxlib.c | 206 |
1 files changed, 181 insertions, 25 deletions
@@ -1,33 +1,36 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lauxlib.c,v 1.70 2002/05/15 18:57:44 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.71 2002/05/16 18:39:46 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 | */ |
6 | 6 | ||
7 | 7 | ||
8 | #include <ctype.h> | ||
8 | #include <stdarg.h> | 9 | #include <stdarg.h> |
9 | #include <stdio.h> | 10 | #include <stdio.h> |
10 | #include <string.h> | 11 | #include <string.h> |
11 | 12 | ||
13 | #ifndef lua_filerror | ||
14 | #include <errno.h> | ||
15 | #define lua_fileerror (strerror(errno)) | ||
16 | #endif | ||
17 | |||
18 | |||
12 | /* This file uses only the official API of Lua. | 19 | /* This file uses only the official API of Lua. |
13 | ** Any function declared here could be written as an application function. | 20 | ** Any function declared here could be written as an application function. |
14 | ** With care, these functions can be used by other libraries. | ||
15 | */ | 21 | */ |
16 | 22 | ||
17 | #include "lua.h" | 23 | #include "lua.h" |
18 | 24 | ||
19 | #include "lauxlib.h" | 25 | #include "lauxlib.h" |
20 | #include "luadebug.h" | 26 | #include "luadebug.h" |
21 | #include "lualib.h" | ||
22 | 27 | ||
23 | 28 | ||
24 | LUALIB_API int luaL_findstring (const char *name, const char *const list[]) { | 29 | /* |
25 | int i; | 30 | ** {====================================================== |
26 | for (i=0; list[i]; i++) | 31 | ** Error-report functions |
27 | if (strcmp(list[i], name) == 0) | 32 | ** ======================================================= |
28 | return i; | 33 | */ |
29 | return -1; /* name not found */ | ||
30 | } | ||
31 | 34 | ||
32 | 35 | ||
33 | LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) { | 36 | LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) { |
@@ -54,6 +57,33 @@ static void tag_error (lua_State *L, int narg, int tag) { | |||
54 | } | 57 | } |
55 | 58 | ||
56 | 59 | ||
60 | LUALIB_API int luaL_verror (lua_State *L, const char *fmt, ...) { | ||
61 | lua_Debug ar; | ||
62 | const char *msg; | ||
63 | va_list argp; | ||
64 | va_start(argp, fmt); | ||
65 | msg = lua_pushvfstring(L, fmt, argp); | ||
66 | va_end(argp); | ||
67 | if (lua_getstack(L, 1, &ar)) { /* check calling function */ | ||
68 | lua_getinfo(L, "Snl", &ar); | ||
69 | if (ar.currentline > 0) | ||
70 | lua_pushfstring(L, "%s:%d: %s", ar.short_src, ar.currentline, msg); | ||
71 | } | ||
72 | return lua_errorobj(L); | ||
73 | } | ||
74 | |||
75 | /* }====================================================== */ | ||
76 | |||
77 | |||
78 | LUALIB_API int luaL_findstring (const char *name, const char *const list[]) { | ||
79 | int i; | ||
80 | for (i=0; list[i]; i++) | ||
81 | if (strcmp(list[i], name) == 0) | ||
82 | return i; | ||
83 | return -1; /* name not found */ | ||
84 | } | ||
85 | |||
86 | |||
57 | LUALIB_API void luaL_check_stack (lua_State *L, int space, const char *mes) { | 87 | LUALIB_API void luaL_check_stack (lua_State *L, int space, const char *mes) { |
58 | if (!lua_checkstack(L, space)) | 88 | if (!lua_checkstack(L, space)) |
59 | luaL_verror(L, "stack overflow (%s)", mes); | 89 | luaL_verror(L, "stack overflow (%s)", mes); |
@@ -143,21 +173,6 @@ LUALIB_API void luaL_opennamedlib (lua_State *L, const char *libname, | |||
143 | } | 173 | } |
144 | 174 | ||
145 | 175 | ||
146 | LUALIB_API int luaL_verror (lua_State *L, const char *fmt, ...) { | ||
147 | lua_Debug ar; | ||
148 | const char *msg; | ||
149 | va_list argp; | ||
150 | va_start(argp, fmt); | ||
151 | msg = lua_pushvfstring(L, fmt, argp); | ||
152 | va_end(argp); | ||
153 | if (lua_getstack(L, 1, &ar)) { /* check calling function */ | ||
154 | lua_getinfo(L, "Snl", &ar); | ||
155 | if (ar.currentline > 0) | ||
156 | lua_pushfstring(L, "%s:%d: %s", ar.short_src, ar.currentline, msg); | ||
157 | } | ||
158 | return lua_errorobj(L); | ||
159 | } | ||
160 | |||
161 | 176 | ||
162 | /* | 177 | /* |
163 | ** {====================================================== | 178 | ** {====================================================== |
@@ -284,3 +299,144 @@ LUALIB_API void luaL_unref (lua_State *L, int t, int ref) { | |||
284 | } | 299 | } |
285 | } | 300 | } |
286 | 301 | ||
302 | |||
303 | /* | ||
304 | ** {====================================================== | ||
305 | ** Load functions | ||
306 | ** ======================================================= | ||
307 | */ | ||
308 | |||
309 | typedef struct LoadF { | ||
310 | FILE *f; | ||
311 | char buff[LUAL_BUFFERSIZE]; | ||
312 | } LoadF; | ||
313 | |||
314 | |||
315 | static const char *getF (void *ud, size_t *size) { | ||
316 | LoadF *lf = (LoadF *)ud; | ||
317 | *size = fread(lf->buff, 1, LUAL_BUFFERSIZE, lf->f); | ||
318 | return (*size > 0) ? lf->buff : NULL; | ||
319 | } | ||
320 | |||
321 | |||
322 | static int errfile (lua_State *L, const char *filename) { | ||
323 | if (filename == NULL) filename = "stdin"; | ||
324 | lua_pushfstring(L, "cannot read %s: %s", filename, lua_fileerror); | ||
325 | return LUA_ERRFILE; | ||
326 | } | ||
327 | |||
328 | |||
329 | LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) { | ||
330 | LoadF lf; | ||
331 | int status; | ||
332 | int c; | ||
333 | int old_top = lua_gettop(L); | ||
334 | lf.f = (filename == NULL) ? stdin : fopen(filename, "r"); | ||
335 | if (lf.f == NULL) return errfile(L, filename); /* unable to open file */ | ||
336 | c = ungetc(getc(lf.f), lf.f); | ||
337 | if (!(isspace(c) || isprint(c)) && lf.f != stdin) { /* binary file? */ | ||
338 | fclose(lf.f); | ||
339 | lf.f = fopen(filename, "rb"); /* reopen in binary mode */ | ||
340 | if (lf.f == NULL) return errfile(L, filename); /* unable to reopen file */ | ||
341 | } | ||
342 | if (filename == NULL) | ||
343 | lua_pushliteral(L, "=stdin"); | ||
344 | else | ||
345 | lua_pushfstring(L, "@%s", filename); | ||
346 | status = lua_load(L, getF, &lf, lua_tostring(L, -1)); | ||
347 | lua_remove(L, old_top+1); /* remove filename from stack */ | ||
348 | if (ferror(lf.f)) { | ||
349 | lua_settop(L, old_top); /* ignore results from `lua_load' */ | ||
350 | return errfile(L, filename); | ||
351 | } | ||
352 | if (lf.f != stdin) | ||
353 | fclose(lf.f); | ||
354 | return status; | ||
355 | } | ||
356 | |||
357 | |||
358 | typedef struct LoadS { | ||
359 | const char *s; | ||
360 | size_t size; | ||
361 | } LoadS; | ||
362 | |||
363 | |||
364 | static const char *getS (void *ud, size_t *size) { | ||
365 | LoadS *ls = (LoadS *)ud; | ||
366 | if (ls->size == 0) return NULL; | ||
367 | *size = ls->size; | ||
368 | ls->size = 0; | ||
369 | return ls->s; | ||
370 | } | ||
371 | |||
372 | |||
373 | LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t size, | ||
374 | const char *name) { | ||
375 | LoadS ls; | ||
376 | ls.s = buff; | ||
377 | ls.size = size; | ||
378 | return lua_load(L, getS, &ls, name); | ||
379 | } | ||
380 | |||
381 | /* }====================================================== */ | ||
382 | |||
383 | |||
384 | /* | ||
385 | ** {====================================================== | ||
386 | ** compatibility code | ||
387 | ** ======================================================= | ||
388 | */ | ||
389 | |||
390 | |||
391 | static void callalert (lua_State *L, int status) { | ||
392 | if (status != 0) { | ||
393 | int top = lua_gettop(L); | ||
394 | lua_getglobal(L, "_ALERT"); | ||
395 | lua_insert(L, -2); | ||
396 | lua_pcall(L, 1, 0, 0); | ||
397 | lua_settop(L, top-1); | ||
398 | } | ||
399 | } | ||
400 | |||
401 | |||
402 | LUALIB_API int lua_call (lua_State *L, int nargs, int nresults) { | ||
403 | int status; | ||
404 | int errpos = lua_gettop(L) - nargs; | ||
405 | lua_getglobal(L, "_ERRORMESSAGE"); | ||
406 | lua_insert(L, errpos); /* put below function and args */ | ||
407 | status = lua_pcall(L, nargs, nresults, errpos); | ||
408 | lua_remove(L, errpos); | ||
409 | callalert(L, status); | ||
410 | return status; | ||
411 | } | ||
412 | |||
413 | |||
414 | static int aux_do (lua_State *L, int status) { | ||
415 | if (status == 0) { /* parse OK? */ | ||
416 | int err = lua_gettop(L); | ||
417 | lua_getglobal(L, "_ERRORMESSAGE"); | ||
418 | lua_insert(L, err); | ||
419 | status = lua_pcall(L, 0, LUA_MULTRET, err); /* call main */ | ||
420 | lua_remove(L, err); /* remove error function */ | ||
421 | } | ||
422 | callalert(L, status); | ||
423 | return status; | ||
424 | } | ||
425 | |||
426 | |||
427 | LUALIB_API int lua_dofile (lua_State *L, const char *filename) { | ||
428 | return aux_do(L, luaL_loadfile(L, filename)); | ||
429 | } | ||
430 | |||
431 | |||
432 | LUALIB_API int lua_dobuffer (lua_State *L, const char *buff, size_t size, | ||
433 | const char *name) { | ||
434 | return aux_do(L, luaL_loadbuffer(L, buff, size, name)); | ||
435 | } | ||
436 | |||
437 | |||
438 | LUALIB_API int lua_dostring (lua_State *L, const char *str) { | ||
439 | return lua_dobuffer(L, str, strlen(str), str); | ||
440 | } | ||
441 | |||
442 | /* }====================================================== */ | ||