]> git.saurik.com Git - redis.git/blobdiff - redis-cli.c
html doc rebuild
[redis.git] / redis-cli.c
index d2bdb2dd5e6a062249a890bf6c34850fd5aa5c7d..ce8dbde8f863a6462df77a1f8388b2996986bb33 100644 (file)
@@ -54,122 +54,128 @@ static struct config {
     long repeat;
     int dbnum;
     int interactive;
+    int shutdown;
     int monitor_mode;
     int pubsub_mode;
+    int raw_output;
     char *auth;
 } config;
 
 struct redisCommand {
     char *name;
     int arity;
+    int flags;
 };
 
+#define CMDFLAG_NONE 0
+#define CMDFLAG_RAWOUTPUT 1
+
 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}
+    {"auth",2,CMDFLAG_NONE},
+    {"get",2,CMDFLAG_NONE},
+    {"set",3,CMDFLAG_NONE},
+    {"setnx",3,CMDFLAG_NONE},
+    {"setex",4,CMDFLAG_NONE},
+    {"append",3,CMDFLAG_NONE},
+    {"substr",4,CMDFLAG_NONE},
+    {"del",-2,CMDFLAG_NONE},
+    {"exists",2,CMDFLAG_NONE},
+    {"incr",2,CMDFLAG_NONE},
+    {"decr",2,CMDFLAG_NONE},
+    {"rpush",3,CMDFLAG_NONE},
+    {"lpush",3,CMDFLAG_NONE},
+    {"rpop",2,CMDFLAG_NONE},
+    {"lpop",2,CMDFLAG_NONE},
+    {"brpop",-3,CMDFLAG_NONE},
+    {"blpop",-3,CMDFLAG_NONE},
+    {"llen",2,CMDFLAG_NONE},
+    {"lindex",3,CMDFLAG_NONE},
+    {"lset",4,CMDFLAG_NONE},
+    {"lrange",4,CMDFLAG_NONE},
+    {"ltrim",4,CMDFLAG_NONE},
+    {"lrem",4,CMDFLAG_NONE},
+    {"rpoplpush",3,CMDFLAG_NONE},
+    {"sadd",3,CMDFLAG_NONE},
+    {"srem",3,CMDFLAG_NONE},
+    {"smove",4,CMDFLAG_NONE},
+    {"sismember",3,CMDFLAG_NONE},
+    {"scard",2,CMDFLAG_NONE},
+    {"spop",2,CMDFLAG_NONE},
+    {"srandmember",2,CMDFLAG_NONE},
+    {"sinter",-2,CMDFLAG_NONE},
+    {"sinterstore",-3,CMDFLAG_NONE},
+    {"sunion",-2,CMDFLAG_NONE},
+    {"sunionstore",-3,CMDFLAG_NONE},
+    {"sdiff",-2,CMDFLAG_NONE},
+    {"sdiffstore",-3,CMDFLAG_NONE},
+    {"smembers",2,CMDFLAG_NONE},
+    {"zadd",4,CMDFLAG_NONE},
+    {"zincrby",4,CMDFLAG_NONE},
+    {"zrem",3,CMDFLAG_NONE},
+    {"zremrangebyscore",4,CMDFLAG_NONE},
+    {"zunion",-4,CMDFLAG_NONE},
+    {"zinter",-4,CMDFLAG_NONE},
+    {"zrange",-4,CMDFLAG_NONE},
+    {"zrank",3,CMDFLAG_NONE},
+    {"zrevrank",3,CMDFLAG_NONE},
+    {"zrangebyscore",-4,CMDFLAG_NONE},
+    {"zcount",4,CMDFLAG_NONE},
+    {"zrevrange",-4,CMDFLAG_NONE},
+    {"zcard",2,CMDFLAG_NONE},
+    {"zscore",3,CMDFLAG_NONE},
+    {"incrby",3,CMDFLAG_NONE},
+    {"decrby",3,CMDFLAG_NONE},
+    {"getset",3,CMDFLAG_NONE},
+    {"randomkey",1,CMDFLAG_NONE},
+    {"select",2,CMDFLAG_NONE},
+    {"move",3,CMDFLAG_NONE},
+    {"rename",3,CMDFLAG_NONE},
+    {"renamenx",3,CMDFLAG_NONE},
+    {"keys",2,CMDFLAG_NONE},
+    {"dbsize",1,CMDFLAG_NONE},
+    {"ping",1,CMDFLAG_NONE},
+    {"echo",2,CMDFLAG_NONE},
+    {"save",1,CMDFLAG_NONE},
+    {"bgsave",1,CMDFLAG_NONE},
+    {"rewriteaof",1,CMDFLAG_NONE},
+    {"bgrewriteaof",1,CMDFLAG_NONE},
+    {"shutdown",1,CMDFLAG_NONE},
+    {"lastsave",1,CMDFLAG_NONE},
+    {"type",2,CMDFLAG_NONE},
+    {"flushdb",1,CMDFLAG_NONE},
+    {"flushall",1,CMDFLAG_NONE},
+    {"sort",-2,CMDFLAG_NONE},
+    {"info",1,CMDFLAG_RAWOUTPUT},
+    {"mget",-2,CMDFLAG_NONE},
+    {"expire",3,CMDFLAG_NONE},
+    {"expireat",3,CMDFLAG_NONE},
+    {"ttl",2,CMDFLAG_NONE},
+    {"slaveof",3,CMDFLAG_NONE},
+    {"debug",-2,CMDFLAG_NONE},
+    {"mset",-3,CMDFLAG_NONE},
+    {"msetnx",-3,CMDFLAG_NONE},
+    {"monitor",1,CMDFLAG_NONE},
+    {"multi",1,CMDFLAG_NONE},
+    {"exec",1,CMDFLAG_NONE},
+    {"discard",1,CMDFLAG_NONE},
+    {"hset",4,CMDFLAG_NONE},
+    {"hget",3,CMDFLAG_NONE},
+    {"hmset",-4,CMDFLAG_NONE},
+    {"hmget",-3,CMDFLAG_NONE},
+    {"hincrby",4,CMDFLAG_NONE},
+    {"hdel",3,CMDFLAG_NONE},
+    {"hlen",2,CMDFLAG_NONE},
+    {"hkeys",2,CMDFLAG_NONE},
+    {"hvals",2,CMDFLAG_NONE},
+    {"hgetall",2,CMDFLAG_NONE},
+    {"hexists",3,CMDFLAG_NONE},
+    {"config",-2,CMDFLAG_NONE},
+    {"subscribe",-2,CMDFLAG_NONE},
+    {"unsubscribe",-1,CMDFLAG_NONE},
+    {"psubscribe",-2,CMDFLAG_NONE},
+    {"punsubscribe",-1,CMDFLAG_NONE},
+    {"publish",3,CMDFLAG_NONE},
+    {NULL,0,CMDFLAG_NONE}
 };
 
 static int cliReadReply(int fd);
@@ -269,7 +275,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 +314,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) ");
@@ -358,12 +367,15 @@ static int cliSendCommand(int argc, char **argv, int repeat) {
         fprintf(stderr,"Unknown command '%s'\n",argv[0]);
         return 1;
     }
+    config.raw_output = (rc->flags & CMDFLAG_RAWOUTPUT);
 
     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,"shutdown")) config.shutdown = 1;
     if (!strcasecmp(rc->name,"monitor")) config.monitor_mode = 1;
     if (!strcasecmp(rc->name,"subscribe") ||
         !strcasecmp(rc->name,"psubscribe")) config.pubsub_mode = 1;
@@ -401,6 +413,7 @@ static int cliSendCommand(int argc, char **argv, int repeat) {
         }
 
         retval = cliReadReply(fd);
+
         if (retval) {
             return retval;
         }
@@ -568,7 +581,7 @@ static void repl() {
             /* Free the argument vector */
             for (j = 0; j < argc; j++)
                 sdsfree(argv[j]);
-            free(argv);
+            zfree(argv);
         }
         /* linenoise() returns malloc-ed lines like readline() */
         free(line);
@@ -585,9 +598,11 @@ int main(int argc, char **argv) {
     config.hostport = 6379;
     config.repeat = 1;
     config.dbnum = 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);