]> git.saurik.com Git - redis.git/blob - src/redis-cli.c
563c75300798e3af6879c7983b7c09215cf319bb
[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 char dbnum[16];
267 if (config.dbnum == 0) return REDIS_OK;
268
269 snprintf(dbnum,sizeof(dbnum),"%d",config.dbnum);
270 reply = redisCommand(context,"SELECT %s",dbnum);
271 if (reply != NULL) {
272 freeReplyObject(reply);
273 return REDIS_OK;
274 }
275 return REDIS_ERR;
276 }
277
278 /* Connect to the client. If force is not zero the connection is performed
279 * even if there is already a connected socket. */
280 static int cliConnect(int force) {
281 if (context == NULL || force) {
282 if (context != NULL)
283 redisFree(context);
284
285 if (config.hostsocket == NULL) {
286 context = redisConnect(config.hostip,config.hostport);
287 } else {
288 context = redisConnectUnix(config.hostsocket);
289 }
290
291 if (context->err) {
292 fprintf(stderr,"Could not connect to Redis at ");
293 if (config.hostsocket == NULL)
294 fprintf(stderr,"%s:%d: %s\n",config.hostip,config.hostport,context->errstr);
295 else
296 fprintf(stderr,"%s: %s\n",config.hostsocket,context->errstr);
297 redisFree(context);
298 context = NULL;
299 return REDIS_ERR;
300 }
301
302 /* Do AUTH and select the right DB. */
303 if (cliAuth() != REDIS_OK)
304 return REDIS_ERR;
305 if (cliSelect() != REDIS_OK)
306 return REDIS_ERR;
307 }
308 return REDIS_OK;
309 }
310
311 static void cliPrintContextErrorAndExit() {
312 if (context == NULL) return;
313 fprintf(stderr,"Error: %s\n",context->errstr);
314 exit(1);
315 }
316
317 static sds cliFormatReplyTTY(redisReply *r, char *prefix) {
318 sds out = sdsempty();
319 switch (r->type) {
320 case REDIS_REPLY_ERROR:
321 out = sdscatprintf(out,"(error) %s\n", r->str);
322 break;
323 case REDIS_REPLY_STATUS:
324 out = sdscat(out,r->str);
325 out = sdscat(out,"\n");
326 break;
327 case REDIS_REPLY_INTEGER:
328 out = sdscatprintf(out,"(integer) %lld\n",r->integer);
329 break;
330 case REDIS_REPLY_STRING:
331 /* If you are producing output for the standard output we want
332 * a more interesting output with quoted characters and so forth */
333 out = sdscatrepr(out,r->str,r->len);
334 out = sdscat(out,"\n");
335 break;
336 case REDIS_REPLY_NIL:
337 out = sdscat(out,"(nil)\n");
338 break;
339 case REDIS_REPLY_ARRAY:
340 if (r->elements == 0) {
341 out = sdscat(out,"(empty list or set)\n");
342 } else {
343 unsigned int i, idxlen = 0;
344 char _prefixlen[16];
345 char _prefixfmt[16];
346 sds _prefix;
347 sds tmp;
348
349 /* Calculate chars needed to represent the largest index */
350 i = r->elements;
351 do {
352 idxlen++;
353 i /= 10;
354 } while(i);
355
356 /* Prefix for nested multi bulks should grow with idxlen+2 spaces */
357 memset(_prefixlen,' ',idxlen+2);
358 _prefixlen[idxlen+2] = '\0';
359 _prefix = sdscat(sdsnew(prefix),_prefixlen);
360
361 /* Setup prefix format for every entry */
362 snprintf(_prefixfmt,sizeof(_prefixfmt),"%%s%%%dd) ",idxlen);
363
364 for (i = 0; i < r->elements; i++) {
365 /* Don't use the prefix for the first element, as the parent
366 * caller already prepended the index number. */
367 out = sdscatprintf(out,_prefixfmt,i == 0 ? "" : prefix,i+1);
368
369 /* Format the multi bulk entry */
370 tmp = cliFormatReplyTTY(r->element[i],_prefix);
371 out = sdscatlen(out,tmp,sdslen(tmp));
372 sdsfree(tmp);
373 }
374 sdsfree(_prefix);
375 }
376 break;
377 default:
378 fprintf(stderr,"Unknown reply type: %d\n", r->type);
379 exit(1);
380 }
381 return out;
382 }
383
384 static sds cliFormatReplyRaw(redisReply *r) {
385 sds out = sdsempty(), tmp;
386 size_t i;
387
388 switch (r->type) {
389 case REDIS_REPLY_NIL:
390 /* Nothing... */
391 break;
392 case REDIS_REPLY_ERROR:
393 case REDIS_REPLY_STATUS:
394 case REDIS_REPLY_STRING:
395 out = sdscatlen(out,r->str,r->len);
396 break;
397 case REDIS_REPLY_INTEGER:
398 out = sdscatprintf(out,"%lld",r->integer);
399 break;
400 case REDIS_REPLY_ARRAY:
401 for (i = 0; i < r->elements; i++) {
402 if (i > 0) out = sdscat(out,config.mb_delim);
403 tmp = cliFormatReplyRaw(r->element[i]);
404 out = sdscatlen(out,tmp,sdslen(tmp));
405 sdsfree(tmp);
406 }
407 break;
408 default:
409 fprintf(stderr,"Unknown reply type: %d\n", r->type);
410 exit(1);
411 }
412 return out;
413 }
414
415 static int cliReadReply(int output_raw_strings) {
416 void *_reply;
417 redisReply *reply;
418 sds out;
419
420 if (redisGetReply(context,&_reply) != REDIS_OK) {
421 if (config.shutdown)
422 return REDIS_OK;
423 if (config.interactive) {
424 /* Filter cases where we should reconnect */
425 if (context->err == REDIS_ERR_IO && errno == ECONNRESET)
426 return REDIS_ERR;
427 if (context->err == REDIS_ERR_EOF)
428 return REDIS_ERR;
429 }
430 cliPrintContextErrorAndExit();
431 return REDIS_ERR; /* avoid compiler warning */
432 }
433
434 reply = (redisReply*)_reply;
435 if (output_raw_strings) {
436 out = cliFormatReplyRaw(reply);
437 } else {
438 if (config.raw_output) {
439 out = cliFormatReplyRaw(reply);
440 out = sdscat(out,"\n");
441 } else {
442 out = cliFormatReplyTTY(reply,"");
443 }
444 }
445 fwrite(out,sdslen(out),1,stdout);
446 sdsfree(out);
447 freeReplyObject(reply);
448 return REDIS_OK;
449 }
450
451 static int cliSendCommand(int argc, char **argv, int repeat) {
452 char *command = argv[0];
453 size_t *argvlen;
454 int j, output_raw;
455
456 if (context == NULL) {
457 printf("Not connected, please use: connect <host> <port>\n");
458 return REDIS_OK;
459 }
460
461 output_raw = !strcasecmp(command,"info");
462 if (!strcasecmp(command,"help") || !strcasecmp(command,"?")) {
463 cliOutputHelp(--argc, ++argv);
464 return REDIS_OK;
465 }
466 if (!strcasecmp(command,"shutdown")) config.shutdown = 1;
467 if (!strcasecmp(command,"monitor")) config.monitor_mode = 1;
468 if (!strcasecmp(command,"subscribe") ||
469 !strcasecmp(command,"psubscribe")) config.pubsub_mode = 1;
470
471 /* Setup argument length */
472 argvlen = malloc(argc*sizeof(size_t));
473 for (j = 0; j < argc; j++)
474 argvlen[j] = sdslen(argv[j]);
475
476 while(repeat--) {
477 redisAppendCommandArgv(context,argc,(const char**)argv,argvlen);
478 while (config.monitor_mode) {
479 if (cliReadReply(output_raw) != REDIS_OK) exit(1);
480 fflush(stdout);
481 }
482
483 if (config.pubsub_mode) {
484 if (!config.raw_output)
485 printf("Reading messages... (press Ctrl-C to quit)\n");
486 while (1) {
487 if (cliReadReply(output_raw) != REDIS_OK) exit(1);
488 }
489 }
490
491 if (cliReadReply(output_raw) != REDIS_OK)
492 return REDIS_ERR;
493 }
494 return REDIS_OK;
495 }
496
497 /*------------------------------------------------------------------------------
498 * User interface
499 *--------------------------------------------------------------------------- */
500
501 static int parseOptions(int argc, char **argv) {
502 int i;
503
504 for (i = 1; i < argc; i++) {
505 int lastarg = i==argc-1;
506
507 if (!strcmp(argv[i],"-h") && !lastarg) {
508 sdsfree(config.hostip);
509 config.hostip = sdsnew(argv[i+1]);
510 i++;
511 } else if (!strcmp(argv[i],"-h") && lastarg) {
512 usage();
513 } else if (!strcmp(argv[i],"--help")) {
514 usage();
515 } else if (!strcmp(argv[i],"-x")) {
516 config.stdinarg = 1;
517 } else if (!strcmp(argv[i],"-p") && !lastarg) {
518 config.hostport = atoi(argv[i+1]);
519 i++;
520 } else if (!strcmp(argv[i],"-s") && !lastarg) {
521 config.hostsocket = argv[i+1];
522 i++;
523 } else if (!strcmp(argv[i],"-r") && !lastarg) {
524 config.repeat = strtoll(argv[i+1],NULL,10);
525 i++;
526 } else if (!strcmp(argv[i],"-n") && !lastarg) {
527 config.dbnum = atoi(argv[i+1]);
528 i++;
529 } else if (!strcmp(argv[i],"-a") && !lastarg) {
530 config.auth = argv[i+1];
531 i++;
532 } else if (!strcmp(argv[i],"--raw")) {
533 config.raw_output = 1;
534 } else if (!strcmp(argv[i],"-d") && !lastarg) {
535 sdsfree(config.mb_delim);
536 config.mb_delim = sdsnew(argv[i+1]);
537 i++;
538 } else if (!strcmp(argv[i],"-v") || !strcmp(argv[i], "--version")) {
539 sds version = cliVersion();
540 printf("redis-cli %s\n", version);
541 sdsfree(version);
542 exit(0);
543 } else {
544 break;
545 }
546 }
547 return i;
548 }
549
550 static sds readArgFromStdin(void) {
551 char buf[1024];
552 sds arg = sdsempty();
553
554 while(1) {
555 int nread = read(fileno(stdin),buf,1024);
556
557 if (nread == 0) break;
558 else if (nread == -1) {
559 perror("Reading from standard input");
560 exit(1);
561 }
562 arg = sdscatlen(arg,buf,nread);
563 }
564 return arg;
565 }
566
567 static void usage() {
568 sds version = cliVersion();
569 fprintf(stderr,
570 "redis-cli %s\n"
571 "\n"
572 "Usage: redis-cli [OPTIONS] [cmd [arg [arg ...]]]\n"
573 " -h <hostname> Server hostname (default: 127.0.0.1)\n"
574 " -p <port> Server port (default: 6379)\n"
575 " -s <socket> Server socket (overrides hostname and port)\n"
576 " -a <password> Password to use when connecting to the server\n"
577 " -r <repeat> Execute specified command N times\n"
578 " -n <db> Database number\n"
579 " -x Read last argument from STDIN\n"
580 " -d <delimiter> Multi-bulk delimiter in for raw formatting (default: \\n)\n"
581 " --raw Use raw formatting for replies (default when STDOUT is not a tty)\n"
582 " --help Output this help and exit\n"
583 " --version Output version and exit\n"
584 "\n"
585 "Examples:\n"
586 " cat /etc/passwd | redis-cli -x set mypasswd\n"
587 " redis-cli get mypasswd\n"
588 " redis-cli -r 100 lpush mylist x\n"
589 "\n"
590 "When no command is given, redis-cli starts in interactive mode.\n"
591 "Type \"help\" in interactive mode for information on available commands.\n"
592 "\n",
593 version);
594 sdsfree(version);
595 exit(1);
596 }
597
598 /* Turn the plain C strings into Sds strings */
599 static char **convertToSds(int count, char** args) {
600 int j;
601 char **sds = zmalloc(sizeof(char*)*count);
602
603 for(j = 0; j < count; j++)
604 sds[j] = sdsnew(args[j]);
605
606 return sds;
607 }
608
609 #define LINE_BUFLEN 4096
610 static void repl() {
611 sds historyfile = NULL;
612 int history = 0;
613 char *line;
614 int argc;
615 sds *argv;
616
617 config.interactive = 1;
618 linenoiseSetCompletionCallback(completionCallback);
619
620 /* Only use history when stdin is a tty. */
621 if (isatty(fileno(stdin))) {
622 history = 1;
623
624 if (getenv("HOME") != NULL) {
625 historyfile = sdscatprintf(sdsempty(),"%s/.rediscli_history",getenv("HOME"));
626 linenoiseHistoryLoad(historyfile);
627 }
628 }
629
630 while((line = linenoise(context ? "redis> " : "not connected> ")) != NULL) {
631 if (line[0] != '\0') {
632 argv = sdssplitargs(line,&argc);
633 if (history) linenoiseHistoryAdd(line);
634 if (historyfile) linenoiseHistorySave(historyfile);
635
636 if (argv == NULL) {
637 printf("Invalid argument(s)\n");
638 continue;
639 } else if (argc > 0) {
640 if (strcasecmp(argv[0],"quit") == 0 ||
641 strcasecmp(argv[0],"exit") == 0)
642 {
643 exit(0);
644 } else if (argc == 3 && !strcasecmp(argv[0],"connect")) {
645 sdsfree(config.hostip);
646 config.hostip = sdsnew(argv[1]);
647 config.hostport = atoi(argv[2]);
648 cliConnect(1);
649 } else if (argc == 1 && !strcasecmp(argv[0],"clear")) {
650 linenoiseClearScreen();
651 } else {
652 long long start_time = mstime(), elapsed;
653
654 if (cliSendCommand(argc,argv,1) != REDIS_OK) {
655 cliConnect(1);
656
657 /* If we still cannot send the command,
658 * print error and abort. */
659 if (cliSendCommand(argc,argv,1) != REDIS_OK)
660 cliPrintContextErrorAndExit();
661 }
662 elapsed = mstime()-start_time;
663 if (elapsed >= 500) {
664 printf("(%.2fs)\n",(double)elapsed/1000);
665 }
666 }
667 }
668 /* Free the argument vector */
669 while(argc--) sdsfree(argv[argc]);
670 zfree(argv);
671 }
672 /* linenoise() returns malloc-ed lines like readline() */
673 free(line);
674 }
675 exit(0);
676 }
677
678 static int noninteractive(int argc, char **argv) {
679 int retval = 0;
680 if (config.stdinarg) {
681 argv = zrealloc(argv, (argc+1)*sizeof(char*));
682 argv[argc] = readArgFromStdin();
683 retval = cliSendCommand(argc+1, argv, config.repeat);
684 } else {
685 /* stdin is probably a tty, can be tested with S_ISCHR(s.st_mode) */
686 retval = cliSendCommand(argc, argv, config.repeat);
687 }
688 return retval;
689 }
690
691 int main(int argc, char **argv) {
692 int firstarg;
693
694 config.hostip = sdsnew("127.0.0.1");
695 config.hostport = 6379;
696 config.hostsocket = NULL;
697 config.repeat = 1;
698 config.dbnum = 0;
699 config.interactive = 0;
700 config.shutdown = 0;
701 config.monitor_mode = 0;
702 config.pubsub_mode = 0;
703 config.stdinarg = 0;
704 config.auth = NULL;
705 config.raw_output = !isatty(fileno(stdout)) && (getenv("FAKETTY") == NULL);
706 config.mb_delim = sdsnew("\n");
707 cliInitHelp();
708
709 firstarg = parseOptions(argc,argv);
710 argc -= firstarg;
711 argv += firstarg;
712
713 /* Try to connect */
714 if (cliConnect(0) != REDIS_OK) exit(1);
715
716 /* Start interactive mode when no command is provided */
717 if (argc == 0) repl();
718 /* Otherwise, we have some arguments to execute */
719 return noninteractive(argc,convertToSds(argc,argv));
720 }