Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 517 lines (463 sloc) 15.48 kb
ed9b544 @antirez first commit
authored
1 /* Redis CLI (command line interface)
2 *
12d090d @antirez A problem with replication with multiple slaves connectiong to a single ...
authored
3 * Copyright (c) 2009-2010, Salvatore Sanfilippo <antirez at gmail dot com>
ed9b544 @antirez first commit
authored
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
23d4709 @antirez feature macros defined to play well with C99
authored
31 #include "fmacros.h"
32
ed9b544 @antirez first commit
authored
33 #include <stdio.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include <unistd.h>
a88a2af @antirez Initial support for quoted strings in redis-cli
authored
37 #include <ctype.h>
32241a0 @antirez redis-cli now supports automatically reconnection in interactive mode
authored
38 #include <errno.h>
eff09a7 @antirez redis-cli completely replaced with the version in Redis master
authored
39 #include <sys/stat.h>
ed9b544 @antirez first commit
authored
40
41 #include "anet.h"
42 #include "sds.h"
43 #include "adlist.h"
44 #include "zmalloc.h"
cf87ebf @soveran Use linenoise for line editing on redis-cli.
soveran authored
45 #include "linenoise.h"
ed9b544 @antirez first commit
authored
46
47 #define REDIS_CMD_INLINE 1
48 #define REDIS_CMD_BULK 2
e17e0b0 @antirez fixed a subtle bug in redis-cli not having visible effects
authored
49 #define REDIS_CMD_MULTIBULK 4
ed9b544 @antirez first commit
authored
50
51 #define REDIS_NOTUSED(V) ((void) V)
52
53 static struct config {
54 char *hostip;
55 int hostport;
5762b7f @antirez redis-cli now accepts a -r (repeat) switch. Still there is a memory leak...
authored
56 long repeat;
62e920d can select db num
ian authored
57 int dbnum;
6cf5882 Add interactive mode to redis-cli.
Michel Martens & Damian Janowski authored
58 int interactive;
36e5db6 @tizoc Don't rely on cliReadReply being able to return on shutdown
tizoc authored
59 int shutdown;
249c3a7 @antirez Minimal support for subscribe/psubscribe in redis-cli
authored
60 int monitor_mode;
61 int pubsub_mode;
eff09a7 @antirez redis-cli completely replaced with the version in Redis master
authored
62 int raw_output; /* output mode per command */
63 int tty; /* flag for default output format */
64 char mb_sep;
288799e @antirez Fixed redis-cli auth code
authored
65 char *auth;
eff09a7 @antirez redis-cli completely replaced with the version in Redis master
authored
66 char *historyfile;
ed9b544 @antirez first commit
authored
67 } config;
68
c937aa8 @antirez Server replies now in the new format, test-redis.tcl and redis-cli modif...
authored
69 static int cliReadReply(int fd);
a915827 @antirez now redis-cli understands -h
authored
70 static void usage();
c937aa8 @antirez Server replies now in the new format, test-redis.tcl and redis-cli modif...
authored
71
32241a0 @antirez redis-cli now supports automatically reconnection in interactive mode
authored
72 /* Connect to the client. If force is not zero the connection is performed
73 * even if there is already a connected socket. */
74 static int cliConnect(int force) {
ed9b544 @antirez first commit
authored
75 char err[ANET_ERR_LEN];
6fa2462 Add support for MULTI/EXEC.
Damian Janowski & Michel Martens authored
76 static int fd = ANET_ERR;
ed9b544 @antirez first commit
authored
77
32241a0 @antirez redis-cli now supports automatically reconnection in interactive mode
authored
78 if (fd == ANET_ERR || force) {
79 if (force) close(fd);
6fa2462 Add support for MULTI/EXEC.
Damian Janowski & Michel Martens authored
80 fd = anetTcpConnect(err,config.hostip,config.hostport);
81 if (fd == ANET_ERR) {
82 fprintf(stderr, "Could not connect to Redis at %s:%d: %s", config.hostip, config.hostport, err);
83 return -1;
84 }
85 anetTcpNoDelay(NULL,fd);
ed9b544 @antirez first commit
authored
86 }
87 return fd;
88 }
89
90 static sds cliReadLine(int fd) {
91 sds line = sdsempty();
92
93 while(1) {
94 char c;
b91f03a @lucsky Fixed redis-cli readLine loop to correctly handle EOF.
lucsky authored
95 ssize_t ret;
ed9b544 @antirez first commit
authored
96
b91f03a @lucsky Fixed redis-cli readLine loop to correctly handle EOF.
lucsky authored
97 ret = read(fd,&c,1);
eff09a7 @antirez redis-cli completely replaced with the version in Redis master
authored
98 if (ret <= 0) {
ed9b544 @antirez first commit
authored
99 sdsfree(line);
100 return NULL;
b91f03a @lucsky Fixed redis-cli readLine loop to correctly handle EOF.
lucsky authored
101 } else if ((ret == 0) || (c == '\n')) {
ed9b544 @antirez first commit
authored
102 break;
103 } else {
104 line = sdscatlen(line,&c,1);
105 }
106 }
107 return sdstrim(line,"\r\n");
108 }
109
62e920d can select db num
ian authored
110 static int cliReadSingleLineReply(int fd, int quiet) {
ed9b544 @antirez first commit
authored
111 sds reply = cliReadLine(fd);
112
113 if (reply == NULL) return 1;
62e920d can select db num
ian authored
114 if (!quiet)
eff09a7 @antirez redis-cli completely replaced with the version in Redis master
authored
115 printf("%s", reply);
621d5c1 @antirez ae.c event loop does no longer support exception notifications, as they ...
authored
116 sdsfree(reply);
ed9b544 @antirez first commit
authored
117 return 0;
118 }
119
21cdc9f @antirez redis-cli now is able to also output the string representation instead o...
authored
120 static void printStringRepr(char *s, int len) {
121 printf("\"");
122 while(len--) {
123 switch(*s) {
124 case '\\':
125 case '"':
126 printf("\\%c",*s);
127 break;
128 case '\n': printf("\\n"); break;
129 case '\r': printf("\\r"); break;
130 case '\t': printf("\\t"); break;
131 case '\a': printf("\\a"); break;
132 case '\b': printf("\\b"); break;
133 default:
134 if (isprint(*s))
135 printf("%c",*s);
136 else
137 printf("\\x%02x",(unsigned char)*s);
138 break;
139 }
140 s++;
141 }
eff09a7 @antirez redis-cli completely replaced with the version in Redis master
authored
142 printf("\"");
21cdc9f @antirez redis-cli now is able to also output the string representation instead o...
authored
143 }
144
c937aa8 @antirez Server replies now in the new format, test-redis.tcl and redis-cli modif...
authored
145 static int cliReadBulkReply(int fd) {
ed9b544 @antirez first commit
authored
146 sds replylen = cliReadLine(fd);
147 char *reply, crlf[2];
c937aa8 @antirez Server replies now in the new format, test-redis.tcl and redis-cli modif...
authored
148 int bulklen;
ed9b544 @antirez first commit
authored
149
150 if (replylen == NULL) return 1;
151 bulklen = atoi(replylen);
c937aa8 @antirez Server replies now in the new format, test-redis.tcl and redis-cli modif...
authored
152 if (bulklen == -1) {
ed9b544 @antirez first commit
authored
153 sdsfree(replylen);
060f6be @antirez Fixed issue 92 in redis: redis-cli (nil) return value lacks CR/LF
authored
154 printf("(nil)\n");
ed9b544 @antirez first commit
authored
155 return 0;
156 }
157 reply = zmalloc(bulklen);
158 anetRead(fd,reply,bulklen);
159 anetRead(fd,crlf,2);
eff09a7 @antirez redis-cli completely replaced with the version in Redis master
authored
160 if (config.raw_output || !config.tty) {
21cdc9f @antirez redis-cli now is able to also output the string representation instead o...
authored
161 if (bulklen && fwrite(reply,bulklen,1,stdout) == 0) {
162 zfree(reply);
163 return 1;
164 }
165 } else {
166 /* If you are producing output for the standard output we want
167 * a more interesting output with quoted characters and so forth */
168 printStringRepr(reply,bulklen);
ed9b544 @antirez first commit
authored
169 }
170 zfree(reply);
c937aa8 @antirez Server replies now in the new format, test-redis.tcl and redis-cli modif...
authored
171 return 0;
ed9b544 @antirez first commit
authored
172 }
173
174 static int cliReadMultiBulkReply(int fd) {
175 sds replylen = cliReadLine(fd);
176 int elements, c = 1;
eff09a7 @antirez redis-cli completely replaced with the version in Redis master
authored
177 int retval = 0;
ed9b544 @antirez first commit
authored
178
179 if (replylen == NULL) return 1;
c937aa8 @antirez Server replies now in the new format, test-redis.tcl and redis-cli modif...
authored
180 elements = atoi(replylen);
181 if (elements == -1) {
ed9b544 @antirez first commit
authored
182 sdsfree(replylen);
183 printf("(nil)\n");
184 return 0;
185 }
c937aa8 @antirez Server replies now in the new format, test-redis.tcl and redis-cli modif...
authored
186 if (elements == 0) {
187 printf("(empty list or set)\n");
188 }
ed9b544 @antirez first commit
authored
189 while(elements--) {
eff09a7 @antirez redis-cli completely replaced with the version in Redis master
authored
190 if (config.tty) printf("%d. ", c);
191 if (cliReadReply(fd)) retval = 1;
192 if (elements) printf("%c",config.mb_sep);
ed9b544 @antirez first commit
authored
193 c++;
194 }
eff09a7 @antirez redis-cli completely replaced with the version in Redis master
authored
195 return retval;
ed9b544 @antirez first commit
authored
196 }
197
c937aa8 @antirez Server replies now in the new format, test-redis.tcl and redis-cli modif...
authored
198 static int cliReadReply(int fd) {
199 char type;
32241a0 @antirez redis-cli now supports automatically reconnection in interactive mode
authored
200 int nread;
c937aa8 @antirez Server replies now in the new format, test-redis.tcl and redis-cli modif...
authored
201
32241a0 @antirez redis-cli now supports automatically reconnection in interactive mode
authored
202 if ((nread = anetRead(fd,&type,1)) <= 0) {
36e5db6 @tizoc Don't rely on cliReadReply being able to return on shutdown
tizoc authored
203 if (config.shutdown) return 0;
32241a0 @antirez redis-cli now supports automatically reconnection in interactive mode
authored
204 if (config.interactive &&
205 (nread == 0 || (nread == -1 && errno == ECONNRESET)))
206 {
207 return ECONNRESET;
208 } else {
209 printf("I/O error while reading from socket: %s",strerror(errno));
210 exit(1);
211 }
36e5db6 @tizoc Don't rely on cliReadReply being able to return on shutdown
tizoc authored
212 }
c937aa8 @antirez Server replies now in the new format, test-redis.tcl and redis-cli modif...
authored
213 switch(type) {
214 case '-':
eff09a7 @antirez redis-cli completely replaced with the version in Redis master
authored
215 if (config.tty) printf("(error) ");
62e920d can select db num
ian authored
216 cliReadSingleLineReply(fd,0);
c937aa8 @antirez Server replies now in the new format, test-redis.tcl and redis-cli modif...
authored
217 return 1;
218 case '+':
62e920d can select db num
ian authored
219 return cliReadSingleLineReply(fd,0);
c937aa8 @antirez Server replies now in the new format, test-redis.tcl and redis-cli modif...
authored
220 case ':':
eff09a7 @antirez redis-cli completely replaced with the version in Redis master
authored
221 if (config.tty) printf("(integer) ");
62e920d can select db num
ian authored
222 return cliReadSingleLineReply(fd,0);
c937aa8 @antirez Server replies now in the new format, test-redis.tcl and redis-cli modif...
authored
223 case '$':
224 return cliReadBulkReply(fd);
225 case '*':
226 return cliReadMultiBulkReply(fd);
227 default:
eff09a7 @antirez redis-cli completely replaced with the version in Redis master
authored
228 printf("protocol error, got '%c' as reply type byte", type);
c937aa8 @antirez Server replies now in the new format, test-redis.tcl and redis-cli modif...
authored
229 return 1;
230 }
231 }
232
6cf5882 Add interactive mode to redis-cli.
Michel Martens & Damian Janowski authored
233 static int selectDb(int fd) {
62e920d can select db num
ian authored
234 int retval;
235 sds cmd;
236 char type;
237
238 if (config.dbnum == 0)
239 return 0;
240
241 cmd = sdsempty();
242 cmd = sdscatprintf(cmd,"SELECT %d\r\n",config.dbnum);
243 anetWrite(fd,cmd,sdslen(cmd));
244 anetRead(fd,&type,1);
245 if (type <= 0 || type != '+') return 1;
246 retval = cliReadSingleLineReply(fd,1);
247 if (retval) {
6cf5882 Add interactive mode to redis-cli.
Michel Martens & Damian Janowski authored
248 return retval;
62e920d can select db num
ian authored
249 }
250 return 0;
251 }
252
eff09a7 @antirez redis-cli completely replaced with the version in Redis master
authored
253 static void showInteractiveHelp(void) {
254 printf(
255 "\n"
256 "Welcome to redis-cli 2.0.0!\n"
257 "Just type any valid Redis command to see a pretty printed output.\n"
258 "\n"
259 "It is possible to quote strings, like in:\n"
260 " set \"my key\" \"some string \\xff\\n\"\n"
261 "\n"
262 "You can find a list of valid Redis commands at\n"
263 " http://code.google.com/p/redis/wiki/CommandReference\n"
264 "\n"
265 "Note: redis-cli supports line editing, use up/down arrows for history."
266 "\n\n");
267 }
268
aab055a Fix authentication for redis-cli on non-interactive mode.
Michel Martens & Damian Janowski authored
269 static int cliSendCommand(int argc, char **argv, int repeat) {
3294ef1 @pietern require the flag "-c" to be used for redis-cli to read the last argument...
pietern authored
270 char *command = argv[0];
ed9b544 @antirez first commit
authored
271 int fd, j, retval = 0;
5762b7f @antirez redis-cli now accepts a -r (repeat) switch. Still there is a memory leak...
authored
272 sds cmd;
ed9b544 @antirez first commit
authored
273
3294ef1 @pietern require the flag "-c" to be used for redis-cli to read the last argument...
pietern authored
274 config.raw_output = !strcasecmp(command,"info");
eff09a7 @antirez redis-cli completely replaced with the version in Redis master
authored
275 if (!strcasecmp(command,"help")) {
276 showInteractiveHelp();
277 return 0;
278 }
3294ef1 @pietern require the flag "-c" to be used for redis-cli to read the last argument...
pietern authored
279 if (!strcasecmp(command,"shutdown")) config.shutdown = 1;
280 if (!strcasecmp(command,"monitor")) config.monitor_mode = 1;
281 if (!strcasecmp(command,"subscribe") ||
282 !strcasecmp(command,"psubscribe")) config.pubsub_mode = 1;
32241a0 @antirez redis-cli now supports automatically reconnection in interactive mode
authored
283 if ((fd = cliConnect(0)) == -1) return 1;
ed9b544 @antirez first commit
authored
284
62e920d can select db num
ian authored
285 /* Select db number */
286 retval = selectDb(fd);
287 if (retval) {
288 fprintf(stderr,"Error setting DB num\n");
289 return 1;
290 }
6cf5882 Add interactive mode to redis-cli.
Michel Martens & Damian Janowski authored
291
2691b01 @pietern build command outside while loop
pietern authored
292 /* Build the command to send */
293 cmd = sdscatprintf(sdsempty(),"*%d\r\n",argc);
294 for (j = 0; j < argc; j++) {
295 cmd = sdscatprintf(cmd,"$%lu\r\n",
296 (unsigned long)sdslen(argv[j]));
297 cmd = sdscatlen(cmd,argv[j],sdslen(argv[j]));
298 cmd = sdscatlen(cmd,"\r\n",2);
299 }
300
aab055a Fix authentication for redis-cli on non-interactive mode.
Michel Martens & Damian Janowski authored
301 while(repeat--) {
5762b7f @antirez redis-cli now accepts a -r (repeat) switch. Still there is a memory leak...
authored
302 anetWrite(fd,cmd,sdslen(cmd));
249c3a7 @antirez Minimal support for subscribe/psubscribe in redis-cli
authored
303 while (config.monitor_mode) {
eff09a7 @antirez redis-cli completely replaced with the version in Redis master
authored
304 if (cliReadSingleLineReply(fd,0)) exit(1);
305 printf("\n");
621d5c1 @antirez ae.c event loop does no longer support exception notifications, as they ...
authored
306 }
307
249c3a7 @antirez Minimal support for subscribe/psubscribe in redis-cli
authored
308 if (config.pubsub_mode) {
309 printf("Reading messages... (press Ctrl-c to quit)\n");
310 while (1) {
311 cliReadReply(fd);
eff09a7 @antirez redis-cli completely replaced with the version in Redis master
authored
312 printf("\n\n");
249c3a7 @antirez Minimal support for subscribe/psubscribe in redis-cli
authored
313 }
314 }
315
5762b7f @antirez redis-cli now accepts a -r (repeat) switch. Still there is a memory leak...
authored
316 retval = cliReadReply(fd);
eff09a7 @antirez redis-cli completely replaced with the version in Redis master
authored
317 if (!config.raw_output && config.tty) printf("\n");
318 if (retval) return retval;
ed9b544 @antirez first commit
authored
319 }
320 return 0;
321 }
322
323 static int parseOptions(int argc, char **argv) {
324 int i;
325
326 for (i = 1; i < argc; i++) {
327 int lastarg = i==argc-1;
6cf5882 Add interactive mode to redis-cli.
Michel Martens & Damian Janowski authored
328
ed9b544 @antirez first commit
authored
329 if (!strcmp(argv[i],"-h") && !lastarg) {
330 char *ip = zmalloc(32);
331 if (anetResolve(NULL,argv[i+1],ip) == ANET_ERR) {
332 printf("Can't resolve %s\n", argv[i]);
333 exit(1);
334 }
335 config.hostip = ip;
336 i++;
a915827 @antirez now redis-cli understands -h
authored
337 } else if (!strcmp(argv[i],"-h") && lastarg) {
338 usage();
ed9b544 @antirez first commit
authored
339 } else if (!strcmp(argv[i],"-p") && !lastarg) {
340 config.hostport = atoi(argv[i+1]);
341 i++;
5762b7f @antirez redis-cli now accepts a -r (repeat) switch. Still there is a memory leak...
authored
342 } else if (!strcmp(argv[i],"-r") && !lastarg) {
343 config.repeat = strtoll(argv[i+1],NULL,10);
344 i++;
62e920d can select db num
ian authored
345 } else if (!strcmp(argv[i],"-n") && !lastarg) {
346 config.dbnum = atoi(argv[i+1]);
347 i++;
fdfdae0 Added Authentication to redis-cli.c using -a switch
root authored
348 } else if (!strcmp(argv[i],"-a") && !lastarg) {
288799e @antirez Fixed redis-cli auth code
authored
349 config.auth = argv[i+1];
fdfdae0 Added Authentication to redis-cli.c using -a switch
root authored
350 i++;
6cf5882 Add interactive mode to redis-cli.
Michel Martens & Damian Janowski authored
351 } else if (!strcmp(argv[i],"-i")) {
eff09a7 @antirez redis-cli completely replaced with the version in Redis master
authored
352 fprintf(stderr,
353 "Starting interactive mode using -i is deprecated. Interactive mode is started\n"
354 "by default when redis-cli is executed without a command to execute.\n"
355 );
3294ef1 @pietern require the flag "-c" to be used for redis-cli to read the last argument...
pietern authored
356 } else if (!strcmp(argv[i],"-c")) {
eff09a7 @antirez redis-cli completely replaced with the version in Redis master
authored
357 fprintf(stderr,
358 "Reading last argument from standard input using -c is deprecated.\n"
359 "When standard input is connected to a pipe or regular file, it is\n"
360 "automatically used as last argument.\n"
361 );
362 } else if (!strcmp(argv[i],"-v")) {
363 printf("redis-cli shipped with Redis verison %s\n", "2.0.0");
364 exit(0);
ed9b544 @antirez first commit
authored
365 } else {
366 break;
367 }
368 }
369 return i;
370 }
371
372 static sds readArgFromStdin(void) {
373 char buf[1024];
374 sds arg = sdsempty();
375
376 while(1) {
377 int nread = read(fileno(stdin),buf,1024);
378
379 if (nread == 0) break;
380 else if (nread == -1) {
381 perror("Reading from standard input");
382 exit(1);
383 }
384 arg = sdscatlen(arg,buf,nread);
385 }
386 return arg;
387 }
388
a915827 @antirez now redis-cli understands -h
authored
389 static void usage() {
eff09a7 @antirez redis-cli completely replaced with the version in Redis master
authored
390 fprintf(stderr, "usage: redis-cli [-iv] [-h host] [-p port] [-a authpw] [-r repeat_times] [-n db_num] cmd arg1 arg2 arg3 ... argN\n");
391 fprintf(stderr, "usage: echo \"argN\" | redis-cli [-h host] [-p port] [-a authpw] [-r repeat_times] [-n db_num] cmd arg1 arg2 ... arg(N-1)\n");
a915827 @antirez now redis-cli understands -h
authored
392 fprintf(stderr, "\nIf a pipe from standard input is detected this data is used as last argument.\n\n");
393 fprintf(stderr, "example: cat /etc/passwd | redis-cli set my_passwd\n");
394 fprintf(stderr, "example: redis-cli get my_passwd\n");
395 fprintf(stderr, "example: redis-cli -r 100 lpush mylist x\n");
d239ec5 @antirez redis-cli now runs in interactive mode if no command is provided
authored
396 fprintf(stderr, "\nRun in interactive mode: redis-cli -i or just don't pass any command\n");
a915827 @antirez now redis-cli understands -h
authored
397 exit(1);
398 }
399
6cf5882 Add interactive mode to redis-cli.
Michel Martens & Damian Janowski authored
400 /* Turn the plain C strings into Sds strings */
401 static char **convertToSds(int count, char** args) {
402 int j;
3294ef1 @pietern require the flag "-c" to be used for redis-cli to read the last argument...
pietern authored
403 char **sds = zmalloc(sizeof(char*)*count);
6cf5882 Add interactive mode to redis-cli.
Michel Martens & Damian Janowski authored
404
405 for(j = 0; j < count; j++)
406 sds[j] = sdsnew(args[j]);
407
408 return sds;
409 }
410
a88a2af @antirez Initial support for quoted strings in redis-cli
authored
411 #define LINE_BUFLEN 4096
6cf5882 Add interactive mode to redis-cli.
Michel Martens & Damian Janowski authored
412 static void repl() {
a88a2af @antirez Initial support for quoted strings in redis-cli
authored
413 int argc, j;
eff09a7 @antirez redis-cli completely replaced with the version in Redis master
authored
414 char *line;
415 sds *argv;
6cf5882 Add interactive mode to redis-cli.
Michel Martens & Damian Janowski authored
416
eff09a7 @antirez redis-cli completely replaced with the version in Redis master
authored
417 config.interactive = 1;
bc86d88 @antirez redis-cli prompt is now redis>
authored
418 while((line = linenoise("redis> ")) != NULL) {
cf87ebf @soveran Use linenoise for line editing on redis-cli.
soveran authored
419 if (line[0] != '\0') {
eff09a7 @antirez redis-cli completely replaced with the version in Redis master
authored
420 argv = sdssplitargs(line,&argc);
a88a2af @antirez Initial support for quoted strings in redis-cli
authored
421 linenoiseHistoryAdd(line);
eff09a7 @antirez redis-cli completely replaced with the version in Redis master
authored
422 if (config.historyfile) linenoiseHistorySave(config.historyfile);
423 if (argv == NULL) {
424 printf("Invalid argument(s)\n");
425 continue;
426 } else if (argc > 0) {
a88a2af @antirez Initial support for quoted strings in redis-cli
authored
427 if (strcasecmp(argv[0],"quit") == 0 ||
428 strcasecmp(argv[0],"exit") == 0)
32241a0 @antirez redis-cli now supports automatically reconnection in interactive mode
authored
429 {
430 exit(0);
431 } else {
432 int err;
433
434 if ((err = cliSendCommand(argc, argv, 1)) != 0) {
435 if (err == ECONNRESET) {
436 printf("Reconnecting... ");
437 fflush(stdout);
438 if (cliConnect(1) == -1) exit(1);
439 printf("OK\n");
440 cliSendCommand(argc,argv,1);
441 }
442 }
443 }
a88a2af @antirez Initial support for quoted strings in redis-cli
authored
444 }
445 /* Free the argument vector */
446 for (j = 0; j < argc; j++)
447 sdsfree(argv[j]);
8ff6a48 Fixed a redis-cli bug, was using free instead of zfree call
antirez authored
448 zfree(argv);
6cf5882 Add interactive mode to redis-cli.
Michel Martens & Damian Janowski authored
449 }
a88a2af @antirez Initial support for quoted strings in redis-cli
authored
450 /* linenoise() returns malloc-ed lines like readline() */
cf87ebf @soveran Use linenoise for line editing on redis-cli.
soveran authored
451 free(line);
6cf5882 Add interactive mode to redis-cli.
Michel Martens & Damian Janowski authored
452 }
453 exit(0);
454 }
455
eff09a7 @antirez redis-cli completely replaced with the version in Redis master
authored
456 static int noninteractive(int argc, char **argv) {
457 int retval = 0;
458 struct stat s;
459 fstat(fileno(stdin), &s);
460 if (S_ISFIFO(s.st_mode) || S_ISREG(s.st_mode)) { /* pipe, regular file */
461 argv = zrealloc(argv, (argc+1)*sizeof(char*));
462 argv[argc] = readArgFromStdin();
463 retval = cliSendCommand(argc+1, argv, config.repeat);
464 } else {
465 /* stdin is probably a tty, can be tested with S_ISCHR(s.st_mode) */
466 retval = cliSendCommand(argc, argv, config.repeat);
467 }
468 return retval;
469 }
470
ed9b544 @antirez first commit
authored
471 int main(int argc, char **argv) {
6cf5882 Add interactive mode to redis-cli.
Michel Martens & Damian Janowski authored
472 int firstarg;
ed9b544 @antirez first commit
authored
473
474 config.hostip = "127.0.0.1";
475 config.hostport = 6379;
5762b7f @antirez redis-cli now accepts a -r (repeat) switch. Still there is a memory leak...
authored
476 config.repeat = 1;
62e920d can select db num
ian authored
477 config.dbnum = 0;
6cf5882 Add interactive mode to redis-cli.
Michel Martens & Damian Janowski authored
478 config.interactive = 0;
eff09a7 @antirez redis-cli completely replaced with the version in Redis master
authored
479 config.shutdown = 0;
249c3a7 @antirez Minimal support for subscribe/psubscribe in redis-cli
authored
480 config.monitor_mode = 0;
481 config.pubsub_mode = 0;
f40b035 @antirez redis-cli INFO output format is now raw again
authored
482 config.raw_output = 0;
288799e @antirez Fixed redis-cli auth code
authored
483 config.auth = NULL;
eff09a7 @antirez redis-cli completely replaced with the version in Redis master
authored
484 config.historyfile = NULL;
485 config.tty = isatty(fileno(stdout)) || (getenv("FAKETTY") != NULL);
486 config.mb_sep = '\n';
487
488 if (getenv("HOME") != NULL) {
489 config.historyfile = malloc(256);
490 snprintf(config.historyfile,256,"%s/.rediscli_history",getenv("HOME"));
491 linenoiseHistoryLoad(config.historyfile);
492 }
ed9b544 @antirez first commit
authored
493
494 firstarg = parseOptions(argc,argv);
495 argc -= firstarg;
496 argv += firstarg;
497
aab055a Fix authentication for redis-cli on non-interactive mode.
Michel Martens & Damian Janowski authored
498 if (config.auth != NULL) {
499 char *authargv[2];
eff09a7 @antirez redis-cli completely replaced with the version in Redis master
authored
500 int dbnum = config.dbnum;
aab055a Fix authentication for redis-cli on non-interactive mode.
Michel Martens & Damian Janowski authored
501
eff09a7 @antirez redis-cli completely replaced with the version in Redis master
authored
502 /* We need to save the real configured database number and set it to
503 * zero here, otherwise cliSendCommand() will try to perform the
504 * SELECT command before the authentication, and it will fail. */
505 config.dbnum = 0;
aab055a Fix authentication for redis-cli on non-interactive mode.
Michel Martens & Damian Janowski authored
506 authargv[0] = "AUTH";
507 authargv[1] = config.auth;
508 cliSendCommand(2, convertToSds(2, authargv), 1);
eff09a7 @antirez redis-cli completely replaced with the version in Redis master
authored
509 config.dbnum = dbnum; /* restore the right DB number */
aab055a Fix authentication for redis-cli on non-interactive mode.
Michel Martens & Damian Janowski authored
510 }
511
eff09a7 @antirez redis-cli completely replaced with the version in Redis master
authored
512 /* Start interactive mode when no command is provided */
513 if (argc == 0) repl();
514 /* Otherwise, we have some arguments to execute */
515 return noninteractive(argc,convertToSds(argc,argv));
ed9b544 @antirez first commit
authored
516 }
Something went wrong with that request. Please try again.