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