diff options
author | Alex Auvolat <alex@adnab.me> | 2017-04-21 14:57:53 +0200 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2017-04-21 14:57:53 +0200 |
commit | ec08d0410730a16836eb40f5e46082b3bbaf45f6 (patch) | |
tree | 1191271926f1fe42049b974f4bd9bdf56a2945bb /src/sysbin/lx/lxsyslib.c | |
parent | 59ecab36f634a00cc6e2c4194bf2d5ebc4ec70eb (diff) | |
download | kogata-ec08d0410730a16836eb40f5e46082b3bbaf45f6.tar.gz kogata-ec08d0410730a16836eb40f5e46082b3bbaf45f6.zip |
include lua_cmsgpack and implement select binding
Diffstat (limited to 'src/sysbin/lx/lxsyslib.c')
-rw-r--r-- | src/sysbin/lx/lxsyslib.c | 41 |
1 files changed, 39 insertions, 2 deletions
diff --git a/src/sysbin/lx/lxsyslib.c b/src/sysbin/lx/lxsyslib.c index fbafde9..52c6978 100644 --- a/src/sysbin/lx/lxsyslib.c +++ b/src/sysbin/lx/lxsyslib.c @@ -191,8 +191,45 @@ static int sys_fctl(lua_State *L) { } static int sys_select(lua_State *L) { - // TODO - return 0; + 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; + } + + sel_fd_t *fds = (sel_fd_t*)malloc(nfds*sizeof(sel_fd_t)); + if (fds == NULL) + luaL_error(L, "Out of memory, could not allocate %d sel_fd_t", nfds); + + for (int i = 0; i < nfds; i++) { + lua_rawgeti(L, 1, i+1); + luaL_checktype(L, -1, LUA_TTABLE); + + lua_rawgeti(L, -1, 1); + fds[i].fd = luaL_checkinteger(L, -1); + lua_pop(L, 1); + + lua_rawgeti(L, -1, 2); + fds[i].req_flags = luaL_checkinteger(L, -1); + lua_pop(L, 2); + } + + bool result = sc_select(fds, nfds, timeout); + + for (int i = 0; i < nfds; i++) { + lua_rawgeti(L, 1, i+1); + lua_pushinteger(L, fds[i].got_flags); + lua_rawseti(L, -2, 3); + lua_pop(L, 1); + } + + free(fds); + + lua_pushboolean(L, result); + return 1; } static int sys_make_channel(lua_State *L) { |