Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
liolib.c File Reference
#include "lprefix.h"
#include <ctype.h>
#include <errno.h>
#include <locale.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.

Data Structures

struct  RN
 

Macros

#define liolib_c
 
#define LUA_LIB
 
#define L_MODEEXT   "b"
 
#define l_popen(L, c, m)
 
#define l_pclose(L, file)   ((void)L, (void)file, -1)
 
#define l_getc(f)   getc(f)
 
#define l_lockfile(f)   ((void)0)
 
#define l_unlockfile(f)   ((void)0)
 
#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 IOPREF_LEN   (sizeof(IO_PREFIX)/sizeof(char) - 1)
 
#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 MAXARGLINE   250
 
#define L_MAXLENNUM   200
 

Typedefs

typedef luaL_Stream LStream
 

Functions

static int l_checkmode (const char *mode)
 
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 f_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 nextc (RN *rn)
 
static int test2 (RN *rn, const char *set)
 
static int readdigits (RN *rn, int hex)
 
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

◆ IO_INPUT

#define IO_INPUT   (IO_PREFIX "input")

Definition at line 138 of file lua-5.3.6/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 139 of file lua-5.3.6/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 136 of file lua-5.3.6/src/liolib.c.

◆ IOPREF_LEN

#define IOPREF_LEN   (sizeof(IO_PREFIX)/sizeof(char) - 1)

Definition at line 137 of file lua-5.3.6/src/liolib.c.

Referenced by getiofile().

◆ isclosed

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

Definition at line 147 of file lua-5.3.6/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 125 of file lua-5.3.6/src/liolib.c.

Referenced by f_seek().

◆ l_ftell

#define l_ftell ( f)    ftell(f)

Definition at line 126 of file lua-5.3.6/src/liolib.c.

Referenced by f_seek().

◆ l_getc

#define l_getc ( f)    getc(f)

Definition at line 90 of file lua-5.3.6/src/liolib.c.

Referenced by nextc(), read_line(), and read_number().

◆ l_lockfile

#define l_lockfile ( f)    ((void)0)

Definition at line 91 of file lua-5.3.6/src/liolib.c.

Referenced by read_line(), and read_number().

◆ L_MAXLENNUM

#define L_MAXLENNUM   200

Definition at line 387 of file lua-5.3.6/src/liolib.c.

Referenced by nextc().

◆ L_MODEEXT

#define L_MODEEXT   "b"

Definition at line 36 of file lua-5.3.6/src/liolib.c.

Referenced by l_checkmode().

◆ l_pclose

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

Definition at line 74 of file lua-5.3.6/src/liolib.c.

Referenced by io_pclose().

◆ l_popen

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

Definition at line 70 of file lua-5.3.6/src/liolib.c.

70#define l_popen(L,c,m) \
71 ((void)((void)c, m), \
72 luaL_error(L, "'popen' not supported"), \
73 (FILE*)0)

Referenced by io_popen().

◆ l_seeknum

#define l_seeknum   long

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

Referenced by f_seek().

◆ l_unlockfile

#define l_unlockfile ( f)    ((void)0)

Definition at line 92 of file lua-5.3.6/src/liolib.c.

Referenced by read_line(), and read_number().

◆ liolib_c

#define liolib_c

Definition at line 7 of file lua-5.3.6/src/liolib.c.

◆ LUA_LIB

#define LUA_LIB

Definition at line 8 of file lua-5.3.6/src/liolib.c.

◆ MAXARGLINE

#define MAXARGLINE   250

Definition at line 339 of file lua-5.3.6/src/liolib.c.

Referenced by aux_lines().

◆ tolstream

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

Typedef Documentation

◆ LStream

Definition at line 142 of file lua-5.3.6/src/liolib.c.

Function Documentation

◆ aux_close()

static int aux_close ( lua_State * L)
static

Definition at line 201 of file lua-5.3.6/src/liolib.c.

201 {
202 LStream *p = tolstream(L);
203 volatile lua_CFunction cf = p->closef;
204 p->closef = NULL; /* mark stream as closed */
205 return (*cf)(L); /* close it */
206}
#define NULL
Definition gmacros.h:924
int(* lua_CFunction)(lua_State *L)
#define tolstream(L)
lua_CFunction closef

References luaL_Stream::closef, NULL, and tolstream.

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

◆ aux_lines()

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

Definition at line 341 of file lua-5.3.6/src/liolib.c.

341 {
342 int n = lua_gettop(L) - 1; /* number of arguments to read */
343 luaL_argcheck(L, n <= MAXARGLINE, MAXARGLINE + 2, "too many arguments");
344 lua_pushinteger(L, n); /* number of arguments to read */
345 lua_pushboolean(L, toclose); /* close/not close file when finished */
346 lua_rotate(L, 2, 2); /* move 'n' and 'toclose' to their positions */
347 lua_pushcclosure(L, io_readline, 3 + n);
348}
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_pushinteger(lua_State *L, lua_Integer n)
LUA_API int lua_gettop(lua_State *L)
#define luaL_argcheck(L, cond, numarg, extramsg)
LUA_API void lua_rotate(lua_State *L, int idx, int n)
#define MAXARGLINE
static int io_readline(lua_State *L)

References io_readline(), lua_gettop(), lua_pushboolean(), lua_pushcclosure(), lua_pushinteger(), lua_rotate(), luaL_argcheck, and MAXARGLINE.

Referenced by f_lines(), and io_lines().

◆ createmeta()

static void createmeta ( lua_State * L)
static

Definition at line 735 of file lua-5.3.6/src/liolib.c.

735 {
736 luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */
737 lua_pushvalue(L, -1); /* push metatable */
738 lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
739 luaL_setfuncs(L, flib, 0); /* add file methods to new metatable */
740 lua_pop(L, 1); /* pop new metatable */
741}
LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
LUA_API void lua_pushvalue(lua_State *L, int idx)
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 756 of file lua-5.3.6/src/liolib.c.

757 {
758 LStream *p = newprefile(L);
759 p->f = f;
760 p->closef = &io_noclose;
761 if (k != NULL) {
762 lua_pushvalue(L, -1);
763 lua_setfield(L, LUA_REGISTRYINDEX, k); /* add file to registry */
764 }
765 lua_setfield(L, -2, fname); /* add file to module */
766}
#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_close()

static int f_close ( lua_State * L)
static

Definition at line 209 of file lua-5.3.6/src/liolib.c.

209 {
210 tofile(L); /* make sure argument is an open stream */
211 return aux_close(L);
212}
static int aux_close(lua_State *L)
static FILE * tofile(lua_State *L)

References aux_close(), and tofile().

Referenced by io_close().

◆ f_flush()

static int f_flush ( lua_State * L)
static

Definition at line 694 of file lua-5.3.6/src/liolib.c.

694 {
695 return luaL_fileresult(L, fflush(tofile(L)) == 0, NULL);
696}
LUALIB_API int luaL_fileresult(lua_State *L, int stat, const char *fname)

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

◆ f_gc()

static int f_gc ( lua_State * L)
static

Definition at line 222 of file lua-5.3.6/src/liolib.c.

222 {
223 LStream *p = tolstream(L);
224 if (!isclosed(p) && p->f != NULL)
225 aux_close(L); /* ignore closed and incompletely open files */
226 return 0;
227}
#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 351 of file lua-5.3.6/src/liolib.c.

351 {
352 tofile(L); /* check that it's a valid file handle */
353 aux_lines(L, 0);
354 return 1;
355}
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 586 of file lua-5.3.6/src/liolib.c.

586 {
587 return g_read(L, tofile(L), 2);
588}
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 658 of file lua-5.3.6/src/liolib.c.

658 {
659 static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
660 static const char *const modenames[] = {"set", "cur", "end", NULL};
661 FILE *f = tofile(L);
662 int op = luaL_checkoption(L, 2, "cur", modenames);
663 lua_Integer p3 = luaL_optinteger(L, 3, 0);
664 l_seeknum offset = (l_seeknum)p3;
665 luaL_argcheck(L, (lua_Integer)offset == p3, 3,
666 "not an integer in proper range");
667 op = l_fseek(f, offset, mode[op]);
668 if (op)
669 return luaL_fileresult(L, 0, NULL); /* error */
670 else {
672 return 1;
673 }
674}
LUALIB_API lua_Integer luaL_optinteger(lua_State *L, int narg, lua_Integer def)
LUALIB_API int luaL_checkoption(lua_State *L, int narg, const char *def, const char *const lst[])
LUA_INTEGER lua_Integer
#define l_seeknum
#define l_fseek(f, o, w)
#define l_ftell(f)

References l_fseek, l_ftell, l_seeknum, lua_pushinteger(), luaL_argcheck, luaL_checkoption(), luaL_fileresult(), luaL_optinteger(), NULL, and tofile().

◆ f_setvbuf()

static int f_setvbuf ( lua_State * L)
static

Definition at line 677 of file lua-5.3.6/src/liolib.c.

677 {
678 static const int mode[] = {_IONBF, _IOFBF, _IOLBF};
679 static const char *const modenames[] = {"no", "full", "line", NULL};
680 FILE *f = tofile(L);
681 int op = luaL_checkoption(L, 2, NULL, modenames);
683 int res = setvbuf(f, NULL, mode[op], (size_t)sz);
684 return luaL_fileresult(L, res == 0, NULL);
685}
#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 164 of file lua-5.3.6/src/liolib.c.

164 {
165 LStream *p = tolstream(L);
166 if (isclosed(p))
167 lua_pushliteral(L, "file (closed)");
168 else
169 lua_pushfstring(L, "file (%p)", p->f);
170 return 1;
171}
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 651 of file lua-5.3.6/src/liolib.c.

651 {
652 FILE *f = tofile(L);
653 lua_pushvalue(L, 1); /* push file at the stack top (to be returned) */
654 return g_write(L, f, 2);
655}
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 305 of file lua-5.3.6/src/liolib.c.

305 {
306 if (!lua_isnoneornil(L, 1)) {
307 const char *filename = lua_tostring(L, 1);
308 if (filename)
309 opencheck(L, filename, mode);
310 else {
311 tofile(L); /* check that it's a valid file handle */
312 lua_pushvalue(L, 1);
313 }
315 }
316 /* return current value */
318 return 1;
319}
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 531 of file lua-5.3.6/src/liolib.c.

531 {
532 int nargs = lua_gettop(L) - 1;
533 int success;
534 int n;
535 clearerr(f);
536 if (nargs == 0) { /* no arguments? */
537 success = read_line(L, f, 1);
538 n = first+1; /* to return 1 result */
539 }
540 else { /* ensure stack space for all results and for auxlib's buffer */
541 luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments");
542 success = 1;
543 for (n = first; nargs-- && success; n++) {
544 if (lua_type(L, n) == LUA_TNUMBER) {
545 size_t l = (size_t)luaL_checkinteger(L, n);
546 success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
547 }
548 else {
549 const char *p = luaL_checkstring(L, n);
550 if (*p == '*') p++; /* skip optional '*' (for compatibility) */
551 switch (*p) {
552 case 'n': /* number */
553 success = read_number(L, f);
554 break;
555 case 'l': /* line */
556 success = read_line(L, f, 1);
557 break;
558 case 'L': /* line with end-of-line */
559 success = read_line(L, f, 0);
560 break;
561 case 'a': /* file */
562 read_all(L, f); /* read entire file */
563 success = 1; /* always success */
564 break;
565 default:
566 return luaL_argerror(L, n, "invalid format");
567 }
568 }
569 }
570 }
571 if (ferror(f))
572 return luaL_fileresult(L, 0, NULL);
573 if (!success) {
574 lua_pop(L, 1); /* remove last result */
575 lua_pushnil(L); /* push nil instead */
576 }
577 return n - first;
578}
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 lua_Integer luaL_checkinteger(lua_State *L, int narg)
LUALIB_API int luaL_argerror(lua_State *L, int narg, const char *extramsg)
#define luaL_checkstring(L, n)
#define LUA_TNUMBER
#define LUA_MINSTACK
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)

References lua_gettop(), LUA_MINSTACK, lua_pop, lua_pushnil(), LUA_TNUMBER, lua_type(), luaL_argerror(), luaL_checkinteger(), luaL_checkstack(), luaL_checkstring, 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 622 of file lua-5.3.6/src/liolib.c.

622 {
623 int nargs = lua_gettop(L) - arg;
624 int status = 1;
625 for (; nargs--; arg++) {
626 if (lua_type(L, arg) == LUA_TNUMBER) {
627 /* optimization: could be done exactly as for strings */
628 int len = lua_isinteger(L, arg)
629 ? fprintf(f, LUA_INTEGER_FMT,
631 : fprintf(f, LUA_NUMBER_FMT,
633 status = status && (len > 0);
634 }
635 else {
636 size_t l;
637 const char *s = luaL_checklstring(L, arg, &l);
638 status = status && (fwrite(s, sizeof(char), l, f) == l);
639 }
640 }
641 if (status) return 1; /* file handle already on stack top */
642 else return luaL_fileresult(L, status, NULL);
643}
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 LUAI_UACNUMBER
#define lua_tointeger(L, i)
#define lua_tonumber(L, i)
LUA_API int lua_isinteger(lua_State *L, int idx)
#define LUA_INTEGER_FMT
#define LUAI_UACINT
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_INTEGER_FMT, lua_isinteger(), LUA_NUMBER_FMT, LUA_TNUMBER, lua_tointeger, lua_tonumber, lua_type(), LUAI_UACINT, LUAI_UACNUMBER, 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 295 of file lua-5.3.6/src/liolib.c.

295 {
296 LStream *p;
298 p = (LStream *)lua_touserdata(L, -1);
299 if (isclosed(p))
300 luaL_error(L, "standard %s file is closed", findex + IOPREF_LEN);
301 return p->f;
302}
LUA_API void * lua_touserdata(lua_State *L, int idx)
#define IOPREF_LEN

References luaL_Stream::f, IOPREF_LEN, 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 215 of file lua-5.3.6/src/liolib.c.

215 {
216 if (lua_isnone(L, 1)) /* no argument? */
217 lua_getfield(L, LUA_REGISTRYINDEX, IO_OUTPUT); /* use standard output */
218 return f_close(L);
219}
#define lua_isnone(L, n)
static int f_close(lua_State *L)
#define IO_OUTPUT

References f_close(), IO_OUTPUT, lua_getfield(), lua_isnone, and LUA_REGISTRYINDEX.

◆ io_fclose()

static int io_fclose ( lua_State * L)
static

Definition at line 233 of file lua-5.3.6/src/liolib.c.

233 {
234 LStream *p = tolstream(L);
235 int res = fclose(p->f);
236 return luaL_fileresult(L, (res == 0), NULL);
237}

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 689 of file lua-5.3.6/src/liolib.c.

689 {
690 return luaL_fileresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL);
691}
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 322 of file lua-5.3.6/src/liolib.c.

322 {
323 return g_iofile(L, IO_INPUT, "r");
324}
#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 358 of file lua-5.3.6/src/liolib.c.

358 {
359 int toclose;
360 if (lua_isnone(L, 1)) lua_pushnil(L); /* at least one argument */
361 if (lua_isnil(L, 1)) { /* no file name? */
362 lua_getfield(L, LUA_REGISTRYINDEX, IO_INPUT); /* get default input */
363 lua_replace(L, 1); /* put it at index 1 */
364 tofile(L); /* check that it's a valid file handle */
365 toclose = 0; /* do not close it after iteration */
366 }
367 else { /* open a new file */
368 const char *filename = luaL_checkstring(L, 1);
369 opencheck(L, filename, "r");
370 lua_replace(L, 1); /* put file at index 1 */
371 toclose = 1; /* close it after iteration */
372 }
373 aux_lines(L, toclose);
374 return 1;
375}
#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 747 of file lua-5.3.6/src/liolib.c.

747 {
748 LStream *p = tolstream(L);
749 p->closef = &io_noclose; /* keep file opened */
750 lua_pushnil(L);
751 lua_pushliteral(L, "cannot close standard file");
752 return 2;
753}

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 256 of file lua-5.3.6/src/liolib.c.

256 {
257 const char *filename = luaL_checkstring(L, 1);
258 const char *mode = luaL_optstring(L, 2, "r");
259 LStream *p = newfile(L);
260 const char *md = mode; /* to traverse/check mode */
261 luaL_argcheck(L, l_checkmode(md), 2, "invalid mode");
262 p->f = fopen(filename, mode);
263 return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1;
264}
#define luaL_optstring(L, n, d)
static int l_checkmode(const char *mode)
static LStream * newfile(lua_State *L)

References luaL_Stream::f, l_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 327 of file lua-5.3.6/src/liolib.c.

327 {
328 return g_iofile(L, IO_OUTPUT, "w");
329}

References g_iofile(), and IO_OUTPUT.

◆ io_pclose()

static int io_pclose ( lua_State * L)
static

Definition at line 270 of file lua-5.3.6/src/liolib.c.

270 {
271 LStream *p = tolstream(L);
272 return luaL_execresult(L, l_pclose(L, p->f));
273}
LUALIB_API int luaL_execresult(lua_State *L, int stat)
#define l_pclose(L, file)

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

Referenced by io_popen().

◆ io_popen()

static int io_popen ( lua_State * L)
static

Definition at line 276 of file lua-5.3.6/src/liolib.c.

276 {
277 const char *filename = luaL_checkstring(L, 1);
278 const char *mode = luaL_optstring(L, 2, "r");
279 LStream *p = newprefile(L);
280 luaL_argcheck(L, ((mode[0] == 'r' || mode[0] == 'w') && mode[1] == '\0'),
281 2, "invalid mode");
282 p->f = l_popen(L, filename, mode);
283 p->closef = &io_pclose;
284 return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1;
285}
#define l_popen(L, c, m)
static int io_pclose(lua_State *L)

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

◆ io_read()

static int io_read ( lua_State * L)
static

Definition at line 581 of file lua-5.3.6/src/liolib.c.

581 {
582 return g_read(L, getiofile(L, IO_INPUT), 1);
583}

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

◆ io_readline()

static int io_readline ( lua_State * L)
static

Definition at line 591 of file lua-5.3.6/src/liolib.c.

591 {
593 int i;
594 int n = (int)lua_tointeger(L, lua_upvalueindex(2));
595 if (isclosed(p)) /* file is already closed? */
596 return luaL_error(L, "file is already closed");
597 lua_settop(L , 1);
598 luaL_checkstack(L, n, "too many arguments");
599 for (i = 1; i <= n; i++) /* push arguments to 'g_read' */
601 n = g_read(L, p->f, 2); /* 'n' is number of results */
602 lua_assert(n > 0); /* should return at least a nil */
603 if (lua_toboolean(L, -n)) /* read at least one value? */
604 return n; /* return them */
605 else { /* first result is nil: EOF or error */
606 if (n > 1) { /* is there error information? */
607 /* 2nd result is error message */
608 return luaL_error(L, "%s", lua_tostring(L, -n + 1));
609 }
610 if (lua_toboolean(L, lua_upvalueindex(3))) { /* generator created file? */
611 lua_settop(L, 0);
613 aux_close(L); /* close it */
614 }
615 return 0;
616 }
617}
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_pushvalue(), lua_settop(), lua_toboolean(), lua_tointeger, lua_tostring, lua_touserdata(), lua_upvalueindex, luaL_checkstack(), and luaL_error().

Referenced by aux_lines().

◆ io_tmpfile()

static int io_tmpfile ( lua_State * L)
static

Definition at line 288 of file lua-5.3.6/src/liolib.c.

288 {
289 LStream *p = newfile(L);
290 p->f = tmpfile();
291 return (p->f == NULL) ? luaL_fileresult(L, 0, NULL) : 1;
292}

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

◆ io_type()

static int io_type ( lua_State * L)
static

Definition at line 150 of file lua-5.3.6/src/liolib.c.

150 {
151 LStream *p;
152 luaL_checkany(L, 1);
154 if (p == NULL)
155 lua_pushnil(L); /* not a file */
156 else if (isclosed(p))
157 lua_pushliteral(L, "closed file");
158 else
159 lua_pushliteral(L, "file");
160 return 1;
161}
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 646 of file lua-5.3.6/src/liolib.c.

646 {
647 return g_write(L, getiofile(L, IO_OUTPUT), 1);
648}

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

◆ l_checkmode()

static int l_checkmode ( const char * mode)
static

Definition at line 40 of file lua-5.3.6/src/liolib.c.

40 {
41 return (*mode != '\0' && strchr("rwa", *(mode++)) != NULL &&
42 (*mode != '+' || (++mode, 1)) && /* skip if char is '+' */
43 (strspn(mode, L_MODEEXT) == strlen(mode))); /* check extensions */
44}
#define L_MODEEXT

References L_MODEEXT, and NULL.

Referenced by io_open().

◆ luaopen_io()

LUAMOD_API int luaopen_io ( lua_State * L)

Definition at line 769 of file lua-5.3.6/src/liolib.c.

769 {
770 luaL_newlib(L, iolib); /* new module */
771 createmeta(L);
772 /* create (and set) default files */
773 createstdfile(L, stdin, IO_INPUT, "stdin");
774 createstdfile(L, stdout, IO_OUTPUT, "stdout");
775 createstdfile(L, stderr, NULL, "stderr");
776 return 1;
777}
#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 240 of file lua-5.3.6/src/liolib.c.

240 {
241 LStream *p = newprefile(L);
242 p->f = NULL;
243 p->closef = &io_fclose;
244 return p;
245}
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 188 of file lua-5.3.6/src/liolib.c.

188 {
189 LStream *p = (LStream *)lua_newuserdata(L, sizeof(LStream));
190 p->closef = NULL; /* mark file handle as 'closed' */
192 return p;
193}
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().

◆ nextc()

static int nextc ( RN * rn)
static

Definition at line 403 of file lua-5.3.6/src/liolib.c.

403 {
404 if (rn->n >= L_MAXLENNUM) { /* buffer overflow? */
405 rn->buff[0] = '\0'; /* invalidate result */
406 return 0; /* fail */
407 }
408 else {
409 rn->buff[rn->n++] = rn->c; /* save current char */
410 rn->c = l_getc(rn->f); /* read next one */
411 return 1;
412 }
413}
#define l_getc(f)
#define L_MAXLENNUM
char buff[L_MAXLENNUM+1]

References RN::buff, RN::c, RN::f, l_getc, L_MAXLENNUM, and RN::n.

Referenced by readdigits(), and test2().

◆ opencheck()

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

Definition at line 248 of file lua-5.3.6/src/liolib.c.

248 {
249 LStream *p = newfile(L);
250 p->f = fopen(fname, mode);
251 if (p->f == NULL)
252 luaL_error(L, "cannot open file '%s' (%s)", fname, strerror(errno));
253}

References luaL_Stream::f, 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 505 of file lua-5.3.6/src/liolib.c.

505 {
506 size_t nr;
507 luaL_Buffer b;
508 luaL_buffinit(L, &b);
509 do { /* read file in chunks of LUAL_BUFFERSIZE bytes */
510 char *p = luaL_prepbuffer(&b);
511 nr = fread(p, sizeof(char), LUAL_BUFFERSIZE, f);
512 luaL_addsize(&b, nr);
513 } while (nr == LUAL_BUFFERSIZE);
514 luaL_pushresult(&b); /* close buffer */
515}
LUALIB_API void luaL_buffinit(lua_State *L, luaL_Buffer *B)
LUALIB_API void luaL_pushresult(luaL_Buffer *B)
#define luaL_addsize(B, n)
#define luaL_prepbuffer(B)
size_t fread(void *, size_t, size_t, FILE *)

References fread(), luaL_addsize, LUAL_BUFFERSIZE, luaL_buffinit(), luaL_prepbuffer, and luaL_pushresult().

Referenced by g_read().

◆ read_chars()

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

Definition at line 518 of file lua-5.3.6/src/liolib.c.

518 {
519 size_t nr; /* number of chars actually read */
520 char *p;
521 luaL_Buffer b;
522 luaL_buffinit(L, &b);
523 p = luaL_prepbuffsize(&b, n); /* prepare buffer to read whole block */
524 nr = fread(p, sizeof(char), n, f); /* try to read 'n' chars */
525 luaL_addsize(&b, nr);
526 luaL_pushresult(&b); /* close buffer */
527 return (nr > 0); /* true iff read something */
528}
LUALIB_API char * luaL_prepbuffsize(luaL_Buffer *B, size_t sz)

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 484 of file lua-5.3.6/src/liolib.c.

484 {
485 luaL_Buffer b;
486 int c = '\0';
487 luaL_buffinit(L, &b);
488 while (c != EOF && c != '\n') { /* repeat until end of line */
489 char *buff = luaL_prepbuffer(&b); /* preallocate buffer */
490 int i = 0;
491 l_lockfile(f); /* no memory errors can happen inside the lock */
492 while (i < LUAL_BUFFERSIZE && (c = l_getc(f)) != EOF && c != '\n')
493 buff[i++] = c;
494 l_unlockfile(f);
495 luaL_addsize(&b, i);
496 }
497 if (!chop && c == '\n') /* want a newline and have one? */
498 luaL_addchar(&b, c); /* add ending newline to result */
499 luaL_pushresult(&b); /* close buffer */
500 /* return ok if read something (either a newline or something else) */
501 return (c == '\n' || lua_rawlen(L, -1) > 0);
502}
#define lua_rawlen(L, index)
Definition LuaXML_lib.c:42
#define luaL_addchar(B, c)
#define l_unlockfile(f)
#define l_lockfile(f)

References l_getc, l_lockfile, l_unlockfile, lua_rawlen, luaL_addchar, luaL_addsize, LUAL_BUFFERSIZE, luaL_buffinit(), luaL_prepbuffer, and luaL_pushresult().

Referenced by g_read().

◆ read_number()

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

Definition at line 442 of file lua-5.3.6/src/liolib.c.

442 {
443 RN rn;
444 int count = 0;
445 int hex = 0;
446 char decp[2];
447 rn.f = f; rn.n = 0;
448 decp[0] = lua_getlocaledecpoint(); /* get decimal point from locale */
449 decp[1] = '.'; /* always accept a dot */
450 l_lockfile(rn.f);
451 do { rn.c = l_getc(rn.f); } while (isspace(rn.c)); /* skip spaces */
452 test2(&rn, "-+"); /* optional signal */
453 if (test2(&rn, "00")) {
454 if (test2(&rn, "xX")) hex = 1; /* numeral is hexadecimal */
455 else count = 1; /* count initial '0' as a valid digit */
456 }
457 count += readdigits(&rn, hex); /* integral part */
458 if (test2(&rn, decp)) /* decimal point? */
459 count += readdigits(&rn, hex); /* fractional part */
460 if (count > 0 && test2(&rn, (hex ? "pP" : "eE"))) { /* exponent mark? */
461 test2(&rn, "-+"); /* exponent signal */
462 readdigits(&rn, 0); /* exponent digits */
463 }
464 ungetc(rn.c, rn.f); /* unread look-ahead char */
465 l_unlockfile(rn.f);
466 rn.buff[rn.n] = '\0'; /* finish string */
467 if (lua_stringtonumber(L, rn.buff)) /* is this a valid number? */
468 return 1; /* ok */
469 else { /* invalid format */
470 lua_pushnil(L); /* "result" to be removed */
471 return 0; /* read fails */
472 }
473}
LUA_API size_t lua_stringtonumber(lua_State *L, const char *s)
static int test2(RN *rn, const char *set)
static int readdigits(RN *rn, int hex)
#define lua_getlocaledecpoint()

References RN::buff, RN::c, RN::f, l_getc, l_lockfile, l_unlockfile, lua_getlocaledecpoint, lua_pushnil(), lua_stringtonumber(), RN::n, readdigits(), and test2().

Referenced by g_read().

◆ readdigits()

static int readdigits ( RN * rn,
int hex )
static

Definition at line 429 of file lua-5.3.6/src/liolib.c.

429 {
430 int count = 0;
431 while ((hex ? isxdigit(rn->c) : isdigit(rn->c)) && nextc(rn))
432 count++;
433 return count;
434}
static int nextc(RN *rn)

References RN::c, and nextc().

Referenced by read_number().

◆ test2()

static int test2 ( RN * rn,
const char * set )
static

Definition at line 419 of file lua-5.3.6/src/liolib.c.

419 {
420 if (rn->c == set[0] || rn->c == set[1])
421 return nextc(rn);
422 else return 0;
423}

References RN::c, and nextc().

Referenced by read_number().

◆ test_eof()

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

Definition at line 476 of file lua-5.3.6/src/liolib.c.

476 {
477 int c = getc(f);
478 ungetc(c, f); /* no-op when c == EOF */
479 lua_pushliteral(L, "");
480 return (c != EOF);
481}

References lua_pushliteral.

Referenced by g_read().

◆ tofile()

static FILE * tofile ( lua_State * L)
static

Definition at line 174 of file lua-5.3.6/src/liolib.c.

174 {
175 LStream *p = tolstream(L);
176 if (isclosed(p))
177 luaL_error(L, "attempt to use a closed file");
178 lua_assert(p->f);
179 return p->f;
180}

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

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

Variable Documentation

◆ flib

const luaL_Reg flib[]
static
Initial value:
= {
{"close", f_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 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 721 of file lua-5.3.6/src/liolib.c.

721 {
722 {"close", f_close},
723 {"flush", f_flush},
724 {"lines", f_lines},
725 {"read", f_read},
726 {"seek", f_seek},
727 {"setvbuf", f_setvbuf},
728 {"write", f_write},
729 {"__gc", f_gc},
730 {"__tostring", f_tostring},
731 {NULL, NULL}
732};

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_close(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 702 of file lua-5.3.6/src/liolib.c.

702 {
703 {"close", io_close},
704 {"flush", io_flush},
705 {"input", io_input},
706 {"lines", io_lines},
707 {"open", io_open},
708 {"output", io_output},
709 {"popen", io_popen},
710 {"read", io_read},
711 {"tmpfile", io_tmpfile},
712 {"type", io_type},
713 {"write", io_write},
714 {NULL, NULL}
715};

Referenced by luaopen_io().