From f8334e283c5eb0efeb4bb8a134041e18388d5f01 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Fri, 21 Apr 2017 16:57:00 +0200 Subject: Lua init --- src/sysbin/lx/lxinit.c | 1 + src/sysbin/lx/lxioctllib.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++ src/sysbin/lx/lxlib.c | 41 ++++++++++++++++++++++++++++++ src/sysbin/lx/lxlib.h | 10 ++++++-- src/sysbin/lx/lxsyslib.c | 43 +++----------------------------- src/sysbin/lx/main.c | 38 ++++++++++++++++++++-------- 6 files changed, 142 insertions(+), 53 deletions(-) create mode 100644 src/sysbin/lx/lxioctllib.c create mode 100644 src/sysbin/lx/lxlib.c (limited to 'src/sysbin/lx') diff --git a/src/sysbin/lx/lxinit.c b/src/sysbin/lx/lxinit.c index c185b45..b727ccd 100644 --- a/src/sysbin/lx/lxinit.c +++ b/src/sysbin/lx/lxinit.c @@ -35,6 +35,7 @@ static const luaL_Reg loadedlibs[] = { {LX_SYSLIBNAME, lx_open_sys}, {LX_MSGLIBNAME, lx_open_msg}, + {LX_IOCTLLIBNAME, lx_open_ioctl}, {NULL, NULL} }; diff --git a/src/sysbin/lx/lxioctllib.c b/src/sysbin/lx/lxioctllib.c new file mode 100644 index 0000000..4b9be91 --- /dev/null +++ b/src/sysbin/lx/lxioctllib.c @@ -0,0 +1,62 @@ +/* +** Lua eXtended ioctl library +*/ + +#define lxioctllib_c +#define LUA_LIB + +#include + + +#include +#include +#include +#include +#include + +#include + +#include +#include + +#include +#include + +#include "lxlib.h" + + +static int ioctl_fb_get_info(lua_State *L) { + int fd = luaL_checkinteger(L, 1); + + fb_info_t mode; + int r = sc_ioctl(fd, IOCTL_FB_GET_INFO, &mode); + if (r == 1) { + lua_createtable(L, 0, 5); + setintfield(L, "width", mode.width); + setintfield(L, "height", mode.height); + setintfield(L, "pitch", mode.pitch); + setintfield(L, "bpp", mode.bpp); + setintfield(L, "memory_model", mode.memory_model); + } else { + lua_pushnil(L); + } + + return 1; +} + + +/* }====================================================== */ + +static const luaL_Reg ioctllib[] = { + {"fb_get_info", ioctl_fb_get_info}, + {NULL, NULL} +}; + + +LUAMOD_API int lx_open_ioctl (lua_State *L) { + luaL_newlib(L, ioctllib); + return 1; +} + + +/* vim: set sts=2 ts=2 sw=2 tw=0 et :*/ diff --git a/src/sysbin/lx/lxlib.c b/src/sysbin/lx/lxlib.c new file mode 100644 index 0000000..9f6843b --- /dev/null +++ b/src/sysbin/lx/lxlib.c @@ -0,0 +1,41 @@ +/* +** Lua eXtended library helpers +*/ + +#define lxsyslib_c +#define LUA_LIB + +#include + + +#include +#include +#include +#include +#include + +#include + +#include +#include + +#include "lxlib.h" + + +bool lx_checkboolean(lua_State *L, int arg) { + if (!lua_isboolean(L, arg)) { + luaL_argerror(L, arg, "expected boolean"); + } + return lua_toboolean(L, arg); +} + + +void setintfield (lua_State *L, const char *key, int value) { + lua_pushinteger(L, value); + lua_setfield(L, -2, key); +} + +void setstrfield (lua_State *L, const char *key, const char* value) { + lua_pushstring(L, value); + lua_setfield(L, -2, key); +} diff --git a/src/sysbin/lx/lxlib.h b/src/sysbin/lx/lxlib.h index 199c78d..260db97 100644 --- a/src/sysbin/lx/lxlib.h +++ b/src/sysbin/lx/lxlib.h @@ -6,12 +6,18 @@ #include -#define LX_SYSLIBNAME "lx.sys" -LUAMOD_API int (lx_open_sys) (lua_State *L); +// Helper for libraries +bool lx_checkboolean(lua_State *L, int arg); +void setintfield (lua_State *L, const char *key, int value); +void setstrfield (lua_State *L, const char *key, const char* value); + #define LX_SYSLIBNAME "lx.sys" LUAMOD_API int (lx_open_sys) (lua_State *L); +#define LX_IOCTLLIBNAME "lx.ioctl" +LUAMOD_API int (lx_open_ioctl) (lua_State *L); + #define LX_MSGLIBNAME "lx.msg" LUAMOD_API int (luaopen_cmsgpack) (lua_State *L); #define lx_open_msg luaopen_cmsgpack diff --git a/src/sysbin/lx/lxsyslib.c b/src/sysbin/lx/lxsyslib.c index 52c6978..72c9882 100644 --- a/src/sysbin/lx/lxsyslib.c +++ b/src/sysbin/lx/lxsyslib.c @@ -22,25 +22,6 @@ #include "lxlib.h" -bool lx_checkboolean(lua_State *L, int arg) { - if (!lua_isboolean(L, arg)) { - luaL_argerror(L, arg, "expected boolean"); - } - return lua_toboolean(L, arg); -} - - -static void setintfield (lua_State *L, const char *key, int value) { - lua_pushinteger(L, value); - lua_setfield(L, -2, key); -} - -static void setstrfield (lua_State *L, const char *key, const char* value) { - lua_pushstring(L, value); - lua_setfield(L, -2, key); -} - - static int sys_dbg_print(lua_State *L) { const char *str = luaL_checkstring(L, 1); sc_dbg_print(str); @@ -68,6 +49,7 @@ static int sys_usleep(lua_State *L) { Missing syscalls : - sys_new_thread, exit_thread -> into specific threading library - mmap, mmap_file, mchmap, munmap -> into specific memory-related library ?? + - ioctl, fctl -> into ioctl library */ static int sys_create(lua_State *L) { @@ -180,25 +162,10 @@ static int sys_stat_open(lua_State *L) { return 1; } -static int sys_ioctl(lua_State *L) { - // TODO - return 0; -} - -static int sys_fctl(lua_State *L) { - // TODO - return 0; -} - static int sys_select(lua_State *L) { luaL_checktype(L, 1, LUA_TTABLE); int nfds = luaL_len(L, 1); - int timeout; - if (lua_isinteger(L, 2)) { - timeout = luaL_checkinteger(L, 2); - } else { - timeout = 0; - } + int timeout = luaL_checkinteger(L, 2); sel_fd_t *fds = (sel_fd_t*)malloc(nfds*sizeof(sel_fd_t)); if (fds == NULL) @@ -398,6 +365,7 @@ static int sys_proc_wait(lua_State *L) { return 1; } +/* }====================================================== */ static const luaL_Reg syslib[] = { {"dbg_print", sys_dbg_print}, @@ -414,8 +382,6 @@ static const luaL_Reg syslib[] = { {"write", sys_write}, {"readdir", sys_readdir}, {"stat_open", sys_stat_open}, - {"ioctl", sys_ioctl}, - {"fctl", sys_fctl}, {"select", sys_select}, {"make_channel",sys_make_channel}, {"make_shm", sys_make_shm}, @@ -438,9 +404,6 @@ static const luaL_Reg syslib[] = { {NULL, NULL} }; -/* }====================================================== */ - - LUAMOD_API int lx_open_sys (lua_State *L) { luaL_newlib(L, syslib); diff --git a/src/sysbin/lx/main.c b/src/sysbin/lx/main.c index b7e08d9..573e1bb 100644 --- a/src/sysbin/lx/main.c +++ b/src/sysbin/lx/main.c @@ -346,12 +346,26 @@ int handle_luainit (lua_State *L) { } -/* -** Main body of stand-alone interpreter (to be called in protected mode). -** Reads the options and handles them all. -*/ +bool find_lua_main(lua_State *L) { + lua_getglobal(L, "package"); + if (lua_getfield(L, -1, "searchers") != LUA_TTABLE) + luaL_error(L, "'package.searchers' must be a table"); + lua_remove(L, -2); + for (int i = 1; ; i++) { + if (lua_rawgeti(L, -1, i) == LUA_TNIL) { /* no more searchers? */ + lua_pop(L, 2); // pop nil & searchers table + return false; + } + lua_pushstring(L, "main"); + lua_call(L, 1, 2); /* call it */ + if (lua_isfunction(L, -2)) /* did it find a loader? */ + return true; /* module loader found */ + else + lua_pop(L, 2); /* remove both returns */ + } +} + int pmain (lua_State *L) { - int status; luaL_checkversion(L); /* check that interpreter has correct version */ print_version(); @@ -362,18 +376,20 @@ int pmain (lua_State *L) { if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */ return 0; /* error running LUA_INIT */ - lua_getglobal(L, "require"); - lua_pushstring(L, "main"); - status = docall(L, 1, 1); /* call 'require("main")' */ - if (status != LUA_OK) { + if (find_lua_main(L)) { + // call loader + lua_pushstring(L, "main"); + lua_insert(L, -2); + lua_call(L, 2, 1); + } else { // no main, launch a REPL if (lua_stdin_is_tty()) { /* running in interactive mode? */ doREPL(L); /* do read-eval-print loop */ } else { - dofile(L, NULL); /* executes stdin as a file */ + dofile(L, NULL); /* executes stdin as a file */ } + lua_pushboolean(L, 1); /* signal no errors */ } - lua_pushboolean(L, 1); /* signal no errors */ return 1; } -- cgit v1.2.3