2004-06-22 18:12:53 +02:00
|
|
|
/*=========================================================================*\
|
2015-08-21 20:39:34 +02:00
|
|
|
* Unix domain socket
|
2004-06-22 18:12:53 +02:00
|
|
|
* LuaSocket toolkit
|
|
|
|
\*=========================================================================*/
|
2019-02-25 23:56:28 +01:00
|
|
|
#include "luasocket.h"
|
|
|
|
|
2016-12-25 16:15:12 +01:00
|
|
|
#include "unixstream.h"
|
|
|
|
#include "unixdgram.h"
|
2004-06-22 18:12:53 +02:00
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
2016-06-24 15:23:00 +02:00
|
|
|
* Modules and functions
|
2004-06-22 18:12:53 +02:00
|
|
|
\*-------------------------------------------------------------------------*/
|
2016-06-24 15:23:00 +02:00
|
|
|
static const luaL_Reg mod[] = {
|
2016-12-25 16:15:12 +01:00
|
|
|
{"stream", unixstream_open},
|
|
|
|
{"dgram", unixdgram_open},
|
2016-06-24 15:23:00 +02:00
|
|
|
{NULL, NULL}
|
|
|
|
};
|
2004-06-22 18:12:53 +02:00
|
|
|
|
2017-01-26 01:04:35 +01:00
|
|
|
static void add_alias(lua_State *L, int index, const char *name, const char *target)
|
|
|
|
{
|
|
|
|
lua_getfield(L, index, target);
|
|
|
|
lua_setfield(L, index, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int compat_socket_unix_call(lua_State *L)
|
|
|
|
{
|
|
|
|
/* Look up socket.unix.stream in the socket.unix table (which is the first
|
|
|
|
* argument). */
|
|
|
|
lua_getfield(L, 1, "stream");
|
|
|
|
|
|
|
|
/* Replace the stack entry for the socket.unix table with the
|
|
|
|
* socket.unix.stream function. */
|
|
|
|
lua_replace(L, 1);
|
|
|
|
|
|
|
|
/* Call socket.unix.stream, passing along any arguments. */
|
|
|
|
int n = lua_gettop(L);
|
|
|
|
lua_call(L, n-1, LUA_MULTRET);
|
|
|
|
|
|
|
|
/* Pass along the return values from socket.unix.stream. */
|
|
|
|
n = lua_gettop(L);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2004-06-22 18:12:53 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
2016-06-24 15:23:00 +02:00
|
|
|
* Initializes module
|
2004-06-22 18:12:53 +02:00
|
|
|
\*-------------------------------------------------------------------------*/
|
2019-02-25 23:56:28 +01:00
|
|
|
LUASOCKET_API int luaopen_socket_unix(lua_State *L)
|
2004-06-22 18:12:53 +02:00
|
|
|
{
|
2017-01-26 01:04:35 +01:00
|
|
|
int i;
|
|
|
|
lua_newtable(L);
|
|
|
|
int socket_unix_table = lua_gettop(L);
|
|
|
|
|
|
|
|
for (i = 0; mod[i].name; i++)
|
|
|
|
mod[i].func(L);
|
2004-06-22 18:12:53 +02:00
|
|
|
|
2017-01-26 01:04:35 +01:00
|
|
|
/* Add backwards compatibility aliases "tcp" and "udp" for the "stream" and
|
|
|
|
* "dgram" functions. */
|
|
|
|
add_alias(L, socket_unix_table, "tcp", "stream");
|
|
|
|
add_alias(L, socket_unix_table, "udp", "dgram");
|
|
|
|
|
|
|
|
/* Add a backwards compatibility function and a metatable setup to call it
|
|
|
|
* for the old socket.unix() interface. */
|
|
|
|
lua_pushcfunction(L, compat_socket_unix_call);
|
|
|
|
lua_setfield(L, socket_unix_table, "__call");
|
|
|
|
lua_pushvalue(L, socket_unix_table);
|
|
|
|
lua_setmetatable(L, socket_unix_table);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|