]>
Commit | Line | Data |
---|---|---|
ed9b544e | 1 | /* Redis CLI (command line interface) |
2 | * | |
3 | * Copyright (c) 2006-2009, Salvatore Sanfilippo <antirez at gmail dot com> | |
4 | * All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * | |
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. | |
17 | * | |
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. | |
29 | */ | |
30 | ||
23d4709d | 31 | #include "fmacros.h" |
32 | ||
ed9b544e | 33 | #include <stdio.h> |
34 | #include <string.h> | |
35 | #include <stdlib.h> | |
36 | #include <unistd.h> | |
37 | ||
38 | #include "anet.h" | |
39 | #include "sds.h" | |
40 | #include "adlist.h" | |
41 | #include "zmalloc.h" | |
42 | ||
43 | #define REDIS_CMD_INLINE 1 | |
44 | #define REDIS_CMD_BULK 2 | |
ed9b544e | 45 | |
46 | #define REDIS_NOTUSED(V) ((void) V) | |
47 | ||
48 | static struct config { | |
49 | char *hostip; | |
50 | int hostport; | |
51 | } config; | |
52 | ||
53 | struct redisCommand { | |
54 | char *name; | |
55 | int arity; | |
56 | int flags; | |
57 | }; | |
58 | ||
59 | static struct redisCommand cmdTable[] = { | |
c937aa89 | 60 | {"get",2,REDIS_CMD_INLINE}, |
61 | {"set",3,REDIS_CMD_BULK}, | |
62 | {"setnx",3,REDIS_CMD_BULK}, | |
5109cdff | 63 | {"del",-2,REDIS_CMD_INLINE}, |
c937aa89 | 64 | {"exists",2,REDIS_CMD_INLINE}, |
65 | {"incr",2,REDIS_CMD_INLINE}, | |
66 | {"decr",2,REDIS_CMD_INLINE}, | |
67 | {"rpush",3,REDIS_CMD_BULK}, | |
68 | {"lpush",3,REDIS_CMD_BULK}, | |
69 | {"rpop",2,REDIS_CMD_INLINE}, | |
70 | {"lpop",2,REDIS_CMD_INLINE}, | |
71 | {"llen",2,REDIS_CMD_INLINE}, | |
72 | {"lindex",3,REDIS_CMD_INLINE}, | |
73 | {"lset",4,REDIS_CMD_BULK}, | |
74 | {"lrange",4,REDIS_CMD_INLINE}, | |
75 | {"ltrim",4,REDIS_CMD_INLINE}, | |
76 | {"lrem",4,REDIS_CMD_BULK}, | |
77 | {"sadd",3,REDIS_CMD_BULK}, | |
78 | {"srem",3,REDIS_CMD_BULK}, | |
a4460ef4 | 79 | {"smove",4,REDIS_CMD_BULK}, |
c937aa89 | 80 | {"sismember",3,REDIS_CMD_BULK}, |
81 | {"scard",2,REDIS_CMD_INLINE}, | |
82 | {"sinter",-2,REDIS_CMD_INLINE}, | |
83 | {"sinterstore",-3,REDIS_CMD_INLINE}, | |
40d224a9 | 84 | {"sunion",-2,REDIS_CMD_INLINE}, |
85 | {"sunionstore",-3,REDIS_CMD_INLINE}, | |
f4f56e1d | 86 | {"sdiff",-2,REDIS_CMD_INLINE}, |
87 | {"sdiffstore",-3,REDIS_CMD_INLINE}, | |
c937aa89 | 88 | {"smembers",2,REDIS_CMD_INLINE}, |
89 | {"incrby",3,REDIS_CMD_INLINE}, | |
90 | {"decrby",3,REDIS_CMD_INLINE}, | |
a431eb74 | 91 | {"getset",3,REDIS_CMD_BULK}, |
c937aa89 | 92 | {"randomkey",1,REDIS_CMD_INLINE}, |
93 | {"select",2,REDIS_CMD_INLINE}, | |
94 | {"move",3,REDIS_CMD_INLINE}, | |
95 | {"rename",3,REDIS_CMD_INLINE}, | |
96 | {"renamenx",3,REDIS_CMD_INLINE}, | |
97 | {"keys",2,REDIS_CMD_INLINE}, | |
98 | {"dbsize",1,REDIS_CMD_INLINE}, | |
99 | {"ping",1,REDIS_CMD_INLINE}, | |
100 | {"echo",2,REDIS_CMD_BULK}, | |
101 | {"save",1,REDIS_CMD_INLINE}, | |
102 | {"bgsave",1,REDIS_CMD_INLINE}, | |
103 | {"shutdown",1,REDIS_CMD_INLINE}, | |
104 | {"lastsave",1,REDIS_CMD_INLINE}, | |
105 | {"type",2,REDIS_CMD_INLINE}, | |
106 | {"flushdb",1,REDIS_CMD_INLINE}, | |
107 | {"flushall",1,REDIS_CMD_INLINE}, | |
108 | {"sort",-2,REDIS_CMD_INLINE}, | |
109 | {"info",1,REDIS_CMD_INLINE}, | |
110 | {"mget",-2,REDIS_CMD_INLINE}, | |
3305306f | 111 | {"expire",3,REDIS_CMD_INLINE}, |
fd88489a | 112 | {"ttl",2,REDIS_CMD_INLINE}, |
ed9b544e | 113 | {NULL,0,0} |
114 | }; | |
115 | ||
c937aa89 | 116 | static int cliReadReply(int fd); |
117 | ||
ed9b544e | 118 | static struct redisCommand *lookupCommand(char *name) { |
119 | int j = 0; | |
120 | while(cmdTable[j].name != NULL) { | |
121 | if (!strcasecmp(name,cmdTable[j].name)) return &cmdTable[j]; | |
122 | j++; | |
123 | } | |
124 | return NULL; | |
125 | } | |
126 | ||
127 | static int cliConnect(void) { | |
128 | char err[ANET_ERR_LEN]; | |
129 | int fd; | |
130 | ||
131 | fd = anetTcpConnect(err,config.hostip,config.hostport); | |
132 | if (fd == ANET_ERR) { | |
133 | fprintf(stderr,"Connect: %s\n",err); | |
134 | return -1; | |
135 | } | |
136 | anetTcpNoDelay(NULL,fd); | |
137 | return fd; | |
138 | } | |
139 | ||
140 | static sds cliReadLine(int fd) { | |
141 | sds line = sdsempty(); | |
142 | ||
143 | while(1) { | |
144 | char c; | |
b91f03a4 | 145 | ssize_t ret; |
ed9b544e | 146 | |
b91f03a4 LH |
147 | ret = read(fd,&c,1); |
148 | if (ret == -1) { | |
ed9b544e | 149 | sdsfree(line); |
150 | return NULL; | |
b91f03a4 | 151 | } else if ((ret == 0) || (c == '\n')) { |
ed9b544e | 152 | break; |
153 | } else { | |
154 | line = sdscatlen(line,&c,1); | |
155 | } | |
156 | } | |
157 | return sdstrim(line,"\r\n"); | |
158 | } | |
159 | ||
c937aa89 | 160 | static int cliReadSingleLineReply(int fd) { |
ed9b544e | 161 | sds reply = cliReadLine(fd); |
162 | ||
163 | if (reply == NULL) return 1; | |
164 | printf("%s\n", reply); | |
ed9b544e | 165 | return 0; |
166 | } | |
167 | ||
c937aa89 | 168 | static int cliReadBulkReply(int fd) { |
ed9b544e | 169 | sds replylen = cliReadLine(fd); |
170 | char *reply, crlf[2]; | |
c937aa89 | 171 | int bulklen; |
ed9b544e | 172 | |
173 | if (replylen == NULL) return 1; | |
ed9b544e | 174 | bulklen = atoi(replylen); |
c937aa89 | 175 | if (bulklen == -1) { |
ed9b544e | 176 | sdsfree(replylen); |
177 | printf("(nil)"); | |
178 | return 0; | |
179 | } | |
ed9b544e | 180 | reply = zmalloc(bulklen); |
181 | anetRead(fd,reply,bulklen); | |
182 | anetRead(fd,crlf,2); | |
183 | if (bulklen && fwrite(reply,bulklen,1,stdout) == 0) { | |
184 | zfree(reply); | |
185 | return 1; | |
186 | } | |
c937aa89 | 187 | if (isatty(fileno(stdout)) && reply[bulklen-1] != '\n') |
ed9b544e | 188 | printf("\n"); |
189 | zfree(reply); | |
c937aa89 | 190 | return 0; |
ed9b544e | 191 | } |
192 | ||
193 | static int cliReadMultiBulkReply(int fd) { | |
194 | sds replylen = cliReadLine(fd); | |
195 | int elements, c = 1; | |
196 | ||
197 | if (replylen == NULL) return 1; | |
c937aa89 | 198 | elements = atoi(replylen); |
199 | if (elements == -1) { | |
ed9b544e | 200 | sdsfree(replylen); |
201 | printf("(nil)\n"); | |
202 | return 0; | |
203 | } | |
c937aa89 | 204 | if (elements == 0) { |
205 | printf("(empty list or set)\n"); | |
206 | } | |
ed9b544e | 207 | while(elements--) { |
208 | printf("%d. ", c); | |
c937aa89 | 209 | if (cliReadReply(fd)) return 1; |
ed9b544e | 210 | c++; |
211 | } | |
212 | return 0; | |
213 | } | |
214 | ||
c937aa89 | 215 | static int cliReadReply(int fd) { |
216 | char type; | |
217 | ||
218 | if (anetRead(fd,&type,1) <= 0) exit(1); | |
219 | switch(type) { | |
220 | case '-': | |
221 | printf("(error) "); | |
222 | cliReadSingleLineReply(fd); | |
223 | return 1; | |
224 | case '+': | |
225 | case ':': | |
226 | return cliReadSingleLineReply(fd); | |
227 | case '$': | |
228 | return cliReadBulkReply(fd); | |
229 | case '*': | |
230 | return cliReadMultiBulkReply(fd); | |
231 | default: | |
232 | printf("protocol error, got '%c' as reply type byte\n", type); | |
233 | return 1; | |
234 | } | |
235 | } | |
236 | ||
ed9b544e | 237 | static int cliSendCommand(int argc, char **argv) { |
238 | struct redisCommand *rc = lookupCommand(argv[0]); | |
239 | int fd, j, retval = 0; | |
240 | sds cmd = sdsempty(); | |
241 | ||
242 | if (!rc) { | |
243 | fprintf(stderr,"Unknown command '%s'\n",argv[0]); | |
244 | return 1; | |
245 | } | |
246 | ||
247 | if ((rc->arity > 0 && argc != rc->arity) || | |
a74f2af6 | 248 | (rc->arity < 0 && argc < -rc->arity)) { |
ed9b544e | 249 | fprintf(stderr,"Wrong number of arguments for '%s'\n",rc->name); |
250 | return 1; | |
251 | } | |
252 | if ((fd = cliConnect()) == -1) return 1; | |
253 | ||
254 | /* Build the command to send */ | |
255 | for (j = 0; j < argc; j++) { | |
256 | if (j != 0) cmd = sdscat(cmd," "); | |
257 | if (j == argc-1 && rc->flags & REDIS_CMD_BULK) { | |
258 | cmd = sdscatprintf(cmd,"%d",sdslen(argv[j])); | |
259 | } else { | |
260 | cmd = sdscatlen(cmd,argv[j],sdslen(argv[j])); | |
261 | } | |
262 | } | |
263 | cmd = sdscat(cmd,"\r\n"); | |
264 | if (rc->flags & REDIS_CMD_BULK) { | |
265 | cmd = sdscatlen(cmd,argv[argc-1],sdslen(argv[argc-1])); | |
266 | cmd = sdscat(cmd,"\r\n"); | |
267 | } | |
268 | anetWrite(fd,cmd,sdslen(cmd)); | |
c937aa89 | 269 | retval = cliReadReply(fd); |
ed9b544e | 270 | if (retval) { |
271 | close(fd); | |
272 | return retval; | |
273 | } | |
274 | close(fd); | |
275 | return 0; | |
276 | } | |
277 | ||
278 | static int parseOptions(int argc, char **argv) { | |
279 | int i; | |
280 | ||
281 | for (i = 1; i < argc; i++) { | |
282 | int lastarg = i==argc-1; | |
283 | ||
284 | if (!strcmp(argv[i],"-h") && !lastarg) { | |
285 | char *ip = zmalloc(32); | |
286 | if (anetResolve(NULL,argv[i+1],ip) == ANET_ERR) { | |
287 | printf("Can't resolve %s\n", argv[i]); | |
288 | exit(1); | |
289 | } | |
290 | config.hostip = ip; | |
291 | i++; | |
292 | } else if (!strcmp(argv[i],"-p") && !lastarg) { | |
293 | config.hostport = atoi(argv[i+1]); | |
294 | i++; | |
295 | } else { | |
296 | break; | |
297 | } | |
298 | } | |
299 | return i; | |
300 | } | |
301 | ||
302 | static sds readArgFromStdin(void) { | |
303 | char buf[1024]; | |
304 | sds arg = sdsempty(); | |
305 | ||
306 | while(1) { | |
307 | int nread = read(fileno(stdin),buf,1024); | |
308 | ||
309 | if (nread == 0) break; | |
310 | else if (nread == -1) { | |
311 | perror("Reading from standard input"); | |
312 | exit(1); | |
313 | } | |
314 | arg = sdscatlen(arg,buf,nread); | |
315 | } | |
316 | return arg; | |
317 | } | |
318 | ||
319 | int main(int argc, char **argv) { | |
320 | int firstarg, j; | |
321 | char **argvcopy; | |
322 | ||
323 | config.hostip = "127.0.0.1"; | |
324 | config.hostport = 6379; | |
325 | ||
326 | firstarg = parseOptions(argc,argv); | |
327 | argc -= firstarg; | |
328 | argv += firstarg; | |
329 | ||
330 | /* Turn the plain C strings into Sds strings */ | |
331 | argvcopy = zmalloc(sizeof(char*)*argc+1); | |
332 | for(j = 0; j < argc; j++) | |
333 | argvcopy[j] = sdsnew(argv[j]); | |
334 | ||
335 | /* Read the last argument from stdandard input */ | |
336 | if (!isatty(fileno(stdin))) { | |
337 | sds lastarg = readArgFromStdin(); | |
338 | argvcopy[argc] = lastarg; | |
339 | argc++; | |
340 | } | |
341 | ||
342 | if (argc < 1) { | |
343 | fprintf(stderr, "usage: redis-cli [-h host] [-p port] cmd arg1 arg2 arg3 ... argN\n"); | |
344 | fprintf(stderr, "usage: echo \"argN\" | redis-cli [-h host] [-p port] 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 | exit(1); | |
349 | } | |
350 | ||
351 | return cliSendCommand(argc, argvcopy); | |
352 | } |