From 24eba2833074496375aea61b0de9f12a24867a95 Mon Sep 17 00:00:00 2001 From: Martin Dvorak Date: Tue, 10 Dec 2019 08:10:29 +0100 Subject: [PATCH] Adding .zsh_history to .zhistory fallback #367 --- src/hstr_history.c | 13 +++++++------ src/hstr_utils.c | 8 ++++++++ src/include/hstr_favorites.h | 1 - src/include/hstr_history.h | 2 ++ src/include/hstr_utils.h | 3 +++ 5 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/hstr_history.c b/src/hstr_history.c index 8a809e6..3b20d60 100644 --- a/src/hstr_history.c +++ b/src/hstr_history.c @@ -49,15 +49,16 @@ char* get_history_file_name(void) { char* historyFile = getenv(ENV_VAR_HISTFILE); if(!historyFile || strlen(historyFile)==0) { - char* home = getenv(ENV_VAR_HOME); - char* fileHistory; if(isZshParentShell()) { - fileHistory=FILE_ZSH_HISTORY; + historyFile = get_home_file_path(FILE_ZSH_HISTORY); + if(access(historyFile, F_OK) == -1) { + free(historyFile); + historyFile = get_home_file_path(FILE_ZSH_ZHISTORY); + // ... fallback if this file doesn't exist? + } } else { - fileHistory=FILE_DEFAULT_HISTORY; + historyFile = get_home_file_path(FILE_DEFAULT_HISTORY); } - historyFile = malloc(strlen(home) + 1 + strlen(fileHistory) + 1); - strcat(strcat(strcpy(historyFile, home), "/"), fileHistory); } else { // allocate so that this function always returns string to be freed // (getenv() returns pointer (no need to free), home is allocated (must be freed) diff --git a/src/hstr_utils.c b/src/hstr_utils.c index 2bfb7df..1431b6c 100644 --- a/src/hstr_utils.c +++ b/src/hstr_utils.c @@ -164,6 +164,14 @@ void get_hostname(int bufferSize, char *buffer) strcpy(buffer, "localhost"); } +char* get_home_file_path(char* filename) +{ + char* home = getenv(ENV_VAR_HOME); + char* path = malloc(strlen(home) + 1 + strlen(filename) + 1); + strcat(strcat(strcpy(path, home), "/"), filename); + return path; +} + void toggle_case(char *str, bool lowercase) { if(str && strlen(str)>0) { int i; diff --git a/src/include/hstr_favorites.h b/src/include/hstr_favorites.h index 80a57d1..d7e60ec 100644 --- a/src/include/hstr_favorites.h +++ b/src/include/hstr_favorites.h @@ -22,7 +22,6 @@ #include "hashset.h" #define ENV_VAR_USER "USER" -#define ENV_VAR_HOME "HOME" #define FILE_HSTR_FAVORITES ".hstr_favorites" diff --git a/src/include/hstr_history.h b/src/include/hstr_history.h index 80ca7d1..9639d9f 100644 --- a/src/include/hstr_history.h +++ b/src/include/hstr_history.h @@ -25,6 +25,7 @@ #include #include +#include "hstr_utils.h" #include "hstr_regexp.h" #include "radixsort.h" #include "hstr_favorites.h" @@ -33,6 +34,7 @@ #define FILE_DEFAULT_HISTORY ".bash_history" #define FILE_ZSH_HISTORY ".zsh_history" +#define FILE_ZSH_ZHISTORY ".zhistory" #define ZSH_HISTORY_ITEM_OFFSET 15 #define BASH_HISTORY_ITEM_OFFSET 0 diff --git a/src/include/hstr_utils.h b/src/include/hstr_utils.h index 660b041..f02dcf0 100644 --- a/src/include/hstr_utils.h +++ b/src/include/hstr_utils.h @@ -28,6 +28,8 @@ #include #include +#define ENV_VAR_HOME "HOME" + #define UNUSED_ARG(expr) do { (void)(expr); } while (0) #define MIN(a,b) (((a)<(b))?(a):(b)) @@ -43,6 +45,7 @@ void tiocsti(); void fill_terminal_input(char* cmd, bool padding); void reverse_char_pointer_array(char** array, unsigned length); void get_hostname(int bufferSize, char* buffer); +char* get_home_file_path(char* filename); void toggle_case(char* str, bool lowercase); bool isZshParentShell(void);