]>
git.saurik.com Git - redis.git/blob - src/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.
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
{
62 int raw_output
; /* output mode per command */
67 static int cliReadReply(int fd
);
70 static int cliConnect(void) {
71 char err
[ANET_ERR_LEN
];
72 static int fd
= ANET_ERR
;
75 fd
= anetTcpConnect(err
,config
.hostip
,config
.hostport
);
77 fprintf(stderr
, "Could not connect to Redis at %s:%d: %s", config
.hostip
, config
.hostport
, err
);
80 anetTcpNoDelay(NULL
,fd
);
85 static sds
cliReadLine(int fd
) {
86 sds line
= sdsempty();
96 } else if ((ret
== 0) || (c
== '\n')) {
99 line
= sdscatlen(line
,&c
,1);
102 return sdstrim(line
,"\r\n");
105 static int cliReadSingleLineReply(int fd
, int quiet
) {
106 sds reply
= cliReadLine(fd
);
108 if (reply
== NULL
) return 1;
110 printf("%s\n", reply
);
115 static void printStringRepr(char *s
, int len
) {
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;
132 printf("\\x%02x",(unsigned char)*s
);
140 static int cliReadBulkReply(int fd
) {
141 sds replylen
= cliReadLine(fd
);
142 char *reply
, crlf
[2];
145 if (replylen
== NULL
) return 1;
146 bulklen
= atoi(replylen
);
152 reply
= zmalloc(bulklen
);
153 anetRead(fd
,reply
,bulklen
);
155 if (config
.raw_output
) {
156 if (bulklen
&& fwrite(reply
,bulklen
,1,stdout
) == 0) {
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
);
170 static int cliReadMultiBulkReply(int fd
) {
171 sds replylen
= cliReadLine(fd
);
174 if (replylen
== NULL
) return 1;
175 elements
= atoi(replylen
);
176 if (elements
== -1) {
182 printf("(empty list or set)\n");
186 if (cliReadReply(fd
)) return 1;
192 static int cliReadReply(int fd
) {
195 if (anetRead(fd
,&type
,1) <= 0) {
196 if (config
.shutdown
) return 0;
202 cliReadSingleLineReply(fd
,0);
205 return cliReadSingleLineReply(fd
,0);
207 printf("(integer) ");
208 return cliReadSingleLineReply(fd
,0);
210 return cliReadBulkReply(fd
);
212 return cliReadMultiBulkReply(fd
);
214 printf("protocol error, got '%c' as reply type byte\n", type
);
219 static int selectDb(int fd
) {
224 if (config
.dbnum
== 0)
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);
239 static int cliSendCommand(int argc
, char **argv
, int repeat
) {
240 char *command
= argv
[0];
241 int fd
, j
, retval
= 0;
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;
249 if ((fd
= cliConnect()) == -1) return 1;
251 /* Select db number */
252 retval
= selectDb(fd
);
254 fprintf(stderr
,"Error setting DB num\n");
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);
268 anetWrite(fd
,cmd
,sdslen(cmd
));
269 while (config
.monitor_mode
) {
270 cliReadSingleLineReply(fd
,0);
273 if (config
.pubsub_mode
) {
274 printf("Reading messages... (press Ctrl-c to quit)\n");
281 retval
= cliReadReply(fd
);
289 static int parseOptions(int argc
, char **argv
) {
292 for (i
= 1; i
< argc
; i
++) {
293 int lastarg
= i
==argc
-1;
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
]);
303 } else if (!strcmp(argv
[i
],"-h") && lastarg
) {
305 } else if (!strcmp(argv
[i
],"-p") && !lastarg
) {
306 config
.hostport
= atoi(argv
[i
+1]);
308 } else if (!strcmp(argv
[i
],"-r") && !lastarg
) {
309 config
.repeat
= strtoll(argv
[i
+1],NULL
,10);
311 } else if (!strcmp(argv
[i
],"-n") && !lastarg
) {
312 config
.dbnum
= atoi(argv
[i
+1]);
314 } else if (!strcmp(argv
[i
],"-a") && !lastarg
) {
315 config
.auth
= argv
[i
+1];
317 } else if (!strcmp(argv
[i
],"-i")) {
318 config
.interactive
= 1;
319 } else if (!strcmp(argv
[i
],"-c")) {
320 config
.argn_from_stdin
= 1;
321 } else if (!strcmp(argv
[i
],"-v")) {
322 printf("redis-cli shipped with Redis verison %s\n", REDIS_VERSION
);
331 static sds
readArgFromStdin(void) {
333 sds arg
= sdsempty();
336 int nread
= read(fileno(stdin
),buf
,1024);
338 if (nread
== 0) break;
339 else if (nread
== -1) {
340 perror("Reading from standard input");
343 arg
= sdscatlen(arg
,buf
,nread
);
348 static void usage() {
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");
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");
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");
355 fprintf(stderr
, "\nRun in interactive mode: redis-cli -i or just don't pass any command\n");
359 /* Turn the plain C strings into Sds strings */
360 static char **convertToSds(int count
, char** args
) {
362 char **sds
= zmalloc(sizeof(char*)*count
);
364 for(j
= 0; j
< count
; j
++)
365 sds
[j
] = sdsnew(args
[j
]);
370 static char **splitArguments(char *line
, int *argc
) {
372 char *current
= NULL
;
373 char **vector
= NULL
;
378 while(*p
&& isspace(*p
)) p
++;
381 int inq
=0; /* set to 1 if we are in "quotes" */
384 if (current
== NULL
) current
= sdsempty();
387 if (*p
== '\\' && *(p
+1)) {
392 case 'n': c
= '\n'; break;
393 case 'r': c
= '\r'; break;
394 case 't': c
= '\t'; break;
395 case 'b': c
= '\b'; break;
396 case 'a': c
= '\a'; break;
397 default: c
= *p
; break;
399 current
= sdscatlen(current
,&c
,1);
400 } else if (*p
== '"') {
401 /* closing quote must be followed by a space */
402 if (*(p
+1) && !isspace(*(p
+1))) goto err
;
405 /* unterminated quotes */
408 current
= sdscatlen(current
,p
,1);
423 current
= sdscatlen(current
,p
,1);
429 /* add the token to the vector */
430 vector
= zrealloc(vector
,((*argc
)+1)*sizeof(char*));
431 vector
[*argc
] = current
;
441 sdsfree(vector
[*argc
]);
443 if (current
) sdsfree(current
);
447 #define LINE_BUFLEN 4096
452 while((line
= linenoise("redis> ")) != NULL
) {
453 if (line
[0] != '\0') {
454 argv
= splitArguments(line
,&argc
);
455 linenoiseHistoryAdd(line
);
456 if (config
.historyfile
) linenoiseHistorySave(config
.historyfile
);
458 printf("Invalid argument(s)\n");
460 } else if (argc
> 0) {
461 if (strcasecmp(argv
[0],"quit") == 0 ||
462 strcasecmp(argv
[0],"exit") == 0)
465 cliSendCommand(argc
, argv
, 1);
467 /* Free the argument vector */
468 for (j
= 0; j
< argc
; j
++)
472 /* linenoise() returns malloc-ed lines like readline() */
478 int main(int argc
, char **argv
) {
482 config
.hostip
= "127.0.0.1";
483 config
.hostport
= 6379;
486 config
.argn_from_stdin
= 0;
488 config
.interactive
= 0;
489 config
.monitor_mode
= 0;
490 config
.pubsub_mode
= 0;
491 config
.raw_output
= 0;
493 config
.historyfile
= NULL
;
495 if (getenv("HOME") != NULL
) {
496 config
.historyfile
= malloc(256);
497 snprintf(config
.historyfile
,256,"%s/.rediscli_history",getenv("HOME"));
498 linenoiseHistoryLoad(config
.historyfile
);
501 firstarg
= parseOptions(argc
,argv
);
505 if (config
.auth
!= NULL
) {
508 authargv
[0] = "AUTH";
509 authargv
[1] = config
.auth
;
510 cliSendCommand(2, convertToSds(2, authargv
), 1);
513 if (argc
== 0 || config
.interactive
== 1) {
514 config
.interactive
= 1;
518 argvcopy
= convertToSds(argc
+1, argv
);
519 if (config
.argn_from_stdin
) {
520 sds lastarg
= readArgFromStdin();
521 argvcopy
[argc
] = lastarg
;
524 return cliSendCommand(argc
, argvcopy
, config
.repeat
);