From 92f2557e7ea689a1515eaec24a8f8454ab4c58b3 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Sun, 13 Nov 2016 23:59:34 +0100 Subject: [PATCH] libft btree --- 42sh/Makefile | 18 ++++++---- 42sh/includes/exec.h | 21 ++++++++++++ .../token.h => includes/lexer_parser.h} | 22 ++++++++++++ .../line-editing => }/includes/line_editing.h | 0 42sh/includes/minishell.h | 3 +- 42sh/libft | 2 +- 42sh/src/exec/exec_pipe.c | 15 ++++++++ 42sh/src/exec/exec_semi.c | 8 +++++ 42sh/src/exec/ft_exec.c | 29 ++++++++++++++++ 42sh/src/lexer-parser/ft_parse.c | 34 +++++++++++++++++++ .../src/{token => lexer-parser}/ft_tokenize.c | 2 +- 42sh/src/lexer-parser/parse_separator.c | 9 +++++ .../{token => lexer-parser}/token_append.c | 2 +- 42sh/src/lexer-parser/token_cmp_type.c | 6 ++++ 42sh/src/{token => lexer-parser}/token_free.c | 2 +- .../{token => lexer-parser}/token_getnext.c | 2 +- 42sh/src/{token => lexer-parser}/token_init.c | 2 +- .../src/{token => lexer-parser}/token_print.c | 2 +- 42sh/src/main/main.c | 10 ++++-- 19 files changed, 172 insertions(+), 17 deletions(-) create mode 100644 42sh/includes/exec.h rename 42sh/{src/token/includes/token.h => includes/lexer_parser.h} (78%) rename 42sh/{src/line-editing => }/includes/line_editing.h (100%) create mode 100644 42sh/src/exec/exec_pipe.c create mode 100644 42sh/src/exec/exec_semi.c create mode 100644 42sh/src/exec/ft_exec.c create mode 100644 42sh/src/lexer-parser/ft_parse.c rename 42sh/src/{token => lexer-parser}/ft_tokenize.c (97%) create mode 100644 42sh/src/lexer-parser/parse_separator.c rename 42sh/src/{token => lexer-parser}/token_append.c (97%) create mode 100644 42sh/src/lexer-parser/token_cmp_type.c rename 42sh/src/{token => lexer-parser}/token_free.c (83%) rename 42sh/src/{token => lexer-parser}/token_getnext.c (98%) rename 42sh/src/{token => lexer-parser}/token_init.c (97%) rename 42sh/src/{token => lexer-parser}/token_print.c (87%) diff --git a/42sh/Makefile b/42sh/Makefile index 9296c46f..bf9102c0 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -11,7 +11,8 @@ D_OBJ = obj F_OBJ = $(notdir $(F_SRC:.c=.o)) DF_OBJ := $(addprefix $(D_OBJ)/, $(F_OBJ)) -D_INC = includes libft/includes src/line-editing/includes src/token/includes +D_INC = includes libft/includes +F_INC := $(shell find $(D_INC) -type f -regex ".*\.h$$") O_INC = $(addprefix -I, $(D_INC)) D_SER = libft/ @@ -30,27 +31,32 @@ RM = /bin/rm -rf all: $(NAME) -$(D_OBJ)/%.o: $(D_SRC)/main/%.c includes/ +$(D_OBJ)/%.o: $(D_SRC)/main/%.c includes/minishell.h @$(MKDIR) $(D_OBJ) @$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS) @echo "Compiling "$<"..." -$(D_OBJ)/%.o: $(D_SRC)/line-editing/%.c src/line-editing/includes +$(D_OBJ)/%.o: $(D_SRC)/builtin/%.c includes/minishell.h @$(MKDIR) $(D_OBJ) @$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS) @echo "Compiling "$<"..." -$(D_OBJ)/%.o: $(D_SRC)/builtin/%.c includes/ +$(D_OBJ)/%.o: $(D_SRC)/minishell-exec/%.c includes/minishell.h @$(MKDIR) $(D_OBJ) @$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS) @echo "Compiling "$<"..." -$(D_OBJ)/%.o: $(D_SRC)/token/%.c src/token/includes +$(D_OBJ)/%.o: $(D_SRC)/line-editing/%.c includes/line_editing.h @$(MKDIR) $(D_OBJ) @$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS) @echo "Compiling "$<"..." -$(D_OBJ)/%.o: $(D_SRC)/minishell-exec/%.c includes/ +$(D_OBJ)/%.o: $(D_SRC)/lexer-parser/%.c includes/lexer_parser.h + @$(MKDIR) $(D_OBJ) + @$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS) + @echo "Compiling "$<"..." + +$(D_OBJ)/%.o: $(D_SRC)/exec/%.c includes/exec.h @$(MKDIR) $(D_OBJ) @$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS) @echo "Compiling "$<"..." diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h new file mode 100644 index 00000000..9a732591 --- /dev/null +++ b/42sh/includes/exec.h @@ -0,0 +1,21 @@ +#ifndef EXEC_H +# define EXEC_H +# include "lexer_parser.h" + +typedef struct s_exec t_exec; + +struct s_exec +{ + t_type type; + int (*f)(t_btree *ast); +}; + +extern t_exec g_exec[]; + +int ft_exec(t_btree *ast); + +int ft_exec(t_btree *ast); +int exec_semi(t_btree *ast); +int exec_pipe(t_btree *ast); + +#endif diff --git a/42sh/src/token/includes/token.h b/42sh/includes/lexer_parser.h similarity index 78% rename from 42sh/src/token/includes/token.h rename to 42sh/includes/lexer_parser.h index 8c765369..af5d4f02 100644 --- a/42sh/src/token/includes/token.h +++ b/42sh/includes/lexer_parser.h @@ -26,7 +26,18 @@ # define TK_WORD 0x0100 typedef long long t_type; +typedef struct s_astnode t_astnode; typedef struct s_token t_token; +typedef struct s_parser t_parser; + +struct s_astnode +{ + t_type type; + union u_astdata + { + int a; + } data; +}; struct s_token { @@ -35,11 +46,22 @@ struct s_token int size; }; +struct s_parser +{ + t_type type; + int (*f)(t_btree **ast, t_list *start, t_list *token); +}; + +extern t_parser g_parser[]; + t_token *token_init(); t_token *token_getnext(int *pos,char *line); int ft_tokenize(t_list **alst, char *str); int token_append(t_token *token, char c); void token_free(void *data, size_t size); +int token_cmp_type(t_type data, t_type ref); void token_print(t_list *lst); +int ft_parse(t_btree **ast, t_list *token); +int parse_separator(t_btree **ast, t_list *start, t_list *lst); #endif diff --git a/42sh/src/line-editing/includes/line_editing.h b/42sh/includes/line_editing.h similarity index 100% rename from 42sh/src/line-editing/includes/line_editing.h rename to 42sh/includes/line_editing.h diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index a153492e..513e6d8b 100644 --- a/42sh/includes/minishell.h +++ b/42sh/includes/minishell.h @@ -15,7 +15,8 @@ # include "libft.h" # include "line_editing.h" -# include "token.h" +# include "lexer_parser.h" +# include "exec.h" # include # include # include diff --git a/42sh/libft b/42sh/libft index d42b309e..7da0a795 160000 --- a/42sh/libft +++ b/42sh/libft @@ -1 +1 @@ -Subproject commit d42b309e42077dd6f0a26cd67171d09cc50de4e1 +Subproject commit 7da0a79521c667ebc5b1cad30f2dd73aa55f2ddb diff --git a/42sh/src/exec/exec_pipe.c b/42sh/src/exec/exec_pipe.c new file mode 100644 index 00000000..842e1d13 --- /dev/null +++ b/42sh/src/exec/exec_pipe.c @@ -0,0 +1,15 @@ +#include "exec.h" + +int exec_pipe(t_btree *ast) +{ + int filedes[2]; + + pipe(filedes); + dup2(filedes[1], 1); + ft_exec(ast->left); + close(filedes[1]); + dup2(filedes[0], 0); + ft_exec(ast->right); + close(filedes[0]); + return (0); +} diff --git a/42sh/src/exec/exec_semi.c b/42sh/src/exec/exec_semi.c new file mode 100644 index 00000000..d73f4120 --- /dev/null +++ b/42sh/src/exec/exec_semi.c @@ -0,0 +1,8 @@ +#include "exec.h" + +int exec_semi(t_btree *ast) +{ + ft_exec(ast->left); + ft_exec(ast->right); + return (0); +} diff --git a/42sh/src/exec/ft_exec.c b/42sh/src/exec/ft_exec.c new file mode 100644 index 00000000..ff3fa240 --- /dev/null +++ b/42sh/src/exec/ft_exec.c @@ -0,0 +1,29 @@ +#include "exec.h" + +t_exec g_exec[] = +{ + {TK_SEMI, &exec_semi}, + {TK_PIPE, &exec_pipe}, + {0, 0}, +}; + +int ft_exec(t_btree *ast) +{ + t_astnode *item; + int i; + + i = 0; + item = ast->item; + if(!ast) + return (0); + while (g_exec[i].type) + { + if (item->type == g_exec[i].type) + { + (*g_exec[i].f)(ast); + return (0); + } + i++; + } + return (0); +} diff --git a/42sh/src/lexer-parser/ft_parse.c b/42sh/src/lexer-parser/ft_parse.c new file mode 100644 index 00000000..98cb3f25 --- /dev/null +++ b/42sh/src/lexer-parser/ft_parse.c @@ -0,0 +1,34 @@ +#include "lexer_parser.h" + +t_parser g_parser[] = +{ + {TK_SEMI, &parse_separator}, + {TK_PIPE, &parse_separator}, + {0, 0}, +}; + +int ft_parse(t_btree **ast, t_list *token) +{ + t_list *lst; + t_astnode item; + int i; + + i = 0; + if(!token) + return (0); + while (g_parser[i].type) + { + if ((lst = ft_lst_find(token, + (int [1]){g_parser[i].type}, &token_cmp_type))) + { + item.type = g_parser[i].type; + *ast = btree_create_node(&item, sizeof(item)); + if (g_parser[i].f) + (*g_parser[i].f)(ast, token, lst); + return (0); + } + else + i++; + } + return (0); +} diff --git a/42sh/src/token/ft_tokenize.c b/42sh/src/lexer-parser/ft_tokenize.c similarity index 97% rename from 42sh/src/token/ft_tokenize.c rename to 42sh/src/lexer-parser/ft_tokenize.c index 2508b4e6..1c905ed5 100644 --- a/42sh/src/token/ft_tokenize.c +++ b/42sh/src/lexer-parser/ft_tokenize.c @@ -10,7 +10,7 @@ /* */ /* ************************************************************************** */ -#include "token.h" +#include "lexer_parser.h" int ft_tokenize(t_list **alst, char *str) { diff --git a/42sh/src/lexer-parser/parse_separator.c b/42sh/src/lexer-parser/parse_separator.c new file mode 100644 index 00000000..2a28cdc6 --- /dev/null +++ b/42sh/src/lexer-parser/parse_separator.c @@ -0,0 +1,9 @@ +#include "lexer_parser.h" + +int parse_separator(t_btree **ast, t_list *start, t_list *lst) +{ + ft_parse(&(*ast)->right, lst->next); + ft_lstdelone(&lst, &token_free); + ft_parse(&(*ast)->left, start); + return (0); +} diff --git a/42sh/src/token/token_append.c b/42sh/src/lexer-parser/token_append.c similarity index 97% rename from 42sh/src/token/token_append.c rename to 42sh/src/lexer-parser/token_append.c index 80296669..082892b4 100644 --- a/42sh/src/token/token_append.c +++ b/42sh/src/lexer-parser/token_append.c @@ -10,7 +10,7 @@ /* */ /* ************************************************************************** */ -#include "token.h" +#include "lexer_parser.h" int token_append(t_token *token, char c) { diff --git a/42sh/src/lexer-parser/token_cmp_type.c b/42sh/src/lexer-parser/token_cmp_type.c new file mode 100644 index 00000000..e2739738 --- /dev/null +++ b/42sh/src/lexer-parser/token_cmp_type.c @@ -0,0 +1,6 @@ +#include "lexer_parser.h" + +int token_cmp_type(t_type data, t_type ref) +{ + return (data != ref); +} diff --git a/42sh/src/token/token_free.c b/42sh/src/lexer-parser/token_free.c similarity index 83% rename from 42sh/src/token/token_free.c rename to 42sh/src/lexer-parser/token_free.c index 52ddc369..8af65706 100644 --- a/42sh/src/token/token_free.c +++ b/42sh/src/lexer-parser/token_free.c @@ -1,4 +1,4 @@ -#include "token.h" +#include "lexer_parser.h" void token_free(void *data, size_t size) { diff --git a/42sh/src/token/token_getnext.c b/42sh/src/lexer-parser/token_getnext.c similarity index 98% rename from 42sh/src/token/token_getnext.c rename to 42sh/src/lexer-parser/token_getnext.c index 5bd5cdcf..1ca30452 100644 --- a/42sh/src/token/token_getnext.c +++ b/42sh/src/lexer-parser/token_getnext.c @@ -1,4 +1,4 @@ -#include "token.h" +#include "lexer_parser.h" static int is_separator(char c) { diff --git a/42sh/src/token/token_init.c b/42sh/src/lexer-parser/token_init.c similarity index 97% rename from 42sh/src/token/token_init.c rename to 42sh/src/lexer-parser/token_init.c index 100bf26e..2988b9f3 100644 --- a/42sh/src/token/token_init.c +++ b/42sh/src/lexer-parser/token_init.c @@ -10,7 +10,7 @@ /* */ /* ************************************************************************** */ -#include "token.h" +#include "lexer_parser.h" t_token *token_init() { diff --git a/42sh/src/token/token_print.c b/42sh/src/lexer-parser/token_print.c similarity index 87% rename from 42sh/src/token/token_print.c rename to 42sh/src/lexer-parser/token_print.c index 15a67bd8..5648a13a 100644 --- a/42sh/src/token/token_print.c +++ b/42sh/src/lexer-parser/token_print.c @@ -1,4 +1,4 @@ -#include "token.h" +#include "lexer_parser.h" void token_print(t_list *lst) { diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index b517b9bb..0fa646bf 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -18,6 +18,7 @@ int main(void) { t_data data; t_list *token; + t_btree *ast; data.env = ft_sstrdup(environ); if (signal(SIGINT, sig_handler) == SIG_ERR) @@ -26,12 +27,15 @@ int main(void) { if (ft_interactive_sh(&data)) return (1); - /* ft_printf("got command:'%s'\n", data.history->prev->content); */ + ft_printf("got command:'%s'\n", data.history->prev->content); if (ft_tokenize(&token, data.history->prev->content)) return (1); - /* token_print(token); */ - /* t_btree *ast = ft_parse(&ast, token); */ + token_print(token); + if (ft_parse(&ast, token)) + return (1); ft_lstdel(&token, &token_free); + if (ft_exec(ast)) + return (1); /* char **av = ft_cmd_getav(data.history->prev->content); */ /* if (av && av[0]) */