- if (anetRead(fd,&type,1) <= 0) {
- if (config.shutdown) return 0;
- exit(1);
- }
- switch(type) {
- case '-':
- printf("(error) ");
- cliReadSingleLineReply(fd,0);
- return 1;
- case '+':
- return cliReadSingleLineReply(fd,0);
- case ':':
- printf("(integer) ");
- return cliReadSingleLineReply(fd,0);
- case '$':
- return cliReadBulkReply(fd);
- case '*':
- return cliReadMultiBulkReply(fd);
+static sds cliFormatReply(redisReply *r, char *prefix) {
+ sds out = sdsempty();
+ switch (r->type) {
+ case REDIS_REPLY_ERROR:
+ if (config.tty) out = sdscat(out,"(error) ");
+ out = sdscatprintf(out,"%s\n", r->str);
+ break;
+ case REDIS_REPLY_STATUS:
+ out = sdscat(out,r->str);
+ out = sdscat(out,"\n");
+ break;
+ case REDIS_REPLY_INTEGER:
+ if (config.tty) out = sdscat(out,"(integer) ");
+ out = sdscatprintf(out,"%lld\n",r->integer);
+ break;
+ case REDIS_REPLY_STRING:
+ if (config.raw_output || !config.tty) {
+ out = sdscatlen(out,r->str,r->len);
+ } else {
+ /* If you are producing output for the standard output we want
+ * a more interesting output with quoted characters and so forth */
+ out = sdscatrepr(out,r->str,r->len);
+ out = sdscat(out,"\n");
+ }
+ break;
+ case REDIS_REPLY_NIL:
+ out = sdscat(out,"(nil)\n");
+ break;
+ case REDIS_REPLY_ARRAY:
+ if (r->elements == 0) {
+ out = sdscat(out,"(empty list or set)\n");
+ } else {
+ 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++) {
+ /* 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;