]> git.saurik.com Git - redis.git/blame - redis-cli.c
Non blocking replication (finally!). C-side linked lists API improved.
[redis.git] / redis-cli.c
CommitLineData
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
48static struct config {
49 char *hostip;
50 int hostport;
51} config;
52
53struct redisCommand {
54 char *name;
55 int arity;
56 int flags;
57};
58
59static struct redisCommand cmdTable[] = {
c937aa89 60 {"get",2,REDIS_CMD_INLINE},
61 {"set",3,REDIS_CMD_BULK},
62 {"setnx",3,REDIS_CMD_BULK},
63 {"del",2,REDIS_CMD_INLINE},
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},
79 {"sismember",3,REDIS_CMD_BULK},
80 {"scard",2,REDIS_CMD_INLINE},
81 {"sinter",-2,REDIS_CMD_INLINE},
82 {"sinterstore",-3,REDIS_CMD_INLINE},
40d224a9 83 {"sunion",-2,REDIS_CMD_INLINE},
84 {"sunionstore",-3,REDIS_CMD_INLINE},
c937aa89 85 {"smembers",2,REDIS_CMD_INLINE},
86 {"incrby",3,REDIS_CMD_INLINE},
87 {"decrby",3,REDIS_CMD_INLINE},
88 {"randomkey",1,REDIS_CMD_INLINE},
89 {"select",2,REDIS_CMD_INLINE},
90 {"move",3,REDIS_CMD_INLINE},
91 {"rename",3,REDIS_CMD_INLINE},
92 {"renamenx",3,REDIS_CMD_INLINE},
93 {"keys",2,REDIS_CMD_INLINE},
94 {"dbsize",1,REDIS_CMD_INLINE},
95 {"ping",1,REDIS_CMD_INLINE},
96 {"echo",2,REDIS_CMD_BULK},
97 {"save",1,REDIS_CMD_INLINE},
98 {"bgsave",1,REDIS_CMD_INLINE},
99 {"shutdown",1,REDIS_CMD_INLINE},
100 {"lastsave",1,REDIS_CMD_INLINE},
101 {"type",2,REDIS_CMD_INLINE},
102 {"flushdb",1,REDIS_CMD_INLINE},
103 {"flushall",1,REDIS_CMD_INLINE},
104 {"sort",-2,REDIS_CMD_INLINE},
105 {"info",1,REDIS_CMD_INLINE},
106 {"mget",-2,REDIS_CMD_INLINE},
3305306f 107 {"expire",3,REDIS_CMD_INLINE},
ed9b544e 108 {NULL,0,0}
109};
110
c937aa89 111static int cliReadReply(int fd);
112
ed9b544e 113static struct redisCommand *lookupCommand(char *name) {
114 int j = 0;
115 while(cmdTable[j].name != NULL) {
116 if (!strcasecmp(name,cmdTable[j].name)) return &cmdTable[j];
117 j++;
118 }
119 return NULL;
120}
121
122static int cliConnect(void) {
123 char err[ANET_ERR_LEN];
124 int fd;
125
126 fd = anetTcpConnect(err,config.hostip,config.hostport);
127 if (fd == ANET_ERR) {
128 fprintf(stderr,"Connect: %s\n",err);
129 return -1;
130 }
131 anetTcpNoDelay(NULL,fd);
132 return fd;
133}
134
135static sds cliReadLine(int fd) {
136 sds line = sdsempty();
137
138 while(1) {
139 char c;
b91f03a4 140 ssize_t ret;
ed9b544e 141
b91f03a4
LH
142 ret = read(fd,&c,1);
143 if (ret == -1) {
ed9b544e 144 sdsfree(line);
145 return NULL;
b91f03a4 146 } else if ((ret == 0) || (c == '\n')) {
ed9b544e 147 break;
148 } else {
149 line = sdscatlen(line,&c,1);
150 }
151 }
152 return sdstrim(line,"\r\n");
153}
154
c937aa89 155static int cliReadSingleLineReply(int fd) {
ed9b544e 156 sds reply = cliReadLine(fd);
157
158 if (reply == NULL) return 1;
159 printf("%s\n", reply);
ed9b544e 160 return 0;
161}
162
c937aa89 163static int cliReadBulkReply(int fd) {
ed9b544e 164 sds replylen = cliReadLine(fd);
165 char *reply, crlf[2];
c937aa89 166 int bulklen;
ed9b544e 167
168 if (replylen == NULL) return 1;
ed9b544e 169 bulklen = atoi(replylen);
c937aa89 170 if (bulklen == -1) {
ed9b544e 171 sdsfree(replylen);
172 printf("(nil)");
173 return 0;
174 }
ed9b544e 175 reply = zmalloc(bulklen);
176 anetRead(fd,reply,bulklen);
177 anetRead(fd,crlf,2);
178 if (bulklen && fwrite(reply,bulklen,1,stdout) == 0) {
179 zfree(reply);
180 return 1;
181 }
c937aa89 182 if (isatty(fileno(stdout)) && reply[bulklen-1] != '\n')
ed9b544e 183 printf("\n");
184 zfree(reply);
c937aa89 185 return 0;
ed9b544e 186}
187
188static int cliReadMultiBulkReply(int fd) {
189 sds replylen = cliReadLine(fd);
190 int elements, c = 1;
191
192 if (replylen == NULL) return 1;
c937aa89 193 elements = atoi(replylen);
194 if (elements == -1) {
ed9b544e 195 sdsfree(replylen);
196 printf("(nil)\n");
197 return 0;
198 }
c937aa89 199 if (elements == 0) {
200 printf("(empty list or set)\n");
201 }
ed9b544e 202 while(elements--) {
203 printf("%d. ", c);
c937aa89 204 if (cliReadReply(fd)) return 1;
ed9b544e 205 c++;
206 }
207 return 0;
208}
209
c937aa89 210static int cliReadReply(int fd) {
211 char type;
212
213 if (anetRead(fd,&type,1) <= 0) exit(1);
214 switch(type) {
215 case '-':
216 printf("(error) ");
217 cliReadSingleLineReply(fd);
218 return 1;
219 case '+':
220 case ':':
221 return cliReadSingleLineReply(fd);
222 case '$':
223 return cliReadBulkReply(fd);
224 case '*':
225 return cliReadMultiBulkReply(fd);
226 default:
227 printf("protocol error, got '%c' as reply type byte\n", type);
228 return 1;
229 }
230}
231
ed9b544e 232static int cliSendCommand(int argc, char **argv) {
233 struct redisCommand *rc = lookupCommand(argv[0]);
234 int fd, j, retval = 0;
235 sds cmd = sdsempty();
236
237 if (!rc) {
238 fprintf(stderr,"Unknown command '%s'\n",argv[0]);
239 return 1;
240 }
241
242 if ((rc->arity > 0 && argc != rc->arity) ||
a74f2af6 243 (rc->arity < 0 && argc < -rc->arity)) {
ed9b544e 244 fprintf(stderr,"Wrong number of arguments for '%s'\n",rc->name);
245 return 1;
246 }
247 if ((fd = cliConnect()) == -1) return 1;
248
249 /* Build the command to send */
250 for (j = 0; j < argc; j++) {
251 if (j != 0) cmd = sdscat(cmd," ");
252 if (j == argc-1 && rc->flags & REDIS_CMD_BULK) {
253 cmd = sdscatprintf(cmd,"%d",sdslen(argv[j]));
254 } else {
255 cmd = sdscatlen(cmd,argv[j],sdslen(argv[j]));
256 }
257 }
258 cmd = sdscat(cmd,"\r\n");
259 if (rc->flags & REDIS_CMD_BULK) {
260 cmd = sdscatlen(cmd,argv[argc-1],sdslen(argv[argc-1]));
261 cmd = sdscat(cmd,"\r\n");
262 }
263 anetWrite(fd,cmd,sdslen(cmd));
c937aa89 264 retval = cliReadReply(fd);
ed9b544e 265 if (retval) {
266 close(fd);
267 return retval;
268 }
269 close(fd);
270 return 0;
271}
272
273static int parseOptions(int argc, char **argv) {
274 int i;
275
276 for (i = 1; i < argc; i++) {
277 int lastarg = i==argc-1;
278
279 if (!strcmp(argv[i],"-h") && !lastarg) {
280 char *ip = zmalloc(32);
281 if (anetResolve(NULL,argv[i+1],ip) == ANET_ERR) {
282 printf("Can't resolve %s\n", argv[i]);
283 exit(1);
284 }
285 config.hostip = ip;
286 i++;
287 } else if (!strcmp(argv[i],"-p") && !lastarg) {
288 config.hostport = atoi(argv[i+1]);
289 i++;
290 } else {
291 break;
292 }
293 }
294 return i;
295}
296
297static sds readArgFromStdin(void) {
298 char buf[1024];
299 sds arg = sdsempty();
300
301 while(1) {
302 int nread = read(fileno(stdin),buf,1024);
303
304 if (nread == 0) break;
305 else if (nread == -1) {
306 perror("Reading from standard input");
307 exit(1);
308 }
309 arg = sdscatlen(arg,buf,nread);
310 }
311 return arg;
312}
313
314int main(int argc, char **argv) {
315 int firstarg, j;
316 char **argvcopy;
317
318 config.hostip = "127.0.0.1";
319 config.hostport = 6379;
320
321 firstarg = parseOptions(argc,argv);
322 argc -= firstarg;
323 argv += firstarg;
324
325 /* Turn the plain C strings into Sds strings */
326 argvcopy = zmalloc(sizeof(char*)*argc+1);
327 for(j = 0; j < argc; j++)
328 argvcopy[j] = sdsnew(argv[j]);
329
330 /* Read the last argument from stdandard input */
331 if (!isatty(fileno(stdin))) {
332 sds lastarg = readArgFromStdin();
333 argvcopy[argc] = lastarg;
334 argc++;
335 }
336
337 if (argc < 1) {
338 fprintf(stderr, "usage: redis-cli [-h host] [-p port] cmd arg1 arg2 arg3 ... argN\n");
339 fprintf(stderr, "usage: echo \"argN\" | redis-cli [-h host] [-p port] cmd arg1 arg2 ... arg(N-1)\n");
340 fprintf(stderr, "\nIf a pipe from standard input is detected this data is used as last argument.\n\n");
341 fprintf(stderr, "example: cat /etc/passwd | redis-cli set my_passwd\n");
342 fprintf(stderr, "example: redis-cli get my_passwd\n");
343 exit(1);
344 }
345
346 return cliSendCommand(argc, argvcopy);
347}