static int cliReadReply(int output_raw_strings) { redisReply *reply; sds out; if (redisGetReply(context,(void**)&reply) != REDIS_OK) { if (config.shutdown) return REDIS_OK; if (config.interactive) { /* Filter cases where we should reconnect */ if (context->err == REDIS_ERR_IO && errno == ECONNRESET) return REDIS_ERR; if (context->err == REDIS_ERR_EOF) return REDIS_ERR; } cliPrintContextErrorAndExit(); return REDIS_ERR; /* avoid compiler warning */ } if (output_raw_strings) { out = cliFormatReplyRaw(reply); } else { if (config.raw_output) { out = cliFormatReplyRaw(reply); out = sdscat(out,"\n"); } else { out = cliFormatReplyTTY(reply,""); } } fwrite(out,sdslen(out),1,stdout); sdsfree(out); freeReplyObject(reply); return REDIS_OK; }
static int cliReadReply(redislite_reply *reply, int output_raw_strings) { sds out; if (reply == NULL) { cliPrintContextErrorAndExit(); return REDISLITE_ERR; /* avoid compiler warning */ } if (output_raw_strings) { out = cliFormatReplyRaw(reply); } else { if (config.raw_output) { out = cliFormatReplyRaw(reply); out = sdscat(out, "\n"); } else { out = cliFormatReplyTTY(reply, ""); } } fwrite(out, sdslen(out), 1, stdout); sdsfree(out); redislite_free_reply(reply); return REDISLITE_OK; }
static sds cliFormatReplyRaw(redisReply *r) { sds out = sdsempty(), tmp; size_t i; switch (r->type) { case REDIS_REPLY_NIL: /* Nothing... */ break; case REDIS_REPLY_ERROR: out = sdscatlen(out,r->str,r->len); out = sdscatlen(out,"\n",1); break; case REDIS_REPLY_STATUS: case REDIS_REPLY_STRING: out = sdscatlen(out,r->str,r->len); break; case REDIS_REPLY_INTEGER: out = sdscatprintf(out,"%lld",r->integer); break; case REDIS_REPLY_ARRAY: for (i = 0; i < r->elements; i++) { if (i > 0) out = sdscat(out,config.mb_delim); tmp = cliFormatReplyRaw(r->element[i]); out = sdscatlen(out,tmp,sdslen(tmp)); sdsfree(tmp); } break; default: fprintf(stderr,"Unknown reply type: %d\n", r->type); exit(1); } return out; }
static int cliReadReply(int output_raw_strings) { void *_reply; redisReply *reply; sds out = NULL; int output = 1; if (redisGetReply(context,&_reply) != REDIS_OK) { if (config.shutdown) return REDIS_OK; if (config.interactive) { /* Filter cases where we should reconnect */ if (context->err == REDIS_ERR_IO && errno == ECONNRESET) return REDIS_ERR; if (context->err == REDIS_ERR_EOF) return REDIS_ERR; } cliPrintContextError(); exit(1); return REDIS_ERR; /* avoid compiler warning */ } reply = (redisReply*)_reply; /* Check if we need to connect to a different node and reissue the * request. */ if (config.cluster_mode && reply->type == REDIS_REPLY_ERROR && (!strncmp(reply->str,"MOVED",5) || !strcmp(reply->str,"ASK"))) { char *p = reply->str, *s; int slot; output = 0; /* Comments show the position of the pointer as: * * [S] for pointer 's' * [P] for pointer 'p' */ s = strchr(p,' '); /* MOVED[S]3999 127.0.0.1:6381 */ p = strchr(s+1,' '); /* MOVED[S]3999[P]127.0.0.1:6381 */ *p = '\0'; slot = atoi(s+1); s = strchr(p+1,':'); /* MOVED 3999[P]127.0.0.1[S]6381 */ *s = '\0'; sdsfree(config.hostip); config.hostip = sdsnew(p+1); config.hostport = atoi(s+1); if (config.interactive) printf("-> Redirected to slot [%d] located at %s:%d\n", slot, config.hostip, config.hostport); config.cluster_reissue_command = 1; } if (output) { if (output_raw_strings) { out = cliFormatReplyRaw(reply); } else { if (config.output == OUTPUT_RAW) { out = cliFormatReplyRaw(reply); out = sdscat(out,"\n"); } else if (config.output == OUTPUT_STANDARD) { out = cliFormatReplyTTY(reply,""); } else if (config.output == OUTPUT_CSV) { out = cliFormatReplyCSV(reply); out = sdscat(out,"\n"); } } fwrite(out,sdslen(out),1,stdout); sdsfree(out); } freeReplyObject(reply); return REDIS_OK; }