/* 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
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;
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