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