327f65098ed8e7bef982983d346db0ab6faccb3a
[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 lua_pushnil(L);
30 lua_pushinteger(L, errno);
31 lua_pushstring(L, strerror(errno));
32 return 3;
33 }
34
35 /* pushes true, if operation succeeded, otherwise call nixio__perror */
36 int nixio__pstatus(lua_State *L, int condition) {
37 if (condition) {
38 lua_pushboolean(L, 1);
39 return 1;
40 } else {
41 return nixio__perror(L);
42 }
43 }
44
45 /* checks whether the first argument is a socket and returns it */
46 nixio_sock* nixio__checksock(lua_State *L) {
47 nixio_sock *sock = (nixio_sock*)luaL_checkudata(L, 1, NIXIO_META);
48 luaL_argcheck(L, sock->fd != -1, 1, "invalid socket object");
49 return sock;
50 }
51
52 FILE* nixio__checkfile(lua_State *L) {
53 FILE **fpp = (FILE**)luaL_checkudata(L, 1, NIXIO_FILE_META);
54 luaL_argcheck(L, *fpp, 1, "invalid file object");
55 return *fpp;
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 (lua_getmetatable(L, ud)) {
74 luaL_getmetatable(L, NIXIO_META);
75 luaL_getmetatable(L, NIXIO_FILE_META);
76 luaL_getmetatable(L, LUA_FILEHANDLE);
77 if (lua_rawequal(L, -3, -4)) {
78 fd = ((nixio_sock*)udata)->fd;
79 } else if (lua_rawequal(L, -2, -4) || lua_rawequal(L, -1, -4)) {
80 fd = (*((FILE **)udata)) ? fileno(*((FILE **)udata)) : -1;
81 }
82 lua_pop(L, 4);
83 }
84 return fd;
85 }
86
87 /* object table */
88 static const luaL_reg R[] = {
89 {NULL, NULL}
90 };
91
92 /* entry point */
93 LUALIB_API int luaopen_nixio(lua_State *L) {
94 /* create metatable */
95 luaL_newmetatable(L, NIXIO_META);
96
97 /* metatable.__index = metatable */
98 lua_pushvalue(L, -1);
99 lua_setfield(L, -2, "__index");
100
101 /* register module */
102 luaL_register(L, "nixio", R);
103
104 /* register metatable as socket_meta */
105 lua_pushvalue(L, -2);
106 lua_setfield(L, -2, "socket_meta");
107
108 /* register methods */
109 nixio_open_file(L);
110 nixio_open_socket(L);
111 nixio_open_sockopt(L);
112 nixio_open_bind(L);
113 nixio_open_address(L);
114 nixio_open_poll(L);
115 nixio_open_io(L);
116 nixio_open_splice(L);
117
118 /* module version */
119 lua_pushnumber(L, VERSION);
120 lua_setfield(L, -2, "version");
121
122 /* remove meta table */
123 lua_remove(L, -2);
124
125 return 1;
126 }