]> git.dkaiser.de - 42/minishell.git/commitdiff
Fix some more bugs
authorDominik Kaiser <dkaiser@3-H-1.42heilbronn.de>
Mon, 20 Jan 2025 16:32:06 +0000 (17:32 +0100)
committerDominik Kaiser <dkaiser@3-H-1.42heilbronn.de>
Mon, 20 Jan 2025 16:32:06 +0000 (17:32 +0100)
include/minishell.h
src/collect_redirs.c
src/parse_cmd.c

index df89630afc098fca1e4a0677d4de98c03b8a4e6c..ea9f68764d5a3635b031ce511655328ee736b782 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: chuhlig <chuhlig@student.42.fr>            +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/06/22 17:14:49 by dkaiser           #+#    #+#             */
-/*   Updated: 2025/01/20 14:39:38 by dkaiser          ###   ########.fr       */
+/*   Updated: 2025/01/20 17:18:48 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -57,4 +57,9 @@ int                           open_file(char *path, int flags, int mode);
 int                            eval_rec(t_node *node, t_env **env, int in_fd);
 int                    create_files(t_list *files);
 
+typedef struct s_minidata {
+    t_env *env;
+    t_list **create_files;
+} t_minidata;
+
 #endif
index db4ab003da47cc926b623f80c853df7ed5ef8862..e5bc8f65a65c42d803e036c83ad2d9188f6143b4 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: chuhlig <chuhlig@student.42.fr>            +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/08/02 13:49:31 by dkaiser           #+#    #+#             */
-/*   Updated: 2025/01/20 17:12:42 by dkaiser          ###   ########.fr       */
+/*   Updated: 2025/01/20 17:30:00 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include <stdlib.h>
 
 static void                            collect_and_check_redir(t_redirection *result,
-                                                       t_token **cur, t_env *env, t_list **create_files);
+                                                       t_token **cur, t_minidata *data, t_token **tokens);
 static t_redirection   *set_redir(t_redirection *redir, int type, char *spec,
                                                        t_env *env);
 static int                             set_heredoc_data(t_token *cur, t_redirection *result,
                                                        t_env *env);
 
 t_redirection  *collect_redirs(t_token **tokens, t_env *env,
-       t_list **create_files)
+               t_list **create_files)
 {
        t_redirection   *result;
        t_token                 *cur;
+       t_minidata              data;
 
        cur = *tokens;
        result = malloc(sizeof(t_redirection) * 2);
@@ -32,10 +33,12 @@ t_redirection       *collect_redirs(t_token **tokens, t_env *env,
                return (free_tokens(*tokens), NULL);
        set_redir(&result[0], 0, NULL, env);
        set_redir(&result[1], 0, NULL, env);
+       data.create_files = create_files;
+       data.env = env;
        while (cur != NULL)
        {
                if (cur->type == REDIR_TOKEN && cur->next->type == STRING_TOKEN)
-                       collect_and_check_redir(result, &cur, env, create_files);
+                       collect_and_check_redir(result, &cur, &data, tokens);
                else if (cur->type == REDIR_TOKEN)
                        return (free(result), NULL);
                else
@@ -47,7 +50,7 @@ t_redirection *collect_redirs(t_token **tokens, t_env *env,
 }
 
 static void    collect_and_check_redir(t_redirection *result, t_token **cur,
-               t_env *env, t_list **create_files)
+               t_minidata *data, t_token **tokens)
 {
        t_token *next_token;
        char    *str;
@@ -56,22 +59,27 @@ static void collect_and_check_redir(t_redirection *result, t_token **cur,
                str = ft_strdup((*cur)->next->content.string);
        if ((*cur)->content.redir_type == INPUT_LIMITER)
        {
-               if (!set_heredoc_data(*cur, result, env))
+               if (!set_heredoc_data(*cur, result, data->env))
                        return ;
        }
        else if ((*cur)->content.redir_type == INPUT_FILE)
-               ft_lstadd_back(create_files, ft_lstnew(set_redir(&result[0], INPUT_FILE, format_string(str, env, 0), env)));
+               ft_lstadd_back(data->create_files, ft_lstnew(set_redir(&result[0],
+                                       INPUT_FILE, format_string(str, data->env, 0), data->env)));
        else if ((*cur)->content.redir_type == OUTPUT_OVERRIDE)
-               ft_lstadd_back(create_files, ft_lstnew(set_redir(&result[1],
-                                       OUTPUT_OVERRIDE, format_string(str, env, 0), env)));
+               ft_lstadd_back(data->create_files, ft_lstnew(set_redir(&result[1],
+                                       OUTPUT_OVERRIDE, format_string(str, data->env, 0),
+                                       data->env)));
        else if ((*cur)->content.redir_type == OUTPUT_APPEND)
-               ft_lstadd_back(create_files, ft_lstnew(set_redir(&result[1],
-                                       OUTPUT_APPEND, format_string(str, env, 0), env)));
+               ft_lstadd_back(data->create_files, ft_lstnew(set_redir(&result[1],
+                                       OUTPUT_APPEND, format_string(str, data->env, 0),
+                                       data->env)));
        next_token = (*cur)->next;
-       // free_token_and_connect(*cur);
+       free_token_and_connect(*cur);
        if (next_token)
        {
-               free_token_and_connect(*cur);
+               if (next_token->previous == NULL)
+                       *tokens = next_token->next;
+               // free_token_and_connect(*cur);
                *cur = next_token->next;
                free_token_and_connect(next_token);
        }
@@ -101,8 +109,6 @@ static t_redirection        *set_redir(t_redirection *redir, int type, char *spec,
        return (NULL);
 }
 
-
-
 static int     set_heredoc_data(t_token *cur, t_redirection *result, t_env *env)
 {
        char    *heredoc_data;
index d13bf3fbebe5211161287a2946016b013e4ea1a5..5502f74852442e992254126418f6c628aa8ae4cb 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: chuhlig <chuhlig@student.42.fr>            +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/07/08 15:06:25 by dkaiser           #+#    #+#             */
-/*   Updated: 2025/01/20 12:44:44 by chuhlig          ###   ########.fr       */
+/*   Updated: 2025/01/20 17:30:06 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -35,10 +35,10 @@ t_node      *parse_cmd(t_token *tokens, t_env **env)
 
 static char    **collect_args(t_token **tokens, t_env **env)
 {
-       t_token *cur;
-       t_token *next;//2
-       char    **result;
-       int             i;
+       t_token *cur;
+       t_token *next; // 2
+       char **result;
+       int i;
 
        cur = *tokens;
        i = 0;
@@ -51,14 +51,14 @@ static char **collect_args(t_token **tokens, t_env **env)
        i = 0;
        while (cur != NULL && cur->type == STRING_TOKEN)
        {
-               next = cur->next;//2
+               next = cur->next; // 2
                if (cur->previous)
                        free_token(cur->previous);
                result[i] = format_string(cur->content.string, *env, ft_atoi("0"));
                i++;
                // cur = cur->next;
-               cur = next;//2
+               cur = next; // 2
        }
        result[i] = NULL;
        return (result);
-}
\ No newline at end of file
+}