/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/02 13:22:57 by ljiriste #+# #+# */
-/* Updated: 2024/08/26 09:23:40 by ljiriste ### ########.fr */
+/* Updated: 2024/08/27 14:31:03 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
# include "minishell_structs.h"
# include "libft.h"
+char *get_line(void);
+void terminate_input(void);
+
void handle_input(char **line, t_execution_env *env);
void unquote_field(char *field);
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* input.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/08/27 14:22:08 by ljiriste #+# #+# */
+/* Updated: 2024/08/27 14:31:04 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "minishell.h"
+#include <stdlib.h>
+#include <unistd.h> // getcwd
+#include "libft.h"
+
+#ifndef NOLEAKS
+# include <stdio.h> // readline
+# include <readline/readline.h> // readline
+# include <readline/history.h> // readline
+#endif // NOLEAKS
+
+#ifndef NOLEAKS
+
+// A string "is_almost_empty" if it:
+// is a NULL pointer
+// is an empty string
+// only contains whitespace
+static int is_almost_empty(const char *str)
+{
+ if (!str)
+ return (1);
+ while (*str)
+ {
+ if (!ft_isspace(*str))
+ return (0);
+ ++str;
+ }
+ return (1);
+}
+
+char *get_line(void)
+{
+ char *cwd;
+ char *prompt;
+ char *line;
+
+ cwd = getcwd(NULL, 0);
+ prompt = ft_strjoin(cwd, "$ ");
+ free(cwd);
+ line = readline(prompt);
+ free(prompt);
+ if (!is_almost_empty(line))
+ add_history(line);
+ return (line);
+}
+
+void terminate_input(void)
+{
+ rl_clear_history();
+ return ;
+}
+
+#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')
+ ft_printf("\n");
+ return (line);
+}
+
+void terminate_input(void)
+{
+ get_next_line(-1);
+ return ;
+}
+
+#endif // NOLEAKS
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/26 13:11:47 by ljiriste #+# #+# */
-/* Updated: 2024/08/27 10:20:29 by ljiriste ### ########.fr */
+/* Updated: 2024/08/27 14:31:19 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
#include "libft.h"
#include <stdlib.h>
-#include <unistd.h> // getcwd
-#ifndef NOLEAKS
-# include <stdio.h> // readline
-# include <readline/readline.h> // readline
-# include <readline/history.h> // readline
-#endif // NOLEAKS
-void print_help(void)
+static 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
-// is an empty string
-// only contains whitespace
-int is_almost_empty(const char *str)
-{
- if (!str)
- return (1);
- while (*str)
- {
- if (!ft_isspace(*str))
- return (0);
- ++str;
- }
- return (1);
-}
-
-char *rl_get_line(void)
-{
- char *cwd;
- char *prompt;
- char *line;
-
- cwd = getcwd(NULL, 0);
- prompt = ft_strjoin(cwd, "$ ");
- free(cwd);
- line = readline(prompt);
- free(prompt);
- if (!is_almost_empty(line))
- add_history(line);
- return (line);
-}
-
/*
// This would be more portable than the main with 3 input args
extern char **environ;
int main(int argc, char **argv)
*/
-int main(int argc, __attribute__((unused)) char **argv, char **envp)
-{
- char *line;
- t_execution_env env;
-
- if (argc > 1)
- {
- print_help();
- return (1);
- }
- if (init_env(&env, envp))
- return (2);
- while (1)
- {
- line = rl_get_line();
- if (!line || !ft_strncmp(line, "exit", 4))
- break ;
- handle_input(&line, &env);
- free(line);
- }
- clean_env(&env);
- rl_clear_history();
- free(line);
- 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')
- ft_printf("\n");
- return (line);
-}
-
int main(int argc, __attribute__((unused)) char **argv, char **envp)
{
char *line;
}
clean_env(&env);
parse(NULL, NULL);
- get_next_line(-1);
+ terminate_input();
free(line);
ft_printf("exit\n");
return (0);
}
-
-#endif // NOLEAKS