X-Git-Url: https://git.saurik.com/redis.git/blobdiff_plain/8ff6a48b99dd5e706f542be848a62beaf995229b..a2f4f8711a7ded05d950fe0b784a5e4c7abeb298:/redis-cli.c diff --git a/redis-cli.c b/redis-cli.c index f541b6ab..2daa7c46 100644 --- a/redis-cli.c +++ b/redis-cli.c @@ -53,137 +53,18 @@ static struct config { int hostport; long repeat; int dbnum; + int argn_from_stdin; int interactive; + int shutdown; int monitor_mode; int pubsub_mode; + int raw_output; char *auth; } config; -struct redisCommand { - char *name; - int arity; -}; - -static struct redisCommand cmdTable[] = { - {"auth",2}, - {"get",2}, - {"set",3}, - {"setnx",3}, - {"setex",4}, - {"append",3}, - {"substr",4}, - {"del",-2}, - {"exists",2}, - {"incr",2}, - {"decr",2}, - {"rpush",3}, - {"lpush",3}, - {"rpop",2}, - {"lpop",2}, - {"brpop",-3}, - {"blpop",-3}, - {"llen",2}, - {"lindex",3}, - {"lset",4}, - {"lrange",4}, - {"ltrim",4}, - {"lrem",4}, - {"rpoplpush",3}, - {"sadd",3}, - {"srem",3}, - {"smove",4}, - {"sismember",3}, - {"scard",2}, - {"spop",2}, - {"srandmember",2}, - {"sinter",-2}, - {"sinterstore",-3}, - {"sunion",-2}, - {"sunionstore",-3}, - {"sdiff",-2}, - {"sdiffstore",-3}, - {"smembers",2}, - {"zadd",4}, - {"zincrby",4}, - {"zrem",3}, - {"zremrangebyscore",4}, - {"zmerge",-3}, - {"zmergeweighed",-4}, - {"zrange",-4}, - {"zrank",3}, - {"zrevrank",3}, - {"zrangebyscore",-4}, - {"zcount",4}, - {"zrevrange",-4}, - {"zcard",2}, - {"zscore",3}, - {"incrby",3}, - {"decrby",3}, - {"getset",3}, - {"randomkey",1}, - {"select",2}, - {"move",3}, - {"rename",3}, - {"renamenx",3}, - {"keys",2}, - {"dbsize",1}, - {"ping",1}, - {"echo",2}, - {"save",1}, - {"bgsave",1}, - {"rewriteaof",1}, - {"bgrewriteaof",1}, - {"shutdown",1}, - {"lastsave",1}, - {"type",2}, - {"flushdb",1}, - {"flushall",1}, - {"sort",-2}, - {"info",1}, - {"mget",-2}, - {"expire",3}, - {"expireat",3}, - {"ttl",2}, - {"slaveof",3}, - {"debug",-2}, - {"mset",-3}, - {"msetnx",-3}, - {"monitor",1}, - {"multi",1}, - {"exec",1}, - {"discard",1}, - {"hset",4}, - {"hget",3}, - {"hmset",-4}, - {"hmget",-3}, - {"hincrby",4}, - {"hdel",3}, - {"hlen",2}, - {"hkeys",2}, - {"hvals",2}, - {"hgetall",2}, - {"hexists",3}, - {"config",-2}, - {"subscribe",-2}, - {"unsubscribe",-1}, - {"psubscribe",-2}, - {"punsubscribe",-1}, - {"publish",3}, - {NULL,0} -}; - static int cliReadReply(int fd); static void usage(); -static struct redisCommand *lookupCommand(char *name) { - int j = 0; - while(cmdTable[j].name != NULL) { - if (!strcasecmp(name,cmdTable[j].name)) return &cmdTable[j]; - j++; - } - return NULL; -} - static int cliConnect(void) { char err[ANET_ERR_LEN]; static int fd = ANET_ERR; @@ -269,7 +150,7 @@ static int cliReadBulkReply(int fd) { reply = zmalloc(bulklen); anetRead(fd,reply,bulklen); anetRead(fd,crlf,2); - if (!isatty(fileno(stdout))) { + if (config.raw_output || !isatty(fileno(stdout))) { if (bulklen && fwrite(reply,bulklen,1,stdout) == 0) { zfree(reply); return 1; @@ -308,7 +189,10 @@ static int cliReadMultiBulkReply(int fd) { static int cliReadReply(int fd) { char type; - if (anetRead(fd,&type,1) <= 0) exit(1); + if (anetRead(fd,&type,1) <= 0) { + if (config.shutdown) return 0; + exit(1); + } switch(type) { case '-': printf("(error) "); @@ -350,23 +234,15 @@ static int selectDb(int fd) { } static int cliSendCommand(int argc, char **argv, int repeat) { - struct redisCommand *rc = lookupCommand(argv[0]); + char *command = argv[0]; int fd, j, retval = 0; sds cmd; - if (!rc) { - fprintf(stderr,"Unknown command '%s'\n",argv[0]); - return 1; - } - - if ((rc->arity > 0 && argc != rc->arity) || - (rc->arity < 0 && argc < -rc->arity)) { - fprintf(stderr,"Wrong number of arguments for '%s'\n",rc->name); - return 1; - } - if (!strcasecmp(rc->name,"monitor")) config.monitor_mode = 1; - if (!strcasecmp(rc->name,"subscribe") || - !strcasecmp(rc->name,"psubscribe")) config.pubsub_mode = 1; + config.raw_output = !strcasecmp(command,"info"); + if (!strcasecmp(command,"shutdown")) config.shutdown = 1; + if (!strcasecmp(command,"monitor")) config.monitor_mode = 1; + if (!strcasecmp(command,"subscribe") || + !strcasecmp(command,"psubscribe")) config.pubsub_mode = 1; if ((fd = cliConnect()) == -1) return 1; /* Select db number */ @@ -376,18 +252,17 @@ static int cliSendCommand(int argc, char **argv, int repeat) { return 1; } + /* Build the command to send */ + cmd = sdscatprintf(sdsempty(),"*%d\r\n",argc); + for (j = 0; j < argc; j++) { + cmd = sdscatprintf(cmd,"$%lu\r\n", + (unsigned long)sdslen(argv[j])); + cmd = sdscatlen(cmd,argv[j],sdslen(argv[j])); + cmd = sdscatlen(cmd,"\r\n",2); + } + while(repeat--) { - /* Build the command to send */ - cmd = sdscatprintf(sdsempty(),"*%d\r\n",argc); - for (j = 0; j < argc; j++) { - cmd = sdscatprintf(cmd,"$%lu\r\n", - (unsigned long)sdslen(argv[j])); - cmd = sdscatlen(cmd,argv[j],sdslen(argv[j])); - cmd = sdscatlen(cmd,"\r\n",2); - } anetWrite(fd,cmd,sdslen(cmd)); - sdsfree(cmd); - while (config.monitor_mode) { cliReadSingleLineReply(fd,0); } @@ -438,6 +313,8 @@ static int parseOptions(int argc, char **argv) { i++; } else if (!strcmp(argv[i],"-i")) { config.interactive = 1; + } else if (!strcmp(argv[i],"-c")) { + config.argn_from_stdin = 1; } else { break; } @@ -464,7 +341,7 @@ static sds readArgFromStdin(void) { static void usage() { fprintf(stderr, "usage: redis-cli [-h host] [-p port] [-a authpw] [-r repeat_times] [-n db_num] [-i] cmd arg1 arg2 arg3 ... argN\n"); - fprintf(stderr, "usage: echo \"argN\" | redis-cli [-h host] [-a authpw] [-p port] [-r repeat_times] [-n db_num] cmd arg1 arg2 ... arg(N-1)\n"); + fprintf(stderr, "usage: echo \"argN\" | redis-cli -c [-h host] [-p port] [-a authpw] [-r repeat_times] [-n db_num] cmd arg1 arg2 ... arg(N-1)\n"); fprintf(stderr, "\nIf a pipe from standard input is detected this data is used as last argument.\n\n"); fprintf(stderr, "example: cat /etc/passwd | redis-cli set my_passwd\n"); fprintf(stderr, "example: redis-cli get my_passwd\n"); @@ -476,7 +353,7 @@ static void usage() { /* Turn the plain C strings into Sds strings */ static char **convertToSds(int count, char** args) { int j; - char **sds = zmalloc(sizeof(char*)*count+1); + char **sds = zmalloc(sizeof(char*)*count); for(j = 0; j < count; j++) sds[j] = sdsnew(args[j]); @@ -579,15 +456,17 @@ static void repl() { int main(int argc, char **argv) { int firstarg; char **argvcopy; - struct redisCommand *rc; config.hostip = "127.0.0.1"; config.hostport = 6379; config.repeat = 1; config.dbnum = 0; + config.argn_from_stdin = 0; + config.shutdown = 0; config.interactive = 0; config.monitor_mode = 0; config.pubsub_mode = 0; + config.raw_output = 0; config.auth = NULL; firstarg = parseOptions(argc,argv); @@ -604,16 +483,11 @@ int main(int argc, char **argv) { if (argc == 0 || config.interactive == 1) repl(); - argvcopy = convertToSds(argc, argv); - - /* Read the last argument from stdandard input if needed */ - if ((rc = lookupCommand(argv[0])) != NULL) { - if (rc->arity > 0 && argc == rc->arity-1) { + argvcopy = convertToSds(argc+1, argv); + if (config.argn_from_stdin) { sds lastarg = readArgFromStdin(); argvcopy[argc] = lastarg; argc++; - } } - return cliSendCommand(argc, argvcopy, config.repeat); }