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