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