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