lauxlib.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. /*
  2. ** $Id: lauxlib.c,v 1.126 2004/09/29 21:03:14 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 <stdlib.h>
  11. #include <string.h>
  12. /* This file uses only the official API of Lua.
  13. ** Any function declared here could be written as an application function.
  14. */
  15. #define lauxlib_c
  16. #define LUA_LIB
  17. #include "lua.h"
  18. #include "lauxlib.h"
  19. /* number of prereserved references (for internal use) */
  20. #define RESERVED_REFS 2
  21. /* reserved references */
  22. #define FREELIST_REF 1 /* free list of references */
  23. #define ARRAYSIZE_REF 2 /* array sizes */
  24. /* convert a stack index to positive */
  25. #define abs_index(L, i) ((i) > 0 || (i) <= LUA_REGISTRYINDEX ? (i) : \
  26. lua_gettop(L) + (i) + 1)
  27. /*
  28. ** {======================================================
  29. ** Error-report functions
  30. ** =======================================================
  31. */
  32. LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) {
  33. lua_Debug ar;
  34. lua_getstack(L, 0, &ar);
  35. lua_getinfo(L, "n", &ar);
  36. if (strcmp(ar.namewhat, "method") == 0) {
  37. narg--; /* do not count `self' */
  38. if (narg == 0) /* error is in the self argument itself? */
  39. return luaL_error(L, "calling `%s' on bad self (%s)", ar.name, extramsg);
  40. }
  41. if (ar.name == NULL)
  42. ar.name = "?";
  43. return luaL_error(L, "bad argument #%d to `%s' (%s)",
  44. narg, ar.name, extramsg);
  45. }
  46. LUALIB_API int luaL_typerror (lua_State *L, int narg, const char *tname) {
  47. const char *msg = lua_pushfstring(L, "%s expected, got %s",
  48. tname, luaL_typename(L, narg));
  49. return luaL_argerror(L, narg, msg);
  50. }
  51. static void tag_error (lua_State *L, int narg, int tag) {
  52. luaL_typerror(L, narg, lua_typename(L, tag));
  53. }
  54. LUALIB_API void luaL_where (lua_State *L, int level) {
  55. lua_Debug ar;
  56. if (lua_getstack(L, level, &ar)) { /* check function at level */
  57. lua_getinfo(L, "Sl", &ar); /* get info about it */
  58. if (ar.currentline > 0) { /* is there info? */
  59. lua_pushfstring(L, "%s:%d: ", ar.short_src, ar.currentline);
  60. return;
  61. }
  62. }
  63. lua_pushliteral(L, ""); /* else, no information available... */
  64. }
  65. LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) {
  66. va_list argp;
  67. va_start(argp, fmt);
  68. luaL_where(L, 1);
  69. lua_pushvfstring(L, fmt, argp);
  70. va_end(argp);
  71. lua_concat(L, 2);
  72. return lua_error(L);
  73. }
  74. /* }====================================================== */
  75. LUALIB_API int luaL_findstring (const char *name, const char *const list[]) {
  76. int i;
  77. for (i=0; list[i]; i++)
  78. if (strcmp(list[i], name) == 0)
  79. return i;
  80. return -1; /* name not found */
  81. }
  82. LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) {
  83. lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get registry.name */
  84. if (!lua_isnil(L, -1)) /* name already in use? */
  85. return 0; /* leave previous value on top, but return 0 */
  86. lua_pop(L, 1);
  87. lua_newtable(L); /* create metatable */
  88. lua_pushvalue(L, -1);
  89. lua_setfield(L, LUA_REGISTRYINDEX, tname); /* registry.name = metatable */
  90. lua_pushvalue(L, -1);
  91. lua_pushstring(L, tname);
  92. lua_rawset(L, LUA_REGISTRYINDEX); /* registry[metatable] = name */
  93. return 1;
  94. }
  95. LUALIB_API void luaL_getmetatable (lua_State *L, const char *tname) {
  96. lua_getfield(L, LUA_REGISTRYINDEX, tname);
  97. }
  98. LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) {
  99. const char *tn;
  100. if (!lua_getmetatable(L, ud)) return NULL; /* no metatable? */
  101. lua_rawget(L, LUA_REGISTRYINDEX); /* get registry[metatable] */
  102. tn = lua_tostring(L, -1);
  103. if (tn && (strcmp(tn, tname) == 0)) {
  104. lua_pop(L, 1);
  105. return lua_touserdata(L, ud);
  106. }
  107. else {
  108. lua_pop(L, 1);
  109. return NULL;
  110. }
  111. }
  112. LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *mes) {
  113. if (!lua_checkstack(L, space))
  114. luaL_error(L, "stack overflow (%s)", mes);
  115. }
  116. LUALIB_API void luaL_checktype (lua_State *L, int narg, int t) {
  117. if (lua_type(L, narg) != t)
  118. tag_error(L, narg, t);
  119. }
  120. LUALIB_API void luaL_checkany (lua_State *L, int narg) {
  121. if (lua_type(L, narg) == LUA_TNONE)
  122. luaL_argerror(L, narg, "value expected");
  123. }
  124. LUALIB_API const char *luaL_checklstring (lua_State *L, int narg, size_t *len) {
  125. const char *s = lua_tostring(L, narg);
  126. if (!s) tag_error(L, narg, LUA_TSTRING);
  127. if (len) *len = lua_strlen(L, narg);
  128. return s;
  129. }
  130. LUALIB_API const char *luaL_optlstring (lua_State *L, int narg,
  131. const char *def, size_t *len) {
  132. if (lua_isnoneornil(L, narg)) {
  133. if (len)
  134. *len = (def ? strlen(def) : 0);
  135. return def;
  136. }
  137. else return luaL_checklstring(L, narg, len);
  138. }
  139. LUALIB_API lua_Number luaL_checknumber (lua_State *L, int narg) {
  140. lua_Number d = lua_tonumber(L, narg);
  141. if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */
  142. tag_error(L, narg, LUA_TNUMBER);
  143. return d;
  144. }
  145. LUALIB_API lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number def) {
  146. if (lua_isnoneornil(L, narg)) return def;
  147. else return luaL_checknumber(L, narg);
  148. }
  149. LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) {
  150. lua_Integer d = lua_tointeger(L, narg);
  151. if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */
  152. tag_error(L, narg, LUA_TNUMBER);
  153. return d;
  154. }
  155. LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int narg,
  156. lua_Integer def) {
  157. if (lua_isnoneornil(L, narg)) return def;
  158. else return luaL_checkinteger(L, narg);
  159. }
  160. LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) {
  161. if (!lua_getmetatable(L, obj)) /* no metatable? */
  162. return 0;
  163. lua_getfield(L, -1, event);
  164. if (lua_isnil(L, -1)) {
  165. lua_pop(L, 2); /* remove metatable and metafield */
  166. return 0;
  167. }
  168. else {
  169. lua_remove(L, -2); /* remove only metatable */
  170. return 1;
  171. }
  172. }
  173. LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {
  174. obj = abs_index(L, obj);
  175. if (!luaL_getmetafield(L, obj, event)) /* no metafield? */
  176. return 0;
  177. lua_pushvalue(L, obj);
  178. lua_call(L, 1, 1);
  179. return 1;
  180. }
  181. LUALIB_API void luaL_openlib (lua_State *L, const char *libname,
  182. const luaL_reg *l, int nup) {
  183. if (libname) {
  184. /* check whether lib already exists */
  185. luaL_getfield(L, LUA_GLOBALSINDEX, libname);
  186. if (lua_isnil(L, -1)) { /* not found? */
  187. lua_pop(L, 1); /* remove previous result */
  188. lua_newtable(L); /* create it */
  189. if (lua_getmetatable(L, LUA_GLOBALSINDEX))
  190. lua_setmetatable(L, -2); /* share metatable with global table */
  191. /* register it with given name */
  192. lua_pushvalue(L, -1);
  193. luaL_setfield(L, LUA_GLOBALSINDEX, libname);
  194. }
  195. else if (!lua_istable(L, -1))
  196. luaL_error(L, "name conflict for library `%s'", libname);
  197. lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
  198. lua_pushvalue(L, -2);
  199. lua_setfield(L, -2, libname); /* _LOADED[modname] = new table */
  200. lua_pop(L, 1); /* remove _LOADED table */
  201. lua_insert(L, -(nup+1)); /* move library table to below upvalues */
  202. }
  203. for (; l->name; l++) {
  204. int i;
  205. for (i=0; i<nup; i++) /* copy upvalues to the top */
  206. lua_pushvalue(L, -nup);
  207. lua_pushcclosure(L, l->func, nup);
  208. lua_setfield(L, -(nup+2), l->name);
  209. }
  210. lua_pop(L, nup); /* remove upvalues */
  211. }
  212. /*
  213. ** {======================================================
  214. ** getn-setn: size for arrays
  215. ** =======================================================
  216. */
  217. static int checkint (lua_State *L, int topop) {
  218. int n = (lua_type(L, -1) == LUA_TNUMBER) ? lua_tointeger(L, -1) : -1;
  219. lua_pop(L, topop);
  220. return n;
  221. }
  222. static void getsizes (lua_State *L) {
  223. lua_rawgeti(L, LUA_REGISTRYINDEX, ARRAYSIZE_REF);
  224. if (lua_isnil(L, -1)) { /* no `size' table? */
  225. lua_pop(L, 1); /* remove nil */
  226. lua_newtable(L); /* create it */
  227. lua_pushvalue(L, -1); /* `size' will be its own metatable */
  228. lua_setmetatable(L, -2);
  229. lua_pushliteral(L, "kv");
  230. lua_setfield(L, -2, "__mode"); /* metatable(N).__mode = "kv" */
  231. lua_pushvalue(L, -1);
  232. lua_rawseti(L, LUA_REGISTRYINDEX, ARRAYSIZE_REF); /* store in register */
  233. }
  234. }
  235. LUALIB_API void luaL_setn (lua_State *L, int t, int n) {
  236. t = abs_index(L, t);
  237. getsizes(L);
  238. lua_pushvalue(L, t);
  239. lua_pushinteger(L, n);
  240. lua_rawset(L, -3); /* sizes[t] = n */
  241. lua_pop(L, 1); /* remove `sizes' */
  242. }
  243. /* find an `n' such that t[n] ~= nil and t[n+1] == nil */
  244. static int countn (lua_State *L, int t) {
  245. int i = LUA_FIRSTINDEX - 1;
  246. int j = 2;
  247. /* find `i' such that i <= n < i*2 (= j) */
  248. for (;;) {
  249. lua_rawgeti(L, t, j);
  250. if (lua_isnil(L, -1)) break;
  251. lua_pop(L, 1);
  252. i = j;
  253. j = i*2;
  254. }
  255. lua_pop(L, 1);
  256. /* i <= n < j; do a binary search */
  257. while (i < j-1) {
  258. int m = (i+j)/2;
  259. lua_rawgeti(L, t, m);
  260. if (lua_isnil(L, -1)) j = m;
  261. else i = m;
  262. lua_pop(L, 1);
  263. }
  264. return i - LUA_FIRSTINDEX + 1;
  265. }
  266. LUALIB_API int luaL_getn (lua_State *L, int t) {
  267. int n;
  268. t = abs_index(L, t);
  269. getsizes(L); /* try sizes[t] */
  270. lua_pushvalue(L, t);
  271. lua_rawget(L, -2);
  272. if ((n = checkint(L, 2)) >= 0) return n;
  273. lua_getfield(L, t, "n"); /* else try t.n */
  274. if ((n = checkint(L, 1)) >= 0) return n;
  275. return countn(L, t);
  276. }
  277. /* }====================================================== */
  278. static const char *pushnexttemplate (lua_State *L, const char *path) {
  279. const char *l;
  280. if (*path == '\0') return NULL; /* no more templates */
  281. if (*path == LUA_PATH_SEP) path++; /* skip separator */
  282. l = strchr(path, LUA_PATH_SEP); /* find next separator */
  283. if (l == NULL) l = path+strlen(path);
  284. lua_pushlstring(L, path, l - path); /* template */
  285. return l;
  286. }
  287. LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p,
  288. const char *r) {
  289. const char *wild;
  290. int l = strlen(p);
  291. luaL_Buffer b;
  292. luaL_buffinit(L, &b);
  293. while ((wild = strstr(s, p)) != NULL) {
  294. luaL_addlstring(&b, s, wild - s); /* push prefix */
  295. luaL_addstring(&b, r); /* push replacement in place of pattern */
  296. s = wild + l; /* continue after `p' */
  297. }
  298. luaL_addstring(&b, s); /* push last suffix (`n' already includes this) */
  299. luaL_pushresult(&b);
  300. return lua_tostring(L, -1);
  301. }
  302. static int readable (const char *fname) {
  303. int err;
  304. FILE *f = fopen(fname, "r"); /* try to open file */
  305. if (f == NULL) return 0; /* open failed */
  306. getc(f); /* try to read it */
  307. err = ferror(f);
  308. fclose(f);
  309. return (err == 0);
  310. }
  311. LUALIB_API const char *luaL_searchpath (lua_State *L, const char *name,
  312. const char *path) {
  313. const char *p = path;
  314. for (;;) {
  315. const char *fname;
  316. if ((p = pushnexttemplate(L, p)) == NULL) {
  317. lua_pushfstring(L, "no readable `%s' in path `%s'", name, path);
  318. return NULL;
  319. }
  320. fname = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name);
  321. lua_remove(L, -2); /* remove path template */
  322. if (readable(fname)) /* does file exist and is readable? */
  323. return fname; /* return that file name */
  324. lua_pop(L, 1); /* remove file name */
  325. }
  326. }
  327. LUALIB_API const char *luaL_getfield (lua_State *L, int idx,
  328. const char *fname) {
  329. const char *e;
  330. lua_pushvalue(L, idx);
  331. while ((e = strchr(fname, '.')) != NULL) {
  332. lua_pushlstring(L, fname, e - fname);
  333. lua_rawget(L, -2);
  334. lua_remove(L, -2); /* remove previous table */
  335. fname = e + 1;
  336. if (!lua_istable(L, -1)) return fname;
  337. }
  338. lua_pushstring(L, fname);
  339. lua_rawget(L, -2); /* get last field */
  340. lua_remove(L, -2); /* remove previous table */
  341. return NULL;
  342. }
  343. LUALIB_API const char *luaL_setfield (lua_State *L, int idx,
  344. const char *fname) {
  345. const char *e;
  346. lua_pushvalue(L, idx);
  347. while ((e = strchr(fname, '.')) != NULL) {
  348. lua_pushlstring(L, fname, e - fname);
  349. lua_rawget(L, -2);
  350. if (lua_isnil(L, -1)) { /* no such field? */
  351. lua_pop(L, 1); /* remove this nil */
  352. lua_newtable(L); /* create a new table for field */
  353. lua_pushlstring(L, fname, e - fname);
  354. lua_pushvalue(L, -2);
  355. lua_settable(L, -4); /* set new table into field */
  356. }
  357. lua_remove(L, -2); /* remove previous table */
  358. fname = e + 1;
  359. if (!lua_istable(L, -1)) {
  360. lua_pop(L, 2); /* remove table and value */
  361. return fname;
  362. }
  363. }
  364. lua_pushstring(L, fname);
  365. lua_pushvalue(L, -3); /* move value to the top */
  366. lua_rawset(L, -3); /* set last field */
  367. lua_pop(L, 2); /* remove value and table */
  368. return NULL;
  369. }
  370. /*
  371. ** {======================================================
  372. ** Generic Buffer manipulation
  373. ** =======================================================
  374. */
  375. #define bufflen(B) ((B)->p - (B)->buffer)
  376. #define bufffree(B) ((size_t)(LUAL_BUFFERSIZE - bufflen(B)))
  377. #define LIMIT (LUA_MINSTACK/2)
  378. static int emptybuffer (luaL_Buffer *B) {
  379. size_t l = bufflen(B);
  380. if (l == 0) return 0; /* put nothing on stack */
  381. else {
  382. lua_pushlstring(B->L, B->buffer, l);
  383. B->p = B->buffer;
  384. B->lvl++;
  385. return 1;
  386. }
  387. }
  388. static void adjuststack (luaL_Buffer *B) {
  389. if (B->lvl > 1) {
  390. lua_State *L = B->L;
  391. int toget = 1; /* number of levels to concat */
  392. size_t toplen = lua_strlen(L, -1);
  393. do {
  394. size_t l = lua_strlen(L, -(toget+1));
  395. if (B->lvl - toget + 1 >= LIMIT || toplen > l) {
  396. toplen += l;
  397. toget++;
  398. }
  399. else break;
  400. } while (toget < B->lvl);
  401. lua_concat(L, toget);
  402. B->lvl = B->lvl - toget + 1;
  403. }
  404. }
  405. LUALIB_API char *luaL_prepbuffer (luaL_Buffer *B) {
  406. if (emptybuffer(B))
  407. adjuststack(B);
  408. return B->buffer;
  409. }
  410. LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {
  411. while (l--)
  412. luaL_putchar(B, *s++);
  413. }
  414. LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) {
  415. luaL_addlstring(B, s, strlen(s));
  416. }
  417. LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
  418. emptybuffer(B);
  419. lua_concat(B->L, B->lvl);
  420. B->lvl = 1;
  421. }
  422. LUALIB_API void luaL_addvalue (luaL_Buffer *B) {
  423. lua_State *L = B->L;
  424. size_t vl = lua_strlen(L, -1);
  425. if (vl <= bufffree(B)) { /* fit into buffer? */
  426. memcpy(B->p, lua_tostring(L, -1), vl); /* put it there */
  427. B->p += vl;
  428. lua_pop(L, 1); /* remove from stack */
  429. }
  430. else {
  431. if (emptybuffer(B))
  432. lua_insert(L, -2); /* put buffer before new value */
  433. B->lvl++; /* add new value into B stack */
  434. adjuststack(B);
  435. }
  436. }
  437. LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
  438. B->L = L;
  439. B->p = B->buffer;
  440. B->lvl = 0;
  441. }
  442. /* }====================================================== */
  443. LUALIB_API int luaL_ref (lua_State *L, int t) {
  444. int ref;
  445. t = abs_index(L, t);
  446. if (lua_isnil(L, -1)) {
  447. lua_pop(L, 1); /* remove from stack */
  448. return LUA_REFNIL; /* `nil' has a unique fixed reference */
  449. }
  450. lua_rawgeti(L, t, FREELIST_REF); /* get first free element */
  451. ref = (int)lua_tointeger(L, -1); /* ref = t[FREELIST_REF] */
  452. lua_pop(L, 1); /* remove it from stack */
  453. if (ref != 0) { /* any free element? */
  454. lua_rawgeti(L, t, ref); /* remove it from list */
  455. lua_rawseti(L, t, FREELIST_REF); /* (t[FREELIST_REF] = t[ref]) */
  456. }
  457. else { /* no free elements */
  458. ref = luaL_getn(L, t);
  459. if (ref < RESERVED_REFS)
  460. ref = RESERVED_REFS; /* skip reserved references */
  461. ref++; /* create new reference */
  462. luaL_setn(L, t, ref);
  463. }
  464. lua_rawseti(L, t, ref);
  465. return ref;
  466. }
  467. LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
  468. if (ref >= 0) {
  469. t = abs_index(L, t);
  470. lua_rawgeti(L, t, FREELIST_REF);
  471. lua_rawseti(L, t, ref); /* t[ref] = t[FREELIST_REF] */
  472. lua_pushinteger(L, ref);
  473. lua_rawseti(L, t, FREELIST_REF); /* t[FREELIST_REF] = ref */
  474. }
  475. }
  476. /*
  477. ** {======================================================
  478. ** Load functions
  479. ** =======================================================
  480. */
  481. typedef struct LoadF {
  482. int extraline;
  483. FILE *f;
  484. char buff[LUAL_BUFFERSIZE];
  485. } LoadF;
  486. static const char *getF (lua_State *L, void *ud, size_t *size) {
  487. LoadF *lf = (LoadF *)ud;
  488. (void)L;
  489. if (lf->extraline) {
  490. lf->extraline = 0;
  491. *size = 1;
  492. return "\n";
  493. }
  494. if (feof(lf->f)) return NULL;
  495. *size = fread(lf->buff, 1, LUAL_BUFFERSIZE, lf->f);
  496. return (*size > 0) ? lf->buff : NULL;
  497. }
  498. static int errfile (lua_State *L, const char *what, int fnameindex) {
  499. const char *serr = strerror(errno);
  500. const char *filename = lua_tostring(L, fnameindex) + 1;
  501. lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr);
  502. lua_remove(L, fnameindex);
  503. return LUA_ERRFILE;
  504. }
  505. LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
  506. LoadF lf;
  507. int status, readstatus;
  508. int c;
  509. int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */
  510. lf.extraline = 0;
  511. if (filename == NULL) {
  512. lua_pushliteral(L, "=stdin");
  513. lf.f = stdin;
  514. }
  515. else {
  516. lua_pushfstring(L, "@%s", filename);
  517. lf.f = fopen(filename, "r");
  518. if (lf.f == NULL) return errfile(L, "open", fnameindex);
  519. }
  520. c = getc(lf.f);
  521. if (c == '#') { /* Unix exec. file? */
  522. lf.extraline = 1;
  523. while ((c = getc(lf.f)) != EOF && c != '\n') ; /* skip first line */
  524. if (c == '\n') c = getc(lf.f);
  525. }
  526. if (c == LUA_SIGNATURE[0] && lf.f != stdin) { /* binary file? */
  527. fclose(lf.f);
  528. lf.f = fopen(filename, "rb"); /* reopen in binary mode */
  529. if (lf.f == NULL) return errfile(L, "reopen", fnameindex);
  530. /* skip eventual `#!...' */
  531. while ((c = getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) ;
  532. lf.extraline = 0;
  533. }
  534. ungetc(c, lf.f);
  535. status = lua_load(L, getF, &lf, lua_tostring(L, -1));
  536. readstatus = ferror(lf.f);
  537. if (lf.f != stdin) fclose(lf.f); /* close file (even in case of errors) */
  538. if (readstatus) {
  539. lua_settop(L, fnameindex); /* ignore results from `lua_load' */
  540. return errfile(L, "read", fnameindex);
  541. }
  542. lua_remove(L, fnameindex);
  543. return status;
  544. }
  545. typedef struct LoadS {
  546. const char *s;
  547. size_t size;
  548. } LoadS;
  549. static const char *getS (lua_State *L, void *ud, size_t *size) {
  550. LoadS *ls = (LoadS *)ud;
  551. (void)L;
  552. if (ls->size == 0) return NULL;
  553. *size = ls->size;
  554. ls->size = 0;
  555. return ls->s;
  556. }
  557. LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t size,
  558. const char *name) {
  559. LoadS ls;
  560. ls.s = buff;
  561. ls.size = size;
  562. return lua_load(L, getS, &ls, name);
  563. }
  564. /* }====================================================== */
  565. static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
  566. (void)ud;
  567. (void)osize;
  568. if (nsize == 0) {
  569. free(ptr);
  570. return NULL;
  571. }
  572. else
  573. return realloc(ptr, nsize);
  574. }
  575. static int panic (lua_State *L) {
  576. fprintf(stderr, "PANIC: unprotected error during Lua-API call\n");
  577. return 0;
  578. }
  579. LUALIB_API lua_State *luaL_newstate (void) {
  580. lua_State *L = lua_newstate(l_alloc, NULL);
  581. lua_atpanic(L, &panic);
  582. return L;
  583. }