libft btree
This commit is contained in:
parent
79cc1f4e2a
commit
92f2557e7e
19 changed files with 172 additions and 17 deletions
|
|
@ -11,7 +11,8 @@ D_OBJ = obj
|
||||||
F_OBJ = $(notdir $(F_SRC:.c=.o))
|
F_OBJ = $(notdir $(F_SRC:.c=.o))
|
||||||
DF_OBJ := $(addprefix $(D_OBJ)/, $(F_OBJ))
|
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))
|
O_INC = $(addprefix -I, $(D_INC))
|
||||||
|
|
||||||
D_SER = libft/
|
D_SER = libft/
|
||||||
|
|
@ -30,27 +31,32 @@ RM = /bin/rm -rf
|
||||||
|
|
||||||
all: $(NAME)
|
all: $(NAME)
|
||||||
|
|
||||||
$(D_OBJ)/%.o: $(D_SRC)/main/%.c includes/
|
$(D_OBJ)/%.o: $(D_SRC)/main/%.c includes/minishell.h
|
||||||
@$(MKDIR) $(D_OBJ)
|
@$(MKDIR) $(D_OBJ)
|
||||||
@$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS)
|
@$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS)
|
||||||
@echo "Compiling "$<"..."
|
@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)
|
@$(MKDIR) $(D_OBJ)
|
||||||
@$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS)
|
@$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS)
|
||||||
@echo "Compiling "$<"..."
|
@echo "Compiling "$<"..."
|
||||||
|
|
||||||
$(D_OBJ)/%.o: $(D_SRC)/builtin/%.c includes/
|
$(D_OBJ)/%.o: $(D_SRC)/minishell-exec/%.c includes/minishell.h
|
||||||
@$(MKDIR) $(D_OBJ)
|
@$(MKDIR) $(D_OBJ)
|
||||||
@$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS)
|
@$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS)
|
||||||
@echo "Compiling "$<"..."
|
@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)
|
@$(MKDIR) $(D_OBJ)
|
||||||
@$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS)
|
@$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS)
|
||||||
@echo "Compiling "$<"..."
|
@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)
|
@$(MKDIR) $(D_OBJ)
|
||||||
@$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS)
|
@$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS)
|
||||||
@echo "Compiling "$<"..."
|
@echo "Compiling "$<"..."
|
||||||
|
|
|
||||||
21
42sh/includes/exec.h
Normal file
21
42sh/includes/exec.h
Normal 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
|
||||||
|
|
@ -26,7 +26,18 @@
|
||||||
# define TK_WORD 0x0100
|
# define TK_WORD 0x0100
|
||||||
|
|
||||||
typedef long long t_type;
|
typedef long long t_type;
|
||||||
|
typedef struct s_astnode t_astnode;
|
||||||
typedef struct s_token t_token;
|
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
|
struct s_token
|
||||||
{
|
{
|
||||||
|
|
@ -35,11 +46,22 @@ struct s_token
|
||||||
int size;
|
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_init();
|
||||||
t_token *token_getnext(int *pos,char *line);
|
t_token *token_getnext(int *pos,char *line);
|
||||||
int ft_tokenize(t_list **alst, char *str);
|
int ft_tokenize(t_list **alst, char *str);
|
||||||
int token_append(t_token *token, char c);
|
int token_append(t_token *token, char c);
|
||||||
void token_free(void *data, size_t size);
|
void token_free(void *data, size_t size);
|
||||||
|
int token_cmp_type(t_type data, t_type ref);
|
||||||
void token_print(t_list *lst);
|
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
|
#endif
|
||||||
|
|
@ -15,7 +15,8 @@
|
||||||
|
|
||||||
# include "libft.h"
|
# include "libft.h"
|
||||||
# include "line_editing.h"
|
# include "line_editing.h"
|
||||||
# include "token.h"
|
# include "lexer_parser.h"
|
||||||
|
# include "exec.h"
|
||||||
# include <dirent.h>
|
# include <dirent.h>
|
||||||
# include <sys/stat.h>
|
# include <sys/stat.h>
|
||||||
# include <sys/types.h>
|
# include <sys/types.h>
|
||||||
|
|
|
||||||
|
|
@ -1 +1 @@
|
||||||
Subproject commit d42b309e42077dd6f0a26cd67171d09cc50de4e1
|
Subproject commit 7da0a79521c667ebc5b1cad30f2dd73aa55f2ddb
|
||||||
15
42sh/src/exec/exec_pipe.c
Normal file
15
42sh/src/exec/exec_pipe.c
Normal 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);
|
||||||
|
}
|
||||||
8
42sh/src/exec/exec_semi.c
Normal file
8
42sh/src/exec/exec_semi.c
Normal 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
29
42sh/src/exec/ft_exec.c
Normal 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);
|
||||||
|
}
|
||||||
34
42sh/src/lexer-parser/ft_parse.c
Normal file
34
42sh/src/lexer-parser/ft_parse.c
Normal 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);
|
||||||
|
}
|
||||||
|
|
@ -10,7 +10,7 @@
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "token.h"
|
#include "lexer_parser.h"
|
||||||
|
|
||||||
int ft_tokenize(t_list **alst, char *str)
|
int ft_tokenize(t_list **alst, char *str)
|
||||||
{
|
{
|
||||||
9
42sh/src/lexer-parser/parse_separator.c
Normal file
9
42sh/src/lexer-parser/parse_separator.c
Normal 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);
|
||||||
|
}
|
||||||
|
|
@ -10,7 +10,7 @@
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "token.h"
|
#include "lexer_parser.h"
|
||||||
|
|
||||||
int token_append(t_token *token, char c)
|
int token_append(t_token *token, char c)
|
||||||
{
|
{
|
||||||
6
42sh/src/lexer-parser/token_cmp_type.c
Normal file
6
42sh/src/lexer-parser/token_cmp_type.c
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
#include "lexer_parser.h"
|
||||||
|
|
||||||
|
int token_cmp_type(t_type data, t_type ref)
|
||||||
|
{
|
||||||
|
return (data != ref);
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
#include "token.h"
|
#include "lexer_parser.h"
|
||||||
|
|
||||||
void token_free(void *data, size_t size)
|
void token_free(void *data, size_t size)
|
||||||
{
|
{
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
#include "token.h"
|
#include "lexer_parser.h"
|
||||||
|
|
||||||
static int is_separator(char c)
|
static int is_separator(char c)
|
||||||
{
|
{
|
||||||
|
|
@ -10,7 +10,7 @@
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "token.h"
|
#include "lexer_parser.h"
|
||||||
|
|
||||||
t_token *token_init()
|
t_token *token_init()
|
||||||
{
|
{
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
#include "token.h"
|
#include "lexer_parser.h"
|
||||||
|
|
||||||
void token_print(t_list *lst)
|
void token_print(t_list *lst)
|
||||||
{
|
{
|
||||||
|
|
@ -18,6 +18,7 @@ int main(void)
|
||||||
{
|
{
|
||||||
t_data data;
|
t_data data;
|
||||||
t_list *token;
|
t_list *token;
|
||||||
|
t_btree *ast;
|
||||||
|
|
||||||
data.env = ft_sstrdup(environ);
|
data.env = ft_sstrdup(environ);
|
||||||
if (signal(SIGINT, sig_handler) == SIG_ERR)
|
if (signal(SIGINT, sig_handler) == SIG_ERR)
|
||||||
|
|
@ -26,12 +27,15 @@ int main(void)
|
||||||
{
|
{
|
||||||
if (ft_interactive_sh(&data))
|
if (ft_interactive_sh(&data))
|
||||||
return (1);
|
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))
|
if (ft_tokenize(&token, data.history->prev->content))
|
||||||
return (1);
|
return (1);
|
||||||
/* token_print(token); */
|
token_print(token);
|
||||||
/* t_btree *ast = ft_parse(&ast, token); */
|
if (ft_parse(&ast, token))
|
||||||
|
return (1);
|
||||||
ft_lstdel(&token, &token_free);
|
ft_lstdel(&token, &token_free);
|
||||||
|
if (ft_exec(ast))
|
||||||
|
return (1);
|
||||||
|
|
||||||
/* char **av = ft_cmd_getav(data.history->prev->content); */
|
/* char **av = ft_cmd_getav(data.history->prev->content); */
|
||||||
/* if (av && av[0]) */
|
/* if (av && av[0]) */
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue