]>
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}, | |
87c68815 | 178 | {"watch",-2,CMDFLAG_NONE}, |
179 | {"unwatch",1,CMDFLAG_NONE}, | |
f40b035d | 180 | {NULL,0,CMDFLAG_NONE} |
ed9b544e | 181 | }; |
182 | ||
c937aa89 | 183 | static int cliReadReply(int fd); |
a9158272 | 184 | static void usage(); |
c937aa89 | 185 | |
ed9b544e | 186 | static struct redisCommand *lookupCommand(char *name) { |
187 | int j = 0; | |
188 | while(cmdTable[j].name != NULL) { | |
189 | if (!strcasecmp(name,cmdTable[j].name)) return &cmdTable[j]; | |
190 | j++; | |
191 | } | |
192 | return NULL; | |
193 | } | |
194 | ||
195 | static int cliConnect(void) { | |
196 | char err[ANET_ERR_LEN]; | |
6fa24622 | 197 | static int fd = ANET_ERR; |
ed9b544e | 198 | |
ed9b544e | 199 | if (fd == ANET_ERR) { |
6fa24622 DJMM |
200 | fd = anetTcpConnect(err,config.hostip,config.hostport); |
201 | if (fd == ANET_ERR) { | |
202 | fprintf(stderr, "Could not connect to Redis at %s:%d: %s", config.hostip, config.hostport, err); | |
203 | return -1; | |
204 | } | |
205 | anetTcpNoDelay(NULL,fd); | |
ed9b544e | 206 | } |
ed9b544e | 207 | return fd; |
208 | } | |
209 | ||
210 | static sds cliReadLine(int fd) { | |
211 | sds line = sdsempty(); | |
212 | ||
213 | while(1) { | |
214 | char c; | |
b91f03a4 | 215 | ssize_t ret; |
ed9b544e | 216 | |
b91f03a4 LH |
217 | ret = read(fd,&c,1); |
218 | if (ret == -1) { | |
ed9b544e | 219 | sdsfree(line); |
220 | return NULL; | |
b91f03a4 | 221 | } else if ((ret == 0) || (c == '\n')) { |
ed9b544e | 222 | break; |
223 | } else { | |
224 | line = sdscatlen(line,&c,1); | |
225 | } | |
226 | } | |
227 | return sdstrim(line,"\r\n"); | |
228 | } | |
229 | ||
62e920df | 230 | static int cliReadSingleLineReply(int fd, int quiet) { |
ed9b544e | 231 | sds reply = cliReadLine(fd); |
232 | ||
233 | if (reply == NULL) return 1; | |
62e920df | 234 | if (!quiet) |
235 | printf("%s\n", reply); | |
621d5c19 | 236 | sdsfree(reply); |
ed9b544e | 237 | return 0; |
238 | } | |
239 | ||
21cdc9f0 | 240 | static void printStringRepr(char *s, int len) { |
241 | printf("\""); | |
242 | while(len--) { | |
243 | switch(*s) { | |
244 | case '\\': | |
245 | case '"': | |
246 | printf("\\%c",*s); | |
247 | break; | |
248 | case '\n': printf("\\n"); break; | |
249 | case '\r': printf("\\r"); break; | |
250 | case '\t': printf("\\t"); break; | |
251 | case '\a': printf("\\a"); break; | |
252 | case '\b': printf("\\b"); break; | |
253 | default: | |
254 | if (isprint(*s)) | |
255 | printf("%c",*s); | |
256 | else | |
257 | printf("\\x%02x",(unsigned char)*s); | |
258 | break; | |
259 | } | |
260 | s++; | |
261 | } | |
262 | printf("\"\n"); | |
263 | } | |
264 | ||
c937aa89 | 265 | static int cliReadBulkReply(int fd) { |
ed9b544e | 266 | sds replylen = cliReadLine(fd); |
267 | char *reply, crlf[2]; | |
c937aa89 | 268 | int bulklen; |
ed9b544e | 269 | |
270 | if (replylen == NULL) return 1; | |
ed9b544e | 271 | bulklen = atoi(replylen); |
c937aa89 | 272 | if (bulklen == -1) { |
ed9b544e | 273 | sdsfree(replylen); |
060f6be6 | 274 | printf("(nil)\n"); |
ed9b544e | 275 | return 0; |
276 | } | |
ed9b544e | 277 | reply = zmalloc(bulklen); |
278 | anetRead(fd,reply,bulklen); | |
279 | anetRead(fd,crlf,2); | |
f40b035d | 280 | if (config.raw_output || !isatty(fileno(stdout))) { |
21cdc9f0 | 281 | if (bulklen && fwrite(reply,bulklen,1,stdout) == 0) { |
282 | zfree(reply); | |
283 | return 1; | |
284 | } | |
21cdc9f0 | 285 | } else { |
286 | /* If you are producing output for the standard output we want | |
287 | * a more interesting output with quoted characters and so forth */ | |
288 | printStringRepr(reply,bulklen); | |
ed9b544e | 289 | } |
ed9b544e | 290 | zfree(reply); |
c937aa89 | 291 | return 0; |
ed9b544e | 292 | } |
293 | ||
294 | static int cliReadMultiBulkReply(int fd) { | |
295 | sds replylen = cliReadLine(fd); | |
296 | int elements, c = 1; | |
297 | ||
298 | if (replylen == NULL) return 1; | |
c937aa89 | 299 | elements = atoi(replylen); |
300 | if (elements == -1) { | |
ed9b544e | 301 | sdsfree(replylen); |
302 | printf("(nil)\n"); | |
303 | return 0; | |
304 | } | |
c937aa89 | 305 | if (elements == 0) { |
306 | printf("(empty list or set)\n"); | |
307 | } | |
ed9b544e | 308 | while(elements--) { |
309 | printf("%d. ", c); | |
c937aa89 | 310 | if (cliReadReply(fd)) return 1; |
ed9b544e | 311 | c++; |
312 | } | |
313 | return 0; | |
314 | } | |
315 | ||
c937aa89 | 316 | static int cliReadReply(int fd) { |
317 | char type; | |
318 | ||
36e5db6d BD |
319 | if (anetRead(fd,&type,1) <= 0) { |
320 | if (config.shutdown) return 0; | |
321 | exit(1); | |
322 | } | |
c937aa89 | 323 | switch(type) { |
324 | case '-': | |
325 | printf("(error) "); | |
62e920df | 326 | cliReadSingleLineReply(fd,0); |
c937aa89 | 327 | return 1; |
328 | case '+': | |
62e920df | 329 | return cliReadSingleLineReply(fd,0); |
c937aa89 | 330 | case ':': |
443c6409 | 331 | printf("(integer) "); |
62e920df | 332 | return cliReadSingleLineReply(fd,0); |
c937aa89 | 333 | case '$': |
334 | return cliReadBulkReply(fd); | |
335 | case '*': | |
336 | return cliReadMultiBulkReply(fd); | |
337 | default: | |
338 | printf("protocol error, got '%c' as reply type byte\n", type); | |
339 | return 1; | |
340 | } | |
341 | } | |
342 | ||
6cf5882c | 343 | static int selectDb(int fd) { |
62e920df | 344 | int retval; |
345 | sds cmd; | |
346 | char type; | |
347 | ||
348 | if (config.dbnum == 0) | |
349 | return 0; | |
350 | ||
351 | cmd = sdsempty(); | |
352 | cmd = sdscatprintf(cmd,"SELECT %d\r\n",config.dbnum); | |
353 | anetWrite(fd,cmd,sdslen(cmd)); | |
354 | anetRead(fd,&type,1); | |
355 | if (type <= 0 || type != '+') return 1; | |
356 | retval = cliReadSingleLineReply(fd,1); | |
357 | if (retval) { | |
6cf5882c | 358 | return retval; |
62e920df | 359 | } |
360 | return 0; | |
361 | } | |
362 | ||
aab055ae | 363 | static int cliSendCommand(int argc, char **argv, int repeat) { |
ed9b544e | 364 | struct redisCommand *rc = lookupCommand(argv[0]); |
365 | int fd, j, retval = 0; | |
5762b7f0 | 366 | sds cmd; |
ed9b544e | 367 | |
368 | if (!rc) { | |
369 | fprintf(stderr,"Unknown command '%s'\n",argv[0]); | |
370 | return 1; | |
371 | } | |
7aaaad50 | 372 | config.raw_output = (rc->flags & CMDFLAG_RAWOUTPUT); |
ed9b544e | 373 | |
374 | if ((rc->arity > 0 && argc != rc->arity) || | |
a74f2af6 | 375 | (rc->arity < 0 && argc < -rc->arity)) { |
ed9b544e | 376 | fprintf(stderr,"Wrong number of arguments for '%s'\n",rc->name); |
377 | return 1; | |
378 | } | |
a57d9cc4 | 379 | |
36e5db6d | 380 | if (!strcasecmp(rc->name,"shutdown")) config.shutdown = 1; |
249c3a7d | 381 | if (!strcasecmp(rc->name,"monitor")) config.monitor_mode = 1; |
382 | if (!strcasecmp(rc->name,"subscribe") || | |
383 | !strcasecmp(rc->name,"psubscribe")) config.pubsub_mode = 1; | |
ed9b544e | 384 | if ((fd = cliConnect()) == -1) return 1; |
385 | ||
62e920df | 386 | /* Select db number */ |
387 | retval = selectDb(fd); | |
388 | if (retval) { | |
389 | fprintf(stderr,"Error setting DB num\n"); | |
390 | return 1; | |
391 | } | |
6cf5882c | 392 | |
aab055ae | 393 | while(repeat--) { |
5762b7f0 | 394 | /* Build the command to send */ |
cbb87f7f | 395 | cmd = sdscatprintf(sdsempty(),"*%d\r\n",argc); |
396 | for (j = 0; j < argc; j++) { | |
397 | cmd = sdscatprintf(cmd,"$%lu\r\n", | |
398 | (unsigned long)sdslen(argv[j])); | |
399 | cmd = sdscatlen(cmd,argv[j],sdslen(argv[j])); | |
400 | cmd = sdscatlen(cmd,"\r\n",2); | |
ed9b544e | 401 | } |
5762b7f0 | 402 | anetWrite(fd,cmd,sdslen(cmd)); |
403 | sdsfree(cmd); | |
621d5c19 | 404 | |
249c3a7d | 405 | while (config.monitor_mode) { |
621d5c19 | 406 | cliReadSingleLineReply(fd,0); |
407 | } | |
408 | ||
249c3a7d | 409 | if (config.pubsub_mode) { |
410 | printf("Reading messages... (press Ctrl-c to quit)\n"); | |
411 | while (1) { | |
412 | cliReadReply(fd); | |
413 | printf("\n"); | |
414 | } | |
415 | } | |
416 | ||
5762b7f0 | 417 | retval = cliReadReply(fd); |
36e5db6d | 418 | |
5762b7f0 | 419 | if (retval) { |
36e5db6d | 420 | return retval; |
8165a5f2 | 421 | } |
ed9b544e | 422 | } |
ed9b544e | 423 | return 0; |
424 | } | |
425 | ||
426 | static int parseOptions(int argc, char **argv) { | |
427 | int i; | |
428 | ||
429 | for (i = 1; i < argc; i++) { | |
430 | int lastarg = i==argc-1; | |
6cf5882c | 431 | |
ed9b544e | 432 | if (!strcmp(argv[i],"-h") && !lastarg) { |
433 | char *ip = zmalloc(32); | |
434 | if (anetResolve(NULL,argv[i+1],ip) == ANET_ERR) { | |
435 | printf("Can't resolve %s\n", argv[i]); | |
436 | exit(1); | |
437 | } | |
438 | config.hostip = ip; | |
439 | i++; | |
a9158272 | 440 | } else if (!strcmp(argv[i],"-h") && lastarg) { |
441 | usage(); | |
ed9b544e | 442 | } else if (!strcmp(argv[i],"-p") && !lastarg) { |
443 | config.hostport = atoi(argv[i+1]); | |
444 | i++; | |
5762b7f0 | 445 | } else if (!strcmp(argv[i],"-r") && !lastarg) { |
446 | config.repeat = strtoll(argv[i+1],NULL,10); | |
447 | i++; | |
62e920df | 448 | } else if (!strcmp(argv[i],"-n") && !lastarg) { |
449 | config.dbnum = atoi(argv[i+1]); | |
450 | i++; | |
fdfdae0f | 451 | } else if (!strcmp(argv[i],"-a") && !lastarg) { |
288799e0 | 452 | config.auth = argv[i+1]; |
fdfdae0f | 453 | i++; |
6cf5882c MMDJ |
454 | } else if (!strcmp(argv[i],"-i")) { |
455 | config.interactive = 1; | |
ed9b544e | 456 | } else { |
457 | break; | |
458 | } | |
459 | } | |
460 | return i; | |
461 | } | |
462 | ||
463 | static sds readArgFromStdin(void) { | |
464 | char buf[1024]; | |
465 | sds arg = sdsempty(); | |
466 | ||
467 | while(1) { | |
468 | int nread = read(fileno(stdin),buf,1024); | |
469 | ||
470 | if (nread == 0) break; | |
471 | else if (nread == -1) { | |
472 | perror("Reading from standard input"); | |
473 | exit(1); | |
474 | } | |
475 | arg = sdscatlen(arg,buf,nread); | |
476 | } | |
477 | return arg; | |
478 | } | |
479 | ||
a9158272 | 480 | static void usage() { |
fdfdae0f | 481 | fprintf(stderr, "usage: redis-cli [-h host] [-p port] [-a authpw] [-r repeat_times] [-n db_num] [-i] cmd arg1 arg2 arg3 ... argN\n"); |
482 | 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 | 483 | fprintf(stderr, "\nIf a pipe from standard input is detected this data is used as last argument.\n\n"); |
484 | fprintf(stderr, "example: cat /etc/passwd | redis-cli set my_passwd\n"); | |
485 | fprintf(stderr, "example: redis-cli get my_passwd\n"); | |
486 | fprintf(stderr, "example: redis-cli -r 100 lpush mylist x\n"); | |
d239ec59 | 487 | fprintf(stderr, "\nRun in interactive mode: redis-cli -i or just don't pass any command\n"); |
a9158272 | 488 | exit(1); |
489 | } | |
490 | ||
6cf5882c MMDJ |
491 | /* Turn the plain C strings into Sds strings */ |
492 | static char **convertToSds(int count, char** args) { | |
493 | int j; | |
494 | char **sds = zmalloc(sizeof(char*)*count+1); | |
495 | ||
496 | for(j = 0; j < count; j++) | |
497 | sds[j] = sdsnew(args[j]); | |
498 | ||
499 | return sds; | |
500 | } | |
501 | ||
a88a2af6 | 502 | static char **splitArguments(char *line, int *argc) { |
503 | char *p = line; | |
504 | char *current = NULL; | |
505 | char **vector = NULL; | |
506 | ||
507 | *argc = 0; | |
508 | while(1) { | |
509 | /* skip blanks */ | |
510 | while(*p && isspace(*p)) p++; | |
511 | if (*p) { | |
512 | /* get a token */ | |
513 | int inq=0; /* set to 1 if we are in "quotes" */ | |
514 | int done = 0; | |
515 | ||
516 | if (current == NULL) current = sdsempty(); | |
517 | while(!done) { | |
518 | if (inq) { | |
519 | if (*p == '\\' && *(p+1)) { | |
520 | char c; | |
521 | ||
522 | p++; | |
523 | switch(*p) { | |
524 | case 'n': c = '\n'; break; | |
525 | case 'r': c = '\r'; break; | |
526 | case 't': c = '\t'; break; | |
527 | case 'b': c = '\b'; break; | |
528 | case 'a': c = '\a'; break; | |
529 | default: c = *p; break; | |
530 | } | |
531 | current = sdscatlen(current,&c,1); | |
532 | } else if (*p == '"') { | |
533 | done = 1; | |
534 | } else { | |
535 | current = sdscatlen(current,p,1); | |
536 | } | |
537 | } else { | |
538 | switch(*p) { | |
539 | case ' ': | |
540 | case '\n': | |
541 | case '\r': | |
542 | case '\t': | |
543 | case '\0': | |
544 | done=1; | |
545 | break; | |
546 | case '"': | |
547 | inq=1; | |
548 | break; | |
549 | default: | |
550 | current = sdscatlen(current,p,1); | |
551 | break; | |
552 | } | |
553 | } | |
554 | if (*p) p++; | |
555 | } | |
556 | /* add the token to the vector */ | |
557 | vector = zrealloc(vector,((*argc)+1)*sizeof(char*)); | |
558 | vector[*argc] = current; | |
559 | (*argc)++; | |
560 | current = NULL; | |
561 | } else { | |
562 | return vector; | |
563 | } | |
564 | } | |
565 | } | |
566 | ||
567 | #define LINE_BUFLEN 4096 | |
6cf5882c | 568 | static void repl() { |
a88a2af6 | 569 | int argc, j; |
570 | char *line, **argv; | |
6cf5882c | 571 | |
bc86d88e | 572 | while((line = linenoise("redis> ")) != NULL) { |
cf87ebf2 | 573 | if (line[0] != '\0') { |
a88a2af6 | 574 | argv = splitArguments(line,&argc); |
575 | linenoiseHistoryAdd(line); | |
576 | if (argc > 0) { | |
577 | if (strcasecmp(argv[0],"quit") == 0 || | |
578 | strcasecmp(argv[0],"exit") == 0) | |
579 | exit(0); | |
580 | else | |
581 | cliSendCommand(argc, argv, 1); | |
582 | } | |
583 | /* Free the argument vector */ | |
584 | for (j = 0; j < argc; j++) | |
585 | sdsfree(argv[j]); | |
8ff6a48b | 586 | zfree(argv); |
6cf5882c | 587 | } |
a88a2af6 | 588 | /* linenoise() returns malloc-ed lines like readline() */ |
cf87ebf2 | 589 | free(line); |
6cf5882c | 590 | } |
6cf5882c MMDJ |
591 | exit(0); |
592 | } | |
593 | ||
ed9b544e | 594 | int main(int argc, char **argv) { |
6cf5882c | 595 | int firstarg; |
ed9b544e | 596 | char **argvcopy; |
2073a849 | 597 | struct redisCommand *rc; |
ed9b544e | 598 | |
599 | config.hostip = "127.0.0.1"; | |
600 | config.hostport = 6379; | |
5762b7f0 | 601 | config.repeat = 1; |
62e920df | 602 | config.dbnum = 0; |
36e5db6d | 603 | config.shutdown = 0; |
6cf5882c | 604 | config.interactive = 0; |
249c3a7d | 605 | config.monitor_mode = 0; |
606 | config.pubsub_mode = 0; | |
f40b035d | 607 | config.raw_output = 0; |
288799e0 | 608 | config.auth = NULL; |
ed9b544e | 609 | |
610 | firstarg = parseOptions(argc,argv); | |
611 | argc -= firstarg; | |
612 | argv += firstarg; | |
ed9b544e | 613 | |
aab055ae MMDJ |
614 | if (config.auth != NULL) { |
615 | char *authargv[2]; | |
616 | ||
617 | authargv[0] = "AUTH"; | |
618 | authargv[1] = config.auth; | |
619 | cliSendCommand(2, convertToSds(2, authargv), 1); | |
620 | } | |
621 | ||
d239ec59 | 622 | if (argc == 0 || config.interactive == 1) repl(); |
ed9b544e | 623 | |
6cf5882c MMDJ |
624 | argvcopy = convertToSds(argc, argv); |
625 | ||
2073a849 | 626 | /* Read the last argument from stdandard input if needed */ |
627 | if ((rc = lookupCommand(argv[0])) != NULL) { | |
6cf5882c MMDJ |
628 | if (rc->arity > 0 && argc == rc->arity-1) { |
629 | sds lastarg = readArgFromStdin(); | |
630 | argvcopy[argc] = lastarg; | |
631 | argc++; | |
632 | } | |
2073a849 | 633 | } |
634 | ||
aab055ae | 635 | return cliSendCommand(argc, argvcopy, config.repeat); |
ed9b544e | 636 | } |