Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
liolib.c File Reference
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"

Go to the source code of this file.

Macros

#define _LARGEFILE_SOURCE   1
 
#define _FILE_OFFSET_BITS   64
 
#define liolib_c
 
#define LUA_LIB
 
#define lua_checkmode(mode)
 
#define lua_popen(L, c, m)
 
#define lua_pclose(L, file)   ((void)((void)L, file), -1)
 
#define l_fseek(f, o, w)   fseek(f,o,w)
 
#define l_ftell(f)   ftell(f)
 
#define l_seeknum   long
 
#define IO_PREFIX   "_IO_"
 
#define IO_INPUT   (IO_PREFIX "input")
 
#define IO_OUTPUT   (IO_PREFIX "output")
 
#define tolstream(L)   ((LStream *)luaL_checkudata(L, 1, LUA_FILEHANDLE))
 
#define isclosed(p)   ((p)->closef == NULL)
 
#define MAX_SIZE_T   (~(size_t)0)
 

Typedefs

typedef luaL_Stream LStream
 

Functions

static int io_type (lua_State *L)
 
static int f_tostring (lua_State *L)
 
static FILE * tofile (lua_State *L)
 
static LStreamnewprefile (lua_State *L)
 
static int aux_close (lua_State *L)
 
static int io_close (lua_State *L)
 
static int f_gc (lua_State *L)
 
static int io_fclose (lua_State *L)
 
static LStreamnewfile (lua_State *L)
 
static void opencheck (lua_State *L, const char *fname, const char *mode)
 
static int io_open (lua_State *L)
 
static int io_pclose (lua_State *L)
 
static int io_popen (lua_State *L)
 
static int io_tmpfile (lua_State *L)
 
static FILE * getiofile (lua_State *L, const char *findex)
 
static int g_iofile (lua_State *L, const char *f, const char *mode)
 
static int io_input (lua_State *L)
 
static int io_output (lua_State *L)
 
static int io_readline (lua_State *L)
 
static void aux_lines (lua_State *L, int toclose)
 
static int f_lines (lua_State *L)
 
static int io_lines (lua_State *L)
 
static int read_number (lua_State *L, FILE *f)
 
static int test_eof (lua_State *L, FILE *f)
 
static int read_line (lua_State *L, FILE *f, int chop)
 
static void read_all (lua_State *L, FILE *f)
 
static int read_chars (lua_State *L, FILE *f, size_t n)
 
static int g_read (lua_State *L, FILE *f, int first)
 
static int io_read (lua_State *L)
 
static int f_read (lua_State *L)
 
static int g_write (lua_State *L, FILE *f, int arg)
 
static int io_write (lua_State *L)
 
static int f_write (lua_State *L)
 
static int f_seek (lua_State *L)
 
static int f_setvbuf (lua_State *L)
 
static int io_flush (lua_State *L)
 
static int f_flush (lua_State *L)
 
static void createmeta (lua_State *L)
 
static int io_noclose (lua_State *L)
 
static void createstdfile (lua_State *L, FILE *f, const char *k, const char *fname)
 
LUAMOD_API int luaopen_io (lua_State *L)
 

Variables

static const luaL_Reg iolib []
 
static const luaL_Reg flib []
 

Macro Definition Documentation

◆ _FILE_OFFSET_BITS

#define _FILE_OFFSET_BITS   64

Definition at line 14 of file lua-5.2.4/src/liolib.c.

◆ _LARGEFILE_SOURCE

#define _LARGEFILE_SOURCE   1

Definition at line 13 of file lua-5.2.4/src/liolib.c.

◆ IO_INPUT

#define IO_INPUT   (IO_PREFIX "input")

Definition at line 118 of file lua-5.2.4/src/liolib.c.

Referenced by io_input(), io_lines(), io_read(), and luaopen_io().

◆ IO_OUTPUT

#define IO_OUTPUT   (IO_PREFIX "output")

Definition at line 119 of file lua-5.2.4/src/liolib.c.

Referenced by io_close(), io_flush(), io_output(), io_write(), and luaopen_io().

◆ IO_PREFIX

#define IO_PREFIX   "_IO_"

Definition at line 117 of file lua-5.2.4/src/liolib.c.

Referenced by getiofile().

◆ isclosed

#define isclosed ( p)    ((p)->closef == NULL)

Definition at line 127 of file lua-5.2.4/src/liolib.c.

Referenced by f_gc(), f_tostring(), getiofile(), io_readline(), io_type(), and tofile().

◆ l_fseek

#define l_fseek ( f,
o,
w )   fseek(f,o,w)

Definition at line 109 of file lua-5.2.4/src/liolib.c.

Referenced by f_seek().

◆ l_ftell

#define l_ftell ( f)    ftell(f)

Definition at line 110 of file lua-5.2.4/src/liolib.c.

Referenced by f_seek().

◆ l_seeknum

#define l_seeknum   long

Definition at line 111 of file lua-5.2.4/src/liolib.c.

Referenced by f_seek().

◆ liolib_c

#define liolib_c

Definition at line 23 of file lua-5.2.4/src/liolib.c.

◆ lua_checkmode

#define lua_checkmode ( mode)
Value:
(*mode != '\0' && strchr("rwa", *(mode++)) != NULL && \
(*mode != '+' || ++mode) && /* skip if char is '+' */ \
(*mode != 'b' || ++mode) && /* skip if char is 'b' */ \
(*mode == '\0'))
#define NULL
Definition gmacros.h:924

Definition at line 39 of file lua-5.2.4/src/liolib.c.

39#define lua_checkmode(mode) \
40 (*mode != '\0' && strchr("rwa", *(mode++)) != NULL && \
41 (*mode != '+' || ++mode) && /* skip if char is '+' */ \
42 (*mode != 'b' || ++mode) && /* skip if char is 'b' */ \
43 (*mode == '\0'))

Referenced by io_open().

◆ LUA_LIB

#define LUA_LIB

Definition at line 24 of file lua-5.2.4/src/liolib.c.

◆ lua_pclose

#define lua_pclose ( L,
file )   ((void)((void)L, file), -1)

Definition at line 71 of file lua-5.2.4/src/liolib.c.

Referenced by io_pclose().

◆ lua_popen

#define lua_popen ( L,
c,
m )
Value:
((void)((void)c, m), \
luaL_error(L, LUA_QL("popen") " not supported"), (FILE*)0)
LUALIB_API int luaL_error(lua_State *L, const char *fmt,...)
#define LUA_QL(x)

Definition at line 69 of file lua-5.2.4/src/liolib.c.

69#define lua_popen(L,c,m) ((void)((void)c, m), \
70 luaL_error(L, LUA_QL("popen") " not supported"), (FILE*)0)

Referenced by io_popen().

◆ MAX_SIZE_T

#define MAX_SIZE_T   (~(size_t)0)

Definition at line 393 of file lua-5.2.4/src/liolib.c.

Referenced by read_all().

◆ tolstream

#define tolstream ( L)    ((LStream *)luaL_checkudata(L, 1, LUA_FILEHANDLE))

Typedef Documentation

◆ LStream

Definition at line 122 of file lua-5.2.4/src/liolib.c.

Function Documentation

◆ aux_close()

static int aux_close ( lua_State * L)
static

Definition at line 176 of file lua-5.2.4/src/liolib.c.

176 {
177 LStream *p = tolstream(L);
178 lua_CFunction cf = p->closef;
179 p->closef = NULL; /* mark stream as closed */
180 return (*cf)(L); /* close it */
181}
int(* lua_CFunction)(lua_State *L)
#define tolstream(L)
lua_CFunction closef

References luaL_Stream::closef, NULL, and tolstream.

Referenced by f_gc(), io_close(), and io_readline().

◆ aux_lines()

static void aux_lines ( lua_State * L,
int toclose )
static

Definition at line 303 of file lua-5.2.4/src/liolib.c.

303 {
304 int i;
305 int n = lua_gettop(L) - 1; /* number of arguments to read */
306 /* ensure that arguments will fit here and into 'io_readline' stack */
307 luaL_argcheck(L, n <= LUA_MINSTACK - 3, LUA_MINSTACK - 3, "too many options");
308 lua_pushvalue(L, 1); /* file handle */
309 lua_pushinteger(L, n); /* number of arguments to read */
310 lua_pushboolean(L, toclose); /* close/not close file when finished */
311 for (i = 1; i <= n; i++) lua_pushvalue(L, i + 1); /* copy arguments */
312 lua_pushcclosure(L, io_readline, 3 + n);
313}
LUA_API void lua_pushcclosure(lua_State *L, lua_CFunction fn, int n)
LUA_API void lua_pushboolean(lua_State *L, int b)
LUA_API void lua_pushvalue(lua_State *L, int idx)
LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
LUA_API int lua_gettop(lua_State *L)
#define luaL_argcheck(L, cond, numarg, extramsg)
#define LUA_MINSTACK
static int io_readline(lua_State *L)

References io_readline(), lua_gettop(), LUA_MINSTACK, lua_pushboolean(), lua_pushcclosure(), lua_pushinteger(), lua_pushvalue(), and luaL_argcheck.

Referenced by f_lines(), and io_lines().

◆ createmeta()

static void createmeta ( lua_State * L)
static

Definition at line 623 of file lua-5.2.4/src/liolib.c.

623 {
624 luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */
625 lua_pushvalue(L, -1); /* push metatable */
626 lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
627 luaL_setfuncs(L, flib, 0); /* add file methods to new metatable */
628 lua_pop(L, 1); /* pop new metatable */
629}
LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
LUALIB_API int luaL_newmetatable(lua_State *L, const char *tname)
#define lua_pop(L, n)
#define LUA_FILEHANDLE
LUALIB_API void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup)
static const luaL_Reg flib[]

References flib, LUA_FILEHANDLE, lua_pop, lua_pushvalue(), lua_setfield(), luaL_newmetatable(), and luaL_setfuncs().

Referenced by luaopen_io().

◆ createstdfile()

static void createstdfile ( lua_State * L,
FILE * f,
const char * k,
const char * fname )
static

Definition at line 644 of file lua-5.2.4/src/liolib.c.

645 {
646 LStream *p = newprefile(L);
647 p->f = f;
648 p->closef = &io_noclose;
649 if (k != NULL) {
650 lua_pushvalue(L, -1);
651 lua_setfield(L, LUA_REGISTRYINDEX, k); /* add file to registry */
652 }
653 lua_setfield(L, -2, fname); /* add file to module */
654}
#define LUA_REGISTRYINDEX
static int io_noclose(lua_State *L)
static LStream * newprefile(lua_State *L)

References luaL_Stream::closef, luaL_Stream::f, io_noclose(), lua_pushvalue(), LUA_REGISTRYINDEX, lua_setfield(), newprefile(), and NULL.

Referenced by luaopen_io().

◆ f_flush()

static int f_flush ( lua_State * L)
static

Definition at line 582 of file lua-5.2.4/src/liolib.c.

582 {
583 return luaL_fileresult(L, fflush(tofile(L)) == 0, NULL);
584}
LUALIB_API int luaL_fileresult(lua_State *L, int stat, const char *fname)
static FILE * tofile(lua_State *L)

References luaL_fileresult(), NULL, and tofile().

◆ f_gc()

static int f_gc ( lua_State * L)
static

Definition at line 192 of file lua-5.2.4/src/liolib.c.

192 {
193 LStream *p = tolstream(L);
194 if (!isclosed(p) && p->f != NULL)
195 aux_close(L); /* ignore closed and incompletely open files */
196 return 0;
197}
static int aux_close(lua_State *L)
#define isclosed(p)

References aux_close(), luaL_Stream::f, isclosed, NULL, and tolstream.

◆ f_lines()

static int f_lines ( lua_State * L)
static

Definition at line 316 of file lua-5.2.4/src/liolib.c.

316 {
317 tofile(L); /* check that it's a valid file handle */
318 aux_lines(L, 0);
319 return 1;
320}
static void aux_lines(lua_State *L, int toclose)

References aux_lines(), and tofile().

◆ f_read()

static int f_read ( lua_State * L)
static

Definition at line 479 of file lua-5.2.4/src/liolib.c.

479 {
480 return g_read(L, tofile(L), 2);
481}
static int g_read(lua_State *L, FILE *f, int first)

References g_read(), and tofile().

◆ f_seek()

static int f_seek ( lua_State * L)
static

Definition at line 546 of file lua-5.2.4/src/liolib.c.

546 {
547 static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
548 static const char *const modenames[] = {"set", "cur", "end", NULL};
549 FILE *f = tofile(L);
550 int op = luaL_checkoption(L, 2, "cur", modenames);
551 lua_Number p3 = luaL_optnumber(L, 3, 0);
552 l_seeknum offset = (l_seeknum)p3;
553 luaL_argcheck(L, (lua_Number)offset == p3, 3,
554 "not an integer in proper range");
555 op = l_fseek(f, offset, mode[op]);
556 if (op)
557 return luaL_fileresult(L, 0, NULL); /* error */
558 else {
560 return 1;
561 }
562}
LUA_API void lua_pushnumber(lua_State *L, lua_Number n)
LUALIB_API lua_Number luaL_optnumber(lua_State *L, int narg, lua_Number def)
LUALIB_API int luaL_checkoption(lua_State *L, int narg, const char *def, const char *const lst[])
LUA_NUMBER lua_Number
#define l_seeknum
#define l_fseek(f, o, w)
#define l_ftell(f)

References l_fseek, l_ftell, l_seeknum, lua_pushnumber(), luaL_argcheck, luaL_checkoption(), luaL_fileresult(), luaL_optnumber(), NULL, and tofile().

◆ f_setvbuf()

static int f_setvbuf ( lua_State * L)
static

Definition at line 565 of file lua-5.2.4/src/liolib.c.

565 {
566 static const int mode[] = {_IONBF, _IOFBF, _IOLBF};
567 static const char *const modenames[] = {"no", "full", "line", NULL};
568 FILE *f = tofile(L);
569 int op = luaL_checkoption(L, 2, NULL, modenames);
571 int res = setvbuf(f, NULL, mode[op], sz);
572 return luaL_fileresult(L, res == 0, NULL);
573}
LUALIB_API lua_Integer luaL_optinteger(lua_State *L, int narg, lua_Integer def)
LUA_INTEGER lua_Integer
#define LUAL_BUFFERSIZE

References LUAL_BUFFERSIZE, luaL_checkoption(), luaL_fileresult(), luaL_optinteger(), NULL, and tofile().

◆ f_tostring()

static int f_tostring ( lua_State * L)
static

Definition at line 144 of file lua-5.2.4/src/liolib.c.

144 {
145 LStream *p = tolstream(L);
146 if (isclosed(p))
147 lua_pushliteral(L, "file (closed)");
148 else
149 lua_pushfstring(L, "file (%p)", p->f);
150 return 1;
151}
LUA_API const char * lua_pushfstring(lua_State *L, const char *fmt,...)
#define lua_pushliteral(L, s)

References luaL_Stream::f, isclosed, lua_pushfstring(), lua_pushliteral, and tolstream.

◆ f_write()

static int f_write ( lua_State * L)
static

Definition at line 539 of file lua-5.2.4/src/liolib.c.

539 {
540 FILE *f = tofile(L);
541 lua_pushvalue(L, 1); /* push file at the stack top (to be returned) */
542 return g_write(L, f, 2);
543}
static int g_write(lua_State *L, FILE *f, int arg)

References g_write(), lua_pushvalue(), and tofile().

◆ g_iofile()

static int g_iofile ( lua_State * L,
const char * f,
const char * mode )
static

Definition at line 273 of file lua-5.2.4/src/liolib.c.

273 {
274 if (!lua_isnoneornil(L, 1)) {
275 const char *filename = lua_tostring(L, 1);
276 if (filename)
277 opencheck(L, filename, mode);
278 else {
279 tofile(L); /* check that it's a valid file handle */
280 lua_pushvalue(L, 1);
281 }
283 }
284 /* return current value */
286 return 1;
287}
LUA_API void lua_getfield(lua_State *L, int idx, const char *k)
#define lua_tostring(L, i)
#define lua_isnoneornil(L, n)
static void opencheck(lua_State *L, const char *fname, const char *mode)

References lua_getfield(), lua_isnoneornil, lua_pushvalue(), LUA_REGISTRYINDEX, lua_setfield(), lua_tostring, opencheck(), and tofile().

Referenced by io_input(), and io_output().

◆ g_read()

static int g_read ( lua_State * L,
FILE * f,
int first )
static

Definition at line 424 of file lua-5.2.4/src/liolib.c.

424 {
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}
LUA_API void lua_pushnil(lua_State *L)
LUA_API int lua_type(lua_State *L, int idx)
LUALIB_API void luaL_checkstack(lua_State *L, int space, const char *mes)
LUALIB_API int luaL_argerror(lua_State *L, int narg, const char *extramsg)
#define LUA_TNUMBER
static void read_all(lua_State *L, FILE *f)
static int read_number(lua_State *L, FILE *f)
static int read_chars(lua_State *L, FILE *f, size_t n)
static int test_eof(lua_State *L, FILE *f)
static int read_line(lua_State *L, FILE *f, int chop)
#define lua_tointeger(L, i)

References lua_gettop(), LUA_MINSTACK, lua_pop, lua_pushnil(), LUA_TNUMBER, lua_tointeger, lua_tostring, lua_type(), luaL_argcheck, luaL_argerror(), luaL_checkstack(), luaL_fileresult(), NULL, read_all(), read_chars(), read_line(), read_number(), and test_eof().

Referenced by f_read(), io_read(), and io_readline().

◆ g_write()

static int g_write ( lua_State * L,
FILE * f,
int arg )
static

Definition at line 514 of file lua-5.2.4/src/liolib.c.

514 {
515 int nargs = lua_gettop(L) - arg;
516 int status = 1;
517 for (; nargs--; arg++) {
518 if (lua_type(L, arg) == LUA_TNUMBER) {
519 /* optimization: could be done exactly as for strings */
520 status = status &&
521 fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0;
522 }
523 else {
524 size_t l;
525 const char *s = luaL_checklstring(L, arg, &l);
526 status = status && (fwrite(s, sizeof(char), l, f) == l);
527 }
528 }
529 if (status) return 1; /* file handle already on stack top */
530 else return luaL_fileresult(L, status, NULL);
531}
CURL_EXTERN int void * arg
Definition curl.h:2622
LUALIB_API const char * luaL_checklstring(lua_State *L, int narg, size_t *len)
#define LUA_NUMBER_FMT
#define lua_tonumber(L, i)
CURL_EXTERN CURLMcode curl_socket_t s
Definition multi.h:318
size_t fwrite(const void *, size_t, size_t, FILE *)

References arg, fwrite(), lua_gettop(), LUA_NUMBER_FMT, LUA_TNUMBER, lua_tonumber, lua_type(), luaL_checklstring(), luaL_fileresult(), NULL, and s.

Referenced by f_write(), and io_write().

◆ getiofile()

static FILE * getiofile ( lua_State * L,
const char * findex )
static

Definition at line 263 of file lua-5.2.4/src/liolib.c.

263 {
264 LStream *p;
266 p = (LStream *)lua_touserdata(L, -1);
267 if (isclosed(p))
268 luaL_error(L, "standard %s file is closed", findex + strlen(IO_PREFIX));
269 return p->f;
270}
LUA_API void * lua_touserdata(lua_State *L, int idx)
#define IO_PREFIX

References luaL_Stream::f, IO_PREFIX, isclosed, lua_getfield(), LUA_REGISTRYINDEX, lua_touserdata(), and luaL_error().

Referenced by io_flush(), io_read(), and io_write().

◆ io_close()

static int io_close ( lua_State * L)
static

Definition at line 184 of file lua-5.2.4/src/liolib.c.

184 {
185 if (lua_isnone(L, 1)) /* no argument? */
186 lua_getfield(L, LUA_REGISTRYINDEX, IO_OUTPUT); /* use standard output */
187 tofile(L); /* make sure argument is an open stream */
188 return aux_close(L);
189}
#define lua_isnone(L, n)
#define IO_OUTPUT

References aux_close(), IO_OUTPUT, lua_getfield(), lua_isnone, LUA_REGISTRYINDEX, and tofile().

◆ io_fclose()

static int io_fclose ( lua_State * L)
static

Definition at line 203 of file lua-5.2.4/src/liolib.c.

203 {
204 LStream *p = tolstream(L);
205 int res = fclose(p->f);
206 return luaL_fileresult(L, (res == 0), NULL);
207}

References luaL_Stream::f, luaL_fileresult(), NULL, and tolstream.

Referenced by newfile().

◆ io_flush()

static int io_flush ( lua_State * L)
static

Definition at line 577 of file lua-5.2.4/src/liolib.c.

577 {
578 return luaL_fileresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL);
579}
static FILE * getiofile(lua_State *L, const char *findex)

References getiofile(), IO_OUTPUT, luaL_fileresult(), and NULL.

◆ io_input()

static int io_input ( lua_State * L)
static

Definition at line 290 of file lua-5.2.4/src/liolib.c.

290 {
291 return g_iofile(L, IO_INPUT, "r");
292}
#define IO_INPUT
static int g_iofile(lua_State *L, const char *f, const char *mode)

References g_iofile(), and IO_INPUT.

◆ io_lines()

static int io_lines ( lua_State * L)
static

Definition at line 323 of file lua-5.2.4/src/liolib.c.

323 {
324 int toclose;
325 if (lua_isnone(L, 1)) lua_pushnil(L); /* at least one argument */
326 if (lua_isnil(L, 1)) { /* no file name? */
327 lua_getfield(L, LUA_REGISTRYINDEX, IO_INPUT); /* get default input */
328 lua_replace(L, 1); /* put it at index 1 */
329 tofile(L); /* check that it's a valid file handle */
330 toclose = 0; /* do not close it after iteration */
331 }
332 else { /* open a new file */
333 const char *filename = luaL_checkstring(L, 1);
334 opencheck(L, filename, "r");
335 lua_replace(L, 1); /* put file at index 1 */
336 toclose = 1; /* close it after iteration */
337 }
338 aux_lines(L, toclose);
339 return 1;
340}
#define luaL_checkstring(L, n)
#define lua_isnil(L, n)
#define lua_replace(L, idx)

References aux_lines(), IO_INPUT, lua_getfield(), lua_isnil, lua_isnone, lua_pushnil(), LUA_REGISTRYINDEX, lua_replace, luaL_checkstring, opencheck(), and tofile().

◆ io_noclose()

static int io_noclose ( lua_State * L)
static

Definition at line 635 of file lua-5.2.4/src/liolib.c.

635 {
636 LStream *p = tolstream(L);
637 p->closef = &io_noclose; /* keep file opened */
638 lua_pushnil(L);
639 lua_pushliteral(L, "cannot close standard file");
640 return 2;
641}

References luaL_Stream::closef, io_noclose(), lua_pushliteral, lua_pushnil(), and tolstream.

Referenced by createstdfile(), and io_noclose().

◆ io_open()

static int io_open ( lua_State * L)
static

Definition at line 226 of file lua-5.2.4/src/liolib.c.

226 {
227 const char *filename = luaL_checkstring(L, 1);
228 const char *mode = luaL_optstring(L, 2, "r");
229 LStream *p = newfile(L);
230 const char *md = mode; /* to traverse/check mode */
231 luaL_argcheck(L, lua_checkmode(md), 2, "invalid mode");
232 p->f = fopen(filename, mode);
233 return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1;
234}
#define luaL_optstring(L, n, d)
static LStream * newfile(lua_State *L)
#define lua_checkmode(mode)

References luaL_Stream::f, lua_checkmode, luaL_argcheck, luaL_checkstring, luaL_fileresult(), luaL_optstring, newfile(), and NULL.

◆ io_output()

static int io_output ( lua_State * L)
static

Definition at line 295 of file lua-5.2.4/src/liolib.c.

295 {
296 return g_iofile(L, IO_OUTPUT, "w");
297}

References g_iofile(), and IO_OUTPUT.

◆ io_pclose()

static int io_pclose ( lua_State * L)
static

Definition at line 240 of file lua-5.2.4/src/liolib.c.

240 {
241 LStream *p = tolstream(L);
242 return luaL_execresult(L, lua_pclose(L, p->f));
243}
LUALIB_API int luaL_execresult(lua_State *L, int stat)
#define lua_pclose(L, file)

References luaL_Stream::f, lua_pclose, luaL_execresult(), and tolstream.

Referenced by io_popen().

◆ io_popen()

static int io_popen ( lua_State * L)
static

Definition at line 246 of file lua-5.2.4/src/liolib.c.

246 {
247 const char *filename = luaL_checkstring(L, 1);
248 const char *mode = luaL_optstring(L, 2, "r");
249 LStream *p = newprefile(L);
250 p->f = lua_popen(L, filename, mode);
251 p->closef = &io_pclose;
252 return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1;
253}
#define lua_popen(L, c, m)
static int io_pclose(lua_State *L)

References luaL_Stream::closef, luaL_Stream::f, io_pclose(), lua_popen, luaL_checkstring, luaL_fileresult(), luaL_optstring, newprefile(), and NULL.

◆ io_read()

static int io_read ( lua_State * L)
static

Definition at line 474 of file lua-5.2.4/src/liolib.c.

474 {
475 return g_read(L, getiofile(L, IO_INPUT), 1);
476}

References g_read(), getiofile(), and IO_INPUT.

◆ io_readline()

static int io_readline ( lua_State * L)
static

Definition at line 484 of file lua-5.2.4/src/liolib.c.

484 {
486 int i;
487 int n = (int)lua_tointeger(L, lua_upvalueindex(2));
488 if (isclosed(p)) /* file is already closed? */
489 return luaL_error(L, "file is already closed");
490 lua_settop(L , 1);
491 for (i = 1; i <= n; i++) /* push arguments to 'g_read' */
493 n = g_read(L, p->f, 2); /* 'n' is number of results */
494 lua_assert(n > 0); /* should return at least a nil */
495 if (!lua_isnil(L, -n)) /* read at least one value? */
496 return n; /* return them */
497 else { /* first result is nil: EOF or error */
498 if (n > 1) { /* is there error information? */
499 /* 2nd result is error message */
500 return luaL_error(L, "%s", lua_tostring(L, -n + 1));
501 }
502 if (lua_toboolean(L, lua_upvalueindex(3))) { /* generator created file? */
503 lua_settop(L, 0);
505 aux_close(L); /* close it */
506 }
507 return 0;
508 }
509}
LUA_API int lua_toboolean(lua_State *L, int idx)
LUA_API void lua_settop(lua_State *L, int idx)
#define lua_assert(c)
#define lua_upvalueindex(i)

References aux_close(), luaL_Stream::f, g_read(), isclosed, lua_assert, lua_isnil, lua_pushvalue(), lua_settop(), lua_toboolean(), lua_tointeger, lua_tostring, lua_touserdata(), lua_upvalueindex, and luaL_error().

Referenced by aux_lines().

◆ io_tmpfile()

static int io_tmpfile ( lua_State * L)
static

Definition at line 256 of file lua-5.2.4/src/liolib.c.

256 {
257 LStream *p = newfile(L);
258 p->f = tmpfile();
259 return (p->f == NULL) ? luaL_fileresult(L, 0, NULL) : 1;
260}

References luaL_Stream::f, luaL_fileresult(), newfile(), and NULL.

◆ io_type()

static int io_type ( lua_State * L)
static

Definition at line 130 of file lua-5.2.4/src/liolib.c.

130 {
131 LStream *p;
132 luaL_checkany(L, 1);
134 if (p == NULL)
135 lua_pushnil(L); /* not a file */
136 else if (isclosed(p))
137 lua_pushliteral(L, "closed file");
138 else
139 lua_pushliteral(L, "file");
140 return 1;
141}
LUALIB_API void luaL_checkany(lua_State *L, int narg)
LUALIB_API void * luaL_testudata(lua_State *L, int ud, const char *tname)

References isclosed, LUA_FILEHANDLE, lua_pushliteral, lua_pushnil(), luaL_checkany(), luaL_testudata(), and NULL.

◆ io_write()

static int io_write ( lua_State * L)
static

Definition at line 534 of file lua-5.2.4/src/liolib.c.

534 {
535 return g_write(L, getiofile(L, IO_OUTPUT), 1);
536}

References g_write(), getiofile(), and IO_OUTPUT.

◆ luaopen_io()

LUAMOD_API int luaopen_io ( lua_State * L)

Definition at line 657 of file lua-5.2.4/src/liolib.c.

657 {
658 luaL_newlib(L, iolib); /* new module */
659 createmeta(L);
660 /* create (and set) default files */
661 createstdfile(L, stdin, IO_INPUT, "stdin");
662 createstdfile(L, stdout, IO_OUTPUT, "stdout");
663 createstdfile(L, stderr, NULL, "stderr");
664 return 1;
665}
#define luaL_newlib(L, l)
static void createstdfile(lua_State *L, FILE *f, const char *k, const char *fname)
static void createmeta(lua_State *L)
static const luaL_Reg iolib[]

References createmeta(), createstdfile(), IO_INPUT, IO_OUTPUT, iolib, luaL_newlib, and NULL.

◆ newfile()

static LStream * newfile ( lua_State * L)
static

Definition at line 210 of file lua-5.2.4/src/liolib.c.

210 {
211 LStream *p = newprefile(L);
212 p->f = NULL;
213 p->closef = &io_fclose;
214 return p;
215}
static int io_fclose(lua_State *L)

References luaL_Stream::closef, luaL_Stream::f, io_fclose(), newprefile(), and NULL.

Referenced by io_open(), io_tmpfile(), and opencheck().

◆ newprefile()

static LStream * newprefile ( lua_State * L)
static

Definition at line 168 of file lua-5.2.4/src/liolib.c.

168 {
169 LStream *p = (LStream *)lua_newuserdata(L, sizeof(LStream));
170 p->closef = NULL; /* mark file handle as 'closed' */
172 return p;
173}
LUALIB_API void luaL_setmetatable(lua_State *L, const char *tname)
#define lua_newuserdata(L, s)

References luaL_Stream::closef, LUA_FILEHANDLE, lua_newuserdata, luaL_setmetatable(), and NULL.

Referenced by createstdfile(), io_popen(), and newfile().

◆ opencheck()

static void opencheck ( lua_State * L,
const char * fname,
const char * mode )
static

Definition at line 218 of file lua-5.2.4/src/liolib.c.

218 {
219 LStream *p = newfile(L);
220 p->f = fopen(fname, mode);
221 if (p->f == NULL)
222 luaL_error(L, "cannot open file " LUA_QS " (%s)", fname, strerror(errno));
223}
#define LUA_QS

References luaL_Stream::f, LUA_QS, luaL_error(), newfile(), and NULL.

Referenced by g_iofile(), and io_lines().

◆ read_all()

static void read_all ( lua_State * L,
FILE * f )
static

Definition at line 395 of file lua-5.2.4/src/liolib.c.

395 {
396 size_t rlen = LUAL_BUFFERSIZE; /* how much to read in each cycle */
397 luaL_Buffer b;
398 luaL_buffinit(L, &b);
399 for (;;) {
400 char *p = luaL_prepbuffsize(&b, rlen);
401 size_t nr = fread(p, sizeof(char), rlen, f);
402 luaL_addsize(&b, nr);
403 if (nr < rlen) break; /* eof? */
404 else if (rlen <= (MAX_SIZE_T / 4)) /* avoid buffers too large */
405 rlen *= 2; /* double buffer size at each iteration */
406 }
407 luaL_pushresult(&b); /* close buffer */
408}
LUALIB_API void luaL_buffinit(lua_State *L, luaL_Buffer *B)
LUALIB_API void luaL_pushresult(luaL_Buffer *B)
#define luaL_addsize(B, n)
LUALIB_API char * luaL_prepbuffsize(luaL_Buffer *B, size_t sz)
#define MAX_SIZE_T
size_t fread(void *, size_t, size_t, FILE *)

References fread(), luaL_addsize, LUAL_BUFFERSIZE, luaL_buffinit(), luaL_prepbuffsize(), luaL_pushresult(), and MAX_SIZE_T.

Referenced by g_read().

◆ read_chars()

static int read_chars ( lua_State * L,
FILE * f,
size_t n )
static

Definition at line 411 of file lua-5.2.4/src/liolib.c.

411 {
412 size_t nr; /* number of chars actually read */
413 char *p;
414 luaL_Buffer b;
415 luaL_buffinit(L, &b);
416 p = luaL_prepbuffsize(&b, n); /* prepare buffer to read whole block */
417 nr = fread(p, sizeof(char), n, f); /* try to read 'n' chars */
418 luaL_addsize(&b, nr);
419 luaL_pushresult(&b); /* close buffer */
420 return (nr > 0); /* true iff read something */
421}

References fread(), luaL_addsize, luaL_buffinit(), luaL_prepbuffsize(), and luaL_pushresult().

Referenced by g_read().

◆ read_line()

static int read_line ( lua_State * L,
FILE * f,
int chop )
static

Definition at line 371 of file lua-5.2.4/src/liolib.c.

371 {
372 luaL_Buffer b;
373 luaL_buffinit(L, &b);
374 for (;;) {
375 size_t l;
376 char *p = luaL_prepbuffer(&b);
377 if (fgets(p, LUAL_BUFFERSIZE, f) == NULL) { /* eof? */
378 luaL_pushresult(&b); /* close buffer */
379 return (lua_rawlen(L, -1) > 0); /* check whether read something */
380 }
381 l = strlen(p);
382 if (l == 0 || p[l-1] != '\n')
383 luaL_addsize(&b, l);
384 else {
385 luaL_addsize(&b, l - chop); /* chop 'eol' if needed */
386 luaL_pushresult(&b); /* close buffer */
387 return 1; /* read at least an `eol' */
388 }
389 }
390}
#define lua_rawlen(L, index)
Definition LuaXML_lib.c:42
#define luaL_prepbuffer(B)

References lua_rawlen, luaL_addsize, LUAL_BUFFERSIZE, luaL_buffinit(), luaL_prepbuffer, luaL_pushresult(), and NULL.

Referenced by g_read().

◆ read_number()

static int read_number ( lua_State * L,
FILE * f )
static

Definition at line 350 of file lua-5.2.4/src/liolib.c.

350 {
351 lua_Number d;
352 if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1) {
353 lua_pushnumber(L, d);
354 return 1;
355 }
356 else {
357 lua_pushnil(L); /* "result" to be removed */
358 return 0; /* read fails */
359 }
360}
#define LUA_NUMBER_SCAN

References LUA_NUMBER_SCAN, lua_pushnil(), and lua_pushnumber().

Referenced by g_read().

◆ test_eof()

static int test_eof ( lua_State * L,
FILE * f )
static

Definition at line 363 of file lua-5.2.4/src/liolib.c.

363 {
364 int c = getc(f);
365 ungetc(c, f);
366 lua_pushlstring(L, NULL, 0);
367 return (c != EOF);
368}
LUA_API void lua_pushlstring(lua_State *L, const char *s, size_t len)

References lua_pushlstring(), and NULL.

Referenced by g_read().

◆ tofile()

static FILE * tofile ( lua_State * L)
static

Definition at line 154 of file lua-5.2.4/src/liolib.c.

154 {
155 LStream *p = tolstream(L);
156 if (isclosed(p))
157 luaL_error(L, "attempt to use a closed file");
158 lua_assert(p->f);
159 return p->f;
160}

References luaL_Stream::f, isclosed, lua_assert, luaL_error(), and tolstream.

Referenced by f_flush(), f_lines(), f_read(), f_seek(), f_setvbuf(), f_write(), g_iofile(), io_close(), and io_lines().

Variable Documentation

◆ flib

const luaL_Reg flib[]
static
Initial value:
= {
{"close", io_close},
{"flush", f_flush},
{"lines", f_lines},
{"read", f_read},
{"seek", f_seek},
{"setvbuf", f_setvbuf},
{"write", f_write},
{"__gc", f_gc},
{"__tostring", f_tostring},
}
static int io_close(lua_State *L)
static int f_gc(lua_State *L)
static int f_lines(lua_State *L)
static int f_setvbuf(lua_State *L)
static int f_seek(lua_State *L)
static int f_flush(lua_State *L)
static int f_write(lua_State *L)
static int f_read(lua_State *L)
static int f_tostring(lua_State *L)

Definition at line 609 of file lua-5.2.4/src/liolib.c.

609 {
610 {"close", io_close},
611 {"flush", f_flush},
612 {"lines", f_lines},
613 {"read", f_read},
614 {"seek", f_seek},
615 {"setvbuf", f_setvbuf},
616 {"write", f_write},
617 {"__gc", f_gc},
618 {"__tostring", f_tostring},
619 {NULL, NULL}
620};

Referenced by createmeta().

◆ iolib

const luaL_Reg iolib[]
static
Initial value:
= {
{"close", io_close},
{"flush", io_flush},
{"input", io_input},
{"lines", io_lines},
{"open", io_open},
{"output", io_output},
{"popen", io_popen},
{"read", io_read},
{"tmpfile", io_tmpfile},
{"type", io_type},
{"write", io_write},
}
static int io_tmpfile(lua_State *L)
static int io_type(lua_State *L)
static int io_flush(lua_State *L)
static int io_output(lua_State *L)
static int io_read(lua_State *L)
static int io_input(lua_State *L)
static int io_popen(lua_State *L)
static int io_lines(lua_State *L)
static int io_open(lua_State *L)
static int io_write(lua_State *L)

Definition at line 590 of file lua-5.2.4/src/liolib.c.

590 {
591 {"close", io_close},
592 {"flush", io_flush},
593 {"input", io_input},
594 {"lines", io_lines},
595 {"open", io_open},
596 {"output", io_output},
597 {"popen", io_popen},
598 {"read", io_read},
599 {"tmpfile", io_tmpfile},
600 {"type", io_type},
601 {"write", io_write},
602 {NULL, NULL}
603};

Referenced by luaopen_io().