History
Command history management: loading, saving, and searching.
Collection of functions for managing the shell’s command history.
- Author
pulgamecanica
Typedefs
-
typedef struct s_shell t_shell
Functions
-
void history_init(t_shell *shell)
Initialize the history subsystem.
Forward declaration to avoid circular dependency with 42sh.h
This sets up readline’s history handling and loads the history file if available.
- Parameters:
shell – The main shell state struct, used to store the history file path.
-
char *history_file_path(void)
Determine the path to the history file based on environment variables.
Follows POSIX-compliant lookup order: first checks $HISTFILE, then falls back to $HOME/.sh_history. Returns a malloc’d string that the caller must free.
- Returns:
A malloc’d string with the history file path, or NULL if neither variable is set or valid.
-
void history_load(const char *file_path)
Load the command history from a file into readline’s in-memory history list.
This function uses readline’s
read_history()to populate the history list. Iffile_pathis NULL, this function does nothing.- Parameters:
file_path – The path to the history file to load, or NULL to skip loading.
-
void history_save(const char *file_path)
Save the in-memory command history list to a file.
This function uses readline’s
write_history()to write the history list to disk. Iffile_pathis NULL, this function does nothing.- Parameters:
file_path – The path to the history file to save, or NULL to skip saving.
history.c
Command history management using GNU Readline.
This module provides functions to initialize the command history subsystem, determine the history file path, load history from a file, and save history to a file.
The history file is determined by checking the $HISTFILE environment variable first, and if that is not set, falling back to $HOME/.sh_history. The history is loaded into memory at shell startup and saved back to disk on shell exit.
- Author
pulgamecanica
Functions
-
void history_init(t_shell *shell)
Initialize the history subsystem.
This sets up readline’s history handling and loads the history file if available.
- Parameters:
shell – The main shell state struct, used to store the history file path.
-
char *history_file_path(void)
Determine the path to the history file based on environment variables.
Follows POSIX-compliant lookup order: first checks $HISTFILE, then falls back to $HOME/.sh_history. Returns a malloc’d string that the caller must free.
- Returns:
A malloc’d string with the history file path, or NULL if neither variable is set or valid.
-
void history_load(const char *file_path)
Load the command history from a file into readline’s in-memory history list.
This function uses readline’s
read_history()to populate the history list. Iffile_pathis NULL, this function does nothing.- Parameters:
file_path – The path to the history file to load, or NULL to skip loading.
-
void history_save(const char *file_path)
Save the in-memory command history list to a file.
This function uses readline’s
write_history()to write the history list to disk. Iffile_pathis NULL, this function does nothing.- Parameters:
file_path – The path to the history file to save, or NULL to skip saving.