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