aboutsummaryrefslogtreecommitdiff
path: root/vendor/luasocket/src/unix.c
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/luasocket/src/unix.c')
-rw-r--r--vendor/luasocket/src/unix.c69
1 files changed, 69 insertions, 0 deletions
diff --git a/vendor/luasocket/src/unix.c b/vendor/luasocket/src/unix.c
new file mode 100644
index 00000000..268d8b21
--- /dev/null
+++ b/vendor/luasocket/src/unix.c
@@ -0,0 +1,69 @@
1/*=========================================================================*\
2* Unix domain socket
3* LuaSocket toolkit
4\*=========================================================================*/
5#include "luasocket.h"
6
7#include "unixstream.h"
8#include "unixdgram.h"
9
10/*-------------------------------------------------------------------------*\
11* Modules and functions
12\*-------------------------------------------------------------------------*/
13static const luaL_Reg mod[] = {
14 {"stream", unixstream_open},
15 {"dgram", unixdgram_open},
16 {NULL, NULL}
17};
18
19static void add_alias(lua_State *L, int index, const char *name, const char *target)
20{
21 lua_getfield(L, index, target);
22 lua_setfield(L, index, name);
23}
24
25static int compat_socket_unix_call(lua_State *L)
26{
27 /* Look up socket.unix.stream in the socket.unix table (which is the first
28 * argument). */
29 lua_getfield(L, 1, "stream");
30
31 /* Replace the stack entry for the socket.unix table with the
32 * socket.unix.stream function. */
33 lua_replace(L, 1);
34
35 /* Call socket.unix.stream, passing along any arguments. */
36 int n = lua_gettop(L);
37 lua_call(L, n-1, LUA_MULTRET);
38
39 /* Pass along the return values from socket.unix.stream. */
40 n = lua_gettop(L);
41 return n;
42}
43
44/*-------------------------------------------------------------------------*\
45* Initializes module
46\*-------------------------------------------------------------------------*/
47LUASOCKET_API int luaopen_socket_unix(lua_State *L)
48{
49 int i;
50 lua_newtable(L);
51 int socket_unix_table = lua_gettop(L);
52
53 for (i = 0; mod[i].name; i++)
54 mod[i].func(L);
55
56 /* Add backwards compatibility aliases "tcp" and "udp" for the "stream" and
57 * "dgram" functions. */
58 add_alias(L, socket_unix_table, "tcp", "stream");
59 add_alias(L, socket_unix_table, "udp", "dgram");
60
61 /* Add a backwards compatibility function and a metatable setup to call it
62 * for the old socket.unix() interface. */
63 lua_pushcfunction(L, compat_socket_unix_call);
64 lua_setfield(L, socket_unix_table, "__call");
65 lua_pushvalue(L, socket_unix_table);
66 lua_setmetatable(L, socket_unix_table);
67
68 return 1;
69}