lauxlib.c 11 KB

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