]> git.saurik.com Git - redis.git/blame - redis-cli.c
rename zslDeleteRange to zslDeleteRangeByScore (to differentiate between deleting...
[redis.git] / redis-cli.c
CommitLineData
ed9b544e 1/* Redis CLI (command line interface)
2 *
12d090d2 3 * Copyright (c) 2009-2010, Salvatore Sanfilippo <antirez at gmail dot com>
ed9b544e 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
e17e0b05 45#define REDIS_CMD_MULTIBULK 4
ed9b544e 46
47#define REDIS_NOTUSED(V) ((void) V)
48
49static struct config {
50 char *hostip;
51 int hostport;
5762b7f0 52 long repeat;
62e920df 53 int dbnum;
6cf5882c 54 int interactive;
ed9b544e 55} config;
56
57struct redisCommand {
58 char *name;
59 int arity;
60 int flags;
61};
62
63static struct redisCommand cmdTable[] = {
c937aa89 64 {"get",2,REDIS_CMD_INLINE},
65 {"set",3,REDIS_CMD_BULK},
66 {"setnx",3,REDIS_CMD_BULK},
4b00bebd 67 {"append",3,REDIS_CMD_BULK},
39191553 68 {"substr",4,REDIS_CMD_INLINE},
5109cdff 69 {"del",-2,REDIS_CMD_INLINE},
c937aa89 70 {"exists",2,REDIS_CMD_INLINE},
71 {"incr",2,REDIS_CMD_INLINE},
72 {"decr",2,REDIS_CMD_INLINE},
73 {"rpush",3,REDIS_CMD_BULK},
74 {"lpush",3,REDIS_CMD_BULK},
75 {"rpop",2,REDIS_CMD_INLINE},
76 {"lpop",2,REDIS_CMD_INLINE},
b177fd30 77 {"brpop",-3,REDIS_CMD_INLINE},
78 {"blpop",-3,REDIS_CMD_INLINE},
c937aa89 79 {"llen",2,REDIS_CMD_INLINE},
80 {"lindex",3,REDIS_CMD_INLINE},
81 {"lset",4,REDIS_CMD_BULK},
82 {"lrange",4,REDIS_CMD_INLINE},
83 {"ltrim",4,REDIS_CMD_INLINE},
84 {"lrem",4,REDIS_CMD_BULK},
0f5f7e9a 85 {"rpoplpush",3,REDIS_CMD_BULK},
c937aa89 86 {"sadd",3,REDIS_CMD_BULK},
87 {"srem",3,REDIS_CMD_BULK},
a4460ef4 88 {"smove",4,REDIS_CMD_BULK},
c937aa89 89 {"sismember",3,REDIS_CMD_BULK},
90 {"scard",2,REDIS_CMD_INLINE},
12fea928 91 {"spop",2,REDIS_CMD_INLINE},
2abb95a9 92 {"srandmember",2,REDIS_CMD_INLINE},
c937aa89 93 {"sinter",-2,REDIS_CMD_INLINE},
94 {"sinterstore",-3,REDIS_CMD_INLINE},
40d224a9 95 {"sunion",-2,REDIS_CMD_INLINE},
96 {"sunionstore",-3,REDIS_CMD_INLINE},
f4f56e1d 97 {"sdiff",-2,REDIS_CMD_INLINE},
98 {"sdiffstore",-3,REDIS_CMD_INLINE},
c937aa89 99 {"smembers",2,REDIS_CMD_INLINE},
fd8ccf44 100 {"zadd",4,REDIS_CMD_BULK},
7db723ad 101 {"zincrby",4,REDIS_CMD_BULK},
1b7106e7 102 {"zrem",3,REDIS_CMD_BULK},
1807985b 103 {"zremrangebyscore",4,REDIS_CMD_INLINE},
752da584 104 {"zrange",-4,REDIS_CMD_INLINE},
3589e1a7 105 {"zrank",3,REDIS_CMD_BULK},
80181f78 106 {"zrangebyscore",-4,REDIS_CMD_INLINE},
f44dd428 107 {"zcount",4,REDIS_CMD_INLINE},
752da584 108 {"zrevrange",-4,REDIS_CMD_INLINE},
3c41331e 109 {"zcard",2,REDIS_CMD_INLINE},
6e333bbe 110 {"zscore",3,REDIS_CMD_BULK},
c937aa89 111 {"incrby",3,REDIS_CMD_INLINE},
112 {"decrby",3,REDIS_CMD_INLINE},
a431eb74 113 {"getset",3,REDIS_CMD_BULK},
c937aa89 114 {"randomkey",1,REDIS_CMD_INLINE},
115 {"select",2,REDIS_CMD_INLINE},
116 {"move",3,REDIS_CMD_INLINE},
117 {"rename",3,REDIS_CMD_INLINE},
118 {"renamenx",3,REDIS_CMD_INLINE},
119 {"keys",2,REDIS_CMD_INLINE},
120 {"dbsize",1,REDIS_CMD_INLINE},
121 {"ping",1,REDIS_CMD_INLINE},
122 {"echo",2,REDIS_CMD_BULK},
123 {"save",1,REDIS_CMD_INLINE},
124 {"bgsave",1,REDIS_CMD_INLINE},
9d65a1bb 125 {"rewriteaof",1,REDIS_CMD_INLINE},
126 {"bgrewriteaof",1,REDIS_CMD_INLINE},
c937aa89 127 {"shutdown",1,REDIS_CMD_INLINE},
128 {"lastsave",1,REDIS_CMD_INLINE},
129 {"type",2,REDIS_CMD_INLINE},
130 {"flushdb",1,REDIS_CMD_INLINE},
131 {"flushall",1,REDIS_CMD_INLINE},
132 {"sort",-2,REDIS_CMD_INLINE},
133 {"info",1,REDIS_CMD_INLINE},
134 {"mget",-2,REDIS_CMD_INLINE},
3305306f 135 {"expire",3,REDIS_CMD_INLINE},
802e8373 136 {"expireat",3,REDIS_CMD_INLINE},
fd88489a 137 {"ttl",2,REDIS_CMD_INLINE},
321b0e13 138 {"slaveof",3,REDIS_CMD_INLINE},
333298da 139 {"debug",-2,REDIS_CMD_INLINE},
8165a5f2 140 {"mset",-3,REDIS_CMD_MULTIBULK},
141 {"msetnx",-3,REDIS_CMD_MULTIBULK},
621d5c19 142 {"monitor",1,REDIS_CMD_INLINE},
6fa24622
DJMM
143 {"multi",1,REDIS_CMD_INLINE},
144 {"exec",1,REDIS_CMD_MULTIBULK},
145 {"discard",1,REDIS_CMD_INLINE},
ed9b544e 146 {NULL,0,0}
147};
148
c937aa89 149static int cliReadReply(int fd);
a9158272 150static void usage();
c937aa89 151
ed9b544e 152static struct redisCommand *lookupCommand(char *name) {
153 int j = 0;
154 while(cmdTable[j].name != NULL) {
155 if (!strcasecmp(name,cmdTable[j].name)) return &cmdTable[j];
156 j++;
157 }
158 return NULL;
159}
160
161static int cliConnect(void) {
162 char err[ANET_ERR_LEN];
6fa24622 163 static int fd = ANET_ERR;
ed9b544e 164
ed9b544e 165 if (fd == ANET_ERR) {
6fa24622
DJMM
166 fd = anetTcpConnect(err,config.hostip,config.hostport);
167 if (fd == ANET_ERR) {
168 fprintf(stderr, "Could not connect to Redis at %s:%d: %s", config.hostip, config.hostport, err);
169 return -1;
170 }
171 anetTcpNoDelay(NULL,fd);
ed9b544e 172 }
ed9b544e 173 return fd;
174}
175
176static sds cliReadLine(int fd) {
177 sds line = sdsempty();
178
179 while(1) {
180 char c;
b91f03a4 181 ssize_t ret;
ed9b544e 182
b91f03a4
LH
183 ret = read(fd,&c,1);
184 if (ret == -1) {
ed9b544e 185 sdsfree(line);
186 return NULL;
b91f03a4 187 } else if ((ret == 0) || (c == '\n')) {
ed9b544e 188 break;
189 } else {
190 line = sdscatlen(line,&c,1);
191 }
192 }
193 return sdstrim(line,"\r\n");
194}
195
62e920df 196static int cliReadSingleLineReply(int fd, int quiet) {
ed9b544e 197 sds reply = cliReadLine(fd);
198
199 if (reply == NULL) return 1;
62e920df 200 if (!quiet)
201 printf("%s\n", reply);
621d5c19 202 sdsfree(reply);
ed9b544e 203 return 0;
204}
205
c937aa89 206static int cliReadBulkReply(int fd) {
ed9b544e 207 sds replylen = cliReadLine(fd);
208 char *reply, crlf[2];
c937aa89 209 int bulklen;
ed9b544e 210
211 if (replylen == NULL) return 1;
ed9b544e 212 bulklen = atoi(replylen);
c937aa89 213 if (bulklen == -1) {
ed9b544e 214 sdsfree(replylen);
060f6be6 215 printf("(nil)\n");
ed9b544e 216 return 0;
217 }
ed9b544e 218 reply = zmalloc(bulklen);
219 anetRead(fd,reply,bulklen);
220 anetRead(fd,crlf,2);
221 if (bulklen && fwrite(reply,bulklen,1,stdout) == 0) {
222 zfree(reply);
223 return 1;
224 }
c937aa89 225 if (isatty(fileno(stdout)) && reply[bulklen-1] != '\n')
ed9b544e 226 printf("\n");
227 zfree(reply);
c937aa89 228 return 0;
ed9b544e 229}
230
231static int cliReadMultiBulkReply(int fd) {
232 sds replylen = cliReadLine(fd);
233 int elements, c = 1;
234
235 if (replylen == NULL) return 1;
c937aa89 236 elements = atoi(replylen);
237 if (elements == -1) {
ed9b544e 238 sdsfree(replylen);
239 printf("(nil)\n");
240 return 0;
241 }
c937aa89 242 if (elements == 0) {
243 printf("(empty list or set)\n");
244 }
ed9b544e 245 while(elements--) {
246 printf("%d. ", c);
c937aa89 247 if (cliReadReply(fd)) return 1;
ed9b544e 248 c++;
249 }
250 return 0;
251}
252
c937aa89 253static int cliReadReply(int fd) {
254 char type;
255
256 if (anetRead(fd,&type,1) <= 0) exit(1);
257 switch(type) {
258 case '-':
259 printf("(error) ");
62e920df 260 cliReadSingleLineReply(fd,0);
c937aa89 261 return 1;
262 case '+':
62e920df 263 return cliReadSingleLineReply(fd,0);
c937aa89 264 case ':':
443c6409 265 printf("(integer) ");
62e920df 266 return cliReadSingleLineReply(fd,0);
c937aa89 267 case '$':
268 return cliReadBulkReply(fd);
269 case '*':
270 return cliReadMultiBulkReply(fd);
271 default:
272 printf("protocol error, got '%c' as reply type byte\n", type);
273 return 1;
274 }
275}
276
6cf5882c 277static int selectDb(int fd) {
62e920df 278 int retval;
279 sds cmd;
280 char type;
281
282 if (config.dbnum == 0)
283 return 0;
284
285 cmd = sdsempty();
286 cmd = sdscatprintf(cmd,"SELECT %d\r\n",config.dbnum);
287 anetWrite(fd,cmd,sdslen(cmd));
288 anetRead(fd,&type,1);
289 if (type <= 0 || type != '+') return 1;
290 retval = cliReadSingleLineReply(fd,1);
291 if (retval) {
6cf5882c 292 return retval;
62e920df 293 }
294 return 0;
295}
296
ed9b544e 297static int cliSendCommand(int argc, char **argv) {
298 struct redisCommand *rc = lookupCommand(argv[0]);
299 int fd, j, retval = 0;
621d5c19 300 int read_forever = 0;
5762b7f0 301 sds cmd;
ed9b544e 302
303 if (!rc) {
304 fprintf(stderr,"Unknown command '%s'\n",argv[0]);
305 return 1;
306 }
307
308 if ((rc->arity > 0 && argc != rc->arity) ||
a74f2af6 309 (rc->arity < 0 && argc < -rc->arity)) {
ed9b544e 310 fprintf(stderr,"Wrong number of arguments for '%s'\n",rc->name);
311 return 1;
312 }
621d5c19 313 if (!strcasecmp(rc->name,"monitor")) read_forever = 1;
ed9b544e 314 if ((fd = cliConnect()) == -1) return 1;
315
62e920df 316 /* Select db number */
317 retval = selectDb(fd);
318 if (retval) {
319 fprintf(stderr,"Error setting DB num\n");
320 return 1;
321 }
6cf5882c 322
5762b7f0 323 while(config.repeat--) {
324 /* Build the command to send */
325 cmd = sdsempty();
326 if (rc->flags & REDIS_CMD_MULTIBULK) {
327 cmd = sdscatprintf(cmd,"*%d\r\n",argc);
328 for (j = 0; j < argc; j++) {
83c6a618 329 cmd = sdscatprintf(cmd,"$%lu\r\n",
330 (unsigned long)sdslen(argv[j]));
8165a5f2 331 cmd = sdscatlen(cmd,argv[j],sdslen(argv[j]));
5762b7f0 332 cmd = sdscatlen(cmd,"\r\n",2);
333 }
334 } else {
335 for (j = 0; j < argc; j++) {
336 if (j != 0) cmd = sdscat(cmd," ");
337 if (j == argc-1 && rc->flags & REDIS_CMD_BULK) {
83c6a618 338 cmd = sdscatprintf(cmd,"%lu",
339 (unsigned long)sdslen(argv[j]));
5762b7f0 340 } else {
341 cmd = sdscatlen(cmd,argv[j],sdslen(argv[j]));
342 }
343 }
344 cmd = sdscat(cmd,"\r\n");
345 if (rc->flags & REDIS_CMD_BULK) {
346 cmd = sdscatlen(cmd,argv[argc-1],sdslen(argv[argc-1]));
347 cmd = sdscatlen(cmd,"\r\n",2);
8165a5f2 348 }
ed9b544e 349 }
5762b7f0 350 anetWrite(fd,cmd,sdslen(cmd));
351 sdsfree(cmd);
621d5c19 352
353 while (read_forever) {
354 cliReadSingleLineReply(fd,0);
355 }
356
5762b7f0 357 retval = cliReadReply(fd);
358 if (retval) {
5762b7f0 359 return retval;
8165a5f2 360 }
ed9b544e 361 }
ed9b544e 362 return 0;
363}
364
365static int parseOptions(int argc, char **argv) {
366 int i;
367
368 for (i = 1; i < argc; i++) {
369 int lastarg = i==argc-1;
6cf5882c 370
ed9b544e 371 if (!strcmp(argv[i],"-h") && !lastarg) {
372 char *ip = zmalloc(32);
373 if (anetResolve(NULL,argv[i+1],ip) == ANET_ERR) {
374 printf("Can't resolve %s\n", argv[i]);
375 exit(1);
376 }
377 config.hostip = ip;
378 i++;
a9158272 379 } else if (!strcmp(argv[i],"-h") && lastarg) {
380 usage();
ed9b544e 381 } else if (!strcmp(argv[i],"-p") && !lastarg) {
382 config.hostport = atoi(argv[i+1]);
383 i++;
5762b7f0 384 } else if (!strcmp(argv[i],"-r") && !lastarg) {
385 config.repeat = strtoll(argv[i+1],NULL,10);
386 i++;
62e920df 387 } else if (!strcmp(argv[i],"-n") && !lastarg) {
388 config.dbnum = atoi(argv[i+1]);
389 i++;
6cf5882c
MMDJ
390 } else if (!strcmp(argv[i],"-i")) {
391 config.interactive = 1;
ed9b544e 392 } else {
393 break;
394 }
395 }
396 return i;
397}
398
399static sds readArgFromStdin(void) {
400 char buf[1024];
401 sds arg = sdsempty();
402
403 while(1) {
404 int nread = read(fileno(stdin),buf,1024);
405
406 if (nread == 0) break;
407 else if (nread == -1) {
408 perror("Reading from standard input");
409 exit(1);
410 }
411 arg = sdscatlen(arg,buf,nread);
412 }
413 return arg;
414}
415
a9158272 416static void usage() {
6cf5882c 417 fprintf(stderr, "usage: redis-cli [-h host] [-p port] [-r repeat_times] [-n db_num] [-i] cmd arg1 arg2 arg3 ... argN\n");
a9158272 418 fprintf(stderr, "usage: echo \"argN\" | redis-cli [-h host] [-p port] [-r repeat_times] [-n db_num] cmd arg1 arg2 ... arg(N-1)\n");
419 fprintf(stderr, "\nIf a pipe from standard input is detected this data is used as last argument.\n\n");
420 fprintf(stderr, "example: cat /etc/passwd | redis-cli set my_passwd\n");
421 fprintf(stderr, "example: redis-cli get my_passwd\n");
422 fprintf(stderr, "example: redis-cli -r 100 lpush mylist x\n");
d239ec59 423 fprintf(stderr, "\nRun in interactive mode: redis-cli -i or just don't pass any command\n");
a9158272 424 exit(1);
425}
426
6cf5882c
MMDJ
427/* Turn the plain C strings into Sds strings */
428static char **convertToSds(int count, char** args) {
429 int j;
430 char **sds = zmalloc(sizeof(char*)*count+1);
431
432 for(j = 0; j < count; j++)
433 sds[j] = sdsnew(args[j]);
434
435 return sds;
436}
437
438static char *prompt(char *line, int size) {
439 char *retval;
440
441 do {
442 printf(">> ");
443 retval = fgets(line, size, stdin);
444 } while (retval && *line == '\n');
e3c7f002
DJMM
445 line[strlen(line) - 1] = '\0';
446
6cf5882c
MMDJ
447 return retval;
448}
449
450static void repl() {
451 int size = 4096, max = size >> 1, argc;
452 char buffer[size];
453 char *line = buffer;
454 char **ap, *args[max];
455
456 while (prompt(line, size)) {
457 argc = 0;
458
459 for (ap = args; (*ap = strsep(&line, " \t")) != NULL;) {
460 if (**ap != '\0') {
461 if (argc >= max) break;
2f4d2242 462 if (strcasecmp(*ap,"quit") == 0 || strcasecmp(*ap,"exit") == 0)
463 exit(0);
6cf5882c
MMDJ
464 ap++;
465 argc++;
466 }
467 }
468
469 config.repeat = 1;
470 cliSendCommand(argc, convertToSds(argc, args));
471 line = buffer;
472 }
473
474 exit(0);
475}
476
ed9b544e 477int main(int argc, char **argv) {
6cf5882c 478 int firstarg;
ed9b544e 479 char **argvcopy;
2073a849 480 struct redisCommand *rc;
ed9b544e 481
482 config.hostip = "127.0.0.1";
483 config.hostport = 6379;
5762b7f0 484 config.repeat = 1;
62e920df 485 config.dbnum = 0;
6cf5882c 486 config.interactive = 0;
ed9b544e 487
488 firstarg = parseOptions(argc,argv);
489 argc -= firstarg;
490 argv += firstarg;
ed9b544e 491
d239ec59 492 if (argc == 0 || config.interactive == 1) repl();
ed9b544e 493
6cf5882c
MMDJ
494 argvcopy = convertToSds(argc, argv);
495
2073a849 496 /* Read the last argument from stdandard input if needed */
497 if ((rc = lookupCommand(argv[0])) != NULL) {
6cf5882c
MMDJ
498 if (rc->arity > 0 && argc == rc->arity-1) {
499 sds lastarg = readArgFromStdin();
500 argvcopy[argc] = lastarg;
501 argc++;
502 }
2073a849 503 }
504
ed9b544e 505 return cliSendCommand(argc, argvcopy);
506}