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