diff src/lua/lauxlib.c @ 11:27763b933818

raise lua sources up one level
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 11:07:39 -0600
parents src/lua/src/lauxlib.c@f9f4f1b99eed
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/lua/lauxlib.c	Sat Mar 03 11:07:39 2012 -0600
     1.3 @@ -0,0 +1,652 @@
     1.4 +/*
     1.5 +** $Id: lauxlib.c,v 1.159.1.3 2008/01/21 13:20:51 roberto Exp $
     1.6 +** Auxiliary functions for building Lua libraries
     1.7 +** See Copyright Notice in lua.h
     1.8 +*/
     1.9 +
    1.10 +
    1.11 +#include <ctype.h>
    1.12 +#include <errno.h>
    1.13 +#include <stdarg.h>
    1.14 +#include <stdio.h>
    1.15 +#include <stdlib.h>
    1.16 +#include <string.h>
    1.17 +
    1.18 +
    1.19 +/* This file uses only the official API of Lua.
    1.20 +** Any function declared here could be written as an application function.
    1.21 +*/
    1.22 +
    1.23 +#define lauxlib_c
    1.24 +#define LUA_LIB
    1.25 +
    1.26 +#include "lua.h"
    1.27 +
    1.28 +#include "lauxlib.h"
    1.29 +
    1.30 +
    1.31 +#define FREELIST_REF	0	/* free list of references */
    1.32 +
    1.33 +
    1.34 +/* convert a stack index to positive */
    1.35 +#define abs_index(L, i)		((i) > 0 || (i) <= LUA_REGISTRYINDEX ? (i) : \
    1.36 +					lua_gettop(L) + (i) + 1)
    1.37 +
    1.38 +
    1.39 +/*
    1.40 +** {======================================================
    1.41 +** Error-report functions
    1.42 +** =======================================================
    1.43 +*/
    1.44 +
    1.45 +
    1.46 +LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) {
    1.47 +  lua_Debug ar;
    1.48 +  if (!lua_getstack(L, 0, &ar))  /* no stack frame? */
    1.49 +    return luaL_error(L, "bad argument #%d (%s)", narg, extramsg);
    1.50 +  lua_getinfo(L, "n", &ar);
    1.51 +  if (strcmp(ar.namewhat, "method") == 0) {
    1.52 +    narg--;  /* do not count `self' */
    1.53 +    if (narg == 0)  /* error is in the self argument itself? */
    1.54 +      return luaL_error(L, "calling " LUA_QS " on bad self (%s)",
    1.55 +                           ar.name, extramsg);
    1.56 +  }
    1.57 +  if (ar.name == NULL)
    1.58 +    ar.name = "?";
    1.59 +  return luaL_error(L, "bad argument #%d to " LUA_QS " (%s)",
    1.60 +                        narg, ar.name, extramsg);
    1.61 +}
    1.62 +
    1.63 +
    1.64 +LUALIB_API int luaL_typerror (lua_State *L, int narg, const char *tname) {
    1.65 +  const char *msg = lua_pushfstring(L, "%s expected, got %s",
    1.66 +                                    tname, luaL_typename(L, narg));
    1.67 +  return luaL_argerror(L, narg, msg);
    1.68 +}
    1.69 +
    1.70 +
    1.71 +static void tag_error (lua_State *L, int narg, int tag) {
    1.72 +  luaL_typerror(L, narg, lua_typename(L, tag));
    1.73 +}
    1.74 +
    1.75 +
    1.76 +LUALIB_API void luaL_where (lua_State *L, int level) {
    1.77 +  lua_Debug ar;
    1.78 +  if (lua_getstack(L, level, &ar)) {  /* check function at level */
    1.79 +    lua_getinfo(L, "Sl", &ar);  /* get info about it */
    1.80 +    if (ar.currentline > 0) {  /* is there info? */
    1.81 +      lua_pushfstring(L, "%s:%d: ", ar.short_src, ar.currentline);
    1.82 +      return;
    1.83 +    }
    1.84 +  }
    1.85 +  lua_pushliteral(L, "");  /* else, no information available... */
    1.86 +}
    1.87 +
    1.88 +
    1.89 +LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) {
    1.90 +  va_list argp;
    1.91 +  va_start(argp, fmt);
    1.92 +  luaL_where(L, 1);
    1.93 +  lua_pushvfstring(L, fmt, argp);
    1.94 +  va_end(argp);
    1.95 +  lua_concat(L, 2);
    1.96 +  return lua_error(L);
    1.97 +}
    1.98 +
    1.99 +/* }====================================================== */
   1.100 +
   1.101 +
   1.102 +LUALIB_API int luaL_checkoption (lua_State *L, int narg, const char *def,
   1.103 +                                 const char *const lst[]) {
   1.104 +  const char *name = (def) ? luaL_optstring(L, narg, def) :
   1.105 +                             luaL_checkstring(L, narg);
   1.106 +  int i;
   1.107 +  for (i=0; lst[i]; i++)
   1.108 +    if (strcmp(lst[i], name) == 0)
   1.109 +      return i;
   1.110 +  return luaL_argerror(L, narg,
   1.111 +                       lua_pushfstring(L, "invalid option " LUA_QS, name));
   1.112 +}
   1.113 +
   1.114 +
   1.115 +LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) {
   1.116 +  lua_getfield(L, LUA_REGISTRYINDEX, tname);  /* get registry.name */
   1.117 +  if (!lua_isnil(L, -1))  /* name already in use? */
   1.118 +    return 0;  /* leave previous value on top, but return 0 */
   1.119 +  lua_pop(L, 1);
   1.120 +  lua_newtable(L);  /* create metatable */
   1.121 +  lua_pushvalue(L, -1);
   1.122 +  lua_setfield(L, LUA_REGISTRYINDEX, tname);  /* registry.name = metatable */
   1.123 +  return 1;
   1.124 +}
   1.125 +
   1.126 +
   1.127 +LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) {
   1.128 +  void *p = lua_touserdata(L, ud);
   1.129 +  if (p != NULL) {  /* value is a userdata? */
   1.130 +    if (lua_getmetatable(L, ud)) {  /* does it have a metatable? */
   1.131 +      lua_getfield(L, LUA_REGISTRYINDEX, tname);  /* get correct metatable */
   1.132 +      if (lua_rawequal(L, -1, -2)) {  /* does it have the correct mt? */
   1.133 +        lua_pop(L, 2);  /* remove both metatables */
   1.134 +        return p;
   1.135 +      }
   1.136 +    }
   1.137 +  }
   1.138 +  luaL_typerror(L, ud, tname);  /* else error */
   1.139 +  return NULL;  /* to avoid warnings */
   1.140 +}
   1.141 +
   1.142 +
   1.143 +LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *mes) {
   1.144 +  if (!lua_checkstack(L, space))
   1.145 +    luaL_error(L, "stack overflow (%s)", mes);
   1.146 +}
   1.147 +
   1.148 +
   1.149 +LUALIB_API void luaL_checktype (lua_State *L, int narg, int t) {
   1.150 +  if (lua_type(L, narg) != t)
   1.151 +    tag_error(L, narg, t);
   1.152 +}
   1.153 +
   1.154 +
   1.155 +LUALIB_API void luaL_checkany (lua_State *L, int narg) {
   1.156 +  if (lua_type(L, narg) == LUA_TNONE)
   1.157 +    luaL_argerror(L, narg, "value expected");
   1.158 +}
   1.159 +
   1.160 +
   1.161 +LUALIB_API const char *luaL_checklstring (lua_State *L, int narg, size_t *len) {
   1.162 +  const char *s = lua_tolstring(L, narg, len);
   1.163 +  if (!s) tag_error(L, narg, LUA_TSTRING);
   1.164 +  return s;
   1.165 +}
   1.166 +
   1.167 +
   1.168 +LUALIB_API const char *luaL_optlstring (lua_State *L, int narg,
   1.169 +                                        const char *def, size_t *len) {
   1.170 +  if (lua_isnoneornil(L, narg)) {
   1.171 +    if (len)
   1.172 +      *len = (def ? strlen(def) : 0);
   1.173 +    return def;
   1.174 +  }
   1.175 +  else return luaL_checklstring(L, narg, len);
   1.176 +}
   1.177 +
   1.178 +
   1.179 +LUALIB_API lua_Number luaL_checknumber (lua_State *L, int narg) {
   1.180 +  lua_Number d = lua_tonumber(L, narg);
   1.181 +  if (d == 0 && !lua_isnumber(L, narg))  /* avoid extra test when d is not 0 */
   1.182 +    tag_error(L, narg, LUA_TNUMBER);
   1.183 +  return d;
   1.184 +}
   1.185 +
   1.186 +
   1.187 +LUALIB_API lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number def) {
   1.188 +  return luaL_opt(L, luaL_checknumber, narg, def);
   1.189 +}
   1.190 +
   1.191 +
   1.192 +LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) {
   1.193 +  lua_Integer d = lua_tointeger(L, narg);
   1.194 +  if (d == 0 && !lua_isnumber(L, narg))  /* avoid extra test when d is not 0 */
   1.195 +    tag_error(L, narg, LUA_TNUMBER);
   1.196 +  return d;
   1.197 +}
   1.198 +
   1.199 +
   1.200 +LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int narg,
   1.201 +                                                      lua_Integer def) {
   1.202 +  return luaL_opt(L, luaL_checkinteger, narg, def);
   1.203 +}
   1.204 +
   1.205 +
   1.206 +LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) {
   1.207 +  if (!lua_getmetatable(L, obj))  /* no metatable? */
   1.208 +    return 0;
   1.209 +  lua_pushstring(L, event);
   1.210 +  lua_rawget(L, -2);
   1.211 +  if (lua_isnil(L, -1)) {
   1.212 +    lua_pop(L, 2);  /* remove metatable and metafield */
   1.213 +    return 0;
   1.214 +  }
   1.215 +  else {
   1.216 +    lua_remove(L, -2);  /* remove only metatable */
   1.217 +    return 1;
   1.218 +  }
   1.219 +}
   1.220 +
   1.221 +
   1.222 +LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {
   1.223 +  obj = abs_index(L, obj);
   1.224 +  if (!luaL_getmetafield(L, obj, event))  /* no metafield? */
   1.225 +    return 0;
   1.226 +  lua_pushvalue(L, obj);
   1.227 +  lua_call(L, 1, 1);
   1.228 +  return 1;
   1.229 +}
   1.230 +
   1.231 +
   1.232 +LUALIB_API void (luaL_register) (lua_State *L, const char *libname,
   1.233 +                                const luaL_Reg *l) {
   1.234 +  luaI_openlib(L, libname, l, 0);
   1.235 +}
   1.236 +
   1.237 +
   1.238 +static int libsize (const luaL_Reg *l) {
   1.239 +  int size = 0;
   1.240 +  for (; l->name; l++) size++;
   1.241 +  return size;
   1.242 +}
   1.243 +
   1.244 +
   1.245 +LUALIB_API void luaI_openlib (lua_State *L, const char *libname,
   1.246 +                              const luaL_Reg *l, int nup) {
   1.247 +  if (libname) {
   1.248 +    int size = libsize(l);
   1.249 +    /* check whether lib already exists */
   1.250 +    luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1);
   1.251 +    lua_getfield(L, -1, libname);  /* get _LOADED[libname] */
   1.252 +    if (!lua_istable(L, -1)) {  /* not found? */
   1.253 +      lua_pop(L, 1);  /* remove previous result */
   1.254 +      /* try global variable (and create one if it does not exist) */
   1.255 +      if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, size) != NULL)
   1.256 +        luaL_error(L, "name conflict for module " LUA_QS, libname);
   1.257 +      lua_pushvalue(L, -1);
   1.258 +      lua_setfield(L, -3, libname);  /* _LOADED[libname] = new table */
   1.259 +    }
   1.260 +    lua_remove(L, -2);  /* remove _LOADED table */
   1.261 +    lua_insert(L, -(nup+1));  /* move library table to below upvalues */
   1.262 +  }
   1.263 +  for (; l->name; l++) {
   1.264 +    int i;
   1.265 +    for (i=0; i<nup; i++)  /* copy upvalues to the top */
   1.266 +      lua_pushvalue(L, -nup);
   1.267 +    lua_pushcclosure(L, l->func, nup);
   1.268 +    lua_setfield(L, -(nup+2), l->name);
   1.269 +  }
   1.270 +  lua_pop(L, nup);  /* remove upvalues */
   1.271 +}
   1.272 +
   1.273 +
   1.274 +
   1.275 +/*
   1.276 +** {======================================================
   1.277 +** getn-setn: size for arrays
   1.278 +** =======================================================
   1.279 +*/
   1.280 +
   1.281 +#if defined(LUA_COMPAT_GETN)
   1.282 +
   1.283 +static int checkint (lua_State *L, int topop) {
   1.284 +  int n = (lua_type(L, -1) == LUA_TNUMBER) ? lua_tointeger(L, -1) : -1;
   1.285 +  lua_pop(L, topop);
   1.286 +  return n;
   1.287 +}
   1.288 +
   1.289 +
   1.290 +static void getsizes (lua_State *L) {
   1.291 +  lua_getfield(L, LUA_REGISTRYINDEX, "LUA_SIZES");
   1.292 +  if (lua_isnil(L, -1)) {  /* no `size' table? */
   1.293 +    lua_pop(L, 1);  /* remove nil */
   1.294 +    lua_newtable(L);  /* create it */
   1.295 +    lua_pushvalue(L, -1);  /* `size' will be its own metatable */
   1.296 +    lua_setmetatable(L, -2);
   1.297 +    lua_pushliteral(L, "kv");
   1.298 +    lua_setfield(L, -2, "__mode");  /* metatable(N).__mode = "kv" */
   1.299 +    lua_pushvalue(L, -1);
   1.300 +    lua_setfield(L, LUA_REGISTRYINDEX, "LUA_SIZES");  /* store in register */
   1.301 +  }
   1.302 +}
   1.303 +
   1.304 +
   1.305 +LUALIB_API void luaL_setn (lua_State *L, int t, int n) {
   1.306 +  t = abs_index(L, t);
   1.307 +  lua_pushliteral(L, "n");
   1.308 +  lua_rawget(L, t);
   1.309 +  if (checkint(L, 1) >= 0) {  /* is there a numeric field `n'? */
   1.310 +    lua_pushliteral(L, "n");  /* use it */
   1.311 +    lua_pushinteger(L, n);
   1.312 +    lua_rawset(L, t);
   1.313 +  }
   1.314 +  else {  /* use `sizes' */
   1.315 +    getsizes(L);
   1.316 +    lua_pushvalue(L, t);
   1.317 +    lua_pushinteger(L, n);
   1.318 +    lua_rawset(L, -3);  /* sizes[t] = n */
   1.319 +    lua_pop(L, 1);  /* remove `sizes' */
   1.320 +  }
   1.321 +}
   1.322 +
   1.323 +
   1.324 +LUALIB_API int luaL_getn (lua_State *L, int t) {
   1.325 +  int n;
   1.326 +  t = abs_index(L, t);
   1.327 +  lua_pushliteral(L, "n");  /* try t.n */
   1.328 +  lua_rawget(L, t);
   1.329 +  if ((n = checkint(L, 1)) >= 0) return n;
   1.330 +  getsizes(L);  /* else try sizes[t] */
   1.331 +  lua_pushvalue(L, t);
   1.332 +  lua_rawget(L, -2);
   1.333 +  if ((n = checkint(L, 2)) >= 0) return n;
   1.334 +  return (int)lua_objlen(L, t);
   1.335 +}
   1.336 +
   1.337 +#endif
   1.338 +
   1.339 +/* }====================================================== */
   1.340 +
   1.341 +
   1.342 +
   1.343 +LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p,
   1.344 +                                                               const char *r) {
   1.345 +  const char *wild;
   1.346 +  size_t l = strlen(p);
   1.347 +  luaL_Buffer b;
   1.348 +  luaL_buffinit(L, &b);
   1.349 +  while ((wild = strstr(s, p)) != NULL) {
   1.350 +    luaL_addlstring(&b, s, wild - s);  /* push prefix */
   1.351 +    luaL_addstring(&b, r);  /* push replacement in place of pattern */
   1.352 +    s = wild + l;  /* continue after `p' */
   1.353 +  }
   1.354 +  luaL_addstring(&b, s);  /* push last suffix */
   1.355 +  luaL_pushresult(&b);
   1.356 +  return lua_tostring(L, -1);
   1.357 +}
   1.358 +
   1.359 +
   1.360 +LUALIB_API const char *luaL_findtable (lua_State *L, int idx,
   1.361 +                                       const char *fname, int szhint) {
   1.362 +  const char *e;
   1.363 +  lua_pushvalue(L, idx);
   1.364 +  do {
   1.365 +    e = strchr(fname, '.');
   1.366 +    if (e == NULL) e = fname + strlen(fname);
   1.367 +    lua_pushlstring(L, fname, e - fname);
   1.368 +    lua_rawget(L, -2);
   1.369 +    if (lua_isnil(L, -1)) {  /* no such field? */
   1.370 +      lua_pop(L, 1);  /* remove this nil */
   1.371 +      lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */
   1.372 +      lua_pushlstring(L, fname, e - fname);
   1.373 +      lua_pushvalue(L, -2);
   1.374 +      lua_settable(L, -4);  /* set new table into field */
   1.375 +    }
   1.376 +    else if (!lua_istable(L, -1)) {  /* field has a non-table value? */
   1.377 +      lua_pop(L, 2);  /* remove table and value */
   1.378 +      return fname;  /* return problematic part of the name */
   1.379 +    }
   1.380 +    lua_remove(L, -2);  /* remove previous table */
   1.381 +    fname = e + 1;
   1.382 +  } while (*e == '.');
   1.383 +  return NULL;
   1.384 +}
   1.385 +
   1.386 +
   1.387 +
   1.388 +/*
   1.389 +** {======================================================
   1.390 +** Generic Buffer manipulation
   1.391 +** =======================================================
   1.392 +*/
   1.393 +
   1.394 +
   1.395 +#define bufflen(B)	((B)->p - (B)->buffer)
   1.396 +#define bufffree(B)	((size_t)(LUAL_BUFFERSIZE - bufflen(B)))
   1.397 +
   1.398 +#define LIMIT	(LUA_MINSTACK/2)
   1.399 +
   1.400 +
   1.401 +static int emptybuffer (luaL_Buffer *B) {
   1.402 +  size_t l = bufflen(B);
   1.403 +  if (l == 0) return 0;  /* put nothing on stack */
   1.404 +  else {
   1.405 +    lua_pushlstring(B->L, B->buffer, l);
   1.406 +    B->p = B->buffer;
   1.407 +    B->lvl++;
   1.408 +    return 1;
   1.409 +  }
   1.410 +}
   1.411 +
   1.412 +
   1.413 +static void adjuststack (luaL_Buffer *B) {
   1.414 +  if (B->lvl > 1) {
   1.415 +    lua_State *L = B->L;
   1.416 +    int toget = 1;  /* number of levels to concat */
   1.417 +    size_t toplen = lua_strlen(L, -1);
   1.418 +    do {
   1.419 +      size_t l = lua_strlen(L, -(toget+1));
   1.420 +      if (B->lvl - toget + 1 >= LIMIT || toplen > l) {
   1.421 +        toplen += l;
   1.422 +        toget++;
   1.423 +      }
   1.424 +      else break;
   1.425 +    } while (toget < B->lvl);
   1.426 +    lua_concat(L, toget);
   1.427 +    B->lvl = B->lvl - toget + 1;
   1.428 +  }
   1.429 +}
   1.430 +
   1.431 +
   1.432 +LUALIB_API char *luaL_prepbuffer (luaL_Buffer *B) {
   1.433 +  if (emptybuffer(B))
   1.434 +    adjuststack(B);
   1.435 +  return B->buffer;
   1.436 +}
   1.437 +
   1.438 +
   1.439 +LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {
   1.440 +  while (l--)
   1.441 +    luaL_addchar(B, *s++);
   1.442 +}
   1.443 +
   1.444 +
   1.445 +LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) {
   1.446 +  luaL_addlstring(B, s, strlen(s));
   1.447 +}
   1.448 +
   1.449 +
   1.450 +LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
   1.451 +  emptybuffer(B);
   1.452 +  lua_concat(B->L, B->lvl);
   1.453 +  B->lvl = 1;
   1.454 +}
   1.455 +
   1.456 +
   1.457 +LUALIB_API void luaL_addvalue (luaL_Buffer *B) {
   1.458 +  lua_State *L = B->L;
   1.459 +  size_t vl;
   1.460 +  const char *s = lua_tolstring(L, -1, &vl);
   1.461 +  if (vl <= bufffree(B)) {  /* fit into buffer? */
   1.462 +    memcpy(B->p, s, vl);  /* put it there */
   1.463 +    B->p += vl;
   1.464 +    lua_pop(L, 1);  /* remove from stack */
   1.465 +  }
   1.466 +  else {
   1.467 +    if (emptybuffer(B))
   1.468 +      lua_insert(L, -2);  /* put buffer before new value */
   1.469 +    B->lvl++;  /* add new value into B stack */
   1.470 +    adjuststack(B);
   1.471 +  }
   1.472 +}
   1.473 +
   1.474 +
   1.475 +LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
   1.476 +  B->L = L;
   1.477 +  B->p = B->buffer;
   1.478 +  B->lvl = 0;
   1.479 +}
   1.480 +
   1.481 +/* }====================================================== */
   1.482 +
   1.483 +
   1.484 +LUALIB_API int luaL_ref (lua_State *L, int t) {
   1.485 +  int ref;
   1.486 +  t = abs_index(L, t);
   1.487 +  if (lua_isnil(L, -1)) {
   1.488 +    lua_pop(L, 1);  /* remove from stack */
   1.489 +    return LUA_REFNIL;  /* `nil' has a unique fixed reference */
   1.490 +  }
   1.491 +  lua_rawgeti(L, t, FREELIST_REF);  /* get first free element */
   1.492 +  ref = (int)lua_tointeger(L, -1);  /* ref = t[FREELIST_REF] */
   1.493 +  lua_pop(L, 1);  /* remove it from stack */
   1.494 +  if (ref != 0) {  /* any free element? */
   1.495 +    lua_rawgeti(L, t, ref);  /* remove it from list */
   1.496 +    lua_rawseti(L, t, FREELIST_REF);  /* (t[FREELIST_REF] = t[ref]) */
   1.497 +  }
   1.498 +  else {  /* no free elements */
   1.499 +    ref = (int)lua_objlen(L, t);
   1.500 +    ref++;  /* create new reference */
   1.501 +  }
   1.502 +  lua_rawseti(L, t, ref);
   1.503 +  return ref;
   1.504 +}
   1.505 +
   1.506 +
   1.507 +LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
   1.508 +  if (ref >= 0) {
   1.509 +    t = abs_index(L, t);
   1.510 +    lua_rawgeti(L, t, FREELIST_REF);
   1.511 +    lua_rawseti(L, t, ref);  /* t[ref] = t[FREELIST_REF] */
   1.512 +    lua_pushinteger(L, ref);
   1.513 +    lua_rawseti(L, t, FREELIST_REF);  /* t[FREELIST_REF] = ref */
   1.514 +  }
   1.515 +}
   1.516 +
   1.517 +
   1.518 +
   1.519 +/*
   1.520 +** {======================================================
   1.521 +** Load functions
   1.522 +** =======================================================
   1.523 +*/
   1.524 +
   1.525 +typedef struct LoadF {
   1.526 +  int extraline;
   1.527 +  FILE *f;
   1.528 +  char buff[LUAL_BUFFERSIZE];
   1.529 +} LoadF;
   1.530 +
   1.531 +
   1.532 +static const char *getF (lua_State *L, void *ud, size_t *size) {
   1.533 +  LoadF *lf = (LoadF *)ud;
   1.534 +  (void)L;
   1.535 +  if (lf->extraline) {
   1.536 +    lf->extraline = 0;
   1.537 +    *size = 1;
   1.538 +    return "\n";
   1.539 +  }
   1.540 +  if (feof(lf->f)) return NULL;
   1.541 +  *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f);
   1.542 +  return (*size > 0) ? lf->buff : NULL;
   1.543 +}
   1.544 +
   1.545 +
   1.546 +static int errfile (lua_State *L, const char *what, int fnameindex) {
   1.547 +  const char *serr = strerror(errno);
   1.548 +  const char *filename = lua_tostring(L, fnameindex) + 1;
   1.549 +  lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr);
   1.550 +  lua_remove(L, fnameindex);
   1.551 +  return LUA_ERRFILE;
   1.552 +}
   1.553 +
   1.554 +
   1.555 +LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
   1.556 +  LoadF lf;
   1.557 +  int status, readstatus;
   1.558 +  int c;
   1.559 +  int fnameindex = lua_gettop(L) + 1;  /* index of filename on the stack */
   1.560 +  lf.extraline = 0;
   1.561 +  if (filename == NULL) {
   1.562 +    lua_pushliteral(L, "=stdin");
   1.563 +    lf.f = stdin;
   1.564 +  }
   1.565 +  else {
   1.566 +    lua_pushfstring(L, "@%s", filename);
   1.567 +    lf.f = fopen(filename, "r");
   1.568 +    if (lf.f == NULL) return errfile(L, "open", fnameindex);
   1.569 +  }
   1.570 +  c = getc(lf.f);
   1.571 +  if (c == '#') {  /* Unix exec. file? */
   1.572 +    lf.extraline = 1;
   1.573 +    while ((c = getc(lf.f)) != EOF && c != '\n') ;  /* skip first line */
   1.574 +    if (c == '\n') c = getc(lf.f);
   1.575 +  }
   1.576 +  if (c == LUA_SIGNATURE[0] && filename) {  /* binary file? */
   1.577 +    lf.f = freopen(filename, "rb", lf.f);  /* reopen in binary mode */
   1.578 +    if (lf.f == NULL) return errfile(L, "reopen", fnameindex);
   1.579 +    /* skip eventual `#!...' */
   1.580 +   while ((c = getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) ;
   1.581 +    lf.extraline = 0;
   1.582 +  }
   1.583 +  ungetc(c, lf.f);
   1.584 +  status = lua_load(L, getF, &lf, lua_tostring(L, -1));
   1.585 +  readstatus = ferror(lf.f);
   1.586 +  if (filename) fclose(lf.f);  /* close file (even in case of errors) */
   1.587 +  if (readstatus) {
   1.588 +    lua_settop(L, fnameindex);  /* ignore results from `lua_load' */
   1.589 +    return errfile(L, "read", fnameindex);
   1.590 +  }
   1.591 +  lua_remove(L, fnameindex);
   1.592 +  return status;
   1.593 +}
   1.594 +
   1.595 +
   1.596 +typedef struct LoadS {
   1.597 +  const char *s;
   1.598 +  size_t size;
   1.599 +} LoadS;
   1.600 +
   1.601 +
   1.602 +static const char *getS (lua_State *L, void *ud, size_t *size) {
   1.603 +  LoadS *ls = (LoadS *)ud;
   1.604 +  (void)L;
   1.605 +  if (ls->size == 0) return NULL;
   1.606 +  *size = ls->size;
   1.607 +  ls->size = 0;
   1.608 +  return ls->s;
   1.609 +}
   1.610 +
   1.611 +
   1.612 +LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t size,
   1.613 +                                const char *name) {
   1.614 +  LoadS ls;
   1.615 +  ls.s = buff;
   1.616 +  ls.size = size;
   1.617 +  return lua_load(L, getS, &ls, name);
   1.618 +}
   1.619 +
   1.620 +
   1.621 +LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s) {
   1.622 +  return luaL_loadbuffer(L, s, strlen(s), s);
   1.623 +}
   1.624 +
   1.625 +
   1.626 +
   1.627 +/* }====================================================== */
   1.628 +
   1.629 +
   1.630 +static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
   1.631 +  (void)ud;
   1.632 +  (void)osize;
   1.633 +  if (nsize == 0) {
   1.634 +    free(ptr);
   1.635 +    return NULL;
   1.636 +  }
   1.637 +  else
   1.638 +    return realloc(ptr, nsize);
   1.639 +}
   1.640 +
   1.641 +
   1.642 +static int panic (lua_State *L) {
   1.643 +  (void)L;  /* to avoid warnings */
   1.644 +  fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n",
   1.645 +                   lua_tostring(L, -1));
   1.646 +  return 0;
   1.647 +}
   1.648 +
   1.649 +
   1.650 +LUALIB_API lua_State *luaL_newstate (void) {
   1.651 +  lua_State *L = lua_newstate(l_alloc, NULL);
   1.652 +  if (L) lua_atpanic(L, &panic);
   1.653 +  return L;
   1.654 +}
   1.655 +