]> git.saurik.com Git - redis.git/blob - redis-cli.c
ZSCORE fixed, now returns NULL on missing key or missing element
[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 "fmacros.h"
32
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
45 #define REDIS_CMD_MULTIBULK 3
46
47 #define REDIS_NOTUSED(V) ((void) V)
48
49 static struct config {
50 char *hostip;
51 int hostport;
52 long repeat;
53 } config;
54
55 struct redisCommand {
56 char *name;
57 int arity;
58 int flags;
59 };
60
61 static struct redisCommand cmdTable[] = {
62 {"get",2,REDIS_CMD_INLINE},
63 {"set",3,REDIS_CMD_BULK},
64 {"setnx",3,REDIS_CMD_BULK},
65 {"del",-2,REDIS_CMD_INLINE},
66 {"exists",2,REDIS_CMD_INLINE},
67 {"incr",2,REDIS_CMD_INLINE},
68 {"decr",2,REDIS_CMD_INLINE},
69 {"rpush",3,REDIS_CMD_BULK},
70 {"lpush",3,REDIS_CMD_BULK},
71 {"rpop",2,REDIS_CMD_INLINE},
72 {"lpop",2,REDIS_CMD_INLINE},
73 {"llen",2,REDIS_CMD_INLINE},
74 {"lindex",3,REDIS_CMD_INLINE},
75 {"lset",4,REDIS_CMD_BULK},
76 {"lrange",4,REDIS_CMD_INLINE},
77 {"ltrim",4,REDIS_CMD_INLINE},
78 {"lrem",4,REDIS_CMD_BULK},
79 {"rpoplpush",3,REDIS_CMD_BULK},
80 {"sadd",3,REDIS_CMD_BULK},
81 {"srem",3,REDIS_CMD_BULK},
82 {"smove",4,REDIS_CMD_BULK},
83 {"sismember",3,REDIS_CMD_BULK},
84 {"scard",2,REDIS_CMD_INLINE},
85 {"spop",2,REDIS_CMD_INLINE},
86 {"srandmember",2,REDIS_CMD_INLINE},
87 {"sinter",-2,REDIS_CMD_INLINE},
88 {"sinterstore",-3,REDIS_CMD_INLINE},
89 {"sunion",-2,REDIS_CMD_INLINE},
90 {"sunionstore",-3,REDIS_CMD_INLINE},
91 {"sdiff",-2,REDIS_CMD_INLINE},
92 {"sdiffstore",-3,REDIS_CMD_INLINE},
93 {"smembers",2,REDIS_CMD_INLINE},
94 {"zadd",4,REDIS_CMD_BULK},
95 {"zrem",3,REDIS_CMD_BULK},
96 {"zremrangebyscore",4,REDIS_CMD_INLINE},
97 {"zrange",4,REDIS_CMD_INLINE},
98 {"zrangebyscore",4,REDIS_CMD_INLINE},
99 {"zrevrange",4,REDIS_CMD_INLINE},
100 {"zcard",2,REDIS_CMD_INLINE},
101 {"zscore",3,REDIS_CMD_BULK},
102 {"incrby",3,REDIS_CMD_INLINE},
103 {"decrby",3,REDIS_CMD_INLINE},
104 {"getset",3,REDIS_CMD_BULK},
105 {"randomkey",1,REDIS_CMD_INLINE},
106 {"select",2,REDIS_CMD_INLINE},
107 {"move",3,REDIS_CMD_INLINE},
108 {"rename",3,REDIS_CMD_INLINE},
109 {"renamenx",3,REDIS_CMD_INLINE},
110 {"keys",2,REDIS_CMD_INLINE},
111 {"dbsize",1,REDIS_CMD_INLINE},
112 {"ping",1,REDIS_CMD_INLINE},
113 {"echo",2,REDIS_CMD_BULK},
114 {"save",1,REDIS_CMD_INLINE},
115 {"bgsave",1,REDIS_CMD_INLINE},
116 {"shutdown",1,REDIS_CMD_INLINE},
117 {"lastsave",1,REDIS_CMD_INLINE},
118 {"type",2,REDIS_CMD_INLINE},
119 {"flushdb",1,REDIS_CMD_INLINE},
120 {"flushall",1,REDIS_CMD_INLINE},
121 {"sort",-2,REDIS_CMD_INLINE},
122 {"info",1,REDIS_CMD_INLINE},
123 {"mget",-2,REDIS_CMD_INLINE},
124 {"expire",3,REDIS_CMD_INLINE},
125 {"expireat",3,REDIS_CMD_INLINE},
126 {"ttl",2,REDIS_CMD_INLINE},
127 {"slaveof",3,REDIS_CMD_INLINE},
128 {"debug",-2,REDIS_CMD_INLINE},
129 {"mset",-3,REDIS_CMD_MULTIBULK},
130 {"msetnx",-3,REDIS_CMD_MULTIBULK},
131 {NULL,0,0}
132 };
133
134 static int cliReadReply(int fd);
135
136 static struct redisCommand *lookupCommand(char *name) {
137 int j = 0;
138 while(cmdTable[j].name != NULL) {
139 if (!strcasecmp(name,cmdTable[j].name)) return &cmdTable[j];
140 j++;
141 }
142 return NULL;
143 }
144
145 static int cliConnect(void) {
146 char err[ANET_ERR_LEN];
147 int fd;
148
149 fd = anetTcpConnect(err,config.hostip,config.hostport);
150 if (fd == ANET_ERR) {
151 fprintf(stderr,"Connect: %s\n",err);
152 return -1;
153 }
154 anetTcpNoDelay(NULL,fd);
155 return fd;
156 }
157
158 static sds cliReadLine(int fd) {
159 sds line = sdsempty();
160
161 while(1) {
162 char c;
163 ssize_t ret;
164
165 ret = read(fd,&c,1);
166 if (ret == -1) {
167 sdsfree(line);
168 return NULL;
169 } else if ((ret == 0) || (c == '\n')) {
170 break;
171 } else {
172 line = sdscatlen(line,&c,1);
173 }
174 }
175 return sdstrim(line,"\r\n");
176 }
177
178 static int cliReadSingleLineReply(int fd) {
179 sds reply = cliReadLine(fd);
180
181 if (reply == NULL) return 1;
182 printf("%s\n", reply);
183 return 0;
184 }
185
186 static int cliReadBulkReply(int fd) {
187 sds replylen = cliReadLine(fd);
188 char *reply, crlf[2];
189 int bulklen;
190
191 if (replylen == NULL) return 1;
192 bulklen = atoi(replylen);
193 if (bulklen == -1) {
194 sdsfree(replylen);
195 printf("(nil)\n");
196 return 0;
197 }
198 reply = zmalloc(bulklen);
199 anetRead(fd,reply,bulklen);
200 anetRead(fd,crlf,2);
201 if (bulklen && fwrite(reply,bulklen,1,stdout) == 0) {
202 zfree(reply);
203 return 1;
204 }
205 if (isatty(fileno(stdout)) && reply[bulklen-1] != '\n')
206 printf("\n");
207 zfree(reply);
208 return 0;
209 }
210
211 static int cliReadMultiBulkReply(int fd) {
212 sds replylen = cliReadLine(fd);
213 int elements, c = 1;
214
215 if (replylen == NULL) return 1;
216 elements = atoi(replylen);
217 if (elements == -1) {
218 sdsfree(replylen);
219 printf("(nil)\n");
220 return 0;
221 }
222 if (elements == 0) {
223 printf("(empty list or set)\n");
224 }
225 while(elements--) {
226 printf("%d. ", c);
227 if (cliReadReply(fd)) return 1;
228 c++;
229 }
230 return 0;
231 }
232
233 static int cliReadReply(int fd) {
234 char type;
235
236 if (anetRead(fd,&type,1) <= 0) exit(1);
237 switch(type) {
238 case '-':
239 printf("(error) ");
240 cliReadSingleLineReply(fd);
241 return 1;
242 case '+':
243 return cliReadSingleLineReply(fd);
244 case ':':
245 printf("(integer) ");
246 return cliReadSingleLineReply(fd);
247 case '$':
248 return cliReadBulkReply(fd);
249 case '*':
250 return cliReadMultiBulkReply(fd);
251 default:
252 printf("protocol error, got '%c' as reply type byte\n", type);
253 return 1;
254 }
255 }
256
257 static int cliSendCommand(int argc, char **argv) {
258 struct redisCommand *rc = lookupCommand(argv[0]);
259 int fd, j, retval = 0;
260 sds cmd;
261
262 if (!rc) {
263 fprintf(stderr,"Unknown command '%s'\n",argv[0]);
264 return 1;
265 }
266
267 if ((rc->arity > 0 && argc != rc->arity) ||
268 (rc->arity < 0 && argc < -rc->arity)) {
269 fprintf(stderr,"Wrong number of arguments for '%s'\n",rc->name);
270 return 1;
271 }
272 if ((fd = cliConnect()) == -1) return 1;
273
274 while(config.repeat--) {
275 /* Build the command to send */
276 cmd = sdsempty();
277 if (rc->flags & REDIS_CMD_MULTIBULK) {
278 cmd = sdscatprintf(cmd,"*%d\r\n",argc);
279 for (j = 0; j < argc; j++) {
280 cmd = sdscatprintf(cmd,"$%d\r\n",sdslen(argv[j]));
281 cmd = sdscatlen(cmd,argv[j],sdslen(argv[j]));
282 cmd = sdscatlen(cmd,"\r\n",2);
283 }
284 } else {
285 for (j = 0; j < argc; j++) {
286 if (j != 0) cmd = sdscat(cmd," ");
287 if (j == argc-1 && rc->flags & REDIS_CMD_BULK) {
288 cmd = sdscatprintf(cmd,"%d",sdslen(argv[j]));
289 } else {
290 cmd = sdscatlen(cmd,argv[j],sdslen(argv[j]));
291 }
292 }
293 cmd = sdscat(cmd,"\r\n");
294 if (rc->flags & REDIS_CMD_BULK) {
295 cmd = sdscatlen(cmd,argv[argc-1],sdslen(argv[argc-1]));
296 cmd = sdscatlen(cmd,"\r\n",2);
297 }
298 }
299 anetWrite(fd,cmd,sdslen(cmd));
300 sdsfree(cmd);
301 retval = cliReadReply(fd);
302 if (retval) {
303 close(fd);
304 return retval;
305 }
306 }
307 close(fd);
308 return 0;
309 }
310
311 static int parseOptions(int argc, char **argv) {
312 int i;
313
314 for (i = 1; i < argc; i++) {
315 int lastarg = i==argc-1;
316
317 if (!strcmp(argv[i],"-h") && !lastarg) {
318 char *ip = zmalloc(32);
319 if (anetResolve(NULL,argv[i+1],ip) == ANET_ERR) {
320 printf("Can't resolve %s\n", argv[i]);
321 exit(1);
322 }
323 config.hostip = ip;
324 i++;
325 } else if (!strcmp(argv[i],"-p") && !lastarg) {
326 config.hostport = atoi(argv[i+1]);
327 i++;
328 } else if (!strcmp(argv[i],"-r") && !lastarg) {
329 config.repeat = strtoll(argv[i+1],NULL,10);
330 i++;
331 } else {
332 break;
333 }
334 }
335 return i;
336 }
337
338 static sds readArgFromStdin(void) {
339 char buf[1024];
340 sds arg = sdsempty();
341
342 while(1) {
343 int nread = read(fileno(stdin),buf,1024);
344
345 if (nread == 0) break;
346 else if (nread == -1) {
347 perror("Reading from standard input");
348 exit(1);
349 }
350 arg = sdscatlen(arg,buf,nread);
351 }
352 return arg;
353 }
354
355 int main(int argc, char **argv) {
356 int firstarg, j;
357 char **argvcopy;
358 struct redisCommand *rc;
359
360 config.hostip = "127.0.0.1";
361 config.hostport = 6379;
362 config.repeat = 1;
363
364 firstarg = parseOptions(argc,argv);
365 argc -= firstarg;
366 argv += firstarg;
367
368 /* Turn the plain C strings into Sds strings */
369 argvcopy = zmalloc(sizeof(char*)*argc+1);
370 for(j = 0; j < argc; j++)
371 argvcopy[j] = sdsnew(argv[j]);
372
373 if (argc < 1) {
374 fprintf(stderr, "usage: redis-cli [-h host] [-p port] [-r repeat_times] cmd arg1 arg2 arg3 ... argN\n");
375 fprintf(stderr, "usage: echo \"argN\" | redis-cli [-h host] [-p port] -r [repeat_times] cmd arg1 arg2 ... arg(N-1)\n");
376 fprintf(stderr, "\nIf a pipe from standard input is detected this data is used as last argument.\n\n");
377 fprintf(stderr, "example: cat /etc/passwd | redis-cli set my_passwd\n");
378 fprintf(stderr, "example: redis-cli get my_passwd\n");
379 fprintf(stderr, "example: redis-cli -r 100 lpush mylist x\n");
380 exit(1);
381 }
382
383 /* Read the last argument from stdandard input if needed */
384 if ((rc = lookupCommand(argv[0])) != NULL) {
385 if (rc->arity > 0 && argc == rc->arity-1) {
386 sds lastarg = readArgFromStdin();
387 argvcopy[argc] = lastarg;
388 argc++;
389 }
390 }
391
392 return cliSendCommand(argc, argvcopy);
393 }