More nixio fixes, initial httpclient
[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 /* object table */
92 static const luaL_reg R[] = {
93 {NULL, NULL}
94 };
95
96 /* entry point */
97 LUALIB_API int luaopen_nixio(lua_State *L) {
98 /* create metatable */
99 luaL_newmetatable(L, NIXIO_META);
100
101 /* metatable.__index = metatable */
102 lua_pushvalue(L, -1);
103 lua_setfield(L, -2, "__index");
104
105 /* register module */
106 luaL_register(L, "nixio", R);
107
108 /* register metatable as socket_meta */
109 lua_pushvalue(L, -2);
110 lua_setfield(L, -2, "socket_meta");
111
112 /* register methods */
113 nixio_open_file(L);
114 nixio_open_socket(L);
115 nixio_open_sockopt(L);
116 nixio_open_bind(L);
117 nixio_open_address(L);
118 nixio_open_poll(L);
119 nixio_open_io(L);
120 nixio_open_splice(L);
121
122 /* module version */
123 lua_pushnumber(L, VERSION);
124 lua_setfield(L, -2, "version");
125
126 /* some constants */
127 lua_createtable(L, 0, 1);
128
129 NIXIO_PUSH_CONSTANT(EACCES);
130 NIXIO_PUSH_CONSTANT(ENOSYS);
131 NIXIO_PUSH_CONSTANT(EINVAL);
132 NIXIO_PUSH_CONSTANT(EWOULDBLOCK);
133 NIXIO_PUSH_CONSTANT(EAGAIN);
134 NIXIO_PUSH_CONSTANT(ENOMEM);
135
136 lua_setfield(L, -2, "const");
137
138 /* remove meta table */
139 lua_remove(L, -2);
140
141 return 1;
142 }