lauxlib.c 12 KB

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