lauxlib.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /*
  2. ** $Id: lauxlib.c,v 1.89 2002/10/22 18:07:55 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_checkstack (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_checktype (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_checkany (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_checklstring (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_optlstring (lua_State *L, int narg,
  92. const char *def, size_t *len) {
  93. if (lua_isnoneornil(L, narg)) {
  94. if (len)
  95. *len = (def ? strlen(def) : 0);
  96. return def;
  97. }
  98. else return luaL_checklstring(L, narg, len);
  99. }
  100. LUALIB_API lua_Number luaL_checknumber (lua_State *L, int narg) {
  101. lua_Number d = lua_tonumber(L, narg);
  102. if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */
  103. tag_error(L, narg, LUA_TNUMBER);
  104. return d;
  105. }
  106. LUALIB_API lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number def) {
  107. if (lua_isnoneornil(L, narg)) return def;
  108. else return luaL_checknumber(L, narg);
  109. }
  110. LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) {
  111. if (!lua_getmetatable(L, obj)) /* no metatable? */
  112. return 0;
  113. lua_pushstring(L, event);
  114. lua_rawget(L, -2);
  115. if (lua_isnil(L, -1)) {
  116. lua_pop(L, 2); /* remove metatable and metafield */
  117. return 0;
  118. }
  119. return 1;
  120. }
  121. LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {
  122. if (!luaL_getmetafield(L, obj, event)) /* no metafield? */
  123. return 0;
  124. lua_pushvalue(L, obj);
  125. lua_call(L, 1, 1);
  126. return 1;
  127. }
  128. LUALIB_API void luaL_openlib (lua_State *L, const char *libname,
  129. const luaL_reg *l, int nup) {
  130. if (libname) {
  131. lua_pushstring(L, libname);
  132. lua_gettable(L, LUA_GLOBALSINDEX); /* check whether lib already exists */
  133. if (lua_isnil(L, -1)) { /* no? */
  134. lua_pop(L, 1);
  135. lua_newtable(L); /* create it */
  136. }
  137. lua_insert(L, -(nup+1)); /* move library table to below upvalues */
  138. }
  139. for (; l->name; l++) {
  140. int i;
  141. lua_pushstring(L, l->name);
  142. for (i=0; i<nup; i++) /* copy upvalues to the top */
  143. lua_pushvalue(L, -(nup+1));
  144. lua_pushcclosure(L, l->func, nup);
  145. lua_settable(L, -(nup+3));
  146. }
  147. lua_pop(L, nup); /* remove upvalues */
  148. if (libname) {
  149. lua_pushstring(L, libname);
  150. lua_pushvalue(L, -2);
  151. lua_settable(L, LUA_GLOBALSINDEX);
  152. }
  153. }
  154. /*
  155. ** {======================================================
  156. ** Generic Buffer manipulation
  157. ** =======================================================
  158. */
  159. #define buffempty(B) ((B)->p == (B)->buffer)
  160. #define bufflen(B) ((B)->p - (B)->buffer)
  161. #define bufffree(B) ((size_t)(LUAL_BUFFERSIZE - bufflen(B)))
  162. #define LIMIT (LUA_MINSTACK/2)
  163. static int emptybuffer (luaL_Buffer *B) {
  164. size_t l = bufflen(B);
  165. if (l == 0) return 0; /* put nothing on stack */
  166. else {
  167. lua_pushlstring(B->L, B->buffer, l);
  168. B->p = B->buffer;
  169. B->lvl++;
  170. return 1;
  171. }
  172. }
  173. static void adjuststack (luaL_Buffer *B) {
  174. if (B->lvl > 1) {
  175. lua_State *L = B->L;
  176. int toget = 1; /* number of levels to concat */
  177. size_t toplen = lua_strlen(L, -1);
  178. do {
  179. size_t l = lua_strlen(L, -(toget+1));
  180. if (B->lvl - toget + 1 >= LIMIT || toplen > l) {
  181. toplen += l;
  182. toget++;
  183. }
  184. else break;
  185. } while (toget < B->lvl);
  186. lua_concat(L, toget);
  187. B->lvl = B->lvl - toget + 1;
  188. }
  189. }
  190. LUALIB_API char *luaL_prepbuffer (luaL_Buffer *B) {
  191. if (emptybuffer(B))
  192. adjuststack(B);
  193. return B->buffer;
  194. }
  195. LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {
  196. while (l--)
  197. luaL_putchar(B, *s++);
  198. }
  199. LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) {
  200. luaL_addlstring(B, s, strlen(s));
  201. }
  202. LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
  203. emptybuffer(B);
  204. lua_concat(B->L, B->lvl);
  205. B->lvl = 1;
  206. }
  207. LUALIB_API void luaL_addvalue (luaL_Buffer *B) {
  208. lua_State *L = B->L;
  209. size_t vl = lua_strlen(L, -1);
  210. if (vl <= bufffree(B)) { /* fit into buffer? */
  211. memcpy(B->p, lua_tostring(L, -1), vl); /* put it there */
  212. B->p += vl;
  213. lua_pop(L, 1); /* remove from stack */
  214. }
  215. else {
  216. if (emptybuffer(B))
  217. lua_insert(L, -2); /* put buffer before new value */
  218. B->lvl++; /* add new value into B stack */
  219. adjuststack(B);
  220. }
  221. }
  222. LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
  223. B->L = L;
  224. B->p = B->buffer;
  225. B->lvl = 0;
  226. }
  227. /* }====================================================== */
  228. LUALIB_API int luaL_ref (lua_State *L, int t) {
  229. int ref;
  230. if (lua_isnil(L, -1)) {
  231. lua_pop(L, 1); /* remove from stack */
  232. return LUA_REFNIL; /* `nil' has a unique fixed reference */
  233. }
  234. lua_rawgeti(L, t, 0); /* get first free element */
  235. ref = (int)lua_tonumber(L, -1); /* ref = t[0] */
  236. lua_pop(L, 1); /* remove it from stack */
  237. if (ref != 0) { /* any free element? */
  238. lua_rawgeti(L, t, ref); /* remove it from list */
  239. lua_rawseti(L, t, 0); /* (that is, t[0] = t[ref]) */
  240. }
  241. else { /* no free elements */
  242. lua_pushliteral(L, "n");
  243. lua_pushvalue(L, -1);
  244. lua_rawget(L, t); /* get t.n */
  245. ref = (int)lua_tonumber(L, -1) + 1; /* ref = t.n + 1 */
  246. lua_pop(L, 1); /* pop t.n */
  247. lua_pushnumber(L, ref);
  248. lua_rawset(L, t); /* t.n = t.n + 1 */
  249. }
  250. lua_rawseti(L, t, ref);
  251. return ref;
  252. }
  253. LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
  254. if (ref >= 0) {
  255. lua_rawgeti(L, t, 0);
  256. lua_rawseti(L, t, ref); /* t[ref] = t[0] */
  257. lua_pushnumber(L, ref);
  258. lua_rawseti(L, t, 0); /* t[0] = ref */
  259. }
  260. }
  261. /*
  262. ** {======================================================
  263. ** Load functions
  264. ** =======================================================
  265. */
  266. typedef struct LoadF {
  267. FILE *f;
  268. char buff[LUAL_BUFFERSIZE];
  269. } LoadF;
  270. static const char *getF (lua_State *L, void *ud, size_t *size) {
  271. LoadF *lf = (LoadF *)ud;
  272. (void)L;
  273. if (feof(lf->f)) return NULL;
  274. *size = fread(lf->buff, 1, LUAL_BUFFERSIZE, lf->f);
  275. return (*size > 0) ? lf->buff : NULL;
  276. }
  277. static int errfile (lua_State *L, int fnameindex) {
  278. const char *filename = lua_tostring(L, fnameindex) + 1;
  279. lua_pushfstring(L, "cannot read %s: %s", filename, strerror(errno));
  280. lua_remove(L, fnameindex);
  281. return LUA_ERRFILE;
  282. }
  283. LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
  284. LoadF lf;
  285. int status, readstatus;
  286. int c;
  287. int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */
  288. if (filename == NULL) {
  289. lua_pushliteral(L, "=stdin");
  290. lf.f = stdin;
  291. }
  292. else {
  293. lua_pushfstring(L, "@%s", filename);
  294. lf.f = fopen(filename, "r");
  295. }
  296. if (lf.f == NULL) return errfile(L, fnameindex); /* unable to open file */
  297. c = ungetc(getc(lf.f), lf.f);
  298. if (!(isspace(c) || isprint(c)) && lf.f != stdin) { /* binary file? */
  299. fclose(lf.f);
  300. lf.f = fopen(filename, "rb"); /* reopen in binary mode */
  301. if (lf.f == NULL) return errfile(L, fnameindex); /* unable to reopen file */
  302. }
  303. status = lua_load(L, getF, &lf, lua_tostring(L, -1));
  304. readstatus = ferror(lf.f);
  305. if (lf.f != stdin) fclose(lf.f); /* close file (even in case of errors) */
  306. if (readstatus) {
  307. lua_settop(L, fnameindex); /* ignore results from `lua_load' */
  308. return errfile(L, fnameindex);
  309. }
  310. lua_remove(L, fnameindex);
  311. return status;
  312. }
  313. typedef struct LoadS {
  314. const char *s;
  315. size_t size;
  316. } LoadS;
  317. static const char *getS (lua_State *L, void *ud, size_t *size) {
  318. LoadS *ls = (LoadS *)ud;
  319. (void)L;
  320. if (ls->size == 0) return NULL;
  321. *size = ls->size;
  322. ls->size = 0;
  323. return ls->s;
  324. }
  325. LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t size,
  326. const char *name) {
  327. LoadS ls;
  328. ls.s = buff;
  329. ls.size = size;
  330. return lua_load(L, getS, &ls, name);
  331. }
  332. /* }====================================================== */
  333. /*
  334. ** {======================================================
  335. ** compatibility code
  336. ** =======================================================
  337. */
  338. static void callalert (lua_State *L, int status) {
  339. if (status != 0) {
  340. lua_getglobal(L, "_ALERT");
  341. if (lua_isfunction(L, -1)) {
  342. lua_insert(L, -2);
  343. lua_call(L, 1, 0);
  344. }
  345. else { /* no _ALERT function; print it on stderr */
  346. fprintf(stderr, "%s\n", lua_tostring(L, -2));
  347. lua_pop(L, 2); /* remove error message and _ALERT */
  348. }
  349. }
  350. }
  351. static int aux_do (lua_State *L, int status) {
  352. if (status == 0) { /* parse OK? */
  353. status = lua_pcall(L, 0, LUA_MULTRET, 0); /* call main */
  354. }
  355. callalert(L, status);
  356. return status;
  357. }
  358. LUALIB_API int lua_dofile (lua_State *L, const char *filename) {
  359. return aux_do(L, luaL_loadfile(L, filename));
  360. }
  361. LUALIB_API int lua_dobuffer (lua_State *L, const char *buff, size_t size,
  362. const char *name) {
  363. return aux_do(L, luaL_loadbuffer(L, buff, size, name));
  364. }
  365. LUALIB_API int lua_dostring (lua_State *L, const char *str) {
  366. return lua_dobuffer(L, str, strlen(str), str);
  367. }
  368. /* }====================================================== */