libft btree

This commit is contained in:
Jack Halford 2016-11-13 23:59:34 +01:00
parent 79cc1f4e2a
commit 92f2557e7e
19 changed files with 172 additions and 17 deletions

View file

@ -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 "$<"..."

21
42sh/includes/exec.h Normal file
View file

@ -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

View file

@ -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

View file

@ -15,7 +15,8 @@
# include "libft.h"
# include "line_editing.h"
# include "token.h"
# include "lexer_parser.h"
# include "exec.h"
# include <dirent.h>
# include <sys/stat.h>
# include <sys/types.h>

@ -1 +1 @@
Subproject commit d42b309e42077dd6f0a26cd67171d09cc50de4e1
Subproject commit 7da0a79521c667ebc5b1cad30f2dd73aa55f2ddb

15
42sh/src/exec/exec_pipe.c Normal file
View file

@ -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);
}

View file

@ -0,0 +1,8 @@
#include "exec.h"
int exec_semi(t_btree *ast)
{
ft_exec(ast->left);
ft_exec(ast->right);
return (0);
}

29
42sh/src/exec/ft_exec.c Normal file
View file

@ -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);
}

View file

@ -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);
}

View file

@ -10,7 +10,7 @@
/* */
/* ************************************************************************** */
#include "token.h"
#include "lexer_parser.h"
int ft_tokenize(t_list **alst, char *str)
{

View file

@ -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);
}

View file

@ -10,7 +10,7 @@
/* */
/* ************************************************************************** */
#include "token.h"
#include "lexer_parser.h"
int token_append(t_token *token, char c)
{

View file

@ -0,0 +1,6 @@
#include "lexer_parser.h"
int token_cmp_type(t_type data, t_type ref)
{
return (data != ref);
}

View file

@ -1,4 +1,4 @@
#include "token.h"
#include "lexer_parser.h"
void token_free(void *data, size_t size)
{

View file

@ -1,4 +1,4 @@
#include "token.h"
#include "lexer_parser.h"
static int is_separator(char c)
{

View file

@ -10,7 +10,7 @@
/* */
/* ************************************************************************** */
#include "token.h"
#include "lexer_parser.h"
t_token *token_init()
{

View file

@ -1,4 +1,4 @@
#include "token.h"
#include "lexer_parser.h"
void token_print(t_list *lst)
{

View file

@ -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]) */