lauxlib.c 11 KB

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