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