aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-06-16 10:10:48 +0200
committerCharles <sircharlesaze@gmail.com>2020-06-16 10:11:58 +0200
commit46c33fbad5e4965d2d56579e0ce6a97f310b3019 (patch)
tree09180cccae5b8e2628dd6f36461db99ae4ddbc0d /src
parent28ab60062e26347cb1f28dfea63dbd8090252e4d (diff)
parent25ba3ac2ccef9ba8295c3975a1681baa033c5ee6 (diff)
downloadminishell-46c33fbad5e4965d2d56579e0ce6a97f310b3019.tar.gz
minishell-46c33fbad5e4965d2d56579e0ce6a97f310b3019.tar.bz2
minishell-46c33fbad5e4965d2d56579e0ce6a97f310b3019.zip
Merge branch 'parse_cmd' into eval
Diffstat (limited to 'src')
-rw-r--r--src/lexer/lexer.c22
-rw-r--r--src/lexer/trim.c64
-rw-r--r--src/main.c2
-rwxr-xr-xsrc/parse/cmd_parse.c2
-rwxr-xr-xsrc/parse/parse.c52
-rwxr-xr-xsrc/parse/redir_parse.c2
6 files changed, 98 insertions, 46 deletions
diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c
index 1d520c7..49be432 100644
--- a/src/lexer/lexer.c
+++ b/src/lexer/lexer.c
@@ -84,7 +84,6 @@ t_token *lexer_lst_token_str(char *input, int i, int j)
free(lst_token);
return(0);
}
- //printf("%s-\n", lst_token->content);
return (lst_token);
}
@@ -93,7 +92,6 @@ enum e_token_tag token_verif_stick(t_token *lst_token)
int i;
i = ft_strlen(lst_token->content);
-
if (i > 0)
if (lst_token->content[i - 1] == ' ')
return(lst_token->tag);
@@ -109,7 +107,7 @@ enum e_token_tag token_str_or_cote(t_token *lst_token)
{
if(lst_token->content[i] == '\'')
{
- return (lst_token->tag = TAG_STR_SINGLE);
+ lst_token->tag = TAG_STR_SINGLE;
return(token_verif_stick(lst_token));
}
if(lst_token->content[i] == '"')
@@ -127,7 +125,7 @@ enum e_token_tag token_str_or_cote(t_token *lst_token)
return(0);
}
-t_token *push_token_enum_and_trim(t_token *lst_token)
+t_token *push_token_enum(t_token *lst_token)
{
enum e_token_tag tk;
@@ -137,7 +135,6 @@ t_token *push_token_enum_and_trim(t_token *lst_token)
lst_token->tag = token_str_or_cote(lst_token);
else
lst_token->tag = tk;
- //printf("%s-, %d\n",lst_token->content, lst_token->tag);
return (lst_token);
}
@@ -154,7 +151,7 @@ static t_ftlst *create_token_list(char *input, t_ftlst **lst)
j = 0;
j += check_input(&input[i]);
lst_token = lexer_lst_token_str(input,i,j);
- lst_token = push_token_enum_and_trim(lst_token);
+ lst_token = push_token_enum(lst_token);
new = ft_lstnew((void *) lst_token);
ft_lstpush_back(lst, new);
i += j;
@@ -164,16 +161,15 @@ static t_ftlst *create_token_list(char *input, t_ftlst **lst)
t_ftlst *lexer(char *input)
{
- t_ftlst **lst;
- /* int i; */
+ t_ftlst *lst;
if (!input)
return (0);
- lst = malloc(sizeof(t_ftlst *) * 1);
+ lst = malloc(sizeof(t_ftlst ) * 1);
if (!lst)
return(0);
- *lst = NULL;
- *lst = create_token_list(input, lst);
- /* i = ft_lstsize(*lst); */
- return (*lst);
+ lst = NULL;
+ lst = create_token_list(input, &lst);
+ lst = lexe_trim_out(lst);
+ return (lst);
}
diff --git a/src/lexer/trim.c b/src/lexer/trim.c
new file mode 100644
index 0000000..ad696a4
--- /dev/null
+++ b/src/lexer/trim.c
@@ -0,0 +1,64 @@
+
+#include "lexer.h"
+
+char *del_space(t_token *tk)
+{
+ int i;
+ char *s;
+
+ i = 0;
+ while(tk->content[++i] != '\0')
+ {
+ if(tk->content[i] == '\\' && tk->content[i + 1] == ' ')
+ {
+ i += 2;
+ if (tk->content[i] == '\0')
+ tk->tag = tk->tag | TAG_STICK;
+ }
+ if(tk->content[i] == ' ')
+ break;
+ }
+ s = ft_strsubf(tk->content, 0, i);
+ return(s);
+}
+
+char *del_quote(char *str)
+{
+ int i;
+ char *s;
+
+ i = 1;
+ while(str[++i] != '\0')
+ if (str[i] == '\'' || str[i] == '"')
+ break;
+ s = ft_strsubf(str, 1, i - 1);
+ return (s);
+}
+
+t_ftlst *lexe_trim_out(t_ftlst *lst)
+{
+ t_ftlst *first;
+ t_token *tk;
+
+ first = lst;
+ while(lst != NULL)
+ {
+ tk = lst->data;
+ if (tk->tag >= TAG_STR_DOUBLE || tk->tag >= TAG_STR_SINGLE)
+ {
+ tk->content = del_quote(tk->content);
+ if(lst->next == NULL)
+ if (tk->tag & TAG_STICK)
+ tk->tag -= TAG_STICK;
+ }
+ else
+ {
+ tk->content = del_space(tk);
+ if(lst->next == NULL)
+ if (tk->tag & TAG_STICK)
+ tk->tag -= TAG_STICK;
+ }
+ lst = lst->next;
+ }
+ return (first);
+}
diff --git a/src/main.c b/src/main.c
index d1e3e50..c0d611f 100644
--- a/src/main.c
+++ b/src/main.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/28 11:45:44 by cacharle #+# #+# */
-/* Updated: 2020/06/15 17:27:21 by charles ### ########.fr */
+/* Updated: 2020/06/16 10:11:30 by charles ### ########.fr */
/* */
/* ************************************************************************** */
diff --git a/src/parse/cmd_parse.c b/src/parse/cmd_parse.c
index 8b52d0a..c655c5c 100755
--- a/src/parse/cmd_parse.c
+++ b/src/parse/cmd_parse.c
@@ -11,7 +11,7 @@ int parse_cmd_str_true_false(enum e_token_tag tag)
}
-t_ast *parse_cmd(t_ast *ast, t_ftlst *rest)
+t_ast *push_cmd(t_ast *ast, t_ftlst *rest)
{
t_ftlst *new;
diff --git a/src/parse/parse.c b/src/parse/parse.c
index f2c4632..1129696 100755
--- a/src/parse/parse.c
+++ b/src/parse/parse.c
@@ -15,7 +15,6 @@ t_ret *parse(t_ftlst *input)
t_ret *ret;
t_ret *first;
enum e_token_tag tag;
- /* int i = 0; */
if(!(ret = malloc(sizeof(t_ret) * 1)))
return(NULL);
@@ -29,43 +28,36 @@ t_ret *parse(t_ftlst *input)
tag = ((t_token *)ret->rest->data)->tag;
if (parse_cmd_str_true_false(tag))
{
- ret->ast = parse_cmd(ret->ast, ret->rest);
+ ret->ast = push_cmd(ret->ast, ret->rest);
+ if (ret->rest != NULL)
+ ret->rest = ret->rest->next;
}
else if (parse_redir_true_false(tag))
{
- ret->ast = parse_redir(ret->ast, ret->rest);
- ret->rest = ret->rest->next;
- if (ret->rest != NULL)
- ret->ast = parse_redir(ret->ast, ret->rest);
- /* printf("%s\n","ici" ); */
+ ret->ast = push_redir(ret->ast, ret->rest);
ret->rest = ret->rest->next;
- if (ret->rest != NULL)
+ while(ret->rest != NULL)
{
+ ret->ast = push_redir(ret->ast, ret->rest);
tag = ((t_token *)ret->rest->data)->tag;
- if ((tag & TAG_IS_STR) && (tag & TAG_STICK))
- ret->ast = parse_redir(ret->ast, ret->rest);
+ if (tag & TAG_IS_STR && tag & TAG_STICK)
+ ret->rest = ret->rest->next;
+ else
+ break;
}
}
- if (ret->rest != NULL)
- ret->rest = ret->rest->next;
}
-
- /* while(ret->ast->cmd_argv != NULL) */
- /* { */
- /* printf("%s\n", ((t_token *)ret->ast->cmd_argv->data)->content); */
- /* ret->ast->cmd_argv = ret->ast->cmd_argv->next; */
- /* } */
- /* while(ret->ast->redirs != NULL) */
- /* { */
- /* if (i == 0) */
- /* { */
- /* printf("redir"); */
- /* i++; */
- /* } */
- /* printf("%s\n", ((t_token *)ret->ast->redirs->data)->content); */
- /* ret->ast->redirs = ret->ast->redirs->next; */
- /* } */
- /* ast_destroy(ret->ast); */
- /* ft_lstdestroy(&ret->rest, (void (*)(void*))token_destroy); */
+ // while(ret->ast->cmd_argv != NULL)
+ // {
+ // printf("%s\n", ((t_token *)ret->ast->cmd_argv->data)->content);
+ // ret->ast->cmd_argv = ret->ast->cmd_argv->next;
+ // }
+ // while(ret->ast->redirs != NULL)
+ // {
+ // printf("%s\n", ((t_token *)ret->ast->redirs->data)->content);
+ // ret->ast->redirs = ret->ast->redirs->next;
+ // }
+ // ast_destroy(ret->ast);
+ // ft_lstdestroy(&ret->rest, (void (*)(void*))token_destroy);
return first;
}
diff --git a/src/parse/redir_parse.c b/src/parse/redir_parse.c
index c92d639..839e37c 100755
--- a/src/parse/redir_parse.c
+++ b/src/parse/redir_parse.c
@@ -8,7 +8,7 @@ int parse_redir_true_false(enum e_token_tag tag)
return (0);
}
-t_ast *parse_redir(t_ast *ast, t_ftlst *rest)
+t_ast *push_redir(t_ast *ast, t_ftlst *rest)
{
t_ftlst *new;