xref: /redis-3.2.3/src/redis-cli.c (revision 87faf906)
1 /* Redis CLI (command line interface)
2  *
3  * Copyright (c) 2009-2010, 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 
31 #include "fmacros.h"
32 #include "version.h"
33 
34 #include <stdio.h>
35 #include <string.h>
36 #include <stdlib.h>
37 #include <unistd.h>
38 #include <ctype.h>
39 #include <errno.h>
40 #include <sys/stat.h>
41 #include <sys/time.h>
42 #include <assert.h>
43 
44 #include "hiredis.h"
45 #include "sds.h"
46 #include "zmalloc.h"
47 #include "linenoise.h"
48 #include "help.h"
49 
50 #define REDIS_NOTUSED(V) ((void) V)
51 
52 #define OUTPUT_STANDARD 0
53 #define OUTPUT_RAW 1
54 #define OUTPUT_CSV 2
55 
56 static redisContext *context;
57 static struct config {
58     char *hostip;
59     int hostport;
60     char *hostsocket;
61     long repeat;
62     long interval;
63     int dbnum;
64     int interactive;
65     int shutdown;
66     int monitor_mode;
67     int pubsub_mode;
68     int latency_mode;
69     int cluster_mode;
70     int cluster_reissue_command;
71     int slave_mode;
72     int stdinarg; /* get last arg from stdin. (-x option) */
73     char *auth;
74     int output; /* output mode, see OUTPUT_* defines */
75     sds mb_delim;
76     char prompt[128];
77     char *eval;
78 } config;
79 
80 static void usage();
81 char *redisGitSHA1(void);
82 char *redisGitDirty(void);
83 
84 /*------------------------------------------------------------------------------
85  * Utility functions
86  *--------------------------------------------------------------------------- */
87 
88 static long long mstime(void) {
89     struct timeval tv;
90     long long mst;
91 
92     gettimeofday(&tv, NULL);
93     mst = ((long)tv.tv_sec)*1000;
94     mst += tv.tv_usec/1000;
95     return mst;
96 }
97 
98 static void cliRefreshPrompt(void) {
99     int len;
100 
101     if (config.hostsocket != NULL)
102         len = snprintf(config.prompt,sizeof(config.prompt),"redis %s",
103                        config.hostsocket);
104     else
105         len = snprintf(config.prompt,sizeof(config.prompt),"redis %s:%d",
106                        config.hostip, config.hostport);
107     /* Add [dbnum] if needed */
108     if (config.dbnum != 0)
109         len += snprintf(config.prompt+len,sizeof(config.prompt)-len,"[%d]",
110             config.dbnum);
111     snprintf(config.prompt+len,sizeof(config.prompt)-len,"> ");
112 }
113 
114 /*------------------------------------------------------------------------------
115  * Help functions
116  *--------------------------------------------------------------------------- */
117 
118 #define CLI_HELP_COMMAND 1
119 #define CLI_HELP_GROUP 2
120 
121 typedef struct {
122     int type;
123     int argc;
124     sds *argv;
125     sds full;
126 
127     /* Only used for help on commands */
128     struct commandHelp *org;
129 } helpEntry;
130 
131 static helpEntry *helpEntries;
132 static int helpEntriesLen;
133 
134 static sds cliVersion() {
135     sds version;
136     version = sdscatprintf(sdsempty(), "%s", REDIS_VERSION);
137 
138     /* Add git commit and working tree status when available */
139     if (strtoll(redisGitSHA1(),NULL,16)) {
140         version = sdscatprintf(version, " (git:%s", redisGitSHA1());
141         if (strtoll(redisGitDirty(),NULL,10))
142             version = sdscatprintf(version, "-dirty");
143         version = sdscat(version, ")");
144     }
145     return version;
146 }
147 
148 static void cliInitHelp() {
149     int commandslen = sizeof(commandHelp)/sizeof(struct commandHelp);
150     int groupslen = sizeof(commandGroups)/sizeof(char*);
151     int i, len, pos = 0;
152     helpEntry tmp;
153 
154     helpEntriesLen = len = commandslen+groupslen;
155     helpEntries = malloc(sizeof(helpEntry)*len);
156 
157     for (i = 0; i < groupslen; i++) {
158         tmp.argc = 1;
159         tmp.argv = malloc(sizeof(sds));
160         tmp.argv[0] = sdscatprintf(sdsempty(),"@%s",commandGroups[i]);
161         tmp.full = tmp.argv[0];
162         tmp.type = CLI_HELP_GROUP;
163         tmp.org = NULL;
164         helpEntries[pos++] = tmp;
165     }
166 
167     for (i = 0; i < commandslen; i++) {
168         tmp.argv = sdssplitargs(commandHelp[i].name,&tmp.argc);
169         tmp.full = sdsnew(commandHelp[i].name);
170         tmp.type = CLI_HELP_COMMAND;
171         tmp.org = &commandHelp[i];
172         helpEntries[pos++] = tmp;
173     }
174 }
175 
176 /* Output command help to stdout. */
177 static void cliOutputCommandHelp(struct commandHelp *help, int group) {
178     printf("\r\n  \x1b[1m%s\x1b[0m \x1b[90m%s\x1b[0m\r\n", help->name, help->params);
179     printf("  \x1b[33msummary:\x1b[0m %s\r\n", help->summary);
180     printf("  \x1b[33msince:\x1b[0m %s\r\n", help->since);
181     if (group) {
182         printf("  \x1b[33mgroup:\x1b[0m %s\r\n", commandGroups[help->group]);
183     }
184 }
185 
186 /* Print generic help. */
187 static void cliOutputGenericHelp() {
188     sds version = cliVersion();
189     printf(
190         "redis-cli %s\r\n"
191         "Type: \"help @<group>\" to get a list of commands in <group>\r\n"
192         "      \"help <command>\" for help on <command>\r\n"
193         "      \"help <tab>\" to get a list of possible help topics\r\n"
194         "      \"quit\" to exit\r\n",
195         version
196     );
197     sdsfree(version);
198 }
199 
200 /* Output all command help, filtering by group or command name. */
201 static void cliOutputHelp(int argc, char **argv) {
202     int i, j, len;
203     int group = -1;
204     helpEntry *entry;
205     struct commandHelp *help;
206 
207     if (argc == 0) {
208         cliOutputGenericHelp();
209         return;
210     } else if (argc > 0 && argv[0][0] == '@') {
211         len = sizeof(commandGroups)/sizeof(char*);
212         for (i = 0; i < len; i++) {
213             if (strcasecmp(argv[0]+1,commandGroups[i]) == 0) {
214                 group = i;
215                 break;
216             }
217         }
218     }
219 
220     assert(argc > 0);
221     for (i = 0; i < helpEntriesLen; i++) {
222         entry = &helpEntries[i];
223         if (entry->type != CLI_HELP_COMMAND) continue;
224 
225         help = entry->org;
226         if (group == -1) {
227             /* Compare all arguments */
228             if (argc == entry->argc) {
229                 for (j = 0; j < argc; j++) {
230                     if (strcasecmp(argv[j],entry->argv[j]) != 0) break;
231                 }
232                 if (j == argc) {
233                     cliOutputCommandHelp(help,1);
234                 }
235             }
236         } else {
237             if (group == help->group) {
238                 cliOutputCommandHelp(help,0);
239             }
240         }
241     }
242     printf("\r\n");
243 }
244 
245 static void completionCallback(const char *buf, linenoiseCompletions *lc) {
246     size_t startpos = 0;
247     int mask;
248     int i;
249     size_t matchlen;
250     sds tmp;
251 
252     if (strncasecmp(buf,"help ",5) == 0) {
253         startpos = 5;
254         while (isspace(buf[startpos])) startpos++;
255         mask = CLI_HELP_COMMAND | CLI_HELP_GROUP;
256     } else {
257         mask = CLI_HELP_COMMAND;
258     }
259 
260     for (i = 0; i < helpEntriesLen; i++) {
261         if (!(helpEntries[i].type & mask)) continue;
262 
263         matchlen = strlen(buf+startpos);
264         if (strncasecmp(buf+startpos,helpEntries[i].full,matchlen) == 0) {
265             tmp = sdsnewlen(buf,startpos);
266             tmp = sdscat(tmp,helpEntries[i].full);
267             linenoiseAddCompletion(lc,tmp);
268             sdsfree(tmp);
269         }
270     }
271 }
272 
273 /*------------------------------------------------------------------------------
274  * Networking / parsing
275  *--------------------------------------------------------------------------- */
276 
277 /* Send AUTH command to the server */
278 static int cliAuth() {
279     redisReply *reply;
280     if (config.auth == NULL) return REDIS_OK;
281 
282     reply = redisCommand(context,"AUTH %s",config.auth);
283     if (reply != NULL) {
284         freeReplyObject(reply);
285         return REDIS_OK;
286     }
287     return REDIS_ERR;
288 }
289 
290 /* Send SELECT dbnum to the server */
291 static int cliSelect() {
292     redisReply *reply;
293     if (config.dbnum == 0) return REDIS_OK;
294 
295     reply = redisCommand(context,"SELECT %d",config.dbnum);
296     if (reply != NULL) {
297         freeReplyObject(reply);
298         return REDIS_OK;
299     }
300     return REDIS_ERR;
301 }
302 
303 /* Connect to the client. If force is not zero the connection is performed
304  * even if there is already a connected socket. */
305 static int cliConnect(int force) {
306     if (context == NULL || force) {
307         if (context != NULL)
308             redisFree(context);
309 
310         if (config.hostsocket == NULL) {
311             context = redisConnect(config.hostip,config.hostport);
312         } else {
313             context = redisConnectUnix(config.hostsocket);
314         }
315 
316         if (context->err) {
317             fprintf(stderr,"Could not connect to Redis at ");
318             if (config.hostsocket == NULL)
319                 fprintf(stderr,"%s:%d: %s\n",config.hostip,config.hostport,context->errstr);
320             else
321                 fprintf(stderr,"%s: %s\n",config.hostsocket,context->errstr);
322             redisFree(context);
323             context = NULL;
324             return REDIS_ERR;
325         }
326 
327         /* Do AUTH and select the right DB. */
328         if (cliAuth() != REDIS_OK)
329             return REDIS_ERR;
330         if (cliSelect() != REDIS_OK)
331             return REDIS_ERR;
332     }
333     return REDIS_OK;
334 }
335 
336 static void cliPrintContextError() {
337     if (context == NULL) return;
338     fprintf(stderr,"Error: %s\n",context->errstr);
339 }
340 
341 static sds cliFormatReplyTTY(redisReply *r, char *prefix) {
342     sds out = sdsempty();
343     switch (r->type) {
344     case REDIS_REPLY_ERROR:
345         out = sdscatprintf(out,"(error) %s\n", r->str);
346     break;
347     case REDIS_REPLY_STATUS:
348         out = sdscat(out,r->str);
349         out = sdscat(out,"\n");
350     break;
351     case REDIS_REPLY_INTEGER:
352         out = sdscatprintf(out,"(integer) %lld\n",r->integer);
353     break;
354     case REDIS_REPLY_STRING:
355         /* If you are producing output for the standard output we want
356         * a more interesting output with quoted characters and so forth */
357         out = sdscatrepr(out,r->str,r->len);
358         out = sdscat(out,"\n");
359     break;
360     case REDIS_REPLY_NIL:
361         out = sdscat(out,"(nil)\n");
362     break;
363     case REDIS_REPLY_ARRAY:
364         if (r->elements == 0) {
365             out = sdscat(out,"(empty list or set)\n");
366         } else {
367             unsigned int i, idxlen = 0;
368             char _prefixlen[16];
369             char _prefixfmt[16];
370             sds _prefix;
371             sds tmp;
372 
373             /* Calculate chars needed to represent the largest index */
374             i = r->elements;
375             do {
376                 idxlen++;
377                 i /= 10;
378             } while(i);
379 
380             /* Prefix for nested multi bulks should grow with idxlen+2 spaces */
381             memset(_prefixlen,' ',idxlen+2);
382             _prefixlen[idxlen+2] = '\0';
383             _prefix = sdscat(sdsnew(prefix),_prefixlen);
384 
385             /* Setup prefix format for every entry */
386             snprintf(_prefixfmt,sizeof(_prefixfmt),"%%s%%%dd) ",idxlen);
387 
388             for (i = 0; i < r->elements; i++) {
389                 /* Don't use the prefix for the first element, as the parent
390                  * caller already prepended the index number. */
391                 out = sdscatprintf(out,_prefixfmt,i == 0 ? "" : prefix,i+1);
392 
393                 /* Format the multi bulk entry */
394                 tmp = cliFormatReplyTTY(r->element[i],_prefix);
395                 out = sdscatlen(out,tmp,sdslen(tmp));
396                 sdsfree(tmp);
397             }
398             sdsfree(_prefix);
399         }
400     break;
401     default:
402         fprintf(stderr,"Unknown reply type: %d\n", r->type);
403         exit(1);
404     }
405     return out;
406 }
407 
408 static sds cliFormatReplyRaw(redisReply *r) {
409     sds out = sdsempty(), tmp;
410     size_t i;
411 
412     switch (r->type) {
413     case REDIS_REPLY_NIL:
414         /* Nothing... */
415         break;
416     case REDIS_REPLY_ERROR:
417         out = sdscatlen(out,r->str,r->len);
418         out = sdscatlen(out,"\n",1);
419         break;
420     case REDIS_REPLY_STATUS:
421     case REDIS_REPLY_STRING:
422         out = sdscatlen(out,r->str,r->len);
423         break;
424     case REDIS_REPLY_INTEGER:
425         out = sdscatprintf(out,"%lld",r->integer);
426         break;
427     case REDIS_REPLY_ARRAY:
428         for (i = 0; i < r->elements; i++) {
429             if (i > 0) out = sdscat(out,config.mb_delim);
430             tmp = cliFormatReplyRaw(r->element[i]);
431             out = sdscatlen(out,tmp,sdslen(tmp));
432             sdsfree(tmp);
433         }
434         break;
435     default:
436         fprintf(stderr,"Unknown reply type: %d\n", r->type);
437         exit(1);
438     }
439     return out;
440 }
441 
442 static sds cliFormatReplyCSV(redisReply *r) {
443     unsigned int i;
444 
445     sds out = sdsempty();
446     switch (r->type) {
447     case REDIS_REPLY_ERROR:
448         out = sdscat(out,"ERROR,");
449         out = sdscatrepr(out,r->str,strlen(r->str));
450     break;
451     case REDIS_REPLY_STATUS:
452         out = sdscatrepr(out,r->str,r->len);
453     break;
454     case REDIS_REPLY_INTEGER:
455         out = sdscatprintf(out,"%lld",r->integer);
456     break;
457     case REDIS_REPLY_STRING:
458         out = sdscatrepr(out,r->str,r->len);
459     break;
460     case REDIS_REPLY_NIL:
461         out = sdscat(out,"NIL\n");
462     break;
463     case REDIS_REPLY_ARRAY:
464         for (i = 0; i < r->elements; i++) {
465             sds tmp = cliFormatReplyCSV(r->element[i]);
466             out = sdscatlen(out,tmp,sdslen(tmp));
467             if (i != r->elements-1) out = sdscat(out,",");
468             sdsfree(tmp);
469         }
470     break;
471     default:
472         fprintf(stderr,"Unknown reply type: %d\n", r->type);
473         exit(1);
474     }
475     return out;
476 }
477 
478 static int cliReadReply(int output_raw_strings) {
479     void *_reply;
480     redisReply *reply;
481     sds out;
482     int output = 1;
483 
484     if (redisGetReply(context,&_reply) != REDIS_OK) {
485         if (config.shutdown)
486             return REDIS_OK;
487         if (config.interactive) {
488             /* Filter cases where we should reconnect */
489             if (context->err == REDIS_ERR_IO && errno == ECONNRESET)
490                 return REDIS_ERR;
491             if (context->err == REDIS_ERR_EOF)
492                 return REDIS_ERR;
493         }
494         cliPrintContextError();
495         exit(1);
496         return REDIS_ERR; /* avoid compiler warning */
497     }
498 
499     reply = (redisReply*)_reply;
500 
501     /* Check if we need to connect to a different node and reissue the request. */
502     if (config.cluster_mode && reply->type == REDIS_REPLY_ERROR &&
503         (!strncmp(reply->str,"MOVED",5) || !strcmp(reply->str,"ASK")))
504     {
505         char *p = reply->str, *s;
506         int slot;
507 
508         output = 0;
509         /* Comments show the position of the pointer as:
510          *
511          * [S] for pointer 's'
512          * [P] for pointer 'p'
513          */
514         s = strchr(p,' ');      /* MOVED[S]3999 127.0.0.1:6381 */
515         p = strchr(s+1,' ');    /* MOVED[S]3999[P]127.0.0.1:6381 */
516         *p = '\0';
517         slot = atoi(s+1);
518         s = strchr(p+1,':');    /* MOVED 3999[P]127.0.0.1[S]6381 */
519         *s = '\0';
520         sdsfree(config.hostip);
521         config.hostip = sdsnew(p+1);
522         config.hostport = atoi(s+1);
523         if (config.interactive)
524             printf("-> Redirected to slot [%d] located at %s:%d\n",
525                 slot, config.hostip, config.hostport);
526         config.cluster_reissue_command = 1;
527     }
528 
529     if (output) {
530         if (output_raw_strings) {
531             out = cliFormatReplyRaw(reply);
532         } else {
533             if (config.output == OUTPUT_RAW) {
534                 out = cliFormatReplyRaw(reply);
535                 out = sdscat(out,"\n");
536             } else if (config.output == OUTPUT_STANDARD) {
537                 out = cliFormatReplyTTY(reply,"");
538             } else if (config.output == OUTPUT_CSV) {
539                 out = cliFormatReplyCSV(reply);
540                 out = sdscat(out,"\n");
541             }
542         }
543         fwrite(out,sdslen(out),1,stdout);
544         sdsfree(out);
545     }
546     freeReplyObject(reply);
547     return REDIS_OK;
548 }
549 
550 static int cliSendCommand(int argc, char **argv, int repeat) {
551     char *command = argv[0];
552     size_t *argvlen;
553     int j, output_raw;
554 
555     if (!strcasecmp(command,"help") || !strcasecmp(command,"?")) {
556         cliOutputHelp(--argc, ++argv);
557         return REDIS_OK;
558     }
559 
560     if (context == NULL) return REDIS_ERR;
561 
562     output_raw = 0;
563     if (!strcasecmp(command,"info") ||
564         (argc == 2 && !strcasecmp(command,"cluster") &&
565                       (!strcasecmp(argv[1],"nodes") ||
566                        !strcasecmp(argv[1],"info"))) ||
567         (argc == 2 && !strcasecmp(command,"client") &&
568                        !strcasecmp(argv[1],"list")))
569 
570     {
571         output_raw = 1;
572     }
573 
574     if (!strcasecmp(command,"shutdown")) config.shutdown = 1;
575     if (!strcasecmp(command,"monitor")) config.monitor_mode = 1;
576     if (!strcasecmp(command,"subscribe") ||
577         !strcasecmp(command,"psubscribe")) config.pubsub_mode = 1;
578 
579     /* Setup argument length */
580     argvlen = malloc(argc*sizeof(size_t));
581     for (j = 0; j < argc; j++)
582         argvlen[j] = sdslen(argv[j]);
583 
584     while(repeat--) {
585         redisAppendCommandArgv(context,argc,(const char**)argv,argvlen);
586         while (config.monitor_mode) {
587             if (cliReadReply(output_raw) != REDIS_OK) exit(1);
588             fflush(stdout);
589         }
590 
591         if (config.pubsub_mode) {
592             if (config.output != OUTPUT_RAW)
593                 printf("Reading messages... (press Ctrl-C to quit)\n");
594             while (1) {
595                 if (cliReadReply(output_raw) != REDIS_OK) exit(1);
596             }
597         }
598 
599         if (cliReadReply(output_raw) != REDIS_OK) {
600             free(argvlen);
601             return REDIS_ERR;
602         } else {
603             /* Store database number when SELECT was successfully executed. */
604             if (!strcasecmp(command,"select") && argc == 2) {
605                 config.dbnum = atoi(argv[1]);
606                 cliRefreshPrompt();
607             }
608         }
609         if (config.interval) usleep(config.interval);
610         fflush(stdout); /* Make it grep friendly */
611     }
612 
613     free(argvlen);
614     return REDIS_OK;
615 }
616 
617 /*------------------------------------------------------------------------------
618  * User interface
619  *--------------------------------------------------------------------------- */
620 
621 static int parseOptions(int argc, char **argv) {
622     int i;
623 
624     for (i = 1; i < argc; i++) {
625         int lastarg = i==argc-1;
626 
627         if (!strcmp(argv[i],"-h") && !lastarg) {
628             sdsfree(config.hostip);
629             config.hostip = sdsnew(argv[++i]);
630         } else if (!strcmp(argv[i],"-h") && lastarg) {
631             usage();
632         } else if (!strcmp(argv[i],"--help")) {
633             usage();
634         } else if (!strcmp(argv[i],"-x")) {
635             config.stdinarg = 1;
636         } else if (!strcmp(argv[i],"-p") && !lastarg) {
637             config.hostport = atoi(argv[++i]);
638         } else if (!strcmp(argv[i],"-s") && !lastarg) {
639             config.hostsocket = argv[++i];
640         } else if (!strcmp(argv[i],"-r") && !lastarg) {
641             config.repeat = strtoll(argv[++i],NULL,10);
642         } else if (!strcmp(argv[i],"-i") && !lastarg) {
643             double seconds = atof(argv[++i]);
644             config.interval = seconds*1000000;
645         } else if (!strcmp(argv[i],"-n") && !lastarg) {
646             config.dbnum = atoi(argv[++i]);
647         } else if (!strcmp(argv[i],"-a") && !lastarg) {
648             config.auth = argv[++i];
649         } else if (!strcmp(argv[i],"--raw")) {
650             config.output = OUTPUT_RAW;
651         } else if (!strcmp(argv[i],"--csv")) {
652             config.output = OUTPUT_CSV;
653         } else if (!strcmp(argv[i],"--latency")) {
654             config.latency_mode = 1;
655         } else if (!strcmp(argv[i],"--slave")) {
656             config.slave_mode = 1;
657         } else if (!strcmp(argv[i],"--eval") && !lastarg) {
658             config.eval = argv[++i];
659         } else if (!strcmp(argv[i],"-c")) {
660             config.cluster_mode = 1;
661         } else if (!strcmp(argv[i],"-d") && !lastarg) {
662             sdsfree(config.mb_delim);
663             config.mb_delim = sdsnew(argv[++i]);
664         } else if (!strcmp(argv[i],"-v") || !strcmp(argv[i], "--version")) {
665             sds version = cliVersion();
666             printf("redis-cli %s\n", version);
667             sdsfree(version);
668             exit(0);
669         } else {
670             break;
671         }
672     }
673     return i;
674 }
675 
676 static sds readArgFromStdin(void) {
677     char buf[1024];
678     sds arg = sdsempty();
679 
680     while(1) {
681         int nread = read(fileno(stdin),buf,1024);
682 
683         if (nread == 0) break;
684         else if (nread == -1) {
685             perror("Reading from standard input");
686             exit(1);
687         }
688         arg = sdscatlen(arg,buf,nread);
689     }
690     return arg;
691 }
692 
693 static void usage() {
694     sds version = cliVersion();
695     fprintf(stderr,
696 "redis-cli %s\n"
697 "\n"
698 "Usage: redis-cli [OPTIONS] [cmd [arg [arg ...]]]\n"
699 "  -h <hostname>    Server hostname (default: 127.0.0.1)\n"
700 "  -p <port>        Server port (default: 6379)\n"
701 "  -s <socket>      Server socket (overrides hostname and port)\n"
702 "  -a <password>    Password to use when connecting to the server\n"
703 "  -r <repeat>      Execute specified command N times\n"
704 "  -i <interval>    When -r is used, waits <interval> seconds per command.\n"
705 "                   It is possible to specify sub-second times like -i 0.1.\n"
706 "  -n <db>          Database number\n"
707 "  -x               Read last argument from STDIN\n"
708 "  -d <delimiter>   Multi-bulk delimiter in for raw formatting (default: \\n)\n"
709 "  -c               Enable cluster mode (follow -ASK and -MOVED redirections)\n"
710 "  --raw            Use raw formatting for replies (default when STDOUT is not a tty)\n"
711 "  --latency        Enter a special mode continuously sampling latency.\n"
712 "  --slave          Simulate a slave showing commands received from the master.\n"
713 "  --eval <file>    Send an EVAL command using the Lua script at <file>.\n"
714 "  --help           Output this help and exit\n"
715 "  --version        Output version and exit\n"
716 "\n"
717 "Examples:\n"
718 "  cat /etc/passwd | redis-cli -x set mypasswd\n"
719 "  redis-cli get mypasswd\n"
720 "  redis-cli -r 100 lpush mylist x\n"
721 "  redis-cli -r 100 -i 1 info | grep used_memory_human:\n"
722 "  redis-cli --eval myscript.lua key1 key2 , arg1 arg2 arg3\n"
723 "  (Note: when using --eval the comma separates KEYS[] from ARGV[] items)\n"
724 "\n"
725 "When no command is given, redis-cli starts in interactive mode.\n"
726 "Type \"help\" in interactive mode for information on available commands.\n"
727 "\n",
728         version);
729     sdsfree(version);
730     exit(1);
731 }
732 
733 /* Turn the plain C strings into Sds strings */
734 static char **convertToSds(int count, char** args) {
735   int j;
736   char **sds = zmalloc(sizeof(char*)*count);
737 
738   for(j = 0; j < count; j++)
739     sds[j] = sdsnew(args[j]);
740 
741   return sds;
742 }
743 
744 #define LINE_BUFLEN 4096
745 static void repl() {
746     sds historyfile = NULL;
747     int history = 0;
748     char *line;
749     int argc;
750     sds *argv;
751 
752     config.interactive = 1;
753     linenoiseSetCompletionCallback(completionCallback);
754 
755     /* Only use history when stdin is a tty. */
756     if (isatty(fileno(stdin))) {
757         history = 1;
758 
759         if (getenv("HOME") != NULL) {
760             historyfile = sdscatprintf(sdsempty(),"%s/.rediscli_history",getenv("HOME"));
761             linenoiseHistoryLoad(historyfile);
762         }
763     }
764 
765     cliRefreshPrompt();
766     while((line = linenoise(context ? config.prompt : "not connected> ")) != NULL) {
767         if (line[0] != '\0') {
768             argv = sdssplitargs(line,&argc);
769             if (history) linenoiseHistoryAdd(line);
770             if (historyfile) linenoiseHistorySave(historyfile);
771 
772             if (argv == NULL) {
773                 printf("Invalid argument(s)\n");
774                 free(line);
775                 continue;
776             } else if (argc > 0) {
777                 if (strcasecmp(argv[0],"quit") == 0 ||
778                     strcasecmp(argv[0],"exit") == 0)
779                 {
780                     exit(0);
781                 } else if (argc == 3 && !strcasecmp(argv[0],"connect")) {
782                     sdsfree(config.hostip);
783                     config.hostip = sdsnew(argv[1]);
784                     config.hostport = atoi(argv[2]);
785                     cliConnect(1);
786                 } else if (argc == 1 && !strcasecmp(argv[0],"clear")) {
787                     linenoiseClearScreen();
788                 } else {
789                     long long start_time = mstime(), elapsed;
790                     int repeat, skipargs = 0;
791 
792                     repeat = atoi(argv[0]);
793                     if (argc > 1 && repeat) {
794                         skipargs = 1;
795                     } else {
796                         repeat = 1;
797                     }
798 
799                     while (1) {
800                         config.cluster_reissue_command = 0;
801                         if (cliSendCommand(argc-skipargs,argv+skipargs,repeat)
802                             != REDIS_OK)
803                         {
804                             cliConnect(1);
805 
806                             /* If we still cannot send the command print error.
807                              * We'll try to reconnect the next time. */
808                             if (cliSendCommand(argc-skipargs,argv+skipargs,repeat)
809                                 != REDIS_OK)
810                                 cliPrintContextError();
811                         }
812                         /* Issue the command again if we got redirected in cluster mode */
813                         if (config.cluster_mode && config.cluster_reissue_command) {
814                             cliConnect(1);
815                         } else {
816                             break;
817                         }
818                     }
819                     elapsed = mstime()-start_time;
820                     if (elapsed >= 500) {
821                         printf("(%.2fs)\n",(double)elapsed/1000);
822                     }
823                 }
824             }
825             /* Free the argument vector */
826             while(argc--) sdsfree(argv[argc]);
827             zfree(argv);
828         }
829         /* linenoise() returns malloc-ed lines like readline() */
830         free(line);
831     }
832     exit(0);
833 }
834 
835 static int noninteractive(int argc, char **argv) {
836     int retval = 0;
837     if (config.stdinarg) {
838         argv = zrealloc(argv, (argc+1)*sizeof(char*));
839         argv[argc] = readArgFromStdin();
840         retval = cliSendCommand(argc+1, argv, config.repeat);
841     } else {
842         /* stdin is probably a tty, can be tested with S_ISCHR(s.st_mode) */
843         retval = cliSendCommand(argc, argv, config.repeat);
844     }
845     return retval;
846 }
847 
848 static int evalMode(int argc, char **argv) {
849     sds script = sdsempty();
850     FILE *fp;
851     char buf[1024];
852     size_t nread;
853     char **argv2;
854     int j, got_comma = 0, keys = 0;
855 
856     /* Load the script from the file, as an sds string. */
857     fp = fopen(config.eval,"r");
858     if (!fp) {
859         fprintf(stderr,
860             "Can't open file '%s': %s\n", config.eval, strerror(errno));
861         exit(1);
862     }
863     while((nread = fread(buf,1,sizeof(buf),fp)) != 0) {
864         script = sdscatlen(script,buf,nread);
865     }
866     fclose(fp);
867 
868     /* Create our argument vector */
869     argv2 = zmalloc(sizeof(sds)*(argc+3));
870     argv2[0] = sdsnew("EVAL");
871     argv2[1] = script;
872     for (j = 0; j < argc; j++) {
873         if (!got_comma && argv[j][0] == ',' && argv[j][1] == 0) {
874             got_comma = 1;
875             continue;
876         }
877         argv2[j+3-got_comma] = sdsnew(argv[j]);
878         if (!got_comma) keys++;
879     }
880     argv2[2] = sdscatprintf(sdsempty(),"%d",keys);
881 
882     /* Call it */
883     return cliSendCommand(argc+3-got_comma, argv2, config.repeat);
884 }
885 
886 static void latencyMode(void) {
887     redisReply *reply;
888     long long start, latency, min, max, tot, count = 0;
889     double avg;
890 
891     if (!context) exit(1);
892     while(1) {
893         start = mstime();
894         reply = redisCommand(context,"PING");
895         if (reply == NULL) {
896             fprintf(stderr,"\nI/O error\n");
897             exit(1);
898         }
899         latency = mstime()-start;
900         freeReplyObject(reply);
901         count++;
902         if (count == 1) {
903             min = max = tot = latency;
904             avg = (double) latency;
905         } else {
906             if (latency < min) min = latency;
907             if (latency > max) max = latency;
908             tot += latency;
909             avg = (double) tot/count;
910         }
911         printf("\x1b[0G\x1b[2Kmin: %lld, max: %lld, avg: %.2f (%lld samples)",
912             min, max, avg, count);
913         fflush(stdout);
914         usleep(10000);
915     }
916 }
917 
918 static void slaveMode(void) {
919     /* To start we need to send the SYNC command and return the payload.
920      * The hiredis client lib does not understand this part of the protocol
921      * and we don't want to mess with its buffers, so everything is performed
922      * using direct low-level I/O. */
923     int fd = context->fd;
924     char buf[1024], *p;
925     ssize_t nread;
926     unsigned long long payload;
927 
928     /* Send the SYNC command. */
929     write(fd,"SYNC\r\n",6);
930 
931     /* Read $<payload>\r\n, making sure to read just up to "\n" */
932     p = buf;
933     while(1) {
934         nread = read(fd,p,1);
935         if (nread <= 0) {
936             fprintf(stderr,"Error reading bulk length while SYNCing\n");
937             exit(1);
938         }
939         if (*p == '\n') break;
940         p++;
941     }
942     *p = '\0';
943     payload = strtoull(buf+1,NULL,10);
944     fprintf(stderr,"SYNC with master, discarding %lld bytes of bulk tranfer...\n",
945             payload);
946 
947     /* Discard the payload. */
948     while(payload) {
949         nread = read(fd,buf,(payload > sizeof(buf)) ? sizeof(buf) : payload);
950         if (nread <= 0) {
951             fprintf(stderr,"Error reading RDB payload while SYNCing\n");
952             exit(1);
953         }
954         payload -= nread;
955     }
956     fprintf(stderr,"SYNC done. Logging commands from master.\n");
957 
958     /* Now we can use the hiredis to read the incoming protocol. */
959     config.output = OUTPUT_CSV;
960     while (cliReadReply(0) == REDIS_OK);
961 }
962 
963 int main(int argc, char **argv) {
964     int firstarg;
965 
966     config.hostip = sdsnew("127.0.0.1");
967     config.hostport = 6379;
968     config.hostsocket = NULL;
969     config.repeat = 1;
970     config.interval = 0;
971     config.dbnum = 0;
972     config.interactive = 0;
973     config.shutdown = 0;
974     config.monitor_mode = 0;
975     config.pubsub_mode = 0;
976     config.latency_mode = 0;
977     config.cluster_mode = 0;
978     config.stdinarg = 0;
979     config.auth = NULL;
980     config.eval = NULL;
981     if (!isatty(fileno(stdout)) && (getenv("FAKETTY") == NULL))
982         config.output = OUTPUT_RAW;
983     else
984         config.output = OUTPUT_STANDARD;
985     config.mb_delim = sdsnew("\n");
986     cliInitHelp();
987 
988     firstarg = parseOptions(argc,argv);
989     argc -= firstarg;
990     argv += firstarg;
991 
992     /* Start in latency mode if appropriate */
993     if (config.latency_mode) {
994         cliConnect(0);
995         latencyMode();
996     }
997 
998     /* Start in slave mode if appropriate */
999     if (config.slave_mode) {
1000         cliConnect(0);
1001         slaveMode();
1002     }
1003 
1004     /* Start interactive mode when no command is provided */
1005     if (argc == 0 && !config.eval) {
1006         /* Note that in repl mode we don't abort on connection error.
1007          * A new attempt will be performed for every command send. */
1008         cliConnect(0);
1009         repl();
1010     }
1011 
1012     /* Otherwise, we have some arguments to execute */
1013     if (cliConnect(0) != REDIS_OK) exit(1);
1014     if (config.eval) {
1015         return evalMode(argc,argv);
1016     } else {
1017         return noninteractive(argc,convertToSds(argc,argv));
1018     }
1019 }
1020