lauxlib.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. ** $Id: lauxlib.c,v 1.72 2002/06/03 20:11:41 roberto Exp roberto $
  3. ** Auxiliary functions for building Lua libraries
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <ctype.h>
  7. #include <stdarg.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #ifndef lua_filerror
  11. #include <errno.h>
  12. #define lua_fileerror (strerror(errno))
  13. #endif
  14. /* This file uses only the official API of Lua.
  15. ** Any function declared here could be written as an application function.
  16. */
  17. #include "lua.h"
  18. #include "lauxlib.h"
  19. #include "luadebug.h"
  20. /*
  21. ** {======================================================
  22. ** Error-report functions
  23. ** =======================================================
  24. */
  25. LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) {
  26. lua_Debug ar;
  27. lua_getstack(L, 0, &ar);
  28. lua_getinfo(L, "n", &ar);
  29. if (strcmp(ar.namewhat, "method") == 0) {
  30. narg--; /* do not count `self' */
  31. if (narg == 0) /* error is in the self argument itself? */
  32. return luaL_verror(L,
  33. "calling %s on bad self (perhaps using `:' instead of `.')",
  34. ar.name);
  35. }
  36. if (ar.name == NULL)
  37. ar.name = "?";
  38. return luaL_verror(L, "bad argument #%d to `%s' (%s)",
  39. narg, ar.name, extramsg);
  40. }
  41. LUALIB_API int luaL_typerror (lua_State *L, int narg, const char *tname) {
  42. const char *msg = lua_pushfstring(L, "%s expected, got %s",
  43. tname, lua_typename(L, lua_type(L,narg)));
  44. return luaL_argerror(L, narg, msg);
  45. }
  46. static void tag_error (lua_State *L, int narg, int tag) {
  47. luaL_typerror(L, narg, lua_typename(L, tag));
  48. }
  49. LUALIB_API int luaL_verror (lua_State *L, const char *fmt, ...) {
  50. lua_Debug ar;
  51. const char *msg;
  52. va_list argp;
  53. va_start(argp, fmt);
  54. msg = lua_pushvfstring(L, fmt, argp);
  55. va_end(argp);
  56. if (lua_getstack(L, 1, &ar)) { /* check calling function */
  57. lua_getinfo(L, "Snl", &ar);
  58. if (ar.currentline > 0)
  59. lua_pushfstring(L, "%s:%d: %s", ar.short_src, ar.currentline, msg);
  60. }
  61. return lua_errorobj(L);
  62. }
  63. /* }====================================================== */
  64. LUALIB_API int luaL_findstring (const char *name, const char *const list[]) {
  65. int i;
  66. for (i=0; list[i]; i++)
  67. if (strcmp(list[i], name) == 0)
  68. return i;
  69. return -1; /* name not found */
  70. }
  71. LUALIB_API void luaL_check_stack (lua_State *L, int space, const char *mes) {
  72. if (!lua_checkstack(L, space))
  73. luaL_verror(L, "stack overflow (%s)", mes);
  74. }
  75. LUALIB_API void luaL_check_type (lua_State *L, int narg, int t) {
  76. if (lua_type(L, narg) != t)
  77. tag_error(L, narg, t);
  78. }
  79. LUALIB_API void luaL_check_any (lua_State *L, int narg) {
  80. if (lua_type(L, narg) == LUA_TNONE)
  81. luaL_argerror(L, narg, "value expected");
  82. }
  83. LUALIB_API const char *luaL_check_lstr (lua_State *L, int narg, size_t *len) {
  84. const char *s = lua_tostring(L, narg);
  85. if (!s) tag_error(L, narg, LUA_TSTRING);
  86. if (len) *len = lua_strlen(L, narg);
  87. return s;
  88. }
  89. LUALIB_API const char *luaL_opt_lstr (lua_State *L, int narg, const char *def, size_t *len) {
  90. if (lua_isnoneornil(L, narg)) {
  91. if (len)
  92. *len = (def ? strlen(def) : 0);
  93. return def;
  94. }
  95. else return luaL_check_lstr(L, narg, len);
  96. }
  97. LUALIB_API lua_Number luaL_check_number (lua_State *L, int narg) {
  98. lua_Number d = lua_tonumber(L, narg);
  99. if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */
  100. tag_error(L, narg, LUA_TNUMBER);
  101. return d;
  102. }
  103. LUALIB_API lua_Number luaL_opt_number (lua_State *L, int narg, lua_Number def) {
  104. if (lua_isnoneornil(L, narg)) return def;
  105. else return luaL_check_number(L, narg);
  106. }
  107. LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {
  108. if (!lua_getmetatable(L, obj)) /* no metatable? */
  109. return 0;
  110. lua_pushstring(L, event);
  111. lua_rawget(L, -2);
  112. if (lua_isnil(L, -1)) {
  113. lua_pop(L, 2); /* remove metatable and metafield */
  114. return 0;
  115. }
  116. lua_pushvalue(L, obj);
  117. lua_rawcall(L, 1, 1);
  118. return 1;
  119. }
  120. LUALIB_API void luaL_openlib (lua_State *L, const luaL_reg *l, int nup) {
  121. for (; l->name; l++) {
  122. int i;
  123. lua_pushstring(L, l->name);
  124. for (i=0; i<nup; i++) /* copy upvalues to the top */
  125. lua_pushvalue(L, -(nup+1));
  126. lua_pushcclosure(L, l->func, nup);
  127. lua_settable(L, -(nup+3));
  128. }
  129. lua_pop(L, nup);
  130. }
  131. LUALIB_API void luaL_opennamedlib (lua_State *L, const char *libname,
  132. const luaL_reg *l, int nup) {
  133. lua_pushstring(L, libname);
  134. lua_insert(L, -(nup+1));
  135. lua_newtable(L);
  136. lua_insert(L, -(nup+1));
  137. luaL_openlib(L, l, nup);
  138. lua_settable(L, LUA_GLOBALSINDEX);
  139. }
  140. /*
  141. ** {======================================================
  142. ** Generic Buffer manipulation
  143. ** =======================================================
  144. */
  145. #define buffempty(B) ((B)->p == (B)->buffer)
  146. #define bufflen(B) ((B)->p - (B)->buffer)
  147. #define bufffree(B) ((size_t)(LUAL_BUFFERSIZE - bufflen(B)))
  148. #define LIMIT (LUA_MINSTACK/2)
  149. static int emptybuffer (luaL_Buffer *B) {
  150. size_t l = bufflen(B);
  151. if (l == 0) return 0; /* put nothing on stack */
  152. else {
  153. lua_pushlstring(B->L, B->buffer, l);
  154. B->p = B->buffer;
  155. B->level++;
  156. return 1;
  157. }
  158. }
  159. static void adjuststack (luaL_Buffer *B) {
  160. if (B->level > 1) {
  161. lua_State *L = B->L;
  162. int toget = 1; /* number of levels to concat */
  163. size_t toplen = lua_strlen(L, -1);
  164. do {
  165. size_t l = lua_strlen(L, -(toget+1));
  166. if (B->level - toget + 1 >= LIMIT || toplen > l) {
  167. toplen += l;
  168. toget++;
  169. }
  170. else break;
  171. } while (toget < B->level);
  172. lua_concat(L, toget);
  173. B->level = B->level - toget + 1;
  174. }
  175. }
  176. LUALIB_API char *luaL_prepbuffer (luaL_Buffer *B) {
  177. if (emptybuffer(B))
  178. adjuststack(B);
  179. return B->buffer;
  180. }
  181. LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {
  182. while (l--)
  183. luaL_putchar(B, *s++);
  184. }
  185. LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) {
  186. luaL_addlstring(B, s, strlen(s));
  187. }
  188. LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
  189. emptybuffer(B);
  190. lua_concat(B->L, B->level);
  191. B->level = 1;
  192. }
  193. LUALIB_API void luaL_addvalue (luaL_Buffer *B) {
  194. lua_State *L = B->L;
  195. size_t vl = lua_strlen(L, -1);
  196. if (vl <= bufffree(B)) { /* fit into buffer? */
  197. memcpy(B->p, lua_tostring(L, -1), vl); /* put it there */
  198. B->p += vl;
  199. lua_pop(L, 1); /* remove from stack */
  200. }
  201. else {
  202. if (emptybuffer(B))
  203. lua_insert(L, -2); /* put buffer before new value */
  204. B->level++; /* add new value into B stack */
  205. adjuststack(B);
  206. }
  207. }
  208. LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
  209. B->L = L;
  210. B->p = B->buffer;
  211. B->level = 0;
  212. }
  213. /* }====================================================== */
  214. LUALIB_API int luaL_ref (lua_State *L, int t) {
  215. int ref;
  216. lua_rawgeti(L, t, 0); /* get first free element */
  217. ref = (int)lua_tonumber(L, -1);
  218. lua_pop(L, 1); /* remove it from stack */
  219. if (ref != 0) { /* any free element? */
  220. lua_rawgeti(L, t, ref); /* remove it from list */
  221. lua_rawseti(L, t, 0);
  222. }
  223. else { /* no free elements */
  224. ref = lua_getn(L, t) + 1; /* use next `n' */
  225. lua_pushliteral(L, "n");
  226. lua_pushnumber(L, ref);
  227. lua_rawset(L, t); /* n = n+1 */
  228. }
  229. lua_rawseti(L, t, ref);
  230. return ref;
  231. }
  232. LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
  233. if (ref >= 0) {
  234. lua_rawgeti(L, t, 0);
  235. lua_pushnumber(L, ref);
  236. lua_rawseti(L, t, 0);
  237. lua_rawseti(L, t, ref);
  238. }
  239. }
  240. /*
  241. ** {======================================================
  242. ** Load functions
  243. ** =======================================================
  244. */
  245. typedef struct LoadF {
  246. FILE *f;
  247. char buff[LUAL_BUFFERSIZE];
  248. } LoadF;
  249. static const char *getF (void *ud, size_t *size) {
  250. LoadF *lf = (LoadF *)ud;
  251. if (feof(lf->f)) return NULL;
  252. *size = fread(lf->buff, 1, LUAL_BUFFERSIZE, lf->f);
  253. return (*size > 0) ? lf->buff : NULL;
  254. }
  255. static int errfile (lua_State *L, const char *filename) {
  256. if (filename == NULL) filename = "stdin";
  257. lua_pushfstring(L, "cannot read %s: %s", filename, lua_fileerror);
  258. return LUA_ERRFILE;
  259. }
  260. LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
  261. LoadF lf;
  262. int status;
  263. int c;
  264. int old_top = lua_gettop(L);
  265. lf.f = (filename == NULL) ? stdin : fopen(filename, "r");
  266. if (lf.f == NULL) return errfile(L, filename); /* unable to open file */
  267. c = ungetc(getc(lf.f), lf.f);
  268. if (!(isspace(c) || isprint(c)) && lf.f != stdin) { /* binary file? */
  269. fclose(lf.f);
  270. lf.f = fopen(filename, "rb"); /* reopen in binary mode */
  271. if (lf.f == NULL) return errfile(L, filename); /* unable to reopen file */
  272. }
  273. if (filename == NULL)
  274. lua_pushliteral(L, "=stdin");
  275. else
  276. lua_pushfstring(L, "@%s", filename);
  277. status = lua_load(L, getF, &lf, lua_tostring(L, -1));
  278. lua_remove(L, old_top+1); /* remove filename from stack */
  279. if (ferror(lf.f)) {
  280. lua_settop(L, old_top); /* ignore results from `lua_load' */
  281. return errfile(L, filename);
  282. }
  283. if (lf.f != stdin)
  284. fclose(lf.f);
  285. return status;
  286. }
  287. typedef struct LoadS {
  288. const char *s;
  289. size_t size;
  290. } LoadS;
  291. static const char *getS (void *ud, size_t *size) {
  292. LoadS *ls = (LoadS *)ud;
  293. if (ls->size == 0) return NULL;
  294. *size = ls->size;
  295. ls->size = 0;
  296. return ls->s;
  297. }
  298. LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t size,
  299. const char *name) {
  300. LoadS ls;
  301. ls.s = buff;
  302. ls.size = size;
  303. return lua_load(L, getS, &ls, name);
  304. }
  305. /* }====================================================== */
  306. /*
  307. ** {======================================================
  308. ** compatibility code
  309. ** =======================================================
  310. */
  311. static void callalert (lua_State *L, int status) {
  312. if (status != 0) {
  313. int top = lua_gettop(L);
  314. lua_getglobal(L, "_ALERT");
  315. lua_insert(L, -2);
  316. lua_pcall(L, 1, 0, 0);
  317. lua_settop(L, top-1);
  318. }
  319. }
  320. LUALIB_API int lua_call (lua_State *L, int nargs, int nresults) {
  321. int status;
  322. int errpos = lua_gettop(L) - nargs;
  323. lua_getglobal(L, "_ERRORMESSAGE");
  324. lua_insert(L, errpos); /* put below function and args */
  325. status = lua_pcall(L, nargs, nresults, errpos);
  326. lua_remove(L, errpos);
  327. callalert(L, status);
  328. return status;
  329. }
  330. static int aux_do (lua_State *L, int status) {
  331. if (status == 0) { /* parse OK? */
  332. int err = lua_gettop(L);
  333. lua_getglobal(L, "_ERRORMESSAGE");
  334. lua_insert(L, err);
  335. status = lua_pcall(L, 0, LUA_MULTRET, err); /* call main */
  336. lua_remove(L, err); /* remove error function */
  337. }
  338. callalert(L, status);
  339. return status;
  340. }
  341. LUALIB_API int lua_dofile (lua_State *L, const char *filename) {
  342. return aux_do(L, luaL_loadfile(L, filename));
  343. }
  344. LUALIB_API int lua_dobuffer (lua_State *L, const char *buff, size_t size,
  345. const char *name) {
  346. return aux_do(L, luaL_loadbuffer(L, buff, size, name));
  347. }
  348. LUALIB_API int lua_dostring (lua_State *L, const char *str) {
  349. return lua_dobuffer(L, str, strlen(str), str);
  350. }
  351. /* }====================================================== */