lauxlib.c 11 KB

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