]> git.saurik.com Git - redis.git/blob - redis-cli.c
Fixed redis-cli readLine loop to correctly handle EOF.
[redis.git] / redis-cli.c
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
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35
36 #include "anet.h"
37 #include "sds.h"
38 #include "adlist.h"
39 #include "zmalloc.h"
40
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
48
49 #define REDIS_NOTUSED(V) ((void) V)
50
51 static struct config {
52 char *hostip;
53 int hostport;
54 } config;
55
56 struct redisCommand {
57 char *name;
58 int arity;
59 int flags;
60 };
61
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},
108 {NULL,0,0}
109 };
110
111 static struct redisCommand *lookupCommand(char *name) {
112 int j = 0;
113 while(cmdTable[j].name != NULL) {
114 if (!strcasecmp(name,cmdTable[j].name)) return &cmdTable[j];
115 j++;
116 }
117 return NULL;
118 }
119
120 static int cliConnect(void) {
121 char err[ANET_ERR_LEN];
122 int fd;
123
124 fd = anetTcpConnect(err,config.hostip,config.hostport);
125 if (fd == ANET_ERR) {
126 fprintf(stderr,"Connect: %s\n",err);
127 return -1;
128 }
129 anetTcpNoDelay(NULL,fd);
130 return fd;
131 }
132
133 static sds cliReadLine(int fd) {
134 sds line = sdsempty();
135
136 while(1) {
137 char c;
138 ssize_t ret;
139
140 ret = read(fd,&c,1);
141 if (ret == -1) {
142 sdsfree(line);
143 return NULL;
144 } else if ((ret == 0) || (c == '\n')) {
145 break;
146 } else {
147 line = sdscatlen(line,&c,1);
148 }
149 }
150 return sdstrim(line,"\r\n");
151 }
152
153 static int cliReadInlineReply(int fd, int type) {
154 sds reply = cliReadLine(fd);
155
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] == '-';
161 return 0;
162 }
163
164 static int cliReadBulkReply(int fd, int multibulk) {
165 sds replylen = cliReadLine(fd);
166 char *reply, crlf[2];
167 int bulklen, error = 0;
168
169 if (replylen == NULL) return 1;
170 if (strcmp(replylen,"nil") == 0) {
171 sdsfree(replylen);
172 printf("(nil)\n");
173 return 0;
174 }
175 bulklen = atoi(replylen);
176 if (multibulk && bulklen == -1) {
177 sdsfree(replylen);
178 printf("(nil)");
179 return 0;
180 }
181 if (bulklen < 0) {
182 bulklen = -bulklen;
183 error = 1;
184 }
185 reply = zmalloc(bulklen);
186 anetRead(fd,reply,bulklen);
187 anetRead(fd,crlf,2);
188 if (bulklen && fwrite(reply,bulklen,1,stdout) == 0) {
189 zfree(reply);
190 return 1;
191 }
192 if (!multibulk && isatty(fileno(stdout)) && reply[bulklen-1] != '\n')
193 printf("\n");
194 zfree(reply);
195 return error;
196 }
197
198 static int cliReadMultiBulkReply(int fd) {
199 sds replylen = cliReadLine(fd);
200 int elements, c = 1;
201
202 if (replylen == NULL) return 1;
203 if (strcmp(replylen,"nil") == 0) {
204 sdsfree(replylen);
205 printf("(nil)\n");
206 return 0;
207 }
208 elements = atoi(replylen);
209 while(elements--) {
210 printf("%d. ", c);
211 if (cliReadBulkReply(fd,1)) return 1;
212 printf("\n");
213 c++;
214 }
215 return 0;
216 }
217
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();
222
223 if (!rc) {
224 fprintf(stderr,"Unknown command '%s'\n",argv[0]);
225 return 1;
226 }
227
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);
231 return 1;
232 }
233 if ((fd = cliConnect()) == -1) return 1;
234
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]));
240 } else {
241 cmd = sdscatlen(cmd,argv[j],sdslen(argv[j]));
242 }
243 }
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");
248 }
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);
260 }
261 if (retval) {
262 close(fd);
263 return retval;
264 }
265 close(fd);
266 return 0;
267 }
268
269 static int parseOptions(int argc, char **argv) {
270 int i;
271
272 for (i = 1; i < argc; i++) {
273 int lastarg = i==argc-1;
274
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]);
279 exit(1);
280 }
281 config.hostip = ip;
282 i++;
283 } else if (!strcmp(argv[i],"-p") && !lastarg) {
284 config.hostport = atoi(argv[i+1]);
285 i++;
286 } else {
287 break;
288 }
289 }
290 return i;
291 }
292
293 static sds readArgFromStdin(void) {
294 char buf[1024];
295 sds arg = sdsempty();
296
297 while(1) {
298 int nread = read(fileno(stdin),buf,1024);
299
300 if (nread == 0) break;
301 else if (nread == -1) {
302 perror("Reading from standard input");
303 exit(1);
304 }
305 arg = sdscatlen(arg,buf,nread);
306 }
307 return arg;
308 }
309
310 int main(int argc, char **argv) {
311 int firstarg, j;
312 char **argvcopy;
313
314 config.hostip = "127.0.0.1";
315 config.hostport = 6379;
316
317 firstarg = parseOptions(argc,argv);
318 argc -= firstarg;
319 argv += firstarg;
320
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]);
325
326 /* Read the last argument from stdandard input */
327 if (!isatty(fileno(stdin))) {
328 sds lastarg = readArgFromStdin();
329 argvcopy[argc] = lastarg;
330 argc++;
331 }
332
333 if (argc < 1) {
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");
339 exit(1);
340 }
341
342 return cliSendCommand(argc, argvcopy);
343 }