| 12243 | |
| 12244 | |
| 12245 | static int g_read (lua_State *L, FILE *f, int first) { |
| 12246 | int nargs = lua_gettop(L) - 1; |
| 12247 | int success; |
| 12248 | int n; |
| 12249 | clearerr(f); |
| 12250 | if (nargs == 0) { /* no arguments? */ |
| 12251 | success = read_line(L, f); |
| 12252 | n = first+1; /* to return 1 result */ |
| 12253 | } |
| 12254 | else { /* ensure stack space for all results and for auxlib's buffer */ |
| 12255 | luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments"); |
| 12256 | success = 1; |
| 12257 | for (n = first; nargs-- && success; n++) { |
| 12258 | if (lua_type(L, n) == LUA_TNUMBER) { |
| 12259 | size_t l = (size_t)lua_tointeger(L, n); |
| 12260 | success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l); |
| 12261 | } |
| 12262 | else { |
| 12263 | const char *p = lua_tostring(L, n); |
| 12264 | luaL_argcheck(L, p && p[0] == '*', n, "invalid option"); |
| 12265 | switch (p[1]) { |
| 12266 | case 'n': /* number */ |
| 12267 | success = read_number(L, f); |
| 12268 | break; |
| 12269 | case 'l': /* line */ |
| 12270 | success = read_line(L, f); |
| 12271 | break; |
| 12272 | case 'a': /* file */ |
| 12273 | read_chars(L, f, ~((size_t)0)); /* read MAX_SIZE_T chars */ |
| 12274 | success = 1; /* always success */ |
| 12275 | break; |
| 12276 | default: |
| 12277 | return luaL_argerror(L, n, "invalid format"); |
| 12278 | } |
| 12279 | } |
| 12280 | } |
| 12281 | } |
| 12282 | if (ferror(f)) |
| 12283 | return pushresult(L, 0, NULL); |
| 12284 | if (!success) { |
| 12285 | lua_pop(L, 1); /* remove last result */ |
| 12286 | lua_pushnil(L); /* push nil instead */ |
| 12287 | } |
| 12288 | return n - first; |
| 12289 | } |
| 12290 | |
| 12291 | |
| 12292 | static int io_read (lua_State *L) { |
no test coverage detected