More splicing stuff
[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 "nixio.h"
20 #include <stdio.h>
21 #include <string.h>
22 #include <errno.h>
23
24 #define VERSION 0.1
25
26
27 /* pushes nil, error number and errstring on the stack */
28 int nixio__perror(lua_State *L) {
29         if (errno == EAGAIN) {
30                 lua_pushboolean(L, 0);
31         } else {
32                 lua_pushnil(L);
33         }
34     lua_pushinteger(L, errno);
35     lua_pushstring(L, strerror(errno));
36     return 3;
37 }
38
39 /* pushes true, if operation succeeded, otherwise call nixio__perror */
40 int nixio__pstatus(lua_State *L, int condition) {
41         if (condition) {
42                 lua_pushboolean(L, 1);
43                 return 1;
44         } else {
45                 return nixio__perror(L);
46         }
47 }
48
49 /* checks whether the first argument is a socket and returns it */
50 nixio_sock* nixio__checksock(lua_State *L) {
51     nixio_sock *sock = (nixio_sock*)luaL_checkudata(L, 1, NIXIO_META);
52     luaL_argcheck(L, sock->fd != -1, 1, "invalid socket object");
53     return sock;
54 }
55
56 FILE* nixio__checkfile(lua_State *L) {
57         FILE **fpp = (FILE**)luaL_checkudata(L, 1, NIXIO_FILE_META);
58         luaL_argcheck(L, *fpp, 1, "invalid file object");
59         return *fpp;
60 }
61
62 /* read fd from nixio_sock object */
63 int nixio__checksockfd(lua_State *L) {
64         return nixio__checksock(L)->fd;
65 }
66
67 /* return any possible fd, otherwise error out */
68 int nixio__checkfd(lua_State *L, int ud) {
69         int fd = nixio__tofd(L, ud);
70         return (fd != -1) ? fd : luaL_argerror(L, ud, "invalid file descriptor");
71 }
72
73 /* return any possible fd */
74 int nixio__tofd(lua_State *L, int ud) {
75         void *udata = lua_touserdata(L, ud);
76         int fd = -1;
77         if (lua_getmetatable(L, ud)) {
78                 luaL_getmetatable(L, NIXIO_META);
79                 luaL_getmetatable(L, NIXIO_FILE_META);
80                 luaL_getmetatable(L, LUA_FILEHANDLE);
81                 if (lua_rawequal(L, -3, -4)) {
82                         fd = ((nixio_sock*)udata)->fd;
83                 } else if (lua_rawequal(L, -2, -4) || lua_rawequal(L, -1, -4)) {
84                         fd = (*((FILE **)udata)) ? fileno(*((FILE **)udata)) : -1;
85                 }
86                 lua_pop(L, 4);
87         }
88         return fd;
89 }
90
91 static int nixio_strerror(lua_State *L) {
92         lua_pushstring(L, strerror(luaL_checkinteger(L, 1)));
93         return 1;
94 }
95
96 /* object table */
97 static const luaL_reg R[] = {
98         {"strerror",    nixio_strerror},
99         {NULL,                  NULL}
100 };
101
102 /* entry point */
103 LUALIB_API int luaopen_nixio(lua_State *L) {
104         /* create metatable */
105         luaL_newmetatable(L, NIXIO_META);
106
107         /* metatable.__index = metatable */
108         lua_pushvalue(L, -1);
109         lua_setfield(L, -2, "__index");
110
111         /* register module */
112         luaL_register(L, "nixio", R);
113
114         /* register metatable as socket_meta */
115         lua_pushvalue(L, -2);
116         lua_setfield(L, -2, "meta_socket");
117
118         /* register methods */
119         nixio_open_file(L);
120         nixio_open_socket(L);
121         nixio_open_sockopt(L);
122         nixio_open_bind(L);
123         nixio_open_address(L);
124         nixio_open_poll(L);
125         nixio_open_io(L);
126         nixio_open_splice(L);
127         nixio_open_tls_context(L);
128         nixio_open_tls_socket(L);
129
130         /* module version */
131         lua_pushnumber(L, VERSION);
132         lua_setfield(L, -2, "version");
133
134         /* some constants */
135         lua_createtable(L, 0, 7);
136
137         NIXIO_PUSH_CONSTANT(EACCES);
138         NIXIO_PUSH_CONSTANT(ENOSYS);
139         NIXIO_PUSH_CONSTANT(EINVAL);
140         NIXIO_PUSH_CONSTANT(EWOULDBLOCK);
141         NIXIO_PUSH_CONSTANT(EAGAIN);
142         NIXIO_PUSH_CONSTANT(ENOMEM);
143         NIXIO_PUSH_CONSTANT(ENOENT);
144
145         lua_setfield(L, -2, "const");
146
147         /* remove meta table */
148         lua_remove(L, -2);
149
150         return 1;
151 }