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