From: Lukáš Jiřiště Date: Tue, 27 Aug 2024 12:33:12 +0000 (+0200) Subject: Refactor input methods X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=1010bb787bc3ea2e4fb8daed51dc5e150b8fea9c;p=42%2Fminishell.git Refactor input methods Input functions are moved and renamed so that laternatives have the same name. This makes it easier to change main as there is a single version. --- diff --git a/Makefile b/Makefile index 27fa6d7..71683a0 100644 --- a/Makefile +++ b/Makefile @@ -24,6 +24,7 @@ INCLUDE := $(addprefix -I, $(INCDIR)) SRCDIR := src SOURCES := main.c \ + input_method.c \ env.c \ vars.c \ input_handling.c \ diff --git a/inc/minishell.h b/inc/minishell.h index 368990c..43452ad 100644 --- a/inc/minishell.h +++ b/inc/minishell.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ @@ -16,6 +16,9 @@ # 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); diff --git a/src/input_method.c b/src/input_method.c new file mode 100644 index 0000000..93eb000 --- /dev/null +++ b/src/input_method.c @@ -0,0 +1,95 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* input.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/27 14:22:08 by ljiriste #+# #+# */ +/* Updated: 2024/08/27 14:31:04 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" +#include +#include // getcwd +#include "libft.h" + +#ifndef NOLEAKS +# include // readline +# include // readline +# include // 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 diff --git a/src/main.c b/src/main.c index 708d319..8d7e2db 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/08/27 10:20:29 by ljiriste ### ########.fr */ +/* Updated: 2024/08/27 14:31:19 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,55 +14,14 @@ #include "minishell.h" #include "libft.h" #include -#include // getcwd -#ifndef NOLEAKS -# include // readline -# include // readline -# include // 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; @@ -70,57 +29,6 @@ const char **envp = 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; @@ -146,10 +54,8 @@ int main(int argc, __attribute__((unused)) char **argv, char **envp) } clean_env(&env); parse(NULL, NULL); - get_next_line(-1); + terminate_input(); free(line); ft_printf("exit\n"); return (0); } - -#endif // NOLEAKS