annotate src/lua/liolib.c @ 135:eb6ba88088d3

Wrote a more efficient input-number-assembly program; 91 oc -> 60 oc.
author Dylan Holmes <ocsenave@gmail.com>
date Sun, 18 Mar 2012 05:13:19 -0500
parents 27763b933818
children
rev   line source
rlm@1 1 /*
rlm@1 2 ** $Id: liolib.c,v 2.73.1.3 2008/01/18 17:47:43 roberto Exp $
rlm@1 3 ** Standard I/O (and system) library
rlm@1 4 ** See Copyright Notice in lua.h
rlm@1 5 */
rlm@1 6
rlm@1 7
rlm@1 8 #include <errno.h>
rlm@1 9 #include <stdio.h>
rlm@1 10 #include <stdlib.h>
rlm@1 11 #include <string.h>
rlm@1 12
rlm@1 13 #define liolib_c
rlm@1 14 #define LUA_LIB
rlm@1 15
rlm@1 16 #include "lua.h"
rlm@1 17
rlm@1 18 #include "lauxlib.h"
rlm@1 19 #include "lualib.h"
rlm@1 20
rlm@1 21
rlm@1 22
rlm@1 23 #define IO_INPUT 1
rlm@1 24 #define IO_OUTPUT 2
rlm@1 25
rlm@1 26
rlm@1 27 static const char *const fnames[] = {"input", "output"};
rlm@1 28
rlm@1 29
rlm@1 30 static int pushresult (lua_State *L, int i, const char *filename) {
rlm@1 31 int en = errno; /* calls to Lua API may change this value */
rlm@1 32 if (i) {
rlm@1 33 lua_pushboolean(L, 1);
rlm@1 34 return 1;
rlm@1 35 }
rlm@1 36 else {
rlm@1 37 lua_pushnil(L);
rlm@1 38 if (filename)
rlm@1 39 lua_pushfstring(L, "%s: %s", filename, strerror(en));
rlm@1 40 else
rlm@1 41 lua_pushfstring(L, "%s", strerror(en));
rlm@1 42 lua_pushinteger(L, en);
rlm@1 43 return 3;
rlm@1 44 }
rlm@1 45 }
rlm@1 46
rlm@1 47
rlm@1 48 static void fileerror (lua_State *L, int arg, const char *filename) {
rlm@1 49 lua_pushfstring(L, "%s: %s", filename, strerror(errno));
rlm@1 50 luaL_argerror(L, arg, lua_tostring(L, -1));
rlm@1 51 }
rlm@1 52
rlm@1 53
rlm@1 54 #define tofilep(L) ((FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE))
rlm@1 55
rlm@1 56
rlm@1 57 static int io_type (lua_State *L) {
rlm@1 58 void *ud;
rlm@1 59 luaL_checkany(L, 1);
rlm@1 60 ud = lua_touserdata(L, 1);
rlm@1 61 lua_getfield(L, LUA_REGISTRYINDEX, LUA_FILEHANDLE);
rlm@1 62 if (ud == NULL || !lua_getmetatable(L, 1) || !lua_rawequal(L, -2, -1))
rlm@1 63 lua_pushnil(L); /* not a file */
rlm@1 64 else if (*((FILE **)ud) == NULL)
rlm@1 65 lua_pushliteral(L, "closed file");
rlm@1 66 else
rlm@1 67 lua_pushliteral(L, "file");
rlm@1 68 return 1;
rlm@1 69 }
rlm@1 70
rlm@1 71
rlm@1 72 static FILE *tofile (lua_State *L) {
rlm@1 73 FILE **f = tofilep(L);
rlm@1 74 if (*f == NULL)
rlm@1 75 luaL_error(L, "attempt to use a closed file");
rlm@1 76 return *f;
rlm@1 77 }
rlm@1 78
rlm@1 79
rlm@1 80
rlm@1 81 /*
rlm@1 82 ** When creating file handles, always creates a `closed' file handle
rlm@1 83 ** before opening the actual file; so, if there is a memory error, the
rlm@1 84 ** file is not left opened.
rlm@1 85 */
rlm@1 86 static FILE **newfile (lua_State *L) {
rlm@1 87 FILE **pf = (FILE **)lua_newuserdata(L, sizeof(FILE *));
rlm@1 88 *pf = NULL; /* file handle is currently `closed' */
rlm@1 89 luaL_getmetatable(L, LUA_FILEHANDLE);
rlm@1 90 lua_setmetatable(L, -2);
rlm@1 91 return pf;
rlm@1 92 }
rlm@1 93
rlm@1 94
rlm@1 95 /*
rlm@1 96 ** function to (not) close the standard files stdin, stdout, and stderr
rlm@1 97 */
rlm@1 98 static int io_noclose (lua_State *L) {
rlm@1 99 lua_pushnil(L);
rlm@1 100 lua_pushliteral(L, "cannot close standard file");
rlm@1 101 return 2;
rlm@1 102 }
rlm@1 103
rlm@1 104
rlm@1 105 /*
rlm@1 106 ** function to close 'popen' files
rlm@1 107 */
rlm@1 108 static int io_pclose (lua_State *L) {
rlm@1 109 FILE **p = tofilep(L);
rlm@1 110 int ok = lua_pclose(L, *p);
rlm@1 111 *p = NULL;
rlm@1 112 return pushresult(L, ok, NULL);
rlm@1 113 }
rlm@1 114
rlm@1 115
rlm@1 116 /*
rlm@1 117 ** function to close regular files
rlm@1 118 */
rlm@1 119 static int io_fclose (lua_State *L) {
rlm@1 120 FILE **p = tofilep(L);
rlm@1 121 int ok = (fclose(*p) == 0);
rlm@1 122 *p = NULL;
rlm@1 123 return pushresult(L, ok, NULL);
rlm@1 124 }
rlm@1 125
rlm@1 126
rlm@1 127 static int aux_close (lua_State *L) {
rlm@1 128 lua_getfenv(L, 1);
rlm@1 129 lua_getfield(L, -1, "__close");
rlm@1 130 return (lua_tocfunction(L, -1))(L);
rlm@1 131 }
rlm@1 132
rlm@1 133
rlm@1 134 static int io_close (lua_State *L) {
rlm@1 135 if (lua_isnone(L, 1))
rlm@1 136 lua_rawgeti(L, LUA_ENVIRONINDEX, IO_OUTPUT);
rlm@1 137 tofile(L); /* make sure argument is a file */
rlm@1 138 return aux_close(L);
rlm@1 139 }
rlm@1 140
rlm@1 141
rlm@1 142 static int io_gc (lua_State *L) {
rlm@1 143 FILE *f = *tofilep(L);
rlm@1 144 /* ignore closed files */
rlm@1 145 if (f != NULL)
rlm@1 146 aux_close(L);
rlm@1 147 return 0;
rlm@1 148 }
rlm@1 149
rlm@1 150
rlm@1 151 static int io_tostring (lua_State *L) {
rlm@1 152 FILE *f = *tofilep(L);
rlm@1 153 if (f == NULL)
rlm@1 154 lua_pushliteral(L, "file (closed)");
rlm@1 155 else
rlm@1 156 lua_pushfstring(L, "file (%p)", f);
rlm@1 157 return 1;
rlm@1 158 }
rlm@1 159
rlm@1 160
rlm@1 161 static int io_open (lua_State *L) {
rlm@1 162 const char *filename = luaL_checkstring(L, 1);
rlm@1 163 const char *mode = luaL_optstring(L, 2, "r");
rlm@1 164 FILE **pf = newfile(L);
rlm@1 165 *pf = fopen(filename, mode);
rlm@1 166 return (*pf == NULL) ? pushresult(L, 0, filename) : 1;
rlm@1 167 }
rlm@1 168
rlm@1 169
rlm@1 170 /*
rlm@1 171 ** this function has a separated environment, which defines the
rlm@1 172 ** correct __close for 'popen' files
rlm@1 173 */
rlm@1 174 static int io_popen (lua_State *L) {
rlm@1 175 const char *filename = luaL_checkstring(L, 1);
rlm@1 176 const char *mode = luaL_optstring(L, 2, "r");
rlm@1 177 FILE **pf = newfile(L);
rlm@1 178 *pf = lua_popen(L, filename, mode);
rlm@1 179 return (*pf == NULL) ? pushresult(L, 0, filename) : 1;
rlm@1 180 }
rlm@1 181
rlm@1 182
rlm@1 183 static int io_tmpfile (lua_State *L) {
rlm@1 184 FILE **pf = newfile(L);
rlm@1 185 *pf = tmpfile();
rlm@1 186 return (*pf == NULL) ? pushresult(L, 0, NULL) : 1;
rlm@1 187 }
rlm@1 188
rlm@1 189
rlm@1 190 static FILE *getiofile (lua_State *L, int findex) {
rlm@1 191 FILE *f;
rlm@1 192 lua_rawgeti(L, LUA_ENVIRONINDEX, findex);
rlm@1 193 f = *(FILE **)lua_touserdata(L, -1);
rlm@1 194 if (f == NULL)
rlm@1 195 luaL_error(L, "standard %s file is closed", fnames[findex - 1]);
rlm@1 196 return f;
rlm@1 197 }
rlm@1 198
rlm@1 199
rlm@1 200 static int g_iofile (lua_State *L, int f, const char *mode) {
rlm@1 201 if (!lua_isnoneornil(L, 1)) {
rlm@1 202 const char *filename = lua_tostring(L, 1);
rlm@1 203 if (filename) {
rlm@1 204 FILE **pf = newfile(L);
rlm@1 205 *pf = fopen(filename, mode);
rlm@1 206 if (*pf == NULL)
rlm@1 207 fileerror(L, 1, filename);
rlm@1 208 }
rlm@1 209 else {
rlm@1 210 tofile(L); /* check that it's a valid file handle */
rlm@1 211 lua_pushvalue(L, 1);
rlm@1 212 }
rlm@1 213 lua_rawseti(L, LUA_ENVIRONINDEX, f);
rlm@1 214 }
rlm@1 215 /* return current value */
rlm@1 216 lua_rawgeti(L, LUA_ENVIRONINDEX, f);
rlm@1 217 return 1;
rlm@1 218 }
rlm@1 219
rlm@1 220
rlm@1 221 static int io_input (lua_State *L) {
rlm@1 222 return g_iofile(L, IO_INPUT, "r");
rlm@1 223 }
rlm@1 224
rlm@1 225
rlm@1 226 static int io_output (lua_State *L) {
rlm@1 227 return g_iofile(L, IO_OUTPUT, "w");
rlm@1 228 }
rlm@1 229
rlm@1 230
rlm@1 231 static int io_readline (lua_State *L);
rlm@1 232
rlm@1 233
rlm@1 234 static void aux_lines (lua_State *L, int idx, int toclose) {
rlm@1 235 lua_pushvalue(L, idx);
rlm@1 236 lua_pushboolean(L, toclose); /* close/not close file when finished */
rlm@1 237 lua_pushcclosure(L, io_readline, 2);
rlm@1 238 }
rlm@1 239
rlm@1 240
rlm@1 241 static int f_lines (lua_State *L) {
rlm@1 242 tofile(L); /* check that it's a valid file handle */
rlm@1 243 aux_lines(L, 1, 0);
rlm@1 244 return 1;
rlm@1 245 }
rlm@1 246
rlm@1 247
rlm@1 248 static int io_lines (lua_State *L) {
rlm@1 249 if (lua_isnoneornil(L, 1)) { /* no arguments? */
rlm@1 250 /* will iterate over default input */
rlm@1 251 lua_rawgeti(L, LUA_ENVIRONINDEX, IO_INPUT);
rlm@1 252 return f_lines(L);
rlm@1 253 }
rlm@1 254 else {
rlm@1 255 const char *filename = luaL_checkstring(L, 1);
rlm@1 256 FILE **pf = newfile(L);
rlm@1 257 *pf = fopen(filename, "r");
rlm@1 258 if (*pf == NULL)
rlm@1 259 fileerror(L, 1, filename);
rlm@1 260 aux_lines(L, lua_gettop(L), 1);
rlm@1 261 return 1;
rlm@1 262 }
rlm@1 263 }
rlm@1 264
rlm@1 265
rlm@1 266 /*
rlm@1 267 ** {======================================================
rlm@1 268 ** READ
rlm@1 269 ** =======================================================
rlm@1 270 */
rlm@1 271
rlm@1 272
rlm@1 273 static int read_number (lua_State *L, FILE *f) {
rlm@1 274 lua_Number d;
rlm@1 275 if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1) {
rlm@1 276 lua_pushnumber(L, d);
rlm@1 277 return 1;
rlm@1 278 }
rlm@1 279 else return 0; /* read fails */
rlm@1 280 }
rlm@1 281
rlm@1 282
rlm@1 283 static int test_eof (lua_State *L, FILE *f) {
rlm@1 284 int c = getc(f);
rlm@1 285 ungetc(c, f);
rlm@1 286 lua_pushlstring(L, NULL, 0);
rlm@1 287 return (c != EOF);
rlm@1 288 }
rlm@1 289
rlm@1 290
rlm@1 291 static int read_line (lua_State *L, FILE *f) {
rlm@1 292 luaL_Buffer b;
rlm@1 293 luaL_buffinit(L, &b);
rlm@1 294 for (;;) {
rlm@1 295 size_t l;
rlm@1 296 char *p = luaL_prepbuffer(&b);
rlm@1 297 if (fgets(p, LUAL_BUFFERSIZE, f) == NULL) { /* eof? */
rlm@1 298 luaL_pushresult(&b); /* close buffer */
rlm@1 299 return (lua_objlen(L, -1) > 0); /* check whether read something */
rlm@1 300 }
rlm@1 301 l = strlen(p);
rlm@1 302 if (l == 0 || p[l-1] != '\n')
rlm@1 303 luaL_addsize(&b, l);
rlm@1 304 else {
rlm@1 305 luaL_addsize(&b, l - 1); /* do not include `eol' */
rlm@1 306 luaL_pushresult(&b); /* close buffer */
rlm@1 307 return 1; /* read at least an `eol' */
rlm@1 308 }
rlm@1 309 }
rlm@1 310 }
rlm@1 311
rlm@1 312
rlm@1 313 static int read_chars (lua_State *L, FILE *f, size_t n) {
rlm@1 314 size_t rlen; /* how much to read */
rlm@1 315 size_t nr; /* number of chars actually read */
rlm@1 316 luaL_Buffer b;
rlm@1 317 luaL_buffinit(L, &b);
rlm@1 318 rlen = LUAL_BUFFERSIZE; /* try to read that much each time */
rlm@1 319 do {
rlm@1 320 char *p = luaL_prepbuffer(&b);
rlm@1 321 if (rlen > n) rlen = n; /* cannot read more than asked */
rlm@1 322 nr = fread(p, sizeof(char), rlen, f);
rlm@1 323 luaL_addsize(&b, nr);
rlm@1 324 n -= nr; /* still have to read `n' chars */
rlm@1 325 } while (n > 0 && nr == rlen); /* until end of count or eof */
rlm@1 326 luaL_pushresult(&b); /* close buffer */
rlm@1 327 return (n == 0 || lua_objlen(L, -1) > 0);
rlm@1 328 }
rlm@1 329
rlm@1 330
rlm@1 331 static int g_read (lua_State *L, FILE *f, int first) {
rlm@1 332 int nargs = lua_gettop(L) - 1;
rlm@1 333 int success;
rlm@1 334 int n;
rlm@1 335 clearerr(f);
rlm@1 336 if (nargs == 0) { /* no arguments? */
rlm@1 337 success = read_line(L, f);
rlm@1 338 n = first+1; /* to return 1 result */
rlm@1 339 }
rlm@1 340 else { /* ensure stack space for all results and for auxlib's buffer */
rlm@1 341 luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments");
rlm@1 342 success = 1;
rlm@1 343 for (n = first; nargs-- && success; n++) {
rlm@1 344 if (lua_type(L, n) == LUA_TNUMBER) {
rlm@1 345 size_t l = (size_t)lua_tointeger(L, n);
rlm@1 346 success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
rlm@1 347 }
rlm@1 348 else {
rlm@1 349 const char *p = lua_tostring(L, n);
rlm@1 350 luaL_argcheck(L, p && p[0] == '*', n, "invalid option");
rlm@1 351 switch (p[1]) {
rlm@1 352 case 'n': /* number */
rlm@1 353 success = read_number(L, f);
rlm@1 354 break;
rlm@1 355 case 'l': /* line */
rlm@1 356 success = read_line(L, f);
rlm@1 357 break;
rlm@1 358 case 'a': /* file */
rlm@1 359 read_chars(L, f, ~((size_t)0)); /* read MAX_SIZE_T chars */
rlm@1 360 success = 1; /* always success */
rlm@1 361 break;
rlm@1 362 default:
rlm@1 363 return luaL_argerror(L, n, "invalid format");
rlm@1 364 }
rlm@1 365 }
rlm@1 366 }
rlm@1 367 }
rlm@1 368 if (ferror(f))
rlm@1 369 return pushresult(L, 0, NULL);
rlm@1 370 if (!success) {
rlm@1 371 lua_pop(L, 1); /* remove last result */
rlm@1 372 lua_pushnil(L); /* push nil instead */
rlm@1 373 }
rlm@1 374 return n - first;
rlm@1 375 }
rlm@1 376
rlm@1 377
rlm@1 378 static int io_read (lua_State *L) {
rlm@1 379 return g_read(L, getiofile(L, IO_INPUT), 1);
rlm@1 380 }
rlm@1 381
rlm@1 382
rlm@1 383 static int f_read (lua_State *L) {
rlm@1 384 return g_read(L, tofile(L), 2);
rlm@1 385 }
rlm@1 386
rlm@1 387
rlm@1 388 static int io_readline (lua_State *L) {
rlm@1 389 FILE *f = *(FILE **)lua_touserdata(L, lua_upvalueindex(1));
rlm@1 390 int sucess;
rlm@1 391 if (f == NULL) /* file is already closed? */
rlm@1 392 luaL_error(L, "file is already closed");
rlm@1 393 sucess = read_line(L, f);
rlm@1 394 if (ferror(f))
rlm@1 395 return luaL_error(L, "%s", strerror(errno));
rlm@1 396 if (sucess) return 1;
rlm@1 397 else { /* EOF */
rlm@1 398 if (lua_toboolean(L, lua_upvalueindex(2))) { /* generator created file? */
rlm@1 399 lua_settop(L, 0);
rlm@1 400 lua_pushvalue(L, lua_upvalueindex(1));
rlm@1 401 aux_close(L); /* close it */
rlm@1 402 }
rlm@1 403 return 0;
rlm@1 404 }
rlm@1 405 }
rlm@1 406
rlm@1 407 /* }====================================================== */
rlm@1 408
rlm@1 409
rlm@1 410 static int g_write (lua_State *L, FILE *f, int arg) {
rlm@1 411 int nargs = lua_gettop(L) - 1;
rlm@1 412 int status = 1;
rlm@1 413 for (; nargs--; arg++) {
rlm@1 414 if (lua_type(L, arg) == LUA_TNUMBER) {
rlm@1 415 /* optimization: could be done exactly as for strings */
rlm@1 416 status = status &&
rlm@1 417 fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0;
rlm@1 418 }
rlm@1 419 else {
rlm@1 420 size_t l;
rlm@1 421 const char *s = luaL_checklstring(L, arg, &l);
rlm@1 422 status = status && (fwrite(s, sizeof(char), l, f) == l);
rlm@1 423 }
rlm@1 424 }
rlm@1 425 return pushresult(L, status, NULL);
rlm@1 426 }
rlm@1 427
rlm@1 428
rlm@1 429 static int io_write (lua_State *L) {
rlm@1 430 return g_write(L, getiofile(L, IO_OUTPUT), 1);
rlm@1 431 }
rlm@1 432
rlm@1 433
rlm@1 434 static int f_write (lua_State *L) {
rlm@1 435 return g_write(L, tofile(L), 2);
rlm@1 436 }
rlm@1 437
rlm@1 438
rlm@1 439 static int f_seek (lua_State *L) {
rlm@1 440 static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
rlm@1 441 static const char *const modenames[] = {"set", "cur", "end", NULL};
rlm@1 442 FILE *f = tofile(L);
rlm@1 443 int op = luaL_checkoption(L, 2, "cur", modenames);
rlm@1 444 long offset = luaL_optlong(L, 3, 0);
rlm@1 445 op = fseek(f, offset, mode[op]);
rlm@1 446 if (op)
rlm@1 447 return pushresult(L, 0, NULL); /* error */
rlm@1 448 else {
rlm@1 449 lua_pushinteger(L, ftell(f));
rlm@1 450 return 1;
rlm@1 451 }
rlm@1 452 }
rlm@1 453
rlm@1 454
rlm@1 455 static int f_setvbuf (lua_State *L) {
rlm@1 456 static const int mode[] = {_IONBF, _IOFBF, _IOLBF};
rlm@1 457 static const char *const modenames[] = {"no", "full", "line", NULL};
rlm@1 458 FILE *f = tofile(L);
rlm@1 459 int op = luaL_checkoption(L, 2, NULL, modenames);
rlm@1 460 lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE);
rlm@1 461 int res = setvbuf(f, NULL, mode[op], sz);
rlm@1 462 return pushresult(L, res == 0, NULL);
rlm@1 463 }
rlm@1 464
rlm@1 465
rlm@1 466
rlm@1 467 static int io_flush (lua_State *L) {
rlm@1 468 return pushresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL);
rlm@1 469 }
rlm@1 470
rlm@1 471
rlm@1 472 static int f_flush (lua_State *L) {
rlm@1 473 return pushresult(L, fflush(tofile(L)) == 0, NULL);
rlm@1 474 }
rlm@1 475
rlm@1 476
rlm@1 477 static const luaL_Reg iolib[] = {
rlm@1 478 {"close", io_close},
rlm@1 479 {"flush", io_flush},
rlm@1 480 {"input", io_input},
rlm@1 481 {"lines", io_lines},
rlm@1 482 {"open", io_open},
rlm@1 483 {"output", io_output},
rlm@1 484 {"popen", io_popen},
rlm@1 485 {"read", io_read},
rlm@1 486 {"tmpfile", io_tmpfile},
rlm@1 487 {"type", io_type},
rlm@1 488 {"write", io_write},
rlm@1 489 {NULL, NULL}
rlm@1 490 };
rlm@1 491
rlm@1 492
rlm@1 493 static const luaL_Reg flib[] = {
rlm@1 494 {"close", io_close},
rlm@1 495 {"flush", f_flush},
rlm@1 496 {"lines", f_lines},
rlm@1 497 {"read", f_read},
rlm@1 498 {"seek", f_seek},
rlm@1 499 {"setvbuf", f_setvbuf},
rlm@1 500 {"write", f_write},
rlm@1 501 {"__gc", io_gc},
rlm@1 502 {"__tostring", io_tostring},
rlm@1 503 {NULL, NULL}
rlm@1 504 };
rlm@1 505
rlm@1 506
rlm@1 507 static void createmeta (lua_State *L) {
rlm@1 508 luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */
rlm@1 509 lua_pushvalue(L, -1); /* push metatable */
rlm@1 510 lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
rlm@1 511 luaL_register(L, NULL, flib); /* file methods */
rlm@1 512 }
rlm@1 513
rlm@1 514
rlm@1 515 static void createstdfile (lua_State *L, FILE *f, int k, const char *fname) {
rlm@1 516 *newfile(L) = f;
rlm@1 517 if (k > 0) {
rlm@1 518 lua_pushvalue(L, -1);
rlm@1 519 lua_rawseti(L, LUA_ENVIRONINDEX, k);
rlm@1 520 }
rlm@1 521 lua_pushvalue(L, -2); /* copy environment */
rlm@1 522 lua_setfenv(L, -2); /* set it */
rlm@1 523 lua_setfield(L, -3, fname);
rlm@1 524 }
rlm@1 525
rlm@1 526
rlm@1 527 static void newfenv (lua_State *L, lua_CFunction cls) {
rlm@1 528 lua_createtable(L, 0, 1);
rlm@1 529 lua_pushcfunction(L, cls);
rlm@1 530 lua_setfield(L, -2, "__close");
rlm@1 531 }
rlm@1 532
rlm@1 533
rlm@1 534 LUALIB_API int luaopen_io (lua_State *L) {
rlm@1 535 createmeta(L);
rlm@1 536 /* create (private) environment (with fields IO_INPUT, IO_OUTPUT, __close) */
rlm@1 537 newfenv(L, io_fclose);
rlm@1 538 lua_replace(L, LUA_ENVIRONINDEX);
rlm@1 539 /* open library */
rlm@1 540 luaL_register(L, LUA_IOLIBNAME, iolib);
rlm@1 541 /* create (and set) default files */
rlm@1 542 newfenv(L, io_noclose); /* close function for default files */
rlm@1 543 createstdfile(L, stdin, IO_INPUT, "stdin");
rlm@1 544 createstdfile(L, stdout, IO_OUTPUT, "stdout");
rlm@1 545 createstdfile(L, stderr, 0, "stderr");
rlm@1 546 lua_pop(L, 1); /* pop environment for default files */
rlm@1 547 lua_getfield(L, -1, "popen");
rlm@1 548 newfenv(L, io_pclose); /* create environment for 'popen' */
rlm@1 549 lua_setfenv(L, -2); /* set fenv for 'popen' */
rlm@1 550 lua_pop(L, 1); /* pop 'popen' */
rlm@1 551 return 1;
rlm@1 552 }
rlm@1 553