]>
git.saurik.com Git - redis.git/blob - redis-cli.c
f541b6ab413aa2ed5f51daae4b75ca6d01ee64a3
1 /* Redis CLI (command line interface)
3 * Copyright (c) 2009-2010, Salvatore Sanfilippo <antirez at gmail dot com>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
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.
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.
43 #include "linenoise.h"
45 #define REDIS_CMD_INLINE 1
46 #define REDIS_CMD_BULK 2
47 #define REDIS_CMD_MULTIBULK 4
49 #define REDIS_NOTUSED(V) ((void) V)
51 static struct config
{
67 static struct redisCommand cmdTable
[] = {
109 {"zremrangebyscore",4},
111 {"zmergeweighed",-4},
115 {"zrangebyscore",-4},
175 static int cliReadReply(int fd
);
178 static struct redisCommand
*lookupCommand(char *name
) {
180 while(cmdTable
[j
].name
!= NULL
) {
181 if (!strcasecmp(name
,cmdTable
[j
].name
)) return &cmdTable
[j
];
187 static int cliConnect(void) {
188 char err
[ANET_ERR_LEN
];
189 static int fd
= ANET_ERR
;
191 if (fd
== ANET_ERR
) {
192 fd
= anetTcpConnect(err
,config
.hostip
,config
.hostport
);
193 if (fd
== ANET_ERR
) {
194 fprintf(stderr
, "Could not connect to Redis at %s:%d: %s", config
.hostip
, config
.hostport
, err
);
197 anetTcpNoDelay(NULL
,fd
);
202 static sds
cliReadLine(int fd
) {
203 sds line
= sdsempty();
213 } else if ((ret
== 0) || (c
== '\n')) {
216 line
= sdscatlen(line
,&c
,1);
219 return sdstrim(line
,"\r\n");
222 static int cliReadSingleLineReply(int fd
, int quiet
) {
223 sds reply
= cliReadLine(fd
);
225 if (reply
== NULL
) return 1;
227 printf("%s\n", reply
);
232 static void printStringRepr(char *s
, int len
) {
240 case '\n': printf("\\n"); break;
241 case '\r': printf("\\r"); break;
242 case '\t': printf("\\t"); break;
243 case '\a': printf("\\a"); break;
244 case '\b': printf("\\b"); break;
249 printf("\\x%02x",(unsigned char)*s
);
257 static int cliReadBulkReply(int fd
) {
258 sds replylen
= cliReadLine(fd
);
259 char *reply
, crlf
[2];
262 if (replylen
== NULL
) return 1;
263 bulklen
= atoi(replylen
);
269 reply
= zmalloc(bulklen
);
270 anetRead(fd
,reply
,bulklen
);
272 if (!isatty(fileno(stdout
))) {
273 if (bulklen
&& fwrite(reply
,bulklen
,1,stdout
) == 0) {
278 /* If you are producing output for the standard output we want
279 * a more interesting output with quoted characters and so forth */
280 printStringRepr(reply
,bulklen
);
286 static int cliReadMultiBulkReply(int fd
) {
287 sds replylen
= cliReadLine(fd
);
290 if (replylen
== NULL
) return 1;
291 elements
= atoi(replylen
);
292 if (elements
== -1) {
298 printf("(empty list or set)\n");
302 if (cliReadReply(fd
)) return 1;
308 static int cliReadReply(int fd
) {
311 if (anetRead(fd
,&type
,1) <= 0) exit(1);
315 cliReadSingleLineReply(fd
,0);
318 return cliReadSingleLineReply(fd
,0);
320 printf("(integer) ");
321 return cliReadSingleLineReply(fd
,0);
323 return cliReadBulkReply(fd
);
325 return cliReadMultiBulkReply(fd
);
327 printf("protocol error, got '%c' as reply type byte\n", type
);
332 static int selectDb(int fd
) {
337 if (config
.dbnum
== 0)
341 cmd
= sdscatprintf(cmd
,"SELECT %d\r\n",config
.dbnum
);
342 anetWrite(fd
,cmd
,sdslen(cmd
));
343 anetRead(fd
,&type
,1);
344 if (type
<= 0 || type
!= '+') return 1;
345 retval
= cliReadSingleLineReply(fd
,1);
352 static int cliSendCommand(int argc
, char **argv
, int repeat
) {
353 struct redisCommand
*rc
= lookupCommand(argv
[0]);
354 int fd
, j
, retval
= 0;
358 fprintf(stderr
,"Unknown command '%s'\n",argv
[0]);
362 if ((rc
->arity
> 0 && argc
!= rc
->arity
) ||
363 (rc
->arity
< 0 && argc
< -rc
->arity
)) {
364 fprintf(stderr
,"Wrong number of arguments for '%s'\n",rc
->name
);
367 if (!strcasecmp(rc
->name
,"monitor")) config
.monitor_mode
= 1;
368 if (!strcasecmp(rc
->name
,"subscribe") ||
369 !strcasecmp(rc
->name
,"psubscribe")) config
.pubsub_mode
= 1;
370 if ((fd
= cliConnect()) == -1) return 1;
372 /* Select db number */
373 retval
= selectDb(fd
);
375 fprintf(stderr
,"Error setting DB num\n");
380 /* Build the command to send */
381 cmd
= sdscatprintf(sdsempty(),"*%d\r\n",argc
);
382 for (j
= 0; j
< argc
; j
++) {
383 cmd
= sdscatprintf(cmd
,"$%lu\r\n",
384 (unsigned long)sdslen(argv
[j
]));
385 cmd
= sdscatlen(cmd
,argv
[j
],sdslen(argv
[j
]));
386 cmd
= sdscatlen(cmd
,"\r\n",2);
388 anetWrite(fd
,cmd
,sdslen(cmd
));
391 while (config
.monitor_mode
) {
392 cliReadSingleLineReply(fd
,0);
395 if (config
.pubsub_mode
) {
396 printf("Reading messages... (press Ctrl-c to quit)\n");
403 retval
= cliReadReply(fd
);
411 static int parseOptions(int argc
, char **argv
) {
414 for (i
= 1; i
< argc
; i
++) {
415 int lastarg
= i
==argc
-1;
417 if (!strcmp(argv
[i
],"-h") && !lastarg
) {
418 char *ip
= zmalloc(32);
419 if (anetResolve(NULL
,argv
[i
+1],ip
) == ANET_ERR
) {
420 printf("Can't resolve %s\n", argv
[i
]);
425 } else if (!strcmp(argv
[i
],"-h") && lastarg
) {
427 } else if (!strcmp(argv
[i
],"-p") && !lastarg
) {
428 config
.hostport
= atoi(argv
[i
+1]);
430 } else if (!strcmp(argv
[i
],"-r") && !lastarg
) {
431 config
.repeat
= strtoll(argv
[i
+1],NULL
,10);
433 } else if (!strcmp(argv
[i
],"-n") && !lastarg
) {
434 config
.dbnum
= atoi(argv
[i
+1]);
436 } else if (!strcmp(argv
[i
],"-a") && !lastarg
) {
437 config
.auth
= argv
[i
+1];
439 } else if (!strcmp(argv
[i
],"-i")) {
440 config
.interactive
= 1;
448 static sds
readArgFromStdin(void) {
450 sds arg
= sdsempty();
453 int nread
= read(fileno(stdin
),buf
,1024);
455 if (nread
== 0) break;
456 else if (nread
== -1) {
457 perror("Reading from standard input");
460 arg
= sdscatlen(arg
,buf
,nread
);
465 static void usage() {
466 fprintf(stderr
, "usage: redis-cli [-h host] [-p port] [-a authpw] [-r repeat_times] [-n db_num] [-i] cmd arg1 arg2 arg3 ... argN\n");
467 fprintf(stderr
, "usage: echo \"argN\" | redis-cli [-h host] [-a authpw] [-p port] [-r repeat_times] [-n db_num] cmd arg1 arg2 ... arg(N-1)\n");
468 fprintf(stderr
, "\nIf a pipe from standard input is detected this data is used as last argument.\n\n");
469 fprintf(stderr
, "example: cat /etc/passwd | redis-cli set my_passwd\n");
470 fprintf(stderr
, "example: redis-cli get my_passwd\n");
471 fprintf(stderr
, "example: redis-cli -r 100 lpush mylist x\n");
472 fprintf(stderr
, "\nRun in interactive mode: redis-cli -i or just don't pass any command\n");
476 /* Turn the plain C strings into Sds strings */
477 static char **convertToSds(int count
, char** args
) {
479 char **sds
= zmalloc(sizeof(char*)*count
+1);
481 for(j
= 0; j
< count
; j
++)
482 sds
[j
] = sdsnew(args
[j
]);
487 static char **splitArguments(char *line
, int *argc
) {
489 char *current
= NULL
;
490 char **vector
= NULL
;
495 while(*p
&& isspace(*p
)) p
++;
498 int inq
=0; /* set to 1 if we are in "quotes" */
501 if (current
== NULL
) current
= sdsempty();
504 if (*p
== '\\' && *(p
+1)) {
509 case 'n': c
= '\n'; break;
510 case 'r': c
= '\r'; break;
511 case 't': c
= '\t'; break;
512 case 'b': c
= '\b'; break;
513 case 'a': c
= '\a'; break;
514 default: c
= *p
; break;
516 current
= sdscatlen(current
,&c
,1);
517 } else if (*p
== '"') {
520 current
= sdscatlen(current
,p
,1);
535 current
= sdscatlen(current
,p
,1);
541 /* add the token to the vector */
542 vector
= zrealloc(vector
,((*argc
)+1)*sizeof(char*));
543 vector
[*argc
] = current
;
552 #define LINE_BUFLEN 4096
557 while((line
= linenoise("redis> ")) != NULL
) {
558 if (line
[0] != '\0') {
559 argv
= splitArguments(line
,&argc
);
560 linenoiseHistoryAdd(line
);
562 if (strcasecmp(argv
[0],"quit") == 0 ||
563 strcasecmp(argv
[0],"exit") == 0)
566 cliSendCommand(argc
, argv
, 1);
568 /* Free the argument vector */
569 for (j
= 0; j
< argc
; j
++)
573 /* linenoise() returns malloc-ed lines like readline() */
579 int main(int argc
, char **argv
) {
582 struct redisCommand
*rc
;
584 config
.hostip
= "127.0.0.1";
585 config
.hostport
= 6379;
588 config
.interactive
= 0;
589 config
.monitor_mode
= 0;
590 config
.pubsub_mode
= 0;
593 firstarg
= parseOptions(argc
,argv
);
597 if (config
.auth
!= NULL
) {
600 authargv
[0] = "AUTH";
601 authargv
[1] = config
.auth
;
602 cliSendCommand(2, convertToSds(2, authargv
), 1);
605 if (argc
== 0 || config
.interactive
== 1) repl();
607 argvcopy
= convertToSds(argc
, argv
);
609 /* Read the last argument from stdandard input if needed */
610 if ((rc
= lookupCommand(argv
[0])) != NULL
) {
611 if (rc
->arity
> 0 && argc
== rc
->arity
-1) {
612 sds lastarg
= readArgFromStdin();
613 argvcopy
[argc
] = lastarg
;
618 return cliSendCommand(argc
, argvcopy
, config
.repeat
);