1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
diff --git a/AUTHORS b/AUTHORS
index ffdadfa..bd5ee8a 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -3,3 +3,8 @@ lua-hiredis bindings authors:
Alexander Gladysh <agladysh@gmail.com>
+lua-hiredis bindings contributors:
+----------------------------------
+
+Stein Ivar Berghei <stein-ivar@berghei.no>
+Peter Melnichenko <petjamelnik@yandex.ru>
diff --git a/COPYRIGHT b/COPYRIGHT
index 0af707a..4f31953 100644
--- a/COPYRIGHT
+++ b/COPYRIGHT
@@ -9,7 +9,7 @@ Note that hiredis library itself (included for convenience) is licensed under BS
===============================================================================
-Copyright (C) 2011—2012 lua-hiredis bindings authors
+Copyright (C) 2011—2014 lua-hiredis bindings authors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
diff --git a/README.md b/README.md
index bfc065b..1121f09 100644
--- a/README.md
+++ b/README.md
@@ -14,7 +14,12 @@ is not 1.0 yet. But all the features necessary for regular usage are here.
API
---
-* `hiredis.connect(host : string, port : number) : conn / nil, err, error_code`
+* `hiredis.connect(host / socket : string, port : number / nil) : conn / nil, err, error_code`
+
+ * `hiredis.connect("localhost", 6379)` connects to Redis at `localhost:6379`
+ via TCP/IP socket.
+ * `hiredis.connect("/var/run/redis/redis.sock")` connects to Redis at
+ `/var/run/redis/redis.sock` via Unix domain socket.
* `hiredis.unwrap_reply(reply) : reply / name, hiredis.REPLY_STATUS / nil, err`
diff --git a/TODO b/TODO
index 49e1097..2e1b220 100644
--- a/TODO
+++ b/TODO
@@ -1,10 +1,7 @@
TODO:
-----
- -- hiredis.unwrap_reply should pass everything through without looking
- if it is called with more than one argument,
- so this would not lose an error:
- hiredis.unwrap_reply(conn.command())
+ -- Make nolibs rockspec default, remove lib/*
-- Document current API.
-- Run splint all over it.
-- Run under valgrind
diff --git a/make.sh b/make.sh
index 0c012f2..4657b4c 100755
--- a/make.sh
+++ b/make.sh
@@ -6,14 +6,14 @@ echo "----> Going pedantic all over the source"
# Ugh. hiredis.h is not c89-compatible
echo "--> c89..."
-gcc -O2 -fPIC -I/usr/include/lua5.1 -c src/lua-hiredis.c -o /dev/null -Isrc/ -Ilib/hiredis/ -Wall --pedantic --std=c89 #-Werror
+gcc -O2 -fPIC -I/usr/include/lua5.1 -c src/lua-hiredis.c -o /dev/null -Isrc/ -Ilib/ -Wall --pedantic --std=c89 #-Werror
echo "--> c99..."
-gcc -O2 -fPIC -I/usr/include/lua5.1 -c src/lua-hiredis.c -o /dev/null -Isrc/ -Ilib/hiredis/ -Wall --pedantic -Werror --std=c99
+gcc -O2 -fPIC -I/usr/include/lua5.1 -c src/lua-hiredis.c -o /dev/null -Isrc/ -Ilib/ -Wall --pedantic -Werror --std=c99
# Ugh. hiredis.h is not c++98-compatible
echo "--> c++98..."
-gcc -xc++ -O2 -fPIC -I/usr/include/lua5.1 -c src/lua-hiredis.c -o /dev/null -Isrc/c/ -Ilib/hiredis/ --pedantic -Wall --std=c++98 #-Werror
+gcc -xc++ -O2 -fPIC -I/usr/include/lua5.1 -c src/lua-hiredis.c -o /dev/null -Isrc/c/ -Ilib/ --pedantic -Wall --std=c++98 #-Werror
echo "----> Making rock"
sudo luarocks make rockspec/lua-hiredis-scm-1.rockspec
diff --git a/rockspec/lua-hiredis-scm-1.rockspec b/rockspec/lua-hiredis-scm-1.rockspec
index a9ec60a..65f87ae 100644
--- a/rockspec/lua-hiredis-scm-1.rockspec
+++ b/rockspec/lua-hiredis-scm-1.rockspec
@@ -31,6 +31,7 @@ build = {
"src/",
-- bundled hiredis code --
+ "lib/",
"lib/hiredis/"
}
}
diff --git a/src/lua-hiredis.c b/src/lua-hiredis.c
index f8b2fdf..3dfafc1 100644
--- a/src/lua-hiredis.c
+++ b/src/lua-hiredis.c
@@ -14,7 +14,7 @@ extern "C" {
}
#endif
-#include "hiredis.h"
+#include "hiredis/hiredis.h"
#if 0
#define SPAM(a) printf a
@@ -23,7 +23,7 @@ extern "C" {
#endif
#define LUAHIREDIS_VERSION "lua-hiredis 0.2.1"
-#define LUAHIREDIS_COPYRIGHT "Copyright (C) 2011—2012, lua-hiredis authors"
+#define LUAHIREDIS_COPYRIGHT "Copyright (C) 2011—2013, lua-hiredis authors"
#define LUAHIREDIS_DESCRIPTION "Bindings for hiredis Redis-client library"
#define LUAHIREDIS_CONN_MT "lua-hiredis.connection"
@@ -77,7 +77,7 @@ static int lconst_tostring(lua_State * L)
}
/* const API */
-static const struct luaL_reg CONST_MT[] =
+static const struct luaL_Reg CONST_MT[] =
{
{ "__tostring", lconst_tostring },
@@ -136,7 +136,7 @@ static int lstatus_index(lua_State * L)
}
/* status API */
-static const struct luaL_reg STATUS_MT[] =
+static const struct luaL_Reg STATUS_MT[] =
{
{ "__index", lstatus_index },
@@ -418,7 +418,7 @@ static int lconn_tostring(lua_State * L)
return 1;
}
-static const luaL_reg M[] =
+static const luaL_Reg M[] =
{
{ "command", lconn_command },
{ "append_command", lconn_append_command },
@@ -436,12 +436,18 @@ static int lhiredis_connect(lua_State * L)
luahiredis_Connection * pResult = NULL;
redisContext * pContext = NULL;
- const char * host = luaL_checkstring(L, 1);
- int port = luaL_checkint(L, 2);
+ const char * host_or_socket = luaL_checkstring(L, 1);
- /* TODO: Support Timeout, Unix and UnixTimeout flavors */
+ /* TODO: Support Timeout and UnixTimeout flavors */
+ if (lua_isnoneornil(L, 2))
+ {
+ pContext = redisConnectUnix(host_or_socket);
+ }
+ else
+ {
+ pContext = redisConnect(host_or_socket, luaL_checkint(L, 2));
+ }
- pContext = redisConnect(host, port);
if (!pContext)
{
luaL_checkstack(L, 2, "not enough stack to push error");
@@ -567,13 +573,13 @@ static int lhiredis_unwrap_reply(lua_State * L)
return 1;
}
-static const struct luaL_reg E[] = /* Empty */
+static const struct luaL_Reg E[] = /* Empty */
{
{ NULL, NULL }
};
/* Lua module API */
-static const struct luaL_reg R[] =
+static const struct luaL_Reg R[] =
{
{ "connect", lhiredis_connect },
{ "unwrap_reply", lhiredis_unwrap_reply },
diff --git a/test/test.lua b/test/test.lua
index 2f15c04..d47fc00 100644
--- a/test/test.lua
+++ b/test/test.lua
@@ -8,6 +8,12 @@ local CACHED_ERR = nil
--------------------------------------------------------------------------------
+local UDS_SOCKET = "/var/run/redis/redis.sock"
+local HOST = "localhost"
+local PORT = 6379
+
+--------------------------------------------------------------------------------
+
assert(type(hiredis.NIL == "table"))
assert(hiredis.NIL.name == "NIL")
assert(hiredis.NIL.type == hiredis.REPLY_NIL)
@@ -53,7 +59,44 @@ assert(hiredis.connect("badaddress", 1) == nil)
--------------------------------------------------------------------------------
-local conn = assert(hiredis.connect("localhost", 6379))
+assert(hiredis.connect("/var/run/redis/inexistant.sock") == nil)
+
+--------------------------------------------------------------------------------
+
+local ok, posix = pcall(require, "posix")
+if not ok then
+ print("WARNING: luaposix not found, can't test Unix Domain Socket support")
+ print(" consider installing it as follows:")
+ print("")
+ print(" sudo luarocks install luaposix")
+ print("")
+elseif not posix.stat(UDS_SOCKET) then
+ print("WARNING: Redis Unix domain socket file not found.")
+ print(" Can't test Unix Domain Socket support.")
+ print(" consider running Redis as follows:")
+ print("")
+ print(" sudo redis-server --unixsocket ".. UDS_SOCKET .. " --port 0")
+ print("")
+else
+ local net_unix = assert(io.open("/proc/net/unix", "r"))
+ local sockets = assert(net_unix:read("*a"))
+ net_unix:close()
+ if not sockets:find(UDS_SOCKET, nil, true) then
+ print("WARNING: Redis Unix domain socket file not open.")
+ print(" Can't test Unix Domain Socket support.")
+ print(" consider running Redis as follows:")
+ print("")
+ print(" sudo redis-server --unixsocket ".. UDS_SOCKET .. " --port 0")
+ print("")
+ else
+ local conn = assert(hiredis.connect(UDS_SOCKET))
+ assert(conn:command("quit"))
+ end
+end
+
+--------------------------------------------------------------------------------
+
+local conn = assert(hiredis.connect(HOST, PORT))
--------------------------------------------------------------------------------
|