new beta-0.90.0
[luatex.git] / source / texk / web2c / luatexdir / luasocket / src / luasocket.c
blobb43114e50f72446303d5b093087402ce9bca9486
1 /*=========================================================================*\
2 * LuaSocket toolkit
3 * Networking support for the Lua language
4 * Diego Nehab
5 * 26/11/1999
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 \*=========================================================================*/
18 #include "lua.h"
19 #include "lauxlib.h"
21 #if !defined(LUA_VERSION_NUM) || (LUA_VERSION_NUM < 501)
22 #include "compat-5.1.h"
23 #endif
25 /*=========================================================================*\
26 * LuaSocket includes
27 \*=========================================================================*/
28 #include "luasocket.h"
29 #include "auxiliar.h"
30 #include "except.h"
31 #include "timeout.h"
32 #include "buffer.h"
33 #include "inet.h"
34 #include "tcp.h"
35 #include "udp.h"
36 #include "select.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},
53 {"inet", inet_open},
54 {"tcp", tcp_open},
55 {"udp", udp_open},
56 {"select", select_open},
57 {NULL, NULL}
60 static luaL_Reg func[] = {
61 {"skip", global_skip},
62 {"__unload", global_unload},
63 {NULL, NULL}
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 /*-------------------------------------------------------------------------*\
76 * Unloads the library
77 \*-------------------------------------------------------------------------*/
78 static int global_unload(lua_State *L) {
79 (void) L;
80 socket_close();
81 return 0;
84 /*-------------------------------------------------------------------------*\
85 * Setup basic stuff.
86 \*-------------------------------------------------------------------------*/
87 static int base_open(lua_State *L) {
88 if (socket_open()) {
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);
94 lua_rawset(L, -3);
95 #endif
96 /* make version string available to scripts */
97 lua_pushstring(L, "_VERSION");
98 lua_pushstring(L, LUASOCKET_VERSION);
99 lua_rawset(L, -3);
100 return 1;
101 } else {
102 lua_pushstring(L, "unable to initialize library");
103 lua_error(L);
104 return 0;
108 /*-------------------------------------------------------------------------*\
109 * Initializes all library modules.
110 \*-------------------------------------------------------------------------*/
111 LUASOCKET_API int luaopen_socket_core(lua_State *L) {
112 int i;
113 base_open(L);
114 for (i = 0; mod[i].name; i++) mod[i].func(L);
115 return 1;