| 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) { /* no arguments? */ |
| 340 | success = read_line(L, f); |
| 341 | n = first+1; /* to return 1 result */ |
| 342 | } |
| 343 | else { /* ensure stack space for all results and for auxlib's buffer */ |
| 344 | luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments"); |
| 345 | success = 1; |
| 346 | for (n = first; nargs-- && success; n++) { |
| 347 | if (lua_type(L, n) == LUA_TNUMBER) { |
| 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); |
| 353 | luaL_argcheck(L, p && p[0] == '*', n, "invalid option"); |
| 354 | switch (p[1]) { |
| 355 | case 'n': /* number */ |
| 356 | success = read_number(L, f); |
| 357 | break; |
| 358 | case 'l': /* line */ |
| 359 | success = read_line(L, f); |
| 360 | break; |
| 361 | case 'a': /* file */ |
| 362 | read_chars(L, f, ~((size_t)0)); /* read MAX_SIZE_T chars */ |
| 363 | success = 1; /* always success */ |
| 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); |
| 373 | if (!success) { |
| 374 | lua_pop(L, 1); /* remove last result */ |
| 375 | lua_pushnil(L); /* push nil instead */ |
| 376 | } |
| 377 | return n - first; |
| 378 | } |
| 379 | |
| 380 | |
| 381 | static int io_read (lua_State *L) { |
no test coverage detected