]> git.saurik.com Git - redis.git/blame - redis-cli.c
redis-cli now is able to also output the string representation instead of the raw...
[redis.git] / 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"
32
ed9b544e 33#include <stdio.h>
34#include <string.h>
35#include <stdlib.h>
36#include <unistd.h>
a88a2af6 37#include <ctype.h>
ed9b544e 38
39#include "anet.h"
40#include "sds.h"
41#include "adlist.h"
42#include "zmalloc.h"
cf87ebf2 43#include "linenoise.h"
ed9b544e 44
45#define REDIS_CMD_INLINE 1
46#define REDIS_CMD_BULK 2
e17e0b05 47#define REDIS_CMD_MULTIBULK 4
ed9b544e 48
49#define REDIS_NOTUSED(V) ((void) V)
50
51static struct config {
52 char *hostip;
53 int hostport;
5762b7f0 54 long repeat;
62e920df 55 int dbnum;
6cf5882c 56 int interactive;
288799e0 57 char *auth;
ed9b544e 58} config;
59
60struct redisCommand {
61 char *name;
62 int arity;
63 int flags;
64};
65
66static struct redisCommand cmdTable[] = {
fdfdae0f 67 {"auth",2,REDIS_CMD_INLINE},
c937aa89 68 {"get",2,REDIS_CMD_INLINE},
69 {"set",3,REDIS_CMD_BULK},
70 {"setnx",3,REDIS_CMD_BULK},
526d00a5 71 {"setex",4,REDIS_CMD_BULK},
4b00bebd 72 {"append",3,REDIS_CMD_BULK},
39191553 73 {"substr",4,REDIS_CMD_INLINE},
5109cdff 74 {"del",-2,REDIS_CMD_INLINE},
c937aa89 75 {"exists",2,REDIS_CMD_INLINE},
76 {"incr",2,REDIS_CMD_INLINE},
77 {"decr",2,REDIS_CMD_INLINE},
78 {"rpush",3,REDIS_CMD_BULK},
79 {"lpush",3,REDIS_CMD_BULK},
80 {"rpop",2,REDIS_CMD_INLINE},
81 {"lpop",2,REDIS_CMD_INLINE},
b177fd30 82 {"brpop",-3,REDIS_CMD_INLINE},
83 {"blpop",-3,REDIS_CMD_INLINE},
c937aa89 84 {"llen",2,REDIS_CMD_INLINE},
85 {"lindex",3,REDIS_CMD_INLINE},
86 {"lset",4,REDIS_CMD_BULK},
87 {"lrange",4,REDIS_CMD_INLINE},
88 {"ltrim",4,REDIS_CMD_INLINE},
89 {"lrem",4,REDIS_CMD_BULK},
0f5f7e9a 90 {"rpoplpush",3,REDIS_CMD_BULK},
c937aa89 91 {"sadd",3,REDIS_CMD_BULK},
92 {"srem",3,REDIS_CMD_BULK},
a4460ef4 93 {"smove",4,REDIS_CMD_BULK},
c937aa89 94 {"sismember",3,REDIS_CMD_BULK},
95 {"scard",2,REDIS_CMD_INLINE},
12fea928 96 {"spop",2,REDIS_CMD_INLINE},
2abb95a9 97 {"srandmember",2,REDIS_CMD_INLINE},
c937aa89 98 {"sinter",-2,REDIS_CMD_INLINE},
99 {"sinterstore",-3,REDIS_CMD_INLINE},
40d224a9 100 {"sunion",-2,REDIS_CMD_INLINE},
101 {"sunionstore",-3,REDIS_CMD_INLINE},
f4f56e1d 102 {"sdiff",-2,REDIS_CMD_INLINE},
103 {"sdiffstore",-3,REDIS_CMD_INLINE},
c937aa89 104 {"smembers",2,REDIS_CMD_INLINE},
fd8ccf44 105 {"zadd",4,REDIS_CMD_BULK},
7db723ad 106 {"zincrby",4,REDIS_CMD_BULK},
1b7106e7 107 {"zrem",3,REDIS_CMD_BULK},
1807985b 108 {"zremrangebyscore",4,REDIS_CMD_INLINE},
b287c9bb
PN
109 {"zmerge",-3,REDIS_CMD_INLINE},
110 {"zmergeweighed",-4,REDIS_CMD_INLINE},
752da584 111 {"zrange",-4,REDIS_CMD_INLINE},
3589e1a7 112 {"zrank",3,REDIS_CMD_BULK},
23d82148 113 {"zrevrank",3,REDIS_CMD_BULK},
80181f78 114 {"zrangebyscore",-4,REDIS_CMD_INLINE},
f44dd428 115 {"zcount",4,REDIS_CMD_INLINE},
752da584 116 {"zrevrange",-4,REDIS_CMD_INLINE},
3c41331e 117 {"zcard",2,REDIS_CMD_INLINE},
6e333bbe 118 {"zscore",3,REDIS_CMD_BULK},
c937aa89 119 {"incrby",3,REDIS_CMD_INLINE},
120 {"decrby",3,REDIS_CMD_INLINE},
a431eb74 121 {"getset",3,REDIS_CMD_BULK},
c937aa89 122 {"randomkey",1,REDIS_CMD_INLINE},
123 {"select",2,REDIS_CMD_INLINE},
124 {"move",3,REDIS_CMD_INLINE},
125 {"rename",3,REDIS_CMD_INLINE},
126 {"renamenx",3,REDIS_CMD_INLINE},
127 {"keys",2,REDIS_CMD_INLINE},
128 {"dbsize",1,REDIS_CMD_INLINE},
129 {"ping",1,REDIS_CMD_INLINE},
130 {"echo",2,REDIS_CMD_BULK},
131 {"save",1,REDIS_CMD_INLINE},
132 {"bgsave",1,REDIS_CMD_INLINE},
9d65a1bb 133 {"rewriteaof",1,REDIS_CMD_INLINE},
134 {"bgrewriteaof",1,REDIS_CMD_INLINE},
c937aa89 135 {"shutdown",1,REDIS_CMD_INLINE},
136 {"lastsave",1,REDIS_CMD_INLINE},
137 {"type",2,REDIS_CMD_INLINE},
138 {"flushdb",1,REDIS_CMD_INLINE},
139 {"flushall",1,REDIS_CMD_INLINE},
140 {"sort",-2,REDIS_CMD_INLINE},
141 {"info",1,REDIS_CMD_INLINE},
142 {"mget",-2,REDIS_CMD_INLINE},
3305306f 143 {"expire",3,REDIS_CMD_INLINE},
802e8373 144 {"expireat",3,REDIS_CMD_INLINE},
fd88489a 145 {"ttl",2,REDIS_CMD_INLINE},
321b0e13 146 {"slaveof",3,REDIS_CMD_INLINE},
333298da 147 {"debug",-2,REDIS_CMD_INLINE},
8165a5f2 148 {"mset",-3,REDIS_CMD_MULTIBULK},
149 {"msetnx",-3,REDIS_CMD_MULTIBULK},
621d5c19 150 {"monitor",1,REDIS_CMD_INLINE},
6fa24622 151 {"multi",1,REDIS_CMD_INLINE},
978c2c94 152 {"exec",1,REDIS_CMD_INLINE},
6fa24622 153 {"discard",1,REDIS_CMD_INLINE},
978c2c94 154 {"hset",4,REDIS_CMD_MULTIBULK},
09aeb579 155 {"hget",3,REDIS_CMD_BULK},
d33278d1 156 {"hmset",-4,REDIS_CMD_MULTIBULK},
09aeb579 157 {"hmget",-3,REDIS_CMD_MULTIBULK},
01426b05 158 {"hincrby",4,REDIS_CMD_INLINE},
07efaf74 159 {"hdel",3,REDIS_CMD_BULK},
92b27fe9 160 {"hlen",2,REDIS_CMD_INLINE},
78409a0f 161 {"hkeys",2,REDIS_CMD_INLINE},
162 {"hvals",2,REDIS_CMD_INLINE},
163 {"hgetall",2,REDIS_CMD_INLINE},
a86f14b1 164 {"hexists",3,REDIS_CMD_BULK},
500ece7c 165 {"config",-2,REDIS_CMD_BULK},
befec3cd 166 {"subscribe",-2,REDIS_CMD_INLINE},
167 {"unsubscribe",-1,REDIS_CMD_INLINE},
168 {"publish",3,REDIS_CMD_BULK},
ed9b544e 169 {NULL,0,0}
170};
171
c937aa89 172static int cliReadReply(int fd);
a9158272 173static void usage();
c937aa89 174
ed9b544e 175static struct redisCommand *lookupCommand(char *name) {
176 int j = 0;
177 while(cmdTable[j].name != NULL) {
178 if (!strcasecmp(name,cmdTable[j].name)) return &cmdTable[j];
179 j++;
180 }
181 return NULL;
182}
183
184static int cliConnect(void) {
185 char err[ANET_ERR_LEN];
6fa24622 186 static int fd = ANET_ERR;
ed9b544e 187
ed9b544e 188 if (fd == ANET_ERR) {
6fa24622
DJMM
189 fd = anetTcpConnect(err,config.hostip,config.hostport);
190 if (fd == ANET_ERR) {
191 fprintf(stderr, "Could not connect to Redis at %s:%d: %s", config.hostip, config.hostport, err);
192 return -1;
193 }
194 anetTcpNoDelay(NULL,fd);
ed9b544e 195 }
ed9b544e 196 return fd;
197}
198
199static sds cliReadLine(int fd) {
200 sds line = sdsempty();
201
202 while(1) {
203 char c;
b91f03a4 204 ssize_t ret;
ed9b544e 205
b91f03a4
LH
206 ret = read(fd,&c,1);
207 if (ret == -1) {
ed9b544e 208 sdsfree(line);
209 return NULL;
b91f03a4 210 } else if ((ret == 0) || (c == '\n')) {
ed9b544e 211 break;
212 } else {
213 line = sdscatlen(line,&c,1);
214 }
215 }
216 return sdstrim(line,"\r\n");
217}
218
62e920df 219static int cliReadSingleLineReply(int fd, int quiet) {
ed9b544e 220 sds reply = cliReadLine(fd);
221
222 if (reply == NULL) return 1;
62e920df 223 if (!quiet)
224 printf("%s\n", reply);
621d5c19 225 sdsfree(reply);
ed9b544e 226 return 0;
227}
228
21cdc9f0 229static void printStringRepr(char *s, int len) {
230 printf("\"");
231 while(len--) {
232 switch(*s) {
233 case '\\':
234 case '"':
235 printf("\\%c",*s);
236 break;
237 case '\n': printf("\\n"); break;
238 case '\r': printf("\\r"); break;
239 case '\t': printf("\\t"); break;
240 case '\a': printf("\\a"); break;
241 case '\b': printf("\\b"); break;
242 default:
243 if (isprint(*s))
244 printf("%c",*s);
245 else
246 printf("\\x%02x",(unsigned char)*s);
247 break;
248 }
249 s++;
250 }
251 printf("\"\n");
252}
253
c937aa89 254static int cliReadBulkReply(int fd) {
ed9b544e 255 sds replylen = cliReadLine(fd);
256 char *reply, crlf[2];
c937aa89 257 int bulklen;
ed9b544e 258
259 if (replylen == NULL) return 1;
ed9b544e 260 bulklen = atoi(replylen);
c937aa89 261 if (bulklen == -1) {
ed9b544e 262 sdsfree(replylen);
060f6be6 263 printf("(nil)\n");
ed9b544e 264 return 0;
265 }
ed9b544e 266 reply = zmalloc(bulklen);
267 anetRead(fd,reply,bulklen);
268 anetRead(fd,crlf,2);
21cdc9f0 269 if (!isatty(fileno(stdout))) {
270 if (bulklen && fwrite(reply,bulklen,1,stdout) == 0) {
271 zfree(reply);
272 return 1;
273 }
274 if (reply[bulklen-1] != '\n') printf("\n");
275 } else {
276 /* If you are producing output for the standard output we want
277 * a more interesting output with quoted characters and so forth */
278 printStringRepr(reply,bulklen);
ed9b544e 279 }
ed9b544e 280 zfree(reply);
c937aa89 281 return 0;
ed9b544e 282}
283
284static int cliReadMultiBulkReply(int fd) {
285 sds replylen = cliReadLine(fd);
286 int elements, c = 1;
287
288 if (replylen == NULL) return 1;
c937aa89 289 elements = atoi(replylen);
290 if (elements == -1) {
ed9b544e 291 sdsfree(replylen);
292 printf("(nil)\n");
293 return 0;
294 }
c937aa89 295 if (elements == 0) {
296 printf("(empty list or set)\n");
297 }
ed9b544e 298 while(elements--) {
299 printf("%d. ", c);
c937aa89 300 if (cliReadReply(fd)) return 1;
ed9b544e 301 c++;
302 }
303 return 0;
304}
305
c937aa89 306static int cliReadReply(int fd) {
307 char type;
308
309 if (anetRead(fd,&type,1) <= 0) exit(1);
310 switch(type) {
311 case '-':
312 printf("(error) ");
62e920df 313 cliReadSingleLineReply(fd,0);
c937aa89 314 return 1;
315 case '+':
62e920df 316 return cliReadSingleLineReply(fd,0);
c937aa89 317 case ':':
443c6409 318 printf("(integer) ");
62e920df 319 return cliReadSingleLineReply(fd,0);
c937aa89 320 case '$':
321 return cliReadBulkReply(fd);
322 case '*':
323 return cliReadMultiBulkReply(fd);
324 default:
325 printf("protocol error, got '%c' as reply type byte\n", type);
326 return 1;
327 }
328}
329
6cf5882c 330static int selectDb(int fd) {
62e920df 331 int retval;
332 sds cmd;
333 char type;
334
335 if (config.dbnum == 0)
336 return 0;
337
338 cmd = sdsempty();
339 cmd = sdscatprintf(cmd,"SELECT %d\r\n",config.dbnum);
340 anetWrite(fd,cmd,sdslen(cmd));
341 anetRead(fd,&type,1);
342 if (type <= 0 || type != '+') return 1;
343 retval = cliReadSingleLineReply(fd,1);
344 if (retval) {
6cf5882c 345 return retval;
62e920df 346 }
347 return 0;
348}
349
aab055ae 350static int cliSendCommand(int argc, char **argv, int repeat) {
ed9b544e 351 struct redisCommand *rc = lookupCommand(argv[0]);
352 int fd, j, retval = 0;
621d5c19 353 int read_forever = 0;
5762b7f0 354 sds cmd;
ed9b544e 355
356 if (!rc) {
357 fprintf(stderr,"Unknown command '%s'\n",argv[0]);
358 return 1;
359 }
360
361 if ((rc->arity > 0 && argc != rc->arity) ||
a74f2af6 362 (rc->arity < 0 && argc < -rc->arity)) {
ed9b544e 363 fprintf(stderr,"Wrong number of arguments for '%s'\n",rc->name);
364 return 1;
365 }
621d5c19 366 if (!strcasecmp(rc->name,"monitor")) read_forever = 1;
ed9b544e 367 if ((fd = cliConnect()) == -1) return 1;
368
62e920df 369 /* Select db number */
370 retval = selectDb(fd);
371 if (retval) {
372 fprintf(stderr,"Error setting DB num\n");
373 return 1;
374 }
6cf5882c 375
aab055ae 376 while(repeat--) {
5762b7f0 377 /* Build the command to send */
378 cmd = sdsempty();
379 if (rc->flags & REDIS_CMD_MULTIBULK) {
380 cmd = sdscatprintf(cmd,"*%d\r\n",argc);
381 for (j = 0; j < argc; j++) {
83c6a618 382 cmd = sdscatprintf(cmd,"$%lu\r\n",
383 (unsigned long)sdslen(argv[j]));
8165a5f2 384 cmd = sdscatlen(cmd,argv[j],sdslen(argv[j]));
5762b7f0 385 cmd = sdscatlen(cmd,"\r\n",2);
386 }
387 } else {
388 for (j = 0; j < argc; j++) {
389 if (j != 0) cmd = sdscat(cmd," ");
390 if (j == argc-1 && rc->flags & REDIS_CMD_BULK) {
83c6a618 391 cmd = sdscatprintf(cmd,"%lu",
392 (unsigned long)sdslen(argv[j]));
5762b7f0 393 } else {
394 cmd = sdscatlen(cmd,argv[j],sdslen(argv[j]));
395 }
396 }
397 cmd = sdscat(cmd,"\r\n");
398 if (rc->flags & REDIS_CMD_BULK) {
399 cmd = sdscatlen(cmd,argv[argc-1],sdslen(argv[argc-1]));
400 cmd = sdscatlen(cmd,"\r\n",2);
8165a5f2 401 }
ed9b544e 402 }
5762b7f0 403 anetWrite(fd,cmd,sdslen(cmd));
404 sdsfree(cmd);
621d5c19 405
406 while (read_forever) {
407 cliReadSingleLineReply(fd,0);
408 }
409
5762b7f0 410 retval = cliReadReply(fd);
411 if (retval) {
5762b7f0 412 return retval;
8165a5f2 413 }
ed9b544e 414 }
ed9b544e 415 return 0;
416}
417
418static int parseOptions(int argc, char **argv) {
419 int i;
420
421 for (i = 1; i < argc; i++) {
422 int lastarg = i==argc-1;
6cf5882c 423
ed9b544e 424 if (!strcmp(argv[i],"-h") && !lastarg) {
425 char *ip = zmalloc(32);
426 if (anetResolve(NULL,argv[i+1],ip) == ANET_ERR) {
427 printf("Can't resolve %s\n", argv[i]);
428 exit(1);
429 }
430 config.hostip = ip;
431 i++;
a9158272 432 } else if (!strcmp(argv[i],"-h") && lastarg) {
433 usage();
ed9b544e 434 } else if (!strcmp(argv[i],"-p") && !lastarg) {
435 config.hostport = atoi(argv[i+1]);
436 i++;
5762b7f0 437 } else if (!strcmp(argv[i],"-r") && !lastarg) {
438 config.repeat = strtoll(argv[i+1],NULL,10);
439 i++;
62e920df 440 } else if (!strcmp(argv[i],"-n") && !lastarg) {
441 config.dbnum = atoi(argv[i+1]);
442 i++;
fdfdae0f 443 } else if (!strcmp(argv[i],"-a") && !lastarg) {
288799e0 444 config.auth = argv[i+1];
fdfdae0f 445 i++;
6cf5882c
MMDJ
446 } else if (!strcmp(argv[i],"-i")) {
447 config.interactive = 1;
ed9b544e 448 } else {
449 break;
450 }
451 }
452 return i;
453}
454
455static sds readArgFromStdin(void) {
456 char buf[1024];
457 sds arg = sdsempty();
458
459 while(1) {
460 int nread = read(fileno(stdin),buf,1024);
461
462 if (nread == 0) break;
463 else if (nread == -1) {
464 perror("Reading from standard input");
465 exit(1);
466 }
467 arg = sdscatlen(arg,buf,nread);
468 }
469 return arg;
470}
471
a9158272 472static void usage() {
fdfdae0f 473 fprintf(stderr, "usage: redis-cli [-h host] [-p port] [-a authpw] [-r repeat_times] [-n db_num] [-i] cmd arg1 arg2 arg3 ... argN\n");
474 fprintf(stderr, "usage: echo \"argN\" | redis-cli [-h host] [-a authpw] [-p port] [-r repeat_times] [-n db_num] cmd arg1 arg2 ... arg(N-1)\n");
a9158272 475 fprintf(stderr, "\nIf a pipe from standard input is detected this data is used as last argument.\n\n");
476 fprintf(stderr, "example: cat /etc/passwd | redis-cli set my_passwd\n");
477 fprintf(stderr, "example: redis-cli get my_passwd\n");
478 fprintf(stderr, "example: redis-cli -r 100 lpush mylist x\n");
d239ec59 479 fprintf(stderr, "\nRun in interactive mode: redis-cli -i or just don't pass any command\n");
a9158272 480 exit(1);
481}
482
6cf5882c
MMDJ
483/* Turn the plain C strings into Sds strings */
484static char **convertToSds(int count, char** args) {
485 int j;
486 char **sds = zmalloc(sizeof(char*)*count+1);
487
488 for(j = 0; j < count; j++)
489 sds[j] = sdsnew(args[j]);
490
491 return sds;
492}
493
a88a2af6 494static char **splitArguments(char *line, int *argc) {
495 char *p = line;
496 char *current = NULL;
497 char **vector = NULL;
498
499 *argc = 0;
500 while(1) {
501 /* skip blanks */
502 while(*p && isspace(*p)) p++;
503 if (*p) {
504 /* get a token */
505 int inq=0; /* set to 1 if we are in "quotes" */
506 int done = 0;
507
508 if (current == NULL) current = sdsempty();
509 while(!done) {
510 if (inq) {
511 if (*p == '\\' && *(p+1)) {
512 char c;
513
514 p++;
515 switch(*p) {
516 case 'n': c = '\n'; break;
517 case 'r': c = '\r'; break;
518 case 't': c = '\t'; break;
519 case 'b': c = '\b'; break;
520 case 'a': c = '\a'; break;
521 default: c = *p; break;
522 }
523 current = sdscatlen(current,&c,1);
524 } else if (*p == '"') {
525 done = 1;
526 } else {
527 current = sdscatlen(current,p,1);
528 }
529 } else {
530 switch(*p) {
531 case ' ':
532 case '\n':
533 case '\r':
534 case '\t':
535 case '\0':
536 done=1;
537 break;
538 case '"':
539 inq=1;
540 break;
541 default:
542 current = sdscatlen(current,p,1);
543 break;
544 }
545 }
546 if (*p) p++;
547 }
548 /* add the token to the vector */
549 vector = zrealloc(vector,((*argc)+1)*sizeof(char*));
550 vector[*argc] = current;
551 (*argc)++;
552 current = NULL;
553 } else {
554 return vector;
555 }
556 }
557}
558
559#define LINE_BUFLEN 4096
6cf5882c 560static void repl() {
a88a2af6 561 int argc, j;
562 char *line, **argv;
6cf5882c 563
bc86d88e 564 while((line = linenoise("redis> ")) != NULL) {
cf87ebf2 565 if (line[0] != '\0') {
a88a2af6 566 argv = splitArguments(line,&argc);
567 linenoiseHistoryAdd(line);
568 if (argc > 0) {
569 if (strcasecmp(argv[0],"quit") == 0 ||
570 strcasecmp(argv[0],"exit") == 0)
571 exit(0);
572 else
573 cliSendCommand(argc, argv, 1);
574 }
575 /* Free the argument vector */
576 for (j = 0; j < argc; j++)
577 sdsfree(argv[j]);
578 free(argv);
6cf5882c 579 }
a88a2af6 580 /* linenoise() returns malloc-ed lines like readline() */
cf87ebf2 581 free(line);
6cf5882c 582 }
6cf5882c
MMDJ
583 exit(0);
584}
585
ed9b544e 586int main(int argc, char **argv) {
6cf5882c 587 int firstarg;
ed9b544e 588 char **argvcopy;
2073a849 589 struct redisCommand *rc;
ed9b544e 590
591 config.hostip = "127.0.0.1";
592 config.hostport = 6379;
5762b7f0 593 config.repeat = 1;
62e920df 594 config.dbnum = 0;
6cf5882c 595 config.interactive = 0;
288799e0 596 config.auth = NULL;
ed9b544e 597
598 firstarg = parseOptions(argc,argv);
599 argc -= firstarg;
600 argv += firstarg;
ed9b544e 601
aab055ae
MMDJ
602 if (config.auth != NULL) {
603 char *authargv[2];
604
605 authargv[0] = "AUTH";
606 authargv[1] = config.auth;
607 cliSendCommand(2, convertToSds(2, authargv), 1);
608 }
609
d239ec59 610 if (argc == 0 || config.interactive == 1) repl();
ed9b544e 611
6cf5882c
MMDJ
612 argvcopy = convertToSds(argc, argv);
613
2073a849 614 /* Read the last argument from stdandard input if needed */
615 if ((rc = lookupCommand(argv[0])) != NULL) {
6cf5882c
MMDJ
616 if (rc->arity > 0 && argc == rc->arity-1) {
617 sds lastarg = readArgFromStdin();
618 argvcopy[argc] = lastarg;
619 argc++;
620 }
2073a849 621 }
622
aab055ae 623 return cliSendCommand(argc, argvcopy, config.repeat);
ed9b544e 624}