]> git.saurik.com Git - redis.git/blob - src/redis-cli.c
fc2d4d73202bab87b746e45aa55daa64ffc946d0
[redis.git] / src / redis-cli.c
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 static redisContext *context;
53 static struct config {
54 char *hostip;
55 int hostport;
56 char *hostsocket;
57 long repeat;
58 int dbnum;
59 int interactive;
60 int shutdown;
61 int monitor_mode;
62 int pubsub_mode;
63 int stdinarg; /* get last arg from stdin. (-x option) */
64 char *auth;
65 int raw_output; /* output mode per command */
66 sds mb_delim;
67 } config;
68
69 static void usage();
70 char *redisGitSHA1(void);
71 char *redisGitDirty(void);
72
73 /*------------------------------------------------------------------------------
74 * Utility functions
75 *--------------------------------------------------------------------------- */
76
77 static long long mstime(void) {
78 struct timeval tv;
79 long long mst;
80
81 gettimeofday(&tv, NULL);
82 mst = ((long)tv.tv_sec)*1000;
83 mst += tv.tv_usec/1000;
84 return mst;
85 }
86
87 /*------------------------------------------------------------------------------
88 * Help functions
89 *--------------------------------------------------------------------------- */
90
91 #define CLI_HELP_COMMAND 1
92 #define CLI_HELP_GROUP 2
93
94 typedef struct {
95 int type;
96 int argc;
97 sds *argv;
98 sds full;
99
100 /* Only used for help on commands */
101 struct commandHelp *org;
102 } helpEntry;
103
104 static helpEntry *helpEntries;
105 static int helpEntriesLen;
106
107 static sds cliVersion() {
108 sds version;
109 version = sdscatprintf(sdsempty(), "%s", REDIS_VERSION);
110
111 /* Add git commit and working tree status when available */
112 if (strtoll(redisGitSHA1(),NULL,16)) {
113 version = sdscatprintf(version, " (git:%s", redisGitSHA1());
114 if (strtoll(redisGitDirty(),NULL,10))
115 version = sdscatprintf(version, "-dirty");
116 version = sdscat(version, ")");
117 }
118 return version;
119 }
120
121 static void cliInitHelp() {
122 int commandslen = sizeof(commandHelp)/sizeof(struct commandHelp);
123 int groupslen = sizeof(commandGroups)/sizeof(char*);
124 int i, len, pos = 0;
125 helpEntry tmp;
126
127 helpEntriesLen = len = commandslen+groupslen;
128 helpEntries = malloc(sizeof(helpEntry)*len);
129
130 for (i = 0; i < groupslen; i++) {
131 tmp.argc = 1;
132 tmp.argv = malloc(sizeof(sds));
133 tmp.argv[0] = sdscatprintf(sdsempty(),"@%s",commandGroups[i]);
134 tmp.full = tmp.argv[0];
135 tmp.type = CLI_HELP_GROUP;
136 tmp.org = NULL;
137 helpEntries[pos++] = tmp;
138 }
139
140 for (i = 0; i < commandslen; i++) {
141 tmp.argv = sdssplitargs(commandHelp[i].name,&tmp.argc);
142 tmp.full = sdsnew(commandHelp[i].name);
143 tmp.type = CLI_HELP_COMMAND;
144 tmp.org = &commandHelp[i];
145 helpEntries[pos++] = tmp;
146 }
147 }
148
149 /* Output command help to stdout. */
150 static void cliOutputCommandHelp(struct commandHelp *help, int group) {
151 printf("\r\n \x1b[1m%s\x1b[0m \x1b[90m%s\x1b[0m\r\n", help->name, help->params);
152 printf(" \x1b[33msummary:\x1b[0m %s\r\n", help->summary);
153 printf(" \x1b[33msince:\x1b[0m %s\r\n", help->since);
154 if (group) {
155 printf(" \x1b[33mgroup:\x1b[0m %s\r\n", commandGroups[help->group]);
156 }
157 }
158
159 /* Print generic help. */
160 static void cliOutputGenericHelp() {
161 sds version = cliVersion();
162 printf(
163 "redis-cli %s\r\n"
164 "Type: \"help @<group>\" to get a list of commands in <group>\r\n"
165 " \"help <command>\" for help on <command>\r\n"
166 " \"help <tab>\" to get a list of possible help topics\r\n"
167 " \"quit\" to exit\r\n",
168 version
169 );
170 sdsfree(version);
171 }
172
173 /* Output all command help, filtering by group or command name. */
174 static void cliOutputHelp(int argc, char **argv) {
175 int i, j, len;
176 int group = -1;
177 helpEntry *entry;
178 struct commandHelp *help;
179
180 if (argc == 0) {
181 cliOutputGenericHelp();
182 return;
183 } else if (argc > 0 && argv[0][0] == '@') {
184 len = sizeof(commandGroups)/sizeof(char*);
185 for (i = 0; i < len; i++) {
186 if (strcasecmp(argv[0]+1,commandGroups[i]) == 0) {
187 group = i;
188 break;
189 }
190 }
191 }
192
193 assert(argc > 0);
194 for (i = 0; i < helpEntriesLen; i++) {
195 entry = &helpEntries[i];
196 if (entry->type != CLI_HELP_COMMAND) continue;
197
198 help = entry->org;
199 if (group == -1) {
200 /* Compare all arguments */
201 if (argc == entry->argc) {
202 for (j = 0; j < argc; j++) {
203 if (strcasecmp(argv[j],entry->argv[j]) != 0) break;
204 }
205 if (j == argc) {
206 cliOutputCommandHelp(help,1);
207 }
208 }
209 } else {
210 if (group == help->group) {
211 cliOutputCommandHelp(help,0);
212 }
213 }
214 }
215 printf("\r\n");
216 }
217
218 static void completionCallback(const char *buf, linenoiseCompletions *lc) {
219 size_t startpos = 0;
220 int mask;
221 int i;
222 size_t matchlen;
223 sds tmp;
224
225 if (strncasecmp(buf,"help ",5) == 0) {
226 startpos = 5;
227 while (isspace(buf[startpos])) startpos++;
228 mask = CLI_HELP_COMMAND | CLI_HELP_GROUP;
229 } else {
230 mask = CLI_HELP_COMMAND;
231 }
232
233 for (i = 0; i < helpEntriesLen; i++) {
234 if (!(helpEntries[i].type & mask)) continue;
235
236 matchlen = strlen(buf+startpos);
237 if (strncasecmp(buf+startpos,helpEntries[i].full,matchlen) == 0) {
238 tmp = sdsnewlen(buf,startpos);
239 tmp = sdscat(tmp,helpEntries[i].full);
240 linenoiseAddCompletion(lc,tmp);
241 sdsfree(tmp);
242 }
243 }
244 }
245
246 /*------------------------------------------------------------------------------
247 * Networking / parsing
248 *--------------------------------------------------------------------------- */
249
250 /* Send AUTH command to the server */
251 static int cliAuth() {
252 redisReply *reply;
253 if (config.auth == NULL) return REDIS_OK;
254
255 reply = redisCommand(context,"AUTH %s",config.auth);
256 if (reply != NULL) {
257 freeReplyObject(reply);
258 return REDIS_OK;
259 }
260 return REDIS_ERR;
261 }
262
263 /* Send SELECT dbnum to the server */
264 static int cliSelect() {
265 redisReply *reply;
266 if (config.dbnum == 0) return REDIS_OK;
267
268 reply = redisCommand(context,"SELECT %d",config.dbnum);
269 if (reply != NULL) {
270 freeReplyObject(reply);
271 return REDIS_OK;
272 }
273 return REDIS_ERR;
274 }
275
276 /* Connect to the client. If force is not zero the connection is performed
277 * even if there is already a connected socket. */
278 static int cliConnect(int force) {
279 if (context == NULL || force) {
280 if (context != NULL)
281 redisFree(context);
282
283 if (config.hostsocket == NULL) {
284 context = redisConnect(config.hostip,config.hostport);
285 } else {
286 context = redisConnectUnix(config.hostsocket);
287 }
288
289 if (context->err) {
290 fprintf(stderr,"Could not connect to Redis at ");
291 if (config.hostsocket == NULL)
292 fprintf(stderr,"%s:%d: %s\n",config.hostip,config.hostport,context->errstr);
293 else
294 fprintf(stderr,"%s: %s\n",config.hostsocket,context->errstr);
295 redisFree(context);
296 context = NULL;
297 return REDIS_ERR;
298 }
299
300 /* Do AUTH and select the right DB. */
301 if (cliAuth() != REDIS_OK)
302 return REDIS_ERR;
303 if (cliSelect() != REDIS_OK)
304 return REDIS_ERR;
305 }
306 return REDIS_OK;
307 }
308
309 static void cliPrintContextErrorAndExit() {
310 if (context == NULL) return;
311 fprintf(stderr,"Error: %s\n",context->errstr);
312 exit(1);
313 }
314
315 static sds cliFormatReplyTTY(redisReply *r, char *prefix) {
316 sds out = sdsempty();
317 switch (r->type) {
318 case REDIS_REPLY_ERROR:
319 out = sdscatprintf(out,"(error) %s\n", r->str);
320 break;
321 case REDIS_REPLY_STATUS:
322 out = sdscat(out,r->str);
323 out = sdscat(out,"\n");
324 break;
325 case REDIS_REPLY_INTEGER:
326 out = sdscatprintf(out,"(integer) %lld\n",r->integer);
327 break;
328 case REDIS_REPLY_STRING:
329 /* If you are producing output for the standard output we want
330 * a more interesting output with quoted characters and so forth */
331 out = sdscatrepr(out,r->str,r->len);
332 out = sdscat(out,"\n");
333 break;
334 case REDIS_REPLY_NIL:
335 out = sdscat(out,"(nil)\n");
336 break;
337 case REDIS_REPLY_ARRAY:
338 if (r->elements == 0) {
339 out = sdscat(out,"(empty list or set)\n");
340 } else {
341 unsigned int i, idxlen = 0;
342 char _prefixlen[16];
343 char _prefixfmt[16];
344 sds _prefix;
345 sds tmp;
346
347 /* Calculate chars needed to represent the largest index */
348 i = r->elements;
349 do {
350 idxlen++;
351 i /= 10;
352 } while(i);
353
354 /* Prefix for nested multi bulks should grow with idxlen+2 spaces */
355 memset(_prefixlen,' ',idxlen+2);
356 _prefixlen[idxlen+2] = '\0';
357 _prefix = sdscat(sdsnew(prefix),_prefixlen);
358
359 /* Setup prefix format for every entry */
360 snprintf(_prefixfmt,sizeof(_prefixfmt),"%%s%%%dd) ",idxlen);
361
362 for (i = 0; i < r->elements; i++) {
363 /* Don't use the prefix for the first element, as the parent
364 * caller already prepended the index number. */
365 out = sdscatprintf(out,_prefixfmt,i == 0 ? "" : prefix,i+1);
366
367 /* Format the multi bulk entry */
368 tmp = cliFormatReplyTTY(r->element[i],_prefix);
369 out = sdscatlen(out,tmp,sdslen(tmp));
370 sdsfree(tmp);
371 }
372 sdsfree(_prefix);
373 }
374 break;
375 default:
376 fprintf(stderr,"Unknown reply type: %d\n", r->type);
377 exit(1);
378 }
379 return out;
380 }
381
382 static sds cliFormatReplyRaw(redisReply *r) {
383 sds out = sdsempty(), tmp;
384 size_t i;
385
386 switch (r->type) {
387 case REDIS_REPLY_NIL:
388 /* Nothing... */
389 break;
390 case REDIS_REPLY_ERROR:
391 case REDIS_REPLY_STATUS:
392 case REDIS_REPLY_STRING:
393 out = sdscatlen(out,r->str,r->len);
394 break;
395 case REDIS_REPLY_INTEGER:
396 out = sdscatprintf(out,"%lld",r->integer);
397 break;
398 case REDIS_REPLY_ARRAY:
399 for (i = 0; i < r->elements; i++) {
400 if (i > 0) out = sdscat(out,config.mb_delim);
401 tmp = cliFormatReplyRaw(r->element[i]);
402 out = sdscatlen(out,tmp,sdslen(tmp));
403 sdsfree(tmp);
404 }
405 break;
406 default:
407 fprintf(stderr,"Unknown reply type: %d\n", r->type);
408 exit(1);
409 }
410 return out;
411 }
412
413 static int cliReadReply(int output_raw_strings) {
414 void *_reply;
415 redisReply *reply;
416 sds out;
417
418 if (redisGetReply(context,&_reply) != REDIS_OK) {
419 if (config.shutdown)
420 return REDIS_OK;
421 if (config.interactive) {
422 /* Filter cases where we should reconnect */
423 if (context->err == REDIS_ERR_IO && errno == ECONNRESET)
424 return REDIS_ERR;
425 if (context->err == REDIS_ERR_EOF)
426 return REDIS_ERR;
427 }
428 cliPrintContextErrorAndExit();
429 return REDIS_ERR; /* avoid compiler warning */
430 }
431
432 reply = (redisReply*)_reply;
433 if (output_raw_strings) {
434 out = cliFormatReplyRaw(reply);
435 } else {
436 if (config.raw_output) {
437 out = cliFormatReplyRaw(reply);
438 out = sdscat(out,"\n");
439 } else {
440 out = cliFormatReplyTTY(reply,"");
441 }
442 }
443 fwrite(out,sdslen(out),1,stdout);
444 sdsfree(out);
445 freeReplyObject(reply);
446 return REDIS_OK;
447 }
448
449 static int cliSendCommand(int argc, char **argv, int repeat) {
450 char *command = argv[0];
451 size_t *argvlen;
452 int j, output_raw;
453
454 if (context == NULL) {
455 printf("Not connected, please use: connect <host> <port>\n");
456 return REDIS_OK;
457 }
458
459 output_raw = !strcasecmp(command,"info");
460 if (!strcasecmp(command,"help") || !strcasecmp(command,"?")) {
461 cliOutputHelp(--argc, ++argv);
462 return REDIS_OK;
463 }
464 if (!strcasecmp(command,"shutdown")) config.shutdown = 1;
465 if (!strcasecmp(command,"monitor")) config.monitor_mode = 1;
466 if (!strcasecmp(command,"subscribe") ||
467 !strcasecmp(command,"psubscribe")) config.pubsub_mode = 1;
468
469 /* Setup argument length */
470 argvlen = malloc(argc*sizeof(size_t));
471 for (j = 0; j < argc; j++)
472 argvlen[j] = sdslen(argv[j]);
473
474 while(repeat--) {
475 redisAppendCommandArgv(context,argc,(const char**)argv,argvlen);
476 while (config.monitor_mode) {
477 if (cliReadReply(output_raw) != REDIS_OK) exit(1);
478 fflush(stdout);
479 }
480
481 if (config.pubsub_mode) {
482 if (!config.raw_output)
483 printf("Reading messages... (press Ctrl-C to quit)\n");
484 while (1) {
485 if (cliReadReply(output_raw) != REDIS_OK) exit(1);
486 }
487 }
488
489 if (cliReadReply(output_raw) != REDIS_OK) {
490 free(argvlen);
491 return REDIS_ERR;
492 } else {
493 /* Store database number when SELECT was successfully executed. */
494 if (!strcasecmp(command,"select") && argc == 2)
495 config.dbnum = atoi(argv[1]);
496 }
497 }
498
499 free(argvlen);
500 return REDIS_OK;
501 }
502
503 /*------------------------------------------------------------------------------
504 * User interface
505 *--------------------------------------------------------------------------- */
506
507 static int parseOptions(int argc, char **argv) {
508 int i;
509
510 for (i = 1; i < argc; i++) {
511 int lastarg = i==argc-1;
512
513 if (!strcmp(argv[i],"-h") && !lastarg) {
514 sdsfree(config.hostip);
515 config.hostip = sdsnew(argv[i+1]);
516 i++;
517 } else if (!strcmp(argv[i],"-h") && lastarg) {
518 usage();
519 } else if (!strcmp(argv[i],"--help")) {
520 usage();
521 } else if (!strcmp(argv[i],"-x")) {
522 config.stdinarg = 1;
523 } else if (!strcmp(argv[i],"-p") && !lastarg) {
524 config.hostport = atoi(argv[i+1]);
525 i++;
526 } else if (!strcmp(argv[i],"-s") && !lastarg) {
527 config.hostsocket = argv[i+1];
528 i++;
529 } else if (!strcmp(argv[i],"-r") && !lastarg) {
530 config.repeat = strtoll(argv[i+1],NULL,10);
531 i++;
532 } else if (!strcmp(argv[i],"-n") && !lastarg) {
533 config.dbnum = atoi(argv[i+1]);
534 i++;
535 } else if (!strcmp(argv[i],"-a") && !lastarg) {
536 config.auth = argv[i+1];
537 i++;
538 } else if (!strcmp(argv[i],"--raw")) {
539 config.raw_output = 1;
540 } else if (!strcmp(argv[i],"-d") && !lastarg) {
541 sdsfree(config.mb_delim);
542 config.mb_delim = sdsnew(argv[i+1]);
543 i++;
544 } else if (!strcmp(argv[i],"-v") || !strcmp(argv[i], "--version")) {
545 sds version = cliVersion();
546 printf("redis-cli %s\n", version);
547 sdsfree(version);
548 exit(0);
549 } else {
550 break;
551 }
552 }
553 return i;
554 }
555
556 static sds readArgFromStdin(void) {
557 char buf[1024];
558 sds arg = sdsempty();
559
560 while(1) {
561 int nread = read(fileno(stdin),buf,1024);
562
563 if (nread == 0) break;
564 else if (nread == -1) {
565 perror("Reading from standard input");
566 exit(1);
567 }
568 arg = sdscatlen(arg,buf,nread);
569 }
570 return arg;
571 }
572
573 static void usage() {
574 sds version = cliVersion();
575 fprintf(stderr,
576 "redis-cli %s\n"
577 "\n"
578 "Usage: redis-cli [OPTIONS] [cmd [arg [arg ...]]]\n"
579 " -h <hostname> Server hostname (default: 127.0.0.1)\n"
580 " -p <port> Server port (default: 6379)\n"
581 " -s <socket> Server socket (overrides hostname and port)\n"
582 " -a <password> Password to use when connecting to the server\n"
583 " -r <repeat> Execute specified command N times\n"
584 " -n <db> Database number\n"
585 " -x Read last argument from STDIN\n"
586 " -d <delimiter> Multi-bulk delimiter in for raw formatting (default: \\n)\n"
587 " --raw Use raw formatting for replies (default when STDOUT is not a tty)\n"
588 " --help Output this help and exit\n"
589 " --version Output version and exit\n"
590 "\n"
591 "Examples:\n"
592 " cat /etc/passwd | redis-cli -x set mypasswd\n"
593 " redis-cli get mypasswd\n"
594 " redis-cli -r 100 lpush mylist x\n"
595 "\n"
596 "When no command is given, redis-cli starts in interactive mode.\n"
597 "Type \"help\" in interactive mode for information on available commands.\n"
598 "\n",
599 version);
600 sdsfree(version);
601 exit(1);
602 }
603
604 /* Turn the plain C strings into Sds strings */
605 static char **convertToSds(int count, char** args) {
606 int j;
607 char **sds = zmalloc(sizeof(char*)*count);
608
609 for(j = 0; j < count; j++)
610 sds[j] = sdsnew(args[j]);
611
612 return sds;
613 }
614
615 #define LINE_BUFLEN 4096
616 static void repl() {
617 sds historyfile = NULL;
618 int history = 0;
619 char *line;
620 int argc;
621 sds *argv;
622
623 config.interactive = 1;
624 linenoiseSetCompletionCallback(completionCallback);
625
626 /* Only use history when stdin is a tty. */
627 if (isatty(fileno(stdin))) {
628 history = 1;
629
630 if (getenv("HOME") != NULL) {
631 historyfile = sdscatprintf(sdsempty(),"%s/.rediscli_history",getenv("HOME"));
632 linenoiseHistoryLoad(historyfile);
633 }
634 }
635
636 while((line = linenoise(context ? "redis> " : "not connected> ")) != NULL) {
637 if (line[0] != '\0') {
638 argv = sdssplitargs(line,&argc);
639 if (history) linenoiseHistoryAdd(line);
640 if (historyfile) linenoiseHistorySave(historyfile);
641
642 if (argv == NULL) {
643 printf("Invalid argument(s)\n");
644 continue;
645 } else if (argc > 0) {
646 if (strcasecmp(argv[0],"quit") == 0 ||
647 strcasecmp(argv[0],"exit") == 0)
648 {
649 exit(0);
650 } else if (argc == 3 && !strcasecmp(argv[0],"connect")) {
651 sdsfree(config.hostip);
652 config.hostip = sdsnew(argv[1]);
653 config.hostport = atoi(argv[2]);
654 cliConnect(1);
655 } else if (argc == 1 && !strcasecmp(argv[0],"clear")) {
656 linenoiseClearScreen();
657 } else {
658 long long start_time = mstime(), elapsed;
659
660 if (cliSendCommand(argc,argv,1) != REDIS_OK) {
661 cliConnect(1);
662
663 /* If we still cannot send the command,
664 * print error and abort. */
665 if (cliSendCommand(argc,argv,1) != REDIS_OK)
666 cliPrintContextErrorAndExit();
667 }
668 elapsed = mstime()-start_time;
669 if (elapsed >= 500) {
670 printf("(%.2fs)\n",(double)elapsed/1000);
671 }
672 }
673 }
674 /* Free the argument vector */
675 while(argc--) sdsfree(argv[argc]);
676 zfree(argv);
677 }
678 /* linenoise() returns malloc-ed lines like readline() */
679 free(line);
680 }
681 exit(0);
682 }
683
684 static int noninteractive(int argc, char **argv) {
685 int retval = 0;
686 if (config.stdinarg) {
687 argv = zrealloc(argv, (argc+1)*sizeof(char*));
688 argv[argc] = readArgFromStdin();
689 retval = cliSendCommand(argc+1, argv, config.repeat);
690 } else {
691 /* stdin is probably a tty, can be tested with S_ISCHR(s.st_mode) */
692 retval = cliSendCommand(argc, argv, config.repeat);
693 }
694 return retval;
695 }
696
697 int main(int argc, char **argv) {
698 int firstarg;
699
700 config.hostip = sdsnew("127.0.0.1");
701 config.hostport = 6379;
702 config.hostsocket = NULL;
703 config.repeat = 1;
704 config.dbnum = 0;
705 config.interactive = 0;
706 config.shutdown = 0;
707 config.monitor_mode = 0;
708 config.pubsub_mode = 0;
709 config.stdinarg = 0;
710 config.auth = NULL;
711 config.raw_output = !isatty(fileno(stdout)) && (getenv("FAKETTY") == NULL);
712 config.mb_delim = sdsnew("\n");
713 cliInitHelp();
714
715 firstarg = parseOptions(argc,argv);
716 argc -= firstarg;
717 argv += firstarg;
718
719 /* Try to connect */
720 if (cliConnect(0) != REDIS_OK) exit(1);
721
722 /* Start interactive mode when no command is provided */
723 if (argc == 0) repl();
724 /* Otherwise, we have some arguments to execute */
725 return noninteractive(argc,convertToSds(argc,argv));
726 }