From b5710182b87b17c14cabb5f178f0516ff7676177 Mon Sep 17 00:00:00 2001 From: nass1pro Date: Sun, 14 Jun 2020 15:52:25 +0200 Subject: Added parse cmd draft --- src/lexer/lexer.c | 9 ++- src/main.c | 195 ++++++++++++++++++++++++++------------------------ src/parse/cmd_parse.c | 3 + src/parse/parse.c | 49 ++++++++++++- 4 files changed, 154 insertions(+), 102 deletions(-) create mode 100644 src/parse/cmd_parse.c (limited to 'src') diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c index 29cea9c..04d7edb 100644 --- a/src/lexer/lexer.c +++ b/src/lexer/lexer.c @@ -109,8 +109,8 @@ enum e_token_tag token_str_or_cote(t_token *lst_token) { if(lst_token->content[i] == '\'') { - lst_token->tag = TAG_STR_SINGLE; - return(token_verif_stick(lst_token)); + return (lst_token->tag = TAG_STR_SINGLE); + //return(token_verif_stick(lst_token)); } if(lst_token->content[i] == '"') { @@ -136,7 +136,7 @@ t_token *push_token_enum_and_trim(t_token *lst_token) { lst_token->tag = token_str_or_cote(lst_token); } - /* printf("%s-, %d\n",lst_token->content, (int)lst_token->tag); */ + //printf("%s-, %d\n",lst_token->content, (int)lst_token->tag); return (lst_token); } @@ -173,6 +173,5 @@ t_ftlst *lexer(char *input) return(0); *lst = create_token_list(input, lst); /* i = ft_lstsize(*lst); */ - free(lst); - return (0); + return (*lst); } diff --git a/src/main.c b/src/main.c index 5d3e9d8..14da075 100644 --- a/src/main.c +++ b/src/main.c @@ -20,103 +20,108 @@ #include "lexer.h" #include "parser.h" -int main(int argc, char **argv, char **envp) -{ - (void)argc; - (void)argv; - /* (void)envp; */ - /* t_path path; */ - t_env env; - /* char *line; */ - /* int ret; */ - env = env_from_array(envp); - /* path = path_update(NULL, env_search(env, "PATH")); */ - /* */ - /* t_ast *ast; */ - /* t_line line; */ - /* t_cmd cmd; */ - /* t_eval_state state; */ - /* */ - /* cmd.argv = ft_split("ls -l", ' '); */ - /* cmd.in = NULL; */ - /* cmd.out = NULL; */ - /* cmd.is_append = false; */ - /* */ - /* line.left = ast_new(TAG_CMD, &cmd); */ - /* line.right = NULL; */ - /* line.sep = SEP_END; */ - /* ast = ast_new(TAG_LINE, &line); */ - /* printf("%p\n", ast); */ - /* printf("%d\n", ast->tag); */ - /* printf("%p\n", ast->data.line.left); */ - /* printf("%p\n", ast->data.line.right); */ - /* printf("%d\n", ast->data.line.left->tag); */ - /* printf("%p\n", ast->data.line.left->data.cmd.argv); */ - /* printf("%s\n", ast->data.line.left->data.cmd.argv[0]); */ - /* printf("%s\n", ast->data.line.left->data.cmd.argv[1]); */ - /* state.pipe_in[0] = -1; */ - /* state.pipe_in[1] = -1; */ - /* state.pipe_out[0] = -1; */ - /* state.pipe_out[1] = -1; */ - /* state.path = path; */ - /* state.env = env; */ - /* t_io_frame frame; */ - /* io_frame_init(&frame); */ - /* printf("ret: %d %s\n", eval(&frame,&state, ast), strerror(errno)); */ - /* char buf[2048]; */ - /* printf("%s\n", getcwd(buf, 2048)); */ - /* builtin_env(NULL, state.env); */ - /* ast_destroy(ast); */ - /* while ((ret = ft_next_line(STDIN_FILENO, &line)) == 1) */ - /* { */ - /* if (eval(parse(line)) == -1) */ - /* continue ; // and display error */ - /* free(line); */ - /* } */ - /* free(line); */ - /* ft_htdestroy(path, free); */ - /* ms_glob("src#<{(|"); */ - /* char *j = ms_glob("|)}>#*.c"); */ - /* printf("%s\n", j); */ - /* free(j); */ - t_ftvec *v = ft_vecnew(32); - ft_vecpush(v, token_new(TAG_STR, "$TERM$LFS$TERM$TERM.")); - ft_vecpush(v, token_new(TAG_STR, "$$LFS$TERM$TERM.")); - ft_vecpush(v, token_new(TAG_STR, "*/*.c$TERM")); - ft_vecpush(v, token_new(TAG_STR, "src/*.c include/*.h")); - ft_vecpush(v, token_new(TAG_STR, "$A$B")); - ft_vecpush(v, token_new(TAG_STR, "\\$TERM")); - ft_vecpush(v, token_new(TAG_STR, "$TER\\M")); - ft_vecpush(v, token_new(TAG_STR, "\\\\")); - ft_vecpush(v, token_new(TAG_STR_SINGLE, "''''$TEST\\TEST")); - ft_vecpush(v, token_new(TAG_STR_DOUBLE, ",$TEST,$B,")); - ft_vecpush(v, token_new(TAG_STR_DOUBLE | TAG_STICK, "$TEST")); - ft_vecpush(v, token_new(TAG_STR_DOUBLE | TAG_STICK, "$TEST")); - ft_vecpush(v, token_new(TAG_STR_DOUBLE , "$TEST")); - ft_vecpush(v, token_new(TAG_STR_DOUBLE | TAG_STICK, "$TEST")); - ft_vecpush(v, token_new(TAG_STR_SINGLE, "$TEST")); - char **as = preprocess(v, env); - char **tmp = as; - while (*as != NULL) - puts(*as++); - ft_split_destroy(tmp); - ft_vecdestroy(env, free); - return (0); -} +// int main(int argc, char **argv, char **envp) +// { +// (void)argc; +// (void)argv; +// /* (void)envp; */ +// /* t_path path; */ +// t_env env; +// /* char *line; */ +// /* int ret; */ +// env = env_from_array(envp); +// /* path = path_update(NULL, env_search(env, "PATH")); */ +// /* */ +// /* t_ast *ast; */ +// /* t_line line; */ +// /* t_cmd cmd; */ +// /* t_eval_state state; */ +// /* */ +// /* cmd.argv = ft_split("ls -l", ' '); */ +// /* cmd.in = NULL; */ +// /* cmd.out = NULL; */ +// /* cmd.is_append = false; */ +// /* */ +// /* line.left = ast_new(TAG_CMD, &cmd); */ +// /* line.right = NULL; */ +// /* line.sep = SEP_END; */ +// /* ast = ast_new(TAG_LINE, &line); */ +// /* printf("%p\n", ast); */ +// /* printf("%d\n", ast->tag); */ +// /* printf("%p\n", ast->data.line.left); */ +// /* printf("%p\n", ast->data.line.right); */ +// /* printf("%d\n", ast->data.line.left->tag); */ +// /* printf("%p\n", ast->data.line.left->data.cmd.argv); */ +// /* printf("%s\n", ast->data.line.left->data.cmd.argv[0]); */ +// /* printf("%s\n", ast->data.line.left->data.cmd.argv[1]); */ +// /* state.pipe_in[0] = -1; */ +// /* state.pipe_in[1] = -1; */ +// /* state.pipe_out[0] = -1; */ +// /* state.pipe_out[1] = -1; */ +// /* state.path = path; */ +// /* state.env = env; */ +// /* t_io_frame frame; */ +// /* io_frame_init(&frame); */ +// /* printf("ret: %d %s\n", eval(&frame,&state, ast), strerror(errno)); */ +// /* char buf[2048]; */ +// /* printf("%s\n", getcwd(buf, 2048)); */ +// /* builtin_env(NULL, state.env); */ +// /* ast_destroy(ast); */ +// /* while ((ret = ft_next_line(STDIN_FILENO, &line)) == 1) */ +// /* { */ +// /* if (eval(parse(line)) == -1) */ +// /* continue ; // and display error */ +// /* free(line); */ +// /* } */ +// /* free(line); */ +// /* ft_htdestroy(path, free); */ +// /* ms_glob("src#<{(|"); */ +// /* char *j = ms_glob("|)}>#*.c"); */ +// /* printf("%s\n", j); */ +// /* free(j); */ +// t_ftvec *v = ft_vecnew(32); +// ft_vecpush(v, token_new(TAG_STR, "$TERM$LFS$TERM$TERM.")); +// ft_vecpush(v, token_new(TAG_STR, "$$LFS$TERM$TERM.")); +// ft_vecpush(v, token_new(TAG_STR, "*/*.c$TERM")); +// ft_vecpush(v, token_new(TAG_STR, "src/*.c include/*.h")); +// ft_vecpush(v, token_new(TAG_STR, "$A$B")); +// ft_vecpush(v, token_new(TAG_STR, "\\$TERM")); +// ft_vecpush(v, token_new(TAG_STR, "$TER\\M")); +// ft_vecpush(v, token_new(TAG_STR, "\\\\")); +// ft_vecpush(v, token_new(TAG_STR_SINGLE, "''''$TEST\\TEST")); +// ft_vecpush(v, token_new(TAG_STR_DOUBLE, ",$TEST,$B,")); +// ft_vecpush(v, token_new(TAG_STR_DOUBLE | TAG_STICK, "$TEST")); +// ft_vecpush(v, token_new(TAG_STR_DOUBLE | TAG_STICK, "$TEST")); +// ft_vecpush(v, token_new(TAG_STR_DOUBLE , "$TEST")); +// ft_vecpush(v, token_new(TAG_STR_DOUBLE | TAG_STICK, "$TEST")); +// ft_vecpush(v, token_new(TAG_STR_SINGLE, "$TEST")); +// char **as = preprocess(v, env); +// char **tmp = as; +// while (*as != NULL) +// puts(*as++); +// ft_split_destroy(tmp); +// ft_vecdestroy(env, free); +// return (0); +// } ///////////////////////////////////////////////////////////////////////////////////////// // lexer main ///////////////////////////////////////////////////////////////////////////////////////// -/* int main(void) */ -/* { */ -/* char *input; */ -/* */ -/* if (!(input = malloc(sizeof(char) * ft_strlen(argv[1]) + 1))) */ -/* return(0); */ -/* ft_strlcpy(input, argv[1], ft_strlen(argv[1]) + 1); */ -/* */ -/* lexer(input); */ -/* exit(0); */ -/* return (0); */ -/* } */ +int main(int argc, char **argv) +{ + char *input; + t_ftlst *lst; + + if (argv[1] == NULL) + return (0); + if (!(input = malloc(sizeof(char) * ft_strlen(argv[1]) + 1))) + return(0); + ft_strlcpy(input, argv[1], ft_strlen(argv[1]) + 1); + + lst = lexer(input); + parse(lst); + free(input); + exit(0); + return (0); +} diff --git a/src/parse/cmd_parse.c b/src/parse/cmd_parse.c new file mode 100644 index 0000000..78ce593 --- /dev/null +++ b/src/parse/cmd_parse.c @@ -0,0 +1,3 @@ + + +#include "parser.h" diff --git a/src/parse/parse.c b/src/parse/parse.c index 92797c8..a3e9526 100644 --- a/src/parse/parse.c +++ b/src/parse/parse.c @@ -4,16 +4,61 @@ */ #include "parser.h" +#include "lexer.h" // stdio.h est deja include dans minishell.h temporairement // (comme ca on doit le retirer a un seul endroit a la fin) +int parse_cmd_str_true_fale(enum e_token_tag tag) +{ + if (tag & TAG_STR || tag & TAG_STR_DOUBLE || tag & TAG_STR_SINGLE) + return (1); + return(0); +} + + +t_ast *parse_cmd(t_ast *ast, t_ftlst *rest) +{ + t_ftlst *new; + + 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); + ft_lstpush_back(&ast->cmd_argv, (void *)new); + } + return (ast); +} -t_ret *parse(t_ftlst *input) +t_ret *parse(t_ftlst *input) { - t_ret *ret; + t_ret *ret; + t_ret *first; + enum e_token_tag tag; if(!(ret = malloc(sizeof(t_ret) * 1))) return(NULL); ret->rest = input; + ret->ast = NULL; + ret->unexpected = NULL; + first = ret; + + while (ret->rest != NULL) + { + tag = ((t_token *)ret->rest->data)->tag; + if (parse_cmd_str_true_fale(tag)) + ret->ast = parse_cmd(ret->ast, ret->rest); + 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); return NULL; } -- cgit From 72ee131562574c4a6cb69800cdd81268b52ace84 Mon Sep 17 00:00:00 2001 From: nass1pro Date: Sun, 14 Jun 2020 17:31:01 +0200 Subject: Change file cmd --- src/lexer/lexer.c | 7 ++++--- src/main.c | 2 +- src/parse/cmd_parse.c | 27 +++++++++++++++++++++++++++ src/parse/parse.c | 29 ++++------------------------- 4 files changed, 36 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c index 04d7edb..bf4557c 100644 --- a/src/lexer/lexer.c +++ b/src/lexer/lexer.c @@ -132,11 +132,12 @@ t_token *push_token_enum_and_trim(t_token *lst_token) enum e_token_tag tk; tk = ret_token(lst_token->content, 0); + if (tk == 0) - { lst_token->tag = token_str_or_cote(lst_token); - } - //printf("%s-, %d\n",lst_token->content, (int)lst_token->tag); + else + lst_token->tag = tk; + //printf("%s-, %d\n",lst_token->content, lst_token->tag); return (lst_token); } diff --git a/src/main.c b/src/main.c index 14da075..b2d7143 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 index 78ce593..07070a1 100644 --- a/src/parse/cmd_parse.c +++ b/src/parse/cmd_parse.c @@ -1,3 +1,30 @@ #include "parser.h" + + +int parse_cmd_str_true_false(enum e_token_tag tag) +{ + if (tag & TAG_STR || tag & TAG_STR_DOUBLE || tag & TAG_STR_SINGLE) + return (1); + return(0); +} + + +t_ast *parse_cmd(t_ast *ast, t_ftlst *rest) +{ + t_ftlst *new; + + 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); + ft_lstpush_back(&ast->cmd_argv, (void *)new); + } + return (ast); +} diff --git a/src/parse/parse.c b/src/parse/parse.c index a3e9526..4fc0aa7 100644 --- a/src/parse/parse.c +++ b/src/parse/parse.c @@ -8,31 +8,7 @@ // stdio.h est deja include dans minishell.h temporairement // (comme ca on doit le retirer a un seul endroit a la fin) -int parse_cmd_str_true_fale(enum e_token_tag tag) -{ - if (tag & TAG_STR || tag & TAG_STR_DOUBLE || tag & TAG_STR_SINGLE) - return (1); - return(0); -} - - -t_ast *parse_cmd(t_ast *ast, t_ftlst *rest) -{ - t_ftlst *new; - 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); - ft_lstpush_back(&ast->cmd_argv, (void *)new); - } - return (ast); -} t_ret *parse(t_ftlst *input) { @@ -50,7 +26,10 @@ t_ret *parse(t_ftlst *input) while (ret->rest != NULL) { tag = ((t_token *)ret->rest->data)->tag; - if (parse_cmd_str_true_fale(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; } -- cgit 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 --- 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 +++++++++++++++++++++++++++ 6 files changed, 69 insertions(+), 17 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 (limited to 'src') 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