From 6b3503f17d09e0d9a600055068efe78369a2a7eb Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Thu, 2 May 2024 14:40:05 +0200 Subject: [PATCH] Add version without history This version doesn't utilize GNU Readline so it has much lower memory footprint. The main benefit however is that tracking with valgrind is made easier with this. --- Makefile | 3 ++ src/main.c | 93 +++++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 81 insertions(+), 15 deletions(-) diff --git a/Makefile b/Makefile index 573dd59..48ae981 100644 --- a/Makefile +++ b/Makefile @@ -28,6 +28,9 @@ all : $(NAME) debug : CFLAGS += -g debug : $(NAME) +noleaks : CFLAGS += -DNOLEAKS +noleaks : debug + $(NAME) : $(OBJECTS) Libft/libft.a $(CC) $(CFLAGS) -o $@ $^ $(LINKS) diff --git a/src/main.c b/src/main.c index 52c5889..5b18a73 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/26 13:11:47 by ljiriste #+# #+# */ -/* Updated: 2024/05/02 13:54:36 by ljiriste ### ########.fr */ +/* Updated: 2024/05/02 14:53:17 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,9 +14,25 @@ #include "libft.h" #include #include // getcwd -#include // readline -#include // readline -#include // readline + +#ifndef NOLEAKS +# include // readline +# include // readline +# include // readline +#endif // NOLEAKS + +void handle_input(__attribute__((unused)) const char *input, __attribute__((unused)) t_vars *vars) +{ + return ; +} + +void print_help(void) +{ + ft_printf("Minishell should be used without any arguments.\n"); + return ; +} + +#ifndef NOLEAKS // A string is "almost_empty" if it: // is a NULL pointer @@ -51,17 +67,6 @@ char *rl_get_line(void) return (line); } -void handle_input(__attribute__((unused)) const char *input, __attribute__((unused)) t_vars *vars) -{ - return ; -} - -void print_help(void) -{ - ft_printf("Minishell should be used without any arguments.\n"); - return ; -} - /* // This would be more portable than the main with 3 input args extern char **environ; @@ -97,3 +102,61 @@ int main(int argc, __attribute__((unused)) char **argv, char **envp) ft_printf("exit\n"); return (0); } + +#else // NOLEAKS + +char *get_line(void) +{ + char *cwd; + char *prompt; + char *line; + + cwd = getcwd(NULL, 0); + prompt = ft_strjoin(cwd, "$ "); + free(cwd); + ft_printf("%s", prompt); + free(prompt); + line = get_next_line(STDIN_FILENO); + if (!line) + { + ft_printf("\n"); + return (line); + } + if (line[ft_strlen(line) - 1] == '\n') + line[ft_strlen(line) - 1] = '\0'; + else + ft_printf("\n"); + return (line); +} + +int main(int argc, __attribute__((unused)) char **argv, char **envp) +{ + char *line; + t_vars vars; + + if (argc > 1) + { + print_help(); + return (1); + } + if (init_vars(&vars, envp)) + { + clean_vars(&vars); + return (2); + } + while (1) + { + line = get_line(); + if (!line || !ft_strcmp(line, "exit")) + break ; + handle_input(line, &vars); + free(line); + } + clean_vars(&vars); + get_next_line(-1); + free(line); + ft_printf("exit\n"); + return (0); +} + +#endif // NOLEAKS -- 2.30.2