1 /* Redis CLI (command line interface)
3 * Copyright (c) 2006-2009, 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.
41 #define REDIS_CMD_INLINE 1
42 #define REDIS_CMD_BULK 2
43 #define REDIS_CMD_INTREPLY 4
44 #define REDIS_CMD_RETCODEREPLY 8
45 #define REDIS_CMD_BULKREPLY 16
46 #define REDIS_CMD_MULTIBULKREPLY 32
47 #define REDIS_CMD_SINGLELINEREPLY 64
49 #define REDIS_NOTUSED(V) ((void) V)
51 static struct config
{
62 static struct redisCommand cmdTable
[] = {
63 {"get",2,REDIS_CMD_INLINE
|REDIS_CMD_BULKREPLY
},
64 {"set",3,REDIS_CMD_BULK
|REDIS_CMD_RETCODEREPLY
},
65 {"setnx",3,REDIS_CMD_BULK
|REDIS_CMD_INTREPLY
},
66 {"del",2,REDIS_CMD_INLINE
|REDIS_CMD_INTREPLY
},
67 {"exists",2,REDIS_CMD_INLINE
|REDIS_CMD_INTREPLY
},
68 {"incr",2,REDIS_CMD_INLINE
|REDIS_CMD_INTREPLY
},
69 {"decr",2,REDIS_CMD_INLINE
|REDIS_CMD_INTREPLY
},
70 {"rpush",3,REDIS_CMD_BULK
|REDIS_CMD_RETCODEREPLY
},
71 {"lpush",3,REDIS_CMD_BULK
|REDIS_CMD_RETCODEREPLY
},
72 {"rpop",2,REDIS_CMD_INLINE
|REDIS_CMD_BULKREPLY
},
73 {"lpop",2,REDIS_CMD_INLINE
|REDIS_CMD_BULKREPLY
},
74 {"llen",2,REDIS_CMD_INLINE
|REDIS_CMD_INTREPLY
},
75 {"lindex",3,REDIS_CMD_INLINE
|REDIS_CMD_BULKREPLY
},
76 {"lset",4,REDIS_CMD_BULK
|REDIS_CMD_RETCODEREPLY
},
77 {"lrange",4,REDIS_CMD_INLINE
|REDIS_CMD_MULTIBULKREPLY
},
78 {"ltrim",4,REDIS_CMD_INLINE
|REDIS_CMD_RETCODEREPLY
},
79 {"lrem",4,REDIS_CMD_BULK
|REDIS_CMD_INTREPLY
},
80 {"sadd",3,REDIS_CMD_BULK
|REDIS_CMD_INTREPLY
},
81 {"srem",3,REDIS_CMD_BULK
|REDIS_CMD_INTREPLY
},
82 {"sismember",3,REDIS_CMD_BULK
|REDIS_CMD_INTREPLY
},
83 {"scard",2,REDIS_CMD_INLINE
|REDIS_CMD_INTREPLY
},
84 {"sinter",-2,REDIS_CMD_INLINE
|REDIS_CMD_MULTIBULKREPLY
},
85 {"sinterstore",-3,REDIS_CMD_INLINE
|REDIS_CMD_RETCODEREPLY
},
86 {"smembers",2,REDIS_CMD_INLINE
|REDIS_CMD_MULTIBULKREPLY
},
87 {"incrby",3,REDIS_CMD_INLINE
|REDIS_CMD_INTREPLY
},
88 {"decrby",3,REDIS_CMD_INLINE
|REDIS_CMD_INTREPLY
},
89 {"randomkey",1,REDIS_CMD_INLINE
|REDIS_CMD_SINGLELINEREPLY
},
90 {"select",2,REDIS_CMD_INLINE
|REDIS_CMD_RETCODEREPLY
},
91 {"move",3,REDIS_CMD_INLINE
|REDIS_CMD_INTREPLY
},
92 {"rename",3,REDIS_CMD_INLINE
|REDIS_CMD_RETCODEREPLY
},
93 {"renamenx",3,REDIS_CMD_INLINE
|REDIS_CMD_INTREPLY
},
94 {"keys",2,REDIS_CMD_INLINE
|REDIS_CMD_BULKREPLY
},
95 {"dbsize",1,REDIS_CMD_INLINE
|REDIS_CMD_INTREPLY
},
96 {"ping",1,REDIS_CMD_INLINE
|REDIS_CMD_RETCODEREPLY
},
97 {"echo",2,REDIS_CMD_BULK
|REDIS_CMD_BULKREPLY
},
98 {"save",1,REDIS_CMD_INLINE
|REDIS_CMD_RETCODEREPLY
},
99 {"bgsave",1,REDIS_CMD_INLINE
|REDIS_CMD_RETCODEREPLY
},
100 {"shutdown",1,REDIS_CMD_INLINE
|REDIS_CMD_RETCODEREPLY
},
101 {"lastsave",1,REDIS_CMD_INLINE
|REDIS_CMD_INTREPLY
},
102 {"type",2,REDIS_CMD_INLINE
|REDIS_CMD_SINGLELINEREPLY
},
103 {"flushdb",1,REDIS_CMD_INLINE
|REDIS_CMD_RETCODEREPLY
},
104 {"flushall",1,REDIS_CMD_INLINE
|REDIS_CMD_RETCODEREPLY
},
105 {"sort",-2,REDIS_CMD_INLINE
|REDIS_CMD_MULTIBULKREPLY
},
106 {"info",1,REDIS_CMD_INLINE
|REDIS_CMD_BULKREPLY
},
107 {"mget",-2,REDIS_CMD_INLINE
|REDIS_CMD_MULTIBULKREPLY
},
111 static struct redisCommand
*lookupCommand(char *name
) {
113 while(cmdTable
[j
].name
!= NULL
) {
114 if (!strcasecmp(name
,cmdTable
[j
].name
)) return &cmdTable
[j
];
120 static int cliConnect(void) {
121 char err
[ANET_ERR_LEN
];
124 fd
= anetTcpConnect(err
,config
.hostip
,config
.hostport
);
125 if (fd
== ANET_ERR
) {
126 fprintf(stderr
,"Connect: %s\n",err
);
129 anetTcpNoDelay(NULL
,fd
);
133 static sds
cliReadLine(int fd
) {
134 sds line
= sdsempty();
139 if (read(fd
,&c
,1) == -1) {
142 } else if (c
== '\n') {
145 line
= sdscatlen(line
,&c
,1);
148 return sdstrim(line
,"\r\n");
151 static int cliReadInlineReply(int fd
, int type
) {
152 sds reply
= cliReadLine(fd
);
154 if (reply
== NULL
) return 1;
155 printf("%s\n", reply
);
156 if (type
== REDIS_CMD_SINGLELINEREPLY
) return 0;
157 if (type
== REDIS_CMD_INTREPLY
) return atoi(reply
) < 0;
158 if (type
== REDIS_CMD_RETCODEREPLY
) return reply
[0] == '-';
162 static int cliReadBulkReply(int fd
, int multibulk
) {
163 sds replylen
= cliReadLine(fd
);
164 char *reply
, crlf
[2];
165 int bulklen
, error
= 0;
167 if (replylen
== NULL
) return 1;
168 if (strcmp(replylen
,"nil") == 0) {
173 bulklen
= atoi(replylen
);
174 if (multibulk
&& bulklen
== -1) {
183 reply
= zmalloc(bulklen
);
184 anetRead(fd
,reply
,bulklen
);
186 if (bulklen
&& fwrite(reply
,bulklen
,1,stdout
) == 0) {
190 if (!multibulk
&& isatty(fileno(stdout
)) && reply
[bulklen
-1] != '\n')
196 static int cliReadMultiBulkReply(int fd
) {
197 sds replylen
= cliReadLine(fd
);
200 if (replylen
== NULL
) return 1;
201 if (strcmp(replylen
,"nil") == 0) {
206 elements
= atoi(replylen
);
209 if (cliReadBulkReply(fd
,1)) return 1;
216 static int cliSendCommand(int argc
, char **argv
) {
217 struct redisCommand
*rc
= lookupCommand(argv
[0]);
218 int fd
, j
, retval
= 0;
219 sds cmd
= sdsempty();
222 fprintf(stderr
,"Unknown command '%s'\n",argv
[0]);
226 if ((rc
->arity
> 0 && argc
!= rc
->arity
) ||
227 (rc
->arity
< 0 && argc
< -rc
->arity
)) {
228 fprintf(stderr
,"Wrong number of arguments for '%s'\n",rc
->name
);
231 if ((fd
= cliConnect()) == -1) return 1;
233 /* Build the command to send */
234 for (j
= 0; j
< argc
; j
++) {
235 if (j
!= 0) cmd
= sdscat(cmd
," ");
236 if (j
== argc
-1 && rc
->flags
& REDIS_CMD_BULK
) {
237 cmd
= sdscatprintf(cmd
,"%d",sdslen(argv
[j
]));
239 cmd
= sdscatlen(cmd
,argv
[j
],sdslen(argv
[j
]));
242 cmd
= sdscat(cmd
,"\r\n");
243 if (rc
->flags
& REDIS_CMD_BULK
) {
244 cmd
= sdscatlen(cmd
,argv
[argc
-1],sdslen(argv
[argc
-1]));
245 cmd
= sdscat(cmd
,"\r\n");
247 anetWrite(fd
,cmd
,sdslen(cmd
));
248 if (rc
->flags
& REDIS_CMD_INTREPLY
) {
249 retval
= cliReadInlineReply(fd
,REDIS_CMD_INTREPLY
);
250 } else if (rc
->flags
& REDIS_CMD_RETCODEREPLY
) {
251 retval
= cliReadInlineReply(fd
,REDIS_CMD_RETCODEREPLY
);
252 } else if (rc
->flags
& REDIS_CMD_SINGLELINEREPLY
) {
253 retval
= cliReadInlineReply(fd
,REDIS_CMD_SINGLELINEREPLY
);
254 } else if (rc
->flags
& REDIS_CMD_BULKREPLY
) {
255 retval
= cliReadBulkReply(fd
,0);
256 } else if (rc
->flags
& REDIS_CMD_MULTIBULKREPLY
) {
257 retval
= cliReadMultiBulkReply(fd
);
267 static int parseOptions(int argc
, char **argv
) {
270 for (i
= 1; i
< argc
; i
++) {
271 int lastarg
= i
==argc
-1;
273 if (!strcmp(argv
[i
],"-h") && !lastarg
) {
274 char *ip
= zmalloc(32);
275 if (anetResolve(NULL
,argv
[i
+1],ip
) == ANET_ERR
) {
276 printf("Can't resolve %s\n", argv
[i
]);
281 } else if (!strcmp(argv
[i
],"-p") && !lastarg
) {
282 config
.hostport
= atoi(argv
[i
+1]);
291 static sds
readArgFromStdin(void) {
293 sds arg
= sdsempty();
296 int nread
= read(fileno(stdin
),buf
,1024);
298 if (nread
== 0) break;
299 else if (nread
== -1) {
300 perror("Reading from standard input");
303 arg
= sdscatlen(arg
,buf
,nread
);
308 int main(int argc
, char **argv
) {
312 config
.hostip
= "127.0.0.1";
313 config
.hostport
= 6379;
315 firstarg
= parseOptions(argc
,argv
);
319 /* Turn the plain C strings into Sds strings */
320 argvcopy
= zmalloc(sizeof(char*)*argc
+1);
321 for(j
= 0; j
< argc
; j
++)
322 argvcopy
[j
] = sdsnew(argv
[j
]);
324 /* Read the last argument from stdandard input */
325 if (!isatty(fileno(stdin
))) {
326 sds lastarg
= readArgFromStdin();
327 argvcopy
[argc
] = lastarg
;
332 fprintf(stderr
, "usage: redis-cli [-h host] [-p port] cmd arg1 arg2 arg3 ... argN\n");
333 fprintf(stderr
, "usage: echo \"argN\" | redis-cli [-h host] [-p port] cmd arg1 arg2 ... arg(N-1)\n");
334 fprintf(stderr
, "\nIf a pipe from standard input is detected this data is used as last argument.\n\n");
335 fprintf(stderr
, "example: cat /etc/passwd | redis-cli set my_passwd\n");
336 fprintf(stderr
, "example: redis-cli get my_passwd\n");
340 return cliSendCommand(argc
, argvcopy
);