nixio + io = nix2io
[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 <stdio.h>
23 #include <string.h>
24 #include <errno.h>
25 #include "nixio.h"
26
27 #define VERSION 0.1
28
29
30 /* pushes nil, error number and errstring on the stack */
31 int nixio__perror(lua_State *L) {
32 lua_pushnil(L);
33 lua_pushinteger(L, errno);
34 lua_pushstring(L, strerror(errno));
35 return 3;
36 }
37
38 /* pushes true, if operation succeeded, otherwise call nixio__perror */
39 int nixio__pstatus(lua_State *L, int condition) {
40 if (condition) {
41 lua_pushboolean(L, 1);
42 return 1;
43 } else {
44 return nixio__perror(L);
45 }
46 }
47
48 /* checks whether the first argument is a socket and returns it */
49 nixio_sock* nixio__checksock(lua_State *L) {
50 nixio_sock *sock = (nixio_sock*)luaL_checkudata(L, 1, NIXIO_META);
51 luaL_argcheck(L, sock->fd != -1, 1, "invalid socket object");
52 return sock;
53 }
54
55 /* read fd from nixio_sock object */
56 int nixio__checksockfd(lua_State *L) {
57 return nixio__checksock(L)->fd;
58 }
59
60 /* return any possible fd, otherwise error out */
61 int nixio__checkfd(lua_State *L, int ud) {
62 int fd = nixio__tofd(L, ud);
63 return (fd != -1) ? fd : luaL_argerror(L, ud, "invalid file descriptor");
64 }
65
66 /* return any possible fd */
67 int nixio__tofd(lua_State *L, int ud) {
68 void *udata = lua_touserdata(L, ud);
69 int fd = -1;
70 if (udata && lua_getmetatable(L, ud)) {
71 luaL_getmetatable(L, NIXIO_META);
72 luaL_getmetatable(L, LUA_FILEHANDLE);
73 if (lua_rawequal(L, -2, -3)) {
74 fd = ((nixio_sock*)udata)->fd;
75 } else if (lua_rawequal(L, -1, -3)) {
76 fd = fileno(*((FILE **)udata));
77 }
78 lua_pop(L, 3);
79 }
80 return fd;
81 }
82
83 /* object table */
84 static const luaL_reg R[] = {
85 {NULL, NULL}
86 };
87
88 /* entry point */
89 LUALIB_API int luaopen_nixio(lua_State *L) {
90 /* create metatable */
91 luaL_newmetatable(L, NIXIO_META);
92
93 /* method table */
94 lua_newtable(L);
95 lua_pushvalue(L, -1);
96
97 /* metatable.__index = M */
98 lua_setfield(L, -3, "__index");
99 lua_remove(L, -2);
100
101 /* register module */
102 luaL_register(L, "nixio", R);
103
104 /* register methods */
105 nixio_open_socket(L);
106 nixio_open_sockopt(L);
107 nixio_open_bind(L);
108 nixio_open_address(L);
109 nixio_open_poll(L);
110 nixio_open_io(L);
111
112 /* module version */
113 lua_pushnumber(L, VERSION);
114 lua_setfield(L, -2, "version");
115
116 /* remove meta table */
117 lua_remove(L, -2);
118
119 return 1;
120 }