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