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_checkmodep(m)   ((m[0] == 'r' || m[0] == 'w') && m[1] == '\0')
 
#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 meth []
 
static const luaL_Reg metameth []
 

Macro Definition Documentation

◆ IO_INPUT

#define IO_INPUT   (IO_PREFIX "input")

Definition at line 151 of file lua-5.4.3/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 152 of file lua-5.4.3/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 149 of file lua-5.4.3/src/liolib.c.

◆ IOPREF_LEN

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

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

Referenced by getiofile().

◆ isclosed

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

Definition at line 160 of file lua-5.4.3/src/liolib.c.

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

◆ l_checkmodep

#define l_checkmodep ( m)    ((m[0] == 'r' || m[0] == 'w') && m[1] == '\0')

Definition at line 89 of file lua-5.4.3/src/liolib.c.

Referenced by io_popen().

◆ l_fseek

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

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

Referenced by f_seek().

◆ l_ftell

#define l_ftell ( f)    ftell(f)

Definition at line 138 of file lua-5.4.3/src/liolib.c.

Referenced by f_seek().

◆ l_getc

#define l_getc ( f)    getc(f)

Definition at line 102 of file lua-5.4.3/src/liolib.c.

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

◆ l_lockfile

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

Definition at line 103 of file lua-5.4.3/src/liolib.c.

Referenced by read_line(), and read_number().

◆ L_MAXLENNUM

#define L_MAXLENNUM   200

Definition at line 422 of file lua-5.4.3/src/liolib.c.

Referenced by nextc().

◆ L_MODEEXT

#define L_MODEEXT   "b"

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

Referenced by l_checkmode().

◆ l_pclose

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

Definition at line 80 of file lua-5.4.3/src/liolib.c.

Referenced by io_pclose().

◆ l_popen

#define l_popen ( L,
c,
m )
Value:
((void)c, (void)m, \
luaL_error(L, "'popen' not supported"), \
(FILE*)0)

Definition at line 76 of file lua-5.4.3/src/liolib.c.

76#define l_popen(L,c,m) \
77 ((void)c, (void)m, \
78 luaL_error(L, "'popen' not supported"), \
79 (FILE*)0)

Referenced by io_popen().

◆ l_seeknum

#define l_seeknum   long

Definition at line 139 of file lua-5.4.3/src/liolib.c.

Referenced by f_seek().

◆ l_unlockfile

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

Definition at line 104 of file lua-5.4.3/src/liolib.c.

Referenced by read_line(), and read_number().

◆ liolib_c

#define liolib_c

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

◆ LUA_LIB

#define LUA_LIB

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

◆ MAXARGLINE

#define MAXARGLINE   250

Definition at line 352 of file lua-5.4.3/src/liolib.c.

Referenced by aux_lines().

◆ tolstream

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

Typedef Documentation

◆ LStream

Definition at line 155 of file lua-5.4.3/src/liolib.c.

Function Documentation

◆ aux_close()

static int aux_close ( lua_State * L)
static

Definition at line 214 of file lua-5.4.3/src/liolib.c.

214 {
215 LStream *p = tolstream(L);
216 volatile lua_CFunction cf = p->closef;
217 p->closef = NULL; /* mark stream as closed */
218 return (*cf)(L); /* close it */
219}
#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 363 of file lua-5.4.3/src/liolib.c.

363 {
364 int n = lua_gettop(L) - 1; /* number of arguments to read */
365 luaL_argcheck(L, n <= MAXARGLINE, MAXARGLINE + 2, "too many arguments");
366 lua_pushvalue(L, 1); /* file */
367 lua_pushinteger(L, n); /* number of arguments to read */
368 lua_pushboolean(L, toclose); /* close/not close file when finished */
369 lua_rotate(L, 2, 3); /* move the three values to their positions */
370 lua_pushcclosure(L, io_readline, 3 + n);
371}
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)
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_pushvalue(), lua_rotate(), luaL_argcheck, and MAXARGLINE.

Referenced by f_lines(), and io_lines().

◆ createmeta()

static void createmeta ( lua_State * L)
static

Definition at line 784 of file lua-5.4.3/src/liolib.c.

784 {
785 luaL_newmetatable(L, LUA_FILEHANDLE); /* metatable for file handles */
786 luaL_setfuncs(L, metameth, 0); /* add metamethods to new metatable */
787 luaL_newlibtable(L, meth); /* create method table */
788 luaL_setfuncs(L, meth, 0); /* add file methods to method table */
789 lua_setfield(L, -2, "__index"); /* metatable.__index = method table */
790 lua_pop(L, 1); /* pop metatable */
791}
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)
#define luaL_newlibtable(L, l)
static const luaL_Reg meth[]
static const luaL_Reg metameth[]

References LUA_FILEHANDLE, lua_pop, lua_setfield(), luaL_newlibtable, luaL_newmetatable(), luaL_setfuncs(), metameth, and meth.

Referenced by luaopen_io().

◆ createstdfile()

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

Definition at line 806 of file lua-5.4.3/src/liolib.c.

807 {
808 LStream *p = newprefile(L);
809 p->f = f;
810 p->closef = &io_noclose;
811 if (k != NULL) {
812 lua_pushvalue(L, -1);
813 lua_setfield(L, LUA_REGISTRYINDEX, k); /* add file to registry */
814 }
815 lua_setfield(L, -2, fname); /* add file to module */
816}
#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 222 of file lua-5.4.3/src/liolib.c.

222 {
223 tofile(L); /* make sure argument is an open stream */
224 return aux_close(L);
225}
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 733 of file lua-5.4.3/src/liolib.c.

733 {
734 return luaL_fileresult(L, fflush(tofile(L)) == 0, NULL);
735}
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 235 of file lua-5.4.3/src/liolib.c.

235 {
236 LStream *p = tolstream(L);
237 if (!isclosed(p) && p->f != NULL)
238 aux_close(L); /* ignore closed and incompletely open files */
239 return 0;
240}
#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 374 of file lua-5.4.3/src/liolib.c.

374 {
375 tofile(L); /* check that it's a valid file handle */
376 aux_lines(L, 0);
377 return 1;
378}
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 621 of file lua-5.4.3/src/liolib.c.

621 {
622 return g_read(L, tofile(L), 2);
623}
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 697 of file lua-5.4.3/src/liolib.c.

697 {
698 static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
699 static const char *const modenames[] = {"set", "cur", "end", NULL};
700 FILE *f = tofile(L);
701 int op = luaL_checkoption(L, 2, "cur", modenames);
702 lua_Integer p3 = luaL_optinteger(L, 3, 0);
703 l_seeknum offset = (l_seeknum)p3;
704 luaL_argcheck(L, (lua_Integer)offset == p3, 3,
705 "not an integer in proper range");
706 op = l_fseek(f, offset, mode[op]);
707 if (l_unlikely(op))
708 return luaL_fileresult(L, 0, NULL); /* error */
709 else {
711 return 1;
712 }
713}
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 716 of file lua-5.4.3/src/liolib.c.

716 {
717 static const int mode[] = {_IONBF, _IOFBF, _IOLBF};
718 static const char *const modenames[] = {"no", "full", "line", NULL};
719 FILE *f = tofile(L);
720 int op = luaL_checkoption(L, 2, NULL, modenames);
722 int res = setvbuf(f, NULL, mode[op], (size_t)sz);
723 return luaL_fileresult(L, res == 0, NULL);
724}
#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 177 of file lua-5.4.3/src/liolib.c.

177 {
178 LStream *p = tolstream(L);
179 if (isclosed(p))
180 lua_pushliteral(L, "file (closed)");
181 else
182 lua_pushfstring(L, "file (%p)", p->f);
183 return 1;
184}
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 690 of file lua-5.4.3/src/liolib.c.

690 {
691 FILE *f = tofile(L);
692 lua_pushvalue(L, 1); /* push file at the stack top (to be returned) */
693 return g_write(L, f, 2);
694}
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 318 of file lua-5.4.3/src/liolib.c.

318 {
319 if (!lua_isnoneornil(L, 1)) {
320 const char *filename = lua_tostring(L, 1);
321 if (filename)
322 opencheck(L, filename, mode);
323 else {
324 tofile(L); /* check that it's a valid file handle */
325 lua_pushvalue(L, 1);
326 }
328 }
329 /* return current value */
331 return 1;
332}
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 566 of file lua-5.4.3/src/liolib.c.

566 {
567 int nargs = lua_gettop(L) - 1;
568 int n, success;
569 clearerr(f);
570 if (nargs == 0) { /* no arguments? */
571 success = read_line(L, f, 1);
572 n = first + 1; /* to return 1 result */
573 }
574 else {
575 /* ensure stack space for all results and for auxlib's buffer */
576 luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments");
577 success = 1;
578 for (n = first; nargs-- && success; n++) {
579 if (lua_type(L, n) == LUA_TNUMBER) {
580 size_t l = (size_t)luaL_checkinteger(L, n);
581 success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
582 }
583 else {
584 const char *p = luaL_checkstring(L, n);
585 if (*p == '*') p++; /* skip optional '*' (for compatibility) */
586 switch (*p) {
587 case 'n': /* number */
588 success = read_number(L, f);
589 break;
590 case 'l': /* line */
591 success = read_line(L, f, 1);
592 break;
593 case 'L': /* line with end-of-line */
594 success = read_line(L, f, 0);
595 break;
596 case 'a': /* file */
597 read_all(L, f); /* read entire file */
598 success = 1; /* always success */
599 break;
600 default:
601 return luaL_argerror(L, n, "invalid format");
602 }
603 }
604 }
605 }
606 if (ferror(f))
607 return luaL_fileresult(L, 0, NULL);
608 if (!success) {
609 lua_pop(L, 1); /* remove last result */
610 luaL_pushfail(L); /* push nil instead */
611 }
612 return n - first;
613}
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
#define luaL_pushfail(L)
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_TNUMBER, lua_type(), luaL_argerror(), luaL_checkinteger(), luaL_checkstack(), luaL_checkstring, luaL_fileresult(), luaL_pushfail, 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 660 of file lua-5.4.3/src/liolib.c.

660 {
661 int nargs = lua_gettop(L) - arg;
662 int status = 1;
663 for (; nargs--; arg++) {
664 if (lua_type(L, arg) == LUA_TNUMBER) {
665 /* optimization: could be done exactly as for strings */
666 int len = lua_isinteger(L, arg)
667 ? fprintf(f, LUA_INTEGER_FMT,
669 : fprintf(f, LUA_NUMBER_FMT,
671 status = status && (len > 0);
672 }
673 else {
674 size_t l;
675 const char *s = luaL_checklstring(L, arg, &l);
676 status = status && (fwrite(s, sizeof(char), l, f) == l);
677 }
678 }
679 if (l_likely(status))
680 return 1; /* file handle already on stack top */
681 else return luaL_fileresult(L, status, NULL);
682}
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 308 of file lua-5.4.3/src/liolib.c.

308 {
309 LStream *p;
311 p = (LStream *)lua_touserdata(L, -1);
312 if (l_unlikely(isclosed(p)))
313 luaL_error(L, "default %s file is closed", findex + IOPREF_LEN);
314 return p->f;
315}
LUA_API void * lua_touserdata(lua_State *L, int idx)
LUALIB_API int luaL_error(lua_State *L, const char *fmt,...)
#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 228 of file lua-5.4.3/src/liolib.c.

228 {
229 if (lua_isnone(L, 1)) /* no argument? */
230 lua_getfield(L, LUA_REGISTRYINDEX, IO_OUTPUT); /* use default output */
231 return f_close(L);
232}
#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 246 of file lua-5.4.3/src/liolib.c.

246 {
247 LStream *p = tolstream(L);
248 int res = fclose(p->f);
249 return luaL_fileresult(L, (res == 0), NULL);
250}

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

728 {
729 return luaL_fileresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL);
730}
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 335 of file lua-5.4.3/src/liolib.c.

335 {
336 return g_iofile(L, IO_INPUT, "r");
337}
#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 386 of file lua-5.4.3/src/liolib.c.

386 {
387 int toclose;
388 if (lua_isnone(L, 1)) lua_pushnil(L); /* at least one argument */
389 if (lua_isnil(L, 1)) { /* no file name? */
390 lua_getfield(L, LUA_REGISTRYINDEX, IO_INPUT); /* get default input */
391 lua_replace(L, 1); /* put it at index 1 */
392 tofile(L); /* check that it's a valid file handle */
393 toclose = 0; /* do not close it after iteration */
394 }
395 else { /* open a new file */
396 const char *filename = luaL_checkstring(L, 1);
397 opencheck(L, filename, "r");
398 lua_replace(L, 1); /* put file at index 1 */
399 toclose = 1; /* close it after iteration */
400 }
401 aux_lines(L, toclose); /* push iteration function */
402 if (toclose) {
403 lua_pushnil(L); /* state */
404 lua_pushnil(L); /* control */
405 lua_pushvalue(L, 1); /* file is the to-be-closed variable (4th result) */
406 return 4;
407 }
408 else
409 return 1;
410}
LUA_API void lua_pushnil(lua_State *L)
#define lua_isnil(L, n)
#define lua_replace(L, idx)

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

◆ io_noclose()

static int io_noclose ( lua_State * L)
static

Definition at line 797 of file lua-5.4.3/src/liolib.c.

797 {
798 LStream *p = tolstream(L);
799 p->closef = &io_noclose; /* keep file opened */
800 luaL_pushfail(L);
801 lua_pushliteral(L, "cannot close standard file");
802 return 2;
803}

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

Referenced by createstdfile(), and io_noclose().

◆ io_open()

static int io_open ( lua_State * L)
static

Definition at line 269 of file lua-5.4.3/src/liolib.c.

269 {
270 const char *filename = luaL_checkstring(L, 1);
271 const char *mode = luaL_optstring(L, 2, "r");
272 LStream *p = newfile(L);
273 const char *md = mode; /* to traverse/check mode */
274 luaL_argcheck(L, l_checkmode(md), 2, "invalid mode");
275 p->f = fopen(filename, mode);
276 return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1;
277}
#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 340 of file lua-5.4.3/src/liolib.c.

340 {
341 return g_iofile(L, IO_OUTPUT, "w");
342}

References g_iofile(), and IO_OUTPUT.

◆ io_pclose()

static int io_pclose ( lua_State * L)
static

Definition at line 283 of file lua-5.4.3/src/liolib.c.

283 {
284 LStream *p = tolstream(L);
285 errno = 0;
286 return luaL_execresult(L, l_pclose(L, p->f));
287}
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 290 of file lua-5.4.3/src/liolib.c.

290 {
291 const char *filename = luaL_checkstring(L, 1);
292 const char *mode = luaL_optstring(L, 2, "r");
293 LStream *p = newprefile(L);
294 luaL_argcheck(L, l_checkmodep(mode), 2, "invalid mode");
295 p->f = l_popen(L, filename, mode);
296 p->closef = &io_pclose;
297 return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1;
298}
#define l_checkmodep(m)
#define l_popen(L, c, m)
static int io_pclose(lua_State *L)

References luaL_Stream::closef, luaL_Stream::f, io_pclose(), l_checkmodep, 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 616 of file lua-5.4.3/src/liolib.c.

616 {
617 return g_read(L, getiofile(L, IO_INPUT), 1);
618}

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

◆ io_readline()

static int io_readline ( lua_State * L)
static

Definition at line 629 of file lua-5.4.3/src/liolib.c.

629 {
631 int i;
632 int n = (int)lua_tointeger(L, lua_upvalueindex(2));
633 if (isclosed(p)) /* file is already closed? */
634 return luaL_error(L, "file is already closed");
635 lua_settop(L , 1);
636 luaL_checkstack(L, n, "too many arguments");
637 for (i = 1; i <= n; i++) /* push arguments to 'g_read' */
639 n = g_read(L, p->f, 2); /* 'n' is number of results */
640 lua_assert(n > 0); /* should return at least a nil */
641 if (lua_toboolean(L, -n)) /* read at least one value? */
642 return n; /* return them */
643 else { /* first result is false: EOF or error */
644 if (n > 1) { /* is there error information? */
645 /* 2nd result is error message */
646 return luaL_error(L, "%s", lua_tostring(L, -n + 1));
647 }
648 if (lua_toboolean(L, lua_upvalueindex(3))) { /* generator created file? */
649 lua_settop(L, 0); /* clear stack */
650 lua_pushvalue(L, lua_upvalueindex(1)); /* push file at index 1 */
651 aux_close(L); /* close it */
652 }
653 return 0;
654 }
655}
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 301 of file lua-5.4.3/src/liolib.c.

301 {
302 LStream *p = newfile(L);
303 p->f = tmpfile();
304 return (p->f == NULL) ? luaL_fileresult(L, 0, NULL) : 1;
305}

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

◆ io_type()

static int io_type ( lua_State * L)
static

Definition at line 163 of file lua-5.4.3/src/liolib.c.

163 {
164 LStream *p;
165 luaL_checkany(L, 1);
167 if (p == NULL)
168 luaL_pushfail(L); /* not a file */
169 else if (isclosed(p))
170 lua_pushliteral(L, "closed file");
171 else
172 lua_pushliteral(L, "file");
173 return 1;
174}
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, luaL_checkany(), luaL_pushfail, luaL_testudata(), and NULL.

◆ io_write()

static int io_write ( lua_State * L)
static

Definition at line 685 of file lua-5.4.3/src/liolib.c.

685 {
686 return g_write(L, getiofile(L, IO_OUTPUT), 1);
687}

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.4.3/src/liolib.c.

40 {
41 return (*mode != '\0' && strchr("rwa", *(mode++)) != NULL &&
42 (*mode != '+' || ((void)(++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 819 of file lua-5.4.3/src/liolib.c.

819 {
820 luaL_newlib(L, iolib); /* new module */
821 createmeta(L);
822 /* create (and set) default files */
823 createstdfile(L, stdin, IO_INPUT, "stdin");
824 createstdfile(L, stdout, IO_OUTPUT, "stdout");
825 createstdfile(L, stderr, NULL, "stderr");
826 return 1;
827}
#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 253 of file lua-5.4.3/src/liolib.c.

253 {
254 LStream *p = newprefile(L);
255 p->f = NULL;
256 p->closef = &io_fclose;
257 return p;
258}
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 201 of file lua-5.4.3/src/liolib.c.

201 {
202 LStream *p = (LStream *)lua_newuserdatauv(L, sizeof(LStream), 0);
203 p->closef = NULL; /* mark file handle as 'closed' */
205 return p;
206}
LUALIB_API void luaL_setmetatable(lua_State *L, const char *tname)
LUA_API void * lua_newuserdatauv(lua_State *L, size_t size, int nuvalue)

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

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

◆ nextc()

static int nextc ( RN * rn)
static

Definition at line 438 of file lua-5.4.3/src/liolib.c.

438 {
439 if (l_unlikely(rn->n >= L_MAXLENNUM)) { /* buffer overflow? */
440 rn->buff[0] = '\0'; /* invalidate result */
441 return 0; /* fail */
442 }
443 else {
444 rn->buff[rn->n++] = rn->c; /* save current char */
445 rn->c = l_getc(rn->f); /* read next one */
446 return 1;
447 }
448}
#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 261 of file lua-5.4.3/src/liolib.c.

261 {
262 LStream *p = newfile(L);
263 p->f = fopen(fname, mode);
264 if (l_unlikely(p->f == NULL))
265 luaL_error(L, "cannot open file '%s' (%s)", fname, strerror(errno));
266}

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

540 {
541 size_t nr;
542 luaL_Buffer b;
543 luaL_buffinit(L, &b);
544 do { /* read file in chunks of LUAL_BUFFERSIZE bytes */
545 char *p = luaL_prepbuffer(&b);
546 nr = fread(p, sizeof(char), LUAL_BUFFERSIZE, f);
547 luaL_addsize(&b, nr);
548 } while (nr == LUAL_BUFFERSIZE);
549 luaL_pushresult(&b); /* close buffer */
550}
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 553 of file lua-5.4.3/src/liolib.c.

553 {
554 size_t nr; /* number of chars actually read */
555 char *p;
556 luaL_Buffer b;
557 luaL_buffinit(L, &b);
558 p = luaL_prepbuffsize(&b, n); /* prepare buffer to read whole block */
559 nr = fread(p, sizeof(char), n, f); /* try to read 'n' chars */
560 luaL_addsize(&b, nr);
561 luaL_pushresult(&b); /* close buffer */
562 return (nr > 0); /* true iff read something */
563}
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 519 of file lua-5.4.3/src/liolib.c.

519 {
520 luaL_Buffer b;
521 int c;
522 luaL_buffinit(L, &b);
523 do { /* may need to read several chunks to get whole line */
524 char *buff = luaL_prepbuffer(&b); /* preallocate buffer space */
525 int i = 0;
526 l_lockfile(f); /* no memory errors can happen inside the lock */
527 while (i < LUAL_BUFFERSIZE && (c = l_getc(f)) != EOF && c != '\n')
528 buff[i++] = c; /* read up to end of line or buffer limit */
529 l_unlockfile(f);
530 luaL_addsize(&b, i);
531 } while (c != EOF && c != '\n'); /* repeat until end of line */
532 if (!chop && c == '\n') /* want a newline and have one? */
533 luaL_addchar(&b, c); /* add ending newline to result */
534 luaL_pushresult(&b); /* close buffer */
535 /* return ok if read something (either a newline or something else) */
536 return (c == '\n' || lua_rawlen(L, -1) > 0);
537}
#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 477 of file lua-5.4.3/src/liolib.c.

477 {
478 RN rn;
479 int count = 0;
480 int hex = 0;
481 char decp[2];
482 rn.f = f; rn.n = 0;
483 decp[0] = lua_getlocaledecpoint(); /* get decimal point from locale */
484 decp[1] = '.'; /* always accept a dot */
485 l_lockfile(rn.f);
486 do { rn.c = l_getc(rn.f); } while (isspace(rn.c)); /* skip spaces */
487 test2(&rn, "-+"); /* optional sign */
488 if (test2(&rn, "00")) {
489 if (test2(&rn, "xX")) hex = 1; /* numeral is hexadecimal */
490 else count = 1; /* count initial '0' as a valid digit */
491 }
492 count += readdigits(&rn, hex); /* integral part */
493 if (test2(&rn, decp)) /* decimal point? */
494 count += readdigits(&rn, hex); /* fractional part */
495 if (count > 0 && test2(&rn, (hex ? "pP" : "eE"))) { /* exponent mark? */
496 test2(&rn, "-+"); /* exponent sign */
497 readdigits(&rn, 0); /* exponent digits */
498 }
499 ungetc(rn.c, rn.f); /* unread look-ahead char */
500 l_unlockfile(rn.f);
501 rn.buff[rn.n] = '\0'; /* finish string */
502 if (l_likely(lua_stringtonumber(L, rn.buff)))
503 return 1; /* ok, it is a valid number */
504 else { /* invalid format */
505 lua_pushnil(L); /* "result" to be removed */
506 return 0; /* read fails */
507 }
508}
LUA_API size_t lua_stringtonumber(lua_State *L, const char *s)
#define lua_getlocaledecpoint()
static int test2(RN *rn, const char *set)
static int readdigits(RN *rn, int hex)

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

464 {
465 int count = 0;
466 while ((hex ? isxdigit(rn->c) : isdigit(rn->c)) && nextc(rn))
467 count++;
468 return count;
469}
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 454 of file lua-5.4.3/src/liolib.c.

454 {
455 if (rn->c == set[0] || rn->c == set[1])
456 return nextc(rn);
457 else return 0;
458}

References RN::c, and nextc().

Referenced by read_number().

◆ test_eof()

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

Definition at line 511 of file lua-5.4.3/src/liolib.c.

511 {
512 int c = getc(f);
513 ungetc(c, f); /* no-op when c == EOF */
514 lua_pushliteral(L, "");
515 return (c != EOF);
516}

References lua_pushliteral.

Referenced by g_read().

◆ tofile()

static FILE * tofile ( lua_State * L)
static

Definition at line 187 of file lua-5.4.3/src/liolib.c.

187 {
188 LStream *p = tolstream(L);
189 if (l_unlikely(isclosed(p)))
190 luaL_error(L, "attempt to use a closed file");
191 lua_assert(p->f);
192 return p->f;
193}

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

◆ 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 741 of file lua-5.4.3/src/liolib.c.

741 {
742 {"close", io_close},
743 {"flush", io_flush},
744 {"input", io_input},
745 {"lines", io_lines},
746 {"open", io_open},
747 {"output", io_output},
748 {"popen", io_popen},
749 {"read", io_read},
750 {"tmpfile", io_tmpfile},
751 {"type", io_type},
752 {"write", io_write},
753 {NULL, NULL}
754};

Referenced by luaopen_io().

◆ metameth

const luaL_Reg metameth[]
static
Initial value:
= {
{"__index", NULL},
{"__gc", f_gc},
{"__close", f_gc},
{"__tostring", f_tostring},
}
static int f_gc(lua_State *L)
static int f_tostring(lua_State *L)

Definition at line 775 of file lua-5.4.3/src/liolib.c.

775 {
776 {"__index", NULL}, /* place holder */
777 {"__gc", f_gc},
778 {"__close", f_gc},
779 {"__tostring", f_tostring},
780 {NULL, NULL}
781};

Referenced by createmeta().

◆ meth

const luaL_Reg meth[]
static
Initial value:
= {
{"read", f_read},
{"write", f_write},
{"lines", f_lines},
{"flush", f_flush},
{"seek", f_seek},
{"close", f_close},
{"setvbuf", f_setvbuf},
}
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)

Definition at line 760 of file lua-5.4.3/src/liolib.c.

760 {
761 {"read", f_read},
762 {"write", f_write},
763 {"lines", f_lines},
764 {"flush", f_flush},
765 {"seek", f_seek},
766 {"close", f_close},
767 {"setvbuf", f_setvbuf},
768 {NULL, NULL}
769};

Referenced by createmeta().