]> git.saurik.com Git - redis.git/blame - src/redis-cli.c
Fix MULTI / EXEC rendering in MONITOR output.
[redis.git] / src / redis-cli.c
CommitLineData
ed9b544e 1/* Redis CLI (command line interface)
2 *
12d090d2 3 * Copyright (c) 2009-2010, Salvatore Sanfilippo <antirez at gmail dot com>
ed9b544e 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
23d4709d 31#include "fmacros.h"
185cabda 32#include "version.h"
23d4709d 33
ed9b544e 34#include <stdio.h>
35#include <string.h>
36#include <stdlib.h>
37#include <unistd.h>
e9f0419c 38#include <time.h>
a88a2af6 39#include <ctype.h>
c0b3d423 40#include <errno.h>
b4b62c34 41#include <sys/stat.h>
3ce014c7 42#include <sys/time.h>
41945ba6 43#include <assert.h>
ed9b544e 44
7fc4ce13 45#include "hiredis.h"
ed9b544e 46#include "sds.h"
ed9b544e 47#include "zmalloc.h"
cf87ebf2 48#include "linenoise.h"
5397f2b5 49#include "help.h"
088c508a 50#include "anet.h"
51#include "ae.h"
ed9b544e 52
ed9b544e 53#define REDIS_NOTUSED(V) ((void) V)
54
60893c6c 55#define OUTPUT_STANDARD 0
56#define OUTPUT_RAW 1
57#define OUTPUT_CSV 2
58
7fc4ce13 59static redisContext *context;
ed9b544e 60static struct config {
61 char *hostip;
62 int hostport;
7e91f971 63 char *hostsocket;
5762b7f0 64 long repeat;
18f63d8d 65 long interval;
62e920df 66 int dbnum;
5d15b520 67 int interactive;
36e5db6d 68 int shutdown;
249c3a7d 69 int monitor_mode;
70 int pubsub_mode;
43071993 71 int latency_mode;
623131d4 72 int cluster_mode;
73 int cluster_reissue_command;
b8283ab2 74 int slave_mode;
088c508a 75 int pipe_mode;
f26761aa 76 int bigkeys;
bc63407b 77 int stdinarg; /* get last arg from stdin. (-x option) */
288799e0 78 char *auth;
60893c6c 79 int output; /* output mode, see OUTPUT_* defines */
65add0a3 80 sds mb_delim;
a5bd0848 81 char prompt[128];
e2f31389 82 char *eval;
ed9b544e 83} config;
84
a9158272 85static void usage();
11fd0c42 86char *redisGitSHA1(void);
c392edf5 87char *redisGitDirty(void);
c937aa89 88
3ce014c7 89/*------------------------------------------------------------------------------
90 * Utility functions
91 *--------------------------------------------------------------------------- */
92
93static long long mstime(void) {
94 struct timeval tv;
95 long long mst;
96
97 gettimeofday(&tv, NULL);
98 mst = ((long)tv.tv_sec)*1000;
99 mst += tv.tv_usec/1000;
100 return mst;
101}
102
3f4eef21 103static void cliRefreshPrompt(void) {
a5bd0848 104 int len;
105
106 if (config.hostsocket != NULL)
107 len = snprintf(config.prompt,sizeof(config.prompt),"redis %s",
108 config.hostsocket);
3f4eef21 109 else
a5bd0848 110 len = snprintf(config.prompt,sizeof(config.prompt),"redis %s:%d",
111 config.hostip, config.hostport);
112 /* Add [dbnum] if needed */
113 if (config.dbnum != 0)
114 len += snprintf(config.prompt+len,sizeof(config.prompt)-len,"[%d]",
115 config.dbnum);
116 snprintf(config.prompt+len,sizeof(config.prompt)-len,"> ");
3f4eef21
PN
117}
118
a2a69d58
PN
119/*------------------------------------------------------------------------------
120 * Help functions
121 *--------------------------------------------------------------------------- */
122
b2cc45bf
PN
123#define CLI_HELP_COMMAND 1
124#define CLI_HELP_GROUP 2
125
126typedef struct {
127 int type;
128 int argc;
129 sds *argv;
130 sds full;
131
132 /* Only used for help on commands */
133 struct commandHelp *org;
134} helpEntry;
135
136static helpEntry *helpEntries;
137static int helpEntriesLen;
138
c392edf5
PN
139static sds cliVersion() {
140 sds version;
141 version = sdscatprintf(sdsempty(), "%s", REDIS_VERSION);
142
143 /* Add git commit and working tree status when available */
144 if (strtoll(redisGitSHA1(),NULL,16)) {
145 version = sdscatprintf(version, " (git:%s", redisGitSHA1());
146 if (strtoll(redisGitDirty(),NULL,10))
147 version = sdscatprintf(version, "-dirty");
148 version = sdscat(version, ")");
149 }
150 return version;
151}
152
b2cc45bf
PN
153static void cliInitHelp() {
154 int commandslen = sizeof(commandHelp)/sizeof(struct commandHelp);
155 int groupslen = sizeof(commandGroups)/sizeof(char*);
156 int i, len, pos = 0;
157 helpEntry tmp;
158
159 helpEntriesLen = len = commandslen+groupslen;
160 helpEntries = malloc(sizeof(helpEntry)*len);
161
162 for (i = 0; i < groupslen; i++) {
163 tmp.argc = 1;
164 tmp.argv = malloc(sizeof(sds));
165 tmp.argv[0] = sdscatprintf(sdsempty(),"@%s",commandGroups[i]);
166 tmp.full = tmp.argv[0];
167 tmp.type = CLI_HELP_GROUP;
168 tmp.org = NULL;
169 helpEntries[pos++] = tmp;
170 }
171
172 for (i = 0; i < commandslen; i++) {
173 tmp.argv = sdssplitargs(commandHelp[i].name,&tmp.argc);
174 tmp.full = sdsnew(commandHelp[i].name);
175 tmp.type = CLI_HELP_COMMAND;
176 tmp.org = &commandHelp[i];
177 helpEntries[pos++] = tmp;
178 }
179}
180
a2a69d58 181/* Output command help to stdout. */
41945ba6
PN
182static void cliOutputCommandHelp(struct commandHelp *help, int group) {
183 printf("\r\n \x1b[1m%s\x1b[0m \x1b[90m%s\x1b[0m\r\n", help->name, help->params);
184 printf(" \x1b[33msummary:\x1b[0m %s\r\n", help->summary);
185 printf(" \x1b[33msince:\x1b[0m %s\r\n", help->since);
186 if (group) {
187 printf(" \x1b[33mgroup:\x1b[0m %s\r\n", commandGroups[help->group]);
188 }
a2a69d58
PN
189}
190
41945ba6
PN
191/* Print generic help. */
192static void cliOutputGenericHelp() {
c392edf5 193 sds version = cliVersion();
41945ba6
PN
194 printf(
195 "redis-cli %s\r\n"
196 "Type: \"help @<group>\" to get a list of commands in <group>\r\n"
197 " \"help <command>\" for help on <command>\r\n"
198 " \"help <tab>\" to get a list of possible help topics\r\n"
199 " \"quit\" to exit\r\n",
c392edf5 200 version
41945ba6 201 );
c392edf5 202 sdsfree(version);
a2a69d58
PN
203}
204
205/* Output all command help, filtering by group or command name. */
41945ba6 206static void cliOutputHelp(int argc, char **argv) {
b2cc45bf 207 int i, j, len;
41945ba6 208 int group = -1;
b2cc45bf
PN
209 helpEntry *entry;
210 struct commandHelp *help;
a2a69d58 211
41945ba6
PN
212 if (argc == 0) {
213 cliOutputGenericHelp();
a2a69d58 214 return;
41945ba6
PN
215 } else if (argc > 0 && argv[0][0] == '@') {
216 len = sizeof(commandGroups)/sizeof(char*);
217 for (i = 0; i < len; i++) {
218 if (strcasecmp(argv[0]+1,commandGroups[i]) == 0) {
219 group = i;
220 break;
221 }
222 }
a2a69d58
PN
223 }
224
41945ba6 225 assert(argc > 0);
b2cc45bf
PN
226 for (i = 0; i < helpEntriesLen; i++) {
227 entry = &helpEntries[i];
228 if (entry->type != CLI_HELP_COMMAND) continue;
229
230 help = entry->org;
a2a69d58 231 if (group == -1) {
b2cc45bf
PN
232 /* Compare all arguments */
233 if (argc == entry->argc) {
234 for (j = 0; j < argc; j++) {
235 if (strcasecmp(argv[j],entry->argv[j]) != 0) break;
236 }
237 if (j == argc) {
238 cliOutputCommandHelp(help,1);
239 }
a2a69d58
PN
240 }
241 } else {
242 if (group == help->group) {
41945ba6 243 cliOutputCommandHelp(help,0);
a2a69d58
PN
244 }
245 }
246 }
41945ba6
PN
247 printf("\r\n");
248}
249
41945ba6
PN
250static void completionCallback(const char *buf, linenoiseCompletions *lc) {
251 size_t startpos = 0;
252 int mask;
253 int i;
254 size_t matchlen;
b2cc45bf 255 sds tmp;
41945ba6
PN
256
257 if (strncasecmp(buf,"help ",5) == 0) {
258 startpos = 5;
259 while (isspace(buf[startpos])) startpos++;
b2cc45bf 260 mask = CLI_HELP_COMMAND | CLI_HELP_GROUP;
41945ba6 261 } else {
b2cc45bf 262 mask = CLI_HELP_COMMAND;
41945ba6
PN
263 }
264
b2cc45bf
PN
265 for (i = 0; i < helpEntriesLen; i++) {
266 if (!(helpEntries[i].type & mask)) continue;
41945ba6
PN
267
268 matchlen = strlen(buf+startpos);
b2cc45bf
PN
269 if (strncasecmp(buf+startpos,helpEntries[i].full,matchlen) == 0) {
270 tmp = sdsnewlen(buf,startpos);
271 tmp = sdscat(tmp,helpEntries[i].full);
41945ba6 272 linenoiseAddCompletion(lc,tmp);
b2cc45bf 273 sdsfree(tmp);
41945ba6
PN
274 }
275 }
a2a69d58
PN
276}
277
3ce014c7 278/*------------------------------------------------------------------------------
279 * Networking / parsing
280 *--------------------------------------------------------------------------- */
281
7fc4ce13
PN
282/* Send AUTH command to the server */
283static int cliAuth() {
284 redisReply *reply;
285 if (config.auth == NULL) return REDIS_OK;
286
287 reply = redisCommand(context,"AUTH %s",config.auth);
288 if (reply != NULL) {
289 freeReplyObject(reply);
290 return REDIS_OK;
291 }
292 return REDIS_ERR;
293}
294
295/* Send SELECT dbnum to the server */
296static int cliSelect() {
297 redisReply *reply;
7fc4ce13
PN
298 if (config.dbnum == 0) return REDIS_OK;
299
96e34b3c 300 reply = redisCommand(context,"SELECT %d",config.dbnum);
7fc4ce13
PN
301 if (reply != NULL) {
302 freeReplyObject(reply);
303 return REDIS_OK;
304 }
305 return REDIS_ERR;
306}
307
c0b3d423 308/* Connect to the client. If force is not zero the connection is performed
309 * even if there is already a connected socket. */
310static int cliConnect(int force) {
7fc4ce13
PN
311 if (context == NULL || force) {
312 if (context != NULL)
313 redisFree(context);
ed9b544e 314
7e91f971 315 if (config.hostsocket == NULL) {
7fc4ce13 316 context = redisConnect(config.hostip,config.hostport);
7e91f971 317 } else {
7fc4ce13 318 context = redisConnectUnix(config.hostsocket);
7e91f971 319 }
7fc4ce13
PN
320
321 if (context->err) {
7e91f971
PN
322 fprintf(stderr,"Could not connect to Redis at ");
323 if (config.hostsocket == NULL)
7fc4ce13 324 fprintf(stderr,"%s:%d: %s\n",config.hostip,config.hostport,context->errstr);
7e91f971 325 else
7fc4ce13
PN
326 fprintf(stderr,"%s: %s\n",config.hostsocket,context->errstr);
327 redisFree(context);
328 context = NULL;
329 return REDIS_ERR;
6fa24622 330 }
ed9b544e 331
7fc4ce13
PN
332 /* Do AUTH and select the right DB. */
333 if (cliAuth() != REDIS_OK)
334 return REDIS_ERR;
335 if (cliSelect() != REDIS_OK)
336 return REDIS_ERR;
ed9b544e 337 }
7fc4ce13 338 return REDIS_OK;
ed9b544e 339}
340
a45f9a1a 341static void cliPrintContextError() {
7fc4ce13
PN
342 if (context == NULL) return;
343 fprintf(stderr,"Error: %s\n",context->errstr);
ed9b544e 344}
345
65add0a3 346static sds cliFormatReplyTTY(redisReply *r, char *prefix) {
7fc4ce13
PN
347 sds out = sdsempty();
348 switch (r->type) {
349 case REDIS_REPLY_ERROR:
65add0a3 350 out = sdscatprintf(out,"(error) %s\n", r->str);
7fc4ce13
PN
351 break;
352 case REDIS_REPLY_STATUS:
7fc4ce13
PN
353 out = sdscat(out,r->str);
354 out = sdscat(out,"\n");
355 break;
356 case REDIS_REPLY_INTEGER:
65add0a3 357 out = sdscatprintf(out,"(integer) %lld\n",r->integer);
7fc4ce13
PN
358 break;
359 case REDIS_REPLY_STRING:
65add0a3
PN
360 /* If you are producing output for the standard output we want
361 * a more interesting output with quoted characters and so forth */
362 out = sdscatrepr(out,r->str,r->len);
363 out = sdscat(out,"\n");
7fc4ce13
PN
364 break;
365 case REDIS_REPLY_NIL:
7fc4ce13
PN
366 out = sdscat(out,"(nil)\n");
367 break;
368 case REDIS_REPLY_ARRAY:
369 if (r->elements == 0) {
7fc4ce13 370 out = sdscat(out,"(empty list or set)\n");
c0b3d423 371 } else {
cfcd5d6d
PN
372 unsigned int i, idxlen = 0;
373 char _prefixlen[16];
374 char _prefixfmt[16];
375 sds _prefix;
7fc4ce13
PN
376 sds tmp;
377
cfcd5d6d
PN
378 /* Calculate chars needed to represent the largest index */
379 i = r->elements;
380 do {
381 idxlen++;
382 i /= 10;
383 } while(i);
384
385 /* Prefix for nested multi bulks should grow with idxlen+2 spaces */
386 memset(_prefixlen,' ',idxlen+2);
387 _prefixlen[idxlen+2] = '\0';
388 _prefix = sdscat(sdsnew(prefix),_prefixlen);
389
390 /* Setup prefix format for every entry */
391 snprintf(_prefixfmt,sizeof(_prefixfmt),"%%s%%%dd) ",idxlen);
392
7fc4ce13 393 for (i = 0; i < r->elements; i++) {
cfcd5d6d
PN
394 /* Don't use the prefix for the first element, as the parent
395 * caller already prepended the index number. */
396 out = sdscatprintf(out,_prefixfmt,i == 0 ? "" : prefix,i+1);
397
398 /* Format the multi bulk entry */
65add0a3 399 tmp = cliFormatReplyTTY(r->element[i],_prefix);
7fc4ce13
PN
400 out = sdscatlen(out,tmp,sdslen(tmp));
401 sdsfree(tmp);
402 }
cfcd5d6d 403 sdsfree(_prefix);
c0b3d423 404 }
7fc4ce13 405 break;
c937aa89 406 default:
7fc4ce13
PN
407 fprintf(stderr,"Unknown reply type: %d\n", r->type);
408 exit(1);
c937aa89 409 }
7fc4ce13 410 return out;
c937aa89 411}
412
65add0a3
PN
413static sds cliFormatReplyRaw(redisReply *r) {
414 sds out = sdsempty(), tmp;
415 size_t i;
416
417 switch (r->type) {
418 case REDIS_REPLY_NIL:
419 /* Nothing... */
ecc91094 420 break;
65add0a3 421 case REDIS_REPLY_ERROR:
ecc91094 422 out = sdscatlen(out,r->str,r->len);
423 out = sdscatlen(out,"\n",1);
424 break;
65add0a3
PN
425 case REDIS_REPLY_STATUS:
426 case REDIS_REPLY_STRING:
427 out = sdscatlen(out,r->str,r->len);
ecc91094 428 break;
65add0a3
PN
429 case REDIS_REPLY_INTEGER:
430 out = sdscatprintf(out,"%lld",r->integer);
ecc91094 431 break;
65add0a3
PN
432 case REDIS_REPLY_ARRAY:
433 for (i = 0; i < r->elements; i++) {
434 if (i > 0) out = sdscat(out,config.mb_delim);
435 tmp = cliFormatReplyRaw(r->element[i]);
436 out = sdscatlen(out,tmp,sdslen(tmp));
437 sdsfree(tmp);
438 }
ecc91094 439 break;
65add0a3
PN
440 default:
441 fprintf(stderr,"Unknown reply type: %d\n", r->type);
442 exit(1);
443 }
444 return out;
445}
446
60893c6c 447static sds cliFormatReplyCSV(redisReply *r) {
448 unsigned int i;
449
450 sds out = sdsempty();
451 switch (r->type) {
452 case REDIS_REPLY_ERROR:
453 out = sdscat(out,"ERROR,");
454 out = sdscatrepr(out,r->str,strlen(r->str));
455 break;
456 case REDIS_REPLY_STATUS:
457 out = sdscatrepr(out,r->str,r->len);
458 break;
459 case REDIS_REPLY_INTEGER:
460 out = sdscatprintf(out,"%lld",r->integer);
461 break;
462 case REDIS_REPLY_STRING:
463 out = sdscatrepr(out,r->str,r->len);
464 break;
465 case REDIS_REPLY_NIL:
466 out = sdscat(out,"NIL\n");
467 break;
468 case REDIS_REPLY_ARRAY:
469 for (i = 0; i < r->elements; i++) {
470 sds tmp = cliFormatReplyCSV(r->element[i]);
471 out = sdscatlen(out,tmp,sdslen(tmp));
472 if (i != r->elements-1) out = sdscat(out,",");
473 sdsfree(tmp);
474 }
475 break;
476 default:
477 fprintf(stderr,"Unknown reply type: %d\n", r->type);
478 exit(1);
479 }
480 return out;
481}
482
65add0a3 483static int cliReadReply(int output_raw_strings) {
8ce39260 484 void *_reply;
7fc4ce13 485 redisReply *reply;
0d44d507 486 sds out = NULL;
623131d4 487 int output = 1;
7fc4ce13 488
8ce39260 489 if (redisGetReply(context,&_reply) != REDIS_OK) {
7fc4ce13
PN
490 if (config.shutdown)
491 return REDIS_OK;
492 if (config.interactive) {
493 /* Filter cases where we should reconnect */
494 if (context->err == REDIS_ERR_IO && errno == ECONNRESET)
495 return REDIS_ERR;
496 if (context->err == REDIS_ERR_EOF)
497 return REDIS_ERR;
498 }
a45f9a1a 499 cliPrintContextError();
500 exit(1);
7fc4ce13 501 return REDIS_ERR; /* avoid compiler warning */
62e920df 502 }
7fc4ce13 503
8ce39260 504 reply = (redisReply*)_reply;
623131d4 505
0d44d507 506 /* Check if we need to connect to a different node and reissue the
507 * request. */
623131d4 508 if (config.cluster_mode && reply->type == REDIS_REPLY_ERROR &&
509 (!strncmp(reply->str,"MOVED",5) || !strcmp(reply->str,"ASK")))
510 {
511 char *p = reply->str, *s;
512 int slot;
513
514 output = 0;
515 /* Comments show the position of the pointer as:
516 *
517 * [S] for pointer 's'
518 * [P] for pointer 'p'
519 */
520 s = strchr(p,' '); /* MOVED[S]3999 127.0.0.1:6381 */
521 p = strchr(s+1,' '); /* MOVED[S]3999[P]127.0.0.1:6381 */
522 *p = '\0';
523 slot = atoi(s+1);
524 s = strchr(p+1,':'); /* MOVED 3999[P]127.0.0.1[S]6381 */
525 *s = '\0';
526 sdsfree(config.hostip);
527 config.hostip = sdsnew(p+1);
528 config.hostport = atoi(s+1);
529 if (config.interactive)
530 printf("-> Redirected to slot [%d] located at %s:%d\n",
531 slot, config.hostip, config.hostport);
532 config.cluster_reissue_command = 1;
533 }
534
535 if (output) {
536 if (output_raw_strings) {
65add0a3 537 out = cliFormatReplyRaw(reply);
65add0a3 538 } else {
60893c6c 539 if (config.output == OUTPUT_RAW) {
623131d4 540 out = cliFormatReplyRaw(reply);
541 out = sdscat(out,"\n");
60893c6c 542 } else if (config.output == OUTPUT_STANDARD) {
623131d4 543 out = cliFormatReplyTTY(reply,"");
60893c6c 544 } else if (config.output == OUTPUT_CSV) {
545 out = cliFormatReplyCSV(reply);
546 out = sdscat(out,"\n");
623131d4 547 }
65add0a3 548 }
623131d4 549 fwrite(out,sdslen(out),1,stdout);
550 sdsfree(out);
65add0a3 551 }
65add0a3 552 freeReplyObject(reply);
7fc4ce13 553 return REDIS_OK;
62e920df 554}
555
aab055ae 556static int cliSendCommand(int argc, char **argv, int repeat) {
37dc9e5a 557 char *command = argv[0];
7fc4ce13 558 size_t *argvlen;
65add0a3 559 int j, output_raw;
ed9b544e 560
4eb3b3e9 561 if (!strcasecmp(command,"help") || !strcasecmp(command,"?")) {
562 cliOutputHelp(--argc, ++argv);
563 return REDIS_OK;
564 }
565
a45f9a1a 566 if (context == NULL) return REDIS_ERR;
efcf948c 567
ecc91094 568 output_raw = 0;
569 if (!strcasecmp(command,"info") ||
570 (argc == 2 && !strcasecmp(command,"cluster") &&
571 (!strcasecmp(argv[1],"nodes") ||
3cd12b56 572 !strcasecmp(argv[1],"info"))) ||
573 (argc == 2 && !strcasecmp(command,"client") &&
574 !strcasecmp(argv[1],"list")))
575
ecc91094 576 {
577 output_raw = 1;
578 }
579
37dc9e5a
PN
580 if (!strcasecmp(command,"shutdown")) config.shutdown = 1;
581 if (!strcasecmp(command,"monitor")) config.monitor_mode = 1;
582 if (!strcasecmp(command,"subscribe") ||
583 !strcasecmp(command,"psubscribe")) config.pubsub_mode = 1;
ed9b544e 584
7fc4ce13
PN
585 /* Setup argument length */
586 argvlen = malloc(argc*sizeof(size_t));
587 for (j = 0; j < argc; j++)
588 argvlen[j] = sdslen(argv[j]);
a2f4f871 589
aab055ae 590 while(repeat--) {
7fc4ce13 591 redisAppendCommandArgv(context,argc,(const char**)argv,argvlen);
249c3a7d 592 while (config.monitor_mode) {
65add0a3 593 if (cliReadReply(output_raw) != REDIS_OK) exit(1);
d9d8ccab 594 fflush(stdout);
621d5c19 595 }
596
249c3a7d 597 if (config.pubsub_mode) {
60893c6c 598 if (config.output != OUTPUT_RAW)
65add0a3 599 printf("Reading messages... (press Ctrl-C to quit)\n");
249c3a7d 600 while (1) {
65add0a3 601 if (cliReadReply(output_raw) != REDIS_OK) exit(1);
249c3a7d 602 }
603 }
604
33753a73
PN
605 if (cliReadReply(output_raw) != REDIS_OK) {
606 free(argvlen);
7fc4ce13 607 return REDIS_ERR;
96e34b3c
PN
608 } else {
609 /* Store database number when SELECT was successfully executed. */
3f4eef21 610 if (!strcasecmp(command,"select") && argc == 2) {
96e34b3c 611 config.dbnum = atoi(argv[1]);
3f4eef21
PN
612 cliRefreshPrompt();
613 }
33753a73 614 }
18f63d8d 615 if (config.interval) usleep(config.interval);
616 fflush(stdout); /* Make it grep friendly */
ed9b544e 617 }
33753a73
PN
618
619 free(argvlen);
7fc4ce13 620 return REDIS_OK;
ed9b544e 621}
622
3ce014c7 623/*------------------------------------------------------------------------------
624 * User interface
625 *--------------------------------------------------------------------------- */
626
ed9b544e 627static int parseOptions(int argc, char **argv) {
628 int i;
629
630 for (i = 1; i < argc; i++) {
631 int lastarg = i==argc-1;
6cf5882c 632
ed9b544e 633 if (!strcmp(argv[i],"-h") && !lastarg) {
efcf948c 634 sdsfree(config.hostip);
e2f31389 635 config.hostip = sdsnew(argv[++i]);
a9158272 636 } else if (!strcmp(argv[i],"-h") && lastarg) {
637 usage();
f18e059e
PN
638 } else if (!strcmp(argv[i],"--help")) {
639 usage();
bc63407b 640 } else if (!strcmp(argv[i],"-x")) {
641 config.stdinarg = 1;
ed9b544e 642 } else if (!strcmp(argv[i],"-p") && !lastarg) {
e2f31389 643 config.hostport = atoi(argv[++i]);
7e91f971 644 } else if (!strcmp(argv[i],"-s") && !lastarg) {
e2f31389 645 config.hostsocket = argv[++i];
5762b7f0 646 } else if (!strcmp(argv[i],"-r") && !lastarg) {
e2f31389 647 config.repeat = strtoll(argv[++i],NULL,10);
18f63d8d 648 } else if (!strcmp(argv[i],"-i") && !lastarg) {
e2f31389 649 double seconds = atof(argv[++i]);
18f63d8d 650 config.interval = seconds*1000000;
62e920df 651 } else if (!strcmp(argv[i],"-n") && !lastarg) {
e2f31389 652 config.dbnum = atoi(argv[++i]);
fdfdae0f 653 } else if (!strcmp(argv[i],"-a") && !lastarg) {
e2f31389 654 config.auth = argv[++i];
65add0a3 655 } else if (!strcmp(argv[i],"--raw")) {
60893c6c 656 config.output = OUTPUT_RAW;
657 } else if (!strcmp(argv[i],"--csv")) {
658 config.output = OUTPUT_CSV;
43071993 659 } else if (!strcmp(argv[i],"--latency")) {
660 config.latency_mode = 1;
b8283ab2 661 } else if (!strcmp(argv[i],"--slave")) {
662 config.slave_mode = 1;
088c508a 663 } else if (!strcmp(argv[i],"--pipe")) {
664 config.pipe_mode = 1;
f26761aa 665 } else if (!strcmp(argv[i],"--bigkeys")) {
666 config.bigkeys = 1;
e2f31389 667 } else if (!strcmp(argv[i],"--eval") && !lastarg) {
668 config.eval = argv[++i];
623131d4 669 } else if (!strcmp(argv[i],"-c")) {
670 config.cluster_mode = 1;
28c07c7b
PN
671 } else if (!strcmp(argv[i],"-d") && !lastarg) {
672 sdsfree(config.mb_delim);
e2f31389 673 config.mb_delim = sdsnew(argv[++i]);
f18e059e
PN
674 } else if (!strcmp(argv[i],"-v") || !strcmp(argv[i], "--version")) {
675 sds version = cliVersion();
676 printf("redis-cli %s\n", version);
677 sdsfree(version);
185cabda 678 exit(0);
ed9b544e 679 } else {
680 break;
681 }
682 }
683 return i;
684}
685
686static sds readArgFromStdin(void) {
687 char buf[1024];
688 sds arg = sdsempty();
689
690 while(1) {
691 int nread = read(fileno(stdin),buf,1024);
692
693 if (nread == 0) break;
694 else if (nread == -1) {
695 perror("Reading from standard input");
696 exit(1);
697 }
698 arg = sdscatlen(arg,buf,nread);
699 }
700 return arg;
701}
702
a9158272 703static void usage() {
f18e059e
PN
704 sds version = cliVersion();
705 fprintf(stderr,
706"redis-cli %s\n"
707"\n"
708"Usage: redis-cli [OPTIONS] [cmd [arg [arg ...]]]\n"
709" -h <hostname> Server hostname (default: 127.0.0.1)\n"
710" -p <port> Server port (default: 6379)\n"
711" -s <socket> Server socket (overrides hostname and port)\n"
712" -a <password> Password to use when connecting to the server\n"
713" -r <repeat> Execute specified command N times\n"
18f63d8d 714" -i <interval> When -r is used, waits <interval> seconds per command.\n"
d665dd08 715" It is possible to specify sub-second times like -i 0.1\n"
f18e059e
PN
716" -n <db> Database number\n"
717" -x Read last argument from STDIN\n"
28c07c7b 718" -d <delimiter> Multi-bulk delimiter in for raw formatting (default: \\n)\n"
623131d4 719" -c Enable cluster mode (follow -ASK and -MOVED redirections)\n"
65add0a3 720" --raw Use raw formatting for replies (default when STDOUT is not a tty)\n"
d665dd08
TN
721" --latency Enter a special mode continuously sampling latency\n"
722" --slave Simulate a slave showing commands received from the master\n"
723" --pipe Transfer raw Redis protocol from stdin to server\n"
724" --bigkeys Sample Redis keys looking for big keys\n"
725" --eval <file> Send an EVAL command using the Lua script at <file>\n"
f18e059e
PN
726" --help Output this help and exit\n"
727" --version Output version and exit\n"
728"\n"
729"Examples:\n"
730" cat /etc/passwd | redis-cli -x set mypasswd\n"
731" redis-cli get mypasswd\n"
732" redis-cli -r 100 lpush mylist x\n"
18f63d8d 733" redis-cli -r 100 -i 1 info | grep used_memory_human:\n"
e2f31389 734" redis-cli --eval myscript.lua key1 key2 , arg1 arg2 arg3\n"
735" (Note: when using --eval the comma separates KEYS[] from ARGV[] items)\n"
f18e059e
PN
736"\n"
737"When no command is given, redis-cli starts in interactive mode.\n"
738"Type \"help\" in interactive mode for information on available commands.\n"
739"\n",
740 version);
741 sdsfree(version);
a9158272 742 exit(1);
743}
744
6cf5882c
MMDJ
745/* Turn the plain C strings into Sds strings */
746static char **convertToSds(int count, char** args) {
747 int j;
37dc9e5a 748 char **sds = zmalloc(sizeof(char*)*count);
6cf5882c
MMDJ
749
750 for(j = 0; j < count; j++)
751 sds[j] = sdsnew(args[j]);
752
753 return sds;
754}
755
a88a2af6 756#define LINE_BUFLEN 4096
6cf5882c 757static void repl() {
ca36b4ab
PN
758 sds historyfile = NULL;
759 int history = 0;
cbce5171 760 char *line;
ca36b4ab 761 int argc;
cbce5171 762 sds *argv;
6cf5882c 763
5d15b520 764 config.interactive = 1;
41945ba6 765 linenoiseSetCompletionCallback(completionCallback);
ce260f73 766
ca36b4ab
PN
767 /* Only use history when stdin is a tty. */
768 if (isatty(fileno(stdin))) {
769 history = 1;
770
771 if (getenv("HOME") != NULL) {
772 historyfile = sdscatprintf(sdsempty(),"%s/.rediscli_history",getenv("HOME"));
773 linenoiseHistoryLoad(historyfile);
774 }
775 }
776
3f4eef21
PN
777 cliRefreshPrompt();
778 while((line = linenoise(context ? config.prompt : "not connected> ")) != NULL) {
cf87ebf2 779 if (line[0] != '\0') {
cbce5171 780 argv = sdssplitargs(line,&argc);
ca36b4ab
PN
781 if (history) linenoiseHistoryAdd(line);
782 if (historyfile) linenoiseHistorySave(historyfile);
783
0439d792
PN
784 if (argv == NULL) {
785 printf("Invalid argument(s)\n");
db6a2e7f 786 free(line);
0439d792
PN
787 continue;
788 } else if (argc > 0) {
a88a2af6 789 if (strcasecmp(argv[0],"quit") == 0 ||
790 strcasecmp(argv[0],"exit") == 0)
c0b3d423 791 {
792 exit(0);
efcf948c 793 } else if (argc == 3 && !strcasecmp(argv[0],"connect")) {
794 sdsfree(config.hostip);
795 config.hostip = sdsnew(argv[1]);
796 config.hostport = atoi(argv[2]);
797 cliConnect(1);
bbac56c2 798 } else if (argc == 1 && !strcasecmp(argv[0],"clear")) {
799 linenoiseClearScreen();
c0b3d423 800 } else {
3ce014c7 801 long long start_time = mstime(), elapsed;
4d19e344 802 int repeat, skipargs = 0;
c0b3d423 803
4d19e344 804 repeat = atoi(argv[0]);
aee7f997 805 if (argc > 1 && repeat) {
4d19e344 806 skipargs = 1;
807 } else {
808 repeat = 1;
809 }
810
623131d4 811 while (1) {
812 config.cluster_reissue_command = 0;
442c748d 813 if (cliSendCommand(argc-skipargs,argv+skipargs,repeat)
814 != REDIS_OK)
623131d4 815 {
816 cliConnect(1);
817
818 /* If we still cannot send the command print error.
819 * We'll try to reconnect the next time. */
820 if (cliSendCommand(argc-skipargs,argv+skipargs,repeat)
821 != REDIS_OK)
822 cliPrintContextError();
823 }
824 /* Issue the command again if we got redirected in cluster mode */
825 if (config.cluster_mode && config.cluster_reissue_command) {
826 cliConnect(1);
827 } else {
828 break;
829 }
c0b3d423 830 }
3ce014c7 831 elapsed = mstime()-start_time;
339b9dc2
PN
832 if (elapsed >= 500) {
833 printf("(%.2fs)\n",(double)elapsed/1000);
834 }
c0b3d423 835 }
a88a2af6 836 }
837 /* Free the argument vector */
ca36b4ab 838 while(argc--) sdsfree(argv[argc]);
8ff6a48b 839 zfree(argv);
6cf5882c 840 }
a88a2af6 841 /* linenoise() returns malloc-ed lines like readline() */
cf87ebf2 842 free(line);
6cf5882c 843 }
6cf5882c
MMDJ
844 exit(0);
845}
846
b4b62c34
PN
847static int noninteractive(int argc, char **argv) {
848 int retval = 0;
bc63407b 849 if (config.stdinarg) {
b4b62c34
PN
850 argv = zrealloc(argv, (argc+1)*sizeof(char*));
851 argv[argc] = readArgFromStdin();
852 retval = cliSendCommand(argc+1, argv, config.repeat);
853 } else {
854 /* stdin is probably a tty, can be tested with S_ISCHR(s.st_mode) */
855 retval = cliSendCommand(argc, argv, config.repeat);
856 }
857 return retval;
858}
859
e2f31389 860static int evalMode(int argc, char **argv) {
861 sds script = sdsempty();
862 FILE *fp;
863 char buf[1024];
864 size_t nread;
865 char **argv2;
866 int j, got_comma = 0, keys = 0;
867
868 /* Load the script from the file, as an sds string. */
869 fp = fopen(config.eval,"r");
870 if (!fp) {
871 fprintf(stderr,
872 "Can't open file '%s': %s\n", config.eval, strerror(errno));
873 exit(1);
874 }
875 while((nread = fread(buf,1,sizeof(buf),fp)) != 0) {
876 script = sdscatlen(script,buf,nread);
877 }
878 fclose(fp);
879
880 /* Create our argument vector */
881 argv2 = zmalloc(sizeof(sds)*(argc+3));
882 argv2[0] = sdsnew("EVAL");
883 argv2[1] = script;
884 for (j = 0; j < argc; j++) {
885 if (!got_comma && argv[j][0] == ',' && argv[j][1] == 0) {
886 got_comma = 1;
887 continue;
888 }
889 argv2[j+3-got_comma] = sdsnew(argv[j]);
890 if (!got_comma) keys++;
891 }
892 argv2[2] = sdscatprintf(sdsempty(),"%d",keys);
893
894 /* Call it */
895 return cliSendCommand(argc+3-got_comma, argv2, config.repeat);
896}
897
43071993 898static void latencyMode(void) {
899 redisReply *reply;
e54fe9a7 900 long long start, latency, min = 0, max = 0, tot = 0, count = 0;
43071993 901 double avg;
902
903 if (!context) exit(1);
904 while(1) {
905 start = mstime();
906 reply = redisCommand(context,"PING");
907 if (reply == NULL) {
908 fprintf(stderr,"\nI/O error\n");
909 exit(1);
910 }
911 latency = mstime()-start;
912 freeReplyObject(reply);
913 count++;
914 if (count == 1) {
915 min = max = tot = latency;
916 avg = (double) latency;
917 } else {
918 if (latency < min) min = latency;
919 if (latency > max) max = latency;
96674b6d 920 tot += latency;
43071993 921 avg = (double) tot/count;
922 }
923 printf("\x1b[0G\x1b[2Kmin: %lld, max: %lld, avg: %.2f (%lld samples)",
924 min, max, avg, count);
925 fflush(stdout);
926 usleep(10000);
927 }
928}
929
b8283ab2 930static void slaveMode(void) {
931 /* To start we need to send the SYNC command and return the payload.
932 * The hiredis client lib does not understand this part of the protocol
933 * and we don't want to mess with its buffers, so everything is performed
934 * using direct low-level I/O. */
935 int fd = context->fd;
936 char buf[1024], *p;
937 ssize_t nread;
938 unsigned long long payload;
939
940 /* Send the SYNC command. */
0d44d507 941 if (write(fd,"SYNC\r\n",6) != 6) {
942 fprintf(stderr,"Error writing to master\n");
943 exit(1);
944 }
b8283ab2 945
946 /* Read $<payload>\r\n, making sure to read just up to "\n" */
947 p = buf;
948 while(1) {
949 nread = read(fd,p,1);
950 if (nread <= 0) {
951 fprintf(stderr,"Error reading bulk length while SYNCing\n");
952 exit(1);
953 }
954 if (*p == '\n') break;
955 p++;
956 }
957 *p = '\0';
958 payload = strtoull(buf+1,NULL,10);
60893c6c 959 fprintf(stderr,"SYNC with master, discarding %lld bytes of bulk tranfer...\n",
b8283ab2 960 payload);
961
962 /* Discard the payload. */
963 while(payload) {
964 nread = read(fd,buf,(payload > sizeof(buf)) ? sizeof(buf) : payload);
965 if (nread <= 0) {
966 fprintf(stderr,"Error reading RDB payload while SYNCing\n");
967 exit(1);
968 }
969 payload -= nread;
970 }
60893c6c 971 fprintf(stderr,"SYNC done. Logging commands from master.\n");
b8283ab2 972
973 /* Now we can use the hiredis to read the incoming protocol. */
60893c6c 974 config.output = OUTPUT_CSV;
975 while (cliReadReply(0) == REDIS_OK);
b8283ab2 976}
977
088c508a 978static void pipeMode(void) {
979 int fd = context->fd;
980 long long errors = 0, replies = 0, obuf_len = 0, obuf_pos = 0;
981 char ibuf[1024*16], obuf[1024*16]; /* Input and output buffers */
982 char aneterr[ANET_ERR_LEN];
983 redisReader *reader = redisReaderCreate();
984 redisReply *reply;
985 int eof = 0; /* True once we consumed all the standard input. */
986 int done = 0;
987 char magic[20]; /* Special reply we recognize. */
988
989 srand(time(NULL));
990
991 /* Use non blocking I/O. */
992 if (anetNonBlock(aneterr,fd) == ANET_ERR) {
993 fprintf(stderr, "Can't set the socket in non blocking mode: %s\n",
994 aneterr);
995 exit(1);
996 }
997
998 /* Transfer raw protocol and read replies from the server at the same
999 * time. */
1000 while(!done) {
1001 int mask = AE_READABLE;
1002
1003 if (!eof || obuf_len != 0) mask |= AE_WRITABLE;
1004 mask = aeWait(fd,mask,1000);
1005
1006 /* Handle the readable state: we can read replies from the server. */
1007 if (mask & AE_READABLE) {
1008 ssize_t nread;
1009
1010 /* Read from socket and feed the hiredis reader. */
1011 do {
1012 nread = read(fd,ibuf,sizeof(ibuf));
ea66be60 1013 if (nread == -1 && errno != EAGAIN && errno != EINTR) {
088c508a 1014 fprintf(stderr, "Error reading from the server: %s\n",
1015 strerror(errno));
1016 exit(1);
1017 }
1018 if (nread > 0) redisReaderFeed(reader,ibuf,nread);
1019 } while(nread > 0);
1020
1021 /* Consume replies. */
1022 do {
1023 if (redisReaderGetReply(reader,(void**)&reply) == REDIS_ERR) {
1024 fprintf(stderr, "Error reading replies from server\n");
1025 exit(1);
1026 }
1027 if (reply) {
1028 if (reply->type == REDIS_REPLY_ERROR) {
1029 fprintf(stderr,"%s\n", reply->str);
1030 errors++;
1031 } else if (eof && reply->type == REDIS_REPLY_STRING &&
1032 reply->len == 20) {
1033 /* Check if this is the reply to our final ECHO
1034 * command. If so everything was received
1035 * from the server. */
1036 if (memcmp(reply->str,magic,20) == 0) {
1037 printf("Last reply received from server.\n");
1038 done = 1;
1039 replies--;
1040 }
1041 }
1042 replies++;
1043 freeReplyObject(reply);
1044 }
1045 } while(reply);
1046 }
1047
1048 /* Handle the writable state: we can send protocol to the server. */
1049 if (mask & AE_WRITABLE) {
1050 while(1) {
1051 /* Transfer current buffer to server. */
1052 if (obuf_len != 0) {
1053 ssize_t nwritten = write(fd,obuf+obuf_pos,obuf_len);
1054
1055 if (nwritten == -1) {
ea66be60 1056 if (errno != EAGAIN && errno != EINTR) {
f6bd9122 1057 fprintf(stderr, "Error writing to the server: %s\n",
1058 strerror(errno));
1059 exit(1);
1060 } else {
1061 nwritten = 0;
1062 }
088c508a 1063 }
1064 obuf_len -= nwritten;
1065 obuf_pos += nwritten;
1066 if (obuf_len != 0) break; /* Can't accept more data. */
1067 }
1068 /* If buffer is empty, load from stdin. */
1069 if (obuf_len == 0 && !eof) {
1070 ssize_t nread = read(STDIN_FILENO,obuf,sizeof(obuf));
1071
1072 if (nread == 0) {
1073 char echo[] =
1074 "*2\r\n$4\r\nECHO\r\n$20\r\n01234567890123456789\r\n";
1075 int j;
1076
1077 eof = 1;
1078 /* Everything transfered, so we queue a special
1079 * ECHO command that we can match in the replies
1080 * to make sure everything was read from the server. */
1081 for (j = 0; j < 20; j++)
1082 magic[j] = rand() & 0xff;
1083 memcpy(echo+19,magic,20);
1084 memcpy(obuf,echo,sizeof(echo)-1);
1085 obuf_len = sizeof(echo)-1;
1086 obuf_pos = 0;
1087 printf("All data transferred. Waiting for the last reply...\n");
1088 } else if (nread == -1) {
1089 fprintf(stderr, "Error reading from stdin: %s\n",
1090 strerror(errno));
1091 exit(1);
1092 } else {
1093 obuf_len = nread;
1094 obuf_pos = 0;
1095 }
1096 }
1097 if (obuf_len == 0 && eof) break;
1098 }
1099 }
1100 }
1101 redisReaderFree(reader);
1102 printf("errors: %lld, replies: %lld\n", errors, replies);
1103 if (errors)
1104 exit(1);
1105 else
1106 exit(0);
1107}
1108
f26761aa 1109#define TYPE_STRING 0
1110#define TYPE_LIST 1
1111#define TYPE_SET 2
1112#define TYPE_HASH 3
1113#define TYPE_ZSET 4
1114
1115static void findBigKeys(void) {
1116 unsigned long long biggest[5] = {0,0,0,0,0};
1117 unsigned long long samples = 0;
e54fe9a7 1118 redisReply *reply1, *reply2, *reply3 = NULL;
f26761aa 1119 char *sizecmd, *typename[] = {"string","list","set","hash","zset"};
1120 int type;
1121
1122 printf("\n# Press ctrl+c when you have had enough of it... :)\n");
1123 printf("# You can use -i 0.1 to sleep 0.1 sec every 100 sampled keys\n");
1124 printf("# in order to reduce server load (usually not needed).\n\n");
1125 while(1) {
1126 /* Sample with RANDOMKEY */
1127 reply1 = redisCommand(context,"RANDOMKEY");
1128 if (reply1 == NULL) {
1129 fprintf(stderr,"\nI/O error\n");
1130 exit(1);
1131 } else if (reply1->type == REDIS_REPLY_ERROR) {
1132 fprintf(stderr, "RANDOMKEY error: %s\n",
1133 reply1->str);
1134 exit(1);
1135 }
1136 /* Get the key type */
1137 reply2 = redisCommand(context,"TYPE %s",reply1->str);
1138 assert(reply2 && reply2->type == REDIS_REPLY_STATUS);
1139 samples++;
1140
1141 /* Get the key "size" */
1142 if (!strcmp(reply2->str,"string")) {
1143 sizecmd = "STRLEN";
1144 type = TYPE_STRING;
1145 } else if (!strcmp(reply2->str,"list")) {
1146 sizecmd = "LLEN";
1147 type = TYPE_LIST;
1148 } else if (!strcmp(reply2->str,"set")) {
1149 sizecmd = "SCARD";
1150 type = TYPE_SET;
1151 } else if (!strcmp(reply2->str,"hash")) {
1152 sizecmd = "HLEN";
1153 type = TYPE_HASH;
1154 } else if (!strcmp(reply2->str,"zset")) {
1155 sizecmd = "ZCARD";
1156 type = TYPE_ZSET;
1157 } else if (!strcmp(reply2->str,"none")) {
1158 freeReplyObject(reply1);
1159 freeReplyObject(reply2);
1160 freeReplyObject(reply3);
1161 continue;
1162 } else {
1163 fprintf(stderr, "Unknown key type '%s' for key '%s'\n",
1164 reply2->str, reply1->str);
1165 exit(1);
1166 }
1167
1168 reply3 = redisCommand(context,"%s %s", sizecmd, reply1->str);
1169 if (reply3 && reply3->type == REDIS_REPLY_INTEGER) {
1170 if (biggest[type] < reply3->integer) {
5386f72d 1171 printf("[%6s] %s | biggest so far with size %llu\n",
f26761aa 1172 typename[type], reply1->str,
1173 (unsigned long long) reply3->integer);
1174 biggest[type] = reply3->integer;
1175 }
1176 }
1177
1178 if ((samples % 1000000) == 0)
1179 printf("(%llu keys sampled)\n", samples);
1180
1181 if ((samples % 100) == 0 && config.interval)
1182 usleep(config.interval);
1183
1184 freeReplyObject(reply1);
1185 freeReplyObject(reply2);
1186 if (reply3) freeReplyObject(reply3);
1187 }
1188}
1189
ed9b544e 1190int main(int argc, char **argv) {
6cf5882c 1191 int firstarg;
ed9b544e 1192
efcf948c 1193 config.hostip = sdsnew("127.0.0.1");
ed9b544e 1194 config.hostport = 6379;
7e91f971 1195 config.hostsocket = NULL;
5762b7f0 1196 config.repeat = 1;
18f63d8d 1197 config.interval = 0;
62e920df 1198 config.dbnum = 0;
5d15b520 1199 config.interactive = 0;
36e5db6d 1200 config.shutdown = 0;
249c3a7d 1201 config.monitor_mode = 0;
1202 config.pubsub_mode = 0;
43071993 1203 config.latency_mode = 0;
623131d4 1204 config.cluster_mode = 0;
f26761aa 1205 config.slave_mode = 0;
088c508a 1206 config.pipe_mode = 0;
f26761aa 1207 config.bigkeys = 0;
bc63407b 1208 config.stdinarg = 0;
288799e0 1209 config.auth = NULL;
e2f31389 1210 config.eval = NULL;
60893c6c 1211 if (!isatty(fileno(stdout)) && (getenv("FAKETTY") == NULL))
1212 config.output = OUTPUT_RAW;
1213 else
1214 config.output = OUTPUT_STANDARD;
65add0a3 1215 config.mb_delim = sdsnew("\n");
41945ba6 1216 cliInitHelp();
99628c1a 1217
ed9b544e 1218 firstarg = parseOptions(argc,argv);
1219 argc -= firstarg;
1220 argv += firstarg;
ed9b544e 1221
088c508a 1222 /* Latency mode */
43071993 1223 if (config.latency_mode) {
1224 cliConnect(0);
1225 latencyMode();
1226 }
1227
088c508a 1228 /* Slave mode */
b8283ab2 1229 if (config.slave_mode) {
1230 cliConnect(0);
1231 slaveMode();
1232 }
1233
088c508a 1234 /* Pipe mode */
1235 if (config.pipe_mode) {
e9828cb6 1236 if (cliConnect(0) == REDIS_ERR) exit(1);
088c508a 1237 pipeMode();
1238 }
1239
f26761aa 1240 /* Find big keys */
1241 if (config.bigkeys) {
1242 cliConnect(0);
1243 findBigKeys();
1244 }
1245
abb731e5 1246 /* Start interactive mode when no command is provided */
e2f31389 1247 if (argc == 0 && !config.eval) {
a45f9a1a 1248 /* Note that in repl mode we don't abort on connection error.
1249 * A new attempt will be performed for every command send. */
1250 cliConnect(0);
1251 repl();
1252 }
1253
b4b62c34 1254 /* Otherwise, we have some arguments to execute */
a45f9a1a 1255 if (cliConnect(0) != REDIS_OK) exit(1);
e2f31389 1256 if (config.eval) {
1257 return evalMode(argc,argv);
1258 } else {
1259 return noninteractive(argc,convertToSds(argc,argv));
1260 }
ed9b544e 1261}