| 567 | |
| 568 | |
| 569 | static int g_read (lua_State *L, FILE *f, int first) { |
| 570 | int nargs = lua_gettop(L) - 1; |
| 571 | int n, success; |
| 572 | clearerr(f); |
| 573 | errno = 0; |
| 574 | if (nargs == 0) { /* no arguments? */ |
| 575 | success = read_line(L, f, 1); |
| 576 | n = first + 1; /* to return 1 result */ |
| 577 | } |
| 578 | else { |
| 579 | /* ensure stack space for all results and for auxlib's buffer */ |
| 580 | luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments"); |
| 581 | success = 1; |
| 582 | for (n = first; nargs-- && success; n++) { |
| 583 | if (lua_type(L, n) == LUA_TNUMBER) { |
| 584 | size_t l = (size_t)luaL_checkinteger(L, n); |
| 585 | success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l); |
| 586 | } |
| 587 | else { |
| 588 | const char *p = luaL_checkstring(L, n); |
| 589 | if (*p == '*') p++; /* skip optional '*' (for compatibility) */ |
| 590 | switch (*p) { |
| 591 | case 'n': /* number */ |
| 592 | success = read_number(L, f); |
| 593 | break; |
| 594 | case 'l': /* line */ |
| 595 | success = read_line(L, f, 1); |
| 596 | break; |
| 597 | case 'L': /* line with end-of-line */ |
| 598 | success = read_line(L, f, 0); |
| 599 | break; |
| 600 | case 'a': /* file */ |
| 601 | read_all(L, f); /* read entire file */ |
| 602 | success = 1; /* always success */ |
| 603 | break; |
| 604 | default: |
| 605 | return luaL_argerror(L, n, "invalid format"); |
| 606 | } |
| 607 | } |
| 608 | } |
| 609 | } |
| 610 | if (ferror(f)) |
| 611 | return luaL_fileresult(L, 0, NULL); |
| 612 | if (!success) { |
| 613 | lua_pop(L, 1); /* remove last result */ |
| 614 | luaL_pushfail(L); /* push nil instead */ |
| 615 | } |
| 616 | return n - first; |
| 617 | } |
| 618 | |
| 619 | |
| 620 | static int io_read (lua_State *L) { |
no test coverage detected