From 0fe21c9a8b221c4797a5eae27f5c3373b16b6919 Mon Sep 17 00:00:00 2001 From: philmoz Date: Mon, 7 Oct 2024 19:06:41 +1100 Subject: [PATCH] Fix 'io' functions. --- radio/src/thirdparty/Lua/src/liolib.c | 425 ++++++++++++++------------ 1 file changed, 225 insertions(+), 200 deletions(-) diff --git a/radio/src/thirdparty/Lua/src/liolib.c b/radio/src/thirdparty/Lua/src/liolib.c index b182518542d..018990cf6e4 100644 --- a/radio/src/thirdparty/Lua/src/liolib.c +++ b/radio/src/thirdparty/Lua/src/liolib.c @@ -24,9 +24,9 @@ #include "FatFs/ff.h" -#define l_getc(f) fatfs_getc(f) -#define l_lockfile(f) ((void)0) -#define l_unlockfile(f) ((void)0) +#define l_getc(f) fatfs_getc(f) +#define l_lockfile(f) ((void)0) +#define l_unlockfile(f) ((void)0) static int fatfs_getc(FIL* f) { char c; UINT result; @@ -61,47 +61,47 @@ static int l_checkmode (const char *mode) { ** ======================================================= */ -#if !defined(l_popen) /* { */ +// #if !defined(l_popen) /* { */ -#if defined(LUA_USE_POSIX) /* { */ +// #if defined(LUA_USE_POSIX) /* { */ -#define l_popen(L,c,m) (fflush(NULL), popen(c,m)) -#define l_pclose(L,file) (pclose(file)) +// #define l_popen(L,c,m) (fflush(NULL), popen(c,m)) +// #define l_pclose(L,file) (pclose(file)) -#elif defined(LUA_USE_WINDOWS) /* }{ */ +// #elif defined(LUA_USE_WINDOWS) /* }{ */ -#define l_popen(L,c,m) (_popen(c,m)) -#define l_pclose(L,file) (_pclose(file)) +// #define l_popen(L,c,m) (_popen(c,m)) +// #define l_pclose(L,file) (_pclose(file)) -#else /* }{ */ +// #else /* }{ */ -/* ISO C definitions */ -#define l_popen(L,c,m) \ - ((void)((void)c, m), \ - luaL_error(L, "'popen' not supported"), \ - (FILE*)0) -#define l_pclose(L,file) ((void)L, (void)file, -1) +// /* ISO C definitions */ +// #define l_popen(L,c,m) \ +// ((void)((void)c, m), \ +// luaL_error(L, "'popen' not supported"), \ +// (FILE*)0) +// #define l_pclose(L,file) ((void)L, (void)file, -1) -#endif /* } */ +// #endif /* } */ -#endif /* } */ +// #endif /* } */ /* }====================================================== */ -#if !defined(l_getc) /* { */ +// #if !defined(l_getc) /* { */ -#if defined(LUA_USE_POSIX) -#define l_getc(f) getc_unlocked(f) -#define l_lockfile(f) flockfile(f) -#define l_unlockfile(f) funlockfile(f) -#else -#define l_getc(f) getc(f) -#define l_lockfile(f) ((void)0) -#define l_unlockfile(f) ((void)0) -#endif +// #if defined(LUA_USE_POSIX) +// #define l_getc(f) getc_unlocked(f) +// #define l_lockfile(f) flockfile(f) +// #define l_unlockfile(f) funlockfile(f) +// #else +// #define l_getc(f) getc(f) +// #define l_lockfile(f) ((void)0) +// #define l_unlockfile(f) ((void)0) +// #endif -#endif /* } */ +// #endif /* } */ /* @@ -110,34 +110,34 @@ static int l_checkmode (const char *mode) { ** ======================================================= */ -#if !defined(l_fseek) /* { */ +// #if !defined(l_fseek) /* { */ -#if defined(LUA_USE_POSIX) /* { */ +// #if defined(LUA_USE_POSIX) /* { */ -#include +// #include -#define l_fseek(f,o,w) fseeko(f,o,w) -#define l_ftell(f) ftello(f) -#define l_seeknum off_t +// #define l_fseek(f,o,w) fseeko(f,o,w) +// #define l_ftell(f) ftello(f) +// #define l_seeknum off_t -#elif defined(LUA_USE_WINDOWS) && !defined(_CRTIMP_TYPEINFO) \ - && defined(_MSC_VER) && (_MSC_VER >= 1400) /* }{ */ +// #elif defined(LUA_USE_WINDOWS) && !defined(_CRTIMP_TYPEINFO) \ +// && defined(_MSC_VER) && (_MSC_VER >= 1400) /* }{ */ -/* Windows (but not DDK) and Visual C++ 2005 or higher */ -#define l_fseek(f,o,w) _fseeki64(f,o,w) -#define l_ftell(f) _ftelli64(f) -#define l_seeknum __int64 +// /* Windows (but not DDK) and Visual C++ 2005 or higher */ +// #define l_fseek(f,o,w) _fseeki64(f,o,w) +// #define l_ftell(f) _ftelli64(f) +// #define l_seeknum __int64 -#else /* }{ */ +// #else /* }{ */ -/* ISO C definitions */ -#define l_fseek(f,o,w) fseek(f,o,w) -#define l_ftell(f) ftell(f) -#define l_seeknum long +// /* ISO C definitions */ +// #define l_fseek(f,o,w) fseek(f,o,w) +// #define l_ftell(f) ftell(f) +// #define l_seeknum long -#endif /* } */ +// #endif /* } */ -#endif /* } */ +// #endif /* } */ /* }====================================================== */ @@ -165,18 +165,18 @@ typedef struct LStream { #define isclosed(p) ((p)->closef == NULL) -static int io_type (lua_State *L) { - LStream *p; - luaL_checkany(L, 1); - p = (LStream *)luaL_testudata(L, 1, LUA_FILEHANDLE); - if (p == NULL) - lua_pushnil(L); /* not a file */ - else if (isclosed(p)) - lua_pushliteral(L, "closed file"); - else - lua_pushliteral(L, "file"); - return 1; -} +// static int io_type (lua_State *L) { +// LStream *p; +// luaL_checkany(L, 1); +// p = (LStream *)luaL_testudata(L, 1, LUA_FILEHANDLE); +// if (p == NULL) +// lua_pushnil(L); /* not a file */ +// else if (isclosed(p)) +// lua_pushliteral(L, "closed file"); +// else +// lua_pushliteral(L, "file"); +// return 1; +// } static FIL *tofile (lua_State *L) { @@ -214,23 +214,27 @@ static int aux_close (lua_State *L) { } -static int file_close (lua_State *L) { - tofile(L); /* make sure argument is an open stream */ - return aux_close(L); -} +// static int file_close (lua_State *L) { +// tofile(L); /* make sure argument is an open stream */ +// return aux_close(L); +// } static int io_close (lua_State *L) { - if (lua_isnone(L, 1)) /* no argument? */ - lua_getfield(L, LUA_REGISTRYINDEX, IO_OUTPUT); /* use standard output */ - return file_close(L); + // if (lua_isnone(L, 1)) /* no argument? */ + // lua_getfield(L, LUA_REGISTRYINDEX, IO_OUTPUT); /* use standard output */ + // return file_close(L); + f_close(tofile(L)); + return 0; } static int file_gc (lua_State *L) { - LStream *p = tolstream(L); - if (!isclosed(p)) - aux_close(L); /* ignore closed and incompletely open files */ + // LStream *p = tolstream(L); + // if (!isclosed(p)) + // aux_close(L); /* ignore closed and incompletely open files */ + // return 0; + f_close(tofile(L)); // no need to check if file was already closed (fatfs will not close it if p->f->fs is 0) return 0; } @@ -264,11 +268,11 @@ static inline BYTE fatfs_open_mode(const char *mode) } } -static void opencheck (lua_State *L, const char *fname, const char *mode) { - LStream *p = newfile(L); - if (f_open(&p->f, fname, fatfs_open_mode(mode)) != FR_OK) - luaL_error(L, "cannot open file '%s' (%s)", fname, strerror(errno)); -} +// static void opencheck (lua_State *L, const char *fname, const char *mode) { +// LStream *p = newfile(L); +// if (f_open(&p->f, fname, fatfs_open_mode(mode)) != FR_OK) +// luaL_error(L, "cannot open file '%s' (%s)", fname, strerror(errno)); +// } static int io_open (lua_State *L) { @@ -291,77 +295,77 @@ static FIL *getiofile (lua_State *L, const char *findex) { } -static int g_iofile (lua_State *L, const char *f, const char *mode) { - if (!lua_isnoneornil(L, 1)) { - const char *filename = lua_tostring(L, 1); - if (filename) - opencheck(L, filename, mode); - else { - tofile(L); /* check that it's a valid file handle */ - lua_pushvalue(L, 1); - } - lua_setfield(L, LUA_REGISTRYINDEX, f); - } - /* return current value */ - lua_getfield(L, LUA_REGISTRYINDEX, f); - return 1; -} +// static int g_iofile (lua_State *L, const char *f, const char *mode) { +// if (!lua_isnoneornil(L, 1)) { +// const char *filename = lua_tostring(L, 1); +// if (filename) +// opencheck(L, filename, mode); +// else { +// tofile(L); /* check that it's a valid file handle */ +// lua_pushvalue(L, 1); +// } +// lua_setfield(L, LUA_REGISTRYINDEX, f); +// } +// /* return current value */ +// lua_getfield(L, LUA_REGISTRYINDEX, f); +// return 1; +// } -static int io_input (lua_State *L) { - return g_iofile(L, IO_INPUT, "r"); -} +// static int io_input (lua_State *L) { +// return g_iofile(L, IO_INPUT, "r"); +// } -static int io_output (lua_State *L) { - return g_iofile(L, IO_OUTPUT, "w"); -} +// static int io_output (lua_State *L) { +// return g_iofile(L, IO_OUTPUT, "w"); +// } -static int io_readline (lua_State *L); +// static int io_readline (lua_State *L); /* ** maximum number of arguments to 'f:lines'/'io.lines' (it + 3 must fit ** in the limit for upvalues of a closure) */ -#define MAXARGLINE 250 - -static void aux_lines (lua_State *L, int toclose) { - int n = lua_gettop(L) - 1; /* number of arguments to read */ - luaL_argcheck(L, n <= MAXARGLINE, MAXARGLINE + 2, "too many arguments"); - lua_pushinteger(L, n); /* number of arguments to read */ - lua_pushboolean(L, toclose); /* close/not close file when finished */ - lua_rotate(L, 2, 2); /* move 'n' and 'toclose' to their positions */ - lua_pushcclosure(L, io_readline, 3 + n); -} - - -static int file_lines (lua_State *L) { - tofile(L); /* check that it's a valid file handle */ - aux_lines(L, 0); - return 1; -} - - -static int io_lines (lua_State *L) { - int toclose; - if (lua_isnone(L, 1)) lua_pushnil(L); /* at least one argument */ - if (lua_isnil(L, 1)) { /* no file name? */ - lua_getfield(L, LUA_REGISTRYINDEX, IO_INPUT); /* get default input */ - lua_replace(L, 1); /* put it at index 1 */ - tofile(L); /* check that it's a valid file handle */ - toclose = 0; /* do not close it after iteration */ - } - else { /* open a new file */ - const char *filename = luaL_checkstring(L, 1); - opencheck(L, filename, "r"); - lua_replace(L, 1); /* put file at index 1 */ - toclose = 1; /* close it after iteration */ - } - aux_lines(L, toclose); - return 1; -} +// #define MAXARGLINE 250 + +// static void aux_lines (lua_State *L, int toclose) { +// int n = lua_gettop(L) - 1; /* number of arguments to read */ +// luaL_argcheck(L, n <= MAXARGLINE, MAXARGLINE + 2, "too many arguments"); +// lua_pushinteger(L, n); /* number of arguments to read */ +// lua_pushboolean(L, toclose); /* close/not close file when finished */ +// lua_rotate(L, 2, 2); /* move 'n' and 'toclose' to their positions */ +// lua_pushcclosure(L, io_readline, 3 + n); +// } + + +// static int file_lines (lua_State *L) { +// tofile(L); /* check that it's a valid file handle */ +// aux_lines(L, 0); +// return 1; +// } + + +// static int io_lines (lua_State *L) { +// int toclose; +// if (lua_isnone(L, 1)) lua_pushnil(L); /* at least one argument */ +// if (lua_isnil(L, 1)) { /* no file name? */ +// lua_getfield(L, LUA_REGISTRYINDEX, IO_INPUT); /* get default input */ +// lua_replace(L, 1); /* put it at index 1 */ +// tofile(L); /* check that it's a valid file handle */ +// toclose = 0; /* do not close it after iteration */ +// } +// else { /* open a new file */ +// const char *filename = luaL_checkstring(L, 1); +// opencheck(L, filename, "r"); +// lua_replace(L, 1); /* put file at index 1 */ +// toclose = 1; /* close it after iteration */ +// } +// aux_lines(L, toclose); +// return 1; +// } /* @@ -471,43 +475,50 @@ static int g_read (lua_State *L, FIL *f, int first) { } -static int io_read (lua_State *L) { - return g_read(L, getiofile(L, IO_INPUT), 1); -} - +// static int io_read (lua_State *L) { +// return g_read(L, getiofile(L, IO_INPUT), 1); +// } -static int file_read (lua_State *L) { - return g_read(L, tofile(L), 2); +static int io_read (lua_State *L) { + LStream *p = tolstream(L); + size_t l = (size_t)lua_tointeger(L, 2); + read_chars(L, &p->f, l); + return 1; } -static int io_readline (lua_State *L) { - LStream *p = (LStream *)lua_touserdata(L, lua_upvalueindex(1)); - int i; - int n = (int)lua_tointeger(L, lua_upvalueindex(2)); - if (isclosed(p)) /* file is already closed? */ - return luaL_error(L, "file is already closed"); - lua_settop(L , 1); - luaL_checkstack(L, n, "too many arguments"); - for (i = 1; i <= n; i++) /* push arguments to 'g_read' */ - lua_pushvalue(L, lua_upvalueindex(3 + i)); - n = g_read(L, &p->f, 2); /* 'n' is number of results */ - lua_assert(n > 0); /* should return at least a nil */ - if (lua_toboolean(L, -n)) /* read at least one value? */ - return n; /* return them */ - else { /* first result is nil: EOF or error */ - if (n > 1) { /* is there error information? */ - /* 2nd result is error message */ - return luaL_error(L, "%s", lua_tostring(L, -n + 1)); - } - if (lua_toboolean(L, lua_upvalueindex(3))) { /* generator created file? */ - lua_settop(L, 0); - lua_pushvalue(L, lua_upvalueindex(1)); - aux_close(L); /* close it */ - } - return 0; - } -} +// static int file_read (lua_State *L) { +// return g_read(L, tofile(L), 2); +// } + + +// static int io_readline (lua_State *L) { +// LStream *p = (LStream *)lua_touserdata(L, lua_upvalueindex(1)); +// int i; +// int n = (int)lua_tointeger(L, lua_upvalueindex(2)); +// if (isclosed(p)) /* file is already closed? */ +// return luaL_error(L, "file is already closed"); +// lua_settop(L , 1); +// luaL_checkstack(L, n, "too many arguments"); +// for (i = 1; i <= n; i++) /* push arguments to 'g_read' */ +// lua_pushvalue(L, lua_upvalueindex(3 + i)); +// n = g_read(L, &p->f, 2); /* 'n' is number of results */ +// lua_assert(n > 0); /* should return at least a nil */ +// if (lua_toboolean(L, -n)) /* read at least one value? */ +// return n; /* return them */ +// else { /* first result is nil: EOF or error */ +// if (n > 1) { /* is there error information? */ +// /* 2nd result is error message */ +// return luaL_error(L, "%s", lua_tostring(L, -n + 1)); +// } +// if (lua_toboolean(L, lua_upvalueindex(3))) { /* generator created file? */ +// lua_settop(L, 0); +// lua_pushvalue(L, lua_upvalueindex(1)); +// aux_close(L); /* close it */ +// } +// return 0; +// } +// } /* }====================================================== */ @@ -537,37 +548,50 @@ static int g_write (lua_State *L, FIL *f, int arg) { } -static int io_write (lua_State *L) { - return g_write(L, getiofile(L, IO_OUTPUT), 1); -} +// static int io_write (lua_State *L) { +// return g_write(L, getiofile(L, IO_OUTPUT), 1); +// } - -static int file_write (lua_State *L) { +static int io_write (lua_State *L) { FIL *f = tofile(L); lua_pushvalue(L, 1); /* push file at the stack top (to be returned) */ return g_write(L, f, 2); } -static int file_seek (lua_State *L) { - static const char *const modenames[] = {"set", "cur", "end", NULL}; +// static int file_write (lua_State *L) { +// FIL *f = tofile(L); +// lua_pushvalue(L, 1); /* push file at the stack top (to be returned) */ +// return g_write(L, f, 2); +// } + + +// static int file_seek (lua_State *L) { +// static const char *const modenames[] = {"set", "cur", "end", NULL}; +// FIL *f = tofile(L); +// int op = luaL_checkoption(L, 2, "cur", modenames); +// lua_Integer p3 = luaL_optinteger(L, 3, 0); +// l_seeknum offset = (l_seeknum)p3; +// luaL_argcheck(L, (lua_Integer)offset == p3, 3, +// "not an integer in proper range"); +// if (op == 1) { +// offset += (l_seeknum)f_tell(f); +// } else if (op == 2) { +// offset += (l_seeknum)f_size(f); +// } +// if (f_lseek(f, offset) != FR_OK) +// return luaL_fileresult(L, 0, NULL); /* error */ +// else { +// lua_pushinteger(L, (lua_Integer)f_tell(f)); +// return 1; +// } +// } + +static int io_seek (lua_State *L) { FIL *f = tofile(L); - int op = luaL_checkoption(L, 2, "cur", modenames); - lua_Integer p3 = luaL_optinteger(L, 3, 0); - l_seeknum offset = (l_seeknum)p3; - luaL_argcheck(L, (lua_Integer)offset == p3, 3, - "not an integer in proper range"); - if (op == 1) { - offset += (l_seeknum)f_tell(f); - } else if (op == 2) { - offset += (l_seeknum)f_size(f); - } - if (f_lseek(f, offset) != FR_OK) - return luaL_fileresult(L, 0, NULL); /* error */ - else { - lua_pushinteger(L, (lua_Integer)f_tell(f)); - return 1; - } + lua_Unsigned offset = luaL_checkunsigned(L, 2); + lua_pushinteger(L, f_lseek(f, offset)); + return 1; } @@ -576,12 +600,13 @@ static int file_seek (lua_State *L) { */ LROT_BEGIN(iolib, NULL, 0) LROT_FUNCENTRY( close, io_close ) - LROT_FUNCENTRY( input, io_input ) - LROT_FUNCENTRY( lines, io_lines ) + LROT_FUNCENTRY( seek, io_seek ) + // LROT_FUNCENTRY( input, io_input ) + // LROT_FUNCENTRY( lines, io_lines ) LROT_FUNCENTRY( open, io_open ) - LROT_FUNCENTRY( output, io_output ) + // LROT_FUNCENTRY( output, io_output ) LROT_FUNCENTRY( read, io_read ) - LROT_FUNCENTRY( type, io_type ) + // LROT_FUNCENTRY( type, io_type ) LROT_FUNCENTRY( write, io_write ) LROT_END(iolib, NULL, 0) @@ -591,11 +616,11 @@ LROT_END(iolib, NULL, 0) ** we define only garbage collector to close any leftover open files */ LROT_BEGIN(file_handle, NULL, LROT_MASK_GC) - LROT_FUNCENTRY( close, file_close ) - LROT_FUNCENTRY( lines, file_lines ) - LROT_FUNCENTRY( read, file_read ) - LROT_FUNCENTRY( seek, file_seek ) - LROT_FUNCENTRY( write, file_write ) + // LROT_FUNCENTRY( close, file_close ) + // LROT_FUNCENTRY( lines, file_lines ) + // LROT_FUNCENTRY( read, file_read ) + // LROT_FUNCENTRY( seek, file_seek ) + // LROT_FUNCENTRY( write, file_write ) LROT_FUNCENTRY( __gc, file_gc ) LROT_END(file_handle, NULL, LROT_MASK_GC)