]>
git.saurik.com Git - redis.git/blob - src/redis-cli.c
1d00fb87398446ff51419165eaaabe3c9de58bdf
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.
44 #include "linenoise.h"
46 #define REDIS_CMD_INLINE 1
47 #define REDIS_CMD_BULK 2
48 #define REDIS_CMD_MULTIBULK 4
50 #define REDIS_NOTUSED(V) ((void) V)
52 static struct config
{
66 static int cliReadReply(int fd
);
69 static int cliConnect(void) {
70 char err
[ANET_ERR_LEN
];
71 static int fd
= ANET_ERR
;
74 fd
= anetTcpConnect(err
,config
.hostip
,config
.hostport
);
76 fprintf(stderr
, "Could not connect to Redis at %s:%d: %s", config
.hostip
, config
.hostport
, err
);
79 anetTcpNoDelay(NULL
,fd
);
84 static sds
cliReadLine(int fd
) {
85 sds line
= sdsempty();
95 } else if ((ret
== 0) || (c
== '\n')) {
98 line
= sdscatlen(line
,&c
,1);
101 return sdstrim(line
,"\r\n");
104 static int cliReadSingleLineReply(int fd
, int quiet
) {
105 sds reply
= cliReadLine(fd
);
107 if (reply
== NULL
) return 1;
109 printf("%s\n", reply
);
114 static void printStringRepr(char *s
, int len
) {
122 case '\n': printf("\\n"); break;
123 case '\r': printf("\\r"); break;
124 case '\t': printf("\\t"); break;
125 case '\a': printf("\\a"); break;
126 case '\b': printf("\\b"); break;
131 printf("\\x%02x",(unsigned char)*s
);
139 static int cliReadBulkReply(int fd
) {
140 sds replylen
= cliReadLine(fd
);
141 char *reply
, crlf
[2];
144 if (replylen
== NULL
) return 1;
145 bulklen
= atoi(replylen
);
151 reply
= zmalloc(bulklen
);
152 anetRead(fd
,reply
,bulklen
);
154 if (config
.raw_output
|| !isatty(fileno(stdout
))) {
155 if (bulklen
&& fwrite(reply
,bulklen
,1,stdout
) == 0) {
160 /* If you are producing output for the standard output we want
161 * a more interesting output with quoted characters and so forth */
162 printStringRepr(reply
,bulklen
);
168 static int cliReadMultiBulkReply(int fd
) {
169 sds replylen
= cliReadLine(fd
);
172 if (replylen
== NULL
) return 1;
173 elements
= atoi(replylen
);
174 if (elements
== -1) {
180 printf("(empty list or set)\n");
184 if (cliReadReply(fd
)) return 1;
190 static int cliReadReply(int fd
) {
193 if (anetRead(fd
,&type
,1) <= 0) {
194 if (config
.shutdown
) return 0;
200 cliReadSingleLineReply(fd
,0);
203 return cliReadSingleLineReply(fd
,0);
205 printf("(integer) ");
206 return cliReadSingleLineReply(fd
,0);
208 return cliReadBulkReply(fd
);
210 return cliReadMultiBulkReply(fd
);
212 printf("protocol error, got '%c' as reply type byte\n", type
);
217 static int selectDb(int fd
) {
222 if (config
.dbnum
== 0)
226 cmd
= sdscatprintf(cmd
,"SELECT %d\r\n",config
.dbnum
);
227 anetWrite(fd
,cmd
,sdslen(cmd
));
228 anetRead(fd
,&type
,1);
229 if (type
<= 0 || type
!= '+') return 1;
230 retval
= cliReadSingleLineReply(fd
,1);
237 static int cliSendCommand(int argc
, char **argv
, int repeat
) {
238 char *command
= argv
[0];
239 int fd
, j
, retval
= 0;
242 config
.raw_output
= !strcasecmp(command
,"info");
243 if (!strcasecmp(command
,"shutdown")) config
.shutdown
= 1;
244 if (!strcasecmp(command
,"monitor")) config
.monitor_mode
= 1;
245 if (!strcasecmp(command
,"subscribe") ||
246 !strcasecmp(command
,"psubscribe")) config
.pubsub_mode
= 1;
247 if ((fd
= cliConnect()) == -1) return 1;
249 /* Select db number */
250 retval
= selectDb(fd
);
252 fprintf(stderr
,"Error setting DB num\n");
256 /* Build the command to send */
257 cmd
= sdscatprintf(sdsempty(),"*%d\r\n",argc
);
258 for (j
= 0; j
< argc
; j
++) {
259 cmd
= sdscatprintf(cmd
,"$%lu\r\n",
260 (unsigned long)sdslen(argv
[j
]));
261 cmd
= sdscatlen(cmd
,argv
[j
],sdslen(argv
[j
]));
262 cmd
= sdscatlen(cmd
,"\r\n",2);
266 anetWrite(fd
,cmd
,sdslen(cmd
));
267 while (config
.monitor_mode
) {
268 cliReadSingleLineReply(fd
,0);
271 if (config
.pubsub_mode
) {
272 printf("Reading messages... (press Ctrl-c to quit)\n");
279 retval
= cliReadReply(fd
);
287 static int parseOptions(int argc
, char **argv
) {
290 for (i
= 1; i
< argc
; i
++) {
291 int lastarg
= i
==argc
-1;
293 if (!strcmp(argv
[i
],"-h") && !lastarg
) {
294 char *ip
= zmalloc(32);
295 if (anetResolve(NULL
,argv
[i
+1],ip
) == ANET_ERR
) {
296 printf("Can't resolve %s\n", argv
[i
]);
301 } else if (!strcmp(argv
[i
],"-h") && lastarg
) {
303 } else if (!strcmp(argv
[i
],"-p") && !lastarg
) {
304 config
.hostport
= atoi(argv
[i
+1]);
306 } else if (!strcmp(argv
[i
],"-r") && !lastarg
) {
307 config
.repeat
= strtoll(argv
[i
+1],NULL
,10);
309 } else if (!strcmp(argv
[i
],"-n") && !lastarg
) {
310 config
.dbnum
= atoi(argv
[i
+1]);
312 } else if (!strcmp(argv
[i
],"-a") && !lastarg
) {
313 config
.auth
= argv
[i
+1];
315 } else if (!strcmp(argv
[i
],"-i")) {
316 config
.interactive
= 1;
317 } else if (!strcmp(argv
[i
],"-c")) {
318 config
.argn_from_stdin
= 1;
319 } else if (!strcmp(argv
[i
],"-v")) {
320 printf("redis-cli shipped with Redis verison %s\n", REDIS_VERSION
);
329 static sds
readArgFromStdin(void) {
331 sds arg
= sdsempty();
334 int nread
= read(fileno(stdin
),buf
,1024);
336 if (nread
== 0) break;
337 else if (nread
== -1) {
338 perror("Reading from standard input");
341 arg
= sdscatlen(arg
,buf
,nread
);
346 static void usage() {
347 fprintf(stderr
, "usage: redis-cli [-iv] [-h host] [-p port] [-a authpw] [-r repeat_times] [-n db_num] cmd arg1 arg2 arg3 ... argN\n");
348 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");
349 fprintf(stderr
, "\nIf a pipe from standard input is detected this data is used as last argument.\n\n");
350 fprintf(stderr
, "example: cat /etc/passwd | redis-cli set my_passwd\n");
351 fprintf(stderr
, "example: redis-cli get my_passwd\n");
352 fprintf(stderr
, "example: redis-cli -r 100 lpush mylist x\n");
353 fprintf(stderr
, "\nRun in interactive mode: redis-cli -i or just don't pass any command\n");
357 /* Turn the plain C strings into Sds strings */
358 static char **convertToSds(int count
, char** args
) {
360 char **sds
= zmalloc(sizeof(char*)*count
);
362 for(j
= 0; j
< count
; j
++)
363 sds
[j
] = sdsnew(args
[j
]);
368 static char **splitArguments(char *line
, int *argc
) {
370 char *current
= NULL
;
371 char **vector
= NULL
;
376 while(*p
&& isspace(*p
)) p
++;
379 int inq
=0; /* set to 1 if we are in "quotes" */
382 if (current
== NULL
) current
= sdsempty();
385 if (*p
== '\\' && *(p
+1)) {
390 case 'n': c
= '\n'; break;
391 case 'r': c
= '\r'; break;
392 case 't': c
= '\t'; break;
393 case 'b': c
= '\b'; break;
394 case 'a': c
= '\a'; break;
395 default: c
= *p
; break;
397 current
= sdscatlen(current
,&c
,1);
398 } else if (*p
== '"') {
401 current
= sdscatlen(current
,p
,1);
416 current
= sdscatlen(current
,p
,1);
422 /* add the token to the vector */
423 vector
= zrealloc(vector
,((*argc
)+1)*sizeof(char*));
424 vector
[*argc
] = current
;
433 #define LINE_BUFLEN 4096
438 while((line
= linenoise("redis> ")) != NULL
) {
439 if (line
[0] != '\0') {
440 argv
= splitArguments(line
,&argc
);
441 linenoiseHistoryAdd(line
);
443 if (strcasecmp(argv
[0],"quit") == 0 ||
444 strcasecmp(argv
[0],"exit") == 0)
447 cliSendCommand(argc
, argv
, 1);
449 /* Free the argument vector */
450 for (j
= 0; j
< argc
; j
++)
454 /* linenoise() returns malloc-ed lines like readline() */
460 int main(int argc
, char **argv
) {
464 config
.hostip
= "127.0.0.1";
465 config
.hostport
= 6379;
468 config
.argn_from_stdin
= 0;
470 config
.interactive
= 0;
471 config
.monitor_mode
= 0;
472 config
.pubsub_mode
= 0;
473 config
.raw_output
= 0;
476 firstarg
= parseOptions(argc
,argv
);
480 if (config
.auth
!= NULL
) {
483 authargv
[0] = "AUTH";
484 authargv
[1] = config
.auth
;
485 cliSendCommand(2, convertToSds(2, authargv
), 1);
488 if (argc
== 0 || config
.interactive
== 1) repl();
490 argvcopy
= convertToSds(argc
+1, argv
);
491 if (config
.argn_from_stdin
) {
492 sds lastarg
= readArgFromStdin();
493 argvcopy
[argc
] = lastarg
;
496 return cliSendCommand(argc
, argvcopy
, config
.repeat
);