Refactor input methods
authorLukáš Jiřiště <jiriste@icpf.cas.cz>
Tue, 27 Aug 2024 12:33:12 +0000 (14:33 +0200)
committerLukáš Jiřiště <jiriste@icpf.cas.cz>
Tue, 27 Aug 2024 12:33:12 +0000 (14:33 +0200)
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.

Makefile
inc/minishell.h
src/input_method.c [new file with mode: 0644]
src/main.c

index 27fa6d7bfe557a37f102ec4500714d52a4ab3e6c..71683a06ba46c40b96f4fe619d64deb76f3c2359 100644 (file)
--- 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        \
index 368990c3365292799b85180bb9dd33754e9299a8..43452addadba3cc302a2bbdd433f5ceb4c1e65f6 100644 (file)
@@ -6,7 +6,7 @@
 /*   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       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -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 (file)
index 0000000..93eb000
--- /dev/null
@@ -0,0 +1,95 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   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
index 708d3196f56972358955901bc435014baf39ba05..8d7e2dbf654a5b24d97aa4e6f97784748820bd4b 100644 (file)
@@ -6,7 +6,7 @@
 /*   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;
@@ -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