]>
Commit | Line | Data |
---|---|---|
ed9b544e | 1 | /* Redis CLI (command line interface) |
2 | * | |
12d090d2 | 3 | * Copyright (c) 2009-2010, Salvatore Sanfilippo <antirez at gmail dot com> |
ed9b544e | 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" |
185cabda | 32 | #include "version.h" |
23d4709d | 33 | |
ed9b544e | 34 | #include <stdio.h> |
35 | #include <string.h> | |
36 | #include <stdlib.h> | |
37 | #include <unistd.h> | |
a88a2af6 | 38 | #include <ctype.h> |
c0b3d423 | 39 | #include <errno.h> |
b4b62c34 | 40 | #include <sys/stat.h> |
3ce014c7 | 41 | #include <sys/time.h> |
41945ba6 | 42 | #include <assert.h> |
ed9b544e | 43 | |
7fc4ce13 | 44 | #include "hiredis.h" |
ed9b544e | 45 | #include "sds.h" |
ed9b544e | 46 | #include "zmalloc.h" |
cf87ebf2 | 47 | #include "linenoise.h" |
5397f2b5 | 48 | #include "help.h" |
ed9b544e | 49 | |
ed9b544e | 50 | #define REDIS_NOTUSED(V) ((void) V) |
51 | ||
7fc4ce13 | 52 | static redisContext *context; |
ed9b544e | 53 | static struct config { |
54 | char *hostip; | |
55 | int hostport; | |
7e91f971 | 56 | char *hostsocket; |
5762b7f0 | 57 | long repeat; |
62e920df | 58 | int dbnum; |
5d15b520 | 59 | int interactive; |
36e5db6d | 60 | int shutdown; |
249c3a7d | 61 | int monitor_mode; |
62 | int pubsub_mode; | |
bc63407b | 63 | int stdinarg; /* get last arg from stdin. (-x option) */ |
288799e0 | 64 | char *auth; |
65add0a3 PN |
65 | int raw_output; /* output mode per command */ |
66 | sds mb_delim; | |
3f4eef21 | 67 | char prompt[32]; |
ed9b544e | 68 | } config; |
69 | ||
a9158272 | 70 | static void usage(); |
11fd0c42 | 71 | char *redisGitSHA1(void); |
c392edf5 | 72 | char *redisGitDirty(void); |
c937aa89 | 73 | |
3ce014c7 | 74 | /*------------------------------------------------------------------------------ |
75 | * Utility functions | |
76 | *--------------------------------------------------------------------------- */ | |
77 | ||
78 | static long long mstime(void) { | |
79 | struct timeval tv; | |
80 | long long mst; | |
81 | ||
82 | gettimeofday(&tv, NULL); | |
83 | mst = ((long)tv.tv_sec)*1000; | |
84 | mst += tv.tv_usec/1000; | |
85 | return mst; | |
86 | } | |
87 | ||
3f4eef21 PN |
88 | static void cliRefreshPrompt(void) { |
89 | if (config.dbnum == 0) | |
90 | snprintf(config.prompt,sizeof(config.prompt),"redis> "); | |
91 | else | |
92 | snprintf(config.prompt,sizeof(config.prompt),"redis:%d> ",config.dbnum); | |
93 | } | |
94 | ||
a2a69d58 PN |
95 | /*------------------------------------------------------------------------------ |
96 | * Help functions | |
97 | *--------------------------------------------------------------------------- */ | |
98 | ||
b2cc45bf PN |
99 | #define CLI_HELP_COMMAND 1 |
100 | #define CLI_HELP_GROUP 2 | |
101 | ||
102 | typedef struct { | |
103 | int type; | |
104 | int argc; | |
105 | sds *argv; | |
106 | sds full; | |
107 | ||
108 | /* Only used for help on commands */ | |
109 | struct commandHelp *org; | |
110 | } helpEntry; | |
111 | ||
112 | static helpEntry *helpEntries; | |
113 | static int helpEntriesLen; | |
114 | ||
c392edf5 PN |
115 | static sds cliVersion() { |
116 | sds version; | |
117 | version = sdscatprintf(sdsempty(), "%s", REDIS_VERSION); | |
118 | ||
119 | /* Add git commit and working tree status when available */ | |
120 | if (strtoll(redisGitSHA1(),NULL,16)) { | |
121 | version = sdscatprintf(version, " (git:%s", redisGitSHA1()); | |
122 | if (strtoll(redisGitDirty(),NULL,10)) | |
123 | version = sdscatprintf(version, "-dirty"); | |
124 | version = sdscat(version, ")"); | |
125 | } | |
126 | return version; | |
127 | } | |
128 | ||
b2cc45bf PN |
129 | static void cliInitHelp() { |
130 | int commandslen = sizeof(commandHelp)/sizeof(struct commandHelp); | |
131 | int groupslen = sizeof(commandGroups)/sizeof(char*); | |
132 | int i, len, pos = 0; | |
133 | helpEntry tmp; | |
134 | ||
135 | helpEntriesLen = len = commandslen+groupslen; | |
136 | helpEntries = malloc(sizeof(helpEntry)*len); | |
137 | ||
138 | for (i = 0; i < groupslen; i++) { | |
139 | tmp.argc = 1; | |
140 | tmp.argv = malloc(sizeof(sds)); | |
141 | tmp.argv[0] = sdscatprintf(sdsempty(),"@%s",commandGroups[i]); | |
142 | tmp.full = tmp.argv[0]; | |
143 | tmp.type = CLI_HELP_GROUP; | |
144 | tmp.org = NULL; | |
145 | helpEntries[pos++] = tmp; | |
146 | } | |
147 | ||
148 | for (i = 0; i < commandslen; i++) { | |
149 | tmp.argv = sdssplitargs(commandHelp[i].name,&tmp.argc); | |
150 | tmp.full = sdsnew(commandHelp[i].name); | |
151 | tmp.type = CLI_HELP_COMMAND; | |
152 | tmp.org = &commandHelp[i]; | |
153 | helpEntries[pos++] = tmp; | |
154 | } | |
155 | } | |
156 | ||
a2a69d58 | 157 | /* Output command help to stdout. */ |
41945ba6 PN |
158 | static void cliOutputCommandHelp(struct commandHelp *help, int group) { |
159 | printf("\r\n \x1b[1m%s\x1b[0m \x1b[90m%s\x1b[0m\r\n", help->name, help->params); | |
160 | printf(" \x1b[33msummary:\x1b[0m %s\r\n", help->summary); | |
161 | printf(" \x1b[33msince:\x1b[0m %s\r\n", help->since); | |
162 | if (group) { | |
163 | printf(" \x1b[33mgroup:\x1b[0m %s\r\n", commandGroups[help->group]); | |
164 | } | |
a2a69d58 PN |
165 | } |
166 | ||
41945ba6 PN |
167 | /* Print generic help. */ |
168 | static void cliOutputGenericHelp() { | |
c392edf5 | 169 | sds version = cliVersion(); |
41945ba6 PN |
170 | printf( |
171 | "redis-cli %s\r\n" | |
172 | "Type: \"help @<group>\" to get a list of commands in <group>\r\n" | |
173 | " \"help <command>\" for help on <command>\r\n" | |
174 | " \"help <tab>\" to get a list of possible help topics\r\n" | |
175 | " \"quit\" to exit\r\n", | |
c392edf5 | 176 | version |
41945ba6 | 177 | ); |
c392edf5 | 178 | sdsfree(version); |
a2a69d58 PN |
179 | } |
180 | ||
181 | /* Output all command help, filtering by group or command name. */ | |
41945ba6 | 182 | static void cliOutputHelp(int argc, char **argv) { |
b2cc45bf | 183 | int i, j, len; |
41945ba6 | 184 | int group = -1; |
b2cc45bf PN |
185 | helpEntry *entry; |
186 | struct commandHelp *help; | |
a2a69d58 | 187 | |
41945ba6 PN |
188 | if (argc == 0) { |
189 | cliOutputGenericHelp(); | |
a2a69d58 | 190 | return; |
41945ba6 PN |
191 | } else if (argc > 0 && argv[0][0] == '@') { |
192 | len = sizeof(commandGroups)/sizeof(char*); | |
193 | for (i = 0; i < len; i++) { | |
194 | if (strcasecmp(argv[0]+1,commandGroups[i]) == 0) { | |
195 | group = i; | |
196 | break; | |
197 | } | |
198 | } | |
a2a69d58 PN |
199 | } |
200 | ||
41945ba6 | 201 | assert(argc > 0); |
b2cc45bf PN |
202 | for (i = 0; i < helpEntriesLen; i++) { |
203 | entry = &helpEntries[i]; | |
204 | if (entry->type != CLI_HELP_COMMAND) continue; | |
205 | ||
206 | help = entry->org; | |
a2a69d58 | 207 | if (group == -1) { |
b2cc45bf PN |
208 | /* Compare all arguments */ |
209 | if (argc == entry->argc) { | |
210 | for (j = 0; j < argc; j++) { | |
211 | if (strcasecmp(argv[j],entry->argv[j]) != 0) break; | |
212 | } | |
213 | if (j == argc) { | |
214 | cliOutputCommandHelp(help,1); | |
215 | } | |
a2a69d58 PN |
216 | } |
217 | } else { | |
218 | if (group == help->group) { | |
41945ba6 | 219 | cliOutputCommandHelp(help,0); |
a2a69d58 PN |
220 | } |
221 | } | |
222 | } | |
41945ba6 PN |
223 | printf("\r\n"); |
224 | } | |
225 | ||
41945ba6 PN |
226 | static void completionCallback(const char *buf, linenoiseCompletions *lc) { |
227 | size_t startpos = 0; | |
228 | int mask; | |
229 | int i; | |
230 | size_t matchlen; | |
b2cc45bf | 231 | sds tmp; |
41945ba6 PN |
232 | |
233 | if (strncasecmp(buf,"help ",5) == 0) { | |
234 | startpos = 5; | |
235 | while (isspace(buf[startpos])) startpos++; | |
b2cc45bf | 236 | mask = CLI_HELP_COMMAND | CLI_HELP_GROUP; |
41945ba6 | 237 | } else { |
b2cc45bf | 238 | mask = CLI_HELP_COMMAND; |
41945ba6 PN |
239 | } |
240 | ||
b2cc45bf PN |
241 | for (i = 0; i < helpEntriesLen; i++) { |
242 | if (!(helpEntries[i].type & mask)) continue; | |
41945ba6 PN |
243 | |
244 | matchlen = strlen(buf+startpos); | |
b2cc45bf PN |
245 | if (strncasecmp(buf+startpos,helpEntries[i].full,matchlen) == 0) { |
246 | tmp = sdsnewlen(buf,startpos); | |
247 | tmp = sdscat(tmp,helpEntries[i].full); | |
41945ba6 | 248 | linenoiseAddCompletion(lc,tmp); |
b2cc45bf | 249 | sdsfree(tmp); |
41945ba6 PN |
250 | } |
251 | } | |
a2a69d58 PN |
252 | } |
253 | ||
3ce014c7 | 254 | /*------------------------------------------------------------------------------ |
255 | * Networking / parsing | |
256 | *--------------------------------------------------------------------------- */ | |
257 | ||
7fc4ce13 PN |
258 | /* Send AUTH command to the server */ |
259 | static int cliAuth() { | |
260 | redisReply *reply; | |
261 | if (config.auth == NULL) return REDIS_OK; | |
262 | ||
263 | reply = redisCommand(context,"AUTH %s",config.auth); | |
264 | if (reply != NULL) { | |
265 | freeReplyObject(reply); | |
266 | return REDIS_OK; | |
267 | } | |
268 | return REDIS_ERR; | |
269 | } | |
270 | ||
271 | /* Send SELECT dbnum to the server */ | |
272 | static int cliSelect() { | |
273 | redisReply *reply; | |
7fc4ce13 PN |
274 | if (config.dbnum == 0) return REDIS_OK; |
275 | ||
96e34b3c | 276 | reply = redisCommand(context,"SELECT %d",config.dbnum); |
7fc4ce13 PN |
277 | if (reply != NULL) { |
278 | freeReplyObject(reply); | |
279 | return REDIS_OK; | |
280 | } | |
281 | return REDIS_ERR; | |
282 | } | |
283 | ||
c0b3d423 | 284 | /* Connect to the client. If force is not zero the connection is performed |
285 | * even if there is already a connected socket. */ | |
286 | static int cliConnect(int force) { | |
7fc4ce13 PN |
287 | if (context == NULL || force) { |
288 | if (context != NULL) | |
289 | redisFree(context); | |
ed9b544e | 290 | |
7e91f971 | 291 | if (config.hostsocket == NULL) { |
7fc4ce13 | 292 | context = redisConnect(config.hostip,config.hostport); |
7e91f971 | 293 | } else { |
7fc4ce13 | 294 | context = redisConnectUnix(config.hostsocket); |
7e91f971 | 295 | } |
7fc4ce13 PN |
296 | |
297 | if (context->err) { | |
7e91f971 PN |
298 | fprintf(stderr,"Could not connect to Redis at "); |
299 | if (config.hostsocket == NULL) | |
7fc4ce13 | 300 | fprintf(stderr,"%s:%d: %s\n",config.hostip,config.hostport,context->errstr); |
7e91f971 | 301 | else |
7fc4ce13 PN |
302 | fprintf(stderr,"%s: %s\n",config.hostsocket,context->errstr); |
303 | redisFree(context); | |
304 | context = NULL; | |
305 | return REDIS_ERR; | |
6fa24622 | 306 | } |
ed9b544e | 307 | |
7fc4ce13 PN |
308 | /* Do AUTH and select the right DB. */ |
309 | if (cliAuth() != REDIS_OK) | |
310 | return REDIS_ERR; | |
311 | if (cliSelect() != REDIS_OK) | |
312 | return REDIS_ERR; | |
ed9b544e | 313 | } |
7fc4ce13 | 314 | return REDIS_OK; |
ed9b544e | 315 | } |
316 | ||
7fc4ce13 PN |
317 | static void cliPrintContextErrorAndExit() { |
318 | if (context == NULL) return; | |
319 | fprintf(stderr,"Error: %s\n",context->errstr); | |
320 | exit(1); | |
ed9b544e | 321 | } |
322 | ||
65add0a3 | 323 | static sds cliFormatReplyTTY(redisReply *r, char *prefix) { |
7fc4ce13 PN |
324 | sds out = sdsempty(); |
325 | switch (r->type) { | |
326 | case REDIS_REPLY_ERROR: | |
65add0a3 | 327 | out = sdscatprintf(out,"(error) %s\n", r->str); |
7fc4ce13 PN |
328 | break; |
329 | case REDIS_REPLY_STATUS: | |
7fc4ce13 PN |
330 | out = sdscat(out,r->str); |
331 | out = sdscat(out,"\n"); | |
332 | break; | |
333 | case REDIS_REPLY_INTEGER: | |
65add0a3 | 334 | out = sdscatprintf(out,"(integer) %lld\n",r->integer); |
7fc4ce13 PN |
335 | break; |
336 | case REDIS_REPLY_STRING: | |
65add0a3 PN |
337 | /* If you are producing output for the standard output we want |
338 | * a more interesting output with quoted characters and so forth */ | |
339 | out = sdscatrepr(out,r->str,r->len); | |
340 | out = sdscat(out,"\n"); | |
7fc4ce13 PN |
341 | break; |
342 | case REDIS_REPLY_NIL: | |
7fc4ce13 PN |
343 | out = sdscat(out,"(nil)\n"); |
344 | break; | |
345 | case REDIS_REPLY_ARRAY: | |
346 | if (r->elements == 0) { | |
7fc4ce13 | 347 | out = sdscat(out,"(empty list or set)\n"); |
c0b3d423 | 348 | } else { |
cfcd5d6d PN |
349 | unsigned int i, idxlen = 0; |
350 | char _prefixlen[16]; | |
351 | char _prefixfmt[16]; | |
352 | sds _prefix; | |
7fc4ce13 PN |
353 | sds tmp; |
354 | ||
cfcd5d6d PN |
355 | /* Calculate chars needed to represent the largest index */ |
356 | i = r->elements; | |
357 | do { | |
358 | idxlen++; | |
359 | i /= 10; | |
360 | } while(i); | |
361 | ||
362 | /* Prefix for nested multi bulks should grow with idxlen+2 spaces */ | |
363 | memset(_prefixlen,' ',idxlen+2); | |
364 | _prefixlen[idxlen+2] = '\0'; | |
365 | _prefix = sdscat(sdsnew(prefix),_prefixlen); | |
366 | ||
367 | /* Setup prefix format for every entry */ | |
368 | snprintf(_prefixfmt,sizeof(_prefixfmt),"%%s%%%dd) ",idxlen); | |
369 | ||
7fc4ce13 | 370 | for (i = 0; i < r->elements; i++) { |
cfcd5d6d PN |
371 | /* Don't use the prefix for the first element, as the parent |
372 | * caller already prepended the index number. */ | |
373 | out = sdscatprintf(out,_prefixfmt,i == 0 ? "" : prefix,i+1); | |
374 | ||
375 | /* Format the multi bulk entry */ | |
65add0a3 | 376 | tmp = cliFormatReplyTTY(r->element[i],_prefix); |
7fc4ce13 PN |
377 | out = sdscatlen(out,tmp,sdslen(tmp)); |
378 | sdsfree(tmp); | |
379 | } | |
cfcd5d6d | 380 | sdsfree(_prefix); |
c0b3d423 | 381 | } |
7fc4ce13 | 382 | break; |
c937aa89 | 383 | default: |
7fc4ce13 PN |
384 | fprintf(stderr,"Unknown reply type: %d\n", r->type); |
385 | exit(1); | |
c937aa89 | 386 | } |
7fc4ce13 | 387 | return out; |
c937aa89 | 388 | } |
389 | ||
65add0a3 PN |
390 | static sds cliFormatReplyRaw(redisReply *r) { |
391 | sds out = sdsempty(), tmp; | |
392 | size_t i; | |
393 | ||
394 | switch (r->type) { | |
395 | case REDIS_REPLY_NIL: | |
396 | /* Nothing... */ | |
397 | break; | |
398 | case REDIS_REPLY_ERROR: | |
399 | case REDIS_REPLY_STATUS: | |
400 | case REDIS_REPLY_STRING: | |
401 | out = sdscatlen(out,r->str,r->len); | |
402 | break; | |
403 | case REDIS_REPLY_INTEGER: | |
404 | out = sdscatprintf(out,"%lld",r->integer); | |
405 | break; | |
406 | case REDIS_REPLY_ARRAY: | |
407 | for (i = 0; i < r->elements; i++) { | |
408 | if (i > 0) out = sdscat(out,config.mb_delim); | |
409 | tmp = cliFormatReplyRaw(r->element[i]); | |
410 | out = sdscatlen(out,tmp,sdslen(tmp)); | |
411 | sdsfree(tmp); | |
412 | } | |
413 | break; | |
414 | default: | |
415 | fprintf(stderr,"Unknown reply type: %d\n", r->type); | |
416 | exit(1); | |
417 | } | |
418 | return out; | |
419 | } | |
420 | ||
421 | static int cliReadReply(int output_raw_strings) { | |
8ce39260 | 422 | void *_reply; |
7fc4ce13 PN |
423 | redisReply *reply; |
424 | sds out; | |
425 | ||
8ce39260 | 426 | if (redisGetReply(context,&_reply) != REDIS_OK) { |
7fc4ce13 PN |
427 | if (config.shutdown) |
428 | return REDIS_OK; | |
429 | if (config.interactive) { | |
430 | /* Filter cases where we should reconnect */ | |
431 | if (context->err == REDIS_ERR_IO && errno == ECONNRESET) | |
432 | return REDIS_ERR; | |
433 | if (context->err == REDIS_ERR_EOF) | |
434 | return REDIS_ERR; | |
435 | } | |
436 | cliPrintContextErrorAndExit(); | |
437 | return REDIS_ERR; /* avoid compiler warning */ | |
62e920df | 438 | } |
7fc4ce13 | 439 | |
8ce39260 | 440 | reply = (redisReply*)_reply; |
65add0a3 PN |
441 | if (output_raw_strings) { |
442 | out = cliFormatReplyRaw(reply); | |
443 | } else { | |
444 | if (config.raw_output) { | |
445 | out = cliFormatReplyRaw(reply); | |
446 | out = sdscat(out,"\n"); | |
447 | } else { | |
448 | out = cliFormatReplyTTY(reply,""); | |
449 | } | |
450 | } | |
7fc4ce13 PN |
451 | fwrite(out,sdslen(out),1,stdout); |
452 | sdsfree(out); | |
65add0a3 | 453 | freeReplyObject(reply); |
7fc4ce13 | 454 | return REDIS_OK; |
62e920df | 455 | } |
456 | ||
aab055ae | 457 | static int cliSendCommand(int argc, char **argv, int repeat) { |
37dc9e5a | 458 | char *command = argv[0]; |
7fc4ce13 | 459 | size_t *argvlen; |
65add0a3 | 460 | int j, output_raw; |
ed9b544e | 461 | |
efcf948c | 462 | if (context == NULL) { |
463 | printf("Not connected, please use: connect <host> <port>\n"); | |
464 | return REDIS_OK; | |
465 | } | |
466 | ||
65add0a3 | 467 | output_raw = !strcasecmp(command,"info"); |
41945ba6 PN |
468 | if (!strcasecmp(command,"help") || !strcasecmp(command,"?")) { |
469 | cliOutputHelp(--argc, ++argv); | |
7fc4ce13 | 470 | return REDIS_OK; |
8079656a | 471 | } |
37dc9e5a PN |
472 | if (!strcasecmp(command,"shutdown")) config.shutdown = 1; |
473 | if (!strcasecmp(command,"monitor")) config.monitor_mode = 1; | |
474 | if (!strcasecmp(command,"subscribe") || | |
475 | !strcasecmp(command,"psubscribe")) config.pubsub_mode = 1; | |
ed9b544e | 476 | |
7fc4ce13 PN |
477 | /* Setup argument length */ |
478 | argvlen = malloc(argc*sizeof(size_t)); | |
479 | for (j = 0; j < argc; j++) | |
480 | argvlen[j] = sdslen(argv[j]); | |
a2f4f871 | 481 | |
aab055ae | 482 | while(repeat--) { |
7fc4ce13 | 483 | redisAppendCommandArgv(context,argc,(const char**)argv,argvlen); |
249c3a7d | 484 | while (config.monitor_mode) { |
65add0a3 | 485 | if (cliReadReply(output_raw) != REDIS_OK) exit(1); |
d9d8ccab | 486 | fflush(stdout); |
621d5c19 | 487 | } |
488 | ||
249c3a7d | 489 | if (config.pubsub_mode) { |
65add0a3 PN |
490 | if (!config.raw_output) |
491 | printf("Reading messages... (press Ctrl-C to quit)\n"); | |
249c3a7d | 492 | while (1) { |
65add0a3 | 493 | if (cliReadReply(output_raw) != REDIS_OK) exit(1); |
249c3a7d | 494 | } |
495 | } | |
496 | ||
33753a73 PN |
497 | if (cliReadReply(output_raw) != REDIS_OK) { |
498 | free(argvlen); | |
7fc4ce13 | 499 | return REDIS_ERR; |
96e34b3c PN |
500 | } else { |
501 | /* Store database number when SELECT was successfully executed. */ | |
3f4eef21 | 502 | if (!strcasecmp(command,"select") && argc == 2) { |
96e34b3c | 503 | config.dbnum = atoi(argv[1]); |
3f4eef21 PN |
504 | cliRefreshPrompt(); |
505 | } | |
33753a73 | 506 | } |
ed9b544e | 507 | } |
33753a73 PN |
508 | |
509 | free(argvlen); | |
7fc4ce13 | 510 | return REDIS_OK; |
ed9b544e | 511 | } |
512 | ||
3ce014c7 | 513 | /*------------------------------------------------------------------------------ |
514 | * User interface | |
515 | *--------------------------------------------------------------------------- */ | |
516 | ||
ed9b544e | 517 | static int parseOptions(int argc, char **argv) { |
518 | int i; | |
519 | ||
520 | for (i = 1; i < argc; i++) { | |
521 | int lastarg = i==argc-1; | |
6cf5882c | 522 | |
ed9b544e | 523 | if (!strcmp(argv[i],"-h") && !lastarg) { |
efcf948c | 524 | sdsfree(config.hostip); |
525 | config.hostip = sdsnew(argv[i+1]); | |
ed9b544e | 526 | i++; |
a9158272 | 527 | } else if (!strcmp(argv[i],"-h") && lastarg) { |
528 | usage(); | |
f18e059e PN |
529 | } else if (!strcmp(argv[i],"--help")) { |
530 | usage(); | |
bc63407b | 531 | } else if (!strcmp(argv[i],"-x")) { |
532 | config.stdinarg = 1; | |
ed9b544e | 533 | } else if (!strcmp(argv[i],"-p") && !lastarg) { |
534 | config.hostport = atoi(argv[i+1]); | |
535 | i++; | |
7e91f971 PN |
536 | } else if (!strcmp(argv[i],"-s") && !lastarg) { |
537 | config.hostsocket = argv[i+1]; | |
538 | i++; | |
5762b7f0 | 539 | } else if (!strcmp(argv[i],"-r") && !lastarg) { |
540 | config.repeat = strtoll(argv[i+1],NULL,10); | |
541 | i++; | |
62e920df | 542 | } else if (!strcmp(argv[i],"-n") && !lastarg) { |
543 | config.dbnum = atoi(argv[i+1]); | |
544 | i++; | |
fdfdae0f | 545 | } else if (!strcmp(argv[i],"-a") && !lastarg) { |
288799e0 | 546 | config.auth = argv[i+1]; |
fdfdae0f | 547 | i++; |
65add0a3 PN |
548 | } else if (!strcmp(argv[i],"--raw")) { |
549 | config.raw_output = 1; | |
28c07c7b PN |
550 | } else if (!strcmp(argv[i],"-d") && !lastarg) { |
551 | sdsfree(config.mb_delim); | |
552 | config.mb_delim = sdsnew(argv[i+1]); | |
553 | i++; | |
f18e059e PN |
554 | } else if (!strcmp(argv[i],"-v") || !strcmp(argv[i], "--version")) { |
555 | sds version = cliVersion(); | |
556 | printf("redis-cli %s\n", version); | |
557 | sdsfree(version); | |
185cabda | 558 | exit(0); |
ed9b544e | 559 | } else { |
560 | break; | |
561 | } | |
562 | } | |
563 | return i; | |
564 | } | |
565 | ||
566 | static sds readArgFromStdin(void) { | |
567 | char buf[1024]; | |
568 | sds arg = sdsempty(); | |
569 | ||
570 | while(1) { | |
571 | int nread = read(fileno(stdin),buf,1024); | |
572 | ||
573 | if (nread == 0) break; | |
574 | else if (nread == -1) { | |
575 | perror("Reading from standard input"); | |
576 | exit(1); | |
577 | } | |
578 | arg = sdscatlen(arg,buf,nread); | |
579 | } | |
580 | return arg; | |
581 | } | |
582 | ||
a9158272 | 583 | static void usage() { |
f18e059e PN |
584 | sds version = cliVersion(); |
585 | fprintf(stderr, | |
586 | "redis-cli %s\n" | |
587 | "\n" | |
588 | "Usage: redis-cli [OPTIONS] [cmd [arg [arg ...]]]\n" | |
589 | " -h <hostname> Server hostname (default: 127.0.0.1)\n" | |
590 | " -p <port> Server port (default: 6379)\n" | |
591 | " -s <socket> Server socket (overrides hostname and port)\n" | |
592 | " -a <password> Password to use when connecting to the server\n" | |
593 | " -r <repeat> Execute specified command N times\n" | |
594 | " -n <db> Database number\n" | |
595 | " -x Read last argument from STDIN\n" | |
28c07c7b | 596 | " -d <delimiter> Multi-bulk delimiter in for raw formatting (default: \\n)\n" |
65add0a3 | 597 | " --raw Use raw formatting for replies (default when STDOUT is not a tty)\n" |
f18e059e PN |
598 | " --help Output this help and exit\n" |
599 | " --version Output version and exit\n" | |
600 | "\n" | |
601 | "Examples:\n" | |
602 | " cat /etc/passwd | redis-cli -x set mypasswd\n" | |
603 | " redis-cli get mypasswd\n" | |
604 | " redis-cli -r 100 lpush mylist x\n" | |
605 | "\n" | |
606 | "When no command is given, redis-cli starts in interactive mode.\n" | |
607 | "Type \"help\" in interactive mode for information on available commands.\n" | |
608 | "\n", | |
609 | version); | |
610 | sdsfree(version); | |
a9158272 | 611 | exit(1); |
612 | } | |
613 | ||
6cf5882c MMDJ |
614 | /* Turn the plain C strings into Sds strings */ |
615 | static char **convertToSds(int count, char** args) { | |
616 | int j; | |
37dc9e5a | 617 | char **sds = zmalloc(sizeof(char*)*count); |
6cf5882c MMDJ |
618 | |
619 | for(j = 0; j < count; j++) | |
620 | sds[j] = sdsnew(args[j]); | |
621 | ||
622 | return sds; | |
623 | } | |
624 | ||
a88a2af6 | 625 | #define LINE_BUFLEN 4096 |
6cf5882c | 626 | static void repl() { |
ca36b4ab PN |
627 | sds historyfile = NULL; |
628 | int history = 0; | |
cbce5171 | 629 | char *line; |
ca36b4ab | 630 | int argc; |
cbce5171 | 631 | sds *argv; |
6cf5882c | 632 | |
5d15b520 | 633 | config.interactive = 1; |
41945ba6 | 634 | linenoiseSetCompletionCallback(completionCallback); |
ce260f73 | 635 | |
ca36b4ab PN |
636 | /* Only use history when stdin is a tty. */ |
637 | if (isatty(fileno(stdin))) { | |
638 | history = 1; | |
639 | ||
640 | if (getenv("HOME") != NULL) { | |
641 | historyfile = sdscatprintf(sdsempty(),"%s/.rediscli_history",getenv("HOME")); | |
642 | linenoiseHistoryLoad(historyfile); | |
643 | } | |
644 | } | |
645 | ||
3f4eef21 PN |
646 | cliRefreshPrompt(); |
647 | while((line = linenoise(context ? config.prompt : "not connected> ")) != NULL) { | |
cf87ebf2 | 648 | if (line[0] != '\0') { |
cbce5171 | 649 | argv = sdssplitargs(line,&argc); |
ca36b4ab PN |
650 | if (history) linenoiseHistoryAdd(line); |
651 | if (historyfile) linenoiseHistorySave(historyfile); | |
652 | ||
0439d792 PN |
653 | if (argv == NULL) { |
654 | printf("Invalid argument(s)\n"); | |
655 | continue; | |
656 | } else if (argc > 0) { | |
a88a2af6 | 657 | if (strcasecmp(argv[0],"quit") == 0 || |
658 | strcasecmp(argv[0],"exit") == 0) | |
c0b3d423 | 659 | { |
660 | exit(0); | |
efcf948c | 661 | } else if (argc == 3 && !strcasecmp(argv[0],"connect")) { |
662 | sdsfree(config.hostip); | |
663 | config.hostip = sdsnew(argv[1]); | |
664 | config.hostport = atoi(argv[2]); | |
665 | cliConnect(1); | |
bbac56c2 | 666 | } else if (argc == 1 && !strcasecmp(argv[0],"clear")) { |
667 | linenoiseClearScreen(); | |
c0b3d423 | 668 | } else { |
3ce014c7 | 669 | long long start_time = mstime(), elapsed; |
c0b3d423 | 670 | |
7fc4ce13 | 671 | if (cliSendCommand(argc,argv,1) != REDIS_OK) { |
efcf948c | 672 | cliConnect(1); |
7fc4ce13 PN |
673 | |
674 | /* If we still cannot send the command, | |
675 | * print error and abort. */ | |
676 | if (cliSendCommand(argc,argv,1) != REDIS_OK) | |
677 | cliPrintContextErrorAndExit(); | |
c0b3d423 | 678 | } |
3ce014c7 | 679 | elapsed = mstime()-start_time; |
339b9dc2 PN |
680 | if (elapsed >= 500) { |
681 | printf("(%.2fs)\n",(double)elapsed/1000); | |
682 | } | |
c0b3d423 | 683 | } |
a88a2af6 | 684 | } |
685 | /* Free the argument vector */ | |
ca36b4ab | 686 | while(argc--) sdsfree(argv[argc]); |
8ff6a48b | 687 | zfree(argv); |
6cf5882c | 688 | } |
a88a2af6 | 689 | /* linenoise() returns malloc-ed lines like readline() */ |
cf87ebf2 | 690 | free(line); |
6cf5882c | 691 | } |
6cf5882c MMDJ |
692 | exit(0); |
693 | } | |
694 | ||
b4b62c34 PN |
695 | static int noninteractive(int argc, char **argv) { |
696 | int retval = 0; | |
bc63407b | 697 | if (config.stdinarg) { |
b4b62c34 PN |
698 | argv = zrealloc(argv, (argc+1)*sizeof(char*)); |
699 | argv[argc] = readArgFromStdin(); | |
700 | retval = cliSendCommand(argc+1, argv, config.repeat); | |
701 | } else { | |
702 | /* stdin is probably a tty, can be tested with S_ISCHR(s.st_mode) */ | |
703 | retval = cliSendCommand(argc, argv, config.repeat); | |
704 | } | |
705 | return retval; | |
706 | } | |
707 | ||
ed9b544e | 708 | int main(int argc, char **argv) { |
6cf5882c | 709 | int firstarg; |
ed9b544e | 710 | |
efcf948c | 711 | config.hostip = sdsnew("127.0.0.1"); |
ed9b544e | 712 | config.hostport = 6379; |
7e91f971 | 713 | config.hostsocket = NULL; |
5762b7f0 | 714 | config.repeat = 1; |
62e920df | 715 | config.dbnum = 0; |
5d15b520 | 716 | config.interactive = 0; |
36e5db6d | 717 | config.shutdown = 0; |
249c3a7d | 718 | config.monitor_mode = 0; |
719 | config.pubsub_mode = 0; | |
bc63407b | 720 | config.stdinarg = 0; |
288799e0 | 721 | config.auth = NULL; |
65add0a3 PN |
722 | config.raw_output = !isatty(fileno(stdout)) && (getenv("FAKETTY") == NULL); |
723 | config.mb_delim = sdsnew("\n"); | |
41945ba6 | 724 | cliInitHelp(); |
99628c1a | 725 | |
ed9b544e | 726 | firstarg = parseOptions(argc,argv); |
727 | argc -= firstarg; | |
728 | argv += firstarg; | |
ed9b544e | 729 | |
7fc4ce13 PN |
730 | /* Try to connect */ |
731 | if (cliConnect(0) != REDIS_OK) exit(1); | |
aab055ae | 732 | |
abb731e5 PN |
733 | /* Start interactive mode when no command is provided */ |
734 | if (argc == 0) repl(); | |
b4b62c34 PN |
735 | /* Otherwise, we have some arguments to execute */ |
736 | return noninteractive(argc,convertToSds(argc,argv)); | |
ed9b544e | 737 | } |