lauxlib.c 10 KB

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