annotate lib/lua/lua-5.2.2/liolib.c @ 2381:cb84b457527c

Feather fall check fixed
author zipi
date Sun, 22 Jun 2014 12:56:25 +0100
parents 41cc4dd3c122
children
rev   line source
1866
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
2 ** $Id: liolib.c,v 2.111 2013/03/21 13:57:27 roberto Exp $
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
3 ** Standard I/O (and system) library
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
4 ** See Copyright Notice in lua.h
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
5 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
6
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
7
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
8 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
9 ** POSIX idiosyncrasy!
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
10 ** This definition must come before the inclusion of 'stdio.h'; it
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
11 ** should not affect non-POSIX systems
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
12 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
13 #if !defined(_FILE_OFFSET_BITS)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
14 #define _FILE_OFFSET_BITS 64
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
15 #endif
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
16
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
17
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
18 #include <errno.h>
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
19 #include <stdio.h>
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
20 #include <stdlib.h>
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
21 #include <string.h>
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
22
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
23 #define liolib_c
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
24 #define LUA_LIB
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
25
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
26 #include "lua.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
27
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
28 #include "lauxlib.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
29 #include "lualib.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
30
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
31
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
32 #if !defined(lua_checkmode)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
33
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
34 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
35 ** Check whether 'mode' matches '[rwa]%+?b?'.
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
36 ** Change this macro to accept other modes for 'fopen' besides
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
37 ** the standard ones.
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
38 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
39 #define lua_checkmode(mode) \
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
40 (*mode != '\0' && strchr("rwa", *(mode++)) != NULL && \
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
41 (*mode != '+' || ++mode) && /* skip if char is '+' */ \
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
42 (*mode != 'b' || ++mode) && /* skip if char is 'b' */ \
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
43 (*mode == '\0'))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
44
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
45 #endif
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
46
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
47 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
48 ** {======================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
49 ** lua_popen spawns a new process connected to the current
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
50 ** one through the file streams.
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
51 ** =======================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
52 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
53
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
54 #if !defined(lua_popen) /* { */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
55
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
56 #if defined(LUA_USE_POPEN) /* { */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
57
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
58 #define lua_popen(L,c,m) ((void)L, fflush(NULL), popen(c,m))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
59 #define lua_pclose(L,file) ((void)L, pclose(file))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
60
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
61 #elif defined(LUA_WIN) /* }{ */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
62
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
63 #define lua_popen(L,c,m) ((void)L, _popen(c,m))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
64 #define lua_pclose(L,file) ((void)L, _pclose(file))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
65
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
66
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
67 #else /* }{ */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
68
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
69 #define lua_popen(L,c,m) ((void)((void)c, m), \
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
70 luaL_error(L, LUA_QL("popen") " not supported"), (FILE*)0)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
71 #define lua_pclose(L,file) ((void)((void)L, file), -1)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
72
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
73
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
74 #endif /* } */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
75
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
76 #endif /* } */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
77
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
78 /* }====================================================== */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
79
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
80
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
81 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
82 ** {======================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
83 ** lua_fseek/lua_ftell: configuration for longer offsets
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
84 ** =======================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
85 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
86
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
87 #if !defined(lua_fseek) /* { */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
88
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
89 #if defined(LUA_USE_POSIX)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
90
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
91 #define l_fseek(f,o,w) fseeko(f,o,w)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
92 #define l_ftell(f) ftello(f)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
93 #define l_seeknum off_t
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
94
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
95 #elif defined(LUA_WIN) && !defined(_CRTIMP_TYPEINFO) \
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
96 && defined(_MSC_VER) && (_MSC_VER >= 1400)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
97 /* Windows (but not DDK) and Visual C++ 2005 or higher */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
98
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
99 #define l_fseek(f,o,w) _fseeki64(f,o,w)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
100 #define l_ftell(f) _ftelli64(f)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
101 #define l_seeknum __int64
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
102
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
103 #else
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
104
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
105 #define l_fseek(f,o,w) fseek(f,o,w)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
106 #define l_ftell(f) ftell(f)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
107 #define l_seeknum long
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
108
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
109 #endif
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
110
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
111 #endif /* } */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
112
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
113 /* }====================================================== */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
114
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
115
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
116 #define IO_PREFIX "_IO_"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
117 #define IO_INPUT (IO_PREFIX "input")
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
118 #define IO_OUTPUT (IO_PREFIX "output")
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
119
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
120
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
121 typedef luaL_Stream LStream;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
122
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
123
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
124 #define tolstream(L) ((LStream *)luaL_checkudata(L, 1, LUA_FILEHANDLE))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
125
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
126 #define isclosed(p) ((p)->closef == NULL)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
127
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
128
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
129 static int io_type (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
130 LStream *p;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
131 luaL_checkany(L, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
132 p = (LStream *)luaL_testudata(L, 1, LUA_FILEHANDLE);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
133 if (p == NULL)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
134 lua_pushnil(L); /* not a file */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
135 else if (isclosed(p))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
136 lua_pushliteral(L, "closed file");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
137 else
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
138 lua_pushliteral(L, "file");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
139 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
140 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
141
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
142
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
143 static int f_tostring (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
144 LStream *p = tolstream(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
145 if (isclosed(p))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
146 lua_pushliteral(L, "file (closed)");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
147 else
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
148 lua_pushfstring(L, "file (%p)", p->f);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
149 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
150 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
151
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
152
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
153 static FILE *tofile (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
154 LStream *p = tolstream(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
155 if (isclosed(p))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
156 luaL_error(L, "attempt to use a closed file");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
157 lua_assert(p->f);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
158 return p->f;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
159 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
160
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
161
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
162 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
163 ** When creating file handles, always creates a `closed' file handle
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
164 ** before opening the actual file; so, if there is a memory error, the
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
165 ** file is not left opened.
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
166 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
167 static LStream *newprefile (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
168 LStream *p = (LStream *)lua_newuserdata(L, sizeof(LStream));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
169 p->closef = NULL; /* mark file handle as 'closed' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
170 luaL_setmetatable(L, LUA_FILEHANDLE);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
171 return p;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
172 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
173
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
174
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
175 static int aux_close (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
176 LStream *p = tolstream(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
177 lua_CFunction cf = p->closef;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
178 p->closef = NULL; /* mark stream as closed */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
179 return (*cf)(L); /* close it */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
180 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
181
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
182
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
183 static int io_close (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
184 if (lua_isnone(L, 1)) /* no argument? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
185 lua_getfield(L, LUA_REGISTRYINDEX, IO_OUTPUT); /* use standard output */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
186 tofile(L); /* make sure argument is an open stream */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
187 return aux_close(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
188 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
189
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
190
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
191 static int f_gc (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
192 LStream *p = tolstream(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
193 if (!isclosed(p) && p->f != NULL)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
194 aux_close(L); /* ignore closed and incompletely open files */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
195 return 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
196 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
197
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
198
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
199 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
200 ** function to close regular files
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
201 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
202 static int io_fclose (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
203 LStream *p = tolstream(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
204 int res = fclose(p->f);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
205 return luaL_fileresult(L, (res == 0), NULL);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
206 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
207
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
208
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
209 static LStream *newfile (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
210 LStream *p = newprefile(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
211 p->f = NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
212 p->closef = &io_fclose;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
213 return p;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
214 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
215
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
216
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
217 static void opencheck (lua_State *L, const char *fname, const char *mode) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
218 LStream *p = newfile(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
219 p->f = fopen(fname, mode);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
220 if (p->f == NULL)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
221 luaL_error(L, "cannot open file " LUA_QS " (%s)", fname, strerror(errno));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
222 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
223
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
224
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
225 static int io_open (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
226 const char *filename = luaL_checkstring(L, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
227 const char *mode = luaL_optstring(L, 2, "r");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
228 LStream *p = newfile(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
229 const char *md = mode; /* to traverse/check mode */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
230 luaL_argcheck(L, lua_checkmode(md), 2, "invalid mode");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
231 p->f = fopen(filename, mode);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
232 return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
233 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
234
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
235
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
236 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
237 ** function to close 'popen' files
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
238 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
239 static int io_pclose (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
240 LStream *p = tolstream(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
241 return luaL_execresult(L, lua_pclose(L, p->f));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
242 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
243
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
244
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
245 static int io_popen (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
246 const char *filename = luaL_checkstring(L, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
247 const char *mode = luaL_optstring(L, 2, "r");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
248 LStream *p = newprefile(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
249 p->f = lua_popen(L, filename, mode);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
250 p->closef = &io_pclose;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
251 return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
252 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
253
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
254
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
255 static int io_tmpfile (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
256 LStream *p = newfile(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
257 p->f = tmpfile();
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
258 return (p->f == NULL) ? luaL_fileresult(L, 0, NULL) : 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
259 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
260
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
261
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
262 static FILE *getiofile (lua_State *L, const char *findex) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
263 LStream *p;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
264 lua_getfield(L, LUA_REGISTRYINDEX, findex);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
265 p = (LStream *)lua_touserdata(L, -1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
266 if (isclosed(p))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
267 luaL_error(L, "standard %s file is closed", findex + strlen(IO_PREFIX));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
268 return p->f;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
269 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
270
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
271
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
272 static int g_iofile (lua_State *L, const char *f, const char *mode) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
273 if (!lua_isnoneornil(L, 1)) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
274 const char *filename = lua_tostring(L, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
275 if (filename)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
276 opencheck(L, filename, mode);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
277 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
278 tofile(L); /* check that it's a valid file handle */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
279 lua_pushvalue(L, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
280 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
281 lua_setfield(L, LUA_REGISTRYINDEX, f);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
282 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
283 /* return current value */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
284 lua_getfield(L, LUA_REGISTRYINDEX, f);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
285 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
286 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
287
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
288
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
289 static int io_input (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
290 return g_iofile(L, IO_INPUT, "r");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
291 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
292
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
293
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
294 static int io_output (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
295 return g_iofile(L, IO_OUTPUT, "w");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
296 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
297
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
298
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
299 static int io_readline (lua_State *L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
300
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
301
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
302 static void aux_lines (lua_State *L, int toclose) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
303 int i;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
304 int n = lua_gettop(L) - 1; /* number of arguments to read */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
305 /* ensure that arguments will fit here and into 'io_readline' stack */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
306 luaL_argcheck(L, n <= LUA_MINSTACK - 3, LUA_MINSTACK - 3, "too many options");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
307 lua_pushvalue(L, 1); /* file handle */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
308 lua_pushinteger(L, n); /* number of arguments to read */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
309 lua_pushboolean(L, toclose); /* close/not close file when finished */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
310 for (i = 1; i <= n; i++) lua_pushvalue(L, i + 1); /* copy arguments */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
311 lua_pushcclosure(L, io_readline, 3 + n);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
312 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
313
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
314
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
315 static int f_lines (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
316 tofile(L); /* check that it's a valid file handle */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
317 aux_lines(L, 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
318 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
319 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
320
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
321
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
322 static int io_lines (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
323 int toclose;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
324 if (lua_isnone(L, 1)) lua_pushnil(L); /* at least one argument */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
325 if (lua_isnil(L, 1)) { /* no file name? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
326 lua_getfield(L, LUA_REGISTRYINDEX, IO_INPUT); /* get default input */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
327 lua_replace(L, 1); /* put it at index 1 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
328 tofile(L); /* check that it's a valid file handle */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
329 toclose = 0; /* do not close it after iteration */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
330 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
331 else { /* open a new file */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
332 const char *filename = luaL_checkstring(L, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
333 opencheck(L, filename, "r");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
334 lua_replace(L, 1); /* put file at index 1 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
335 toclose = 1; /* close it after iteration */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
336 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
337 aux_lines(L, toclose);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
338 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
339 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
340
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
341
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
342 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
343 ** {======================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
344 ** READ
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
345 ** =======================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
346 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
347
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
348
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
349 static int read_number (lua_State *L, FILE *f) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
350 lua_Number d;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
351 if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
352 lua_pushnumber(L, d);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
353 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
354 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
355 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
356 lua_pushnil(L); /* "result" to be removed */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
357 return 0; /* read fails */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
358 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
359 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
360
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
361
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
362 static int test_eof (lua_State *L, FILE *f) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
363 int c = getc(f);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
364 ungetc(c, f);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
365 lua_pushlstring(L, NULL, 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
366 return (c != EOF);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
367 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
368
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
369
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
370 static int read_line (lua_State *L, FILE *f, int chop) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
371 luaL_Buffer b;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
372 luaL_buffinit(L, &b);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
373 for (;;) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
374 size_t l;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
375 char *p = luaL_prepbuffer(&b);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
376 if (fgets(p, LUAL_BUFFERSIZE, f) == NULL) { /* eof? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
377 luaL_pushresult(&b); /* close buffer */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
378 return (lua_rawlen(L, -1) > 0); /* check whether read something */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
379 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
380 l = strlen(p);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
381 if (l == 0 || p[l-1] != '\n')
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
382 luaL_addsize(&b, l);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
383 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
384 luaL_addsize(&b, l - chop); /* chop 'eol' if needed */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
385 luaL_pushresult(&b); /* close buffer */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
386 return 1; /* read at least an `eol' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
387 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
388 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
389 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
390
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
391
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
392 #define MAX_SIZE_T (~(size_t)0)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
393
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
394 static void read_all (lua_State *L, FILE *f) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
395 size_t rlen = LUAL_BUFFERSIZE; /* how much to read in each cycle */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
396 luaL_Buffer b;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
397 luaL_buffinit(L, &b);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
398 for (;;) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
399 char *p = luaL_prepbuffsize(&b, rlen);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
400 size_t nr = fread(p, sizeof(char), rlen, f);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
401 luaL_addsize(&b, nr);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
402 if (nr < rlen) break; /* eof? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
403 else if (rlen <= (MAX_SIZE_T / 4)) /* avoid buffers too large */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
404 rlen *= 2; /* double buffer size at each iteration */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
405 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
406 luaL_pushresult(&b); /* close buffer */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
407 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
408
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
409
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
410 static int read_chars (lua_State *L, FILE *f, size_t n) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
411 size_t nr; /* number of chars actually read */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
412 char *p;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
413 luaL_Buffer b;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
414 luaL_buffinit(L, &b);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
415 p = luaL_prepbuffsize(&b, n); /* prepare buffer to read whole block */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
416 nr = fread(p, sizeof(char), n, f); /* try to read 'n' chars */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
417 luaL_addsize(&b, nr);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
418 luaL_pushresult(&b); /* close buffer */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
419 return (nr > 0); /* true iff read something */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
420 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
421
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
422
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
423 static int g_read (lua_State *L, FILE *f, int first) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
424 int nargs = lua_gettop(L) - 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
425 int success;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
426 int n;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
427 clearerr(f);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
428 if (nargs == 0) { /* no arguments? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
429 success = read_line(L, f, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
430 n = first+1; /* to return 1 result */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
431 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
432 else { /* ensure stack space for all results and for auxlib's buffer */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
433 luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
434 success = 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
435 for (n = first; nargs-- && success; n++) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
436 if (lua_type(L, n) == LUA_TNUMBER) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
437 size_t l = (size_t)lua_tointeger(L, n);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
438 success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
439 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
440 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
441 const char *p = lua_tostring(L, n);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
442 luaL_argcheck(L, p && p[0] == '*', n, "invalid option");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
443 switch (p[1]) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
444 case 'n': /* number */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
445 success = read_number(L, f);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
446 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
447 case 'l': /* line */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
448 success = read_line(L, f, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
449 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
450 case 'L': /* line with end-of-line */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
451 success = read_line(L, f, 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
452 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
453 case 'a': /* file */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
454 read_all(L, f); /* read entire file */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
455 success = 1; /* always success */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
456 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
457 default:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
458 return luaL_argerror(L, n, "invalid format");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
459 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
460 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
461 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
462 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
463 if (ferror(f))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
464 return luaL_fileresult(L, 0, NULL);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
465 if (!success) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
466 lua_pop(L, 1); /* remove last result */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
467 lua_pushnil(L); /* push nil instead */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
468 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
469 return n - first;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
470 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
471
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
472
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
473 static int io_read (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
474 return g_read(L, getiofile(L, IO_INPUT), 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
475 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
476
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
477
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
478 static int f_read (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
479 return g_read(L, tofile(L), 2);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
480 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
481
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
482
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
483 static int io_readline (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
484 LStream *p = (LStream *)lua_touserdata(L, lua_upvalueindex(1));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
485 int i;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
486 int n = (int)lua_tointeger(L, lua_upvalueindex(2));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
487 if (isclosed(p)) /* file is already closed? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
488 return luaL_error(L, "file is already closed");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
489 lua_settop(L , 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
490 for (i = 1; i <= n; i++) /* push arguments to 'g_read' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
491 lua_pushvalue(L, lua_upvalueindex(3 + i));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
492 n = g_read(L, p->f, 2); /* 'n' is number of results */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
493 lua_assert(n > 0); /* should return at least a nil */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
494 if (!lua_isnil(L, -n)) /* read at least one value? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
495 return n; /* return them */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
496 else { /* first result is nil: EOF or error */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
497 if (n > 1) { /* is there error information? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
498 /* 2nd result is error message */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
499 return luaL_error(L, "%s", lua_tostring(L, -n + 1));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
500 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
501 if (lua_toboolean(L, lua_upvalueindex(3))) { /* generator created file? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
502 lua_settop(L, 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
503 lua_pushvalue(L, lua_upvalueindex(1));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
504 aux_close(L); /* close it */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
505 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
506 return 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
507 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
508 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
509
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
510 /* }====================================================== */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
511
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
512
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
513 static int g_write (lua_State *L, FILE *f, int arg) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
514 int nargs = lua_gettop(L) - arg;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
515 int status = 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
516 for (; nargs--; arg++) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
517 if (lua_type(L, arg) == LUA_TNUMBER) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
518 /* optimization: could be done exactly as for strings */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
519 status = status &&
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
520 fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
521 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
522 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
523 size_t l;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
524 const char *s = luaL_checklstring(L, arg, &l);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
525 status = status && (fwrite(s, sizeof(char), l, f) == l);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
526 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
527 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
528 if (status) return 1; /* file handle already on stack top */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
529 else return luaL_fileresult(L, status, NULL);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
530 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
531
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
532
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
533 static int io_write (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
534 return g_write(L, getiofile(L, IO_OUTPUT), 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
535 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
536
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
537
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
538 static int f_write (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
539 FILE *f = tofile(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
540 lua_pushvalue(L, 1); /* push file at the stack top (to be returned) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
541 return g_write(L, f, 2);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
542 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
543
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
544
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
545 static int f_seek (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
546 static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
547 static const char *const modenames[] = {"set", "cur", "end", NULL};
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
548 FILE *f = tofile(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
549 int op = luaL_checkoption(L, 2, "cur", modenames);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
550 lua_Number p3 = luaL_optnumber(L, 3, 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
551 l_seeknum offset = (l_seeknum)p3;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
552 luaL_argcheck(L, (lua_Number)offset == p3, 3,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
553 "not an integer in proper range");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
554 op = l_fseek(f, offset, mode[op]);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
555 if (op)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
556 return luaL_fileresult(L, 0, NULL); /* error */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
557 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
558 lua_pushnumber(L, (lua_Number)l_ftell(f));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
559 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
560 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
561 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
562
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
563
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
564 static int f_setvbuf (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
565 static const int mode[] = {_IONBF, _IOFBF, _IOLBF};
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
566 static const char *const modenames[] = {"no", "full", "line", NULL};
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
567 FILE *f = tofile(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
568 int op = luaL_checkoption(L, 2, NULL, modenames);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
569 lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
570 int res = setvbuf(f, NULL, mode[op], sz);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
571 return luaL_fileresult(L, res == 0, NULL);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
572 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
573
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
574
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
575
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
576 static int io_flush (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
577 return luaL_fileresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
578 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
579
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
580
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
581 static int f_flush (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
582 return luaL_fileresult(L, fflush(tofile(L)) == 0, NULL);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
583 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
584
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
585
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
586 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
587 ** functions for 'io' library
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
588 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
589 static const luaL_Reg iolib[] = {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
590 {"close", io_close},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
591 {"flush", io_flush},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
592 {"input", io_input},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
593 {"lines", io_lines},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
594 {"open", io_open},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
595 {"output", io_output},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
596 {"popen", io_popen},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
597 {"read", io_read},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
598 {"tmpfile", io_tmpfile},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
599 {"type", io_type},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
600 {"write", io_write},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
601 {NULL, NULL}
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
602 };
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
603
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
604
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
605 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
606 ** methods for file handles
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
607 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
608 static const luaL_Reg flib[] = {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
609 {"close", io_close},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
610 {"flush", f_flush},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
611 {"lines", f_lines},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
612 {"read", f_read},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
613 {"seek", f_seek},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
614 {"setvbuf", f_setvbuf},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
615 {"write", f_write},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
616 {"__gc", f_gc},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
617 {"__tostring", f_tostring},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
618 {NULL, NULL}
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
619 };
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
620
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
621
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
622 static void createmeta (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
623 luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
624 lua_pushvalue(L, -1); /* push metatable */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
625 lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
626 luaL_setfuncs(L, flib, 0); /* add file methods to new metatable */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
627 lua_pop(L, 1); /* pop new metatable */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
628 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
629
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
630
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
631 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
632 ** function to (not) close the standard files stdin, stdout, and stderr
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
633 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
634 static int io_noclose (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
635 LStream *p = tolstream(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
636 p->closef = &io_noclose; /* keep file opened */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
637 lua_pushnil(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
638 lua_pushliteral(L, "cannot close standard file");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
639 return 2;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
640 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
641
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
642
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
643 static void createstdfile (lua_State *L, FILE *f, const char *k,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
644 const char *fname) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
645 LStream *p = newprefile(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
646 p->f = f;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
647 p->closef = &io_noclose;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
648 if (k != NULL) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
649 lua_pushvalue(L, -1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
650 lua_setfield(L, LUA_REGISTRYINDEX, k); /* add file to registry */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
651 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
652 lua_setfield(L, -2, fname); /* add file to module */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
653 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
654
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
655
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
656 LUAMOD_API int luaopen_io (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
657 luaL_newlib(L, iolib); /* new module */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
658 createmeta(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
659 /* create (and set) default files */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
660 createstdfile(L, stdin, IO_INPUT, "stdin");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
661 createstdfile(L, stdout, IO_OUTPUT, "stdout");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
662 createstdfile(L, stderr, NULL, "stderr");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
663 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
664 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
665