Merge nixio 0.2
[project/luci.git] / libs / nixio / src / poll.c
index 8fd585f..4af3289 100644 (file)
  */
 
 #include "nixio.h"
-#include <poll.h>
 #include <time.h>
 #include <errno.h>
+#include <string.h>
 #include <stdlib.h>
-#include "nixio.h"
 
 
 /**
@@ -49,18 +48,6 @@ static int nixio_nanosleep(lua_State *L) {
 }
 
 /**
- * Checks whether a flag is set in the table and translates it into a bitmap
- */
-static void nixio_poll_flags__w(lua_State *L, int *map, int f, const char *t) {
-       lua_pushstring(L, t);
-       lua_rawget(L, -2);
-       if (lua_toboolean(L, -1)) {
-               *map |= f;
-       }
-       lua_pop(L, 1);
-}
-
-/**
  * Checks whether a flag is set in the bitmap and sets the matching table value
  */
 static void nixio_poll_flags__r(lua_State *L, int *map, int f, const char *t) {
@@ -78,25 +65,46 @@ static void nixio_poll_flags__r(lua_State *L, int *map, int f, const char *t) {
  */
 static int nixio_poll_flags(lua_State *L) {
        int flags;
-       if (lua_istable(L, 1)) {
-               lua_settop(L, 1);
-               flags = 0;
-               nixio_poll_flags__w(L, &flags, POLLIN, "in");
-               nixio_poll_flags__w(L, &flags, POLLPRI, "pri");
-               nixio_poll_flags__w(L, &flags, POLLOUT, "out");
-               nixio_poll_flags__w(L, &flags, POLLERR, "err");
-               nixio_poll_flags__w(L, &flags, POLLHUP, "hup");
-               nixio_poll_flags__w(L, &flags, POLLNVAL, "nval");
-               lua_pushinteger(L, flags);
-       } else {
+       if (lua_isnumber(L, 1)) {
                flags = luaL_checkinteger(L, 1);
                lua_newtable(L);
                nixio_poll_flags__r(L, &flags, POLLIN, "in");
-               nixio_poll_flags__r(L, &flags, POLLPRI, "pri");
                nixio_poll_flags__r(L, &flags, POLLOUT, "out");
                nixio_poll_flags__r(L, &flags, POLLERR, "err");
+#ifndef __WINNT__
+               nixio_poll_flags__r(L, &flags, POLLPRI, "pri");
                nixio_poll_flags__r(L, &flags, POLLHUP, "hup");
                nixio_poll_flags__r(L, &flags, POLLNVAL, "nval");
+#endif
+        } else {
+               flags = 0;
+               const int j = lua_gettop(L);
+               for (int i=1; i<=j; i++) {
+                       const char *flag = luaL_checkstring(L, i);
+                       if (!strcmp(flag, "in")) {
+                               flags |= POLLIN;
+                       } else if (!strcmp(flag, "out")) {
+                               flags |= POLLOUT;
+                       } else if (!strcmp(flag, "err")) {
+                               flags |= POLLERR;
+                       } else if (!strcmp(flag, "pri")) {
+#ifndef __WINNT__
+                               flags |= POLLPRI;
+#endif
+                       } else if (!strcmp(flag, "hup")) {
+#ifndef __WINNT__
+                               flags |= POLLHUP;
+#endif
+                       } else if (!strcmp(flag, "nval")) {
+#ifndef __WINNT__
+                               flags |= POLLNVAL;
+#endif
+                       } else {
+                               return luaL_argerror(L, i,
+                                "supported values: in, pri, out, err, hup, nval");
+                       }
+               }
+               lua_pushinteger(L, flags);
        }
        return 1;
 }
@@ -112,11 +120,19 @@ static int nixio_poll(lua_State *L) {
 
        /* we are being abused as sleep() replacement... */
        if (lua_isnoneornil(L, 1) || len < 1) {
-               return nixio__pstatus(L, !poll(NULL, 0, timeout));
+               if (!poll(NULL, 0, timeout)) {
+                       lua_pushinteger(L, 0);
+                       return 1;
+               } else {
+                       return nixio__perror(L);
+               }
        }
 
        luaL_checktype(L, 1, LUA_TTABLE);
        struct pollfd *fds = calloc(len, sizeof(struct pollfd));
+       if (!fds) {
+               return luaL_error(L, NIXIO_OOM);
+       }
 
        for (i = 0; i < len; i++) {
                lua_rawgeti(L, 1, i+1);
@@ -143,7 +159,11 @@ static int nixio_poll(lua_State *L) {
 
        status = poll(fds, (nfds_t)len, timeout);
 
-       if (status < 1) {
+       if (status == 0) {
+               free(fds);
+               lua_pushboolean(L, 0);
+               return 1;
+       } else if (status < 0) {
                free(fds);
                return nixio__perror(L);
        }