char *auth;
int raw_output; /* output mode per command */
sds mb_delim;
+ char prompt[32];
} config;
static void usage();
return mst;
}
+static void cliRefreshPrompt(void) {
+ if (config.dbnum == 0)
+ snprintf(config.prompt,sizeof(config.prompt),"redis> ");
+ else
+ snprintf(config.prompt,sizeof(config.prompt),"redis:%d> ",config.dbnum);
+}
+
/*------------------------------------------------------------------------------
* Help functions
*--------------------------------------------------------------------------- */
/* Send SELECT dbnum to the server */
static int cliSelect() {
redisReply *reply;
- char dbnum[16];
if (config.dbnum == 0) return REDIS_OK;
- snprintf(dbnum,sizeof(dbnum),"%d",config.dbnum);
- reply = redisCommand(context,"SELECT %s",dbnum);
+ reply = redisCommand(context,"SELECT %d",config.dbnum);
if (reply != NULL) {
freeReplyObject(reply);
return REDIS_OK;
switch (r->type) {
case REDIS_REPLY_NIL:
/* Nothing... */
- break;
+ 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;
+ break;
case REDIS_REPLY_INTEGER:
out = sdscatprintf(out,"%lld",r->integer);
- break;
+ break;
case REDIS_REPLY_ARRAY:
for (i = 0; i < r->elements; i++) {
if (i > 0) out = sdscat(out,config.mb_delim);
out = sdscatlen(out,tmp,sdslen(tmp));
sdsfree(tmp);
}
- break;
+ break;
default:
fprintf(stderr,"Unknown reply type: %d\n", r->type);
exit(1);
return REDIS_OK;
}
- output_raw = !strcasecmp(command,"info");
+ output_raw = 0;
+ if (!strcasecmp(command,"info") ||
+ (argc == 2 && !strcasecmp(command,"cluster") &&
+ (!strcasecmp(argv[1],"nodes") ||
+ !strcasecmp(argv[1],"info"))) ||
+ (argc == 2 && !strcasecmp(command,"client") &&
+ !strcasecmp(argv[1],"list")))
+
+ {
+ output_raw = 1;
+ }
+
if (!strcasecmp(command,"help") || !strcasecmp(command,"?")) {
cliOutputHelp(--argc, ++argv);
return REDIS_OK;
if (cliReadReply(output_raw) != REDIS_OK) {
free(argvlen);
return REDIS_ERR;
+ } else {
+ /* Store database number when SELECT was successfully executed. */
+ if (!strcasecmp(command,"select") && argc == 2) {
+ config.dbnum = atoi(argv[1]);
+ cliRefreshPrompt();
+ }
}
}
}
}
- while((line = linenoise(context ? "redis> " : "not connected> ")) != NULL) {
+ cliRefreshPrompt();
+ while((line = linenoise(context ? config.prompt : "not connected> ")) != NULL) {
if (line[0] != '\0') {
argv = sdssplitargs(line,&argc);
if (history) linenoiseHistoryAdd(line);