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();
144 } else if ((ret
== 0) || (c
== '\n')) {
147 line
= sdscatlen(line
,&c
,1);
150 return sdstrim(line
,"\r\n");
153 static int cliReadInlineReply(int fd
, int type
) {
154 sds reply
= cliReadLine(fd
);
156 if (reply
== NULL
) return 1;
157 printf("%s\n", reply
);
158 if (type
== REDIS_CMD_SINGLELINEREPLY
) return 0;
159 if (type
== REDIS_CMD_INTREPLY
) return atoi(reply
) < 0;
160 if (type
== REDIS_CMD_RETCODEREPLY
) return reply
[0] == '-';
164 static int cliReadBulkReply(int fd
, int multibulk
) {
165 sds replylen
= cliReadLine(fd
);
166 char *reply
, crlf
[2];
167 int bulklen
, error
= 0;
169 if (replylen
== NULL
) return 1;
170 if (strcmp(replylen
,"nil") == 0) {
175 bulklen
= atoi(replylen
);
176 if (multibulk
&& bulklen
== -1) {
185 reply
= zmalloc(bulklen
);
186 anetRead(fd
,reply
,bulklen
);
188 if (bulklen
&& fwrite(reply
,bulklen
,1,stdout
) == 0) {
192 if (!multibulk
&& isatty(fileno(stdout
)) && reply
[bulklen
-1] != '\n')
198 static int cliReadMultiBulkReply(int fd
) {
199 sds replylen
= cliReadLine(fd
);
202 if (replylen
== NULL
) return 1;
203 if (strcmp(replylen
,"nil") == 0) {
208 elements
= atoi(replylen
);
211 if (cliReadBulkReply(fd
,1)) return 1;
218 static int cliSendCommand(int argc
, char **argv
) {
219 struct redisCommand
*rc
= lookupCommand(argv
[0]);
220 int fd
, j
, retval
= 0;
221 sds cmd
= sdsempty();
224 fprintf(stderr
,"Unknown command '%s'\n",argv
[0]);
228 if ((rc
->arity
> 0 && argc
!= rc
->arity
) ||
229 (rc
->arity
< 0 && argc
< -rc
->arity
)) {
230 fprintf(stderr
,"Wrong number of arguments for '%s'\n",rc
->name
);
233 if ((fd
= cliConnect()) == -1) return 1;
235 /* Build the command to send */
236 for (j
= 0; j
< argc
; j
++) {
237 if (j
!= 0) cmd
= sdscat(cmd
," ");
238 if (j
== argc
-1 && rc
->flags
& REDIS_CMD_BULK
) {
239 cmd
= sdscatprintf(cmd
,"%d",sdslen(argv
[j
]));
241 cmd
= sdscatlen(cmd
,argv
[j
],sdslen(argv
[j
]));
244 cmd
= sdscat(cmd
,"\r\n");
245 if (rc
->flags
& REDIS_CMD_BULK
) {
246 cmd
= sdscatlen(cmd
,argv
[argc
-1],sdslen(argv
[argc
-1]));
247 cmd
= sdscat(cmd
,"\r\n");
249 anetWrite(fd
,cmd
,sdslen(cmd
));
250 if (rc
->flags
& REDIS_CMD_INTREPLY
) {
251 retval
= cliReadInlineReply(fd
,REDIS_CMD_INTREPLY
);
252 } else if (rc
->flags
& REDIS_CMD_RETCODEREPLY
) {
253 retval
= cliReadInlineReply(fd
,REDIS_CMD_RETCODEREPLY
);
254 } else if (rc
->flags
& REDIS_CMD_SINGLELINEREPLY
) {
255 retval
= cliReadInlineReply(fd
,REDIS_CMD_SINGLELINEREPLY
);
256 } else if (rc
->flags
& REDIS_CMD_BULKREPLY
) {
257 retval
= cliReadBulkReply(fd
,0);
258 } else if (rc
->flags
& REDIS_CMD_MULTIBULKREPLY
) {
259 retval
= cliReadMultiBulkReply(fd
);
269 static int parseOptions(int argc
, char **argv
) {
272 for (i
= 1; i
< argc
; i
++) {
273 int lastarg
= i
==argc
-1;
275 if (!strcmp(argv
[i
],"-h") && !lastarg
) {
276 char *ip
= zmalloc(32);
277 if (anetResolve(NULL
,argv
[i
+1],ip
) == ANET_ERR
) {
278 printf("Can't resolve %s\n", argv
[i
]);
283 } else if (!strcmp(argv
[i
],"-p") && !lastarg
) {
284 config
.hostport
= atoi(argv
[i
+1]);
293 static sds
readArgFromStdin(void) {
295 sds arg
= sdsempty();
298 int nread
= read(fileno(stdin
),buf
,1024);
300 if (nread
== 0) break;
301 else if (nread
== -1) {
302 perror("Reading from standard input");
305 arg
= sdscatlen(arg
,buf
,nread
);
310 int main(int argc
, char **argv
) {
314 config
.hostip
= "127.0.0.1";
315 config
.hostport
= 6379;
317 firstarg
= parseOptions(argc
,argv
);
321 /* Turn the plain C strings into Sds strings */
322 argvcopy
= zmalloc(sizeof(char*)*argc
+1);
323 for(j
= 0; j
< argc
; j
++)
324 argvcopy
[j
] = sdsnew(argv
[j
]);
326 /* Read the last argument from stdandard input */
327 if (!isatty(fileno(stdin
))) {
328 sds lastarg
= readArgFromStdin();
329 argvcopy
[argc
] = lastarg
;
334 fprintf(stderr
, "usage: redis-cli [-h host] [-p port] cmd arg1 arg2 arg3 ... argN\n");
335 fprintf(stderr
, "usage: echo \"argN\" | redis-cli [-h host] [-p port] cmd arg1 arg2 ... arg(N-1)\n");
336 fprintf(stderr
, "\nIf a pipe from standard input is detected this data is used as last argument.\n\n");
337 fprintf(stderr
, "example: cat /etc/passwd | redis-cli set my_passwd\n");
338 fprintf(stderr
, "example: redis-cli get my_passwd\n");
342 return cliSendCommand(argc
, argvcopy
);