diff src/lua/liolib.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/liolib.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/liolib.c	Sat Mar 03 11:07:39 2012 -0600
     1.3 @@ -0,0 +1,553 @@
     1.4 +/*
     1.5 +** $Id: liolib.c,v 2.73.1.3 2008/01/18 17:47:43 roberto Exp $
     1.6 +** Standard I/O (and system) library
     1.7 +** See Copyright Notice in lua.h
     1.8 +*/
     1.9 +
    1.10 +
    1.11 +#include <errno.h>
    1.12 +#include <stdio.h>
    1.13 +#include <stdlib.h>
    1.14 +#include <string.h>
    1.15 +
    1.16 +#define liolib_c
    1.17 +#define LUA_LIB
    1.18 +
    1.19 +#include "lua.h"
    1.20 +
    1.21 +#include "lauxlib.h"
    1.22 +#include "lualib.h"
    1.23 +
    1.24 +
    1.25 +
    1.26 +#define IO_INPUT	1
    1.27 +#define IO_OUTPUT	2
    1.28 +
    1.29 +
    1.30 +static const char *const fnames[] = {"input", "output"};
    1.31 +
    1.32 +
    1.33 +static int pushresult (lua_State *L, int i, const char *filename) {
    1.34 +  int en = errno;  /* calls to Lua API may change this value */
    1.35 +  if (i) {
    1.36 +    lua_pushboolean(L, 1);
    1.37 +    return 1;
    1.38 +  }
    1.39 +  else {
    1.40 +    lua_pushnil(L);
    1.41 +    if (filename)
    1.42 +      lua_pushfstring(L, "%s: %s", filename, strerror(en));
    1.43 +    else
    1.44 +      lua_pushfstring(L, "%s", strerror(en));
    1.45 +    lua_pushinteger(L, en);
    1.46 +    return 3;
    1.47 +  }
    1.48 +}
    1.49 +
    1.50 +
    1.51 +static void fileerror (lua_State *L, int arg, const char *filename) {
    1.52 +  lua_pushfstring(L, "%s: %s", filename, strerror(errno));
    1.53 +  luaL_argerror(L, arg, lua_tostring(L, -1));
    1.54 +}
    1.55 +
    1.56 +
    1.57 +#define tofilep(L)	((FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE))
    1.58 +
    1.59 +
    1.60 +static int io_type (lua_State *L) {
    1.61 +  void *ud;
    1.62 +  luaL_checkany(L, 1);
    1.63 +  ud = lua_touserdata(L, 1);
    1.64 +  lua_getfield(L, LUA_REGISTRYINDEX, LUA_FILEHANDLE);
    1.65 +  if (ud == NULL || !lua_getmetatable(L, 1) || !lua_rawequal(L, -2, -1))
    1.66 +    lua_pushnil(L);  /* not a file */
    1.67 +  else if (*((FILE **)ud) == NULL)
    1.68 +    lua_pushliteral(L, "closed file");
    1.69 +  else
    1.70 +    lua_pushliteral(L, "file");
    1.71 +  return 1;
    1.72 +}
    1.73 +
    1.74 +
    1.75 +static FILE *tofile (lua_State *L) {
    1.76 +  FILE **f = tofilep(L);
    1.77 +  if (*f == NULL)
    1.78 +    luaL_error(L, "attempt to use a closed file");
    1.79 +  return *f;
    1.80 +}
    1.81 +
    1.82 +
    1.83 +
    1.84 +/*
    1.85 +** When creating file handles, always creates a `closed' file handle
    1.86 +** before opening the actual file; so, if there is a memory error, the
    1.87 +** file is not left opened.
    1.88 +*/
    1.89 +static FILE **newfile (lua_State *L) {
    1.90 +  FILE **pf = (FILE **)lua_newuserdata(L, sizeof(FILE *));
    1.91 +  *pf = NULL;  /* file handle is currently `closed' */
    1.92 +  luaL_getmetatable(L, LUA_FILEHANDLE);
    1.93 +  lua_setmetatable(L, -2);
    1.94 +  return pf;
    1.95 +}
    1.96 +
    1.97 +
    1.98 +/*
    1.99 +** function to (not) close the standard files stdin, stdout, and stderr
   1.100 +*/
   1.101 +static int io_noclose (lua_State *L) {
   1.102 +  lua_pushnil(L);
   1.103 +  lua_pushliteral(L, "cannot close standard file");
   1.104 +  return 2;
   1.105 +}
   1.106 +
   1.107 +
   1.108 +/*
   1.109 +** function to close 'popen' files
   1.110 +*/
   1.111 +static int io_pclose (lua_State *L) {
   1.112 +  FILE **p = tofilep(L);
   1.113 +  int ok = lua_pclose(L, *p);
   1.114 +  *p = NULL;
   1.115 +  return pushresult(L, ok, NULL);
   1.116 +}
   1.117 +
   1.118 +
   1.119 +/*
   1.120 +** function to close regular files
   1.121 +*/
   1.122 +static int io_fclose (lua_State *L) {
   1.123 +  FILE **p = tofilep(L);
   1.124 +  int ok = (fclose(*p) == 0);
   1.125 +  *p = NULL;
   1.126 +  return pushresult(L, ok, NULL);
   1.127 +}
   1.128 +
   1.129 +
   1.130 +static int aux_close (lua_State *L) {
   1.131 +  lua_getfenv(L, 1);
   1.132 +  lua_getfield(L, -1, "__close");
   1.133 +  return (lua_tocfunction(L, -1))(L);
   1.134 +}
   1.135 +
   1.136 +
   1.137 +static int io_close (lua_State *L) {
   1.138 +  if (lua_isnone(L, 1))
   1.139 +    lua_rawgeti(L, LUA_ENVIRONINDEX, IO_OUTPUT);
   1.140 +  tofile(L);  /* make sure argument is a file */
   1.141 +  return aux_close(L);
   1.142 +}
   1.143 +
   1.144 +
   1.145 +static int io_gc (lua_State *L) {
   1.146 +  FILE *f = *tofilep(L);
   1.147 +  /* ignore closed files */
   1.148 +  if (f != NULL)
   1.149 +    aux_close(L);
   1.150 +  return 0;
   1.151 +}
   1.152 +
   1.153 +
   1.154 +static int io_tostring (lua_State *L) {
   1.155 +  FILE *f = *tofilep(L);
   1.156 +  if (f == NULL)
   1.157 +    lua_pushliteral(L, "file (closed)");
   1.158 +  else
   1.159 +    lua_pushfstring(L, "file (%p)", f);
   1.160 +  return 1;
   1.161 +}
   1.162 +
   1.163 +
   1.164 +static int io_open (lua_State *L) {
   1.165 +  const char *filename = luaL_checkstring(L, 1);
   1.166 +  const char *mode = luaL_optstring(L, 2, "r");
   1.167 +  FILE **pf = newfile(L);
   1.168 +  *pf = fopen(filename, mode);
   1.169 +  return (*pf == NULL) ? pushresult(L, 0, filename) : 1;
   1.170 +}
   1.171 +
   1.172 +
   1.173 +/*
   1.174 +** this function has a separated environment, which defines the
   1.175 +** correct __close for 'popen' files
   1.176 +*/
   1.177 +static int io_popen (lua_State *L) {
   1.178 +  const char *filename = luaL_checkstring(L, 1);
   1.179 +  const char *mode = luaL_optstring(L, 2, "r");
   1.180 +  FILE **pf = newfile(L);
   1.181 +  *pf = lua_popen(L, filename, mode);
   1.182 +  return (*pf == NULL) ? pushresult(L, 0, filename) : 1;
   1.183 +}
   1.184 +
   1.185 +
   1.186 +static int io_tmpfile (lua_State *L) {
   1.187 +  FILE **pf = newfile(L);
   1.188 +  *pf = tmpfile();
   1.189 +  return (*pf == NULL) ? pushresult(L, 0, NULL) : 1;
   1.190 +}
   1.191 +
   1.192 +
   1.193 +static FILE *getiofile (lua_State *L, int findex) {
   1.194 +  FILE *f;
   1.195 +  lua_rawgeti(L, LUA_ENVIRONINDEX, findex);
   1.196 +  f = *(FILE **)lua_touserdata(L, -1);
   1.197 +  if (f == NULL)
   1.198 +    luaL_error(L, "standard %s file is closed", fnames[findex - 1]);
   1.199 +  return f;
   1.200 +}
   1.201 +
   1.202 +
   1.203 +static int g_iofile (lua_State *L, int f, const char *mode) {
   1.204 +  if (!lua_isnoneornil(L, 1)) {
   1.205 +    const char *filename = lua_tostring(L, 1);
   1.206 +    if (filename) {
   1.207 +      FILE **pf = newfile(L);
   1.208 +      *pf = fopen(filename, mode);
   1.209 +      if (*pf == NULL)
   1.210 +        fileerror(L, 1, filename);
   1.211 +    }
   1.212 +    else {
   1.213 +      tofile(L);  /* check that it's a valid file handle */
   1.214 +      lua_pushvalue(L, 1);
   1.215 +    }
   1.216 +    lua_rawseti(L, LUA_ENVIRONINDEX, f);
   1.217 +  }
   1.218 +  /* return current value */
   1.219 +  lua_rawgeti(L, LUA_ENVIRONINDEX, f);
   1.220 +  return 1;
   1.221 +}
   1.222 +
   1.223 +
   1.224 +static int io_input (lua_State *L) {
   1.225 +  return g_iofile(L, IO_INPUT, "r");
   1.226 +}
   1.227 +
   1.228 +
   1.229 +static int io_output (lua_State *L) {
   1.230 +  return g_iofile(L, IO_OUTPUT, "w");
   1.231 +}
   1.232 +
   1.233 +
   1.234 +static int io_readline (lua_State *L);
   1.235 +
   1.236 +
   1.237 +static void aux_lines (lua_State *L, int idx, int toclose) {
   1.238 +  lua_pushvalue(L, idx);
   1.239 +  lua_pushboolean(L, toclose);  /* close/not close file when finished */
   1.240 +  lua_pushcclosure(L, io_readline, 2);
   1.241 +}
   1.242 +
   1.243 +
   1.244 +static int f_lines (lua_State *L) {
   1.245 +  tofile(L);  /* check that it's a valid file handle */
   1.246 +  aux_lines(L, 1, 0);
   1.247 +  return 1;
   1.248 +}
   1.249 +
   1.250 +
   1.251 +static int io_lines (lua_State *L) {
   1.252 +  if (lua_isnoneornil(L, 1)) {  /* no arguments? */
   1.253 +    /* will iterate over default input */
   1.254 +    lua_rawgeti(L, LUA_ENVIRONINDEX, IO_INPUT);
   1.255 +    return f_lines(L);
   1.256 +  }
   1.257 +  else {
   1.258 +    const char *filename = luaL_checkstring(L, 1);
   1.259 +    FILE **pf = newfile(L);
   1.260 +    *pf = fopen(filename, "r");
   1.261 +    if (*pf == NULL)
   1.262 +      fileerror(L, 1, filename);
   1.263 +    aux_lines(L, lua_gettop(L), 1);
   1.264 +    return 1;
   1.265 +  }
   1.266 +}
   1.267 +
   1.268 +
   1.269 +/*
   1.270 +** {======================================================
   1.271 +** READ
   1.272 +** =======================================================
   1.273 +*/
   1.274 +
   1.275 +
   1.276 +static int read_number (lua_State *L, FILE *f) {
   1.277 +  lua_Number d;
   1.278 +  if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1) {
   1.279 +    lua_pushnumber(L, d);
   1.280 +    return 1;
   1.281 +  }
   1.282 +  else return 0;  /* read fails */
   1.283 +}
   1.284 +
   1.285 +
   1.286 +static int test_eof (lua_State *L, FILE *f) {
   1.287 +  int c = getc(f);
   1.288 +  ungetc(c, f);
   1.289 +  lua_pushlstring(L, NULL, 0);
   1.290 +  return (c != EOF);
   1.291 +}
   1.292 +
   1.293 +
   1.294 +static int read_line (lua_State *L, FILE *f) {
   1.295 +  luaL_Buffer b;
   1.296 +  luaL_buffinit(L, &b);
   1.297 +  for (;;) {
   1.298 +    size_t l;
   1.299 +    char *p = luaL_prepbuffer(&b);
   1.300 +    if (fgets(p, LUAL_BUFFERSIZE, f) == NULL) {  /* eof? */
   1.301 +      luaL_pushresult(&b);  /* close buffer */
   1.302 +      return (lua_objlen(L, -1) > 0);  /* check whether read something */
   1.303 +    }
   1.304 +    l = strlen(p);
   1.305 +    if (l == 0 || p[l-1] != '\n')
   1.306 +      luaL_addsize(&b, l);
   1.307 +    else {
   1.308 +      luaL_addsize(&b, l - 1);  /* do not include `eol' */
   1.309 +      luaL_pushresult(&b);  /* close buffer */
   1.310 +      return 1;  /* read at least an `eol' */
   1.311 +    }
   1.312 +  }
   1.313 +}
   1.314 +
   1.315 +
   1.316 +static int read_chars (lua_State *L, FILE *f, size_t n) {
   1.317 +  size_t rlen;  /* how much to read */
   1.318 +  size_t nr;  /* number of chars actually read */
   1.319 +  luaL_Buffer b;
   1.320 +  luaL_buffinit(L, &b);
   1.321 +  rlen = LUAL_BUFFERSIZE;  /* try to read that much each time */
   1.322 +  do {
   1.323 +    char *p = luaL_prepbuffer(&b);
   1.324 +    if (rlen > n) rlen = n;  /* cannot read more than asked */
   1.325 +    nr = fread(p, sizeof(char), rlen, f);
   1.326 +    luaL_addsize(&b, nr);
   1.327 +    n -= nr;  /* still have to read `n' chars */
   1.328 +  } while (n > 0 && nr == rlen);  /* until end of count or eof */
   1.329 +  luaL_pushresult(&b);  /* close buffer */
   1.330 +  return (n == 0 || lua_objlen(L, -1) > 0);
   1.331 +}
   1.332 +
   1.333 +
   1.334 +static int g_read (lua_State *L, FILE *f, int first) {
   1.335 +  int nargs = lua_gettop(L) - 1;
   1.336 +  int success;
   1.337 +  int n;
   1.338 +  clearerr(f);
   1.339 +  if (nargs == 0) {  /* no arguments? */
   1.340 +    success = read_line(L, f);
   1.341 +    n = first+1;  /* to return 1 result */
   1.342 +  }
   1.343 +  else {  /* ensure stack space for all results and for auxlib's buffer */
   1.344 +    luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments");
   1.345 +    success = 1;
   1.346 +    for (n = first; nargs-- && success; n++) {
   1.347 +      if (lua_type(L, n) == LUA_TNUMBER) {
   1.348 +        size_t l = (size_t)lua_tointeger(L, n);
   1.349 +        success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
   1.350 +      }
   1.351 +      else {
   1.352 +        const char *p = lua_tostring(L, n);
   1.353 +        luaL_argcheck(L, p && p[0] == '*', n, "invalid option");
   1.354 +        switch (p[1]) {
   1.355 +          case 'n':  /* number */
   1.356 +            success = read_number(L, f);
   1.357 +            break;
   1.358 +          case 'l':  /* line */
   1.359 +            success = read_line(L, f);
   1.360 +            break;
   1.361 +          case 'a':  /* file */
   1.362 +            read_chars(L, f, ~((size_t)0));  /* read MAX_SIZE_T chars */
   1.363 +            success = 1; /* always success */
   1.364 +            break;
   1.365 +          default:
   1.366 +            return luaL_argerror(L, n, "invalid format");
   1.367 +        }
   1.368 +      }
   1.369 +    }
   1.370 +  }
   1.371 +  if (ferror(f))
   1.372 +    return pushresult(L, 0, NULL);
   1.373 +  if (!success) {
   1.374 +    lua_pop(L, 1);  /* remove last result */
   1.375 +    lua_pushnil(L);  /* push nil instead */
   1.376 +  }
   1.377 +  return n - first;
   1.378 +}
   1.379 +
   1.380 +
   1.381 +static int io_read (lua_State *L) {
   1.382 +  return g_read(L, getiofile(L, IO_INPUT), 1);
   1.383 +}
   1.384 +
   1.385 +
   1.386 +static int f_read (lua_State *L) {
   1.387 +  return g_read(L, tofile(L), 2);
   1.388 +}
   1.389 +
   1.390 +
   1.391 +static int io_readline (lua_State *L) {
   1.392 +  FILE *f = *(FILE **)lua_touserdata(L, lua_upvalueindex(1));
   1.393 +  int sucess;
   1.394 +  if (f == NULL)  /* file is already closed? */
   1.395 +    luaL_error(L, "file is already closed");
   1.396 +  sucess = read_line(L, f);
   1.397 +  if (ferror(f))
   1.398 +    return luaL_error(L, "%s", strerror(errno));
   1.399 +  if (sucess) return 1;
   1.400 +  else {  /* EOF */
   1.401 +    if (lua_toboolean(L, lua_upvalueindex(2))) {  /* generator created file? */
   1.402 +      lua_settop(L, 0);
   1.403 +      lua_pushvalue(L, lua_upvalueindex(1));
   1.404 +      aux_close(L);  /* close it */
   1.405 +    }
   1.406 +    return 0;
   1.407 +  }
   1.408 +}
   1.409 +
   1.410 +/* }====================================================== */
   1.411 +
   1.412 +
   1.413 +static int g_write (lua_State *L, FILE *f, int arg) {
   1.414 +  int nargs = lua_gettop(L) - 1;
   1.415 +  int status = 1;
   1.416 +  for (; nargs--; arg++) {
   1.417 +    if (lua_type(L, arg) == LUA_TNUMBER) {
   1.418 +      /* optimization: could be done exactly as for strings */
   1.419 +      status = status &&
   1.420 +          fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0;
   1.421 +    }
   1.422 +    else {
   1.423 +      size_t l;
   1.424 +      const char *s = luaL_checklstring(L, arg, &l);
   1.425 +      status = status && (fwrite(s, sizeof(char), l, f) == l);
   1.426 +    }
   1.427 +  }
   1.428 +  return pushresult(L, status, NULL);
   1.429 +}
   1.430 +
   1.431 +
   1.432 +static int io_write (lua_State *L) {
   1.433 +  return g_write(L, getiofile(L, IO_OUTPUT), 1);
   1.434 +}
   1.435 +
   1.436 +
   1.437 +static int f_write (lua_State *L) {
   1.438 +  return g_write(L, tofile(L), 2);
   1.439 +}
   1.440 +
   1.441 +
   1.442 +static int f_seek (lua_State *L) {
   1.443 +  static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
   1.444 +  static const char *const modenames[] = {"set", "cur", "end", NULL};
   1.445 +  FILE *f = tofile(L);
   1.446 +  int op = luaL_checkoption(L, 2, "cur", modenames);
   1.447 +  long offset = luaL_optlong(L, 3, 0);
   1.448 +  op = fseek(f, offset, mode[op]);
   1.449 +  if (op)
   1.450 +    return pushresult(L, 0, NULL);  /* error */
   1.451 +  else {
   1.452 +    lua_pushinteger(L, ftell(f));
   1.453 +    return 1;
   1.454 +  }
   1.455 +}
   1.456 +
   1.457 +
   1.458 +static int f_setvbuf (lua_State *L) {
   1.459 +  static const int mode[] = {_IONBF, _IOFBF, _IOLBF};
   1.460 +  static const char *const modenames[] = {"no", "full", "line", NULL};
   1.461 +  FILE *f = tofile(L);
   1.462 +  int op = luaL_checkoption(L, 2, NULL, modenames);
   1.463 +  lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE);
   1.464 +  int res = setvbuf(f, NULL, mode[op], sz);
   1.465 +  return pushresult(L, res == 0, NULL);
   1.466 +}
   1.467 +
   1.468 +
   1.469 +
   1.470 +static int io_flush (lua_State *L) {
   1.471 +  return pushresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL);
   1.472 +}
   1.473 +
   1.474 +
   1.475 +static int f_flush (lua_State *L) {
   1.476 +  return pushresult(L, fflush(tofile(L)) == 0, NULL);
   1.477 +}
   1.478 +
   1.479 +
   1.480 +static const luaL_Reg iolib[] = {
   1.481 +  {"close", io_close},
   1.482 +  {"flush", io_flush},
   1.483 +  {"input", io_input},
   1.484 +  {"lines", io_lines},
   1.485 +  {"open", io_open},
   1.486 +  {"output", io_output},
   1.487 +  {"popen", io_popen},
   1.488 +  {"read", io_read},
   1.489 +  {"tmpfile", io_tmpfile},
   1.490 +  {"type", io_type},
   1.491 +  {"write", io_write},
   1.492 +  {NULL, NULL}
   1.493 +};
   1.494 +
   1.495 +
   1.496 +static const luaL_Reg flib[] = {
   1.497 +  {"close", io_close},
   1.498 +  {"flush", f_flush},
   1.499 +  {"lines", f_lines},
   1.500 +  {"read", f_read},
   1.501 +  {"seek", f_seek},
   1.502 +  {"setvbuf", f_setvbuf},
   1.503 +  {"write", f_write},
   1.504 +  {"__gc", io_gc},
   1.505 +  {"__tostring", io_tostring},
   1.506 +  {NULL, NULL}
   1.507 +};
   1.508 +
   1.509 +
   1.510 +static void createmeta (lua_State *L) {
   1.511 +  luaL_newmetatable(L, LUA_FILEHANDLE);  /* create metatable for file handles */
   1.512 +  lua_pushvalue(L, -1);  /* push metatable */
   1.513 +  lua_setfield(L, -2, "__index");  /* metatable.__index = metatable */
   1.514 +  luaL_register(L, NULL, flib);  /* file methods */
   1.515 +}
   1.516 +
   1.517 +
   1.518 +static void createstdfile (lua_State *L, FILE *f, int k, const char *fname) {
   1.519 +  *newfile(L) = f;
   1.520 +  if (k > 0) {
   1.521 +    lua_pushvalue(L, -1);
   1.522 +    lua_rawseti(L, LUA_ENVIRONINDEX, k);
   1.523 +  }
   1.524 +  lua_pushvalue(L, -2);  /* copy environment */
   1.525 +  lua_setfenv(L, -2);  /* set it */
   1.526 +  lua_setfield(L, -3, fname);
   1.527 +}
   1.528 +
   1.529 +
   1.530 +static void newfenv (lua_State *L, lua_CFunction cls) {
   1.531 +  lua_createtable(L, 0, 1);
   1.532 +  lua_pushcfunction(L, cls);
   1.533 +  lua_setfield(L, -2, "__close");
   1.534 +}
   1.535 +
   1.536 +
   1.537 +LUALIB_API int luaopen_io (lua_State *L) {
   1.538 +  createmeta(L);
   1.539 +  /* create (private) environment (with fields IO_INPUT, IO_OUTPUT, __close) */
   1.540 +  newfenv(L, io_fclose);
   1.541 +  lua_replace(L, LUA_ENVIRONINDEX);
   1.542 +  /* open library */
   1.543 +  luaL_register(L, LUA_IOLIBNAME, iolib);
   1.544 +  /* create (and set) default files */
   1.545 +  newfenv(L, io_noclose);  /* close function for default files */
   1.546 +  createstdfile(L, stdin, IO_INPUT, "stdin");
   1.547 +  createstdfile(L, stdout, IO_OUTPUT, "stdout");
   1.548 +  createstdfile(L, stderr, 0, "stderr");
   1.549 +  lua_pop(L, 1);  /* pop environment for default files */
   1.550 +  lua_getfield(L, -1, "popen");
   1.551 +  newfenv(L, io_pclose);  /* create environment for 'popen' */
   1.552 +  lua_setfenv(L, -2);  /* set fenv for 'popen' */
   1.553 +  lua_pop(L, 1);  /* pop 'popen' */
   1.554 +  return 1;
   1.555 +}
   1.556 +