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