aboutsummaryrefslogtreecommitdiff
path: root/src/parse_cmd.c
diff options
context:
space:
mode:
authorDominik Kaiser2024-08-02 15:29:00 +0200
committerGitHub2024-08-02 15:29:00 +0200
commitc7a4494fd97b7e80665cbd47ed96bc37bf5800e5 (patch)
treed2520064c761e00772185dcdb82f83d566634a85 /src/parse_cmd.c
parent82c1eed4f7795ef135a586af2fb334145b64b2f6 (diff)
downloadminishell-c7a4494fd97b7e80665cbd47ed96bc37bf5800e5.tar.gz
minishell-c7a4494fd97b7e80665cbd47ed96bc37bf5800e5.zip
Refactor parser
* Fix norme for free_node.c * Uglify parser.c * Merge parser updates into refactor-parser * Outsource collect_assigns and collect_redirs * Make parse_cmd.c norme conform * Make free_tokens() norme conform * Make collect_assigns.c norme conform * Some refactoring of collect_redirs * Refactor collect_redirs
Diffstat (limited to 'src/parse_cmd.c')
-rw-r--r--src/parse_cmd.c138
1 files changed, 5 insertions, 133 deletions
diff --git a/src/parse_cmd.c b/src/parse_cmd.c
index 8ed9785..068a4c1 100644
--- a/src/parse_cmd.c
+++ b/src/parse_cmd.c
@@ -6,18 +6,13 @@
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/08 15:06:25 by dkaiser #+# #+# */
-/* Updated: 2024/08/02 13:22:18 by dkaiser ### ########.fr */
+/* Updated: 2024/08/02 14:22:32 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
-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);
-static void set_redir(t_redirection *redir, int type,
- char *specifier);
+static char **collect_args(t_token **tokens);
t_node *parse_cmd(t_token *tokens)
{
@@ -31,120 +26,6 @@ t_node *parse_cmd(t_token *tokens)
return (new_cmd_node(args, assigns, redirs));
}
-static t_redirection *collect_redirs(t_token **tokens)
-{
- t_redirection *result;
- t_token *cur;
- int idx;
-
- cur = *tokens;
- result = malloc(sizeof(t_redirection) * 2);
- if (result == NULL)
- {
- // free all tokens
- return (NULL);
- }
- set_redir(&result[0], 0, NULL);
- set_redir(&result[1], 0, NULL);
- 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))
- idx = 0;
- else if (cur->content.redir_type & (OUTPUT_APPEND | OUTPUT_OVERRIDE))
- idx = 1;
- set_redir(&result[idx], cur->content.redir_type,
- cur->next->content.string);
- cur = cur->next;
- free_token_and_connect(cur->previous);
- if (cur->next != NULL)
- {
- if (cur->previous == NULL)
- *tokens = cur->next;
- 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)
-{
- t_token *cur;
- t_assign **result;
- int i;
-
- 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)
- {
- i++;
- cur = cur->next;
- }
- result = malloc(sizeof(t_assign *) * (i + 1));
- if (!result)
- {
- // free all tokens
- return (NULL);
- }
- 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(cur->content.string);
- i++;
- if (cur->next != NULL)
- {
- cur = cur->next;
- free_token(cur->previous);
- }
- else
- {
- free_token(cur);
- }
- }
- *tokens = cur;
- result[i] = NULL;
- return (result);
-}
-
-static t_assign *to_assign(char *str)
-{
- t_assign *result;
- char *split_pos;
-
- split_pos = ft_strchr(str, '=');
- *split_pos = '\0';
- result = malloc(sizeof(t_assign));
- if (result == NULL)
- {
- return (NULL);
- }
- result->var = str;
- result->value = split_pos + 1;
- return (result);
-}
-
static char **collect_args(t_token **tokens)
{
t_token *cur;
@@ -159,26 +40,17 @@ static char **collect_args(t_token **tokens)
cur = cur->next;
}
result = malloc(sizeof(char *) * (i + 1));
- if (!result)
- {
- // free all tokens;
- return (NULL);
- }
+ if (result == NULL)
+ return (free_tokens(*tokens), NULL);
cur = *tokens;
i = 0;
while (cur != NULL && cur->type == STRING_TOKEN)
{
result[i] = cur->content.string;
- // free token
i++;
cur = cur->next;
+ free_token(cur->previous);
}
result[i] = NULL;
return (result);
}
-
-static void set_redir(t_redirection *redir, int type, char *specifier)
-{
- redir->type = type;
- redir->specifier = specifier;
-}