1997-09-16 23:25:59 +04:00
|
|
|
/*
|
2014-10-01 20:05:06 +04:00
|
|
|
** $Id: liolib.c,v 2.129 2014/09/26 14:09:21 roberto Exp roberto $
|
1997-09-16 23:25:59 +04:00
|
|
|
** Standard I/O (and system) library
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2011-09-14 01:09:04 +04:00
|
|
|
/*
|
|
|
|
** This definition must come before the inclusion of 'stdio.h'; it
|
|
|
|
** should not affect non-POSIX systems
|
|
|
|
*/
|
|
|
|
#if !defined(_FILE_OFFSET_BITS)
|
2013-04-11 22:34:06 +04:00
|
|
|
#define _LARGEFILE_SOURCE 1
|
|
|
|
#define _FILE_OFFSET_BITS 64
|
2011-09-14 01:09:04 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2014-05-21 19:24:21 +04:00
|
|
|
#include <ctype.h>
|
2002-02-09 01:39:56 +03:00
|
|
|
#include <errno.h>
|
2014-05-21 19:24:21 +04:00
|
|
|
#include <locale.h>
|
1993-07-28 17:18:00 +04:00
|
|
|
#include <stdio.h>
|
1997-12-18 22:11:43 +03:00
|
|
|
#include <stdlib.h>
|
1994-11-16 20:39:16 +03:00
|
|
|
#include <string.h>
|
1993-07-28 17:18:00 +04:00
|
|
|
|
2002-12-04 20:38:31 +03:00
|
|
|
#define liolib_c
|
2004-05-01 00:13:38 +04:00
|
|
|
#define LUA_LIB
|
2002-12-04 20:38:31 +03:00
|
|
|
|
1993-07-28 17:18:00 +04:00
|
|
|
#include "lua.h"
|
2000-06-12 17:52:05 +04:00
|
|
|
|
|
|
|
#include "lauxlib.h"
|
1994-08-17 19:10:04 +04:00
|
|
|
#include "lualib.h"
|
1993-07-28 17:18:00 +04:00
|
|
|
|
1996-11-01 20:03:36 +03:00
|
|
|
|
2014-03-14 00:07:18 +04:00
|
|
|
#if !defined(l_checkmode)
|
2013-03-17 01:10:18 +04:00
|
|
|
|
|
|
|
/*
|
2013-03-21 17:57:27 +04:00
|
|
|
** Check whether 'mode' matches '[rwa]%+?b?'.
|
|
|
|
** Change this macro to accept other modes for 'fopen' besides
|
|
|
|
** the standard ones.
|
2013-03-17 01:10:18 +04:00
|
|
|
*/
|
2014-03-14 00:07:18 +04:00
|
|
|
#define l_checkmode(mode) \
|
2013-03-21 17:57:27 +04:00
|
|
|
(*mode != '\0' && strchr("rwa", *(mode++)) != NULL && \
|
|
|
|
(*mode != '+' || ++mode) && /* skip if char is '+' */ \
|
|
|
|
(*mode != 'b' || ++mode) && /* skip if char is 'b' */ \
|
|
|
|
(*mode == '\0'))
|
2013-03-17 01:10:18 +04:00
|
|
|
|
|
|
|
#endif
|
2010-11-08 20:27:22 +03:00
|
|
|
|
2009-12-17 16:08:51 +03:00
|
|
|
/*
|
2011-09-14 01:09:04 +04:00
|
|
|
** {======================================================
|
2014-03-14 00:07:18 +04:00
|
|
|
** l_popen spawns a new process connected to the current
|
2011-09-14 01:09:04 +04:00
|
|
|
** one through the file streams.
|
|
|
|
** =======================================================
|
2009-12-17 16:08:51 +03:00
|
|
|
*/
|
2011-09-14 01:09:04 +04:00
|
|
|
|
2014-03-14 00:07:18 +04:00
|
|
|
#if !defined(l_popen) /* { */
|
2009-12-17 16:08:51 +03:00
|
|
|
|
2014-02-26 19:27:56 +04:00
|
|
|
#if defined(LUA_USE_POSIX) /* { */
|
2009-12-17 16:08:51 +03:00
|
|
|
|
2014-03-14 00:07:18 +04:00
|
|
|
#define l_popen(L,c,m) (fflush(NULL), popen(c,m))
|
|
|
|
#define l_pclose(L,file) (pclose(file))
|
2011-02-21 22:12:54 +03:00
|
|
|
|
2011-02-10 18:35:50 +03:00
|
|
|
#elif defined(LUA_WIN) /* }{ */
|
|
|
|
|
2014-03-14 00:07:18 +04:00
|
|
|
#define l_popen(L,c,m) (_popen(c,m))
|
|
|
|
#define l_pclose(L,file) (_pclose(file))
|
2011-02-10 18:35:50 +03:00
|
|
|
|
|
|
|
#else /* }{ */
|
|
|
|
|
2014-02-26 19:27:56 +04:00
|
|
|
/* ANSI definitions */
|
2014-03-14 00:07:18 +04:00
|
|
|
#define l_popen(L,c,m) \
|
2014-02-26 19:27:56 +04:00
|
|
|
((void)((void)c, m), \
|
|
|
|
luaL_error(L, LUA_QL("popen") " not supported"), \
|
|
|
|
(FILE*)0)
|
2014-03-14 00:07:18 +04:00
|
|
|
#define l_pclose(L,file) ((void)L, (void)file, -1)
|
2011-02-21 22:12:54 +03:00
|
|
|
|
2011-02-10 18:35:50 +03:00
|
|
|
#endif /* } */
|
|
|
|
|
2014-02-26 19:27:56 +04:00
|
|
|
#endif /* } */
|
2009-12-17 16:08:51 +03:00
|
|
|
|
2011-09-14 01:09:04 +04:00
|
|
|
/* }====================================================== */
|
|
|
|
|
|
|
|
|
2014-03-14 00:07:18 +04:00
|
|
|
#if !defined(l_getc) /* { */
|
2014-03-06 21:12:02 +04:00
|
|
|
|
|
|
|
#if defined(LUA_USE_POSIX)
|
2014-03-14 00:07:18 +04:00
|
|
|
#define l_getc(f) getc_unlocked(f)
|
|
|
|
#define l_lockfile(f) flockfile(f)
|
|
|
|
#define l_unlockfile(f) funlockfile(f)
|
2014-03-06 21:12:02 +04:00
|
|
|
#else
|
2014-03-14 00:07:18 +04:00
|
|
|
#define l_getc(f) getc(f)
|
|
|
|
#define l_lockfile(f) ((void)0)
|
|
|
|
#define l_unlockfile(f) ((void)0)
|
2014-03-06 21:12:02 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* } */
|
|
|
|
|
|
|
|
|
2011-09-14 01:09:04 +04:00
|
|
|
/*
|
|
|
|
** {======================================================
|
2014-03-14 00:07:18 +04:00
|
|
|
** l_fseek: configuration for longer offsets
|
2011-09-14 01:09:04 +04:00
|
|
|
** =======================================================
|
|
|
|
*/
|
|
|
|
|
2014-03-14 00:07:18 +04:00
|
|
|
#if !defined(l_fseek) /* { */
|
2011-09-14 01:09:04 +04:00
|
|
|
|
2013-04-11 22:34:06 +04:00
|
|
|
#if defined(LUA_USE_POSIX) /* { */
|
2011-09-14 01:09:04 +04:00
|
|
|
|
2014-05-11 18:46:19 +04:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2011-09-14 01:09:04 +04:00
|
|
|
#define l_fseek(f,o,w) fseeko(f,o,w)
|
|
|
|
#define l_ftell(f) ftello(f)
|
|
|
|
#define l_seeknum off_t
|
|
|
|
|
2011-11-25 16:50:03 +04:00
|
|
|
#elif defined(LUA_WIN) && !defined(_CRTIMP_TYPEINFO) \
|
2013-04-11 22:34:06 +04:00
|
|
|
&& defined(_MSC_VER) && (_MSC_VER >= 1400) /* }{ */
|
2011-09-14 01:09:04 +04:00
|
|
|
|
2014-02-26 19:27:56 +04:00
|
|
|
/* Windows (but not DDK) and Visual C++ 2005 or higher */
|
2011-09-14 01:09:04 +04:00
|
|
|
#define l_fseek(f,o,w) _fseeki64(f,o,w)
|
|
|
|
#define l_ftell(f) _ftelli64(f)
|
|
|
|
#define l_seeknum __int64
|
|
|
|
|
2014-02-26 19:27:56 +04:00
|
|
|
#else /* }{ */
|
2013-04-11 22:34:06 +04:00
|
|
|
|
2014-02-26 19:27:56 +04:00
|
|
|
/* ANSI definitions */
|
2011-09-14 01:09:04 +04:00
|
|
|
#define l_fseek(f,o,w) fseek(f,o,w)
|
|
|
|
#define l_ftell(f) ftell(f)
|
|
|
|
#define l_seeknum long
|
2014-02-26 19:27:56 +04:00
|
|
|
|
|
|
|
#endif /* } */
|
|
|
|
|
|
|
|
#endif /* } */
|
2011-09-14 01:09:04 +04:00
|
|
|
|
|
|
|
/* }====================================================== */
|
|
|
|
|
2001-02-06 19:01:29 +03:00
|
|
|
|
2011-06-27 23:42:31 +04:00
|
|
|
#define IO_PREFIX "_IO_"
|
2011-06-21 17:43:48 +04:00
|
|
|
#define IO_INPUT (IO_PREFIX "input")
|
|
|
|
#define IO_OUTPUT (IO_PREFIX "output")
|
2000-03-20 22:13:45 +03:00
|
|
|
|
1998-12-27 23:21:28 +03:00
|
|
|
|
2011-11-14 20:55:35 +04:00
|
|
|
typedef luaL_Stream LStream;
|
2004-07-09 19:47:48 +04:00
|
|
|
|
2004-08-09 18:35:59 +04:00
|
|
|
|
2011-06-21 17:43:48 +04:00
|
|
|
#define tolstream(L) ((LStream *)luaL_checkudata(L, 1, LUA_FILEHANDLE))
|
|
|
|
|
|
|
|
#define isclosed(p) ((p)->closef == NULL)
|
2002-11-25 18:05:39 +03:00
|
|
|
|
|
|
|
|
2002-12-04 18:27:17 +03:00
|
|
|
static int io_type (lua_State *L) {
|
2011-06-21 17:43:48 +04:00
|
|
|
LStream *p;
|
2005-08-17 23:05:04 +04:00
|
|
|
luaL_checkany(L, 1);
|
2011-06-21 17:43:48 +04:00
|
|
|
p = (LStream *)luaL_testudata(L, 1, LUA_FILEHANDLE);
|
|
|
|
if (p == NULL)
|
2005-08-17 23:05:04 +04:00
|
|
|
lua_pushnil(L); /* not a file */
|
2011-06-21 17:43:48 +04:00
|
|
|
else if (isclosed(p))
|
2002-12-04 18:27:17 +03:00
|
|
|
lua_pushliteral(L, "closed file");
|
|
|
|
else
|
|
|
|
lua_pushliteral(L, "file");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-21 17:43:48 +04:00
|
|
|
static int f_tostring (lua_State *L) {
|
|
|
|
LStream *p = tolstream(L);
|
|
|
|
if (isclosed(p))
|
|
|
|
lua_pushliteral(L, "file (closed)");
|
|
|
|
else
|
|
|
|
lua_pushfstring(L, "file (%p)", p->f);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-07-09 19:47:48 +04:00
|
|
|
static FILE *tofile (lua_State *L) {
|
2011-06-21 17:43:48 +04:00
|
|
|
LStream *p = tolstream(L);
|
|
|
|
if (isclosed(p))
|
2002-11-25 18:05:39 +03:00
|
|
|
luaL_error(L, "attempt to use a closed file");
|
2011-06-21 17:43:48 +04:00
|
|
|
lua_assert(p->f);
|
|
|
|
return p->f;
|
1999-03-16 23:07:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-10-17 00:41:35 +04:00
|
|
|
/*
|
|
|
|
** When creating file handles, always creates a `closed' file handle
|
|
|
|
** before opening the actual file; so, if there is a memory error, the
|
|
|
|
** file is not left opened.
|
|
|
|
*/
|
2011-06-21 17:43:48 +04:00
|
|
|
static LStream *newprefile (lua_State *L) {
|
|
|
|
LStream *p = (LStream *)lua_newuserdata(L, sizeof(LStream));
|
|
|
|
p->closef = NULL; /* mark file handle as 'closed' */
|
2010-11-10 21:05:36 +03:00
|
|
|
luaL_setmetatable(L, LUA_FILEHANDLE);
|
2011-06-21 17:43:48 +04:00
|
|
|
return p;
|
1997-10-30 23:29:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-26 18:09:21 +04:00
|
|
|
/*
|
|
|
|
** Calls the 'close' function from a file handle. The 'volatile' avoids
|
|
|
|
** a bug in some verisions of the Clang compiler (e.g., clang 3.0 for
|
|
|
|
** 32 bits).
|
|
|
|
*/
|
2011-06-21 17:43:48 +04:00
|
|
|
static int aux_close (lua_State *L) {
|
|
|
|
LStream *p = tolstream(L);
|
2014-09-26 18:09:21 +04:00
|
|
|
volatile lua_CFunction cf = p->closef;
|
2011-06-21 17:43:48 +04:00
|
|
|
p->closef = NULL; /* mark stream as closed */
|
|
|
|
return (*cf)(L); /* close it */
|
2010-03-18 00:37:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-21 17:43:48 +04:00
|
|
|
static int io_close (lua_State *L) {
|
|
|
|
if (lua_isnone(L, 1)) /* no argument? */
|
|
|
|
lua_getfield(L, LUA_REGISTRYINDEX, IO_OUTPUT); /* use standard output */
|
|
|
|
tofile(L); /* make sure argument is an open stream */
|
|
|
|
return aux_close(L);
|
2007-04-20 00:22:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-21 17:43:48 +04:00
|
|
|
static int f_gc (lua_State *L) {
|
|
|
|
LStream *p = tolstream(L);
|
|
|
|
if (!isclosed(p) && p->f != NULL)
|
|
|
|
aux_close(L); /* ignore closed and incompletely open files */
|
|
|
|
return 0;
|
2005-06-06 17:30:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-20 00:22:32 +04:00
|
|
|
/*
|
|
|
|
** function to close regular files
|
|
|
|
*/
|
2005-06-06 17:30:52 +04:00
|
|
|
static int io_fclose (lua_State *L) {
|
2011-06-21 17:43:48 +04:00
|
|
|
LStream *p = tolstream(L);
|
|
|
|
int res = fclose(p->f);
|
|
|
|
return luaL_fileresult(L, (res == 0), NULL);
|
2005-06-06 17:30:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-21 17:43:48 +04:00
|
|
|
static LStream *newfile (lua_State *L) {
|
|
|
|
LStream *p = newprefile(L);
|
|
|
|
p->f = NULL;
|
|
|
|
p->closef = &io_fclose;
|
|
|
|
return p;
|
2002-11-25 18:05:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-28 22:41:15 +04:00
|
|
|
static void opencheck (lua_State *L, const char *fname, const char *mode) {
|
|
|
|
LStream *p = newfile(L);
|
|
|
|
p->f = fopen(fname, mode);
|
2011-08-02 22:00:01 +04:00
|
|
|
if (p->f == NULL)
|
|
|
|
luaL_error(L, "cannot open file " LUA_QS " (%s)", fname, strerror(errno));
|
2011-07-28 22:41:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-04-05 00:24:56 +04:00
|
|
|
static int io_open (lua_State *L) {
|
2002-11-14 18:41:38 +03:00
|
|
|
const char *filename = luaL_checkstring(L, 1);
|
|
|
|
const char *mode = luaL_optstring(L, 2, "r");
|
2011-06-21 17:43:48 +04:00
|
|
|
LStream *p = newfile(L);
|
2013-03-21 17:57:27 +04:00
|
|
|
const char *md = mode; /* to traverse/check mode */
|
2014-03-14 00:07:18 +04:00
|
|
|
luaL_argcheck(L, l_checkmode(md), 2, "invalid mode");
|
2011-06-21 17:43:48 +04:00
|
|
|
p->f = fopen(filename, mode);
|
|
|
|
return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1;
|
2000-05-30 22:55:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-20 00:22:32 +04:00
|
|
|
/*
|
2011-06-21 17:43:48 +04:00
|
|
|
** function to close 'popen' files
|
2007-04-20 00:22:32 +04:00
|
|
|
*/
|
2011-06-21 17:43:48 +04:00
|
|
|
static int io_pclose (lua_State *L) {
|
|
|
|
LStream *p = tolstream(L);
|
2014-03-14 00:07:18 +04:00
|
|
|
return luaL_execresult(L, l_pclose(L, p->f));
|
2011-06-21 17:43:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-06 17:30:52 +04:00
|
|
|
static int io_popen (lua_State *L) {
|
|
|
|
const char *filename = luaL_checkstring(L, 1);
|
|
|
|
const char *mode = luaL_optstring(L, 2, "r");
|
2011-06-21 17:43:48 +04:00
|
|
|
LStream *p = newprefile(L);
|
2014-03-14 00:07:18 +04:00
|
|
|
p->f = l_popen(L, filename, mode);
|
2011-06-21 17:43:48 +04:00
|
|
|
p->closef = &io_pclose;
|
|
|
|
return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1;
|
2005-06-06 17:30:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-11-23 16:49:35 +03:00
|
|
|
static int io_tmpfile (lua_State *L) {
|
2011-06-21 17:43:48 +04:00
|
|
|
LStream *p = newfile(L);
|
|
|
|
p->f = tmpfile();
|
|
|
|
return (p->f == NULL) ? luaL_fileresult(L, 0, NULL) : 1;
|
1997-06-27 00:39:10 +04:00
|
|
|
}
|
|
|
|
|
1996-11-01 20:03:36 +03:00
|
|
|
|
2011-06-21 17:43:48 +04:00
|
|
|
static FILE *getiofile (lua_State *L, const char *findex) {
|
|
|
|
LStream *p;
|
|
|
|
lua_getfield(L, LUA_REGISTRYINDEX, findex);
|
|
|
|
p = (LStream *)lua_touserdata(L, -1);
|
|
|
|
if (isclosed(p))
|
|
|
|
luaL_error(L, "standard %s file is closed", findex + strlen(IO_PREFIX));
|
|
|
|
return p->f;
|
2002-04-05 00:24:56 +04:00
|
|
|
}
|
2000-03-20 22:13:45 +03:00
|
|
|
|
2002-04-05 00:24:56 +04:00
|
|
|
|
2011-06-21 17:43:48 +04:00
|
|
|
static int g_iofile (lua_State *L, const char *f, const char *mode) {
|
2003-03-14 22:08:11 +03:00
|
|
|
if (!lua_isnoneornil(L, 1)) {
|
2002-04-05 00:24:56 +04:00
|
|
|
const char *filename = lua_tostring(L, 1);
|
2011-07-28 22:41:15 +04:00
|
|
|
if (filename)
|
|
|
|
opencheck(L, filename, mode);
|
2002-04-05 00:24:56 +04:00
|
|
|
else {
|
2004-07-09 19:47:48 +04:00
|
|
|
tofile(L); /* check that it's a valid file handle */
|
2002-04-05 00:24:56 +04:00
|
|
|
lua_pushvalue(L, 1);
|
|
|
|
}
|
2011-06-21 17:43:48 +04:00
|
|
|
lua_setfield(L, LUA_REGISTRYINDEX, f);
|
1996-11-01 20:03:36 +03:00
|
|
|
}
|
2003-03-14 22:08:11 +03:00
|
|
|
/* return current value */
|
2011-06-21 17:43:48 +04:00
|
|
|
lua_getfield(L, LUA_REGISTRYINDEX, f);
|
2003-03-14 22:08:11 +03:00
|
|
|
return 1;
|
2000-03-20 22:13:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-04-05 00:24:56 +04:00
|
|
|
static int io_input (lua_State *L) {
|
|
|
|
return g_iofile(L, IO_INPUT, "r");
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-04-05 00:24:56 +04:00
|
|
|
static int io_output (lua_State *L) {
|
|
|
|
return g_iofile(L, IO_OUTPUT, "w");
|
1995-11-10 20:55:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-18 00:35:54 +04:00
|
|
|
static int io_readline (lua_State *L);
|
|
|
|
|
2002-10-12 00:40:32 +04:00
|
|
|
|
2010-03-03 21:48:57 +03:00
|
|
|
static void aux_lines (lua_State *L, int toclose) {
|
|
|
|
int n = lua_gettop(L) - 1; /* number of arguments to read */
|
|
|
|
lua_pushinteger(L, n); /* number of arguments to read */
|
2005-03-18 21:01:14 +03:00
|
|
|
lua_pushboolean(L, toclose); /* close/not close file when finished */
|
2014-05-13 23:40:28 +04:00
|
|
|
lua_rotate(L, 2, 2); /* move 'n' and 'toclose' to their positions */
|
2010-03-03 21:48:57 +03:00
|
|
|
lua_pushcclosure(L, io_readline, 3 + n);
|
2002-09-18 00:35:54 +04:00
|
|
|
}
|
|
|
|
|
2002-10-12 00:40:32 +04:00
|
|
|
|
2002-09-18 00:35:54 +04:00
|
|
|
static int f_lines (lua_State *L) {
|
2004-07-09 19:47:48 +04:00
|
|
|
tofile(L); /* check that it's a valid file handle */
|
2010-03-03 21:48:57 +03:00
|
|
|
aux_lines(L, 0);
|
2002-09-18 00:35:54 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-10-12 00:40:32 +04:00
|
|
|
static int io_lines (lua_State *L) {
|
2010-03-03 21:48:57 +03:00
|
|
|
int toclose;
|
|
|
|
if (lua_isnone(L, 1)) lua_pushnil(L); /* at least one argument */
|
|
|
|
if (lua_isnil(L, 1)) { /* no file name? */
|
2011-06-21 17:43:48 +04:00
|
|
|
lua_getfield(L, LUA_REGISTRYINDEX, IO_INPUT); /* get default input */
|
2011-02-21 22:12:54 +03:00
|
|
|
lua_replace(L, 1); /* put it at index 1 */
|
2010-03-03 21:48:57 +03:00
|
|
|
tofile(L); /* check that it's a valid file handle */
|
|
|
|
toclose = 0; /* do not close it after iteration */
|
2002-10-12 00:40:32 +04:00
|
|
|
}
|
2010-03-03 21:48:57 +03:00
|
|
|
else { /* open a new file */
|
2002-11-14 18:41:38 +03:00
|
|
|
const char *filename = luaL_checkstring(L, 1);
|
2011-07-28 22:41:15 +04:00
|
|
|
opencheck(L, filename, "r");
|
2011-02-21 22:12:54 +03:00
|
|
|
lua_replace(L, 1); /* put file at index 1 */
|
2010-03-03 21:48:57 +03:00
|
|
|
toclose = 1; /* close it after iteration */
|
2002-10-12 00:40:32 +04:00
|
|
|
}
|
2010-03-03 21:48:57 +03:00
|
|
|
aux_lines(L, toclose);
|
|
|
|
return 1;
|
2002-10-12 00:40:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-01-04 15:41:12 +03:00
|
|
|
/*
|
|
|
|
** {======================================================
|
1998-12-27 23:21:28 +03:00
|
|
|
** READ
|
1999-01-04 15:41:12 +03:00
|
|
|
** =======================================================
|
|
|
|
*/
|
1998-12-27 23:21:28 +03:00
|
|
|
|
1999-04-15 00:40:32 +04:00
|
|
|
|
2014-05-21 19:24:21 +04:00
|
|
|
/* maximum length of a numeral */
|
|
|
|
#define MAXRN 200
|
|
|
|
|
|
|
|
/* auxiliary structure used by 'read_number' */
|
|
|
|
typedef struct {
|
|
|
|
FILE *f; /* file being read */
|
|
|
|
int c; /* current character (look ahead) */
|
|
|
|
int n; /* number of elements in buffer 'buff' */
|
2014-06-02 07:00:51 +04:00
|
|
|
char buff[MAXRN + 1]; /* +1 for ending '\0' */
|
2014-05-21 19:24:21 +04:00
|
|
|
} RN;
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Add current char to buffer (if not out of space) and read next one
|
|
|
|
*/
|
|
|
|
static int nextc (RN *rn) {
|
2014-06-02 07:00:51 +04:00
|
|
|
if (rn->n >= MAXRN) { /* buffer overflow? */
|
|
|
|
rn->buff[0] = '\0'; /* invalidate result */
|
2014-05-21 19:24:21 +04:00
|
|
|
return 0; /* fail */
|
2014-06-02 07:00:51 +04:00
|
|
|
}
|
2013-05-14 19:57:43 +04:00
|
|
|
else {
|
2014-05-21 19:24:21 +04:00
|
|
|
rn->buff[rn->n++] = rn->c; /* save current char */
|
|
|
|
rn->c = l_getc(rn->f); /* read next one */
|
|
|
|
return 1;
|
2013-05-14 19:57:43 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-21 19:24:21 +04:00
|
|
|
/*
|
|
|
|
** Accept current char if it is in 'set' (of size 1 or 2)
|
|
|
|
*/
|
|
|
|
static int test2 (RN *rn, const char *set) {
|
|
|
|
if (rn->c == set[0] || (rn->c == set[1] && rn->c != '\0'))
|
|
|
|
return nextc(rn);
|
|
|
|
else return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2014-06-30 23:48:08 +04:00
|
|
|
** Read a sequence of (hex)digits
|
2014-05-21 19:24:21 +04:00
|
|
|
*/
|
2014-06-30 23:48:08 +04:00
|
|
|
static int readdigits (RN *rn, int hex) {
|
2014-05-21 19:24:21 +04:00
|
|
|
int count = 0;
|
2014-06-30 23:48:08 +04:00
|
|
|
while ((hex ? isxdigit(rn->c) : isdigit(rn->c)) && nextc(rn))
|
2014-05-21 19:24:21 +04:00
|
|
|
count++;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* access to locale "radix character" (decimal point) */
|
|
|
|
#if !defined(getlocaledecpoint)
|
|
|
|
#define getlocaledecpoint() (localeconv()->decimal_point[0])
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Read a number: first reads a valid prefix of a numeral into a buffer.
|
|
|
|
** Then it calls 'lua_strtonum' to check whether the format is correct
|
|
|
|
** and to convert it to a Lua number
|
|
|
|
*/
|
2001-06-22 17:49:42 +04:00
|
|
|
static int read_number (lua_State *L, FILE *f) {
|
2014-05-21 19:24:21 +04:00
|
|
|
RN rn;
|
|
|
|
int count = 0;
|
2014-06-30 23:48:08 +04:00
|
|
|
int hex = 0;
|
2014-05-21 19:24:21 +04:00
|
|
|
char decp[2] = ".";
|
|
|
|
rn.f = f; rn.n = 0;
|
|
|
|
decp[0] = getlocaledecpoint(); /* get decimal point from locale */
|
|
|
|
l_lockfile(rn.f);
|
|
|
|
do { rn.c = l_getc(rn.f); } while (isspace(rn.c)); /* skip spaces */
|
|
|
|
test2(&rn, "-+"); /* optional signal */
|
|
|
|
if (test2(&rn, "0")) {
|
2014-06-30 23:48:08 +04:00
|
|
|
if (test2(&rn, "xX")) hex = 1; /* numeral is hexadecimal */
|
2014-05-21 19:24:21 +04:00
|
|
|
else count = 1; /* count initial '0' as a valid digit */
|
2000-05-24 17:54:49 +04:00
|
|
|
}
|
2014-06-30 23:48:08 +04:00
|
|
|
count += readdigits(&rn, hex); /* integral part */
|
2014-05-21 19:24:21 +04:00
|
|
|
if (test2(&rn, decp)) /* decimal point? */
|
2014-06-30 23:48:08 +04:00
|
|
|
count += readdigits(&rn, hex); /* fractional part */
|
|
|
|
if (count > 0 && test2(&rn, (hex ? "pP" : "eE"))) { /* exponent mark? */
|
2014-05-21 19:24:21 +04:00
|
|
|
test2(&rn, "-+"); /* exponent signal */
|
|
|
|
readdigits(&rn, 0); /* exponent digits */
|
|
|
|
}
|
|
|
|
ungetc(rn.c, rn.f); /* unread look-ahead char */
|
|
|
|
l_unlockfile(rn.f);
|
|
|
|
rn.buff[rn.n] = '\0'; /* finish string */
|
|
|
|
if (lua_strtonum(L, rn.buff)) /* is this a valid number? */
|
|
|
|
return 1; /* ok */
|
|
|
|
else { /* invalid format */
|
2010-03-03 21:48:57 +03:00
|
|
|
lua_pushnil(L); /* "result" to be removed */
|
|
|
|
return 0; /* read fails */
|
|
|
|
}
|
1998-12-27 23:21:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-22 17:49:42 +04:00
|
|
|
static int test_eof (lua_State *L, FILE *f) {
|
2001-11-28 23:13:13 +03:00
|
|
|
int c = getc(f);
|
2014-01-27 17:28:45 +04:00
|
|
|
ungetc(c, f); /* no-op when c == EOF */
|
2001-06-22 17:49:42 +04:00
|
|
|
lua_pushlstring(L, NULL, 0);
|
|
|
|
return (c != EOF);
|
1998-12-27 23:21:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-03 21:48:57 +03:00
|
|
|
static int read_line (lua_State *L, FILE *f, int chop) {
|
2002-04-12 23:56:25 +04:00
|
|
|
luaL_Buffer b;
|
2014-03-06 21:12:02 +04:00
|
|
|
int c;
|
2002-04-12 23:56:25 +04:00
|
|
|
luaL_buffinit(L, &b);
|
2014-03-14 00:07:18 +04:00
|
|
|
l_lockfile(f);
|
|
|
|
while ((c = l_getc(f)) != EOF && c != '\n')
|
2014-03-06 21:12:02 +04:00
|
|
|
luaL_addchar(&b, c);
|
2014-03-14 00:07:18 +04:00
|
|
|
l_unlockfile(f);
|
2014-03-06 21:12:02 +04:00
|
|
|
if (!chop && c == '\n') luaL_addchar(&b, c);
|
|
|
|
luaL_pushresult(&b); /* close buffer */
|
|
|
|
return (c == '\n' || lua_rawlen(L, -1) > 0);
|
2002-04-12 23:56:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-09 19:57:49 +03:00
|
|
|
static void read_all (lua_State *L, FILE *f) {
|
2014-02-21 18:39:50 +04:00
|
|
|
size_t nr;
|
2001-06-22 17:49:42 +04:00
|
|
|
luaL_Buffer b;
|
|
|
|
luaL_buffinit(L, &b);
|
2014-02-21 18:39:50 +04:00
|
|
|
do { /* read file in chunks of LUAL_BUFFERSIZE bytes */
|
|
|
|
char *p = luaL_prepbuffsize(&b, LUAL_BUFFERSIZE);
|
|
|
|
nr = fread(p, sizeof(char), LUAL_BUFFERSIZE, f);
|
2001-07-12 18:59:14 +04:00
|
|
|
luaL_addsize(&b, nr);
|
2014-02-21 18:39:50 +04:00
|
|
|
} while (nr == LUAL_BUFFERSIZE);
|
2010-11-09 19:57:49 +03:00
|
|
|
luaL_pushresult(&b); /* close buffer */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int read_chars (lua_State *L, FILE *f, size_t n) {
|
|
|
|
size_t nr; /* number of chars actually read */
|
|
|
|
char *p;
|
|
|
|
luaL_Buffer b;
|
|
|
|
luaL_buffinit(L, &b);
|
|
|
|
p = luaL_prepbuffsize(&b, n); /* prepare buffer to read whole block */
|
|
|
|
nr = fread(p, sizeof(char), n, f); /* try to read 'n' chars */
|
|
|
|
luaL_addsize(&b, nr);
|
2001-06-22 17:49:42 +04:00
|
|
|
luaL_pushresult(&b); /* close buffer */
|
2010-11-09 19:57:49 +03:00
|
|
|
return (nr > 0); /* true iff read something */
|
1999-10-07 23:18:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-04-05 00:24:56 +04:00
|
|
|
static int g_read (lua_State *L, FILE *f, int first) {
|
2001-06-22 17:49:42 +04:00
|
|
|
int nargs = lua_gettop(L) - 1;
|
2000-12-22 20:32:28 +03:00
|
|
|
int success;
|
2000-08-29 18:33:31 +04:00
|
|
|
int n;
|
2004-08-13 23:52:13 +04:00
|
|
|
clearerr(f);
|
2001-01-25 19:45:36 +03:00
|
|
|
if (nargs == 0) { /* no arguments? */
|
2010-03-03 21:48:57 +03:00
|
|
|
success = read_line(L, f, 1);
|
2002-04-05 00:24:56 +04:00
|
|
|
n = first+1; /* to return 1 result */
|
2000-08-29 18:33:31 +04:00
|
|
|
}
|
2001-01-25 19:45:36 +03:00
|
|
|
else { /* ensure stack space for all results and for auxlib's buffer */
|
2002-11-14 18:41:38 +03:00
|
|
|
luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments");
|
2001-01-25 19:45:36 +03:00
|
|
|
success = 1;
|
2002-04-05 00:24:56 +04:00
|
|
|
for (n = first; nargs-- && success; n++) {
|
2001-12-05 23:15:18 +03:00
|
|
|
if (lua_type(L, n) == LUA_TNUMBER) {
|
2003-10-08 00:13:41 +04:00
|
|
|
size_t l = (size_t)lua_tointeger(L, n);
|
2001-06-22 17:49:42 +04:00
|
|
|
success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
|
|
|
|
}
|
1999-10-07 23:18:36 +04:00
|
|
|
else {
|
2014-04-15 20:46:45 +04:00
|
|
|
const char *p = luaL_checkstring(L, n);
|
|
|
|
if (*p == '*') p++; /* skip optional '*' (for compatibility) */
|
|
|
|
switch (*p) {
|
2001-11-28 23:13:13 +03:00
|
|
|
case 'n': /* number */
|
2000-12-22 20:32:28 +03:00
|
|
|
success = read_number(L, f);
|
|
|
|
break;
|
2001-11-28 23:13:13 +03:00
|
|
|
case 'l': /* line */
|
2010-03-03 21:48:57 +03:00
|
|
|
success = read_line(L, f, 1);
|
|
|
|
break;
|
|
|
|
case 'L': /* line with end-of-line */
|
|
|
|
success = read_line(L, f, 0);
|
1999-10-07 23:18:36 +04:00
|
|
|
break;
|
2001-11-28 23:13:13 +03:00
|
|
|
case 'a': /* file */
|
2010-11-09 19:57:49 +03:00
|
|
|
read_all(L, f); /* read entire file */
|
1999-10-07 23:18:36 +04:00
|
|
|
success = 1; /* always success */
|
|
|
|
break;
|
|
|
|
default:
|
2002-05-06 23:05:10 +04:00
|
|
|
return luaL_argerror(L, n, "invalid format");
|
1999-10-07 23:18:36 +04:00
|
|
|
}
|
|
|
|
}
|
1998-12-27 23:21:28 +03:00
|
|
|
}
|
2000-12-22 20:32:28 +03:00
|
|
|
}
|
2004-08-13 23:52:13 +04:00
|
|
|
if (ferror(f))
|
2011-03-03 19:34:46 +03:00
|
|
|
return luaL_fileresult(L, 0, NULL);
|
2000-12-22 20:32:28 +03:00
|
|
|
if (!success) {
|
|
|
|
lua_pop(L, 1); /* remove last result */
|
|
|
|
lua_pushnil(L); /* push nil instead */
|
|
|
|
}
|
2002-04-05 00:24:56 +04:00
|
|
|
return n - first;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int io_read (lua_State *L) {
|
|
|
|
return g_read(L, getiofile(L, IO_INPUT), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int f_read (lua_State *L) {
|
2004-07-09 19:47:48 +04:00
|
|
|
return g_read(L, tofile(L), 2);
|
1995-11-10 20:55:48 +03:00
|
|
|
}
|
|
|
|
|
2002-09-18 00:35:54 +04:00
|
|
|
|
|
|
|
static int io_readline (lua_State *L) {
|
2011-06-21 17:43:48 +04:00
|
|
|
LStream *p = (LStream *)lua_touserdata(L, lua_upvalueindex(1));
|
2010-03-03 21:48:57 +03:00
|
|
|
int i;
|
2010-10-25 23:01:37 +04:00
|
|
|
int n = (int)lua_tointeger(L, lua_upvalueindex(2));
|
2011-06-21 17:43:48 +04:00
|
|
|
if (isclosed(p)) /* file is already closed? */
|
|
|
|
return luaL_error(L, "file is already closed");
|
2010-03-03 21:48:57 +03:00
|
|
|
lua_settop(L , 1);
|
2014-05-15 19:21:06 +04:00
|
|
|
luaL_checkstack(L, n, "too many arguments");
|
2010-03-03 21:48:57 +03:00
|
|
|
for (i = 1; i <= n; i++) /* push arguments to 'g_read' */
|
|
|
|
lua_pushvalue(L, lua_upvalueindex(3 + i));
|
2011-06-21 17:43:48 +04:00
|
|
|
n = g_read(L, p->f, 2); /* 'n' is number of results */
|
2010-03-03 21:48:57 +03:00
|
|
|
lua_assert(n > 0); /* should return at least a nil */
|
2014-10-01 20:05:06 +04:00
|
|
|
if (lua_toboolean(L, -n)) /* read at least one value? */
|
2010-03-03 21:48:57 +03:00
|
|
|
return n; /* return them */
|
|
|
|
else { /* first result is nil: EOF or error */
|
2011-11-09 18:10:43 +04:00
|
|
|
if (n > 1) { /* is there error information? */
|
|
|
|
/* 2nd result is error message */
|
|
|
|
return luaL_error(L, "%s", lua_tostring(L, -n + 1));
|
|
|
|
}
|
2010-03-03 21:48:57 +03:00
|
|
|
if (lua_toboolean(L, lua_upvalueindex(3))) { /* generator created file? */
|
2002-09-18 00:35:54 +04:00
|
|
|
lua_settop(L, 0);
|
2005-02-18 15:40:02 +03:00
|
|
|
lua_pushvalue(L, lua_upvalueindex(1));
|
2002-09-18 00:35:54 +04:00
|
|
|
aux_close(L); /* close it */
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-01-04 15:41:12 +03:00
|
|
|
/* }====================================================== */
|
|
|
|
|
1995-11-10 20:55:48 +03:00
|
|
|
|
2002-04-05 00:24:56 +04:00
|
|
|
static int g_write (lua_State *L, FILE *f, int arg) {
|
2009-09-01 23:10:48 +04:00
|
|
|
int nargs = lua_gettop(L) - arg;
|
1996-11-01 20:03:36 +03:00
|
|
|
int status = 1;
|
2002-04-05 00:24:56 +04:00
|
|
|
for (; nargs--; arg++) {
|
2001-12-05 23:15:18 +03:00
|
|
|
if (lua_type(L, arg) == LUA_TNUMBER) {
|
1999-12-28 14:52:49 +03:00
|
|
|
/* optimization: could be done exactly as for strings */
|
2013-06-07 23:01:35 +04:00
|
|
|
int len = lua_isinteger(L, arg)
|
|
|
|
? fprintf(f, LUA_INTEGER_FMT, lua_tointeger(L, arg))
|
|
|
|
: fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg));
|
|
|
|
status = status && (len > 0);
|
1999-12-28 14:52:49 +03:00
|
|
|
}
|
|
|
|
else {
|
2000-05-24 17:54:49 +04:00
|
|
|
size_t l;
|
2002-11-14 18:41:38 +03:00
|
|
|
const char *s = luaL_checklstring(L, arg, &l);
|
2001-11-28 23:13:13 +03:00
|
|
|
status = status && (fwrite(s, sizeof(char), l, f) == l);
|
1999-10-07 23:18:36 +04:00
|
|
|
}
|
|
|
|
}
|
2009-09-01 23:10:48 +04:00
|
|
|
if (status) return 1; /* file handle already on stack top */
|
2011-03-03 19:34:46 +03:00
|
|
|
else return luaL_fileresult(L, status, NULL);
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|
|
|
|
|
1996-11-01 20:03:36 +03:00
|
|
|
|
2002-04-05 00:24:56 +04:00
|
|
|
static int io_write (lua_State *L) {
|
|
|
|
return g_write(L, getiofile(L, IO_OUTPUT), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int f_write (lua_State *L) {
|
2011-06-21 17:43:48 +04:00
|
|
|
FILE *f = tofile(L);
|
2009-09-01 23:10:48 +04:00
|
|
|
lua_pushvalue(L, 1); /* push file at the stack top (to be returned) */
|
|
|
|
return g_write(L, f, 2);
|
2002-04-05 00:24:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int f_seek (lua_State *L) {
|
1999-08-17 00:52:00 +04:00
|
|
|
static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
|
2001-11-28 23:13:13 +03:00
|
|
|
static const char *const modenames[] = {"set", "cur", "end", NULL};
|
2004-07-09 19:47:48 +04:00
|
|
|
FILE *f = tofile(L);
|
2005-05-25 17:21:26 +04:00
|
|
|
int op = luaL_checkoption(L, 2, "cur", modenames);
|
2014-03-19 22:57:42 +04:00
|
|
|
lua_Integer p3 = luaL_optinteger(L, 3, 0);
|
2011-09-14 01:09:04 +04:00
|
|
|
l_seeknum offset = (l_seeknum)p3;
|
2014-03-19 22:57:42 +04:00
|
|
|
luaL_argcheck(L, (lua_Integer)offset == p3, 3,
|
2011-09-14 01:09:04 +04:00
|
|
|
"not an integer in proper range");
|
|
|
|
op = l_fseek(f, offset, mode[op]);
|
1998-08-25 00:14:56 +04:00
|
|
|
if (op)
|
2011-03-03 19:34:46 +03:00
|
|
|
return luaL_fileresult(L, 0, NULL); /* error */
|
2000-08-28 21:57:04 +04:00
|
|
|
else {
|
2014-03-19 22:57:42 +04:00
|
|
|
lua_pushinteger(L, (lua_Integer)l_ftell(f));
|
2000-08-28 21:57:04 +04:00
|
|
|
return 1;
|
|
|
|
}
|
1998-08-25 00:14:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-05-14 23:03:43 +04:00
|
|
|
static int f_setvbuf (lua_State *L) {
|
|
|
|
static const int mode[] = {_IONBF, _IOFBF, _IOLBF};
|
|
|
|
static const char *const modenames[] = {"no", "full", "line", NULL};
|
2004-07-09 19:47:48 +04:00
|
|
|
FILE *f = tofile(L);
|
2005-05-25 17:21:26 +04:00
|
|
|
int op = luaL_checkoption(L, 2, NULL, modenames);
|
2005-10-14 20:24:11 +04:00
|
|
|
lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE);
|
2014-07-29 20:01:00 +04:00
|
|
|
int res = setvbuf(f, NULL, mode[op], (size_t)sz);
|
2011-03-03 19:34:46 +03:00
|
|
|
return luaL_fileresult(L, res == 0, NULL);
|
2003-05-14 23:03:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-08-28 21:57:04 +04:00
|
|
|
static int io_flush (lua_State *L) {
|
2011-03-03 19:34:46 +03:00
|
|
|
return luaL_fileresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL);
|
2002-04-05 00:24:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int f_flush (lua_State *L) {
|
2011-03-03 19:34:46 +03:00
|
|
|
return luaL_fileresult(L, fflush(tofile(L)) == 0, NULL);
|
1998-08-25 00:14:56 +04:00
|
|
|
}
|
|
|
|
|
2002-04-05 00:24:56 +04:00
|
|
|
|
2011-06-21 17:43:48 +04:00
|
|
|
/*
|
|
|
|
** functions for 'io' library
|
|
|
|
*/
|
2005-08-26 21:36:32 +04:00
|
|
|
static const luaL_Reg iolib[] = {
|
2002-03-20 15:54:08 +03:00
|
|
|
{"close", io_close},
|
2002-04-05 00:24:56 +04:00
|
|
|
{"flush", io_flush},
|
2005-07-12 18:32:08 +04:00
|
|
|
{"input", io_input},
|
|
|
|
{"lines", io_lines},
|
2002-04-05 00:24:56 +04:00
|
|
|
{"open", io_open},
|
2005-07-12 18:32:08 +04:00
|
|
|
{"output", io_output},
|
2005-06-06 17:30:52 +04:00
|
|
|
{"popen", io_popen},
|
2002-04-05 00:24:56 +04:00
|
|
|
{"read", io_read},
|
|
|
|
{"tmpfile", io_tmpfile},
|
2002-12-04 18:27:17 +03:00
|
|
|
{"type", io_type},
|
2002-04-05 00:24:56 +04:00
|
|
|
{"write", io_write},
|
2002-03-20 15:54:08 +03:00
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
2002-04-05 00:24:56 +04:00
|
|
|
|
2011-06-21 17:43:48 +04:00
|
|
|
/*
|
|
|
|
** methods for file handles
|
|
|
|
*/
|
2005-08-26 21:36:32 +04:00
|
|
|
static const luaL_Reg flib[] = {
|
2005-07-12 18:32:08 +04:00
|
|
|
{"close", io_close},
|
2002-04-05 00:24:56 +04:00
|
|
|
{"flush", f_flush},
|
2002-09-18 00:35:54 +04:00
|
|
|
{"lines", f_lines},
|
2005-07-12 18:32:08 +04:00
|
|
|
{"read", f_read},
|
2002-04-05 00:24:56 +04:00
|
|
|
{"seek", f_seek},
|
2003-05-14 23:03:43 +04:00
|
|
|
{"setvbuf", f_setvbuf},
|
2002-04-05 00:24:56 +04:00
|
|
|
{"write", f_write},
|
2011-06-21 17:43:48 +04:00
|
|
|
{"__gc", f_gc},
|
|
|
|
{"__tostring", f_tostring},
|
2002-04-05 00:24:56 +04:00
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static void createmeta (lua_State *L) {
|
2004-05-28 22:35:05 +04:00
|
|
|
luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */
|
2003-10-10 16:57:55 +04:00
|
|
|
lua_pushvalue(L, -1); /* push metatable */
|
|
|
|
lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
|
2010-07-28 19:51:59 +04:00
|
|
|
luaL_setfuncs(L, flib, 0); /* add file methods to new metatable */
|
2010-03-18 00:37:37 +03:00
|
|
|
lua_pop(L, 1); /* pop new metatable */
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|
|
|
|
|
1996-02-09 22:02:30 +03:00
|
|
|
|
2011-06-21 17:43:48 +04:00
|
|
|
/*
|
|
|
|
** function to (not) close the standard files stdin, stdout, and stderr
|
|
|
|
*/
|
|
|
|
static int io_noclose (lua_State *L) {
|
|
|
|
LStream *p = tolstream(L);
|
|
|
|
p->closef = &io_noclose; /* keep file opened */
|
|
|
|
lua_pushnil(L);
|
|
|
|
lua_pushliteral(L, "cannot close standard file");
|
|
|
|
return 2;
|
2007-04-20 00:22:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-21 17:43:48 +04:00
|
|
|
static void createstdfile (lua_State *L, FILE *f, const char *k,
|
|
|
|
const char *fname) {
|
|
|
|
LStream *p = newprefile(L);
|
|
|
|
p->f = f;
|
|
|
|
p->closef = &io_noclose;
|
|
|
|
if (k != NULL) {
|
|
|
|
lua_pushvalue(L, -1);
|
|
|
|
lua_setfield(L, LUA_REGISTRYINDEX, k); /* add file to registry */
|
|
|
|
}
|
|
|
|
lua_setfield(L, -2, fname); /* add file to module */
|
1994-08-12 03:11:57 +04:00
|
|
|
}
|
|
|
|
|
2002-03-20 15:54:08 +03:00
|
|
|
|
2009-11-24 15:05:44 +03:00
|
|
|
LUAMOD_API int luaopen_io (lua_State *L) {
|
2011-06-21 17:43:48 +04:00
|
|
|
luaL_newlib(L, iolib); /* new module */
|
2003-03-18 15:25:32 +03:00
|
|
|
createmeta(L);
|
2005-06-06 17:30:52 +04:00
|
|
|
/* create (and set) default files */
|
|
|
|
createstdfile(L, stdin, IO_INPUT, "stdin");
|
|
|
|
createstdfile(L, stdout, IO_OUTPUT, "stdout");
|
2011-06-21 17:43:48 +04:00
|
|
|
createstdfile(L, stderr, NULL, "stderr");
|
2002-12-20 13:26:33 +03:00
|
|
|
return 1;
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|
1998-12-27 23:21:28 +03:00
|
|
|
|