luasocket/src/luasocket.c

117 lines
3.8 KiB
C
Raw Normal View History

2000-06-02 19:55:14 +02:00
/*=========================================================================*\
* LuaSocket toolkit
* Networking support for the Lua language
2000-06-02 19:55:14 +02:00
* Diego Nehab
* 26/11/1999
*
* This library is part of an effort to progressively increase the network
* connectivity of the Lua language. The Lua interface to networking
* functions follows the Sockets API closely, trying to simplify all tasks
* involved in setting up both client and server connections. The provided
* IO routines, however, follow the Lua style, being very similar to the
* standard Lua read and write functions.
*
* RCS ID: $Id$
2000-06-02 19:55:14 +02:00
\*=========================================================================*/
/*=========================================================================*\
* Standard include files
2000-06-02 19:55:14 +02:00
\*=========================================================================*/
#include <lua.h>
#include <lauxlib.h>
#include "compat-5.1.h"
2005-01-02 23:51:33 +01:00
#include "luasocket.h"
/*=========================================================================*\
* LuaSocket includes
\*=========================================================================*/
2003-05-25 03:54:13 +02:00
#include "auxiliar.h"
#include "except.h"
#include "timeout.h"
#include "buffer.h"
2003-05-25 03:54:13 +02:00
#include "inet.h"
#include "tcp.h"
#include "udp.h"
#include "select.h"
/*-------------------------------------------------------------------------*\
* Internal function prototypes
\*-------------------------------------------------------------------------*/
static int global_skip(lua_State *L);
static int global_unload(lua_State *L);
static int base_open(lua_State *L);
/*-------------------------------------------------------------------------*\
* Modules and functions
\*-------------------------------------------------------------------------*/
static const luaL_reg mod[] = {
2004-05-30 23:36:22 +02:00
{"auxiliar", aux_open},
{"except", except_open},
2004-05-30 23:36:22 +02:00
{"timeout", tm_open},
{"buffer", buf_open},
{"inet", inet_open},
{"tcp", tcp_open},
{"udp", udp_open},
{"select", select_open},
{NULL, NULL}
};
static luaL_reg func[] = {
{"skip", global_skip},
{"__unload", global_unload},
{NULL, NULL}
};
/*-------------------------------------------------------------------------*\
* Skip a few arguments
\*-------------------------------------------------------------------------*/
static int global_skip(lua_State *L) {
int amount = luaL_checkint(L, 1);
int ret = lua_gettop(L) - amount - 1;
return ret >= 0 ? ret : 0;
}
/*-------------------------------------------------------------------------*\
* Unloads the library
\*-------------------------------------------------------------------------*/
static int global_unload(lua_State *L) {
2004-06-17 23:46:22 +02:00
(void) L;
sock_close();
return 0;
}
/*-------------------------------------------------------------------------*\
* Setup basic stuff.
\*-------------------------------------------------------------------------*/
static int base_open(lua_State *L) {
if (sock_open()) {
/* export functions (and leave namespace table on top of stack) */
2005-02-08 11:01:01 +01:00
luaL_openlib(L, "socket", func, 0);
#ifdef LUASOCKET_DEBUG
lua_pushstring(L, "DEBUG");
lua_pushboolean(L, 1);
lua_rawset(L, -3);
#endif
/* make version string available to scripts */
lua_pushstring(L, "VERSION");
lua_pushstring(L, LUASOCKET_VERSION);
lua_rawset(L, -3);
return 1;
} else {
lua_pushstring(L, "unable to initialize library");
lua_error(L);
return 0;
}
}
/*-------------------------------------------------------------------------*\
* Initializes all library modules.
\*-------------------------------------------------------------------------*/
2005-02-08 11:01:01 +01:00
LUASOCKET_API int luaopen_csocket(lua_State *L) {
2004-05-28 08:16:43 +02:00
int i;
base_open(L);
2004-06-04 17:15:45 +02:00
for (i = 0; mod[i].name; i++) mod[i].func(L);
return 1;
}