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