2 * nixio - Linux I/O library for lua
4 * Copyright (C) 2009 Steven Barth <steven@midlink.org>
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
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);
34 lua_pushinteger(L
, errno
);
35 lua_pushstring(L
, strerror(errno
));
39 /* pushes true, if operation succeeded, otherwise call nixio__perror */
40 int nixio__pstatus(lua_State
*L
, int condition
) {
42 lua_pushboolean(L
, 1);
45 return nixio__perror(L
);
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");
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");
62 /* read fd from nixio_sock object */
63 int nixio__checksockfd(lua_State
*L
) {
64 return nixio__checksock(L
)->fd
;
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");
73 /* return any possible fd */
74 int nixio__tofd(lua_State
*L
, int ud
) {
75 void *udata
= lua_touserdata(L
, ud
);
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;
92 static const luaL_reg R
[] = {
97 LUALIB_API
int luaopen_nixio(lua_State
*L
) {
98 /* create metatable */
99 luaL_newmetatable(L
, NIXIO_META
);
101 /* metatable.__index = metatable */
102 lua_pushvalue(L
, -1);
103 lua_setfield(L
, -2, "__index");
105 /* register module */
106 luaL_register(L
, "nixio", R
);
108 /* register metatable as socket_meta */
109 lua_pushvalue(L
, -2);
110 lua_setfield(L
, -2, "socket_meta");
112 /* register methods */
114 nixio_open_socket(L
);
115 nixio_open_sockopt(L
);
117 nixio_open_address(L
);
120 nixio_open_splice(L
);
121 nixio_open_tls_context(L
);
122 nixio_open_tls_socket(L
);
125 lua_pushnumber(L
, VERSION
);
126 lua_setfield(L
, -2, "version");
129 lua_createtable(L
, 0, 1);
131 NIXIO_PUSH_CONSTANT(EACCES
);
132 NIXIO_PUSH_CONSTANT(ENOSYS
);
133 NIXIO_PUSH_CONSTANT(EINVAL
);
134 NIXIO_PUSH_CONSTANT(EWOULDBLOCK
);
135 NIXIO_PUSH_CONSTANT(EAGAIN
);
136 NIXIO_PUSH_CONSTANT(ENOMEM
);
138 lua_setfield(L
, -2, "const");
140 /* remove meta table */