From 39af6d2d529716cc7689433ebe2211bcb77b941b Mon Sep 17 00:00:00 2001 From: whaffman Date: Wed, 5 Feb 2025 17:13:48 +0100 Subject: [PATCH] add persistent history --- .gitignore | 2 +- inc/prompt.h | 4 +++- src/main.c | 3 ++- src/prompt/ft_load_history.c | 34 ++++++++++++++++++++++++++++++++++ src/prompt/ft_write_history.c | 25 +++++++++++++++++++++++++ src/prompt/load_history.c | 0 src/prompt/prompt.c | 5 ++++- 7 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 src/prompt/ft_load_history.c create mode 100644 src/prompt/ft_write_history.c delete mode 100644 src/prompt/load_history.c diff --git a/.gitignore b/.gitignore index ba5fe3a..293add1 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,4 @@ a.out *.d *.o obj/ -.vscode +.minishell_history diff --git a/inc/prompt.h b/inc/prompt.h index e3db725..155de23 100644 --- a/inc/prompt.h +++ b/inc/prompt.h @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/02/04 16:35:35 by whaffman #+# #+# */ -/* Updated: 2025/02/05 16:05:44 by whaffman ######## odam.nl */ +/* Updated: 2025/02/05 17:05:32 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -17,5 +17,7 @@ void print_banner(void); char *ft_prompt(t_minishell *minishell); +void ft_write_history(char *line); +void ft_load_history(void); #endif // PROMPT_H diff --git a/src/main.c b/src/main.c index b069781..9479f80 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/02/04 16:19:22 by whaffman #+# #+# */ -/* Updated: 2025/02/05 16:28:45 by whaffman ######## odam.nl */ +/* Updated: 2025/02/05 17:06:47 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -19,6 +19,7 @@ int main(int argc, char **argv, char **envp) (void)argc; (void)argv; print_banner(); + ft_load_history(); minishell = init_minishell(); parse_enviroment(envp, &(minishell->enviroment)); while (TRUE) diff --git a/src/prompt/ft_load_history.c b/src/prompt/ft_load_history.c new file mode 100644 index 0000000..cd1816e --- /dev/null +++ b/src/prompt/ft_load_history.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_load_history.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/02/05 16:44:51 by whaffman #+# #+# */ +/* Updated: 2025/02/05 17:11:59 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void ft_load_history(void) +{ + int fd; + char *line; + + fd = open(".minishell_history", O_RDONLY); + if (fd < 0) + return ; + while (TRUE) + { + line = get_next_line(fd); + if (!line) + return ; + line[ft_strlen(line) - 1] = '\0'; + if (*line) + add_history(line); + free(line); + } + close(fd); +} diff --git a/src/prompt/ft_write_history.c b/src/prompt/ft_write_history.c new file mode 100644 index 0000000..38f9901 --- /dev/null +++ b/src/prompt/ft_write_history.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_write_history.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/02/05 17:12:17 by whaffman #+# #+# */ +/* Updated: 2025/02/05 17:12:23 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void ft_write_history(char *line) +{ + int fd; + + fd = open(".minishell_history", O_WRONLY | O_APPEND | O_CREAT, 0644); + if (fd < 0) + return ; + if (*line) + ft_putendl_fd(line, fd); + close(fd); +} diff --git a/src/prompt/load_history.c b/src/prompt/load_history.c deleted file mode 100644 index e69de29..0000000 diff --git a/src/prompt/prompt.c b/src/prompt/prompt.c index 6689d01..46713fe 100644 --- a/src/prompt/prompt.c +++ b/src/prompt/prompt.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/02/04 16:13:08 by whaffman #+# #+# */ -/* Updated: 2025/02/05 16:18:18 by whaffman ######## odam.nl */ +/* Updated: 2025/02/05 17:05:12 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -75,6 +75,9 @@ char *ft_prompt(t_minishell *minishell) line = readline(prompt); free(prompt); if (line != NULL) + { add_history(line); + ft_write_history(line); + } return (line); }