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