From 032a38df4e2f2d0b0d72ad6657b52b39bef1c9e1 Mon Sep 17 00:00:00 2001 From: William Wilgus Date: Tue, 16 Dec 2025 10:35:03 -0500 Subject: [PATCH] lua -- remove direct rb-> libc calls infavor of macro substitution WIP there are no functional changes in this patch it just makes it easier to build lua in core app or plugin form -Update moved some things around messing up compilation on Native targets due to *errno Change-Id: I0921df62d72a87516ad95c68e986b5931c35345e --- apps/plugins/lua/lauxlib.c | 10 ++++----- apps/plugins/lua/liolib.c | 28 +++++++++++------------ apps/plugins/lua/lmathlib.c | 4 ++-- apps/plugins/lua/loadlib.c | 4 ++-- apps/plugins/lua/loslib.c | 6 ++--- apps/plugins/lua/luadir.c | 14 ++++++------ apps/plugins/lua/rockaux.c | 44 +++++++++++++++++++++++-------------- apps/plugins/lua/rockconf.h | 6 ++++- apps/plugins/lua/rocklib.c | 2 +- apps/plugins/lua/rocklibc.h | 35 +++++++++++++++++++++++++++++ apps/plugins/lua/rocklua.c | 6 ++--- apps/plugins/lua/strftime.c | 5 ++++- 12 files changed, 109 insertions(+), 55 deletions(-) diff --git a/apps/plugins/lua/lauxlib.c b/apps/plugins/lua/lauxlib.c index 9a5939aff9..3c95328c39 100644 --- a/apps/plugins/lua/lauxlib.c +++ b/apps/plugins/lua/lauxlib.c @@ -25,10 +25,10 @@ #include "lauxlib.h" +#include "rocklibc.h" /* ROCKLUA ADDED */ #define FREELIST_REF 0 /* free list of references */ - /* convert a stack index to positive */ #define abs_index(L, i) ((i) > 0 || (i) <= LUA_REGISTRYINDEX ? (i) : \ lua_gettop(L) + (i) + 1) @@ -701,7 +701,7 @@ static const char *getF (lua_State *L, void *ud, size_t *size) { *size = 1; return "\n"; } - *size = rb->read(lf->f, lf->buff, LUAL_BUFFERSIZE); + *size = read(lf->f, lf->buff, LUAL_BUFFERSIZE); return (*size > 0) ? lf->buff : NULL; } @@ -719,11 +719,11 @@ LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) { int status; int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ lf.extraline = 0; - lf.f = rb->open(filename, O_RDONLY); + lf.f = open(filename, O_RDONLY); lua_pushfstring(L, "@%s", filename); if (lf.f < 0) return errfile(L, "open", fnameindex); status = lua_load(L, getF, &lf, lua_tostring(L, -1)); - rb->close(lf.f); + close(lf.f); lua_remove(L, fnameindex); return status; } @@ -794,7 +794,7 @@ static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { static int panic (lua_State *L) { DEBUGF("PANIC: unprotected error in call to Lua API (%s)\n", lua_tostring(L, -1)); - rb->splashf(5 * HZ, "PANIC: unprotected error in call to Lua API (%s)", + splashf(5 * HZ, "PANIC: unprotected error in call to Lua API (%s)", lua_tostring(L, -1)); return 0; diff --git a/apps/plugins/lua/liolib.c b/apps/plugins/lua/liolib.c index 81edb9a534..b44a732369 100644 --- a/apps/plugins/lua/liolib.c +++ b/apps/plugins/lua/liolib.c @@ -8,7 +8,7 @@ #include #include #include - +#include "rocklibc.h" /* ROCKLUA ADDED */ #define liolib_c #define LUA_LIB @@ -16,8 +16,8 @@ #include "lauxlib.h" #include "lualib.h" -#include "rocklibc.h" -#include "rocklib.h" + +#include "rocklib.h" /* ROCKLUA ADDED */ #include "llimits.h" @@ -96,7 +96,7 @@ static int* newfile (lua_State *L) { */ static int io_fclose (lua_State *L) { int *p = tofile(L); - int ok = (rb->close(*p) == 0); + int ok = (close(*p) == 0); *p = -1; return pushresult(L, ok, NULL); } @@ -165,7 +165,7 @@ static int io_open (lua_State *L) { flags |= wrmode; - *pf = rb->open(filename, flags, 0666); + *pf = open(filename, flags, 0666); return (*pf < 0) ? pushresult(L, 0, filename) : 1; } @@ -185,7 +185,7 @@ static int g_iofile (lua_State *L, int f, int flags) { const char *filename = lua_tostring(L, 1); if (filename) { int *pf = newfile(L); - *pf = rb->open(filename, flags); + *pf = open(filename, flags); if (*pf < 0) fileerror(L, 1, filename); } @@ -237,7 +237,7 @@ static int io_lines (lua_State *L) { else { const char *filename = luaL_checkstring(L, 1); int *pf = newfile(L); - *pf = rb->open(filename, O_RDONLY); + *pf = open(filename, O_RDONLY); if (*pf < 0) fileerror(L, 1, filename); aux_lines(L, lua_gettop(L), 1); @@ -266,9 +266,9 @@ static int read_number (lua_State *L, int *f) { static int test_eof (lua_State *L, int *f) { - off_t s = rb->lseek(*f, 0, SEEK_CUR); + off_t s = lseek(*f, 0, SEEK_CUR); lua_pushlstring(L, NULL, 0); - return s < rb->filesize(*f); + return s < filesize(*f); } @@ -279,7 +279,7 @@ static int _read_line (lua_State *L, int *f) { for (;;) { size_t l; char *p = luaL_prepbuffer(&b); - off_t r = rb->read_line(*f, p, LUAL_BUFFERSIZE); /* does not include `eol' */ + off_t r = read_line(*f, p, LUAL_BUFFERSIZE); /* does not include `eol' */ if (r <= 0) { /* eof? */ luaL_pushresult(&b); /* close buffer */ return (lua_objlen(L, -1) > 0); /* check whether read something */ @@ -304,7 +304,7 @@ static int read_chars (lua_State *L, int *f, size_t n) { do { char *p = luaL_prepbuffer(&b); if (rlen > n) rlen = n; /* cannot read more than asked */ - nr = rb->read(*f, p, rlen); + nr = read(*f, p, rlen); if (nr < 0) luaL_error(L, "error reading file"); luaL_addsize(&b, nr); @@ -396,12 +396,12 @@ static int g_write (lua_State *L, int *f, int arg) { if (lua_type(L, arg) == LUA_TNUMBER) { /* optimization: could be done exactly as for strings */ status = status && - rb->fdprintf(*f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0; + fdprintf(*f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0; } else { size_t l; const char *s = luaL_checklstring(L, arg, &l); - status = status && (rb->write(*f, s, l) == (ssize_t)l); + status = status && (write(*f, s, l) == (ssize_t)l); } } return pushresult(L, status, NULL); @@ -424,7 +424,7 @@ static int f_seek (lua_State *L) { int *f = tofile(L); int op = luaL_checkoption(L, 2, "cur", modenames); long offset = luaL_optlong(L, 3, 0); - off_t size = rb->lseek(*f, offset, mode[op]); + off_t size = lseek(*f, offset, mode[op]); if (size < 0 || size > MAX_INT) /* signed limit */ return pushresult(L, 0, NULL); /* error */ else { diff --git a/apps/plugins/lua/lmathlib.c b/apps/plugins/lua/lmathlib.c index 839d2014ad..3a868a1521 100644 --- a/apps/plugins/lua/lmathlib.c +++ b/apps/plugins/lua/lmathlib.c @@ -191,7 +191,7 @@ static int math_max (lua_State *L) { static int math_random (lua_State *L) { /* We're not SunOS */ - lua_Number r = (lua_Number)(rb->rand()); + lua_Number r = (lua_Number)(rand()); switch (lua_gettop(L)) { /* check number of arguments */ case 0: { /* no arguments */ lua_pushnumber(L, r); /* Number between 0 and RAND_MAX */ @@ -217,7 +217,7 @@ static int math_random (lua_State *L) { static int math_randomseed (lua_State *L) { - rb->srand(luaL_checkint(L, 1)); + srand(luaL_checkint(L, 1)); return 0; } diff --git a/apps/plugins/lua/loadlib.c b/apps/plugins/lua/loadlib.c index 732ad707b5..49383028fd 100644 --- a/apps/plugins/lua/loadlib.c +++ b/apps/plugins/lua/loadlib.c @@ -34,9 +34,9 @@ static int readable (const char *filename) { - int f = rb->open(filename, O_RDONLY); /* try to open file */ + int f = open(filename, O_RDONLY); /* try to open file */ if (f < 0) return 0; /* open failed */ - rb->close(f); + close(f); return 1; } diff --git a/apps/plugins/lua/loslib.c b/apps/plugins/lua/loslib.c index fcd5c6a16c..8ab1cabde9 100644 --- a/apps/plugins/lua/loslib.c +++ b/apps/plugins/lua/loslib.c @@ -17,7 +17,7 @@ #include "lauxlib.h" #include "lualib.h" - +#include "rocklibc.h" /* ROCKLUA ADDED */ static int os_pushresult (lua_State *L, int i, const char *filename) { int en = errno; /* calls to Lua API may change this value */ @@ -36,14 +36,14 @@ static int os_pushresult (lua_State *L, int i, const char *filename) { static int os_remove (lua_State *L) { const char *filename = luaL_checkstring(L, 1); - return os_pushresult(L, rb->remove(filename) == 0, filename); + return os_pushresult(L, remove(filename) == 0, filename); } static int os_rename (lua_State *L) { const char *fromname = luaL_checkstring(L, 1); const char *toname = luaL_checkstring(L, 2); - return os_pushresult(L, rb->rename(fromname, toname) == 0, fromname); + return os_pushresult(L, rename(fromname, toname) == 0, fromname); } diff --git a/apps/plugins/lua/luadir.c b/apps/plugins/lua/luadir.c index bea26661e1..cf0b4a9f8b 100644 --- a/apps/plugins/lua/luadir.c +++ b/apps/plugins/lua/luadir.c @@ -37,13 +37,13 @@ typedef struct dir_data { static int make_dir (lua_State *L) { const char *path = luaL_checkstring (L, 1); - lua_pushboolean (L, !rb->mkdir(path)); + lua_pushboolean (L, !mkdir(path)); return 1; } static int remove_dir (lua_State *L) { const char *path = luaL_checkstring (L, 1); - lua_pushboolean (L, !rb->rmdir (path)); + lua_pushboolean (L, !rmdir (path)); return 1; } @@ -57,8 +57,8 @@ static int dir_iter (lua_State *L) { luaL_argcheck (L, !d->closed, 1, "closed directory"); - if ((entry = rb->readdir (d->dir)) != NULL) { - struct dirinfo info = rb->dir_get_info(d->dir, entry); + if ((entry = readdir (d->dir)) != NULL) { + struct dirinfo info = dir_get_info(d->dir, entry); lua_pushstring (L, entry->d_name); lua_pushboolean (L, info.attribute & ATTR_DIRECTORY); if (lua_toboolean (L, lua_upvalueindex(1))) { @@ -77,7 +77,7 @@ static int dir_iter (lua_State *L) { return 3; } else { /* no more entries => close directory */ - rb->closedir (d->dir); + closedir (d->dir); d->closed = 1; return 0; } @@ -91,7 +91,7 @@ static int dir_close (lua_State *L) { dir_data *d = (dir_data *)lua_touserdata (L, 1); if (!d->closed && d->dir) { - rb->closedir (d->dir); + closedir (d->dir); d->closed = 1; } return 0; @@ -111,7 +111,7 @@ static int dir_iter_factory (lua_State *L) { luaL_getmetatable (L, DIR_METATABLE); lua_setmetatable (L, -2); - d->dir = rb->opendir (path); + d->dir = opendir (path); if (d->dir == NULL) { luaL_error (L, "cannot open %s: %d", path, errno); diff --git a/apps/plugins/lua/rockaux.c b/apps/plugins/lua/rockaux.c index 95986943c7..7eb5ff0d28 100644 --- a/apps/plugins/lua/rockaux.c +++ b/apps/plugins/lua/rockaux.c @@ -21,10 +21,22 @@ * ****************************************************************************/ -#include "plugin.h" -#define _ROCKCONF_H_ /* Protect against unwanted include */ #include "lua.h" +#include "rocklibc.h" /* ROCKLUA ADDED */ + +#ifdef PLUGIN +#include "plugin.h" #include "lib/pluginlib_actions.h" +#define lcd_getstringsize rb->lcd_getstringsize +#define lcd_clear_display rb->lcd_clear_display +#define lcd_putsxy rb->lcd_putsxy +#define lcd_update rb->lcd_update +#define beep_play rb->beep_play +#define get_action rb->get_action +#if 0 /* ndef _WIN32 -- supplied by strfrtime.lua */ +#define gmtime_r rb->gmtime_r +#endif +#endif /* def PLUGIN*/ extern const char *strpbrk_n(const char *s, int smax, const char *accept); @@ -54,7 +66,7 @@ int splash_scroller(int timeout, const char* str) if (!str) str = "[nil]"; int w, ch_w, ch_h; - rb->lcd_getstringsize("W", &ch_w, &ch_h); + lcd_getstringsize("W", &ch_w, &ch_h); const int max_w = LCD_WIDTH - (ch_w * 2); const int max_lines = LCD_HEIGHT / ch_h - 1; @@ -74,7 +86,7 @@ int splash_scroller(int timeout, const char* str) while (cycles > 0) { /* walk whole buffer every refresh, only display relevant portion */ - rb->lcd_clear_display(); + lcd_clear_display(); curline = 0; linepos = 0; linesdisp = 0; @@ -89,7 +101,7 @@ int splash_scroller(int timeout, const char* str) else if (ch[0] == '\b' && timeout > 0) { line[linepos] = ' '; - rb->beep_play(1000, HZ, 1000); + beep_play(1000, HZ, 1000); } else if (ch[0] < ' ') /* Dont copy control characters */ line[linepos] = (linepos == 0) ? '\0' : ' '; @@ -97,7 +109,7 @@ int splash_scroller(int timeout, const char* str) line[linepos] = ch[0]; line[linepos + 1] = '\0'; /* terminate to check text extent */ - rb->lcd_getstringsize(line, &w, NULL); + lcd_getstringsize(line, &w, NULL); /* try to not split in middle of words */ if (w + wrap_thresh >= max_w && @@ -124,7 +136,7 @@ int splash_scroller(int timeout, const char* str) realline = curline - firstline; if (realline >= 0 && realline < max_lines) { - rb->lcd_putsxy(0, realline * ch_h, line); + lcd_putsxy(0, realline * ch_h, line); linesdisp++; } linepos = 0; @@ -134,10 +146,10 @@ int splash_scroller(int timeout, const char* str) linepos++; } - rb->lcd_update(); + lcd_update(); if (timeout >= TIMEOUT_BLOCK) { - action = rb->get_action(CONTEXT_STD, timeout); + action = get_action(CONTEXT_STD, timeout); switch(action) { case ACTION_STD_OK: @@ -198,14 +210,14 @@ long rb_pow(long x, long n) int strcoll(const char * str1, const char * str2) { - return rb->strcmp(str1, str2); + return strcmp(str1, str2); } #if 0 //ndef _WIN32 /* supplied by strfrtime.lua*/ struct tm * gmtime(const time_t *timep) { static struct tm time; - return rb->gmtime_r(timep, &time); + return gmtime_r(timep, &time); } #endif @@ -220,7 +232,7 @@ int get_current_path(lua_State *L, int level) lua_getinfo(L, "S", &ar); const char* curfile = &ar.source[1]; - const char* pos = rb->strrchr(curfile, '/'); + const char* pos = strrchr(curfile, '/'); if(pos != NULL) { lua_pushlstring (L, curfile, pos - curfile + 1); @@ -250,7 +262,7 @@ int filetol(int fd, long *num) bool neg = false; long val; - while (rb->read(fd, &chbuf, 1) == 1) + while (read(fd, &chbuf, 1) == 1) { if(retn || !isspace(chbuf)) { @@ -275,12 +287,12 @@ int filetol(int fd, long *num) } } - while (rb->read(fd, &chbuf, 1) == 1) + while (read(fd, &chbuf, 1) == 1) { get_digits: if(!isdigit(chbuf)) { - rb->lseek(fd, -1, SEEK_CUR); + lseek(fd, -1, SEEK_CUR); break; } else if (count < LUAI_MAXNUMBER2STR - 2) @@ -290,7 +302,7 @@ get_digits: if(count) { buffer[count] = '\0'; - val = rb->strtol(buffer, NULL, 10); + val = strtol(buffer, NULL, 10); *num = (neg)? -val:val; retn = 1; } diff --git a/apps/plugins/lua/rockconf.h b/apps/plugins/lua/rockconf.h index 87fec966db..d55b3d434f 100644 --- a/apps/plugins/lua/rockconf.h +++ b/apps/plugins/lua/rockconf.h @@ -70,5 +70,9 @@ int splash_scroller(int timeout, const char* str); #define mktime rb->mktime #define get_time rb->get_time +#define read_line(fd,buf,n) rb->read_line(fd,buf,n) +#define fdprintf(...) rb->fdprintf(__VA_ARGS__) +#define splashf(...) rb->splashf(__VA_ARGS__) +#define rand() rb->rand() +#define srand(i) rb->srand(i) #endif /* _ROCKCONF_H_ */ - diff --git a/apps/plugins/lua/rocklib.c b/apps/plugins/lua/rocklib.c index bd9d60959d..08597f8ff5 100644 --- a/apps/plugins/lua/rocklib.c +++ b/apps/plugins/lua/rocklib.c @@ -308,7 +308,7 @@ RB_WRAP(splash) { int timeout = luaL_checkint(L, 1); const char *str = luaL_checkstring(L, 2); - rb->splashf(timeout, str); /*rockaux.c*/ + splashf(timeout, str); /*rockaux.c*/ return 0; } diff --git a/apps/plugins/lua/rocklibc.h b/apps/plugins/lua/rocklibc.h index f119265e8e..3902bf8fb8 100644 --- a/apps/plugins/lua/rocklibc.h +++ b/apps/plugins/lua/rocklibc.h @@ -26,6 +26,8 @@ #include #include "plugin.h" +#ifndef LUA_LIB + #if (CONFIG_PLATFORM & PLATFORM_HOSTED) #include #define PREFIX(_x_) sim_ ## _x_ @@ -35,14 +37,47 @@ extern int errno; #define ERANGE 34 /* Math result not representable */ #define PREFIX #endif +#endif #define __likely LIKELY #define __unlikely UNLIKELY +#ifdef PLUGIN /* Simple substitutions */ #define memcmp rb->memcmp #define strlen rb->strlen #define strrchr rb->strrchr +#define write(fd,buf,n) rb->write(fd,buf,n) +#define read(fd,buf,n) rb->read(fd,buf,n) +#define open(...) rb->open(__VA_ARGS__) +#define close(fd) rb->close(fd) +#define lseek(fd,off,w) rb->lseek(fd,off,w) +#define filesize(fd) rb->filesize(fd) +#define ftruncate(fd,l) rb->ftruncate(fd,l) +#define file_exists(p) rb->file_exists(p) +#define remove(f) rb->remove(f) +#define rename(old,new) rb->rename(old,new) + +#define mkdir(p) rb->mkdir(p) +#define rmdir(p) rb->rmdir(p) +#define opendir(p) rb->opendir(p) +#define closedir(d) rb->closedir(d) +#define readdir(d) rb->readdir(d) +#define dir_get_info(d,e) rb->dir_get_info(d,e) + +#else /*ndef PLUGIN*/ +#define write(fd,buf,n) PREFIX(write)(fd,buf,n) +#define read(fd,buf,n) PREFIX(read)(fd,buf,n) +#define open(...) PREFIX(open)(__VA_ARGS__) +#define close(fd) PREFIX(close)(fd) +#define lseek(fd,off,w) PREFIX(lseek)(fd,off,w) +#define filesize(fd) PREFIX(filesize)(fd) +#define ftruncate(fd,l) PREFIX(ftruncate)(fd,l) +#define file_exists(p) PREFIX(file_exists)(p) +#define remove(f) PREFIX(remove)(f) +#define rename(old,new) PREFIX(rename)(old,new) + +#endif /* def PLUGIN */ #endif /* _ROCKLIBC_H_ */ diff --git a/apps/plugins/lua/rocklua.c b/apps/plugins/lua/rocklua.c index 650c4d6dae..f35f2462f6 100644 --- a/apps/plugins/lua/rocklua.c +++ b/apps/plugins/lua/rocklua.c @@ -238,7 +238,7 @@ static void display_traceback(const char *errstr) #if 1 splash_scroller(HZ * 5, errstr); /*rockaux.c*/ #else - rb->splash(10 * HZ, errstr); + splash(10 * HZ, errstr); #endif } @@ -264,7 +264,7 @@ enum plugin_status plugin_start(const void* parameter) { if (!Ls) { - rb->splash(HZ, "Play a .lua file!"); + splash(HZ, "Play a .lua file!"); return browse_scripts(); } } @@ -285,7 +285,7 @@ enum plugin_status plugin_start(const void* parameter) if (lu_status) { DEBUGF("%s\n", lua_tostring(Ls, -1)); display_traceback(lua_tostring(Ls, -1)); - //rb->splash(10 * HZ, lua_tostring(Ls, -1)); + //splash(10 * HZ, lua_tostring(Ls, -1)); /*lua_pop(Ls, 1);*/ } lua_close(Ls); diff --git a/apps/plugins/lua/strftime.c b/apps/plugins/lua/strftime.c index cc110469bf..6f5e6fc303 100644 --- a/apps/plugins/lua/strftime.c +++ b/apps/plugins/lua/strftime.c @@ -1,6 +1,9 @@ #include #include #include "plugin.h" +#ifdef PLUGIN +#define mktime(t) rb->mktime(t) +#endif static const char sweekdays [7] [4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" @@ -85,7 +88,7 @@ again: case 'W': no = (tm->tm_yday - (tm->tm_wday - 1 + 7) % 7 + 7) / 7; goto _no; case 's': { #if CONFIG_RTC - time_t t = rb->mktime((struct tm*)tm); + time_t t = mktime((struct tm*)tm); char* c; sbuf[100]=0; for (c=sbuf+99; c>sbuf; --c) {