]> git.dkaiser.de - 42/minishell.git/commitdiff
Adding working parsing
authorDominik Kaiser <dkaiser@1-C-7.42heilbronn.de>
Tue, 9 Jul 2024 17:23:17 +0000 (19:23 +0200)
committerDominik Kaiser <dkaiser@1-C-7.42heilbronn.de>
Tue, 9 Jul 2024 17:23:17 +0000 (19:23 +0200)
TODO: Refactoring

include/ast.h
include/token.h
src/free_token.c
src/parse_cmd.c

index 6479272b884809ffa44f4450a23168d5b965ea6f..b0ed996d2a45df0dd094c6fb7135f6198ac8a408 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/06/27 11:48:27 by dkaiser           #+#    #+#             */
-/*   Updated: 2024/07/08 16:09:32 by dkaiser          ###   ########.fr       */
+/*   Updated: 2024/07/09 16:54:29 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -34,10 +34,10 @@ typedef struct s_pipe
 
 enum                                           e_redirection_type
 {
-       INPUT_FILE,
-       INPUT_LIMITER,
-       OUTPUT_OVERRIDE,
-       OUTPUT_APPEND
+       INPUT_FILE = 1,
+       INPUT_LIMITER = 2,
+       OUTPUT_OVERRIDE = 4,
+       OUTPUT_APPEND = 8
 };
 
 typedef struct s_redirection
index 32b042da5725e1b62bdf0fac407091b466cd4800..d61195d2d19a149fe5ae2043fbf5642e43025437 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/06/27 13:27:18 by dkaiser           #+#    #+#             */
-/*   Updated: 2024/07/08 14:48:57 by dkaiser          ###   ########.fr       */
+/*   Updated: 2024/07/09 18:38:50 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 
 enum                                           e_token_type
 {
-       STRING_TOKEN,
-       PIPE_TOKEN,
-       REDIR_TOKEN,
-       NEWLINE_TOKEN
+       STRING_TOKEN = 1,
+       PIPE_TOKEN = 2,
+       REDIR_TOKEN = 4,
+       NEWLINE_TOKEN = 8
 };
 
 union                                          u_token_content
index 083484c0818766f218441345c675afa774aeb6d9..a7b1ef213c2a412f3ac71ed3c75d8b15954b88f2 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/06/27 14:38:57 by dkaiser           #+#    #+#             */
-/*   Updated: 2024/07/08 16:44:38 by dkaiser          ###   ########.fr       */
+/*   Updated: 2024/07/09 14:30:02 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -23,8 +23,6 @@ void  free_token(t_token *token)
 
 void   free_token_and_connect(t_token *token)
 {
-       if (token->type == STRING_TOKEN)
-               free(token->content.string);
        if (token->previous != NULL)
                token->previous->next = token->next;
        if (token->next != NULL)
index 739c02219e7946efc7b3d66535a553ac935bd0d2..4fa2bc2defc0f44f02076e137dc833ca798f6353 100644 (file)
@@ -6,17 +6,13 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/07/08 15:06:25 by dkaiser           #+#    #+#             */
-/*   Updated: 2024/07/08 17:29:22 by dkaiser          ###   ########.fr       */
+/*   Updated: 2024/07/09 19:22:51 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
-#include "libft.h"
 #include "minishell.h"
-#include "token.h"
-#include <stdlib.h>
-#include <string.h>
 
-static t_redirection   **collect_redirs(t_token **tokens);
+static t_redirection   *collect_redirs(t_token **tokens);
 static t_assign                        **collect_assigns(t_token **tokens);
 static char                            **collect_args(t_token **tokens);
 static t_assign                        *to_assign(char *str);
@@ -25,7 +21,7 @@ t_node        *parse_cmd(t_token *tokens)
 {
        char                    **args;
        t_assign                **assigns;
-       t_redirection   redirs[2];
+       t_redirection   *redirs;
 
        redirs = collect_redirs(&tokens);
        assigns = collect_assigns(&tokens);
@@ -33,80 +29,144 @@ t_node     *parse_cmd(t_token *tokens)
        return (new_cmd_node(args, assigns, redirs));
 }
 
-static t_redirection   **collect_redirs(t_token **tokens)
+static t_redirection   *collect_redirs(t_token **tokens)
 {
-       return (NULL);
+       t_redirection   *result;
+       t_token                 *cur;
+
+       cur = *tokens;
+       result = malloc(sizeof(t_redirection) * 2);
+       if (result == NULL)
+       {
+               // free all tokens
+               return (NULL);
+       }
+       result[0].specifier = NULL;
+       result[0].type = 0;
+       result[1].specifier = NULL;
+       result[1].type = 0;
+       while (cur != NULL && cur->next != NULL)
+       {
+               if (cur->type == REDIR_TOKEN && cur->next->type == STRING_TOKEN)
+               {
+                       if (cur->content.redir_type & (INPUT_FILE | INPUT_LIMITER))
+                       {
+                               result[0].type = cur->content.redir_type;
+                               result[0].specifier = cur->next->content.string;
+                       }
+                       else if (cur->content.redir_type & (OUTPUT_APPEND | OUTPUT_OVERRIDE))
+                       {
+                               result[1].type = cur->content.redir_type;
+                               result[1].specifier = cur->next->content.string;
+                       }
+                       cur = cur->next;
+                       free_token_and_connect(cur->previous);
+                       if (cur->next != NULL)
+                       {
+                               cur = cur->next;
+                               free_token_and_connect(cur->previous);
+                       }
+                       else
+                       {
+                               free_token(cur);
+                       }
+               }
+               else if (cur->type == REDIR_TOKEN)
+               {
+                       // err Parsing error
+                       free(result);
+                       return (NULL);
+               }
+               else
+               {
+                       cur = cur->next;
+               }
+       }
+       return (result);
 }
 
 static t_assign        **collect_assigns(t_token **tokens)
 {
-       int                     i;
+       t_token         *cur;
        t_assign        **result;
+       int                     i;
 
+       cur = *tokens;
        i = 0;
-       while (ft_strchr(tokens[i]->content.string, '=') != NULL)
+       while (cur != NULL && cur->type == STRING_TOKEN
+               && cur->content.string[0] != '"' && cur->content.string[0] != '\''
+               && ft_strchr(cur->content.string, '=') != NULL)
        {
                i++;
+               cur = cur->next;
        }
        result = malloc(sizeof(t_assign *) * (i + 1));
-       if (result == NULL)
+       if (!result)
        {
-               // free everything
+               // free all tokens
                return (NULL);
        }
-       result[i] = NULL;
-       i--;
-       while (i >= 0)
+       cur = *tokens;
+       i = 0;
+       while (cur != NULL && cur->type == STRING_TOKEN
+               && cur->content.string[0] != '"' && cur->content.string[0] != '\''
+               && ft_strchr(cur->content.string, '=') != NULL)
        {
-               result[i] = to_assign(tokens[i]->content.string);
-               i--;
+               result[i] = to_assign(cur->content.string);
+               i++;
+               cur = cur->next;
+               free_token(cur->previous);
        }
+       *tokens = cur;
+       result[i] = NULL;
        return (result);
 }
 
 static t_assign        *to_assign(char *str)
 {
        t_assign        *result;
-       char            *split;
+       char            *split_pos;
 
+       split_pos = ft_strchr(str, '=');
+       *split_pos = '\0';
        result = malloc(sizeof(t_assign));
        if (result == NULL)
+       {
                return (NULL);
-       split = ft_strchr(str, '=');
-       if (split == NULL)
-               return (NULL);
-       *split = '\0';
+       }
        result->var = str;
-       result->value = split + 1;
+       result->value = split_pos + 1;
        return (result);
 }
 
 static char    **collect_args(t_token **tokens)
 {
-       t_token *cur;
-       char **result;
-       int i;
+       t_token *cur;
+       char    **result;
+       int             i;
 
        cur = *tokens;
        i = 0;
-       while (cur != NULL) {
+       while (cur != NULL)
+       {
                i++;
                cur = cur->next;
        }
-       result = malloc(sizeof(char*) * (i + 1));
+       result = malloc(sizeof(char *) * (i + 1));
        if (!result)
        {
-               //free all tokens;
+               // free all tokens;
                return (NULL);
        }
        cur = *tokens;
        i = 0;
-       while(cur != NULL)
+       while (cur != NULL)
        {
                result[i] = cur->content.string;
                // free token
                i++;
                cur = cur->next;
        }
+       result[i] = NULL;
        return (result);
 }