From a7dae7d30b7087bcd9972792a2ee1248e081cfce Mon Sep 17 00:00:00 2001 From: nass1pro Date: Mon, 15 Jun 2020 13:00:24 +0200 Subject: ADD redir_parse draft --- include/ast.h | 5 ++--- include/lexer.h | 2 ++ include/parser.h | 2 ++ src/ast.c | 7 ++----- src/lexer/lexer.c | 2 +- src/main.c | 2 +- src/parse/cmd_parse.c | 3 ++- src/parse/parse.c | 45 ++++++++++++++++++++++++++++++++++++--------- src/parse/redir_parse.c | 27 +++++++++++++++++++++++++++ 9 files changed, 75 insertions(+), 20 deletions(-) mode change 100644 => 100755 src/parse/cmd_parse.c mode change 100644 => 100755 src/parse/parse.c create mode 100755 src/parse/redir_parse.c diff --git a/include/ast.h b/include/ast.h index 790ac29..4e89c96 100644 --- a/include/ast.h +++ b/include/ast.h @@ -32,6 +32,7 @@ struct s_ast; ** \param sep Type of separator */ + typedef struct s_line { struct s_ast *left; @@ -69,9 +70,7 @@ typedef struct s_ast t_line line; t_ftlst *cmd_argv; }; - t_ftlst *in; - t_ftlst *out; - bool is_append; + t_ftlst *redirs; } t_ast; typedef struct s_ret diff --git a/include/lexer.h b/include/lexer.h index e2abe28..bc1238c 100644 --- a/include/lexer.h +++ b/include/lexer.h @@ -21,6 +21,8 @@ enum e_token_tag TAG_STR_DOUBLE = 1 << 10, TAG_STR_SINGLE = 1 << 11, TAG_STICK = 1 << 12, + TAG_IS_STR = TAG_STR | TAG_STR_SINGLE | TAG_STR_DOUBLE, + TAG_IS_REDIR = TAG_REDIR_IN | TAG_REDIR_OUT | TAG_REDIR_APPEND, }; typedef struct diff --git a/include/parser.h b/include/parser.h index 099d4aa..2e6bae1 100644 --- a/include/parser.h +++ b/include/parser.h @@ -39,7 +39,9 @@ t_ret *parse(t_ftlst *input); t_ast *parse_cmd(t_ast *ast, t_ftlst *ret); +t_ast *parse_redir(t_ast *ast, t_ftlst *rest); int parse_cmd_str_true_false(enum e_token_tag tag); +int parse_redir_true_false(enum e_token_tag tag); #endif diff --git a/src/ast.c b/src/ast.c index 98ca4ac..99d1db3 100644 --- a/src/ast.c +++ b/src/ast.c @@ -30,9 +30,7 @@ t_ast *ast_new(enum e_ast_tag tag) if ((ast = (t_ast*)malloc(sizeof(t_ast))) == NULL) return (NULL); ast->tag = tag; - ast->in = NULL; - ast->out = NULL; - ast->is_append = false; + ast->redirs = NULL; ast->line.left = NULL; ast->line.right = NULL; ast->cmd_argv = NULL; @@ -51,8 +49,7 @@ void ast_destroy(t_ast *ast) if (ast->tag == AST_CMD) { ft_lstdestroy(&ast->cmd_argv, (void (*)(void*))token_destroy); - ft_lstdestroy(&ast->in, (void (*)(void*))token_destroy); - ft_lstdestroy(&ast->out, (void (*)(void*))token_destroy); + ft_lstdestroy(&ast->redirs, (void (*)(void*))token_destroy); } else if (ast->tag == AST_LINE) { diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c index bf4557c..079d77d 100644 --- a/src/lexer/lexer.c +++ b/src/lexer/lexer.c @@ -110,7 +110,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); - //return(token_verif_stick(lst_token)); + return(token_verif_stick(lst_token)); } if(lst_token->content[i] == '"') { diff --git a/src/main.c b/src/main.c index b2d7143..14da075 100644 --- a/src/main.c +++ b/src/main.c @@ -120,7 +120,7 @@ int main(int argc, char **argv) ft_strlcpy(input, argv[1], ft_strlen(argv[1]) + 1); lst = lexer(input); - //parse(lst); + parse(lst); free(input); exit(0); return (0); diff --git a/src/parse/cmd_parse.c b/src/parse/cmd_parse.c old mode 100644 new mode 100755 index 07070a1..562cea3 --- a/src/parse/cmd_parse.c +++ b/src/parse/cmd_parse.c @@ -18,12 +18,13 @@ t_ast *parse_cmd(t_ast *ast, t_ftlst *rest) new = rest->data; if (ast == NULL) { + ast = ast_new(AST_CMD); ast->cmd_argv = ft_lstnew((t_token *)rest->data); } else { - new = ft_lstnew(rest->data); + new = ft_lstnew((t_token *)rest->data); ft_lstpush_back(&ast->cmd_argv, (void *)new); } return (ast); diff --git a/src/parse/parse.c b/src/parse/parse.c old mode 100644 new mode 100755 index 4fc0aa7..bb19bea --- a/src/parse/parse.c +++ b/src/parse/parse.c @@ -15,6 +15,7 @@ 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); @@ -26,18 +27,44 @@ t_ret *parse(t_ftlst *input) while (ret->rest != NULL) { tag = ((t_token *)ret->rest->data)->tag; - printf("%d\n", tag); - if (parse_redir_true_false(tag)) - ; if (parse_cmd_str_true_false(tag)) + { ret->ast = parse_cmd(ret->ast, ret->rest); - 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->rest = ret->rest->next; + if (ret->rest != NULL) + { + tag = ((t_token *)ret->rest->data)->tag; + if(tag & TAG_IS_STR & TAG_STICK) + ret->ast = parse_redir(ret->ast, ret->rest); + } + } + 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; - } - free(ret->ast); + { + 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); return NULL; } diff --git a/src/parse/redir_parse.c b/src/parse/redir_parse.c new file mode 100755 index 0000000..bdcb3ab --- /dev/null +++ b/src/parse/redir_parse.c @@ -0,0 +1,27 @@ + +#include "parser.h" + +int parse_redir_true_false(enum e_token_tag tag) +{ + if (tag & TAG_IS_REDIR) + return (1); + return (0); +} + +t_ast *parse_redir(t_ast *ast, t_ftlst *rest) +{ + t_ftlst *new; + + new = rest->data; + if (ast == NULL) + { + ast = ast_new(AST_CMD); + ast->redirs = ft_lstnew((t_token *)rest->data); + } + else + { + new = ft_lstnew((t_token *)rest->data); + ft_lstpush_back(&ast->redirs, (void *)new); + } + return (ast); +} -- cgit