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