| 1 | /* Redis CLI (command line interface) |
| 2 | * |
| 3 | * Copyright (c) 2009-2010, Salvatore Sanfilippo <antirez at gmail dot com> |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are met: |
| 8 | * |
| 9 | * * Redistributions of source code must retain the above copyright notice, |
| 10 | * this list of conditions and the following disclaimer. |
| 11 | * * Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * * Neither the name of Redis nor the names of its contributors may be used |
| 15 | * to endorse or promote products derived from this software without |
| 16 | * specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | * POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | |
| 31 | #include "fmacros.h" |
| 32 | |
| 33 | #include <stdio.h> |
| 34 | #include <string.h> |
| 35 | #include <stdlib.h> |
| 36 | #include <unistd.h> |
| 37 | |
| 38 | #include "anet.h" |
| 39 | #include "sds.h" |
| 40 | #include "adlist.h" |
| 41 | #include "zmalloc.h" |
| 42 | |
| 43 | #define REDIS_CMD_INLINE 1 |
| 44 | #define REDIS_CMD_BULK 2 |
| 45 | #define REDIS_CMD_MULTIBULK 4 |
| 46 | |
| 47 | #define REDIS_NOTUSED(V) ((void) V) |
| 48 | |
| 49 | static struct config { |
| 50 | char *hostip; |
| 51 | int hostport; |
| 52 | long repeat; |
| 53 | int dbnum; |
| 54 | int interactive; |
| 55 | } config; |
| 56 | |
| 57 | struct redisCommand { |
| 58 | char *name; |
| 59 | int arity; |
| 60 | int flags; |
| 61 | }; |
| 62 | |
| 63 | static struct redisCommand cmdTable[] = { |
| 64 | {"get",2,REDIS_CMD_INLINE}, |
| 65 | {"set",3,REDIS_CMD_BULK}, |
| 66 | {"setnx",3,REDIS_CMD_BULK}, |
| 67 | {"append",3,REDIS_CMD_BULK}, |
| 68 | {"substr",4,REDIS_CMD_INLINE}, |
| 69 | {"del",-2,REDIS_CMD_INLINE}, |
| 70 | {"exists",2,REDIS_CMD_INLINE}, |
| 71 | {"incr",2,REDIS_CMD_INLINE}, |
| 72 | {"decr",2,REDIS_CMD_INLINE}, |
| 73 | {"rpush",3,REDIS_CMD_BULK}, |
| 74 | {"lpush",3,REDIS_CMD_BULK}, |
| 75 | {"rpop",2,REDIS_CMD_INLINE}, |
| 76 | {"lpop",2,REDIS_CMD_INLINE}, |
| 77 | {"brpop",-3,REDIS_CMD_INLINE}, |
| 78 | {"blpop",-3,REDIS_CMD_INLINE}, |
| 79 | {"llen",2,REDIS_CMD_INLINE}, |
| 80 | {"lindex",3,REDIS_CMD_INLINE}, |
| 81 | {"lset",4,REDIS_CMD_BULK}, |
| 82 | {"lrange",4,REDIS_CMD_INLINE}, |
| 83 | {"ltrim",4,REDIS_CMD_INLINE}, |
| 84 | {"lrem",4,REDIS_CMD_BULK}, |
| 85 | {"rpoplpush",3,REDIS_CMD_BULK}, |
| 86 | {"sadd",3,REDIS_CMD_BULK}, |
| 87 | {"srem",3,REDIS_CMD_BULK}, |
| 88 | {"smove",4,REDIS_CMD_BULK}, |
| 89 | {"sismember",3,REDIS_CMD_BULK}, |
| 90 | {"scard",2,REDIS_CMD_INLINE}, |
| 91 | {"spop",2,REDIS_CMD_INLINE}, |
| 92 | {"srandmember",2,REDIS_CMD_INLINE}, |
| 93 | {"sinter",-2,REDIS_CMD_INLINE}, |
| 94 | {"sinterstore",-3,REDIS_CMD_INLINE}, |
| 95 | {"sunion",-2,REDIS_CMD_INLINE}, |
| 96 | {"sunionstore",-3,REDIS_CMD_INLINE}, |
| 97 | {"sdiff",-2,REDIS_CMD_INLINE}, |
| 98 | {"sdiffstore",-3,REDIS_CMD_INLINE}, |
| 99 | {"smembers",2,REDIS_CMD_INLINE}, |
| 100 | {"zadd",4,REDIS_CMD_BULK}, |
| 101 | {"zincrby",4,REDIS_CMD_BULK}, |
| 102 | {"zrem",3,REDIS_CMD_BULK}, |
| 103 | {"zremrangebyscore",4,REDIS_CMD_INLINE}, |
| 104 | {"zrange",-4,REDIS_CMD_INLINE}, |
| 105 | {"zrank",3,REDIS_CMD_BULK}, |
| 106 | {"zrangebyscore",-4,REDIS_CMD_INLINE}, |
| 107 | {"zcount",4,REDIS_CMD_INLINE}, |
| 108 | {"zrevrange",-4,REDIS_CMD_INLINE}, |
| 109 | {"zcard",2,REDIS_CMD_INLINE}, |
| 110 | {"zscore",3,REDIS_CMD_BULK}, |
| 111 | {"incrby",3,REDIS_CMD_INLINE}, |
| 112 | {"decrby",3,REDIS_CMD_INLINE}, |
| 113 | {"getset",3,REDIS_CMD_BULK}, |
| 114 | {"randomkey",1,REDIS_CMD_INLINE}, |
| 115 | {"select",2,REDIS_CMD_INLINE}, |
| 116 | {"move",3,REDIS_CMD_INLINE}, |
| 117 | {"rename",3,REDIS_CMD_INLINE}, |
| 118 | {"renamenx",3,REDIS_CMD_INLINE}, |
| 119 | {"keys",2,REDIS_CMD_INLINE}, |
| 120 | {"dbsize",1,REDIS_CMD_INLINE}, |
| 121 | {"ping",1,REDIS_CMD_INLINE}, |
| 122 | {"echo",2,REDIS_CMD_BULK}, |
| 123 | {"save",1,REDIS_CMD_INLINE}, |
| 124 | {"bgsave",1,REDIS_CMD_INLINE}, |
| 125 | {"rewriteaof",1,REDIS_CMD_INLINE}, |
| 126 | {"bgrewriteaof",1,REDIS_CMD_INLINE}, |
| 127 | {"shutdown",1,REDIS_CMD_INLINE}, |
| 128 | {"lastsave",1,REDIS_CMD_INLINE}, |
| 129 | {"type",2,REDIS_CMD_INLINE}, |
| 130 | {"flushdb",1,REDIS_CMD_INLINE}, |
| 131 | {"flushall",1,REDIS_CMD_INLINE}, |
| 132 | {"sort",-2,REDIS_CMD_INLINE}, |
| 133 | {"info",1,REDIS_CMD_INLINE}, |
| 134 | {"mget",-2,REDIS_CMD_INLINE}, |
| 135 | {"expire",3,REDIS_CMD_INLINE}, |
| 136 | {"expireat",3,REDIS_CMD_INLINE}, |
| 137 | {"ttl",2,REDIS_CMD_INLINE}, |
| 138 | {"slaveof",3,REDIS_CMD_INLINE}, |
| 139 | {"debug",-2,REDIS_CMD_INLINE}, |
| 140 | {"mset",-3,REDIS_CMD_MULTIBULK}, |
| 141 | {"msetnx",-3,REDIS_CMD_MULTIBULK}, |
| 142 | {"monitor",1,REDIS_CMD_INLINE}, |
| 143 | {"multi",1,REDIS_CMD_INLINE}, |
| 144 | {"exec",1,REDIS_CMD_MULTIBULK}, |
| 145 | {"discard",1,REDIS_CMD_INLINE}, |
| 146 | {NULL,0,0} |
| 147 | }; |
| 148 | |
| 149 | static int cliReadReply(int fd); |
| 150 | static void usage(); |
| 151 | |
| 152 | static struct redisCommand *lookupCommand(char *name) { |
| 153 | int j = 0; |
| 154 | while(cmdTable[j].name != NULL) { |
| 155 | if (!strcasecmp(name,cmdTable[j].name)) return &cmdTable[j]; |
| 156 | j++; |
| 157 | } |
| 158 | return NULL; |
| 159 | } |
| 160 | |
| 161 | static int cliConnect(void) { |
| 162 | char err[ANET_ERR_LEN]; |
| 163 | static int fd = ANET_ERR; |
| 164 | |
| 165 | if (fd == ANET_ERR) { |
| 166 | fd = anetTcpConnect(err,config.hostip,config.hostport); |
| 167 | if (fd == ANET_ERR) { |
| 168 | fprintf(stderr, "Could not connect to Redis at %s:%d: %s", config.hostip, config.hostport, err); |
| 169 | return -1; |
| 170 | } |
| 171 | anetTcpNoDelay(NULL,fd); |
| 172 | } |
| 173 | return fd; |
| 174 | } |
| 175 | |
| 176 | static sds cliReadLine(int fd) { |
| 177 | sds line = sdsempty(); |
| 178 | |
| 179 | while(1) { |
| 180 | char c; |
| 181 | ssize_t ret; |
| 182 | |
| 183 | ret = read(fd,&c,1); |
| 184 | if (ret == -1) { |
| 185 | sdsfree(line); |
| 186 | return NULL; |
| 187 | } else if ((ret == 0) || (c == '\n')) { |
| 188 | break; |
| 189 | } else { |
| 190 | line = sdscatlen(line,&c,1); |
| 191 | } |
| 192 | } |
| 193 | return sdstrim(line,"\r\n"); |
| 194 | } |
| 195 | |
| 196 | static int cliReadSingleLineReply(int fd, int quiet) { |
| 197 | sds reply = cliReadLine(fd); |
| 198 | |
| 199 | if (reply == NULL) return 1; |
| 200 | if (!quiet) |
| 201 | printf("%s\n", reply); |
| 202 | sdsfree(reply); |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | static int cliReadBulkReply(int fd) { |
| 207 | sds replylen = cliReadLine(fd); |
| 208 | char *reply, crlf[2]; |
| 209 | int bulklen; |
| 210 | |
| 211 | if (replylen == NULL) return 1; |
| 212 | bulklen = atoi(replylen); |
| 213 | if (bulklen == -1) { |
| 214 | sdsfree(replylen); |
| 215 | printf("(nil)\n"); |
| 216 | return 0; |
| 217 | } |
| 218 | reply = zmalloc(bulklen); |
| 219 | anetRead(fd,reply,bulklen); |
| 220 | anetRead(fd,crlf,2); |
| 221 | if (bulklen && fwrite(reply,bulklen,1,stdout) == 0) { |
| 222 | zfree(reply); |
| 223 | return 1; |
| 224 | } |
| 225 | if (isatty(fileno(stdout)) && reply[bulklen-1] != '\n') |
| 226 | printf("\n"); |
| 227 | zfree(reply); |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | static int cliReadMultiBulkReply(int fd) { |
| 232 | sds replylen = cliReadLine(fd); |
| 233 | int elements, c = 1; |
| 234 | |
| 235 | if (replylen == NULL) return 1; |
| 236 | elements = atoi(replylen); |
| 237 | if (elements == -1) { |
| 238 | sdsfree(replylen); |
| 239 | printf("(nil)\n"); |
| 240 | return 0; |
| 241 | } |
| 242 | if (elements == 0) { |
| 243 | printf("(empty list or set)\n"); |
| 244 | } |
| 245 | while(elements--) { |
| 246 | printf("%d. ", c); |
| 247 | if (cliReadReply(fd)) return 1; |
| 248 | c++; |
| 249 | } |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | static int cliReadReply(int fd) { |
| 254 | char type; |
| 255 | |
| 256 | if (anetRead(fd,&type,1) <= 0) exit(1); |
| 257 | switch(type) { |
| 258 | case '-': |
| 259 | printf("(error) "); |
| 260 | cliReadSingleLineReply(fd,0); |
| 261 | return 1; |
| 262 | case '+': |
| 263 | return cliReadSingleLineReply(fd,0); |
| 264 | case ':': |
| 265 | printf("(integer) "); |
| 266 | return cliReadSingleLineReply(fd,0); |
| 267 | case '$': |
| 268 | return cliReadBulkReply(fd); |
| 269 | case '*': |
| 270 | return cliReadMultiBulkReply(fd); |
| 271 | default: |
| 272 | printf("protocol error, got '%c' as reply type byte\n", type); |
| 273 | return 1; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | static int selectDb(int fd) { |
| 278 | int retval; |
| 279 | sds cmd; |
| 280 | char type; |
| 281 | |
| 282 | if (config.dbnum == 0) |
| 283 | return 0; |
| 284 | |
| 285 | cmd = sdsempty(); |
| 286 | cmd = sdscatprintf(cmd,"SELECT %d\r\n",config.dbnum); |
| 287 | anetWrite(fd,cmd,sdslen(cmd)); |
| 288 | anetRead(fd,&type,1); |
| 289 | if (type <= 0 || type != '+') return 1; |
| 290 | retval = cliReadSingleLineReply(fd,1); |
| 291 | if (retval) { |
| 292 | return retval; |
| 293 | } |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | static int cliSendCommand(int argc, char **argv) { |
| 298 | struct redisCommand *rc = lookupCommand(argv[0]); |
| 299 | int fd, j, retval = 0; |
| 300 | int read_forever = 0; |
| 301 | sds cmd; |
| 302 | |
| 303 | if (!rc) { |
| 304 | fprintf(stderr,"Unknown command '%s'\n",argv[0]); |
| 305 | return 1; |
| 306 | } |
| 307 | |
| 308 | if ((rc->arity > 0 && argc != rc->arity) || |
| 309 | (rc->arity < 0 && argc < -rc->arity)) { |
| 310 | fprintf(stderr,"Wrong number of arguments for '%s'\n",rc->name); |
| 311 | return 1; |
| 312 | } |
| 313 | if (!strcasecmp(rc->name,"monitor")) read_forever = 1; |
| 314 | if ((fd = cliConnect()) == -1) return 1; |
| 315 | |
| 316 | /* Select db number */ |
| 317 | retval = selectDb(fd); |
| 318 | if (retval) { |
| 319 | fprintf(stderr,"Error setting DB num\n"); |
| 320 | return 1; |
| 321 | } |
| 322 | |
| 323 | while(config.repeat--) { |
| 324 | /* Build the command to send */ |
| 325 | cmd = sdsempty(); |
| 326 | if (rc->flags & REDIS_CMD_MULTIBULK) { |
| 327 | cmd = sdscatprintf(cmd,"*%d\r\n",argc); |
| 328 | for (j = 0; j < argc; j++) { |
| 329 | cmd = sdscatprintf(cmd,"$%lu\r\n", |
| 330 | (unsigned long)sdslen(argv[j])); |
| 331 | cmd = sdscatlen(cmd,argv[j],sdslen(argv[j])); |
| 332 | cmd = sdscatlen(cmd,"\r\n",2); |
| 333 | } |
| 334 | } else { |
| 335 | for (j = 0; j < argc; j++) { |
| 336 | if (j != 0) cmd = sdscat(cmd," "); |
| 337 | if (j == argc-1 && rc->flags & REDIS_CMD_BULK) { |
| 338 | cmd = sdscatprintf(cmd,"%lu", |
| 339 | (unsigned long)sdslen(argv[j])); |
| 340 | } else { |
| 341 | cmd = sdscatlen(cmd,argv[j],sdslen(argv[j])); |
| 342 | } |
| 343 | } |
| 344 | cmd = sdscat(cmd,"\r\n"); |
| 345 | if (rc->flags & REDIS_CMD_BULK) { |
| 346 | cmd = sdscatlen(cmd,argv[argc-1],sdslen(argv[argc-1])); |
| 347 | cmd = sdscatlen(cmd,"\r\n",2); |
| 348 | } |
| 349 | } |
| 350 | anetWrite(fd,cmd,sdslen(cmd)); |
| 351 | sdsfree(cmd); |
| 352 | |
| 353 | while (read_forever) { |
| 354 | cliReadSingleLineReply(fd,0); |
| 355 | } |
| 356 | |
| 357 | retval = cliReadReply(fd); |
| 358 | if (retval) { |
| 359 | return retval; |
| 360 | } |
| 361 | } |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | static int parseOptions(int argc, char **argv) { |
| 366 | int i; |
| 367 | |
| 368 | for (i = 1; i < argc; i++) { |
| 369 | int lastarg = i==argc-1; |
| 370 | |
| 371 | if (!strcmp(argv[i],"-h") && !lastarg) { |
| 372 | char *ip = zmalloc(32); |
| 373 | if (anetResolve(NULL,argv[i+1],ip) == ANET_ERR) { |
| 374 | printf("Can't resolve %s\n", argv[i]); |
| 375 | exit(1); |
| 376 | } |
| 377 | config.hostip = ip; |
| 378 | i++; |
| 379 | } else if (!strcmp(argv[i],"-h") && lastarg) { |
| 380 | usage(); |
| 381 | } else if (!strcmp(argv[i],"-p") && !lastarg) { |
| 382 | config.hostport = atoi(argv[i+1]); |
| 383 | i++; |
| 384 | } else if (!strcmp(argv[i],"-r") && !lastarg) { |
| 385 | config.repeat = strtoll(argv[i+1],NULL,10); |
| 386 | i++; |
| 387 | } else if (!strcmp(argv[i],"-n") && !lastarg) { |
| 388 | config.dbnum = atoi(argv[i+1]); |
| 389 | i++; |
| 390 | } else if (!strcmp(argv[i],"-i")) { |
| 391 | config.interactive = 1; |
| 392 | } else { |
| 393 | break; |
| 394 | } |
| 395 | } |
| 396 | return i; |
| 397 | } |
| 398 | |
| 399 | static sds readArgFromStdin(void) { |
| 400 | char buf[1024]; |
| 401 | sds arg = sdsempty(); |
| 402 | |
| 403 | while(1) { |
| 404 | int nread = read(fileno(stdin),buf,1024); |
| 405 | |
| 406 | if (nread == 0) break; |
| 407 | else if (nread == -1) { |
| 408 | perror("Reading from standard input"); |
| 409 | exit(1); |
| 410 | } |
| 411 | arg = sdscatlen(arg,buf,nread); |
| 412 | } |
| 413 | return arg; |
| 414 | } |
| 415 | |
| 416 | static void usage() { |
| 417 | fprintf(stderr, "usage: redis-cli [-h host] [-p port] [-r repeat_times] [-n db_num] [-i] cmd arg1 arg2 arg3 ... argN\n"); |
| 418 | fprintf(stderr, "usage: echo \"argN\" | redis-cli [-h host] [-p port] [-r repeat_times] [-n db_num] cmd arg1 arg2 ... arg(N-1)\n"); |
| 419 | fprintf(stderr, "\nIf a pipe from standard input is detected this data is used as last argument.\n\n"); |
| 420 | fprintf(stderr, "example: cat /etc/passwd | redis-cli set my_passwd\n"); |
| 421 | fprintf(stderr, "example: redis-cli get my_passwd\n"); |
| 422 | fprintf(stderr, "example: redis-cli -r 100 lpush mylist x\n"); |
| 423 | fprintf(stderr, "\nRun in interactive mode: redis-cli -i \n"); |
| 424 | exit(1); |
| 425 | } |
| 426 | |
| 427 | /* Turn the plain C strings into Sds strings */ |
| 428 | static char **convertToSds(int count, char** args) { |
| 429 | int j; |
| 430 | char **sds = zmalloc(sizeof(char*)*count+1); |
| 431 | |
| 432 | for(j = 0; j < count; j++) |
| 433 | sds[j] = sdsnew(args[j]); |
| 434 | |
| 435 | return sds; |
| 436 | } |
| 437 | |
| 438 | static char *prompt(char *line, int size) { |
| 439 | char *retval; |
| 440 | |
| 441 | do { |
| 442 | printf(">> "); |
| 443 | retval = fgets(line, size, stdin); |
| 444 | } while (retval && *line == '\n'); |
| 445 | line[strlen(line) - 1] = '\0'; |
| 446 | |
| 447 | return retval; |
| 448 | } |
| 449 | |
| 450 | static void repl() { |
| 451 | int size = 4096, max = size >> 1, argc; |
| 452 | char buffer[size]; |
| 453 | char *line = buffer; |
| 454 | char **ap, *args[max]; |
| 455 | |
| 456 | while (prompt(line, size)) { |
| 457 | argc = 0; |
| 458 | |
| 459 | for (ap = args; (*ap = strsep(&line, " \t")) != NULL;) { |
| 460 | if (**ap != '\0') { |
| 461 | if (argc >= max) break; |
| 462 | if (strcasecmp(*ap,"quit") == 0 || strcasecmp(*ap,"exit") == 0) |
| 463 | exit(0); |
| 464 | ap++; |
| 465 | argc++; |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | config.repeat = 1; |
| 470 | cliSendCommand(argc, convertToSds(argc, args)); |
| 471 | line = buffer; |
| 472 | } |
| 473 | |
| 474 | exit(0); |
| 475 | } |
| 476 | |
| 477 | int main(int argc, char **argv) { |
| 478 | int firstarg; |
| 479 | char **argvcopy; |
| 480 | struct redisCommand *rc; |
| 481 | |
| 482 | config.hostip = "127.0.0.1"; |
| 483 | config.hostport = 6379; |
| 484 | config.repeat = 1; |
| 485 | config.dbnum = 0; |
| 486 | config.interactive = 0; |
| 487 | |
| 488 | firstarg = parseOptions(argc,argv); |
| 489 | |
| 490 | argc -= firstarg; |
| 491 | argv += firstarg; |
| 492 | |
| 493 | if (config.interactive == 1) repl(); |
| 494 | if (argc < 1) usage(); |
| 495 | |
| 496 | argvcopy = convertToSds(argc, argv); |
| 497 | |
| 498 | /* Read the last argument from stdandard input if needed */ |
| 499 | if ((rc = lookupCommand(argv[0])) != NULL) { |
| 500 | if (rc->arity > 0 && argc == rc->arity-1) { |
| 501 | sds lastarg = readArgFromStdin(); |
| 502 | argvcopy[argc] = lastarg; |
| 503 | argc++; |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | return cliSendCommand(argc, argvcopy); |
| 508 | } |