1 /*=========================================================================*\
3 * Networking support for the Lua language
7 * This library is part of an effort to progressively increase the network
8 * connectivity of the Lua language. The Lua interface to networking
9 * functions follows the Sockets API closely, trying to simplify all tasks
10 * involved in setting up both client and server connections. The provided
11 * IO routines, however, follow the Lua style, being very similar to the
12 * standard Lua read and write functions.
13 \*=========================================================================*/
15 /*=========================================================================*\
16 * Standard include files
17 \*=========================================================================*/
21 #if !defined(LUA_VERSION_NUM) || (LUA_VERSION_NUM < 501)
22 #include "compat-5.1.h"
25 /*=========================================================================*\
27 \*=========================================================================*/
28 #include "luasocket.h"
38 /*-------------------------------------------------------------------------*\
39 * Internal function prototypes
40 \*-------------------------------------------------------------------------*/
41 static int global_skip(lua_State
*L
);
42 static int global_unload(lua_State
*L
);
43 static int base_open(lua_State
*L
);
45 /*-------------------------------------------------------------------------*\
46 * Modules and functions
47 \*-------------------------------------------------------------------------*/
48 static const luaL_Reg mod
[] = {
49 {"auxiliar", auxiliar_open
},
50 {"except", except_open
},
51 {"timeout", timeout_open
},
52 {"buffer", buffer_open
},
56 {"select", select_open
},
60 static luaL_Reg func
[] = {
61 {"skip", global_skip
},
62 {"__unload", global_unload
},
66 /*-------------------------------------------------------------------------*\
67 * Skip a few arguments
68 \*-------------------------------------------------------------------------*/
69 static int global_skip(lua_State
*L
) {
70 int amount
= luaL_checkint(L
, 1);
71 int ret
= lua_gettop(L
) - amount
- 1;
72 return ret
>= 0 ? ret
: 0;
75 /*-------------------------------------------------------------------------*\
77 \*-------------------------------------------------------------------------*/
78 static int global_unload(lua_State
*L
) {
84 /*-------------------------------------------------------------------------*\
86 \*-------------------------------------------------------------------------*/
87 static int base_open(lua_State
*L
) {
89 /* export functions (and leave namespace table on top of stack) */
90 luaL_openlib(L
, "socket", func
, 0);
91 #ifdef LUASOCKET_DEBUG
92 lua_pushstring(L
, "_DEBUG");
93 lua_pushboolean(L
, 1);
96 /* make version string available to scripts */
97 lua_pushstring(L
, "_VERSION");
98 lua_pushstring(L
, LUASOCKET_VERSION
);
102 lua_pushstring(L
, "unable to initialize library");
108 /*-------------------------------------------------------------------------*\
109 * Initializes all library modules.
110 \*-------------------------------------------------------------------------*/
111 LUASOCKET_API
int luaopen_socket_core(lua_State
*L
) {
114 for (i
= 0; mod
[i
].name
; i
++) mod
[i
].func(L
);