Adding .zsh_history to .zhistory fallback #367

This commit is contained in:
Martin Dvorak 2019-12-10 08:10:29 +01:00
parent 0c17ce178b
commit 24eba28330
5 changed files with 20 additions and 7 deletions

View file

@ -49,15 +49,16 @@ char* get_history_file_name(void)
{ {
char* historyFile = getenv(ENV_VAR_HISTFILE); char* historyFile = getenv(ENV_VAR_HISTFILE);
if(!historyFile || strlen(historyFile)==0) { if(!historyFile || strlen(historyFile)==0) {
char* home = getenv(ENV_VAR_HOME);
char* fileHistory;
if(isZshParentShell()) { 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 { } 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 { } else {
// allocate so that this function always returns string to be freed // allocate so that this function always returns string to be freed
// (getenv() returns pointer (no need to free), home is allocated (must be freed) // (getenv() returns pointer (no need to free), home is allocated (must be freed)

View file

@ -164,6 +164,14 @@ void get_hostname(int bufferSize, char *buffer)
strcpy(buffer, "localhost"); 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) { void toggle_case(char *str, bool lowercase) {
if(str && strlen(str)>0) { if(str && strlen(str)>0) {
int i; int i;

View file

@ -22,7 +22,6 @@
#include "hashset.h" #include "hashset.h"
#define ENV_VAR_USER "USER" #define ENV_VAR_USER "USER"
#define ENV_VAR_HOME "HOME"
#define FILE_HSTR_FAVORITES ".hstr_favorites" #define FILE_HSTR_FAVORITES ".hstr_favorites"

View file

@ -25,6 +25,7 @@
#include <stdio.h> #include <stdio.h>
#include <readline/history.h> #include <readline/history.h>
#include "hstr_utils.h"
#include "hstr_regexp.h" #include "hstr_regexp.h"
#include "radixsort.h" #include "radixsort.h"
#include "hstr_favorites.h" #include "hstr_favorites.h"
@ -33,6 +34,7 @@
#define FILE_DEFAULT_HISTORY ".bash_history" #define FILE_DEFAULT_HISTORY ".bash_history"
#define FILE_ZSH_HISTORY ".zsh_history" #define FILE_ZSH_HISTORY ".zsh_history"
#define FILE_ZSH_ZHISTORY ".zhistory"
#define ZSH_HISTORY_ITEM_OFFSET 15 #define ZSH_HISTORY_ITEM_OFFSET 15
#define BASH_HISTORY_ITEM_OFFSET 0 #define BASH_HISTORY_ITEM_OFFSET 0

View file

@ -28,6 +28,8 @@
#include <stdbool.h> #include <stdbool.h>
#include <unistd.h> #include <unistd.h>
#define ENV_VAR_HOME "HOME"
#define UNUSED_ARG(expr) do { (void)(expr); } while (0) #define UNUSED_ARG(expr) do { (void)(expr); } while (0)
#define MIN(a,b) (((a)<(b))?(a):(b)) #define MIN(a,b) (((a)<(b))?(a):(b))
@ -43,6 +45,7 @@ void tiocsti();
void fill_terminal_input(char* cmd, bool padding); void fill_terminal_input(char* cmd, bool padding);
void reverse_char_pointer_array(char** array, unsigned length); void reverse_char_pointer_array(char** array, unsigned length);
void get_hostname(int bufferSize, char* buffer); void get_hostname(int bufferSize, char* buffer);
char* get_home_file_path(char* filename);
void toggle_case(char* str, bool lowercase); void toggle_case(char* str, bool lowercase);
bool isZshParentShell(void); bool isZshParentShell(void);