]>
git.saurik.com Git - redis.git/blob - src/redis-cli.c
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.
47 #include "linenoise.h"
50 #define REDIS_NOTUSED(V) ((void) V)
52 static redisContext
*context
;
53 static struct config
{
64 int stdinarg
; /* get last arg from stdin. (-x option) */
66 int raw_output
; /* output mode per command */
72 char *redisGitSHA1(void);
73 char *redisGitDirty(void);
75 /*------------------------------------------------------------------------------
77 *--------------------------------------------------------------------------- */
79 static long long mstime(void) {
83 gettimeofday(&tv
, NULL
);
84 mst
= ((long)tv
.tv_sec
)*1000;
85 mst
+= tv
.tv_usec
/1000;
89 static void cliRefreshPrompt(void) {
90 if (config
.dbnum
== 0)
91 snprintf(config
.prompt
,sizeof(config
.prompt
),"redis %s:%d> ",
92 config
.hostip
, config
.hostport
);
94 snprintf(config
.prompt
,sizeof(config
.prompt
),"redis %s:%d[%d]> ",
95 config
.hostip
, config
.hostport
, config
.dbnum
);
98 /*------------------------------------------------------------------------------
100 *--------------------------------------------------------------------------- */
102 #define CLI_HELP_COMMAND 1
103 #define CLI_HELP_GROUP 2
111 /* Only used for help on commands */
112 struct commandHelp
*org
;
115 static helpEntry
*helpEntries
;
116 static int helpEntriesLen
;
118 static sds
cliVersion() {
120 version
= sdscatprintf(sdsempty(), "%s", REDIS_VERSION
);
122 /* Add git commit and working tree status when available */
123 if (strtoll(redisGitSHA1(),NULL
,16)) {
124 version
= sdscatprintf(version
, " (git:%s", redisGitSHA1());
125 if (strtoll(redisGitDirty(),NULL
,10))
126 version
= sdscatprintf(version
, "-dirty");
127 version
= sdscat(version
, ")");
132 static void cliInitHelp() {
133 int commandslen
= sizeof(commandHelp
)/sizeof(struct commandHelp
);
134 int groupslen
= sizeof(commandGroups
)/sizeof(char*);
138 helpEntriesLen
= len
= commandslen
+groupslen
;
139 helpEntries
= malloc(sizeof(helpEntry
)*len
);
141 for (i
= 0; i
< groupslen
; i
++) {
143 tmp
.argv
= malloc(sizeof(sds
));
144 tmp
.argv
[0] = sdscatprintf(sdsempty(),"@%s",commandGroups
[i
]);
145 tmp
.full
= tmp
.argv
[0];
146 tmp
.type
= CLI_HELP_GROUP
;
148 helpEntries
[pos
++] = tmp
;
151 for (i
= 0; i
< commandslen
; i
++) {
152 tmp
.argv
= sdssplitargs(commandHelp
[i
].name
,&tmp
.argc
);
153 tmp
.full
= sdsnew(commandHelp
[i
].name
);
154 tmp
.type
= CLI_HELP_COMMAND
;
155 tmp
.org
= &commandHelp
[i
];
156 helpEntries
[pos
++] = tmp
;
160 /* Output command help to stdout. */
161 static void cliOutputCommandHelp(struct commandHelp
*help
, int group
) {
162 printf("\r\n \x1b[1m%s\x1b[0m \x1b[90m%s\x1b[0m\r\n", help
->name
, help
->params
);
163 printf(" \x1b[33msummary:\x1b[0m %s\r\n", help
->summary
);
164 printf(" \x1b[33msince:\x1b[0m %s\r\n", help
->since
);
166 printf(" \x1b[33mgroup:\x1b[0m %s\r\n", commandGroups
[help
->group
]);
170 /* Print generic help. */
171 static void cliOutputGenericHelp() {
172 sds version
= cliVersion();
175 "Type: \"help @<group>\" to get a list of commands in <group>\r\n"
176 " \"help <command>\" for help on <command>\r\n"
177 " \"help <tab>\" to get a list of possible help topics\r\n"
178 " \"quit\" to exit\r\n",
184 /* Output all command help, filtering by group or command name. */
185 static void cliOutputHelp(int argc
, char **argv
) {
189 struct commandHelp
*help
;
192 cliOutputGenericHelp();
194 } else if (argc
> 0 && argv
[0][0] == '@') {
195 len
= sizeof(commandGroups
)/sizeof(char*);
196 for (i
= 0; i
< len
; i
++) {
197 if (strcasecmp(argv
[0]+1,commandGroups
[i
]) == 0) {
205 for (i
= 0; i
< helpEntriesLen
; i
++) {
206 entry
= &helpEntries
[i
];
207 if (entry
->type
!= CLI_HELP_COMMAND
) continue;
211 /* Compare all arguments */
212 if (argc
== entry
->argc
) {
213 for (j
= 0; j
< argc
; j
++) {
214 if (strcasecmp(argv
[j
],entry
->argv
[j
]) != 0) break;
217 cliOutputCommandHelp(help
,1);
221 if (group
== help
->group
) {
222 cliOutputCommandHelp(help
,0);
229 static void completionCallback(const char *buf
, linenoiseCompletions
*lc
) {
236 if (strncasecmp(buf
,"help ",5) == 0) {
238 while (isspace(buf
[startpos
])) startpos
++;
239 mask
= CLI_HELP_COMMAND
| CLI_HELP_GROUP
;
241 mask
= CLI_HELP_COMMAND
;
244 for (i
= 0; i
< helpEntriesLen
; i
++) {
245 if (!(helpEntries
[i
].type
& mask
)) continue;
247 matchlen
= strlen(buf
+startpos
);
248 if (strncasecmp(buf
+startpos
,helpEntries
[i
].full
,matchlen
) == 0) {
249 tmp
= sdsnewlen(buf
,startpos
);
250 tmp
= sdscat(tmp
,helpEntries
[i
].full
);
251 linenoiseAddCompletion(lc
,tmp
);
257 /*------------------------------------------------------------------------------
258 * Networking / parsing
259 *--------------------------------------------------------------------------- */
261 /* Send AUTH command to the server */
262 static int cliAuth() {
264 if (config
.auth
== NULL
) return REDIS_OK
;
266 reply
= redisCommand(context
,"AUTH %s",config
.auth
);
268 freeReplyObject(reply
);
274 /* Send SELECT dbnum to the server */
275 static int cliSelect() {
277 if (config
.dbnum
== 0) return REDIS_OK
;
279 reply
= redisCommand(context
,"SELECT %d",config
.dbnum
);
281 freeReplyObject(reply
);
287 /* Connect to the client. If force is not zero the connection is performed
288 * even if there is already a connected socket. */
289 static int cliConnect(int force
) {
290 if (context
== NULL
|| force
) {
294 if (config
.hostsocket
== NULL
) {
295 context
= redisConnect(config
.hostip
,config
.hostport
);
297 context
= redisConnectUnix(config
.hostsocket
);
301 fprintf(stderr
,"Could not connect to Redis at ");
302 if (config
.hostsocket
== NULL
)
303 fprintf(stderr
,"%s:%d: %s\n",config
.hostip
,config
.hostport
,context
->errstr
);
305 fprintf(stderr
,"%s: %s\n",config
.hostsocket
,context
->errstr
);
311 /* Do AUTH and select the right DB. */
312 if (cliAuth() != REDIS_OK
)
314 if (cliSelect() != REDIS_OK
)
320 static void cliPrintContextError() {
321 if (context
== NULL
) return;
322 fprintf(stderr
,"Error: %s\n",context
->errstr
);
325 static sds
cliFormatReplyTTY(redisReply
*r
, char *prefix
) {
326 sds out
= sdsempty();
328 case REDIS_REPLY_ERROR
:
329 out
= sdscatprintf(out
,"(error) %s\n", r
->str
);
331 case REDIS_REPLY_STATUS
:
332 out
= sdscat(out
,r
->str
);
333 out
= sdscat(out
,"\n");
335 case REDIS_REPLY_INTEGER
:
336 out
= sdscatprintf(out
,"(integer) %lld\n",r
->integer
);
338 case REDIS_REPLY_STRING
:
339 /* If you are producing output for the standard output we want
340 * a more interesting output with quoted characters and so forth */
341 out
= sdscatrepr(out
,r
->str
,r
->len
);
342 out
= sdscat(out
,"\n");
344 case REDIS_REPLY_NIL
:
345 out
= sdscat(out
,"(nil)\n");
347 case REDIS_REPLY_ARRAY
:
348 if (r
->elements
== 0) {
349 out
= sdscat(out
,"(empty list or set)\n");
351 unsigned int i
, idxlen
= 0;
357 /* Calculate chars needed to represent the largest index */
364 /* Prefix for nested multi bulks should grow with idxlen+2 spaces */
365 memset(_prefixlen
,' ',idxlen
+2);
366 _prefixlen
[idxlen
+2] = '\0';
367 _prefix
= sdscat(sdsnew(prefix
),_prefixlen
);
369 /* Setup prefix format for every entry */
370 snprintf(_prefixfmt
,sizeof(_prefixfmt
),"%%s%%%dd) ",idxlen
);
372 for (i
= 0; i
< r
->elements
; i
++) {
373 /* Don't use the prefix for the first element, as the parent
374 * caller already prepended the index number. */
375 out
= sdscatprintf(out
,_prefixfmt
,i
== 0 ? "" : prefix
,i
+1);
377 /* Format the multi bulk entry */
378 tmp
= cliFormatReplyTTY(r
->element
[i
],_prefix
);
379 out
= sdscatlen(out
,tmp
,sdslen(tmp
));
386 fprintf(stderr
,"Unknown reply type: %d\n", r
->type
);
392 static sds
cliFormatReplyRaw(redisReply
*r
) {
393 sds out
= sdsempty(), tmp
;
397 case REDIS_REPLY_NIL
:
400 case REDIS_REPLY_ERROR
:
401 out
= sdscatlen(out
,r
->str
,r
->len
);
402 out
= sdscatlen(out
,"\n",1);
404 case REDIS_REPLY_STATUS
:
405 case REDIS_REPLY_STRING
:
406 out
= sdscatlen(out
,r
->str
,r
->len
);
408 case REDIS_REPLY_INTEGER
:
409 out
= sdscatprintf(out
,"%lld",r
->integer
);
411 case REDIS_REPLY_ARRAY
:
412 for (i
= 0; i
< r
->elements
; i
++) {
413 if (i
> 0) out
= sdscat(out
,config
.mb_delim
);
414 tmp
= cliFormatReplyRaw(r
->element
[i
]);
415 out
= sdscatlen(out
,tmp
,sdslen(tmp
));
420 fprintf(stderr
,"Unknown reply type: %d\n", r
->type
);
426 static int cliReadReply(int output_raw_strings
) {
431 if (redisGetReply(context
,&_reply
) != REDIS_OK
) {
434 if (config
.interactive
) {
435 /* Filter cases where we should reconnect */
436 if (context
->err
== REDIS_ERR_IO
&& errno
== ECONNRESET
)
438 if (context
->err
== REDIS_ERR_EOF
)
441 cliPrintContextError();
443 return REDIS_ERR
; /* avoid compiler warning */
446 reply
= (redisReply
*)_reply
;
447 if (output_raw_strings
) {
448 out
= cliFormatReplyRaw(reply
);
450 if (config
.raw_output
) {
451 out
= cliFormatReplyRaw(reply
);
452 out
= sdscat(out
,"\n");
454 out
= cliFormatReplyTTY(reply
,"");
457 fwrite(out
,sdslen(out
),1,stdout
);
459 freeReplyObject(reply
);
463 static int cliSendCommand(int argc
, char **argv
, int repeat
) {
464 char *command
= argv
[0];
468 if (context
== NULL
) return REDIS_ERR
;
471 if (!strcasecmp(command
,"info") ||
472 (argc
== 2 && !strcasecmp(command
,"cluster") &&
473 (!strcasecmp(argv
[1],"nodes") ||
474 !strcasecmp(argv
[1],"info"))) ||
475 (argc
== 2 && !strcasecmp(command
,"client") &&
476 !strcasecmp(argv
[1],"list")))
482 if (!strcasecmp(command
,"help") || !strcasecmp(command
,"?")) {
483 cliOutputHelp(--argc
, ++argv
);
486 if (!strcasecmp(command
,"shutdown")) config
.shutdown
= 1;
487 if (!strcasecmp(command
,"monitor")) config
.monitor_mode
= 1;
488 if (!strcasecmp(command
,"subscribe") ||
489 !strcasecmp(command
,"psubscribe")) config
.pubsub_mode
= 1;
491 /* Setup argument length */
492 argvlen
= malloc(argc
*sizeof(size_t));
493 for (j
= 0; j
< argc
; j
++)
494 argvlen
[j
] = sdslen(argv
[j
]);
497 redisAppendCommandArgv(context
,argc
,(const char**)argv
,argvlen
);
498 while (config
.monitor_mode
) {
499 if (cliReadReply(output_raw
) != REDIS_OK
) exit(1);
503 if (config
.pubsub_mode
) {
504 if (!config
.raw_output
)
505 printf("Reading messages... (press Ctrl-C to quit)\n");
507 if (cliReadReply(output_raw
) != REDIS_OK
) exit(1);
511 if (cliReadReply(output_raw
) != REDIS_OK
) {
515 /* Store database number when SELECT was successfully executed. */
516 if (!strcasecmp(command
,"select") && argc
== 2) {
517 config
.dbnum
= atoi(argv
[1]);
521 if (config
.interval
) usleep(config
.interval
);
522 fflush(stdout
); /* Make it grep friendly */
529 /*------------------------------------------------------------------------------
531 *--------------------------------------------------------------------------- */
533 static int parseOptions(int argc
, char **argv
) {
536 for (i
= 1; i
< argc
; i
++) {
537 int lastarg
= i
==argc
-1;
539 if (!strcmp(argv
[i
],"-h") && !lastarg
) {
540 sdsfree(config
.hostip
);
541 config
.hostip
= sdsnew(argv
[i
+1]);
543 } else if (!strcmp(argv
[i
],"-h") && lastarg
) {
545 } else if (!strcmp(argv
[i
],"--help")) {
547 } else if (!strcmp(argv
[i
],"-x")) {
549 } else if (!strcmp(argv
[i
],"-p") && !lastarg
) {
550 config
.hostport
= atoi(argv
[i
+1]);
552 } else if (!strcmp(argv
[i
],"-s") && !lastarg
) {
553 config
.hostsocket
= argv
[i
+1];
555 } else if (!strcmp(argv
[i
],"-r") && !lastarg
) {
556 config
.repeat
= strtoll(argv
[i
+1],NULL
,10);
558 } else if (!strcmp(argv
[i
],"-i") && !lastarg
) {
559 double seconds
= atof(argv
[i
+1]);
560 config
.interval
= seconds
*1000000;
562 } else if (!strcmp(argv
[i
],"-n") && !lastarg
) {
563 config
.dbnum
= atoi(argv
[i
+1]);
565 } else if (!strcmp(argv
[i
],"-a") && !lastarg
) {
566 config
.auth
= argv
[i
+1];
568 } else if (!strcmp(argv
[i
],"--raw")) {
569 config
.raw_output
= 1;
570 } else if (!strcmp(argv
[i
],"-d") && !lastarg
) {
571 sdsfree(config
.mb_delim
);
572 config
.mb_delim
= sdsnew(argv
[i
+1]);
574 } else if (!strcmp(argv
[i
],"-v") || !strcmp(argv
[i
], "--version")) {
575 sds version
= cliVersion();
576 printf("redis-cli %s\n", version
);
586 static sds
readArgFromStdin(void) {
588 sds arg
= sdsempty();
591 int nread
= read(fileno(stdin
),buf
,1024);
593 if (nread
== 0) break;
594 else if (nread
== -1) {
595 perror("Reading from standard input");
598 arg
= sdscatlen(arg
,buf
,nread
);
603 static void usage() {
604 sds version
= cliVersion();
608 "Usage: redis-cli [OPTIONS] [cmd [arg [arg ...]]]\n"
609 " -h <hostname> Server hostname (default: 127.0.0.1)\n"
610 " -p <port> Server port (default: 6379)\n"
611 " -s <socket> Server socket (overrides hostname and port)\n"
612 " -a <password> Password to use when connecting to the server\n"
613 " -r <repeat> Execute specified command N times\n"
614 " -i <interval> When -r is used, waits <interval> seconds per command.\n"
615 " It is possible to specify sub-second times like -i 0.1.\n"
616 " -n <db> Database number\n"
617 " -x Read last argument from STDIN\n"
618 " -d <delimiter> Multi-bulk delimiter in for raw formatting (default: \\n)\n"
619 " --raw Use raw formatting for replies (default when STDOUT is not a tty)\n"
620 " --help Output this help and exit\n"
621 " --version Output version and exit\n"
624 " cat /etc/passwd | redis-cli -x set mypasswd\n"
625 " redis-cli get mypasswd\n"
626 " redis-cli -r 100 lpush mylist x\n"
627 " redis-cli -r 100 -i 1 info | grep used_memory_human:\n"
629 "When no command is given, redis-cli starts in interactive mode.\n"
630 "Type \"help\" in interactive mode for information on available commands.\n"
637 /* Turn the plain C strings into Sds strings */
638 static char **convertToSds(int count
, char** args
) {
640 char **sds
= zmalloc(sizeof(char*)*count
);
642 for(j
= 0; j
< count
; j
++)
643 sds
[j
] = sdsnew(args
[j
]);
648 #define LINE_BUFLEN 4096
650 sds historyfile
= NULL
;
656 config
.interactive
= 1;
657 linenoiseSetCompletionCallback(completionCallback
);
659 /* Only use history when stdin is a tty. */
660 if (isatty(fileno(stdin
))) {
663 if (getenv("HOME") != NULL
) {
664 historyfile
= sdscatprintf(sdsempty(),"%s/.rediscli_history",getenv("HOME"));
665 linenoiseHistoryLoad(historyfile
);
670 while((line
= linenoise(context
? config
.prompt
: "not connected> ")) != NULL
) {
671 if (line
[0] != '\0') {
672 argv
= sdssplitargs(line
,&argc
);
673 if (history
) linenoiseHistoryAdd(line
);
674 if (historyfile
) linenoiseHistorySave(historyfile
);
677 printf("Invalid argument(s)\n");
679 } else if (argc
> 0) {
680 if (strcasecmp(argv
[0],"quit") == 0 ||
681 strcasecmp(argv
[0],"exit") == 0)
684 } else if (argc
== 3 && !strcasecmp(argv
[0],"connect")) {
685 sdsfree(config
.hostip
);
686 config
.hostip
= sdsnew(argv
[1]);
687 config
.hostport
= atoi(argv
[2]);
689 } else if (argc
== 1 && !strcasecmp(argv
[0],"clear")) {
690 linenoiseClearScreen();
692 long long start_time
= mstime(), elapsed
;
693 int repeat
, skipargs
= 0;
695 repeat
= atoi(argv
[0]);
696 if (argc
> 1 && repeat
) {
702 if (cliSendCommand(argc
-skipargs
,argv
+skipargs
,repeat
)
707 /* If we still cannot send the command print error.
708 * We'll try to reconnect the next time. */
709 if (cliSendCommand(argc
-skipargs
,argv
+skipargs
,repeat
)
711 cliPrintContextError();
713 elapsed
= mstime()-start_time
;
714 if (elapsed
>= 500) {
715 printf("(%.2fs)\n",(double)elapsed
/1000);
719 /* Free the argument vector */
720 while(argc
--) sdsfree(argv
[argc
]);
723 /* linenoise() returns malloc-ed lines like readline() */
729 static int noninteractive(int argc
, char **argv
) {
731 if (config
.stdinarg
) {
732 argv
= zrealloc(argv
, (argc
+1)*sizeof(char*));
733 argv
[argc
] = readArgFromStdin();
734 retval
= cliSendCommand(argc
+1, argv
, config
.repeat
);
736 /* stdin is probably a tty, can be tested with S_ISCHR(s.st_mode) */
737 retval
= cliSendCommand(argc
, argv
, config
.repeat
);
742 int main(int argc
, char **argv
) {
745 config
.hostip
= sdsnew("127.0.0.1");
746 config
.hostport
= 6379;
747 config
.hostsocket
= NULL
;
751 config
.interactive
= 0;
753 config
.monitor_mode
= 0;
754 config
.pubsub_mode
= 0;
757 config
.raw_output
= !isatty(fileno(stdout
)) && (getenv("FAKETTY") == NULL
);
758 config
.mb_delim
= sdsnew("\n");
761 firstarg
= parseOptions(argc
,argv
);
765 /* Start interactive mode when no command is provided */
767 /* Note that in repl mode we don't abort on connection error.
768 * A new attempt will be performed for every command send. */
773 /* Otherwise, we have some arguments to execute */
774 if (cliConnect(0) != REDIS_OK
) exit(1);
775 return noninteractive(argc
,convertToSds(argc
,argv
));