Add version without history
authorLukas Jiriste <ljiriste@student.42prague.com>
Thu, 2 May 2024 12:40:05 +0000 (14:40 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Thu, 2 May 2024 12:53:35 +0000 (14:53 +0200)
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
src/main.c

index 573dd599d2608530c056fcfb1cbb96057b79c1b3..48ae981b18d9af4cb361121b637e2d60c4f36afd 100644 (file)
--- 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)
 
index 52c5889807f573f9cb0e1c4dc0e702bf5f48ed91..5b18a73e3ea4ed33802a00f1b72c09e1b7228eb1 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include "libft.h"
 #include <stdlib.h>
 #include <unistd.h>                            // getcwd
-#include <stdio.h>                             // readline
-#include <readline/readline.h> // readline
-#include <readline/history.h>  // readline
+
+#ifndef NOLEAKS
+# include <stdio.h>                            // readline
+# include <readline/readline.h>        // readline
+# include <readline/history.h> // 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