nixio: Export more constants, introduce {g,s}et{g,u}id, getpid, getppid
[project/luci.git] / libs / nixio / src / nixio.c
index 1152f2c..d433ba4 100644 (file)
  *  limitations under the License.
  */
 
-#include <lua.h>
-#include <lualib.h>
-#include <lauxlib.h>
-#include <unistd.h>
-#include <stdlib.h>
+#include "nixio.h"
 #include <stdio.h>
 #include <string.h>
-#include <sys/socket.h>
 #include <errno.h>
-#include "nixio.h"
+#include <signal.h>
 
 #define VERSION 0.1
 
 
 /* pushes nil, error number and errstring on the stack */
 int nixio__perror(lua_State *L) {
-    lua_pushnil(L);
+       if (errno == EAGAIN) {
+               lua_pushboolean(L, 0);
+       } else {
+               lua_pushnil(L);
+       }
     lua_pushinteger(L, errno);
     lua_pushstring(L, strerror(errno));
     return 3;
@@ -70,21 +69,30 @@ int nixio__checkfd(lua_State *L, int ud) {
 int nixio__tofd(lua_State *L, int ud) {
        void *udata = lua_touserdata(L, ud);
        int fd = -1;
-       if (udata && lua_getmetatable(L, ud)) {
+       if (lua_getmetatable(L, ud)) {
                luaL_getmetatable(L, NIXIO_META);
+               luaL_getmetatable(L, NIXIO_FILE_META);
                luaL_getmetatable(L, LUA_FILEHANDLE);
-               if (lua_rawequal(L, -2, -3)) {
+               if (lua_rawequal(L, -3, -4)) {
                        fd = ((nixio_sock*)udata)->fd;
-               } else if (lua_rawequal(L, -1, -3)) {
-                       fd = fileno(*((FILE **)udata));
+               } else if (lua_rawequal(L, -2, -4)) {
+                       fd = *((int*)udata);
+               } else if (lua_rawequal(L, -1, -4)) {
+                       fd = (*((FILE **)udata)) ? fileno(*((FILE **)udata)) : -1;
                }
-               lua_pop(L, 3);
+               lua_pop(L, 4);
        }
        return fd;
 }
 
+static int nixio_strerror(lua_State *L) {
+       lua_pushstring(L, strerror(luaL_checkinteger(L, 1)));
+       return 1;
+}
+
 /* object table */
 static const luaL_reg R[] = {
+       {"strerror",    nixio_strerror},
        {NULL,                  NULL}
 };
 
@@ -93,28 +101,56 @@ LUALIB_API int luaopen_nixio(lua_State *L) {
        /* create metatable */
        luaL_newmetatable(L, NIXIO_META);
 
-       /* method table */
-       lua_newtable(L);
+       /* metatable.__index = metatable */
        lua_pushvalue(L, -1);
-
-       /* metatable.__index = M */
-       lua_setfield(L, -3, "__index");
-       lua_remove(L, -2);
+       lua_setfield(L, -2, "__index");
 
        /* register module */
        luaL_register(L, "nixio", R);
 
+       /* register metatable as socket_meta */
+       lua_pushvalue(L, -2);
+       lua_setfield(L, -2, "meta_socket");
+
        /* register methods */
+       nixio_open_file(L);
        nixio_open_socket(L);
        nixio_open_sockopt(L);
        nixio_open_bind(L);
        nixio_open_address(L);
-       nixio_open_select(L);
+       nixio_open_poll(L);
+       nixio_open_io(L);
+       nixio_open_splice(L);
+       nixio_open_process(L);
+       nixio_open_tls_context(L);
+       nixio_open_tls_socket(L);
 
        /* module version */
        lua_pushnumber(L, VERSION);
        lua_setfield(L, -2, "version");
 
+       /* some constants */
+       lua_createtable(L, 0, 16);
+
+       NIXIO_PUSH_CONSTANT(EACCES);
+       NIXIO_PUSH_CONSTANT(EINTR);
+       NIXIO_PUSH_CONSTANT(ENOSYS);
+       NIXIO_PUSH_CONSTANT(EINVAL);
+       NIXIO_PUSH_CONSTANT(EWOULDBLOCK);
+       NIXIO_PUSH_CONSTANT(EAGAIN);
+       NIXIO_PUSH_CONSTANT(ENOMEM);
+       NIXIO_PUSH_CONSTANT(ENOENT);
+       NIXIO_PUSH_CONSTANT(SIGALRM);
+       NIXIO_PUSH_CONSTANT(SIGINT);
+       NIXIO_PUSH_CONSTANT(SIGTERM);
+       NIXIO_PUSH_CONSTANT(SIGKILL);
+       NIXIO_PUSH_CONSTANT(SIGHUP);
+       NIXIO_PUSH_CONSTANT(SIGSTOP);
+       NIXIO_PUSH_CONSTANT(SIGCONT);
+       NIXIO_PUSH_CONSTANT(SIGSEGV);
+
+       lua_setfield(L, -2, "const");
+
        /* remove meta table */
        lua_remove(L, -2);