]> git.saurik.com Git - redis.git/blobdiff - src/redis-cli.c
volatile-lru maxmemory policy segfault fixed, thanks to Anthony Lauzon for reporting...
[redis.git] / src / redis-cli.c
index b191441481018d96cb55bf133da946b0c03bbf70..eef5ad1e07f7776618842d98af0a6a96944cb819 100644 (file)
@@ -67,6 +67,7 @@ static struct config {
 } config;
 
 static void usage();
+char *redisGitSHA1(void);
 
 /*------------------------------------------------------------------------------
  * Utility functions
@@ -157,22 +158,18 @@ static sds cliFormatReply(redisReply *r, char *prefix) {
     sds out = sdsempty();
     switch (r->type) {
     case REDIS_REPLY_ERROR:
-        out = sdscat(out,prefix);
         if (config.tty) out = sdscat(out,"(error) ");
         out = sdscatprintf(out,"%s\n", r->str);
     break;
     case REDIS_REPLY_STATUS:
-        out = sdscat(out,prefix);
         out = sdscat(out,r->str);
         out = sdscat(out,"\n");
     break;
     case REDIS_REPLY_INTEGER:
-        out = sdscat(out,prefix);
         if (config.tty) out = sdscat(out,"(integer) ");
         out = sdscatprintf(out,"%lld\n",r->integer);
     break;
     case REDIS_REPLY_STRING:
-        out = sdscat(out,prefix);
         if (config.raw_output || !config.tty) {
             out = sdscatlen(out,r->str,r->len);
         } else {
@@ -183,23 +180,44 @@ static sds cliFormatReply(redisReply *r, char *prefix) {
         }
     break;
     case REDIS_REPLY_NIL:
-        out = sdscat(out,prefix);
         out = sdscat(out,"(nil)\n");
     break;
     case REDIS_REPLY_ARRAY:
         if (r->elements == 0) {
-            out = sdscat(out,prefix);
             out = sdscat(out,"(empty list or set)\n");
         } else {
-            unsigned int i;
+            unsigned int i, idxlen = 0;
+            char _prefixlen[16];
+            char _prefixfmt[16];
+            sds _prefix;
             sds tmp;
 
+            /* Calculate chars needed to represent the largest index */
+            i = r->elements;
+            do {
+                idxlen++;
+                i /= 10;
+            } while(i);
+
+            /* Prefix for nested multi bulks should grow with idxlen+2 spaces */
+            memset(_prefixlen,' ',idxlen+2);
+            _prefixlen[idxlen+2] = '\0';
+            _prefix = sdscat(sdsnew(prefix),_prefixlen);
+
+            /* Setup prefix format for every entry */
+            snprintf(_prefixfmt,sizeof(_prefixfmt),"%%s%%%dd) ",idxlen);
+
             for (i = 0; i < r->elements; i++) {
-                tmp = cliFormatReply(r->element[i],prefix);
-                out = sdscat(out,prefix);
+                /* Don't use the prefix for the first element, as the parent
+                 * caller already prepended the index number. */
+                out = sdscatprintf(out,_prefixfmt,i == 0 ? "" : prefix,i+1);
+
+                /* Format the multi bulk entry */
+                tmp = cliFormatReply(r->element[i],_prefix);
                 out = sdscatlen(out,tmp,sdslen(tmp));
                 sdsfree(tmp);
             }
+            sdsfree(_prefix);
         }
     break;
     default:
@@ -274,6 +292,7 @@ static int cliSendCommand(int argc, char **argv, int repeat) {
         redisAppendCommandArgv(context,argc,(const char**)argv,argvlen);
         while (config.monitor_mode) {
             if (cliReadReply() != REDIS_OK) exit(1);
+            fflush(stdout);
         }
 
         if (config.pubsub_mode) {
@@ -333,7 +352,7 @@ static int parseOptions(int argc, char **argv) {
 "automatically used as last argument.\n"
             );
         } else if (!strcmp(argv[i],"-v")) {
-            printf("redis-cli shipped with Redis version %s\n", REDIS_VERSION);
+            printf("redis-cli shipped with Redis version %s (%s)\n", REDIS_VERSION, redisGitSHA1());
             exit(0);
         } else {
             break;
@@ -415,8 +434,9 @@ static void repl() {
                             cliPrintContextErrorAndExit();
                     }
                     elapsed = mstime()-start_time;
-                    if (elapsed > 500) printf("%.2f seconds\n",
-                        (double)elapsed/1000);
+                    if (elapsed >= 500) {
+                        printf("(%.2fs)\n",(double)elapsed/1000);
+                    }
                 }
             }
             /* Free the argument vector */