From f64e1f055dc90bebb69b9ce64b80ad59ec5df09d Mon Sep 17 00:00:00 2001 From: whaffman Date: Wed, 5 Feb 2025 16:24:17 +0100 Subject: [PATCH 1/4] split functions cleanup init and free process --- Makefile | 18 ++++----- inc/minishell.h | 8 ++-- inc/prompt.h | 5 +-- inc/typedef.h | 30 +++++++-------- inc/utils.h | 24 ++++++++++++ src/enviroment/parse_enviroment.c | 13 ++++++- src/enviroment/print_enviroment.c | 4 +- src/main.c | 63 ++++++------------------------- src/prompt/prompt.c | 10 ++--- src/utils/cmp_value.c | 24 ++++++++++++ src/utils/free_minishell.c | 22 +++++++++++ src/utils/free_minishell_line.c | 23 +++++++++++ src/utils/init_minishell.c | 30 +++++++++++++++ src/utils/print_banner.c | 27 +++++++++++++ src/utils/print_list.c | 21 +++++++++++ src/utils/simple_builtins.c | 26 +++++++++++++ 16 files changed, 255 insertions(+), 93 deletions(-) create mode 100644 inc/utils.h create mode 100644 src/utils/cmp_value.c create mode 100644 src/utils/free_minishell.c create mode 100644 src/utils/free_minishell_line.c create mode 100644 src/utils/init_minishell.c create mode 100644 src/utils/print_banner.c create mode 100644 src/utils/print_list.c create mode 100644 src/utils/simple_builtins.c diff --git a/Makefile b/Makefile index b33949b..e29142e 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,12 @@ # **************************************************************************** # # # -# ::: :::::::: # -# Makefile :+: :+: :+: # -# +:+ +:+ +:+ # -# By: qmennen +#+ +:+ +#+ # -# +#+#+#+#+#+ +#+ # -# Created: 2024/10/15 11:48:46 by whaffman #+# #+# # -# Updated: 2025/02/04 16:57:05 by qmennen ### ########.fr # +# :::::::: # +# Makefile :+: :+: # +# +:+ # +# By: qmennen +#+ # +# +#+ # +# Created: 2024/10/15 11:48:46 by whaffman #+# #+# # +# Updated: 2025/02/05 16:02:39 by whaffman ######## odam.nl # # # # **************************************************************************** # @@ -22,7 +22,7 @@ LIBFT = $(LIBFT_PATH)/libft.a OBJ_PATH = obj -VPATH = src:src/enviroment:src/prompt:src/tokenizer +VPATH = src:src/enviroment:src/prompt:src/tokenizer:src/utils SOURCES = $(shell basename -a $(shell find $(SRC_PATH) -type f -name "*.c")) OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o)) @@ -31,7 +31,7 @@ DEPENDS = ${OBJECTS:.o=.d} CC = cc RM = rm -rf -INCLUDES = -I./$(INC_PATH) -I./$(LIBFT_INC_PATH) +INCLUDES = -I./$(INC_PATH) -I./$(LIBFT_INC_PATH) CFLAGS = -Wall -Wextra -Werror -fsanitize=address,undefined -MMD UNAME_S := $(shell uname -s) diff --git a/inc/minishell.h b/inc/minishell.h index b6a1bd1..39766f4 100644 --- a/inc/minishell.h +++ b/inc/minishell.h @@ -6,20 +6,20 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/02/04 16:13:13 by whaffman #+# #+# */ -/* Updated: 2025/02/05 12:37:14 by whaffman ######## odam.nl */ +/* Updated: 2025/02/05 16:06:21 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ #ifndef MINISHELL_H # define MINISHELL_H -# include "typedef.h" # include "allowed.h" # include "libft.h" +# include "typedef.h" # include "enviroment.h" # include "prompt.h" # include "tokenizer.h" - +# include "utils.h" # define TRUE 1 # define FALSE 0 @@ -37,6 +37,4 @@ # define RESET "\001\033[0m\002" -int ft_isspace(const char c); - #endif diff --git a/inc/prompt.h b/inc/prompt.h index 8d64c5f..e3db725 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 11:54:38 by whaffman ######## odam.nl */ +/* Updated: 2025/02/05 16:05:44 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -15,8 +15,7 @@ # include "minishell.h" -typedef struct s_minishell t_minishell; // Forward declaration WHY? - +void print_banner(void); char *ft_prompt(t_minishell *minishell); #endif // PROMPT_H diff --git a/inc/typedef.h b/inc/typedef.h index ee4eb4e..6d13724 100644 --- a/inc/typedef.h +++ b/inc/typedef.h @@ -6,27 +6,13 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/02/05 12:36:08 by whaffman #+# #+# */ -/* Updated: 2025/02/05 12:36:44 by whaffman ######## odam.nl */ +/* Updated: 2025/02/05 15:25:40 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ #ifndef TYPEDEF_H # define TYPEDEF_H - -typedef struct s_enviroment -{ - char *name; - char *value; - struct s_enviroment *next; -} t_enviroment; - -typedef struct s_minishell -{ - t_enviroment *enviroment; - -} t_minishell; - typedef enum e_token_type { T_WORD, @@ -38,6 +24,13 @@ typedef enum e_token_type T_ERROR } t_token_type; +typedef struct s_enviroment +{ + char *name; + char *value; + struct s_enviroment *next; +} t_enviroment; + typedef struct s_token { t_token_type type; @@ -53,4 +46,11 @@ typedef struct s_lexer char current_char; } t_lexer; +typedef struct s_minishell +{ + t_enviroment *enviroment; + char *line; + t_lexer *lexer; + t_list *tokens; +} t_minishell; #endif // TYPEDEF_H diff --git a/inc/utils.h b/inc/utils.h new file mode 100644 index 0000000..f1b930e --- /dev/null +++ b/inc/utils.h @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* utils.h :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/02/05 16:06:35 by whaffman #+# #+# */ +/* Updated: 2025/02/05 16:23:21 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#ifndef UTILS_H +# define UTILS_H + +void free_minishell_line(t_minishell *minishell); +void free_minishell(t_minishell *minishell); +t_minishell *init_minishell(void); +void print_banner(void); +void print_list(void *content); +int cmp_value(t_list *list, char *str); +void simple_builtins(t_minishell *minishell); + +#endif // UTILS_H diff --git a/src/enviroment/parse_enviroment.c b/src/enviroment/parse_enviroment.c index a8ae268..4f7e7b0 100644 --- a/src/enviroment/parse_enviroment.c +++ b/src/enviroment/parse_enviroment.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* parse_enviroment.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/02/05 15:52:33 by whaffman #+# #+# */ +/* Updated: 2025/02/05 15:52:34 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + #include "minishell.h" int parse_enviroment(char **envp, t_enviroment **enviroment) @@ -5,7 +17,6 @@ int parse_enviroment(char **envp, t_enviroment **enviroment) char **env; *enviroment = NULL; - if (envp == NULL) return (FAILURE); while (*envp != NULL) diff --git a/src/enviroment/print_enviroment.c b/src/enviroment/print_enviroment.c index fab5b22..27192ec 100644 --- a/src/enviroment/print_enviroment.c +++ b/src/enviroment/print_enviroment.c @@ -6,13 +6,13 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/02/04 16:13:04 by whaffman #+# #+# */ -/* Updated: 2025/02/04 16:15:52 by whaffman ######## odam.nl */ +/* Updated: 2025/02/05 15:52:44 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" -void print_enviroment(t_enviroment *enviroment) +void print_enviroment(t_enviroment *enviroment) { while (enviroment != NULL) { diff --git a/src/main.c b/src/main.c index b824484..339c202 100644 --- a/src/main.c +++ b/src/main.c @@ -6,70 +6,31 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/02/04 16:19:22 by whaffman #+# #+# */ -/* Updated: 2025/02/05 11:59:23 by whaffman ######## odam.nl */ +/* Updated: 2025/02/05 16:23:08 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" -#include "prompt.h" -void print_banner(void) -{ - ft_printf("%s\n", BOLD RED "__ ________________________________________________________________ __" GREEN); - ft_printf("%s\n", RED "__" GREEN " _______ _____ __ _ _____ _______ _ _ _______ " RED "__"); - ft_printf("%s\n", RED "__" GREEN " | | | | | \\ | | |______ |_____| |______ | | "RED "__"); - ft_printf("%s\n", RED "__" GREEN " | | | __|__ | \\_| __|__ ______| | | |______ |_____ |_____ "RED "__"); - ft_printf("%s\n", RED "__ ________________________________________________________________ __\n" RESET); -} -void print_list(void *content) +int main(int argc, char **argv, char **envp) { - t_token *token; - token = (t_token *)content; - ft_printf("%s\n", token->value); -} - -int main(int argc, char **argv, char **envp) -{ - t_enviroment *enviroment; - char *line; - t_lexer *lexer; - t_list *list; - t_minishell *minishell; + t_minishell *minishell; (void)argc; (void)argv; print_banner(); - minishell = malloc(sizeof(t_minishell)); - if (!minishell) - { - perror("failed assigning minishell memory"); - exit(EXIT_FAILURE); - } - parse_enviroment(envp, &enviroment); - minishell->enviroment = enviroment; + minishell = init_minishell(); + parse_enviroment(envp, &(minishell->enviroment)); while (TRUE) { - line = ft_prompt(minishell); - if (line != NULL) - add_history(line); - lexer = ft_lexer_new(line); - list = ft_parse_input(lexer); - ft_lstiter(list, print_list); - free(line); - if (list != NULL && ft_strncmp(((t_token *)list->content)->value, "clear\0", 6) == 0) - printf("\033[2J\033[1;1H"); - else if (list != NULL && ft_strncmp(((t_token *)list->content)->value, "env\0", 4) == 0) - print_enviroment(enviroment); - else if (list != NULL && ft_strncmp(((t_token *)list->content)->value, "exit\0", 5) == 0) - break; - ft_lstclear(&list, ft_clear_tokenlist); - ft_lexer_free(lexer); + minishell->line = ft_prompt(minishell); + minishell->lexer = ft_lexer_new(minishell->line); + minishell->tokens = ft_parse_input(minishell->lexer); + simple_builtins(minishell); + ft_lstiter(minishell->tokens, print_list); + free_minishell_line(minishell); } - ft_lstclear(&list, ft_clear_tokenlist); - ft_lexer_free(lexer); - // print_enviroment(enviroment); - free_enviroment(enviroment); - free(minishell); + free_minishell(minishell); return (EXIT_SUCCESS); } diff --git a/src/prompt/prompt.c b/src/prompt/prompt.c index 39f9d88..6689d01 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 11:54:12 by whaffman ######## odam.nl */ +/* Updated: 2025/02/05 16:18:18 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -26,14 +26,11 @@ char *get_user(t_enviroment *enviroment) free(str1); str1 = ft_strjoin(str2, RESET "@" GREEN "minishell" RESET ": "); free(str2); - return (str1); } - char *get_path(t_enviroment *enviroment) { - char *home; char *temp; char *cwd; @@ -68,9 +65,7 @@ char *ft_prompt(t_minishell *minishell) cwd = get_path(minishell->enviroment); if (cwd == NULL) - { return (NULL); - } user = get_user(minishell->enviroment); temp = ft_strjoin(user, cwd); free(user); @@ -79,6 +74,7 @@ char *ft_prompt(t_minishell *minishell) free(temp); line = readline(prompt); free(prompt); + if (line != NULL) + add_history(line); return (line); } - diff --git a/src/utils/cmp_value.c b/src/utils/cmp_value.c new file mode 100644 index 0000000..8a2001e --- /dev/null +++ b/src/utils/cmp_value.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* cmp_value.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/02/05 16:13:19 by whaffman #+# #+# */ +/* Updated: 2025/02/05 16:14:51 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int cmp_value(t_list *list, char *str) +{ + if (list != NULL + && ft_strncmp( + ((t_token *)list->content)->value, + str, + ft_strlen(str) + 1) == 0) + return (TRUE); + return (FALSE); +} diff --git a/src/utils/free_minishell.c b/src/utils/free_minishell.c new file mode 100644 index 0000000..b32f9a1 --- /dev/null +++ b/src/utils/free_minishell.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* free_minishell.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/02/05 16:03:57 by whaffman #+# #+# */ +/* Updated: 2025/02/05 16:04:18 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void free_minishell(t_minishell *minishell) +{ + if (minishell->line) + free_minishell_line(minishell); + if (minishell->enviroment) + free_enviroment(minishell->enviroment); + free(minishell); +} diff --git a/src/utils/free_minishell_line.c b/src/utils/free_minishell_line.c new file mode 100644 index 0000000..1ca764e --- /dev/null +++ b/src/utils/free_minishell_line.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* free_minishell_line.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/02/05 16:01:44 by whaffman #+# #+# */ +/* Updated: 2025/02/05 16:02:07 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void free_minishell_line(t_minishell *minishell) +{ + if (minishell->line) + free(minishell->line); + if (minishell->lexer) + ft_lexer_free(minishell->lexer); + if (minishell->tokens) + ft_lstclear(&minishell->tokens, ft_clear_tokenlist); +} diff --git a/src/utils/init_minishell.c b/src/utils/init_minishell.c new file mode 100644 index 0000000..3deda12 --- /dev/null +++ b/src/utils/init_minishell.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* init_minishell.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/02/05 16:03:03 by whaffman #+# #+# */ +/* Updated: 2025/02/05 16:03:15 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +t_minishell *init_minishell(void) +{ + t_minishell *minishell; + + minishell = malloc(sizeof(t_minishell)); + if (!minishell) + { + perror("failed assigning minishell memory"); + exit(EXIT_FAILURE); + } + minishell->enviroment = NULL; + minishell->line = NULL; + minishell->lexer = NULL; + minishell->tokens = NULL; + return (minishell); +} diff --git a/src/utils/print_banner.c b/src/utils/print_banner.c new file mode 100644 index 0000000..b2b155f --- /dev/null +++ b/src/utils/print_banner.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* print_banner.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/02/05 16:04:44 by whaffman #+# #+# */ +/* Updated: 2025/02/05 16:05:07 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void print_banner(void) +{ + ft_printf("%s\n", BOLD RED "__ ___________________________" + "_____________________________________ __" GREEN); + ft_printf("%s\n", RED "__" GREEN " _______ _____ __ _ __" + "___ _______ _ _ _______ " RED "__"); + ft_printf("%s\n", RED "__" GREEN " | | | | | \\ | " + " | |______ |_____| |______ | | "RED "__"); + ft_printf("%s\n", RED "__" GREEN " | | | __|__ | \\_| _" + "_|__ ______| | | |______ |_____ |_____ "RED "__"); + ft_printf("%s\n", RED "__ ________________________________" + "________________________________ __\n" RESET); +} diff --git a/src/utils/print_list.c b/src/utils/print_list.c new file mode 100644 index 0000000..bc1b16a --- /dev/null +++ b/src/utils/print_list.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* print_list.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/02/05 16:12:14 by whaffman #+# #+# */ +/* Updated: 2025/02/05 16:12:45 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void print_list(void *content) +{ + t_token *token; + + token = (t_token *)content; + ft_printf("%s\n", token->value); +} diff --git a/src/utils/simple_builtins.c b/src/utils/simple_builtins.c new file mode 100644 index 0000000..35d2a86 --- /dev/null +++ b/src/utils/simple_builtins.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* simple_builtins.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/02/05 16:21:39 by whaffman #+# #+# */ +/* Updated: 2025/02/05 16:22:47 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void simple_builtins(t_minishell *minishell) +{ + if (cmp_value(minishell->tokens, "clear")) + printf("\033[2J\033[1;1H"); + else if (cmp_value(minishell->tokens, "env")) + print_enviroment(minishell->enviroment); + else if (cmp_value(minishell->tokens, "exit")) + { + free_minishell(minishell); + exit(EXIT_SUCCESS); + } +} From 88ace0811b468a3c6fc50c5cbbd67a7f3c8ba597 Mon Sep 17 00:00:00 2001 From: whaffman Date: Wed, 5 Feb 2025 16:24:37 +0100 Subject: [PATCH 2/4] libft norm --- lib/libft | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/libft b/lib/libft index d76fa93..8072c13 160000 --- a/lib/libft +++ b/lib/libft @@ -1 +1 @@ -Subproject commit d76fa9350d07e61e0b2524a411e8aca5151381e4 +Subproject commit 8072c13f1e064dabf7249e2f148aa6111660dab0 From c2ca3c4176400c62d690a0c7f4f139e2d062869f Mon Sep 17 00:00:00 2001 From: whaffman Date: Wed, 5 Feb 2025 16:29:18 +0100 Subject: [PATCH 3/4] norm --- inc/minishell.h | 3 +-- src/main.c | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/inc/minishell.h b/inc/minishell.h index 39766f4..e02e5d8 100644 --- a/inc/minishell.h +++ b/inc/minishell.h @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/02/04 16:13:13 by whaffman #+# #+# */ -/* Updated: 2025/02/05 16:06:21 by whaffman ######## odam.nl */ +/* Updated: 2025/02/05 16:28:59 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -36,5 +36,4 @@ # define CYAN "\001\033[0;36m\002" # define RESET "\001\033[0m\002" - #endif diff --git a/src/main.c b/src/main.c index 339c202..b069781 100644 --- a/src/main.c +++ b/src/main.c @@ -6,13 +6,12 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/02/04 16:19:22 by whaffman #+# #+# */ -/* Updated: 2025/02/05 16:23:08 by whaffman ######## odam.nl */ +/* Updated: 2025/02/05 16:28:45 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" - int main(int argc, char **argv, char **envp) { t_minishell *minishell; From 39af6d2d529716cc7689433ebe2211bcb77b941b Mon Sep 17 00:00:00 2001 From: whaffman Date: Wed, 5 Feb 2025 17:13:48 +0100 Subject: [PATCH 4/4] 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); }