]> git.dkaiser.de - 42/minishell.git/commitdiff
Refactor collect_redirs
authorDominik Kaiser <dkaiser@3-H-6.42heilbronn.de>
Wed, 15 Jan 2025 17:35:29 +0000 (18:35 +0100)
committerDominik Kaiser <dkaiser@3-H-6.42heilbronn.de>
Wed, 15 Jan 2025 17:35:29 +0000 (18:35 +0100)
Makefile
include/minishell.h
src/collect_redirs.c
src/read_heredoc.c [new file with mode: 0644]

index 415d92476773045df325a147af4c4a94fc7b70d6..650f5e4e7b676ffb319272cb5022a0158e239ae4 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -20,6 +20,7 @@ SRC     := main.c debug_tools.c init.c signal_handling.c repl.c new_token.c \
            parse_cmd.c collect_redirs.c print_ast.c interpreter.c env.c \
            get_cmd_path.c env_to_strlst.c execute_cmd.c format_string.c \
                   builtins_part_one.c builtins_part_two.c env_tools.c error.c \
+                  read_heredoc.c
 
 OBJ_DIR := _obj
 OBJ     := $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o))
index 2a426cf6e739b86f6dbd409e9f335783f5c682f8..3b97f6a9c2fcef42f99853ba06066bc7acdb4d39 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: chuhlig <chuhlig@student.42.fr>            +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/06/22 17:14:49 by dkaiser           #+#    #+#             */
-/*   Updated: 2025/01/15 17:20:56 by dkaiser          ###   ########.fr       */
+/*   Updated: 2025/01/15 18:24:09 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -42,8 +42,9 @@ int                           eval(t_node *node, t_env **env);
 char                   *get_cmd_path(char *cmd, t_env *env, int *return_code);
 int                            execute_cmd(t_cmd *cmd, t_env **env);
 char                   *format_string(char *str, t_env *env);
-int set_return_code(int return_code, t_env **env);
+int                            set_return_code(int return_code, t_env **env);
 int                            handle_redirections(t_redirection *redirs);
-void   *error(int err_code, char *err_text, int exit_code, int *ret_code);
-
+void                   *error(int err_code, char *err_text, int exit_code,
+                                       int *ret_code);
+char                   *read_heredoc(char *delimiter);
 #endif
index 02c866fd321e21d749b5ce352d05e17e867f9ac0..2947c52ce0a7e3c88e6910b145385b04c6788a10 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: chuhlig <chuhlig@student.42.fr>            +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/08/02 13:49:31 by dkaiser           #+#    #+#             */
-/*   Updated: 2025/01/15 18:10:46 by dkaiser          ###   ########.fr       */
+/*   Updated: 2025/01/15 18:33:36 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 
 static void            collect_and_check_redir(t_redirection *result, t_token **cur,
                                        t_env *env);
-static void            set_redir(t_redirection *redir, int type, char *specifier,
+static void            set_redir(t_redirection *redir, int type, char *spec,
                                        t_env *env);
-
-static char    *read_heredoc(char *delimiter)
-{
-       char    *line;
-       char    *result;
-       char    *temp;
-       size_t  total_length;
-       size_t  line_length;
-
-       total_length = 0;
-       result = NULL;
-       while (1)
-       {
-               line = readline("> ");
-               if (!line || ft_strcmp(line, delimiter) == 0)
-               {
-                       free(line);
-                       break ;
-               }
-               line_length = ft_strlen(line) + 1;
-               temp = malloc(total_length + line_length + 1);
-               if (!temp)
-               {
-                       perror("malloc");
-                       return (free(result), NULL);
-               }
-               if (result)
-               {
-                       ft_strcpy(temp, result);
-                       free(result);
-               }
-               else
-                       temp[0] = '\0';
-               ft_strcat(temp, line);
-               ft_strcat(temp, "\n");
-               result = temp;
-               total_length += line_length;
-               free(line);
-       }
-       return (result);
-}
+static char            *get_heredoc_data(t_token *cur);
 
 t_redirection  *collect_redirs(t_token **tokens, t_env *env)
 {
@@ -83,42 +43,33 @@ t_redirection       *collect_redirs(t_token **tokens, t_env *env)
        return (result);
 }
 
-static void    set_redir(t_redirection *redir, int type, char *specifier,
-               t_env *env)
+static void    set_redir(t_redirection *redir, int type, char *spec, t_env *env)
 {
        redir->type = type;
-       if (specifier != NULL)
-               redir->specifier = format_string(specifier, env);
+       if (spec != NULL)
+               redir->specifier = format_string(spec, env);
        else
-               redir->specifier = specifier;
+               redir->specifier = spec;
 }
 
 static void    collect_and_check_redir(t_redirection *result, t_token **cur,
                t_env *env)
 {
-       char    *heredoc_data;
        t_token *next_token;
+       char    *str;
 
-       heredoc_data = NULL;
+       if ((*cur)->content.redir_type != INPUT_LIMITER)
+               str = ft_strdup((*cur)->next->content.string);
        if ((*cur)->content.redir_type == INPUT_LIMITER)
        {
-               heredoc_data = read_heredoc((*cur)->next->content.string);
-               if (!heredoc_data)
-               {
-                       perror("Heredoc allocation failed");
-                       return ;
-               }
-               set_redir(&result[0], INPUT_LIMITER, heredoc_data, env);
+               set_redir(&result[0], INPUT_LIMITER, get_heredoc_data(*cur), env);
        }
        else if ((*cur)->content.redir_type == INPUT_FILE)
-               set_redir(&result[0], INPUT_FILE,
-                       ft_strdup((*cur)->next->content.string), env);
+               set_redir(&result[0], INPUT_FILE, str, env);
        else if ((*cur)->content.redir_type == OUTPUT_OVERRIDE)
-               set_redir(&result[1], OUTPUT_OVERRIDE,
-                       ft_strdup((*cur)->next->content.string), env);
+               set_redir(&result[1], OUTPUT_OVERRIDE, str, env);
        else if ((*cur)->content.redir_type == OUTPUT_APPEND)
-               set_redir(&result[1], OUTPUT_APPEND,
-                       ft_strdup((*cur)->next->content.string), env);
+               set_redir(&result[1], OUTPUT_APPEND, str, env);
        next_token = (*cur)->next;
        free_token_and_connect(*cur);
        if (next_token)
@@ -129,3 +80,20 @@ static void collect_and_check_redir(t_redirection *result, t_token **cur,
        else
                *cur = NULL;
 }
+
+static char    *get_heredoc_data(t_token *cur)
+{
+       char    *heredoc_data;
+
+       if (cur->content.redir_type == INPUT_LIMITER)
+       {
+               heredoc_data = read_heredoc(cur->next->content.string);
+               if (!heredoc_data)
+               {
+                       perror("Heredoc allocation failed");
+                       return ;
+               }
+               set_redir(&result[0], INPUT_LIMITER, heredoc_data, env);
+       }
+       return (heredoc_data);
+}
diff --git a/src/read_heredoc.c b/src/read_heredoc.c
new file mode 100644 (file)
index 0000000..78efbd7
--- /dev/null
@@ -0,0 +1,54 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   read_heredoc.c                                     :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2025/01/15 18:22:09 by dkaiser           #+#    #+#             */
+/*   Updated: 2025/01/15 18:22:52 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "minishell.h"
+
+char   *read_heredoc(char *delimiter)
+{
+       char    *line;
+       char    *result;
+       char    *temp;
+       size_t  total_length;
+       size_t  line_length;
+
+       total_length = 0;
+       result = NULL;
+       while (1)
+       {
+               line = readline("> ");
+               if (!line || ft_strcmp(line, delimiter) == 0)
+               {
+                       free(line);
+                       break ;
+               }
+               line_length = ft_strlen(line) + 1;
+               temp = malloc(total_length + line_length + 1);
+               if (!temp)
+               {
+                       perror("malloc");
+                       return (free(result), NULL);
+               }
+               if (result)
+               {
+                       ft_strcpy(temp, result);
+                       free(result);
+               }
+               else
+                       temp[0] = '\0';
+               ft_strcat(temp, line);
+               ft_strcat(temp, "\n");
+               result = temp;
+               total_length += line_length;
+               free(line);
+       }
+       return (result);
+}