]>
git.saurik.com Git - redis.git/blob - redis-cli.c
1 /* Redis CLI (command line interface)
3 * Copyright (c) 2009-2010, Salvatore Sanfilippo <antirez at gmail dot com>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
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.
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.
43 #include "linenoise.h"
45 #define REDIS_CMD_INLINE 1
46 #define REDIS_CMD_BULK 2
47 #define REDIS_CMD_MULTIBULK 4
49 #define REDIS_NOTUSED(V) ((void) V)
51 static struct config
{
65 static int cliReadReply(int fd
);
68 static int cliConnect(void) {
69 char err
[ANET_ERR_LEN
];
70 static int fd
= ANET_ERR
;
73 fd
= anetTcpConnect(err
,config
.hostip
,config
.hostport
);
75 fprintf(stderr
, "Could not connect to Redis at %s:%d: %s", config
.hostip
, config
.hostport
, err
);
78 anetTcpNoDelay(NULL
,fd
);
83 static sds
cliReadLine(int fd
) {
84 sds line
= sdsempty();
94 } else if ((ret
== 0) || (c
== '\n')) {
97 line
= sdscatlen(line
,&c
,1);
100 return sdstrim(line
,"\r\n");
103 static int cliReadSingleLineReply(int fd
, int quiet
) {
104 sds reply
= cliReadLine(fd
);
106 if (reply
== NULL
) return 1;
108 printf("%s\n", reply
);
113 static void printStringRepr(char *s
, int len
) {
121 case '\n': printf("\\n"); break;
122 case '\r': printf("\\r"); break;
123 case '\t': printf("\\t"); break;
124 case '\a': printf("\\a"); break;
125 case '\b': printf("\\b"); break;
130 printf("\\x%02x",(unsigned char)*s
);
138 static int cliReadBulkReply(int fd
) {
139 sds replylen
= cliReadLine(fd
);
140 char *reply
, crlf
[2];
143 if (replylen
== NULL
) return 1;
144 bulklen
= atoi(replylen
);
150 reply
= zmalloc(bulklen
);
151 anetRead(fd
,reply
,bulklen
);
153 if (config
.raw_output
|| !isatty(fileno(stdout
))) {
154 if (bulklen
&& fwrite(reply
,bulklen
,1,stdout
) == 0) {
159 /* If you are producing output for the standard output we want
160 * a more interesting output with quoted characters and so forth */
161 printStringRepr(reply
,bulklen
);
167 static int cliReadMultiBulkReply(int fd
) {
168 sds replylen
= cliReadLine(fd
);
171 if (replylen
== NULL
) return 1;
172 elements
= atoi(replylen
);
173 if (elements
== -1) {
179 printf("(empty list or set)\n");
183 if (cliReadReply(fd
)) return 1;
189 static int cliReadReply(int fd
) {
192 if (anetRead(fd
,&type
,1) <= 0) {
193 if (config
.shutdown
) return 0;
199 cliReadSingleLineReply(fd
,0);
202 return cliReadSingleLineReply(fd
,0);
204 printf("(integer) ");
205 return cliReadSingleLineReply(fd
,0);
207 return cliReadBulkReply(fd
);
209 return cliReadMultiBulkReply(fd
);
211 printf("protocol error, got '%c' as reply type byte\n", type
);
216 static int selectDb(int fd
) {
221 if (config
.dbnum
== 0)
225 cmd
= sdscatprintf(cmd
,"SELECT %d\r\n",config
.dbnum
);
226 anetWrite(fd
,cmd
,sdslen(cmd
));
227 anetRead(fd
,&type
,1);
228 if (type
<= 0 || type
!= '+') return 1;
229 retval
= cliReadSingleLineReply(fd
,1);
236 static int cliSendCommand(int argc
, char **argv
, int repeat
) {
237 char *command
= argv
[0];
238 int fd
, j
, retval
= 0;
241 config
.raw_output
= !strcasecmp(command
,"info");
242 if (!strcasecmp(command
,"shutdown")) config
.shutdown
= 1;
243 if (!strcasecmp(command
,"monitor")) config
.monitor_mode
= 1;
244 if (!strcasecmp(command
,"subscribe") ||
245 !strcasecmp(command
,"psubscribe")) config
.pubsub_mode
= 1;
246 if ((fd
= cliConnect()) == -1) return 1;
248 /* Select db number */
249 retval
= selectDb(fd
);
251 fprintf(stderr
,"Error setting DB num\n");
255 /* Build the command to send */
256 cmd
= sdscatprintf(sdsempty(),"*%d\r\n",argc
);
257 for (j
= 0; j
< argc
; j
++) {
258 cmd
= sdscatprintf(cmd
,"$%lu\r\n",
259 (unsigned long)sdslen(argv
[j
]));
260 cmd
= sdscatlen(cmd
,argv
[j
],sdslen(argv
[j
]));
261 cmd
= sdscatlen(cmd
,"\r\n",2);
265 anetWrite(fd
,cmd
,sdslen(cmd
));
266 while (config
.monitor_mode
) {
267 cliReadSingleLineReply(fd
,0);
270 if (config
.pubsub_mode
) {
271 printf("Reading messages... (press Ctrl-c to quit)\n");
278 retval
= cliReadReply(fd
);
286 static int parseOptions(int argc
, char **argv
) {
289 for (i
= 1; i
< argc
; i
++) {
290 int lastarg
= i
==argc
-1;
292 if (!strcmp(argv
[i
],"-h") && !lastarg
) {
293 char *ip
= zmalloc(32);
294 if (anetResolve(NULL
,argv
[i
+1],ip
) == ANET_ERR
) {
295 printf("Can't resolve %s\n", argv
[i
]);
300 } else if (!strcmp(argv
[i
],"-h") && lastarg
) {
302 } else if (!strcmp(argv
[i
],"-p") && !lastarg
) {
303 config
.hostport
= atoi(argv
[i
+1]);
305 } else if (!strcmp(argv
[i
],"-r") && !lastarg
) {
306 config
.repeat
= strtoll(argv
[i
+1],NULL
,10);
308 } else if (!strcmp(argv
[i
],"-n") && !lastarg
) {
309 config
.dbnum
= atoi(argv
[i
+1]);
311 } else if (!strcmp(argv
[i
],"-a") && !lastarg
) {
312 config
.auth
= argv
[i
+1];
314 } else if (!strcmp(argv
[i
],"-i")) {
315 config
.interactive
= 1;
316 } else if (!strcmp(argv
[i
],"-c")) {
317 config
.argn_from_stdin
= 1;
325 static sds
readArgFromStdin(void) {
327 sds arg
= sdsempty();
330 int nread
= read(fileno(stdin
),buf
,1024);
332 if (nread
== 0) break;
333 else if (nread
== -1) {
334 perror("Reading from standard input");
337 arg
= sdscatlen(arg
,buf
,nread
);
342 static void usage() {
343 fprintf(stderr
, "usage: redis-cli [-h host] [-p port] [-a authpw] [-r repeat_times] [-n db_num] [-i] cmd arg1 arg2 arg3 ... argN\n");
344 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");
345 fprintf(stderr
, "\nIf a pipe from standard input is detected this data is used as last argument.\n\n");
346 fprintf(stderr
, "example: cat /etc/passwd | redis-cli set my_passwd\n");
347 fprintf(stderr
, "example: redis-cli get my_passwd\n");
348 fprintf(stderr
, "example: redis-cli -r 100 lpush mylist x\n");
349 fprintf(stderr
, "\nRun in interactive mode: redis-cli -i or just don't pass any command\n");
353 /* Turn the plain C strings into Sds strings */
354 static char **convertToSds(int count
, char** args
) {
356 char **sds
= zmalloc(sizeof(char*)*count
);
358 for(j
= 0; j
< count
; j
++)
359 sds
[j
] = sdsnew(args
[j
]);
364 static char **splitArguments(char *line
, int *argc
) {
366 char *current
= NULL
;
367 char **vector
= NULL
;
372 while(*p
&& isspace(*p
)) p
++;
375 int inq
=0; /* set to 1 if we are in "quotes" */
378 if (current
== NULL
) current
= sdsempty();
381 if (*p
== '\\' && *(p
+1)) {
386 case 'n': c
= '\n'; break;
387 case 'r': c
= '\r'; break;
388 case 't': c
= '\t'; break;
389 case 'b': c
= '\b'; break;
390 case 'a': c
= '\a'; break;
391 default: c
= *p
; break;
393 current
= sdscatlen(current
,&c
,1);
394 } else if (*p
== '"') {
397 current
= sdscatlen(current
,p
,1);
412 current
= sdscatlen(current
,p
,1);
418 /* add the token to the vector */
419 vector
= zrealloc(vector
,((*argc
)+1)*sizeof(char*));
420 vector
[*argc
] = current
;
429 #define LINE_BUFLEN 4096
434 while((line
= linenoise("redis> ")) != NULL
) {
435 if (line
[0] != '\0') {
436 argv
= splitArguments(line
,&argc
);
437 linenoiseHistoryAdd(line
);
439 if (strcasecmp(argv
[0],"quit") == 0 ||
440 strcasecmp(argv
[0],"exit") == 0)
443 cliSendCommand(argc
, argv
, 1);
445 /* Free the argument vector */
446 for (j
= 0; j
< argc
; j
++)
450 /* linenoise() returns malloc-ed lines like readline() */
456 int main(int argc
, char **argv
) {
460 config
.hostip
= "127.0.0.1";
461 config
.hostport
= 6379;
464 config
.argn_from_stdin
= 0;
466 config
.interactive
= 0;
467 config
.monitor_mode
= 0;
468 config
.pubsub_mode
= 0;
469 config
.raw_output
= 0;
472 firstarg
= parseOptions(argc
,argv
);
476 if (config
.auth
!= NULL
) {
479 authargv
[0] = "AUTH";
480 authargv
[1] = config
.auth
;
481 cliSendCommand(2, convertToSds(2, authargv
), 1);
484 if (argc
== 0 || config
.interactive
== 1) repl();
486 argvcopy
= convertToSds(argc
+1, argv
);
487 if (config
.argn_from_stdin
) {
488 sds lastarg
= readArgFromStdin();
489 argvcopy
[argc
] = lastarg
;
492 return cliSendCommand(argc
, argvcopy
, config
.repeat
);