]>
Commit | Line | Data |
---|---|---|
ed9b544e | 1 | /* Redis CLI (command line interface) |
2 | * | |
3 | * Copyright (c) 2006-2009, Salvatore Sanfilippo <antirez at gmail dot com> | |
4 | * All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * | |
9 | * * Redistributions of source code must retain the above copyright notice, | |
10 | * this list of conditions and the following disclaimer. | |
11 | * * Redistributions in binary form must reproduce the above copyright | |
12 | * notice, this list of conditions and the following disclaimer in the | |
13 | * documentation and/or other materials provided with the distribution. | |
14 | * * Neither the name of Redis nor the names of its contributors may be used | |
15 | * to endorse or promote products derived from this software without | |
16 | * specific prior written permission. | |
17 | * | |
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
28 | * POSSIBILITY OF SUCH DAMAGE. | |
29 | */ | |
30 | ||
23d4709d | 31 | #include "fmacros.h" |
32 | ||
ed9b544e | 33 | #include <stdio.h> |
34 | #include <string.h> | |
35 | #include <stdlib.h> | |
36 | #include <unistd.h> | |
37 | ||
38 | #include "anet.h" | |
39 | #include "sds.h" | |
40 | #include "adlist.h" | |
41 | #include "zmalloc.h" | |
42 | ||
43 | #define REDIS_CMD_INLINE 1 | |
44 | #define REDIS_CMD_BULK 2 | |
8165a5f2 | 45 | #define REDIS_CMD_MULTIBULK 3 |
ed9b544e | 46 | |
47 | #define REDIS_NOTUSED(V) ((void) V) | |
48 | ||
49 | static struct config { | |
50 | char *hostip; | |
51 | int hostport; | |
5762b7f0 | 52 | long repeat; |
ed9b544e | 53 | } config; |
54 | ||
55 | struct redisCommand { | |
56 | char *name; | |
57 | int arity; | |
58 | int flags; | |
59 | }; | |
60 | ||
61 | static struct redisCommand cmdTable[] = { | |
c937aa89 | 62 | {"get",2,REDIS_CMD_INLINE}, |
63 | {"set",3,REDIS_CMD_BULK}, | |
64 | {"setnx",3,REDIS_CMD_BULK}, | |
5109cdff | 65 | {"del",-2,REDIS_CMD_INLINE}, |
c937aa89 | 66 | {"exists",2,REDIS_CMD_INLINE}, |
67 | {"incr",2,REDIS_CMD_INLINE}, | |
68 | {"decr",2,REDIS_CMD_INLINE}, | |
69 | {"rpush",3,REDIS_CMD_BULK}, | |
70 | {"lpush",3,REDIS_CMD_BULK}, | |
71 | {"rpop",2,REDIS_CMD_INLINE}, | |
72 | {"lpop",2,REDIS_CMD_INLINE}, | |
73 | {"llen",2,REDIS_CMD_INLINE}, | |
74 | {"lindex",3,REDIS_CMD_INLINE}, | |
75 | {"lset",4,REDIS_CMD_BULK}, | |
76 | {"lrange",4,REDIS_CMD_INLINE}, | |
77 | {"ltrim",4,REDIS_CMD_INLINE}, | |
78 | {"lrem",4,REDIS_CMD_BULK}, | |
0f5f7e9a | 79 | {"rpoplpush",3,REDIS_CMD_BULK}, |
c937aa89 | 80 | {"sadd",3,REDIS_CMD_BULK}, |
81 | {"srem",3,REDIS_CMD_BULK}, | |
a4460ef4 | 82 | {"smove",4,REDIS_CMD_BULK}, |
c937aa89 | 83 | {"sismember",3,REDIS_CMD_BULK}, |
84 | {"scard",2,REDIS_CMD_INLINE}, | |
12fea928 | 85 | {"spop",2,REDIS_CMD_INLINE}, |
2abb95a9 | 86 | {"srandmember",2,REDIS_CMD_INLINE}, |
c937aa89 | 87 | {"sinter",-2,REDIS_CMD_INLINE}, |
88 | {"sinterstore",-3,REDIS_CMD_INLINE}, | |
40d224a9 | 89 | {"sunion",-2,REDIS_CMD_INLINE}, |
90 | {"sunionstore",-3,REDIS_CMD_INLINE}, | |
f4f56e1d | 91 | {"sdiff",-2,REDIS_CMD_INLINE}, |
92 | {"sdiffstore",-3,REDIS_CMD_INLINE}, | |
c937aa89 | 93 | {"smembers",2,REDIS_CMD_INLINE}, |
fd8ccf44 | 94 | {"zadd",4,REDIS_CMD_BULK}, |
1b7106e7 | 95 | {"zrem",3,REDIS_CMD_BULK}, |
1807985b | 96 | {"zremrangebyscore",4,REDIS_CMD_INLINE}, |
cc812361 | 97 | {"zrange",4,REDIS_CMD_INLINE}, |
50c55df5 | 98 | {"zrangebyscore",4,REDIS_CMD_INLINE}, |
e3870fab | 99 | {"zrevrange",4,REDIS_CMD_INLINE}, |
3c41331e | 100 | {"zcard",2,REDIS_CMD_INLINE}, |
6e333bbe | 101 | {"zscore",3,REDIS_CMD_BULK}, |
c937aa89 | 102 | {"incrby",3,REDIS_CMD_INLINE}, |
103 | {"decrby",3,REDIS_CMD_INLINE}, | |
a431eb74 | 104 | {"getset",3,REDIS_CMD_BULK}, |
c937aa89 | 105 | {"randomkey",1,REDIS_CMD_INLINE}, |
106 | {"select",2,REDIS_CMD_INLINE}, | |
107 | {"move",3,REDIS_CMD_INLINE}, | |
108 | {"rename",3,REDIS_CMD_INLINE}, | |
109 | {"renamenx",3,REDIS_CMD_INLINE}, | |
110 | {"keys",2,REDIS_CMD_INLINE}, | |
111 | {"dbsize",1,REDIS_CMD_INLINE}, | |
112 | {"ping",1,REDIS_CMD_INLINE}, | |
113 | {"echo",2,REDIS_CMD_BULK}, | |
114 | {"save",1,REDIS_CMD_INLINE}, | |
115 | {"bgsave",1,REDIS_CMD_INLINE}, | |
116 | {"shutdown",1,REDIS_CMD_INLINE}, | |
117 | {"lastsave",1,REDIS_CMD_INLINE}, | |
118 | {"type",2,REDIS_CMD_INLINE}, | |
119 | {"flushdb",1,REDIS_CMD_INLINE}, | |
120 | {"flushall",1,REDIS_CMD_INLINE}, | |
121 | {"sort",-2,REDIS_CMD_INLINE}, | |
122 | {"info",1,REDIS_CMD_INLINE}, | |
123 | {"mget",-2,REDIS_CMD_INLINE}, | |
3305306f | 124 | {"expire",3,REDIS_CMD_INLINE}, |
802e8373 | 125 | {"expireat",3,REDIS_CMD_INLINE}, |
fd88489a | 126 | {"ttl",2,REDIS_CMD_INLINE}, |
321b0e13 | 127 | {"slaveof",3,REDIS_CMD_INLINE}, |
333298da | 128 | {"debug",-2,REDIS_CMD_INLINE}, |
8165a5f2 | 129 | {"mset",-3,REDIS_CMD_MULTIBULK}, |
130 | {"msetnx",-3,REDIS_CMD_MULTIBULK}, | |
ed9b544e | 131 | {NULL,0,0} |
132 | }; | |
133 | ||
c937aa89 | 134 | static int cliReadReply(int fd); |
135 | ||
ed9b544e | 136 | static struct redisCommand *lookupCommand(char *name) { |
137 | int j = 0; | |
138 | while(cmdTable[j].name != NULL) { | |
139 | if (!strcasecmp(name,cmdTable[j].name)) return &cmdTable[j]; | |
140 | j++; | |
141 | } | |
142 | return NULL; | |
143 | } | |
144 | ||
145 | static int cliConnect(void) { | |
146 | char err[ANET_ERR_LEN]; | |
147 | int fd; | |
148 | ||
149 | fd = anetTcpConnect(err,config.hostip,config.hostport); | |
150 | if (fd == ANET_ERR) { | |
151 | fprintf(stderr,"Connect: %s\n",err); | |
152 | return -1; | |
153 | } | |
154 | anetTcpNoDelay(NULL,fd); | |
155 | return fd; | |
156 | } | |
157 | ||
158 | static sds cliReadLine(int fd) { | |
159 | sds line = sdsempty(); | |
160 | ||
161 | while(1) { | |
162 | char c; | |
b91f03a4 | 163 | ssize_t ret; |
ed9b544e | 164 | |
b91f03a4 LH |
165 | ret = read(fd,&c,1); |
166 | if (ret == -1) { | |
ed9b544e | 167 | sdsfree(line); |
168 | return NULL; | |
b91f03a4 | 169 | } else if ((ret == 0) || (c == '\n')) { |
ed9b544e | 170 | break; |
171 | } else { | |
172 | line = sdscatlen(line,&c,1); | |
173 | } | |
174 | } | |
175 | return sdstrim(line,"\r\n"); | |
176 | } | |
177 | ||
c937aa89 | 178 | static int cliReadSingleLineReply(int fd) { |
ed9b544e | 179 | sds reply = cliReadLine(fd); |
180 | ||
181 | if (reply == NULL) return 1; | |
182 | printf("%s\n", reply); | |
ed9b544e | 183 | return 0; |
184 | } | |
185 | ||
c937aa89 | 186 | static int cliReadBulkReply(int fd) { |
ed9b544e | 187 | sds replylen = cliReadLine(fd); |
188 | char *reply, crlf[2]; | |
c937aa89 | 189 | int bulklen; |
ed9b544e | 190 | |
191 | if (replylen == NULL) return 1; | |
ed9b544e | 192 | bulklen = atoi(replylen); |
c937aa89 | 193 | if (bulklen == -1) { |
ed9b544e | 194 | sdsfree(replylen); |
060f6be6 | 195 | printf("(nil)\n"); |
ed9b544e | 196 | return 0; |
197 | } | |
ed9b544e | 198 | reply = zmalloc(bulklen); |
199 | anetRead(fd,reply,bulklen); | |
200 | anetRead(fd,crlf,2); | |
201 | if (bulklen && fwrite(reply,bulklen,1,stdout) == 0) { | |
202 | zfree(reply); | |
203 | return 1; | |
204 | } | |
c937aa89 | 205 | if (isatty(fileno(stdout)) && reply[bulklen-1] != '\n') |
ed9b544e | 206 | printf("\n"); |
207 | zfree(reply); | |
c937aa89 | 208 | return 0; |
ed9b544e | 209 | } |
210 | ||
211 | static int cliReadMultiBulkReply(int fd) { | |
212 | sds replylen = cliReadLine(fd); | |
213 | int elements, c = 1; | |
214 | ||
215 | if (replylen == NULL) return 1; | |
c937aa89 | 216 | elements = atoi(replylen); |
217 | if (elements == -1) { | |
ed9b544e | 218 | sdsfree(replylen); |
219 | printf("(nil)\n"); | |
220 | return 0; | |
221 | } | |
c937aa89 | 222 | if (elements == 0) { |
223 | printf("(empty list or set)\n"); | |
224 | } | |
ed9b544e | 225 | while(elements--) { |
226 | printf("%d. ", c); | |
c937aa89 | 227 | if (cliReadReply(fd)) return 1; |
ed9b544e | 228 | c++; |
229 | } | |
230 | return 0; | |
231 | } | |
232 | ||
c937aa89 | 233 | static int cliReadReply(int fd) { |
234 | char type; | |
235 | ||
236 | if (anetRead(fd,&type,1) <= 0) exit(1); | |
237 | switch(type) { | |
238 | case '-': | |
239 | printf("(error) "); | |
240 | cliReadSingleLineReply(fd); | |
241 | return 1; | |
242 | case '+': | |
e2aba0f9 | 243 | return cliReadSingleLineReply(fd); |
c937aa89 | 244 | case ':': |
443c6409 | 245 | printf("(integer) "); |
c937aa89 | 246 | return cliReadSingleLineReply(fd); |
247 | case '$': | |
248 | return cliReadBulkReply(fd); | |
249 | case '*': | |
250 | return cliReadMultiBulkReply(fd); | |
251 | default: | |
252 | printf("protocol error, got '%c' as reply type byte\n", type); | |
253 | return 1; | |
254 | } | |
255 | } | |
256 | ||
ed9b544e | 257 | static int cliSendCommand(int argc, char **argv) { |
258 | struct redisCommand *rc = lookupCommand(argv[0]); | |
259 | int fd, j, retval = 0; | |
5762b7f0 | 260 | sds cmd; |
ed9b544e | 261 | |
262 | if (!rc) { | |
263 | fprintf(stderr,"Unknown command '%s'\n",argv[0]); | |
264 | return 1; | |
265 | } | |
266 | ||
267 | if ((rc->arity > 0 && argc != rc->arity) || | |
a74f2af6 | 268 | (rc->arity < 0 && argc < -rc->arity)) { |
ed9b544e | 269 | fprintf(stderr,"Wrong number of arguments for '%s'\n",rc->name); |
270 | return 1; | |
271 | } | |
272 | if ((fd = cliConnect()) == -1) return 1; | |
273 | ||
5762b7f0 | 274 | while(config.repeat--) { |
275 | /* Build the command to send */ | |
276 | cmd = sdsempty(); | |
277 | if (rc->flags & REDIS_CMD_MULTIBULK) { | |
278 | cmd = sdscatprintf(cmd,"*%d\r\n",argc); | |
279 | for (j = 0; j < argc; j++) { | |
280 | cmd = sdscatprintf(cmd,"$%d\r\n",sdslen(argv[j])); | |
8165a5f2 | 281 | cmd = sdscatlen(cmd,argv[j],sdslen(argv[j])); |
5762b7f0 | 282 | cmd = sdscatlen(cmd,"\r\n",2); |
283 | } | |
284 | } else { | |
285 | for (j = 0; j < argc; j++) { | |
286 | if (j != 0) cmd = sdscat(cmd," "); | |
287 | if (j == argc-1 && rc->flags & REDIS_CMD_BULK) { | |
288 | cmd = sdscatprintf(cmd,"%d",sdslen(argv[j])); | |
289 | } else { | |
290 | cmd = sdscatlen(cmd,argv[j],sdslen(argv[j])); | |
291 | } | |
292 | } | |
293 | cmd = sdscat(cmd,"\r\n"); | |
294 | if (rc->flags & REDIS_CMD_BULK) { | |
295 | cmd = sdscatlen(cmd,argv[argc-1],sdslen(argv[argc-1])); | |
296 | cmd = sdscatlen(cmd,"\r\n",2); | |
8165a5f2 | 297 | } |
ed9b544e | 298 | } |
5762b7f0 | 299 | anetWrite(fd,cmd,sdslen(cmd)); |
300 | sdsfree(cmd); | |
301 | retval = cliReadReply(fd); | |
302 | if (retval) { | |
303 | close(fd); | |
304 | return retval; | |
8165a5f2 | 305 | } |
ed9b544e | 306 | } |
ed9b544e | 307 | close(fd); |
308 | return 0; | |
309 | } | |
310 | ||
311 | static int parseOptions(int argc, char **argv) { | |
312 | int i; | |
313 | ||
314 | for (i = 1; i < argc; i++) { | |
315 | int lastarg = i==argc-1; | |
316 | ||
317 | if (!strcmp(argv[i],"-h") && !lastarg) { | |
318 | char *ip = zmalloc(32); | |
319 | if (anetResolve(NULL,argv[i+1],ip) == ANET_ERR) { | |
320 | printf("Can't resolve %s\n", argv[i]); | |
321 | exit(1); | |
322 | } | |
323 | config.hostip = ip; | |
324 | i++; | |
325 | } else if (!strcmp(argv[i],"-p") && !lastarg) { | |
326 | config.hostport = atoi(argv[i+1]); | |
327 | i++; | |
5762b7f0 | 328 | } else if (!strcmp(argv[i],"-r") && !lastarg) { |
329 | config.repeat = strtoll(argv[i+1],NULL,10); | |
330 | i++; | |
ed9b544e | 331 | } else { |
332 | break; | |
333 | } | |
334 | } | |
335 | return i; | |
336 | } | |
337 | ||
338 | static sds readArgFromStdin(void) { | |
339 | char buf[1024]; | |
340 | sds arg = sdsempty(); | |
341 | ||
342 | while(1) { | |
343 | int nread = read(fileno(stdin),buf,1024); | |
344 | ||
345 | if (nread == 0) break; | |
346 | else if (nread == -1) { | |
347 | perror("Reading from standard input"); | |
348 | exit(1); | |
349 | } | |
350 | arg = sdscatlen(arg,buf,nread); | |
351 | } | |
352 | return arg; | |
353 | } | |
354 | ||
355 | int main(int argc, char **argv) { | |
356 | int firstarg, j; | |
357 | char **argvcopy; | |
2073a849 | 358 | struct redisCommand *rc; |
ed9b544e | 359 | |
360 | config.hostip = "127.0.0.1"; | |
361 | config.hostport = 6379; | |
5762b7f0 | 362 | config.repeat = 1; |
ed9b544e | 363 | |
364 | firstarg = parseOptions(argc,argv); | |
365 | argc -= firstarg; | |
366 | argv += firstarg; | |
367 | ||
368 | /* Turn the plain C strings into Sds strings */ | |
369 | argvcopy = zmalloc(sizeof(char*)*argc+1); | |
370 | for(j = 0; j < argc; j++) | |
371 | argvcopy[j] = sdsnew(argv[j]); | |
372 | ||
ed9b544e | 373 | if (argc < 1) { |
5762b7f0 | 374 | fprintf(stderr, "usage: redis-cli [-h host] [-p port] [-r repeat_times] cmd arg1 arg2 arg3 ... argN\n"); |
375 | fprintf(stderr, "usage: echo \"argN\" | redis-cli [-h host] [-p port] -r [repeat_times] cmd arg1 arg2 ... arg(N-1)\n"); | |
ed9b544e | 376 | fprintf(stderr, "\nIf a pipe from standard input is detected this data is used as last argument.\n\n"); |
377 | fprintf(stderr, "example: cat /etc/passwd | redis-cli set my_passwd\n"); | |
378 | fprintf(stderr, "example: redis-cli get my_passwd\n"); | |
5762b7f0 | 379 | fprintf(stderr, "example: redis-cli -r 100 lpush mylist x\n"); |
ed9b544e | 380 | exit(1); |
381 | } | |
382 | ||
2073a849 | 383 | /* Read the last argument from stdandard input if needed */ |
384 | if ((rc = lookupCommand(argv[0])) != NULL) { | |
385 | if (rc->arity > 0 && argc == rc->arity-1) { | |
386 | sds lastarg = readArgFromStdin(); | |
387 | argvcopy[argc] = lastarg; | |
388 | argc++; | |
389 | } | |
390 | } | |
391 | ||
ed9b544e | 392 | return cliSendCommand(argc, argvcopy); |
393 | } |