/src/testdir/build/lua-master/source/liolib.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | ** $Id: liolib.c $ |
3 | | ** Standard I/O (and system) library |
4 | | ** See Copyright Notice in lua.h |
5 | | */ |
6 | | |
7 | | #define liolib_c |
8 | | #define LUA_LIB |
9 | | |
10 | | #include "lprefix.h" |
11 | | |
12 | | |
13 | | #include <ctype.h> |
14 | | #include <errno.h> |
15 | | #include <locale.h> |
16 | | #include <stdio.h> |
17 | | #include <stdlib.h> |
18 | | #include <string.h> |
19 | | |
20 | | #include "lua.h" |
21 | | |
22 | | #include "lauxlib.h" |
23 | | #include "lualib.h" |
24 | | #include "llimits.h" |
25 | | |
26 | | |
27 | | /* |
28 | | ** Change this macro to accept other modes for 'fopen' besides |
29 | | ** the standard ones. |
30 | | */ |
31 | | #if !defined(l_checkmode) |
32 | | |
33 | | /* accepted extensions to 'mode' in 'fopen' */ |
34 | | #if !defined(L_MODEEXT) |
35 | 1.29M | #define L_MODEEXT "b" |
36 | | #endif |
37 | | |
38 | | /* Check whether 'mode' matches '[rwa]%+?[L_MODEEXT]*' */ |
39 | 1.29M | static int l_checkmode (const char *mode) { |
40 | 1.29M | return (*mode != '\0' && strchr("rwa", *(mode++)) != NULL && |
41 | 1.29M | (*mode != '+' || ((void)(++mode), 1)) && /* skip if char is '+' */ |
42 | 1.29M | (strspn(mode, L_MODEEXT) == strlen(mode))); /* check extensions */ |
43 | 1.29M | } |
44 | | |
45 | | #endif |
46 | | |
47 | | /* |
48 | | ** {====================================================== |
49 | | ** l_popen spawns a new process connected to the current |
50 | | ** one through the file streams. |
51 | | ** ======================================================= |
52 | | */ |
53 | | |
54 | | #if !defined(l_popen) /* { */ |
55 | | |
56 | | #if defined(LUA_USE_POSIX) /* { */ |
57 | | |
58 | 207 | #define l_popen(L,c,m) (fflush(NULL), popen(c,m)) |
59 | 206 | #define l_pclose(L,file) (pclose(file)) |
60 | | |
61 | | #elif defined(LUA_USE_WINDOWS) /* }{ */ |
62 | | |
63 | | #define l_popen(L,c,m) (_popen(c,m)) |
64 | | #define l_pclose(L,file) (_pclose(file)) |
65 | | |
66 | | #if !defined(l_checkmodep) |
67 | | /* Windows accepts "[rw][bt]?" as valid modes */ |
68 | | #define l_checkmodep(m) ((m[0] == 'r' || m[0] == 'w') && \ |
69 | | (m[1] == '\0' || ((m[1] == 'b' || m[1] == 't') && m[2] == '\0'))) |
70 | | #endif |
71 | | |
72 | | #else /* }{ */ |
73 | | |
74 | | /* ISO C definitions */ |
75 | | #define l_popen(L,c,m) \ |
76 | | ((void)c, (void)m, \ |
77 | | luaL_error(L, "'popen' not supported"), \ |
78 | | (FILE*)0) |
79 | | #define l_pclose(L,file) ((void)L, (void)file, -1) |
80 | | |
81 | | #endif /* } */ |
82 | | |
83 | | #endif /* } */ |
84 | | |
85 | | |
86 | | #if !defined(l_checkmodep) |
87 | | /* By default, Lua accepts only "r" or "w" as valid modes */ |
88 | | #define l_checkmodep(m) ((m[0] == 'r' || m[0] == 'w') && m[1] == '\0') |
89 | | #endif |
90 | | |
91 | | /* }====================================================== */ |
92 | | |
93 | | |
94 | | #if !defined(l_getc) /* { */ |
95 | | |
96 | | #if defined(LUA_USE_POSIX) |
97 | 21.4M | #define l_getc(f) getc_unlocked(f) |
98 | 119k | #define l_lockfile(f) flockfile(f) |
99 | 119k | #define l_unlockfile(f) funlockfile(f) |
100 | | #else |
101 | | #define l_getc(f) getc(f) |
102 | | #define l_lockfile(f) ((void)0) |
103 | | #define l_unlockfile(f) ((void)0) |
104 | | #endif |
105 | | |
106 | | #endif /* } */ |
107 | | |
108 | | |
109 | | /* |
110 | | ** {====================================================== |
111 | | ** l_fseek: configuration for longer offsets |
112 | | ** ======================================================= |
113 | | */ |
114 | | |
115 | | #if !defined(l_fseek) /* { */ |
116 | | |
117 | | #if defined(LUA_USE_POSIX) /* { */ |
118 | | |
119 | | #include <sys/types.h> |
120 | | |
121 | 28.8k | #define l_fseek(f,o,w) fseeko(f,o,w) |
122 | 28.6k | #define l_ftell(f) ftello(f) |
123 | 28.8k | #define l_seeknum off_t |
124 | | |
125 | | #elif defined(LUA_USE_WINDOWS) && !defined(_CRTIMP_TYPEINFO) \ |
126 | | && defined(_MSC_VER) && (_MSC_VER >= 1400) /* }{ */ |
127 | | |
128 | | /* Windows (but not DDK) and Visual C++ 2005 or higher */ |
129 | | #define l_fseek(f,o,w) _fseeki64(f,o,w) |
130 | | #define l_ftell(f) _ftelli64(f) |
131 | | #define l_seeknum __int64 |
132 | | |
133 | | #else /* }{ */ |
134 | | |
135 | | /* ISO C definitions */ |
136 | | #define l_fseek(f,o,w) fseek(f,o,w) |
137 | | #define l_ftell(f) ftell(f) |
138 | | #define l_seeknum long |
139 | | |
140 | | #endif /* } */ |
141 | | |
142 | | #endif /* } */ |
143 | | |
144 | | /* }====================================================== */ |
145 | | |
146 | | |
147 | | |
148 | 17.0M | #define IO_PREFIX "_IO_" |
149 | 69 | #define IOPREF_LEN (sizeof(IO_PREFIX)/sizeof(char) - 1) |
150 | 737k | #define IO_INPUT (IO_PREFIX "input") |
151 | 16.2M | #define IO_OUTPUT (IO_PREFIX "output") |
152 | | |
153 | | |
154 | | typedef luaL_Stream LStream; |
155 | | |
156 | | |
157 | 7.19M | #define tolstream(L) ((LStream *)luaL_checkudata(L, 1, LUA_FILEHANDLE)) |
158 | | |
159 | 5.38M | #define isclosed(p) ((p)->closef == NULL) |
160 | | |
161 | | |
162 | 349k | static int io_type (lua_State *L) { |
163 | 349k | LStream *p; |
164 | 349k | luaL_checkany(L, 1); |
165 | 349k | p = (LStream *)luaL_testudata(L, 1, LUA_FILEHANDLE); |
166 | 349k | if (p == NULL) |
167 | 311k | luaL_pushfail(L); /* not a file */ |
168 | 38.3k | else if (isclosed(p)) |
169 | 79 | lua_pushliteral(L, "closed file"); |
170 | 38.2k | else |
171 | 38.2k | lua_pushliteral(L, "file"); |
172 | 349k | return 1; |
173 | 349k | } |
174 | | |
175 | | |
176 | 3.77k | static int f_tostring (lua_State *L) { |
177 | 3.77k | LStream *p = tolstream(L); |
178 | 3.77k | if (isclosed(p)) |
179 | 115 | lua_pushliteral(L, "file (closed)"); |
180 | 3.66k | else |
181 | 3.66k | lua_pushfstring(L, "file (%p)", p->f); |
182 | 3.77k | return 1; |
183 | 3.77k | } |
184 | | |
185 | | |
186 | 706k | static FILE *tofile (lua_State *L) { |
187 | 706k | LStream *p = tolstream(L); |
188 | 706k | if (l_unlikely(isclosed(p))) |
189 | 559 | luaL_error(L, "attempt to use a closed file"); |
190 | 706k | lua_assert(p->f); |
191 | 700k | return p->f; |
192 | 706k | } |
193 | | |
194 | | |
195 | | /* |
196 | | ** When creating file handles, always creates a 'closed' file handle |
197 | | ** before opening the actual file; so, if there is a memory error, the |
198 | | ** handle is in a consistent state. |
199 | | */ |
200 | 2.60M | static LStream *newprefile (lua_State *L) { |
201 | 2.60M | LStream *p = (LStream *)lua_newuserdatauv(L, sizeof(LStream), 0); |
202 | 2.60M | p->closef = NULL; /* mark file handle as 'closed' */ |
203 | 2.60M | luaL_setmetatable(L, LUA_FILEHANDLE); |
204 | 2.60M | return p; |
205 | 2.60M | } |
206 | | |
207 | | |
208 | | /* |
209 | | ** Calls the 'close' function from a file handle. The 'volatile' avoids |
210 | | ** a bug in some versions of the Clang compiler (e.g., clang 3.0 for |
211 | | ** 32 bits). |
212 | | */ |
213 | 1.94M | static int aux_close (lua_State *L) { |
214 | 1.94M | LStream *p = tolstream(L); |
215 | 1.94M | volatile lua_CFunction cf = p->closef; |
216 | 1.94M | p->closef = NULL; /* mark stream as closed */ |
217 | 1.94M | return (*cf)(L); /* close it */ |
218 | 1.94M | } |
219 | | |
220 | | |
221 | 218k | static int f_close (lua_State *L) { |
222 | 218k | tofile(L); /* make sure argument is an open stream */ |
223 | 218k | return aux_close(L); |
224 | 218k | } |
225 | | |
226 | | |
227 | 209k | static int io_close (lua_State *L) { |
228 | 209k | if (lua_isnone(L, 1)) /* no argument? */ |
229 | 204k | lua_getfield(L, LUA_REGISTRYINDEX, IO_OUTPUT); /* use default output */ |
230 | 209k | return f_close(L); |
231 | 209k | } |
232 | | |
233 | | |
234 | 2.60M | static int f_gc (lua_State *L) { |
235 | 2.60M | LStream *p = tolstream(L); |
236 | 2.60M | if (!isclosed(p) && p->f != NULL) |
237 | 1.71M | aux_close(L); /* ignore closed and incompletely open files */ |
238 | 2.60M | return 0; |
239 | 2.60M | } |
240 | | |
241 | | |
242 | | /* |
243 | | ** function to close regular files |
244 | | */ |
245 | 1.64M | static int io_fclose (lua_State *L) { |
246 | 1.64M | LStream *p = tolstream(L); |
247 | 1.64M | errno = 0; |
248 | 1.64M | return luaL_fileresult(L, (fclose(p->f) == 0), NULL); |
249 | 1.64M | } |
250 | | |
251 | | |
252 | 2.51M | static LStream *newfile (lua_State *L) { |
253 | 2.51M | LStream *p = newprefile(L); |
254 | 2.51M | p->f = NULL; |
255 | 2.51M | p->closef = &io_fclose; |
256 | 2.51M | return p; |
257 | 2.51M | } |
258 | | |
259 | | |
260 | 1.21M | static void opencheck (lua_State *L, const char *fname, const char *mode) { |
261 | 1.21M | LStream *p = newfile(L); |
262 | 1.21M | p->f = fopen(fname, mode); |
263 | 1.21M | if (l_unlikely(p->f == NULL)) |
264 | 128 | luaL_error(L, "cannot open file '%s' (%s)", fname, strerror(errno)); |
265 | 1.21M | } |
266 | | |
267 | | |
268 | 1.29M | static int io_open (lua_State *L) { |
269 | 1.29M | const char *filename = luaL_checkstring(L, 1); |
270 | 1.29M | const char *mode = luaL_optstring(L, 2, "r"); |
271 | 1.29M | LStream *p = newfile(L); |
272 | 1.29M | const char *md = mode; /* to traverse/check mode */ |
273 | 1.29M | luaL_argcheck(L, l_checkmode(md), 2, "invalid mode"); |
274 | 1.29M | errno = 0; |
275 | 1.29M | p->f = fopen(filename, mode); |
276 | 1.29M | return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; |
277 | 1.29M | } |
278 | | |
279 | | |
280 | | /* |
281 | | ** function to close 'popen' files |
282 | | */ |
283 | 206 | static int io_pclose (lua_State *L) { |
284 | 206 | LStream *p = tolstream(L); |
285 | 206 | errno = 0; |
286 | 206 | return luaL_execresult(L, l_pclose(L, p->f)); |
287 | 206 | } |
288 | | |
289 | | |
290 | 207 | static int io_popen (lua_State *L) { |
291 | 207 | const char *filename = luaL_checkstring(L, 1); |
292 | 207 | const char *mode = luaL_optstring(L, 2, "r"); |
293 | 207 | LStream *p = newprefile(L); |
294 | 207 | luaL_argcheck(L, l_checkmodep(mode), 2, "invalid mode"); |
295 | 207 | errno = 0; |
296 | 207 | p->f = l_popen(L, filename, mode); |
297 | 207 | p->closef = &io_pclose; |
298 | 207 | return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; |
299 | 207 | } |
300 | | |
301 | | |
302 | 427 | static int io_tmpfile (lua_State *L) { |
303 | 427 | LStream *p = newfile(L); |
304 | 427 | errno = 0; |
305 | 427 | p->f = tmpfile(); |
306 | 427 | return (p->f == NULL) ? luaL_fileresult(L, 0, NULL) : 1; |
307 | 427 | } |
308 | | |
309 | | |
310 | 15.3M | static FILE *getiofile (lua_State *L, const char *findex) { |
311 | 15.3M | LStream *p; |
312 | 15.3M | lua_getfield(L, LUA_REGISTRYINDEX, findex); |
313 | 15.3M | p = (LStream *)lua_touserdata(L, -1); |
314 | 15.3M | if (l_unlikely(isclosed(p))) |
315 | 69 | luaL_error(L, "default %s file is closed", findex + IOPREF_LEN); |
316 | 15.3M | return p->f; |
317 | 15.3M | } |
318 | | |
319 | | |
320 | 1.29M | static int g_iofile (lua_State *L, const char *f, const char *mode) { |
321 | 1.29M | if (!lua_isnoneornil(L, 1)) { |
322 | 1.12M | const char *filename = lua_tostring(L, 1); |
323 | 1.12M | if (filename) |
324 | 810k | opencheck(L, filename, mode); |
325 | 314k | else { |
326 | 314k | tofile(L); /* check that it's a valid file handle */ |
327 | 314k | lua_pushvalue(L, 1); |
328 | 314k | } |
329 | 1.12M | lua_setfield(L, LUA_REGISTRYINDEX, f); |
330 | 1.12M | } |
331 | | /* return current value */ |
332 | 1.29M | lua_getfield(L, LUA_REGISTRYINDEX, f); |
333 | 1.29M | return 1; |
334 | 1.29M | } |
335 | | |
336 | | |
337 | 623k | static int io_input (lua_State *L) { |
338 | 623k | return g_iofile(L, IO_INPUT, "r"); |
339 | 623k | } |
340 | | |
341 | | |
342 | 671k | static int io_output (lua_State *L) { |
343 | 671k | return g_iofile(L, IO_OUTPUT, "w"); |
344 | 671k | } |
345 | | |
346 | | |
347 | | static int io_readline (lua_State *L); |
348 | | |
349 | | |
350 | | /* |
351 | | ** maximum number of arguments to 'f:lines'/'io.lines' (it + 3 must fit |
352 | | ** in the limit for upvalues of a closure) |
353 | | */ |
354 | | #define MAXARGLINE 250 |
355 | | |
356 | | /* |
357 | | ** Auxiliary function to create the iteration function for 'lines'. |
358 | | ** The iteration function is a closure over 'io_readline', with |
359 | | ** the following upvalues: |
360 | | ** 1) The file being read (first value in the stack) |
361 | | ** 2) the number of arguments to read |
362 | | ** 3) a boolean, true iff file has to be closed when finished ('toclose') |
363 | | ** *) a variable number of format arguments (rest of the stack) |
364 | | */ |
365 | 486k | static void aux_lines (lua_State *L, int toclose) { |
366 | 486k | int n = lua_gettop(L) - 1; /* number of arguments to read */ |
367 | 486k | luaL_argcheck(L, n <= MAXARGLINE, MAXARGLINE + 2, "too many arguments"); |
368 | 486k | lua_pushvalue(L, 1); /* file */ |
369 | 486k | lua_pushinteger(L, n); /* number of arguments to read */ |
370 | 486k | lua_pushboolean(L, toclose); /* close/not close file when finished */ |
371 | 486k | lua_rotate(L, 2, 3); /* move the three values to their positions */ |
372 | 486k | lua_pushcclosure(L, io_readline, 3 + n); |
373 | 486k | } |
374 | | |
375 | | |
376 | 373 | static int f_lines (lua_State *L) { |
377 | 373 | tofile(L); /* check that it's a valid file handle */ |
378 | 373 | aux_lines(L, 0); |
379 | 373 | return 1; |
380 | 373 | } |
381 | | |
382 | | |
383 | | /* |
384 | | ** Return an iteration function for 'io.lines'. If file has to be |
385 | | ** closed, also returns the file itself as a second result (to be |
386 | | ** closed as the state at the exit of a generic for). |
387 | | */ |
388 | 486k | static int io_lines (lua_State *L) { |
389 | 486k | int toclose; |
390 | 486k | if (lua_isnone(L, 1)) lua_pushnil(L); /* at least one argument */ |
391 | 486k | if (lua_isnil(L, 1)) { /* no file name? */ |
392 | 78.9k | lua_getfield(L, LUA_REGISTRYINDEX, IO_INPUT); /* get default input */ |
393 | 78.9k | lua_replace(L, 1); /* put it at index 1 */ |
394 | 78.9k | tofile(L); /* check that it's a valid file handle */ |
395 | 78.9k | toclose = 0; /* do not close it after iteration */ |
396 | 78.9k | } |
397 | 407k | else { /* open a new file */ |
398 | 407k | const char *filename = luaL_checkstring(L, 1); |
399 | 407k | opencheck(L, filename, "r"); |
400 | 407k | lua_replace(L, 1); /* put file at index 1 */ |
401 | 407k | toclose = 1; /* close it after iteration */ |
402 | 407k | } |
403 | 486k | aux_lines(L, toclose); /* push iteration function */ |
404 | 486k | if (toclose) { |
405 | 407k | lua_pushnil(L); /* state */ |
406 | 407k | lua_pushnil(L); /* control */ |
407 | 407k | lua_pushvalue(L, 1); /* file is the to-be-closed variable (4th result) */ |
408 | 407k | return 4; |
409 | 407k | } |
410 | 78.9k | else |
411 | 78.9k | return 1; |
412 | 486k | } |
413 | | |
414 | | |
415 | | /* |
416 | | ** {====================================================== |
417 | | ** READ |
418 | | ** ======================================================= |
419 | | */ |
420 | | |
421 | | |
422 | | /* maximum length of a numeral */ |
423 | | #if !defined (L_MAXLENNUM) |
424 | | #define L_MAXLENNUM 200 |
425 | | #endif |
426 | | |
427 | | |
428 | | /* auxiliary structure used by 'read_number' */ |
429 | | typedef struct { |
430 | | FILE *f; /* file being read */ |
431 | | int c; /* current character (look ahead) */ |
432 | | int n; /* number of elements in buffer 'buff' */ |
433 | | char buff[L_MAXLENNUM + 1]; /* +1 for ending '\0' */ |
434 | | } RN; |
435 | | |
436 | | |
437 | | /* |
438 | | ** Add current char to buffer (if not out of space) and read next one |
439 | | */ |
440 | 3.58k | static int nextc (RN *rn) { |
441 | 3.58k | if (l_unlikely(rn->n >= L_MAXLENNUM)) { /* buffer overflow? */ |
442 | 4 | rn->buff[0] = '\0'; /* invalidate result */ |
443 | 4 | return 0; /* fail */ |
444 | 4 | } |
445 | 3.58k | else { |
446 | 3.58k | rn->buff[rn->n++] = cast_char(rn->c); /* save current char */ |
447 | 3.58k | rn->c = l_getc(rn->f); /* read next one */ |
448 | 3.58k | return 1; |
449 | 3.58k | } |
450 | 3.58k | } |
451 | | |
452 | | |
453 | | /* |
454 | | ** Accept current char if it is in 'set' (of size 2) |
455 | | */ |
456 | 2.72k | static int test2 (RN *rn, const char *set) { |
457 | 2.72k | if (rn->c == set[0] || rn->c == set[1]) |
458 | 91 | return nextc(rn); |
459 | 2.63k | else return 0; |
460 | 2.72k | } |
461 | | |
462 | | |
463 | | /* |
464 | | ** Read a sequence of (hex)digits |
465 | | */ |
466 | 917 | static int readdigits (RN *rn, int hex) { |
467 | 917 | int count = 0; |
468 | 4.40k | while ((hex ? isxdigit(rn->c) : isdigit(rn->c)) && nextc(rn)) |
469 | 3.49k | count++; |
470 | 917 | return count; |
471 | 917 | } |
472 | | |
473 | | |
474 | | /* |
475 | | ** Read a number: first reads a valid prefix of a numeral into a buffer. |
476 | | ** Then it calls 'lua_stringtonumber' to check whether the format is |
477 | | ** correct and to convert it to a Lua number. |
478 | | */ |
479 | 862 | static int read_number (lua_State *L, FILE *f) { |
480 | 862 | RN rn; |
481 | 862 | int count = 0; |
482 | 862 | int hex = 0; |
483 | 862 | char decp[2]; |
484 | 862 | rn.f = f; rn.n = 0; |
485 | 862 | decp[0] = lua_getlocaledecpoint(); /* get decimal point from locale */ |
486 | 862 | decp[1] = '.'; /* always accept a dot */ |
487 | 862 | l_lockfile(rn.f); |
488 | 1.41k | do { rn.c = l_getc(rn.f); } while (isspace(rn.c)); /* skip spaces */ |
489 | 862 | test2(&rn, "-+"); /* optional sign */ |
490 | 862 | if (test2(&rn, "00")) { |
491 | 5 | if (test2(&rn, "xX")) hex = 1; /* numeral is hexadecimal */ |
492 | 5 | else count = 1; /* count initial '0' as a valid digit */ |
493 | 5 | } |
494 | 862 | count += readdigits(&rn, hex); /* integral part */ |
495 | 862 | if (test2(&rn, decp)) /* decimal point? */ |
496 | 32 | count += readdigits(&rn, hex); /* fractional part */ |
497 | 862 | if (count > 0 && test2(&rn, (hex ? "pP" : "eE"))) { /* exponent mark? */ |
498 | 23 | test2(&rn, "-+"); /* exponent sign */ |
499 | 23 | readdigits(&rn, 0); /* exponent digits */ |
500 | 23 | } |
501 | 862 | ungetc(rn.c, rn.f); /* unread look-ahead char */ |
502 | 862 | l_unlockfile(rn.f); |
503 | 862 | rn.buff[rn.n] = '\0'; /* finish string */ |
504 | 862 | if (l_likely(lua_stringtonumber(L, rn.buff))) |
505 | 103 | return 1; /* ok, it is a valid number */ |
506 | 759 | else { /* invalid format */ |
507 | 759 | lua_pushnil(L); /* "result" to be removed */ |
508 | 759 | return 0; /* read fails */ |
509 | 759 | } |
510 | 862 | } |
511 | | |
512 | | |
513 | 22.6k | static int test_eof (lua_State *L, FILE *f) { |
514 | 22.6k | int c = getc(f); |
515 | 22.6k | ungetc(c, f); /* no-op when c == EOF */ |
516 | 22.6k | lua_pushliteral(L, ""); |
517 | 22.6k | return (c != EOF); |
518 | 22.6k | } |
519 | | |
520 | | |
521 | 98.6k | static int read_line (lua_State *L, FILE *f, int chop) { |
522 | 98.6k | luaL_Buffer b; |
523 | 98.6k | int c; |
524 | 98.6k | luaL_buffinit(L, &b); |
525 | 118k | do { /* may need to read several chunks to get whole line */ |
526 | 118k | char *buff = luaL_prepbuffer(&b); /* preallocate buffer space */ |
527 | 118k | unsigned i = 0; |
528 | 118k | l_lockfile(f); /* no memory errors can happen inside the lock */ |
529 | 21.4M | while (i < LUAL_BUFFERSIZE && (c = l_getc(f)) != EOF && c != '\n') |
530 | 21.3M | buff[i++] = cast_char(c); /* read up to end of line or buffer limit */ |
531 | 118k | l_unlockfile(f); |
532 | 118k | luaL_addsize(&b, i); |
533 | 118k | } while (c != EOF && c != '\n'); /* repeat until end of line */ |
534 | 98.6k | if (!chop && c == '\n') /* want a newline and have one? */ |
535 | 1 | luaL_addchar(&b, '\n'); /* add ending newline to result */ |
536 | 98.6k | luaL_pushresult(&b); /* close buffer */ |
537 | | /* return ok if read something (either a newline or something else) */ |
538 | 98.6k | return (c == '\n' || lua_rawlen(L, -1) > 0); |
539 | 98.6k | } |
540 | | |
541 | | |
542 | 17.8k | static void read_all (lua_State *L, FILE *f) { |
543 | 17.8k | size_t nr; |
544 | 17.8k | luaL_Buffer b; |
545 | 17.8k | luaL_buffinit(L, &b); |
546 | 19.6k | do { /* read file in chunks of LUAL_BUFFERSIZE bytes */ |
547 | 19.6k | char *p = luaL_prepbuffer(&b); |
548 | 19.6k | nr = fread(p, sizeof(char), LUAL_BUFFERSIZE, f); |
549 | 19.6k | luaL_addsize(&b, nr); |
550 | 19.6k | } while (nr == LUAL_BUFFERSIZE); |
551 | 17.8k | luaL_pushresult(&b); /* close buffer */ |
552 | 17.8k | } |
553 | | |
554 | | |
555 | 29.9k | static int read_chars (lua_State *L, FILE *f, size_t n) { |
556 | 29.9k | size_t nr; /* number of chars actually read */ |
557 | 29.9k | char *p; |
558 | 29.9k | luaL_Buffer b; |
559 | 29.9k | luaL_buffinit(L, &b); |
560 | 29.9k | p = luaL_prepbuffsize(&b, n); /* prepare buffer to read whole block */ |
561 | 29.9k | nr = fread(p, sizeof(char), n, f); /* try to read 'n' chars */ |
562 | 29.9k | luaL_addsize(&b, nr); |
563 | 29.9k | luaL_pushresult(&b); /* close buffer */ |
564 | 29.9k | return (nr > 0); /* true iff read something */ |
565 | 29.9k | } |
566 | | |
567 | | |
568 | 173k | static int g_read (lua_State *L, FILE *f, int first) { |
569 | 173k | int nargs = lua_gettop(L) - 1; |
570 | 173k | int n, success; |
571 | 173k | clearerr(f); |
572 | 173k | errno = 0; |
573 | 173k | if (nargs == 0) { /* no arguments? */ |
574 | 89.6k | success = read_line(L, f, 1); |
575 | 89.6k | n = first + 1; /* to return 1 result */ |
576 | 89.6k | } |
577 | 84.2k | else { |
578 | | /* ensure stack space for all results and for auxlib's buffer */ |
579 | 84.2k | luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments"); |
580 | 84.2k | success = 1; |
581 | 168k | for (n = first; nargs-- && success; n++) { |
582 | 84.4k | if (lua_type(L, n) == LUA_TNUMBER) { |
583 | 56.6k | size_t l = (size_t)luaL_checkinteger(L, n); |
584 | 56.6k | success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l); |
585 | 56.6k | } |
586 | 27.8k | else { |
587 | 27.8k | const char *p = luaL_checkstring(L, n); |
588 | 27.8k | if (*p == '*') p++; /* skip optional '*' (for compatibility) */ |
589 | 27.8k | switch (*p) { |
590 | 862 | case 'n': /* number */ |
591 | 862 | success = read_number(L, f); |
592 | 862 | break; |
593 | 8.94k | case 'l': /* line */ |
594 | 8.94k | success = read_line(L, f, 1); |
595 | 8.94k | break; |
596 | 94 | case 'L': /* line with end-of-line */ |
597 | 94 | success = read_line(L, f, 0); |
598 | 94 | break; |
599 | 17.8k | case 'a': /* file */ |
600 | 17.8k | read_all(L, f); /* read entire file */ |
601 | 17.8k | success = 1; /* always success */ |
602 | 17.8k | break; |
603 | 90 | default: |
604 | 90 | return luaL_argerror(L, n, "invalid format"); |
605 | 27.8k | } |
606 | 27.8k | } |
607 | 84.4k | } |
608 | 84.2k | } |
609 | 173k | if (ferror(f)) |
610 | 2.52k | return luaL_fileresult(L, 0, NULL); |
611 | 171k | if (!success) { |
612 | 87.6k | lua_pop(L, 1); /* remove last result */ |
613 | 87.6k | luaL_pushfail(L); /* push nil instead */ |
614 | 87.6k | } |
615 | 171k | return n - first; |
616 | 173k | } |
617 | | |
618 | | |
619 | 3.16k | static int io_read (lua_State *L) { |
620 | 3.16k | return g_read(L, getiofile(L, IO_INPUT), 1); |
621 | 3.16k | } |
622 | | |
623 | | |
624 | 28.9k | static int f_read (lua_State *L) { |
625 | 28.9k | return g_read(L, tofile(L), 2); |
626 | 28.9k | } |
627 | | |
628 | | |
629 | | /* |
630 | | ** Iteration function for 'lines'. |
631 | | */ |
632 | 145k | static int io_readline (lua_State *L) { |
633 | 145k | LStream *p = (LStream *)lua_touserdata(L, lua_upvalueindex(1)); |
634 | 145k | int i; |
635 | 145k | int n = (int)lua_tointeger(L, lua_upvalueindex(2)); |
636 | 145k | if (isclosed(p)) /* file is already closed? */ |
637 | 3.57k | return luaL_error(L, "file is already closed"); |
638 | 141k | lua_settop(L , 1); |
639 | 141k | luaL_checkstack(L, n, "too many arguments"); |
640 | 240k | for (i = 1; i <= n; i++) /* push arguments to 'g_read' */ |
641 | 98.5k | lua_pushvalue(L, lua_upvalueindex(3 + i)); |
642 | 141k | n = g_read(L, p->f, 2); /* 'n' is number of results */ |
643 | 141k | lua_assert(n > 0); /* should return at least a nil */ |
644 | 132k | if (lua_toboolean(L, -n)) /* read at least one value? */ |
645 | 46.8k | return n; /* return them */ |
646 | 85.7k | else { /* first result is false: EOF or error */ |
647 | 85.7k | if (n > 1) { /* is there error information? */ |
648 | | /* 2nd result is error message */ |
649 | 3 | return luaL_error(L, "%s", lua_tostring(L, -n + 1)); |
650 | 3 | } |
651 | 85.7k | if (lua_toboolean(L, lua_upvalueindex(3))) { /* generator created file? */ |
652 | 11.2k | lua_settop(L, 0); /* clear stack */ |
653 | 11.2k | lua_pushvalue(L, lua_upvalueindex(1)); /* push file at index 1 */ |
654 | 11.2k | aux_close(L); /* close it */ |
655 | 11.2k | } |
656 | 85.7k | return 0; |
657 | 85.7k | } |
658 | 132k | } |
659 | | |
660 | | /* }====================================================== */ |
661 | | |
662 | | |
663 | 15.3M | static int g_write (lua_State *L, FILE *f, int arg) { |
664 | 15.3M | int nargs = lua_gettop(L) - arg; |
665 | 15.3M | size_t totalbytes = 0; /* total number of bytes written */ |
666 | 15.3M | errno = 0; |
667 | 91.3M | for (; nargs--; arg++) { /* for each argument */ |
668 | 75.9M | char buff[LUA_N2SBUFFSZ]; |
669 | 75.9M | const char *s; |
670 | 75.9M | size_t numbytes; /* bytes written in one call to 'fwrite' */ |
671 | 75.9M | size_t len = lua_numbertocstring(L, arg, buff); /* try as a number */ |
672 | 75.9M | if (len > 0) { /* did conversion work (value was a number)? */ |
673 | 30.2M | s = buff; |
674 | 30.2M | len--; |
675 | 30.2M | } |
676 | 45.7M | else /* must be a string */ |
677 | 45.7M | s = luaL_checklstring(L, arg, &len); |
678 | 75.9M | numbytes = fwrite(s, sizeof(char), len, f); |
679 | 75.9M | totalbytes += numbytes; |
680 | 75.9M | if (numbytes < len) { /* write error? */ |
681 | 9.15k | int n = luaL_fileresult(L, 0, NULL); |
682 | 9.15k | lua_pushinteger(L, cast_st2S(totalbytes)); |
683 | 9.15k | return n + 1; /* return fail, error msg., error code, and counter */ |
684 | 9.15k | } |
685 | 75.9M | } |
686 | 15.3M | return 1; /* no errors; file handle already on stack top */ |
687 | 15.3M | } |
688 | | |
689 | | |
690 | 15.3M | static int io_write (lua_State *L) { |
691 | 15.3M | return g_write(L, getiofile(L, IO_OUTPUT), 1); |
692 | 15.3M | } |
693 | | |
694 | | |
695 | 19.1k | static int f_write (lua_State *L) { |
696 | 19.1k | FILE *f = tofile(L); |
697 | 19.1k | lua_pushvalue(L, 1); /* push file at the stack top (to be returned) */ |
698 | 19.1k | return g_write(L, f, 2); |
699 | 19.1k | } |
700 | | |
701 | | |
702 | 28.8k | static int f_seek (lua_State *L) { |
703 | 28.8k | static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END}; |
704 | 28.8k | static const char *const modenames[] = {"set", "cur", "end", NULL}; |
705 | 28.8k | FILE *f = tofile(L); |
706 | 28.8k | int op = luaL_checkoption(L, 2, "cur", modenames); |
707 | 28.8k | lua_Integer p3 = luaL_optinteger(L, 3, 0); |
708 | 28.8k | l_seeknum offset = (l_seeknum)p3; |
709 | 28.8k | luaL_argcheck(L, (lua_Integer)offset == p3, 3, |
710 | 28.8k | "not an integer in proper range"); |
711 | 28.8k | errno = 0; |
712 | 28.8k | op = l_fseek(f, offset, mode[op]); |
713 | 28.8k | if (l_unlikely(op)) |
714 | 229 | return luaL_fileresult(L, 0, NULL); /* error */ |
715 | 28.6k | else { |
716 | 28.6k | lua_pushinteger(L, (lua_Integer)l_ftell(f)); |
717 | 28.6k | return 1; |
718 | 28.6k | } |
719 | 28.8k | } |
720 | | |
721 | | |
722 | 17.5k | static int f_setvbuf (lua_State *L) { |
723 | 17.5k | static const int mode[] = {_IONBF, _IOFBF, _IOLBF}; |
724 | 17.5k | static const char *const modenames[] = {"no", "full", "line", NULL}; |
725 | 17.5k | FILE *f = tofile(L); |
726 | 17.5k | int op = luaL_checkoption(L, 2, NULL, modenames); |
727 | 17.5k | lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE); |
728 | 17.5k | int res; |
729 | 17.5k | errno = 0; |
730 | 17.5k | res = setvbuf(f, NULL, mode[op], (size_t)sz); |
731 | 17.5k | return luaL_fileresult(L, res == 0, NULL); |
732 | 17.5k | } |
733 | | |
734 | | |
735 | 1.87k | static int aux_flush (lua_State *L, FILE *f) { |
736 | 1.87k | errno = 0; |
737 | 1.87k | return luaL_fileresult(L, fflush(f) == 0, NULL); |
738 | 1.87k | } |
739 | | |
740 | | |
741 | 26 | static int f_flush (lua_State *L) { |
742 | 26 | return aux_flush(L, tofile(L)); |
743 | 26 | } |
744 | | |
745 | | |
746 | 1.84k | static int io_flush (lua_State *L) { |
747 | 1.84k | return aux_flush(L, getiofile(L, IO_OUTPUT)); |
748 | 1.84k | } |
749 | | |
750 | | |
751 | | /* |
752 | | ** functions for 'io' library |
753 | | */ |
754 | | static const luaL_Reg iolib[] = { |
755 | | {"close", io_close}, |
756 | | {"flush", io_flush}, |
757 | | {"input", io_input}, |
758 | | {"lines", io_lines}, |
759 | | {"open", io_open}, |
760 | | {"output", io_output}, |
761 | | {"popen", io_popen}, |
762 | | {"read", io_read}, |
763 | | {"tmpfile", io_tmpfile}, |
764 | | {"type", io_type}, |
765 | | {"write", io_write}, |
766 | | {NULL, NULL} |
767 | | }; |
768 | | |
769 | | |
770 | | /* |
771 | | ** methods for file handles |
772 | | */ |
773 | | static const luaL_Reg meth[] = { |
774 | | {"read", f_read}, |
775 | | {"write", f_write}, |
776 | | {"lines", f_lines}, |
777 | | {"flush", f_flush}, |
778 | | {"seek", f_seek}, |
779 | | {"close", f_close}, |
780 | | {"setvbuf", f_setvbuf}, |
781 | | {NULL, NULL} |
782 | | }; |
783 | | |
784 | | |
785 | | /* |
786 | | ** metamethods for file handles |
787 | | */ |
788 | | static const luaL_Reg metameth[] = { |
789 | | {"__index", NULL}, /* placeholder */ |
790 | | {"__gc", f_gc}, |
791 | | {"__close", f_gc}, |
792 | | {"__tostring", f_tostring}, |
793 | | {NULL, NULL} |
794 | | }; |
795 | | |
796 | | |
797 | 31.9k | static void createmeta (lua_State *L) { |
798 | 31.9k | luaL_newmetatable(L, LUA_FILEHANDLE); /* metatable for file handles */ |
799 | 31.9k | luaL_setfuncs(L, metameth, 0); /* add metamethods to new metatable */ |
800 | 31.9k | luaL_newlibtable(L, meth); /* create method table */ |
801 | 31.9k | luaL_setfuncs(L, meth, 0); /* add file methods to method table */ |
802 | 31.9k | lua_setfield(L, -2, "__index"); /* metatable.__index = method table */ |
803 | 31.9k | lua_pop(L, 1); /* pop metatable */ |
804 | 31.9k | } |
805 | | |
806 | | |
807 | | /* |
808 | | ** function to (not) close the standard files stdin, stdout, and stderr |
809 | | */ |
810 | 296k | static int io_noclose (lua_State *L) { |
811 | 296k | LStream *p = tolstream(L); |
812 | 296k | p->closef = &io_noclose; /* keep file opened */ |
813 | 296k | luaL_pushfail(L); |
814 | 296k | lua_pushliteral(L, "cannot close standard file"); |
815 | 296k | return 2; |
816 | 296k | } |
817 | | |
818 | | |
819 | | static void createstdfile (lua_State *L, FILE *f, const char *k, |
820 | 95.9k | const char *fname) { |
821 | 95.9k | LStream *p = newprefile(L); |
822 | 95.9k | p->f = f; |
823 | 95.9k | p->closef = &io_noclose; |
824 | 95.9k | if (k != NULL) { |
825 | 63.9k | lua_pushvalue(L, -1); |
826 | 63.9k | lua_setfield(L, LUA_REGISTRYINDEX, k); /* add file to registry */ |
827 | 63.9k | } |
828 | 95.9k | lua_setfield(L, -2, fname); /* add file to module */ |
829 | 95.9k | } |
830 | | |
831 | | |
832 | 31.9k | LUAMOD_API int luaopen_io (lua_State *L) { |
833 | 31.9k | luaL_newlib(L, iolib); /* new module */ |
834 | 31.9k | createmeta(L); |
835 | | /* create (and set) default files */ |
836 | 31.9k | createstdfile(L, stdin, IO_INPUT, "stdin"); |
837 | 31.9k | createstdfile(L, stdout, IO_OUTPUT, "stdout"); |
838 | 31.9k | createstdfile(L, stderr, NULL, "stderr"); |
839 | 31.9k | return 1; |
840 | 31.9k | } |
841 | | |