summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles Hooper <chooper@plumata.com>2014-11-10 22:40:25 -0800
committerantirez <antirez@gmail.com>2015-02-10 14:55:17 +0100
commit1f4074e957f812ddda396b7ff856d05ddf3708ad (patch)
tree18e358a714091dd2372b894f583292a53c021681
parent9e83d2d26f9e1def7e4c1ffc9dc7f969cc4b8b49 (diff)
downloadredis-1f4074e957f812ddda396b7ff856d05ddf3708ad.tar.gz
override histfile from env - fixes #831 and copies #833
-rw-r--r--src/redis-cli.c33
1 files changed, 29 insertions, 4 deletions
diff --git a/src/redis-cli.c b/src/redis-cli.c
index a80ec319c..131d3bf68 100644
--- a/src/redis-cli.c
+++ b/src/redis-cli.c
@@ -61,6 +61,8 @@
#define OUTPUT_CSV 2
#define REDIS_CLI_KEEPALIVE_INTERVAL 15 /* seconds */
#define REDIS_CLI_DEFAULT_PIPE_TIMEOUT 30 /* seconds */
+#define REDIS_CLI_HISTFILE_ENV "REDISCLI_HISTFILE"
+#define REDIS_CLI_HISTFILE_DEFAULT ".rediscli_history"
static redisContext *context;
static struct config {
@@ -142,6 +144,30 @@ static void cliRefreshPrompt(void) {
snprintf(config.prompt+len,sizeof(config.prompt)-len,"> ");
}
+static sds getHistoryPath() {
+ char *path = NULL;
+ sds historyPath = NULL;
+
+ /* check the env for a histfile override */
+ path = getenv(REDIS_CLI_HISTFILE_ENV);
+ if (path != NULL && *path != '\0') {
+ if (!strcmp("/dev/null", path)) {
+ return NULL;
+ }
+
+ /* if the env is set, return it */
+ historyPath = sdscatprintf(sdsempty(), "%s", path);
+ } else {
+ char *home = getenv("HOME");
+ if (home != NULL && *home != '\0') {
+ /* otherwise, return the default */
+ historyPath = sdscatprintf(sdsempty(), "%s/%s", home, REDIS_CLI_HISTFILE_DEFAULT);
+ }
+ }
+
+ return historyPath;
+}
+
/*------------------------------------------------------------------------------
* Help functions
*--------------------------------------------------------------------------- */
@@ -906,10 +932,9 @@ static void repl(void) {
/* Only use history when stdin is a tty. */
if (isatty(fileno(stdin))) {
- history = 1;
-
- if (getenv("HOME") != NULL) {
- historyfile = sdscatprintf(sdsempty(),"%s/.rediscli_history",getenv("HOME"));
+ historyfile = getHistoryPath();
+ if (historyfile != NULL) {
+ history = 1;
linenoiseHistoryLoad(historyfile);
}
}