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 liolib_c
 
#define LUA_LIB
 
#define IO_INPUT   1
 
#define IO_OUTPUT   2
 
#define tofilep(L)   ((FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE))
 

Functions

static int pushresult (lua_State *L, int i, const char *filename)
 
static void fileerror (lua_State *L, int arg, const char *filename)
 
static int io_type (lua_State *L)
 
static FILE * tofile (lua_State *L)
 
static FILE ** newfile (lua_State *L)
 
static int io_noclose (lua_State *L)
 
static int io_pclose (lua_State *L)
 
static int io_fclose (lua_State *L)
 
static int aux_close (lua_State *L)
 
static int io_close (lua_State *L)
 
static int io_gc (lua_State *L)
 
static int io_tostring (lua_State *L)
 
static int io_open (lua_State *L)
 
static int io_popen (lua_State *L)
 
static int io_tmpfile (lua_State *L)
 
static FILE * getiofile (lua_State *L, int findex)
 
static int g_iofile (lua_State *L, int 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 idx, 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)
 
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 void createstdfile (lua_State *L, FILE *f, int k, const char *fname)
 
static void newfenv (lua_State *L, lua_CFunction cls)
 
LUALIB_API int luaopen_io (lua_State *L)
 

Variables

static const char *const fnames [] = {"input", "output"}
 
static const luaL_Reg iolib []
 
static const luaL_Reg flib []
 

Macro Definition Documentation

◆ IO_INPUT

#define IO_INPUT   1

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

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

◆ IO_OUTPUT

#define IO_OUTPUT   2

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

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

◆ liolib_c

#define liolib_c

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

◆ LUA_LIB

#define LUA_LIB

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

◆ tofilep

#define tofilep ( L)    ((FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE))

Definition at line 54 of file lua-5.1.5/src/liolib.c.

Referenced by io_fclose(), io_gc(), io_pclose(), io_tostring(), and tofile().

Function Documentation

◆ aux_close()

static int aux_close ( lua_State * L)
static

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

127 {
128 lua_getfenv(L, 1);
129 lua_getfield(L, -1, "__close");
130 return (lua_tocfunction(L, -1))(L);
131}
LUA_API void lua_getfenv(lua_State *L, int idx)
LUA_API void lua_getfield(lua_State *L, int idx, const char *k)
LUA_API lua_CFunction lua_tocfunction(lua_State *L, int idx)

References lua_getfenv(), lua_getfield(), and lua_tocfunction().

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

◆ aux_lines()

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

Definition at line 234 of file lua-5.1.5/src/liolib.c.

234 {
235 lua_pushvalue(L, idx);
236 lua_pushboolean(L, toclose); /* close/not close file when finished */
238}
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)
static int io_readline(lua_State *L)

References io_readline(), lua_pushboolean(), lua_pushcclosure(), and lua_pushvalue().

Referenced by f_lines(), and io_lines().

◆ createmeta()

static void createmeta ( lua_State * L)
static

Definition at line 510 of file lua-5.1.5/src/liolib.c.

510 {
511 luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */
512 lua_pushvalue(L, -1); /* push metatable */
513 lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
514 luaL_register(L, NULL, flib); /* file methods */
515}
#define NULL
Definition gmacros.h:924
LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
LUALIB_API int luaL_newmetatable(lua_State *L, const char *tname)
LUALIB_API void luaL_register(lua_State *L, const char *libname, const luaL_Reg *l)
static const luaL_Reg flib[]
#define LUA_FILEHANDLE

References flib, LUA_FILEHANDLE, lua_pushvalue(), lua_setfield(), luaL_newmetatable(), luaL_register(), and NULL.

Referenced by luaopen_io().

◆ createstdfile()

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

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

518 {
519 *newfile(L) = f;
520 if (k > 0) {
521 lua_pushvalue(L, -1);
523 }
524 lua_pushvalue(L, -2); /* copy environment */
525 lua_setfenv(L, -2); /* set it */
526 lua_setfield(L, -3, fname);
527}
LUA_API void lua_rawseti(lua_State *L, int idx, int n)
LUA_API int lua_setfenv(lua_State *L, int idx)
static FILE ** newfile(lua_State *L)
#define LUA_ENVIRONINDEX

References LUA_ENVIRONINDEX, lua_pushvalue(), lua_rawseti(), lua_setfenv(), lua_setfield(), and newfile().

Referenced by luaopen_io().

◆ f_flush()

static int f_flush ( lua_State * L)
static

Definition at line 475 of file lua-5.1.5/src/liolib.c.

475 {
476 return pushresult(L, fflush(tofile(L)) == 0, NULL);
477}
static FILE * tofile(lua_State *L)
static int pushresult(lua_State *L, int i, const char *filename)

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

◆ f_lines()

static int f_lines ( lua_State * L)
static

Definition at line 241 of file lua-5.1.5/src/liolib.c.

241 {
242 tofile(L); /* check that it's a valid file handle */
243 aux_lines(L, 1, 0);
244 return 1;
245}
static void aux_lines(lua_State *L, int idx, int toclose)

References aux_lines(), and tofile().

Referenced by io_lines().

◆ f_read()

static int f_read ( lua_State * L)
static

Definition at line 386 of file lua-5.1.5/src/liolib.c.

386 {
387 return g_read(L, tofile(L), 2);
388}
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 442 of file lua-5.1.5/src/liolib.c.

442 {
443 static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
444 static const char *const modenames[] = {"set", "cur", "end", NULL};
445 FILE *f = tofile(L);
446 int op = luaL_checkoption(L, 2, "cur", modenames);
447 long offset = luaL_optlong(L, 3, 0);
448 op = fseek(f, offset, mode[op]);
449 if (op)
450 return pushresult(L, 0, NULL); /* error */
451 else {
452 lua_pushinteger(L, ftell(f));
453 return 1;
454 }
455}
LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
LUALIB_API int luaL_checkoption(lua_State *L, int narg, const char *def, const char *const lst[])
#define luaL_optlong(L, n, d)

References lua_pushinteger(), luaL_checkoption(), luaL_optlong, NULL, pushresult(), and tofile().

◆ f_setvbuf()

static int f_setvbuf ( lua_State * L)
static

Definition at line 458 of file lua-5.1.5/src/liolib.c.

458 {
459 static const int mode[] = {_IONBF, _IOFBF, _IOLBF};
460 static const char *const modenames[] = {"no", "full", "line", NULL};
461 FILE *f = tofile(L);
462 int op = luaL_checkoption(L, 2, NULL, modenames);
464 int res = setvbuf(f, NULL, mode[op], sz);
465 return pushresult(L, res == 0, NULL);
466}
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_optinteger(), NULL, pushresult(), and tofile().

◆ f_write()

static int f_write ( lua_State * L)
static

Definition at line 437 of file lua-5.1.5/src/liolib.c.

437 {
438 return g_write(L, tofile(L), 2);
439}
static int g_write(lua_State *L, FILE *f, int arg)

References g_write(), and tofile().

◆ fileerror()

static void fileerror ( lua_State * L,
int arg,
const char * filename )
static

Definition at line 48 of file lua-5.1.5/src/liolib.c.

48 {
49 lua_pushfstring(L, "%s: %s", filename, strerror(errno));
50 luaL_argerror(L, arg, lua_tostring(L, -1));
51}
CURL_EXTERN int void * arg
Definition curl.h:2622
LUA_API const char * lua_pushfstring(lua_State *L, const char *fmt,...)
LUALIB_API int luaL_argerror(lua_State *L, int narg, const char *extramsg)
#define lua_tostring(L, i)

References arg, lua_pushfstring(), lua_tostring, and luaL_argerror().

Referenced by g_iofile(), and io_lines().

◆ g_iofile()

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

Definition at line 200 of file lua-5.1.5/src/liolib.c.

200 {
201 if (!lua_isnoneornil(L, 1)) {
202 const char *filename = lua_tostring(L, 1);
203 if (filename) {
204 FILE **pf = newfile(L);
205 *pf = fopen(filename, mode);
206 if (*pf == NULL)
207 fileerror(L, 1, filename);
208 }
209 else {
210 tofile(L); /* check that it's a valid file handle */
211 lua_pushvalue(L, 1);
212 }
214 }
215 /* return current value */
217 return 1;
218}
LUA_API void lua_rawgeti(lua_State *L, int idx, int n)
static void fileerror(lua_State *L, int arg, const char *filename)
#define lua_isnoneornil(L, n)

References fileerror(), LUA_ENVIRONINDEX, lua_isnoneornil, lua_pushvalue(), lua_rawgeti(), lua_rawseti(), lua_tostring, newfile(), NULL, 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 334 of file lua-5.1.5/src/liolib.c.

334 {
335 int nargs = lua_gettop(L) - 1;
336 int success;
337 int n;
338 clearerr(f);
339 if (nargs == 0) { /* no arguments? */
340 success = read_line(L, f);
341 n = first+1; /* to return 1 result */
342 }
343 else { /* ensure stack space for all results and for auxlib's buffer */
344 luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments");
345 success = 1;
346 for (n = first; nargs-- && success; n++) {
347 if (lua_type(L, n) == LUA_TNUMBER) {
348 size_t l = (size_t)lua_tointeger(L, n);
349 success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
350 }
351 else {
352 const char *p = lua_tostring(L, n);
353 luaL_argcheck(L, p && p[0] == '*', n, "invalid option");
354 switch (p[1]) {
355 case 'n': /* number */
356 success = read_number(L, f);
357 break;
358 case 'l': /* line */
359 success = read_line(L, f);
360 break;
361 case 'a': /* file */
362 read_chars(L, f, ~((size_t)0)); /* read MAX_SIZE_T chars */
363 success = 1; /* always success */
364 break;
365 default:
366 return luaL_argerror(L, n, "invalid format");
367 }
368 }
369 }
370 }
371 if (ferror(f))
372 return pushresult(L, 0, NULL);
373 if (!success) {
374 lua_pop(L, 1); /* remove last result */
375 lua_pushnil(L); /* push nil instead */
376 }
377 return n - first;
378}
LUA_API void lua_pushnil(lua_State *L)
LUA_API int lua_type(lua_State *L, int idx)
LUA_API int lua_gettop(lua_State *L)
LUALIB_API void luaL_checkstack(lua_State *L, int space, const char *mes)
#define luaL_argcheck(L, cond, numarg, extramsg)
static int read_line(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)
#define LUA_TNUMBER
#define lua_pop(L, n)
#define LUA_MINSTACK
#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(), NULL, pushresult(), read_chars(), read_line(), read_number(), and test_eof().

Referenced by f_read(), and io_read().

◆ g_write()

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

Definition at line 413 of file lua-5.1.5/src/liolib.c.

413 {
414 int nargs = lua_gettop(L) - 1;
415 int status = 1;
416 for (; nargs--; arg++) {
417 if (lua_type(L, arg) == LUA_TNUMBER) {
418 /* optimization: could be done exactly as for strings */
419 status = status &&
420 fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0;
421 }
422 else {
423 size_t l;
424 const char *s = luaL_checklstring(L, arg, &l);
425 status = status && (fwrite(s, sizeof(char), l, f) == l);
426 }
427 }
428 return pushresult(L, status, NULL);
429}
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(), NULL, pushresult(), and s.

Referenced by f_write(), and io_write().

◆ getiofile()

static FILE * getiofile ( lua_State * L,
int findex )
static

Definition at line 190 of file lua-5.1.5/src/liolib.c.

190 {
191 FILE *f;
192 lua_rawgeti(L, LUA_ENVIRONINDEX, findex);
193 f = *(FILE **)lua_touserdata(L, -1);
194 if (f == NULL)
195 luaL_error(L, "standard %s file is closed", fnames[findex - 1]);
196 return f;
197}
LUA_API void * lua_touserdata(lua_State *L, int idx)
LUALIB_API int luaL_error(lua_State *L, const char *fmt,...)
static const char *const fnames[]

References fnames, LUA_ENVIRONINDEX, lua_rawgeti(), lua_touserdata(), luaL_error(), and NULL.

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

◆ io_close()

static int io_close ( lua_State * L)
static

Definition at line 134 of file lua-5.1.5/src/liolib.c.

134 {
135 if (lua_isnone(L, 1))
137 tofile(L); /* make sure argument is a file */
138 return aux_close(L);
139}
static int aux_close(lua_State *L)
#define IO_OUTPUT
#define lua_isnone(L, n)

References aux_close(), IO_OUTPUT, LUA_ENVIRONINDEX, lua_isnone, lua_rawgeti(), and tofile().

◆ io_fclose()

static int io_fclose ( lua_State * L)
static

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

119 {
120 FILE **p = tofilep(L);
121 int ok = (fclose(*p) == 0);
122 *p = NULL;
123 return pushresult(L, ok, NULL);
124}
#define tofilep(L)

References NULL, pushresult(), and tofilep.

Referenced by luaopen_io().

◆ io_flush()

static int io_flush ( lua_State * L)
static

Definition at line 470 of file lua-5.1.5/src/liolib.c.

470 {
471 return pushresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL);
472}
static FILE * getiofile(lua_State *L, int findex)

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

◆ io_gc()

static int io_gc ( lua_State * L)
static

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

142 {
143 FILE *f = *tofilep(L);
144 /* ignore closed files */
145 if (f != NULL)
146 aux_close(L);
147 return 0;
148}

References aux_close(), NULL, and tofilep.

◆ io_input()

static int io_input ( lua_State * L)
static

Definition at line 221 of file lua-5.1.5/src/liolib.c.

221 {
222 return g_iofile(L, IO_INPUT, "r");
223}
#define IO_INPUT
static int g_iofile(lua_State *L, int f, const char *mode)

References g_iofile(), and IO_INPUT.

◆ io_lines()

static int io_lines ( lua_State * L)
static

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

248 {
249 if (lua_isnoneornil(L, 1)) { /* no arguments? */
250 /* will iterate over default input */
252 return f_lines(L);
253 }
254 else {
255 const char *filename = luaL_checkstring(L, 1);
256 FILE **pf = newfile(L);
257 *pf = fopen(filename, "r");
258 if (*pf == NULL)
259 fileerror(L, 1, filename);
260 aux_lines(L, lua_gettop(L), 1);
261 return 1;
262 }
263}
#define luaL_checkstring(L, n)
static int f_lines(lua_State *L)

References aux_lines(), f_lines(), fileerror(), IO_INPUT, LUA_ENVIRONINDEX, lua_gettop(), lua_isnoneornil, lua_rawgeti(), luaL_checkstring, newfile(), and NULL.

◆ io_noclose()

static int io_noclose ( lua_State * L)
static

Definition at line 98 of file lua-5.1.5/src/liolib.c.

98 {
99 lua_pushnil(L);
100 lua_pushliteral(L, "cannot close standard file");
101 return 2;
102}
#define lua_pushliteral(L, s)

References lua_pushliteral, and lua_pushnil().

Referenced by luaopen_io().

◆ io_open()

static int io_open ( lua_State * L)
static

Definition at line 161 of file lua-5.1.5/src/liolib.c.

161 {
162 const char *filename = luaL_checkstring(L, 1);
163 const char *mode = luaL_optstring(L, 2, "r");
164 FILE **pf = newfile(L);
165 *pf = fopen(filename, mode);
166 return (*pf == NULL) ? pushresult(L, 0, filename) : 1;
167}
#define luaL_optstring(L, n, d)

References luaL_checkstring, luaL_optstring, newfile(), NULL, and pushresult().

◆ io_output()

static int io_output ( lua_State * L)
static

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

226 {
227 return g_iofile(L, IO_OUTPUT, "w");
228}

References g_iofile(), and IO_OUTPUT.

◆ io_pclose()

static int io_pclose ( lua_State * L)
static

Definition at line 108 of file lua-5.1.5/src/liolib.c.

108 {
109 FILE **p = tofilep(L);
110 int ok = lua_pclose(L, *p);
111 *p = NULL;
112 return pushresult(L, ok, NULL);
113}
#define lua_pclose(L, file)

References lua_pclose, NULL, pushresult(), and tofilep.

Referenced by luaopen_io().

◆ io_popen()

static int io_popen ( lua_State * L)
static

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

174 {
175 const char *filename = luaL_checkstring(L, 1);
176 const char *mode = luaL_optstring(L, 2, "r");
177 FILE **pf = newfile(L);
178 *pf = lua_popen(L, filename, mode);
179 return (*pf == NULL) ? pushresult(L, 0, filename) : 1;
180}
#define lua_popen(L, c, m)

References lua_popen, luaL_checkstring, luaL_optstring, newfile(), NULL, and pushresult().

◆ io_read()

static int io_read ( lua_State * L)
static

Definition at line 381 of file lua-5.1.5/src/liolib.c.

381 {
382 return g_read(L, getiofile(L, IO_INPUT), 1);
383}

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

◆ io_readline()

static int io_readline ( lua_State * L)
static

Definition at line 391 of file lua-5.1.5/src/liolib.c.

391 {
392 FILE *f = *(FILE **)lua_touserdata(L, lua_upvalueindex(1));
393 int sucess;
394 if (f == NULL) /* file is already closed? */
395 luaL_error(L, "file is already closed");
396 sucess = read_line(L, f);
397 if (ferror(f))
398 return luaL_error(L, "%s", strerror(errno));
399 if (sucess) return 1;
400 else { /* EOF */
401 if (lua_toboolean(L, lua_upvalueindex(2))) { /* generator created file? */
402 lua_settop(L, 0);
404 aux_close(L); /* close it */
405 }
406 return 0;
407 }
408}
LUA_API int lua_toboolean(lua_State *L, int idx)
LUA_API void lua_settop(lua_State *L, int idx)
#define lua_upvalueindex(i)

References aux_close(), lua_pushvalue(), lua_settop(), lua_toboolean(), lua_touserdata(), lua_upvalueindex, luaL_error(), NULL, and read_line().

Referenced by aux_lines().

◆ io_tmpfile()

static int io_tmpfile ( lua_State * L)
static

Definition at line 183 of file lua-5.1.5/src/liolib.c.

183 {
184 FILE **pf = newfile(L);
185 *pf = tmpfile();
186 return (*pf == NULL) ? pushresult(L, 0, NULL) : 1;
187}

References newfile(), NULL, and pushresult().

◆ io_tostring()

static int io_tostring ( lua_State * L)
static

Definition at line 151 of file lua-5.1.5/src/liolib.c.

151 {
152 FILE *f = *tofilep(L);
153 if (f == NULL)
154 lua_pushliteral(L, "file (closed)");
155 else
156 lua_pushfstring(L, "file (%p)", f);
157 return 1;
158}

References lua_pushfstring(), lua_pushliteral, NULL, and tofilep.

◆ io_type()

static int io_type ( lua_State * L)
static

Definition at line 57 of file lua-5.1.5/src/liolib.c.

57 {
58 void *ud;
59 luaL_checkany(L, 1);
60 ud = lua_touserdata(L, 1);
62 if (ud == NULL || !lua_getmetatable(L, 1) || !lua_rawequal(L, -2, -1))
63 lua_pushnil(L); /* not a file */
64 else if (*((FILE **)ud) == NULL)
65 lua_pushliteral(L, "closed file");
66 else
67 lua_pushliteral(L, "file");
68 return 1;
69}
LUA_API int lua_getmetatable(lua_State *L, int objindex)
LUA_API int lua_rawequal(lua_State *L, int index1, int index2)
LUALIB_API void luaL_checkany(lua_State *L, int narg)
#define LUA_REGISTRYINDEX

References LUA_FILEHANDLE, lua_getfield(), lua_getmetatable(), lua_pushliteral, lua_pushnil(), lua_rawequal(), LUA_REGISTRYINDEX, lua_touserdata(), luaL_checkany(), and NULL.

◆ io_write()

static int io_write ( lua_State * L)
static

Definition at line 432 of file lua-5.1.5/src/liolib.c.

432 {
433 return g_write(L, getiofile(L, IO_OUTPUT), 1);
434}

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

◆ luaopen_io()

LUALIB_API int luaopen_io ( lua_State * L)

Definition at line 537 of file lua-5.1.5/src/liolib.c.

537 {
538 createmeta(L);
539 /* create (private) environment (with fields IO_INPUT, IO_OUTPUT, __close) */
540 newfenv(L, io_fclose);
542 /* open library */
544 /* create (and set) default files */
545 newfenv(L, io_noclose); /* close function for default files */
546 createstdfile(L, stdin, IO_INPUT, "stdin");
547 createstdfile(L, stdout, IO_OUTPUT, "stdout");
548 createstdfile(L, stderr, 0, "stderr");
549 lua_pop(L, 1); /* pop environment for default files */
550 lua_getfield(L, -1, "popen");
551 newfenv(L, io_pclose); /* create environment for 'popen' */
552 lua_setfenv(L, -2); /* set fenv for 'popen' */
553 lua_pop(L, 1); /* pop 'popen' */
554 return 1;
555}
static void createmeta(lua_State *L)
static void createstdfile(lua_State *L, FILE *f, int k, const char *fname)
static int io_fclose(lua_State *L)
static void newfenv(lua_State *L, lua_CFunction cls)
static int io_noclose(lua_State *L)
static const luaL_Reg iolib[]
static int io_pclose(lua_State *L)
#define LUA_IOLIBNAME
#define lua_replace(L, idx)

References createmeta(), createstdfile(), io_fclose(), IO_INPUT, io_noclose(), IO_OUTPUT, io_pclose(), iolib, LUA_ENVIRONINDEX, lua_getfield(), LUA_IOLIBNAME, lua_pop, lua_replace, lua_setfenv(), luaL_register(), and newfenv().

◆ newfenv()

static void newfenv ( lua_State * L,
lua_CFunction cls )
static

Definition at line 530 of file lua-5.1.5/src/liolib.c.

530 {
531 lua_createtable(L, 0, 1);
532 lua_pushcfunction(L, cls);
533 lua_setfield(L, -2, "__close");
534}
LUA_API void lua_createtable(lua_State *L, int narray, int nrec)
#define lua_pushcfunction(L, f)

References lua_createtable(), lua_pushcfunction, and lua_setfield().

Referenced by luaopen_io().

◆ newfile()

static FILE ** newfile ( lua_State * L)
static

Definition at line 86 of file lua-5.1.5/src/liolib.c.

86 {
87 FILE **pf = (FILE **)lua_newuserdata(L, sizeof(FILE *));
88 *pf = NULL; /* file handle is currently `closed' */
90 lua_setmetatable(L, -2);
91 return pf;
92}
LUA_API int lua_setmetatable(lua_State *L, int objindex)
#define luaL_getmetatable(L, n)
#define lua_newuserdata(L, s)

References LUA_FILEHANDLE, lua_newuserdata, lua_setmetatable(), luaL_getmetatable, and NULL.

Referenced by createstdfile(), g_iofile(), io_lines(), io_open(), io_popen(), and io_tmpfile().

◆ pushresult()

static int pushresult ( lua_State * L,
int i,
const char * filename )
static

Definition at line 30 of file lua-5.1.5/src/liolib.c.

30 {
31 int en = errno; /* calls to Lua API may change this value */
32 if (i) {
33 lua_pushboolean(L, 1);
34 return 1;
35 }
36 else {
37 lua_pushnil(L);
38 if (filename)
39 lua_pushfstring(L, "%s: %s", filename, strerror(en));
40 else
41 lua_pushfstring(L, "%s", strerror(en));
42 lua_pushinteger(L, en);
43 return 3;
44 }
45}

References lua_pushboolean(), lua_pushfstring(), lua_pushinteger(), and lua_pushnil().

Referenced by f_flush(), f_seek(), f_setvbuf(), g_read(), g_write(), io_fclose(), io_flush(), io_open(), io_pclose(), io_popen(), and io_tmpfile().

◆ read_chars()

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

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

316 {
317 size_t rlen; /* how much to read */
318 size_t nr; /* number of chars actually read */
319 luaL_Buffer b;
320 luaL_buffinit(L, &b);
321 rlen = LUAL_BUFFERSIZE; /* try to read that much each time */
322 do {
323 char *p = luaL_prepbuffer(&b);
324 if (rlen > n) rlen = n; /* cannot read more than asked */
325 nr = fread(p, sizeof(char), rlen, f);
326 luaL_addsize(&b, nr);
327 n -= nr; /* still have to read `n' chars */
328 } while (n > 0 && nr == rlen); /* until end of count or eof */
329 luaL_pushresult(&b); /* close buffer */
330 return (n == 0 || lua_objlen(L, -1) > 0);
331}
LUA_API size_t lua_objlen(lua_State *L, int idx)
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(), lua_objlen(), luaL_addsize, LUAL_BUFFERSIZE, luaL_buffinit(), luaL_prepbuffer, and luaL_pushresult().

Referenced by g_read().

◆ read_line()

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

Definition at line 294 of file lua-5.1.5/src/liolib.c.

294 {
295 luaL_Buffer b;
296 luaL_buffinit(L, &b);
297 for (;;) {
298 size_t l;
299 char *p = luaL_prepbuffer(&b);
300 if (fgets(p, LUAL_BUFFERSIZE, f) == NULL) { /* eof? */
301 luaL_pushresult(&b); /* close buffer */
302 return (lua_objlen(L, -1) > 0); /* check whether read something */
303 }
304 l = strlen(p);
305 if (l == 0 || p[l-1] != '\n')
306 luaL_addsize(&b, l);
307 else {
308 luaL_addsize(&b, l - 1); /* do not include `eol' */
309 luaL_pushresult(&b); /* close buffer */
310 return 1; /* read at least an `eol' */
311 }
312 }
313}

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

Referenced by g_read(), and io_readline().

◆ read_number()

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

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

273 {
274 lua_Number d;
275 if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1) {
276 lua_pushnumber(L, d);
277 return 1;
278 }
279 else {
280 lua_pushnil(L); /* "result" to be removed */
281 return 0; /* read fails */
282 }
283}
LUA_API void lua_pushnumber(lua_State *L, lua_Number n)
LUA_NUMBER lua_Number
#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 286 of file lua-5.1.5/src/liolib.c.

286 {
287 int c = getc(f);
288 ungetc(c, f);
289 lua_pushlstring(L, NULL, 0);
290 return (c != EOF);
291}
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 72 of file lua-5.1.5/src/liolib.c.

72 {
73 FILE **f = tofilep(L);
74 if (*f == NULL)
75 luaL_error(L, "attempt to use a closed file");
76 return *f;
77}

References luaL_error(), NULL, and tofilep.

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

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", io_gc},
{"__tostring", io_tostring},
}
static int io_close(lua_State *L)
static int io_tostring(lua_State *L)
static int f_setvbuf(lua_State *L)
static int io_gc(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 496 of file lua-5.1.5/src/liolib.c.

496 {
497 {"close", io_close},
498 {"flush", f_flush},
499 {"lines", f_lines},
500 {"read", f_read},
501 {"seek", f_seek},
502 {"setvbuf", f_setvbuf},
503 {"write", f_write},
504 {"__gc", io_gc},
505 {"__tostring", io_tostring},
506 {NULL, NULL}
507};

Referenced by createmeta().

◆ fnames

const char* const fnames[] = {"input", "output"}
static

Definition at line 27 of file lua-5.1.5/src/liolib.c.

27{"input", "output"};

Referenced by getiofile().

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

480 {
481 {"close", io_close},
482 {"flush", io_flush},
483 {"input", io_input},
484 {"lines", io_lines},
485 {"open", io_open},
486 {"output", io_output},
487 {"popen", io_popen},
488 {"read", io_read},
489 {"tmpfile", io_tmpfile},
490 {"type", io_type},
491 {"write", io_write},
492 {NULL, NULL}
493};

Referenced by luaopen_io().