]>
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" |
185cabda | 32 | #include "version.h" |
23d4709d | 33 | |
ed9b544e | 34 | #include <stdio.h> |
35 | #include <string.h> | |
36 | #include <stdlib.h> | |
37 | #include <unistd.h> | |
a88a2af6 | 38 | #include <ctype.h> |
ed9b544e | 39 | |
40 | #include "anet.h" | |
41 | #include "sds.h" | |
42 | #include "adlist.h" | |
43 | #include "zmalloc.h" | |
cf87ebf2 | 44 | #include "linenoise.h" |
ed9b544e | 45 | |
46 | #define REDIS_CMD_INLINE 1 | |
47 | #define REDIS_CMD_BULK 2 | |
e17e0b05 | 48 | #define REDIS_CMD_MULTIBULK 4 |
ed9b544e | 49 | |
50 | #define REDIS_NOTUSED(V) ((void) V) | |
51 | ||
52 | static struct config { | |
53 | char *hostip; | |
54 | int hostport; | |
7e91f971 | 55 | char *hostsocket; |
5762b7f0 | 56 | long repeat; |
62e920df | 57 | int dbnum; |
37dc9e5a | 58 | int argn_from_stdin; |
6cf5882c | 59 | int interactive; |
36e5db6d | 60 | int shutdown; |
249c3a7d | 61 | int monitor_mode; |
62 | int pubsub_mode; | |
f40b035d | 63 | int raw_output; |
288799e0 | 64 | char *auth; |
99628c1a | 65 | char *historyfile; |
ed9b544e | 66 | } config; |
67 | ||
c937aa89 | 68 | static int cliReadReply(int fd); |
a9158272 | 69 | static void usage(); |
c937aa89 | 70 | |
ed9b544e | 71 | static int cliConnect(void) { |
72 | char err[ANET_ERR_LEN]; | |
6fa24622 | 73 | static int fd = ANET_ERR; |
ed9b544e | 74 | |
ed9b544e | 75 | if (fd == ANET_ERR) { |
7e91f971 PN |
76 | if (config.hostsocket == NULL) { |
77 | fd = anetTcpConnect(err,config.hostip,config.hostport); | |
78 | } else { | |
79 | fd = anetUnixConnect(err,config.hostsocket); | |
80 | if (fd == ANET_ERR) { | |
81 | fprintf(stderr, "Could not connect to Redis at %s: %s", config.hostsocket, err); | |
82 | return -1; | |
83 | } | |
84 | } | |
6fa24622 | 85 | if (fd == ANET_ERR) { |
7e91f971 PN |
86 | fprintf(stderr,"Could not connect to Redis at "); |
87 | if (config.hostsocket == NULL) | |
88 | fprintf(stderr,"%s:%d: %s",config.hostip,config.hostport,err); | |
89 | else | |
90 | fprintf(stderr,"%s: %s",config.hostsocket,err); | |
6fa24622 DJMM |
91 | return -1; |
92 | } | |
93 | anetTcpNoDelay(NULL,fd); | |
ed9b544e | 94 | } |
ed9b544e | 95 | return fd; |
96 | } | |
97 | ||
98 | static sds cliReadLine(int fd) { | |
99 | sds line = sdsempty(); | |
100 | ||
101 | while(1) { | |
102 | char c; | |
b91f03a4 | 103 | ssize_t ret; |
ed9b544e | 104 | |
b91f03a4 LH |
105 | ret = read(fd,&c,1); |
106 | if (ret == -1) { | |
ed9b544e | 107 | sdsfree(line); |
108 | return NULL; | |
b91f03a4 | 109 | } else if ((ret == 0) || (c == '\n')) { |
ed9b544e | 110 | break; |
111 | } else { | |
112 | line = sdscatlen(line,&c,1); | |
113 | } | |
114 | } | |
115 | return sdstrim(line,"\r\n"); | |
116 | } | |
117 | ||
62e920df | 118 | static int cliReadSingleLineReply(int fd, int quiet) { |
ed9b544e | 119 | sds reply = cliReadLine(fd); |
120 | ||
121 | if (reply == NULL) return 1; | |
62e920df | 122 | if (!quiet) |
123 | printf("%s\n", reply); | |
621d5c19 | 124 | sdsfree(reply); |
ed9b544e | 125 | return 0; |
126 | } | |
127 | ||
21cdc9f0 | 128 | static void printStringRepr(char *s, int len) { |
129 | printf("\""); | |
130 | while(len--) { | |
131 | switch(*s) { | |
132 | case '\\': | |
133 | case '"': | |
134 | printf("\\%c",*s); | |
135 | break; | |
136 | case '\n': printf("\\n"); break; | |
137 | case '\r': printf("\\r"); break; | |
138 | case '\t': printf("\\t"); break; | |
139 | case '\a': printf("\\a"); break; | |
140 | case '\b': printf("\\b"); break; | |
141 | default: | |
142 | if (isprint(*s)) | |
143 | printf("%c",*s); | |
144 | else | |
145 | printf("\\x%02x",(unsigned char)*s); | |
146 | break; | |
147 | } | |
148 | s++; | |
149 | } | |
150 | printf("\"\n"); | |
151 | } | |
152 | ||
c937aa89 | 153 | static int cliReadBulkReply(int fd) { |
ed9b544e | 154 | sds replylen = cliReadLine(fd); |
155 | char *reply, crlf[2]; | |
c937aa89 | 156 | int bulklen; |
ed9b544e | 157 | |
158 | if (replylen == NULL) return 1; | |
ed9b544e | 159 | bulklen = atoi(replylen); |
c937aa89 | 160 | if (bulklen == -1) { |
ed9b544e | 161 | sdsfree(replylen); |
060f6be6 | 162 | printf("(nil)\n"); |
ed9b544e | 163 | return 0; |
164 | } | |
ed9b544e | 165 | reply = zmalloc(bulklen); |
166 | anetRead(fd,reply,bulklen); | |
167 | anetRead(fd,crlf,2); | |
f40b035d | 168 | if (config.raw_output || !isatty(fileno(stdout))) { |
21cdc9f0 | 169 | if (bulklen && fwrite(reply,bulklen,1,stdout) == 0) { |
170 | zfree(reply); | |
171 | return 1; | |
172 | } | |
21cdc9f0 | 173 | } else { |
174 | /* If you are producing output for the standard output we want | |
175 | * a more interesting output with quoted characters and so forth */ | |
176 | printStringRepr(reply,bulklen); | |
ed9b544e | 177 | } |
ed9b544e | 178 | zfree(reply); |
c937aa89 | 179 | return 0; |
ed9b544e | 180 | } |
181 | ||
182 | static int cliReadMultiBulkReply(int fd) { | |
183 | sds replylen = cliReadLine(fd); | |
184 | int elements, c = 1; | |
185 | ||
186 | if (replylen == NULL) return 1; | |
c937aa89 | 187 | elements = atoi(replylen); |
188 | if (elements == -1) { | |
ed9b544e | 189 | sdsfree(replylen); |
190 | printf("(nil)\n"); | |
191 | return 0; | |
192 | } | |
c937aa89 | 193 | if (elements == 0) { |
194 | printf("(empty list or set)\n"); | |
195 | } | |
ed9b544e | 196 | while(elements--) { |
197 | printf("%d. ", c); | |
c937aa89 | 198 | if (cliReadReply(fd)) return 1; |
ed9b544e | 199 | c++; |
200 | } | |
201 | return 0; | |
202 | } | |
203 | ||
c937aa89 | 204 | static int cliReadReply(int fd) { |
205 | char type; | |
206 | ||
36e5db6d BD |
207 | if (anetRead(fd,&type,1) <= 0) { |
208 | if (config.shutdown) return 0; | |
209 | exit(1); | |
210 | } | |
c937aa89 | 211 | switch(type) { |
212 | case '-': | |
213 | printf("(error) "); | |
62e920df | 214 | cliReadSingleLineReply(fd,0); |
c937aa89 | 215 | return 1; |
216 | case '+': | |
62e920df | 217 | return cliReadSingleLineReply(fd,0); |
c937aa89 | 218 | case ':': |
443c6409 | 219 | printf("(integer) "); |
62e920df | 220 | return cliReadSingleLineReply(fd,0); |
c937aa89 | 221 | case '$': |
222 | return cliReadBulkReply(fd); | |
223 | case '*': | |
224 | return cliReadMultiBulkReply(fd); | |
225 | default: | |
226 | printf("protocol error, got '%c' as reply type byte\n", type); | |
227 | return 1; | |
228 | } | |
229 | } | |
230 | ||
6cf5882c | 231 | static int selectDb(int fd) { |
62e920df | 232 | int retval; |
233 | sds cmd; | |
234 | char type; | |
235 | ||
236 | if (config.dbnum == 0) | |
237 | return 0; | |
238 | ||
239 | cmd = sdsempty(); | |
240 | cmd = sdscatprintf(cmd,"SELECT %d\r\n",config.dbnum); | |
241 | anetWrite(fd,cmd,sdslen(cmd)); | |
242 | anetRead(fd,&type,1); | |
243 | if (type <= 0 || type != '+') return 1; | |
244 | retval = cliReadSingleLineReply(fd,1); | |
245 | if (retval) { | |
6cf5882c | 246 | return retval; |
62e920df | 247 | } |
248 | return 0; | |
249 | } | |
250 | ||
aab055ae | 251 | static int cliSendCommand(int argc, char **argv, int repeat) { |
37dc9e5a | 252 | char *command = argv[0]; |
ed9b544e | 253 | int fd, j, retval = 0; |
5762b7f0 | 254 | sds cmd; |
ed9b544e | 255 | |
37dc9e5a PN |
256 | config.raw_output = !strcasecmp(command,"info"); |
257 | if (!strcasecmp(command,"shutdown")) config.shutdown = 1; | |
258 | if (!strcasecmp(command,"monitor")) config.monitor_mode = 1; | |
259 | if (!strcasecmp(command,"subscribe") || | |
260 | !strcasecmp(command,"psubscribe")) config.pubsub_mode = 1; | |
ed9b544e | 261 | if ((fd = cliConnect()) == -1) return 1; |
262 | ||
62e920df | 263 | /* Select db number */ |
264 | retval = selectDb(fd); | |
265 | if (retval) { | |
266 | fprintf(stderr,"Error setting DB num\n"); | |
267 | return 1; | |
268 | } | |
6cf5882c | 269 | |
a2f4f871 PN |
270 | /* Build the command to send */ |
271 | cmd = sdscatprintf(sdsempty(),"*%d\r\n",argc); | |
272 | for (j = 0; j < argc; j++) { | |
273 | cmd = sdscatprintf(cmd,"$%lu\r\n", | |
274 | (unsigned long)sdslen(argv[j])); | |
275 | cmd = sdscatlen(cmd,argv[j],sdslen(argv[j])); | |
276 | cmd = sdscatlen(cmd,"\r\n",2); | |
277 | } | |
278 | ||
aab055ae | 279 | while(repeat--) { |
5762b7f0 | 280 | anetWrite(fd,cmd,sdslen(cmd)); |
249c3a7d | 281 | while (config.monitor_mode) { |
621d5c19 | 282 | cliReadSingleLineReply(fd,0); |
283 | } | |
284 | ||
249c3a7d | 285 | if (config.pubsub_mode) { |
286 | printf("Reading messages... (press Ctrl-c to quit)\n"); | |
287 | while (1) { | |
288 | cliReadReply(fd); | |
289 | printf("\n"); | |
290 | } | |
291 | } | |
292 | ||
5762b7f0 | 293 | retval = cliReadReply(fd); |
294 | if (retval) { | |
36e5db6d | 295 | return retval; |
8165a5f2 | 296 | } |
ed9b544e | 297 | } |
ed9b544e | 298 | return 0; |
299 | } | |
300 | ||
301 | static int parseOptions(int argc, char **argv) { | |
302 | int i; | |
303 | ||
304 | for (i = 1; i < argc; i++) { | |
305 | int lastarg = i==argc-1; | |
6cf5882c | 306 | |
ed9b544e | 307 | if (!strcmp(argv[i],"-h") && !lastarg) { |
308 | char *ip = zmalloc(32); | |
309 | if (anetResolve(NULL,argv[i+1],ip) == ANET_ERR) { | |
310 | printf("Can't resolve %s\n", argv[i]); | |
311 | exit(1); | |
312 | } | |
313 | config.hostip = ip; | |
314 | i++; | |
a9158272 | 315 | } else if (!strcmp(argv[i],"-h") && lastarg) { |
316 | usage(); | |
ed9b544e | 317 | } else if (!strcmp(argv[i],"-p") && !lastarg) { |
318 | config.hostport = atoi(argv[i+1]); | |
319 | i++; | |
7e91f971 PN |
320 | } else if (!strcmp(argv[i],"-s") && !lastarg) { |
321 | config.hostsocket = argv[i+1]; | |
322 | i++; | |
5762b7f0 | 323 | } else if (!strcmp(argv[i],"-r") && !lastarg) { |
324 | config.repeat = strtoll(argv[i+1],NULL,10); | |
325 | i++; | |
62e920df | 326 | } else if (!strcmp(argv[i],"-n") && !lastarg) { |
327 | config.dbnum = atoi(argv[i+1]); | |
328 | i++; | |
fdfdae0f | 329 | } else if (!strcmp(argv[i],"-a") && !lastarg) { |
288799e0 | 330 | config.auth = argv[i+1]; |
fdfdae0f | 331 | i++; |
6cf5882c MMDJ |
332 | } else if (!strcmp(argv[i],"-i")) { |
333 | config.interactive = 1; | |
37dc9e5a PN |
334 | } else if (!strcmp(argv[i],"-c")) { |
335 | config.argn_from_stdin = 1; | |
185cabda | 336 | } else if (!strcmp(argv[i],"-v")) { |
337 | printf("redis-cli shipped with Redis verison %s\n", REDIS_VERSION); | |
338 | exit(0); | |
ed9b544e | 339 | } else { |
340 | break; | |
341 | } | |
342 | } | |
343 | return i; | |
344 | } | |
345 | ||
346 | static sds readArgFromStdin(void) { | |
347 | char buf[1024]; | |
348 | sds arg = sdsempty(); | |
349 | ||
350 | while(1) { | |
351 | int nread = read(fileno(stdin),buf,1024); | |
352 | ||
353 | if (nread == 0) break; | |
354 | else if (nread == -1) { | |
355 | perror("Reading from standard input"); | |
356 | exit(1); | |
357 | } | |
358 | arg = sdscatlen(arg,buf,nread); | |
359 | } | |
360 | return arg; | |
361 | } | |
362 | ||
a9158272 | 363 | static void usage() { |
7e91f971 PN |
364 | fprintf(stderr, "usage: redis-cli [-iv] [-h host] [-p port] [-s /path/to/socket] [-a authpw] [-r repeat_times] [-n db_num] cmd arg1 arg2 arg3 ... argN\n"); |
365 | fprintf(stderr, "usage: echo \"argN\" | redis-cli -c [-h host] [-p port] [-s /path/to/socket] [-a authpw] [-r repeat_times] [-n db_num] cmd arg1 arg2 ... arg(N-1)\n"); | |
a9158272 | 366 | fprintf(stderr, "\nIf a pipe from standard input is detected this data is used as last argument.\n\n"); |
367 | fprintf(stderr, "example: cat /etc/passwd | redis-cli set my_passwd\n"); | |
368 | fprintf(stderr, "example: redis-cli get my_passwd\n"); | |
369 | fprintf(stderr, "example: redis-cli -r 100 lpush mylist x\n"); | |
d239ec59 | 370 | fprintf(stderr, "\nRun in interactive mode: redis-cli -i or just don't pass any command\n"); |
a9158272 | 371 | exit(1); |
372 | } | |
373 | ||
6cf5882c MMDJ |
374 | /* Turn the plain C strings into Sds strings */ |
375 | static char **convertToSds(int count, char** args) { | |
376 | int j; | |
37dc9e5a | 377 | char **sds = zmalloc(sizeof(char*)*count); |
6cf5882c MMDJ |
378 | |
379 | for(j = 0; j < count; j++) | |
380 | sds[j] = sdsnew(args[j]); | |
381 | ||
382 | return sds; | |
383 | } | |
384 | ||
a88a2af6 | 385 | static char **splitArguments(char *line, int *argc) { |
386 | char *p = line; | |
387 | char *current = NULL; | |
388 | char **vector = NULL; | |
389 | ||
390 | *argc = 0; | |
391 | while(1) { | |
392 | /* skip blanks */ | |
393 | while(*p && isspace(*p)) p++; | |
394 | if (*p) { | |
395 | /* get a token */ | |
396 | int inq=0; /* set to 1 if we are in "quotes" */ | |
397 | int done = 0; | |
398 | ||
399 | if (current == NULL) current = sdsempty(); | |
400 | while(!done) { | |
401 | if (inq) { | |
402 | if (*p == '\\' && *(p+1)) { | |
403 | char c; | |
404 | ||
405 | p++; | |
406 | switch(*p) { | |
407 | case 'n': c = '\n'; break; | |
408 | case 'r': c = '\r'; break; | |
409 | case 't': c = '\t'; break; | |
410 | case 'b': c = '\b'; break; | |
411 | case 'a': c = '\a'; break; | |
412 | default: c = *p; break; | |
413 | } | |
414 | current = sdscatlen(current,&c,1); | |
415 | } else if (*p == '"') { | |
416 | done = 1; | |
417 | } else { | |
418 | current = sdscatlen(current,p,1); | |
419 | } | |
420 | } else { | |
421 | switch(*p) { | |
422 | case ' ': | |
423 | case '\n': | |
424 | case '\r': | |
425 | case '\t': | |
426 | case '\0': | |
427 | done=1; | |
428 | break; | |
429 | case '"': | |
430 | inq=1; | |
431 | break; | |
432 | default: | |
433 | current = sdscatlen(current,p,1); | |
434 | break; | |
435 | } | |
436 | } | |
437 | if (*p) p++; | |
438 | } | |
439 | /* add the token to the vector */ | |
440 | vector = zrealloc(vector,((*argc)+1)*sizeof(char*)); | |
441 | vector[*argc] = current; | |
442 | (*argc)++; | |
443 | current = NULL; | |
444 | } else { | |
445 | return vector; | |
446 | } | |
447 | } | |
448 | } | |
449 | ||
450 | #define LINE_BUFLEN 4096 | |
6cf5882c | 451 | static void repl() { |
a88a2af6 | 452 | int argc, j; |
453 | char *line, **argv; | |
6cf5882c | 454 | |
bc86d88e | 455 | while((line = linenoise("redis> ")) != NULL) { |
cf87ebf2 | 456 | if (line[0] != '\0') { |
a88a2af6 | 457 | argv = splitArguments(line,&argc); |
458 | linenoiseHistoryAdd(line); | |
99628c1a | 459 | if (config.historyfile) linenoiseHistorySave(config.historyfile); |
a88a2af6 | 460 | if (argc > 0) { |
461 | if (strcasecmp(argv[0],"quit") == 0 || | |
462 | strcasecmp(argv[0],"exit") == 0) | |
463 | exit(0); | |
464 | else | |
465 | cliSendCommand(argc, argv, 1); | |
466 | } | |
467 | /* Free the argument vector */ | |
468 | for (j = 0; j < argc; j++) | |
469 | sdsfree(argv[j]); | |
8ff6a48b | 470 | zfree(argv); |
6cf5882c | 471 | } |
a88a2af6 | 472 | /* linenoise() returns malloc-ed lines like readline() */ |
cf87ebf2 | 473 | free(line); |
6cf5882c | 474 | } |
6cf5882c MMDJ |
475 | exit(0); |
476 | } | |
477 | ||
ed9b544e | 478 | int main(int argc, char **argv) { |
6cf5882c | 479 | int firstarg; |
ed9b544e | 480 | char **argvcopy; |
481 | ||
482 | config.hostip = "127.0.0.1"; | |
483 | config.hostport = 6379; | |
7e91f971 | 484 | config.hostsocket = NULL; |
5762b7f0 | 485 | config.repeat = 1; |
62e920df | 486 | config.dbnum = 0; |
37dc9e5a | 487 | config.argn_from_stdin = 0; |
36e5db6d | 488 | config.shutdown = 0; |
6cf5882c | 489 | config.interactive = 0; |
249c3a7d | 490 | config.monitor_mode = 0; |
491 | config.pubsub_mode = 0; | |
f40b035d | 492 | config.raw_output = 0; |
288799e0 | 493 | config.auth = NULL; |
99628c1a | 494 | config.historyfile = NULL; |
495 | ||
496 | if (getenv("HOME") != NULL) { | |
497 | config.historyfile = malloc(256); | |
498 | snprintf(config.historyfile,256,"%s/.rediscli_history",getenv("HOME")); | |
499 | linenoiseHistoryLoad(config.historyfile); | |
500 | } | |
ed9b544e | 501 | |
502 | firstarg = parseOptions(argc,argv); | |
503 | argc -= firstarg; | |
504 | argv += firstarg; | |
ed9b544e | 505 | |
aab055ae MMDJ |
506 | if (config.auth != NULL) { |
507 | char *authargv[2]; | |
508 | ||
509 | authargv[0] = "AUTH"; | |
510 | authargv[1] = config.auth; | |
511 | cliSendCommand(2, convertToSds(2, authargv), 1); | |
512 | } | |
513 | ||
d239ec59 | 514 | if (argc == 0 || config.interactive == 1) repl(); |
ed9b544e | 515 | |
37dc9e5a PN |
516 | argvcopy = convertToSds(argc+1, argv); |
517 | if (config.argn_from_stdin) { | |
6cf5882c MMDJ |
518 | sds lastarg = readArgFromStdin(); |
519 | argvcopy[argc] = lastarg; | |
520 | argc++; | |
2073a849 | 521 | } |
aab055ae | 522 | return cliSendCommand(argc, argvcopy, config.repeat); |
ed9b544e | 523 | } |