From 1e5e7a1bfc5c4336e0499cc64f45f8381c8acb49 Mon Sep 17 00:00:00 2001 From: lderidde Date: Sat, 8 Feb 2025 20:29:32 +0100 Subject: [PATCH] mmoat complete history --- includes/minishell.h | 1 + srcs/main.c | 38 ++++++++++++++++++++++++++++++++------ srcs/msh_struct.c | 3 +++ 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/includes/minishell.h b/includes/minishell.h index cad76c4..6a9c3b8 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -23,6 +23,7 @@ typedef struct s_msh int ex_code; t_ast_n *head; char *input; + int hist; char **env; } t_msh; diff --git a/srcs/main.c b/srcs/main.c index b1a9360..7ea4199 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -11,8 +11,20 @@ /* ************************************************************************** */ #include "../includes/minishell.h" +#include -static char *powerline(void) +static void handle_input(char *in, char *li, char *pr, t_msh *msh) +{ + if (ft_strlen(in) > 0 && !is_only_space(in)) + { + add_history(in); + ft_fprintf(msh->hist, "%s\n", in); + } + free(pr); + free(li); +} + +static char *powerline(t_msh *msh) { char *pwd; char *tilt; @@ -34,13 +46,26 @@ static char *powerline(void) prompt = ft_sprintf("%s\n%s  MMOAT %s%s%s%s%s %s%s%s ", line, POW1, POW2, POW3, POW4, tilt, pwd, RESET, POW5, RESET); input = readline(prompt); - if (ft_strlen(input) > 0) - add_history(input); - free(prompt); - free(line); + handle_input(input, line, prompt, msh); return (input); } +static void add_prevhistory(t_msh *msh) +{ + char *str; + char *tmp; + + str = get_next_line(msh->hist); + while (str) + { + tmp = ft_substr(str, 0, ft_strlen(str) - 1); + add_history(tmp); + free(tmp); + free(str); + str = get_next_line(msh->hist); + } +} + static void interpret_cmd(char **input, t_msh *msh) { msh->head = parser(*input, msh); @@ -62,6 +87,7 @@ int main(int ac, char **av, char **envp) (void)ac; (void)av; msh = init_msh(envp); + add_prevhistory(msh); if (!msh) return (1); if (ac == 1) @@ -69,7 +95,7 @@ int main(int ac, char **av, char **envp) while (1) { while (!msh->input || !msh->input[0] || is_only_space(msh->input)) - msh->input = powerline(); + msh->input = powerline(msh); interpret_cmd(&msh->input, msh); } } diff --git a/srcs/msh_struct.c b/srcs/msh_struct.c index 1affb30..797ed74 100644 --- a/srcs/msh_struct.c +++ b/srcs/msh_struct.c @@ -11,6 +11,7 @@ /* ************************************************************************** */ #include "../includes/minishell.h" +#include static char **ft_setnewenv(void) { @@ -31,6 +32,7 @@ t_msh *init_msh(char **envp) t_msh *msh; msh = malloc(sizeof(t_msh) * 1); + msh->hist = open(".mmoat_hisotry", O_RDWR | O_CREAT | O_APPEND, 0666); msh->ex_code = 0; msh->input = NULL; if (!msh) @@ -45,6 +47,7 @@ t_msh *init_msh(char **envp) void free_msh(t_msh *msh) { free_tab(msh->env); + close(msh->hist); free(msh->input); free(msh); }