aboutsummaryrefslogtreecommitdiff
path: root/src/parse_cmd.c
diff options
context:
space:
mode:
authorDominik Kaiser2024-07-09 19:23:17 +0200
committerDominik Kaiser2024-07-09 19:23:17 +0200
commit8f386bcd29425b96017c1e9b2cd1bbd670939563 (patch)
treea8a3745009b91dcd38e5c64a2c726728ac8a9788 /src/parse_cmd.c
parent16ca09f57525cd9a4e7eb82ce70e4c1e0cb85a31 (diff)
downloadminishell-8f386bcd29425b96017c1e9b2cd1bbd670939563.tar.gz
minishell-8f386bcd29425b96017c1e9b2cd1bbd670939563.zip
Adding working parsing
TODO: Refactoring
Diffstat (limited to 'src/parse_cmd.c')
-rw-r--r--src/parse_cmd.c122
1 files changed, 91 insertions, 31 deletions
diff --git a/src/parse_cmd.c b/src/parse_cmd.c
index 739c022..4fa2bc2 100644
--- a/src/parse_cmd.c
+++ b/src/parse_cmd.c
@@ -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);
}