X-Git-Url: https://git.saurik.com/redis.git/blobdiff_plain/ca36b4ab31be1a1fea24f422028c07c6eeb21120..0b10e1044496699585b401196dd86e19a9f87cc4:/src/redis-cli.c diff --git a/src/redis-cli.c b/src/redis-cli.c index 563c7530..123231d4 100644 --- a/src/redis-cli.c +++ b/src/redis-cli.c @@ -64,6 +64,7 @@ static struct config { char *auth; int raw_output; /* output mode per command */ sds mb_delim; + char prompt[32]; } config; static void usage(); @@ -84,6 +85,13 @@ static long long mstime(void) { 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 *--------------------------------------------------------------------------- */ @@ -263,11 +271,9 @@ static int cliAuth() { /* 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; @@ -488,9 +494,19 @@ static int cliSendCommand(int argc, char **argv, int repeat) { } } - if (cliReadReply(output_raw) != 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(); + } + } } + + free(argvlen); return REDIS_OK; } @@ -627,7 +643,8 @@ static void repl() { } } - 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);