327f65098ed8e7bef982983d346db0ab6faccb3a
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
) {
30 lua_pushinteger(L
, errno
);
31 lua_pushstring(L
, strerror(errno
));
35 /* pushes true, if operation succeeded, otherwise call nixio__perror */
36 int nixio__pstatus(lua_State
*L
, int condition
) {
38 lua_pushboolean(L
, 1);
41 return nixio__perror(L
);
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");
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");
58 /* read fd from nixio_sock object */
59 int nixio__checksockfd(lua_State
*L
) {
60 return nixio__checksock(L
)->fd
;
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");
69 /* return any possible fd */
70 int nixio__tofd(lua_State
*L
, int ud
) {
71 void *udata
= lua_touserdata(L
, ud
);
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;
88 static const luaL_reg R
[] = {
93 LUALIB_API
int luaopen_nixio(lua_State
*L
) {
94 /* create metatable */
95 luaL_newmetatable(L
, NIXIO_META
);
97 /* metatable.__index = metatable */
99 lua_setfield(L
, -2, "__index");
101 /* register module */
102 luaL_register(L
, "nixio", R
);
104 /* register metatable as socket_meta */
105 lua_pushvalue(L
, -2);
106 lua_setfield(L
, -2, "socket_meta");
108 /* register methods */
110 nixio_open_socket(L
);
111 nixio_open_sockopt(L
);
113 nixio_open_address(L
);
116 nixio_open_splice(L
);
119 lua_pushnumber(L
, VERSION
);
120 lua_setfield(L
, -2, "version");
122 /* remove meta table */