1152f2cc8862ebacd7ae4a07d3e04067fd010a63
[project/luci.git] / libs / nixio / src / nixio.c
1 /*
2  * nixio - Linux I/O library for lua
3  *
4  *   Copyright (C) 2009 Steven Barth <steven@midlink.org>
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */
18
19 #include <lua.h>
20 #include <lualib.h>
21 #include <lauxlib.h>
22 #include <unistd.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <sys/socket.h>
27 #include <errno.h>
28 #include "nixio.h"
29
30 #define VERSION 0.1
31
32
33 /* pushes nil, error number and errstring on the stack */
34 int nixio__perror(lua_State *L) {
35     lua_pushnil(L);
36     lua_pushinteger(L, errno);
37     lua_pushstring(L, strerror(errno));
38     return 3;
39 }
40
41 /* pushes true, if operation succeeded, otherwise call nixio__perror */
42 int nixio__pstatus(lua_State *L, int condition) {
43         if (condition) {
44                 lua_pushboolean(L, 1);
45                 return 1;
46         } else {
47                 return nixio__perror(L);
48         }
49 }
50
51 /* checks whether the first argument is a socket and returns it */
52 nixio_sock* nixio__checksock(lua_State *L) {
53     nixio_sock *sock = (nixio_sock*)luaL_checkudata(L, 1, NIXIO_META);
54     luaL_argcheck(L, sock->fd != -1, 1, "invalid socket object");
55     return sock;
56 }
57
58 /* read fd from nixio_sock object */
59 int nixio__checksockfd(lua_State *L) {
60         return nixio__checksock(L)->fd;
61 }
62
63 /* return any possible fd, otherwise error out */
64 int nixio__checkfd(lua_State *L, int ud) {
65         int fd = nixio__tofd(L, ud);
66         return (fd != -1) ? fd : luaL_argerror(L, ud, "invalid file descriptor");
67 }
68
69 /* return any possible fd */
70 int nixio__tofd(lua_State *L, int ud) {
71         void *udata = lua_touserdata(L, ud);
72         int fd = -1;
73         if (udata && lua_getmetatable(L, ud)) {
74                 luaL_getmetatable(L, NIXIO_META);
75                 luaL_getmetatable(L, LUA_FILEHANDLE);
76                 if (lua_rawequal(L, -2, -3)) {
77                         fd = ((nixio_sock*)udata)->fd;
78                 } else if (lua_rawequal(L, -1, -3)) {
79                         fd = fileno(*((FILE **)udata));
80                 }
81                 lua_pop(L, 3);
82         }
83         return fd;
84 }
85
86 /* object table */
87 static const luaL_reg R[] = {
88         {NULL,                  NULL}
89 };
90
91 /* entry point */
92 LUALIB_API int luaopen_nixio(lua_State *L) {
93         /* create metatable */
94         luaL_newmetatable(L, NIXIO_META);
95
96         /* method table */
97         lua_newtable(L);
98         lua_pushvalue(L, -1);
99
100         /* metatable.__index = M */
101         lua_setfield(L, -3, "__index");
102         lua_remove(L, -2);
103
104         /* register module */
105         luaL_register(L, "nixio", R);
106
107         /* register methods */
108         nixio_open_socket(L);
109         nixio_open_sockopt(L);
110         nixio_open_bind(L);
111         nixio_open_address(L);
112         nixio_open_select(L);
113
114         /* module version */
115         lua_pushnumber(L, VERSION);
116         lua_setfield(L, -2, "version");
117
118         /* remove meta table */
119         lua_remove(L, -2);
120
121         return 1;
122 }