| 422 | |
| 423 | |
| 424 | static int g_read (lua_State *L, FILE *f, int first) { |
| 425 | int nargs = lua_gettop(L) - 1; |
| 426 | int success; |
| 427 | int n; |
| 428 | clearerr(f); |
| 429 | if (nargs == 0) { /* no arguments? */ |
| 430 | success = read_line(L, f, 1); |
| 431 | n = first+1; /* to return 1 result */ |
| 432 | } |
| 433 | else { /* ensure stack space for all results and for auxlib's buffer */ |
| 434 | luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments"); |
| 435 | success = 1; |
| 436 | for (n = first; nargs-- && success; n++) { |
| 437 | if (lua_type(L, n) == LUA_TNUMBER) { |
| 438 | size_t l = (size_t)lua_tointeger(L, n); |
| 439 | success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l); |
| 440 | } |
| 441 | else { |
| 442 | const char *p = lua_tostring(L, n); |
| 443 | luaL_argcheck(L, p && p[0] == '*', n, "invalid option"); |
| 444 | switch (p[1]) { |
| 445 | case 'n': /* number */ |
| 446 | success = read_number(L, f); |
| 447 | break; |
| 448 | case 'l': /* line */ |
| 449 | success = read_line(L, f, 1); |
| 450 | break; |
| 451 | case 'L': /* line with end-of-line */ |
| 452 | success = read_line(L, f, 0); |
| 453 | break; |
| 454 | case 'a': /* file */ |
| 455 | read_all(L, f); /* read entire file */ |
| 456 | success = 1; /* always success */ |
| 457 | break; |
| 458 | default: |
| 459 | return luaL_argerror(L, n, "invalid format"); |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | if (ferror(f)) |
| 465 | return luaL_fileresult(L, 0, NULL); |
| 466 | if (!success) { |
| 467 | lua_pop(L, 1); /* remove last result */ |
| 468 | lua_pushnil(L); /* push nil instead */ |
| 469 | } |
| 470 | return n - first; |
| 471 | } |
| 472 | |
| 473 | |
| 474 | static int io_read (lua_State *L) { |
no test coverage detected