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