lauxlib.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. ** $Id: lauxlib.c,v 1.71 2002/05/16 18:39:46 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) narg--; /* do not count `self' */
  30. if (ar.name == NULL)
  31. ar.name = "?";
  32. return luaL_verror(L, "bad argument #%d to `%s' (%s)",
  33. narg, ar.name, extramsg);
  34. }
  35. LUALIB_API int luaL_typerror (lua_State *L, int narg, const char *tname) {
  36. const char *msg = lua_pushfstring(L, "%s expected, got %s",
  37. tname, lua_typename(L, lua_type(L,narg)));
  38. return luaL_argerror(L, narg, msg);
  39. }
  40. static void tag_error (lua_State *L, int narg, int tag) {
  41. luaL_typerror(L, narg, lua_typename(L, tag));
  42. }
  43. LUALIB_API int luaL_verror (lua_State *L, const char *fmt, ...) {
  44. lua_Debug ar;
  45. const char *msg;
  46. va_list argp;
  47. va_start(argp, fmt);
  48. msg = lua_pushvfstring(L, fmt, argp);
  49. va_end(argp);
  50. if (lua_getstack(L, 1, &ar)) { /* check calling function */
  51. lua_getinfo(L, "Snl", &ar);
  52. if (ar.currentline > 0)
  53. lua_pushfstring(L, "%s:%d: %s", ar.short_src, ar.currentline, msg);
  54. }
  55. return lua_errorobj(L);
  56. }
  57. /* }====================================================== */
  58. LUALIB_API int luaL_findstring (const char *name, const char *const list[]) {
  59. int i;
  60. for (i=0; list[i]; i++)
  61. if (strcmp(list[i], name) == 0)
  62. return i;
  63. return -1; /* name not found */
  64. }
  65. LUALIB_API void luaL_check_stack (lua_State *L, int space, const char *mes) {
  66. if (!lua_checkstack(L, space))
  67. luaL_verror(L, "stack overflow (%s)", mes);
  68. }
  69. LUALIB_API void luaL_check_type (lua_State *L, int narg, int t) {
  70. if (lua_type(L, narg) != t)
  71. tag_error(L, narg, t);
  72. }
  73. LUALIB_API void luaL_check_any (lua_State *L, int narg) {
  74. if (lua_type(L, narg) == LUA_TNONE)
  75. luaL_argerror(L, narg, "value expected");
  76. }
  77. LUALIB_API const char *luaL_check_lstr (lua_State *L, int narg, size_t *len) {
  78. const char *s = lua_tostring(L, narg);
  79. if (!s) tag_error(L, narg, LUA_TSTRING);
  80. if (len) *len = lua_strlen(L, narg);
  81. return s;
  82. }
  83. LUALIB_API const char *luaL_opt_lstr (lua_State *L, int narg, const char *def, size_t *len) {
  84. if (lua_isnoneornil(L, narg)) {
  85. if (len)
  86. *len = (def ? strlen(def) : 0);
  87. return def;
  88. }
  89. else return luaL_check_lstr(L, narg, len);
  90. }
  91. LUALIB_API lua_Number luaL_check_number (lua_State *L, int narg) {
  92. lua_Number d = lua_tonumber(L, narg);
  93. if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */
  94. tag_error(L, narg, LUA_TNUMBER);
  95. return d;
  96. }
  97. LUALIB_API lua_Number luaL_opt_number (lua_State *L, int narg, lua_Number def) {
  98. if (lua_isnoneornil(L, narg)) return def;
  99. else return luaL_check_number(L, narg);
  100. }
  101. LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {
  102. if (!lua_getmetatable(L, obj)) /* no metatable? */
  103. return 0;
  104. lua_pushstring(L, event);
  105. lua_rawget(L, -2);
  106. if (lua_isnil(L, -1)) {
  107. lua_pop(L, 2); /* remove metatable and metafield */
  108. return 0;
  109. }
  110. lua_pushvalue(L, obj);
  111. lua_rawcall(L, 1, 1);
  112. return 1;
  113. }
  114. LUALIB_API void luaL_openlib (lua_State *L, const luaL_reg *l, int nup) {
  115. for (; l->name; l++) {
  116. int i;
  117. lua_pushstring(L, l->name);
  118. for (i=0; i<nup; i++) /* copy upvalues to the top */
  119. lua_pushvalue(L, -(nup+1));
  120. lua_pushcclosure(L, l->func, nup);
  121. lua_settable(L, -(nup+3));
  122. }
  123. lua_pop(L, nup);
  124. }
  125. LUALIB_API void luaL_opennamedlib (lua_State *L, const char *libname,
  126. const luaL_reg *l, int nup) {
  127. lua_pushstring(L, libname);
  128. lua_insert(L, -(nup+1));
  129. lua_newtable(L);
  130. lua_insert(L, -(nup+1));
  131. luaL_openlib(L, l, nup);
  132. lua_settable(L, LUA_GLOBALSINDEX);
  133. }
  134. /*
  135. ** {======================================================
  136. ** Generic Buffer manipulation
  137. ** =======================================================
  138. */
  139. #define buffempty(B) ((B)->p == (B)->buffer)
  140. #define bufflen(B) ((B)->p - (B)->buffer)
  141. #define bufffree(B) ((size_t)(LUAL_BUFFERSIZE - bufflen(B)))
  142. #define LIMIT (LUA_MINSTACK/2)
  143. static int emptybuffer (luaL_Buffer *B) {
  144. size_t l = bufflen(B);
  145. if (l == 0) return 0; /* put nothing on stack */
  146. else {
  147. lua_pushlstring(B->L, B->buffer, l);
  148. B->p = B->buffer;
  149. B->level++;
  150. return 1;
  151. }
  152. }
  153. static void adjuststack (luaL_Buffer *B) {
  154. if (B->level > 1) {
  155. lua_State *L = B->L;
  156. int toget = 1; /* number of levels to concat */
  157. size_t toplen = lua_strlen(L, -1);
  158. do {
  159. size_t l = lua_strlen(L, -(toget+1));
  160. if (B->level - toget + 1 >= LIMIT || toplen > l) {
  161. toplen += l;
  162. toget++;
  163. }
  164. else break;
  165. } while (toget < B->level);
  166. lua_concat(L, toget);
  167. B->level = B->level - toget + 1;
  168. }
  169. }
  170. LUALIB_API char *luaL_prepbuffer (luaL_Buffer *B) {
  171. if (emptybuffer(B))
  172. adjuststack(B);
  173. return B->buffer;
  174. }
  175. LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {
  176. while (l--)
  177. luaL_putchar(B, *s++);
  178. }
  179. LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) {
  180. luaL_addlstring(B, s, strlen(s));
  181. }
  182. LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
  183. emptybuffer(B);
  184. lua_concat(B->L, B->level);
  185. B->level = 1;
  186. }
  187. LUALIB_API void luaL_addvalue (luaL_Buffer *B) {
  188. lua_State *L = B->L;
  189. size_t vl = lua_strlen(L, -1);
  190. if (vl <= bufffree(B)) { /* fit into buffer? */
  191. memcpy(B->p, lua_tostring(L, -1), vl); /* put it there */
  192. B->p += vl;
  193. lua_pop(L, 1); /* remove from stack */
  194. }
  195. else {
  196. if (emptybuffer(B))
  197. lua_insert(L, -2); /* put buffer before new value */
  198. B->level++; /* add new value into B stack */
  199. adjuststack(B);
  200. }
  201. }
  202. LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
  203. B->L = L;
  204. B->p = B->buffer;
  205. B->level = 0;
  206. }
  207. /* }====================================================== */
  208. LUALIB_API int luaL_ref (lua_State *L, int t) {
  209. int ref;
  210. lua_rawgeti(L, t, 0); /* get first free element */
  211. ref = (int)lua_tonumber(L, -1);
  212. lua_pop(L, 1); /* remove it from stack */
  213. if (ref != 0) { /* any free element? */
  214. lua_rawgeti(L, t, ref); /* remove it from list */
  215. lua_rawseti(L, t, 0);
  216. }
  217. else { /* no free elements */
  218. ref = lua_getn(L, t) + 1; /* use next `n' */
  219. lua_pushliteral(L, "n");
  220. lua_pushnumber(L, ref);
  221. lua_rawset(L, t); /* n = n+1 */
  222. }
  223. lua_rawseti(L, t, ref);
  224. return ref;
  225. }
  226. LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
  227. if (ref >= 0) {
  228. lua_rawgeti(L, t, 0);
  229. lua_pushnumber(L, ref);
  230. lua_rawseti(L, t, 0);
  231. lua_rawseti(L, t, ref);
  232. }
  233. }
  234. /*
  235. ** {======================================================
  236. ** Load functions
  237. ** =======================================================
  238. */
  239. typedef struct LoadF {
  240. FILE *f;
  241. char buff[LUAL_BUFFERSIZE];
  242. } LoadF;
  243. static const char *getF (void *ud, size_t *size) {
  244. LoadF *lf = (LoadF *)ud;
  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 = lua_gettop(L);
  307. lua_getglobal(L, "_ALERT");
  308. lua_insert(L, -2);
  309. lua_pcall(L, 1, 0, 0);
  310. lua_settop(L, top-1);
  311. }
  312. }
  313. LUALIB_API int lua_call (lua_State *L, int nargs, int nresults) {
  314. int status;
  315. int errpos = lua_gettop(L) - nargs;
  316. lua_getglobal(L, "_ERRORMESSAGE");
  317. lua_insert(L, errpos); /* put below function and args */
  318. status = lua_pcall(L, nargs, nresults, errpos);
  319. lua_remove(L, errpos);
  320. callalert(L, status);
  321. return status;
  322. }
  323. static int aux_do (lua_State *L, int status) {
  324. if (status == 0) { /* parse OK? */
  325. int err = lua_gettop(L);
  326. lua_getglobal(L, "_ERRORMESSAGE");
  327. lua_insert(L, err);
  328. status = lua_pcall(L, 0, LUA_MULTRET, err); /* call main */
  329. lua_remove(L, err); /* remove error function */
  330. }
  331. callalert(L, status);
  332. return status;
  333. }
  334. LUALIB_API int lua_dofile (lua_State *L, const char *filename) {
  335. return aux_do(L, luaL_loadfile(L, filename));
  336. }
  337. LUALIB_API int lua_dobuffer (lua_State *L, const char *buff, size_t size,
  338. const char *name) {
  339. return aux_do(L, luaL_loadbuffer(L, buff, size, name));
  340. }
  341. LUALIB_API int lua_dostring (lua_State *L, const char *str) {
  342. return lua_dobuffer(L, str, strlen(str), str);
  343. }
  344. /* }====================================================== */