From 3c46e10736bb19fead181f06d25b307706de8c99 Mon Sep 17 00:00:00 2001 From: wescande Date: Mon, 30 Jan 2017 17:19:09 +0100 Subject: [PATCH 1/2] integration du retour de glob dans les tokens --- 42sh/Makefile | 61 ++++++++++--------- 42sh/includes/glob.h | 30 ++++++--- 42sh/includes/lexer.h | 3 +- .../{glob_print.c => glob_expand_token.c} | 18 +++--- 42sh/src/lexer/token_free.c | 3 +- 42sh/src/lexer/token_init.c | 3 +- 42sh/src/main/main.c | 4 +- 7 files changed, 71 insertions(+), 51 deletions(-) rename 42sh/src/glob/{glob_print.c => glob_expand_token.c} (67%) diff --git a/42sh/Makefile b/42sh/Makefile index fe295d2b..b769970d 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -30,10 +30,14 @@ INC_DIR = includes/ OBJ_DIR = objs/ SRC_BASE = \ +builtin/bt_read_get.c\ +builtin/bt_read_parse.c\ +builtin/bt_read_term.c\ builtin/builtin_cd.c\ builtin/builtin_echo.c\ builtin/builtin_env.c\ builtin/builtin_exit.c\ +builtin/builtin_read.c\ builtin/builtin_setenv.c\ builtin/builtin_unsetenv.c\ builtin/is_builtin.c\ @@ -58,7 +62,7 @@ exec/set_exitstatus.c\ glob/dir_glob.c\ glob/expand_brace.c\ glob/glob.c\ -glob/glob_print.c\ +glob/glob_expand_token.c\ glob/is_char_esc.c\ glob/lib_perso/ft_ld_back.c\ glob/lib_perso/ft_ld_clear.c\ @@ -92,6 +96,7 @@ job-control/job_kill_all.c\ job-control/job_notify_change.c\ job-control/job_notify_new.c\ job-control/job_remove.c\ +job-control/job_run.c\ job-control/job_update_id.c\ job-control/job_update_status.c\ job-control/job_wait.c\ @@ -133,35 +138,31 @@ lexer/token_expand_var.c\ lexer/token_free.c\ lexer/token_init.c\ lexer/token_print.c\ -line-editing/curs_ask.c\ -line-editing/curs_clear.c\ -line-editing/curs_goto.c\ -line-editing/curs_move.c\ -line-editing/curs_setup.c\ -line-editing/curs_single.c\ -line-editing/curs_term_setup.c\ -line-editing/curs_write.c\ -line-editing/ft_readline.c\ -line-editing/rl_bitset.c\ -line-editing/rl_clear_function.c\ -line-editing/rl_clipboard_function.c\ -line-editing/rl_clipboard_utility.c\ -line-editing/rl_comp_function.c\ -line-editing/rl_default_function.c\ -line-editing/rl_display.c\ -line-editing/rl_dynamic_buffer.c\ -line-editing/rl_long_move_function.c\ -line-editing/rl_merge_line.c\ -line-editing/rl_page_move_function.c\ -line-editing/rl_prompt.c\ -line-editing/rl_quoting_function.c\ -line-editing/rl_remove_function.c\ -line-editing/rl_setup.c\ -line-editing/rl_short_move_function.c\ -line-editing/rl_stack_line.c\ -line-editing/rl_state_function.c\ -line-editing/rl_word_move_function.c\ -line-editing/rl_word_utility.c\ +line-editing/builtin_history.c\ +line-editing/check_backslash.c\ +line-editing/copy_cut_paste.c\ +line-editing/ft_split_whitespaces.c\ +line-editing/get_touch.c\ +line-editing/get_touch_toolz.c\ +line-editing/get_touch_toolz_2.c\ +line-editing/heredoc.c\ +line-editing/history_parsing.c\ +line-editing/history_parsing_toolz.c\ +line-editing/history_parsing_toolz_2.c\ +line-editing/list_toolz.c\ +line-editing/move_to_line.c\ +line-editing/print_and_del.c\ +line-editing/prompt.c\ +line-editing/quotes_gest.c\ +line-editing/reader.c\ +line-editing/readline.c\ +line-editing/surch_in_history.c\ +line-editing/tool_line.c\ +line-editing/tool_line_2.c\ +line-editing/toolz.c\ +line-editing/toolz2.c\ +line-editing/toolz_parseur.c\ +line-editing/toolz_termcaps.c\ main/data_exit.c\ main/data_init.c\ main/data_singleton.c\ diff --git a/42sh/includes/glob.h b/42sh/includes/glob.h index 4ba55910..61ee2079 100644 --- a/42sh/includes/glob.h +++ b/42sh/includes/glob.h @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/04 16:31:18 by wescande #+# #+# */ -/* Updated: 2017/01/30 12:12:23 by wescande ### ########.fr */ +/* Updated: 2017/01/30 17:14:48 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -42,15 +42,16 @@ typedef struct s_expand char *s1; } t_expand; -char **glob(const char *str, const unsigned char *esc, char **env); -void expand_brace(t_glob *tglob); -void glob_print(t_list *token, t_data *data); -int match_pattern(t_glob *tglob, char *str, char *full_word); -void dir_research(t_glob *tglob, char *p, const char *pat); -void dir_research_recursive(t_glob *tglob, char *p, const char *pat); +/* +** interface of glob. +** It fill all token->expand_data with correspind expansion +*/ +void glob_expand_token(t_list *token, t_data *data); + /* ** return TRUE if path file is a directory. */ + int is_directory(const char *path); /* ** return TRUE if char at str_pos in ini_str is escape. @@ -58,6 +59,21 @@ int is_directory(const char *path); int is_char_esc(const unsigned char *esc, const char *ini_str, const char *str_pos); +/* +** Internal function. +** +** +** +** +** +** +** +*/ +char **glob(const char *str, const unsigned char *esc, char **env); +void expand_brace(t_glob *tglob); +int match_pattern(t_glob *tglob, char *str, char *full_word); +void dir_research(t_glob *tglob, char *p, const char *pat); +void dir_research_recursive(t_glob *tglob, char *p, const char *pat); /* ** LIST D: */ diff --git a/42sh/includes/lexer.h b/42sh/includes/lexer.h index 1df99c39..7e06d6e8 100644 --- a/42sh/includes/lexer.h +++ b/42sh/includes/lexer.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */ -/* Updated: 2017/01/30 13:07:23 by wescande ### ########.fr */ +/* Updated: 2017/01/30 17:05:20 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -63,6 +63,7 @@ struct s_token { t_type type; char *data; + char **expand_data; unsigned char *esc; int size; }; diff --git a/42sh/src/glob/glob_print.c b/42sh/src/glob/glob_expand_token.c similarity index 67% rename from 42sh/src/glob/glob_print.c rename to 42sh/src/glob/glob_expand_token.c index 46fcdd8d..d7eaf7c0 100644 --- a/42sh/src/glob/glob_print.c +++ b/42sh/src/glob/glob_expand_token.c @@ -1,18 +1,18 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* glob_print.c :+: :+: :+: */ +/* glob_expand_token.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2017/01/05 16:09:40 by wescande #+# #+# */ -/* Updated: 2017/01/27 21:57:24 by wescande ### ########.fr */ +/* Created: 2017/01/30 17:10:34 by wescande #+# #+# */ +/* Updated: 2017/01/30 17:12:02 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ #include "glob.h" -void ft_tabprint_fd(char **mytab, int fd) +static void ft_tabprint_fd(char **mytab, int fd) { int i; @@ -26,12 +26,11 @@ void ft_tabprint_fd(char **mytab, int fd) } } -void glob_print(t_list *lst, t_data *data) +void glob_expand_token(t_list *lst, t_data *data) { t_token *token; int i; t_type type; - char **glob_ret; while (lst) { @@ -40,9 +39,10 @@ void glob_print(t_list *lst, t_data *data) type = token->type; while (type >> (i++ + 2)) ; - glob_ret = glob(token->data, token->esc, data->env); - DG("%02i '%s'", i, token->data); - ft_tabprint_fd(glob_ret, 3); + token->expand_data = glob(token->data, token->esc, data->env); + DG("EXPANSION DU TOKEN : %02i '%s'", i, token->data); + ft_tabprint_fd(token->expand_data, 4); + DG("FIN DE L'EXPANSION DU TOKEN"); lst = lst->next; } } diff --git a/42sh/src/lexer/token_free.c b/42sh/src/lexer/token_free.c index 466d4407..064f34bb 100644 --- a/42sh/src/lexer/token_free.c +++ b/42sh/src/lexer/token_free.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:07:30 by jhalford #+# #+# */ -/* Updated: 2017/01/27 21:54:05 by wescande ### ########.fr */ +/* Updated: 2017/01/30 17:06:23 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,6 +21,7 @@ void token_free(void *data, size_t size) if (!(token->type & TK_NON_FREEABLE)) { ft_strdel(&token->data); + ft_tabdel(&token->expand_data); ft_memdel((void **)&token->esc); } free(token); diff --git a/42sh/src/lexer/token_init.c b/42sh/src/lexer/token_init.c index 009f27dd..81c94ca8 100644 --- a/42sh/src/lexer/token_init.c +++ b/42sh/src/lexer/token_init.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 15:30:25 by jhalford #+# #+# */ -/* Updated: 2017/01/27 21:54:53 by wescande ### ########.fr */ +/* Updated: 2017/01/30 17:05:55 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,5 +21,6 @@ t_token *token_init(void) token->size = 8; token->data = ft_strnew(token->size + 1); token->esc = (unsigned char *)ft_strnew(token->size / 8 + 1); + token->expand_data = NULL; return (token); } diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 55c960f3..9de6cd66 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */ -/* Updated: 2017/01/30 13:09:12 by wescande ### ########.fr */ +/* Updated: 2017/01/30 17:15:28 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,7 +30,7 @@ int shell_single_command(char *command) return (1); DG("after post_tokenize"); token_print(token); - glob_print(token, data_singleton()); + glob_expand_token(token, data_singleton()); if (ft_parse(&ast, &token)) return (1); btree_print(STDBUG, ast, &ft_putast); From ad7d814c74703ca288f04bff9798c8c74772315d Mon Sep 17 00:00:00 2001 From: wescande Date: Tue, 31 Jan 2017 19:36:29 +0100 Subject: [PATCH 2/2] extension du globbing implemente SUPER MAKEFILE QUI DECHIRE TA M*RE --- 42sh/Makefile | 14 +++++--- 42sh/includes/glob.h | 17 +++------- 42sh/includes/lexer.h | 3 +- 42sh/includes/parser.h | 4 ++- 42sh/src/exec/ast_free.c | 8 +++-- 42sh/src/exec/exec_command.c | 47 +++++++++++++++++++++++++-- 42sh/src/glob/dir_glob.c | 30 ++++++++--------- 42sh/src/glob/expand_brace.c | 4 ++- 42sh/src/glob/glob.c | 52 ++++-------------------------- 42sh/src/glob/glob_expand_token.c | 48 --------------------------- 42sh/src/lexer/command_getoutput.c | 2 +- 42sh/src/lexer/token_free.c | 3 +- 42sh/src/lexer/token_init.c | 3 +- 42sh/src/main/main.c | 4 +-- 42sh/src/parser/ft_parse.c | 4 +-- 42sh/src/parser/parse_word.c | 8 +++-- 16 files changed, 105 insertions(+), 146 deletions(-) delete mode 100644 42sh/src/glob/glob_expand_token.c diff --git a/42sh/Makefile b/42sh/Makefile index b769970d..911c8683 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -13,11 +13,11 @@ NAME = 42sh CC = gcc -FLAGS = -Wall -Wextra -Werror +FLAGS = -Wall -Wextra -Werror -fsanitize=address -g D_FLAGS = -g LEN_NAME = `printf "%s" $(NAME) |wc -c` -DELTA = $$(echo "$$(tput cols)-24-$(LEN_NAME)"|bc) +DELTA = $$(echo "$$(tput cols)-37-$(LEN_NAME)"|bc) LIBFT_DIR = libft/ LIBFT_LIB = $(LIBFT_DIR)libft.a @@ -62,7 +62,6 @@ exec/set_exitstatus.c\ glob/dir_glob.c\ glob/expand_brace.c\ glob/glob.c\ -glob/glob_expand_token.c\ glob/is_char_esc.c\ glob/lib_perso/ft_ld_back.c\ glob/lib_perso/ft_ld_clear.c\ @@ -189,6 +188,8 @@ parser/parse_word.c SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE)) OBJS = $(addprefix $(OBJ_DIR), $(SRC_BASE:.c=.o)) +NB = $(words $(SRC_BASE)) +INDEX = 0 all : @make -j $(NAME) @@ -210,10 +211,15 @@ $(OBJ_DIR) : @mkdir -p $(dir $(OBJS)) $(OBJ_DIR)%.o : $(SRC_DIR)%.c | $(OBJ_DIR) + @$(eval DONE=$(shell echo $$(($(INDEX)*20/$(NB))))) + @$(eval PERCENT=$(shell echo $$(($(INDEX)*100/$(NB))))) + @$(eval COLOR=$(shell echo $$(($(PERCENT)%35+196)))) + @$(eval TO_DO=$(shell echo $$((20-$(INDEX)*20/$(NB))))) + @printf "\r\033[38;5;11m⌛ MAKE %s : %2d%% \033[48;5;%dm%*s\033[0m%*s\033[48;5;255m \033[0m \033[38;5;11m %*s\033[0m\033[K" $(NAME) $(PERCENT) $(COLOR) $(DONE) "" $(TO_DO) "" $(DELTA) "$@" @$(CC) $(FLAGS) -MMD -c $< -o $@\ -I $(INC_DIR)\ -I $(LIBFT_INC) - @printf "\r\033[38;5;11m⌛ MAKE %s plz wait : %*s\033[0m\033[K" $(NAME) $(DELTA) "$@" + @$(eval INDEX=$(shell echo $$(($(INDEX)+1)))) clean: cleanlib @rm -rf $(OBJ_DIR) diff --git a/42sh/includes/glob.h b/42sh/includes/glob.h index 61ee2079..fb5efd35 100644 --- a/42sh/includes/glob.h +++ b/42sh/includes/glob.h @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/04 16:31:18 by wescande #+# #+# */ -/* Updated: 2017/01/30 17:14:48 by wescande ### ########.fr */ +/* Updated: 2017/01/31 17:54:19 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -44,9 +44,8 @@ typedef struct s_expand /* ** interface of glob. -** It fill all token->expand_data with correspind expansion */ -void glob_expand_token(t_list *token, t_data *data); +char **glob(const char *str, const unsigned char *esc); /* ** return TRUE if path file is a directory. @@ -61,19 +60,11 @@ int is_char_esc(const unsigned char *esc, /* ** Internal function. -** -** -** -** -** -** -** */ -char **glob(const char *str, const unsigned char *esc, char **env); void expand_brace(t_glob *tglob); int match_pattern(t_glob *tglob, char *str, char *full_word); -void dir_research(t_glob *tglob, char *p, const char *pat); -void dir_research_recursive(t_glob *tglob, char *p, const char *pat); +int dir_research(t_glob *tglob, char *p, const char *pat); +int dir_research_recursive(t_glob *tglob, char *p, const char *pat); /* ** LIST D: */ diff --git a/42sh/includes/lexer.h b/42sh/includes/lexer.h index 7e06d6e8..24b36150 100644 --- a/42sh/includes/lexer.h +++ b/42sh/includes/lexer.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */ -/* Updated: 2017/01/30 17:05:20 by wescande ### ########.fr */ +/* Updated: 2017/01/31 18:35:34 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -63,7 +63,6 @@ struct s_token { t_type type; char *data; - char **expand_data; unsigned char *esc; int size; }; diff --git a/42sh/includes/parser.h b/42sh/includes/parser.h index 3c8a7fed..cdb28caa 100644 --- a/42sh/includes/parser.h +++ b/42sh/includes/parser.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/01 12:15:54 by jhalford #+# #+# */ -/* Updated: 2017/01/11 17:06:17 by jhalford ### ########.fr */ +/* Updated: 2017/01/31 19:17:25 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,7 @@ # include "minishell.h" typedef struct s_parser t_parser; +typedef struct s_ld t_ld; typedef struct s_astnode t_astnode; typedef struct s_redir t_redir; typedef union u_astdata t_astdata; @@ -44,6 +45,7 @@ struct s_redir union u_astdata { t_redir redir; + t_ld *token; char **sstr; char *str; }; diff --git a/42sh/src/exec/ast_free.c b/42sh/src/exec/ast_free.c index b0b3f592..fdf827f7 100644 --- a/42sh/src/exec/ast_free.c +++ b/42sh/src/exec/ast_free.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/05 11:50:51 by jhalford #+# #+# */ -/* Updated: 2017/01/11 17:50:03 by jhalford ### ########.fr */ +/* Updated: 2017/01/31 19:15:28 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,9 +19,11 @@ void ast_free(void *data, size_t content_size) (void)content_size; node = data; if (node->type == TK_COMMAND) + ft_ld_clear(&node->data.token, &ft_tabdel); + else if (node->type == TK_SUBSHELL) { - if (node->data.sstr) - ft_sstrfree(node->data.sstr); + ft_sstrfree(node->data.sstr); + node->data.sstr = NULL; } else if (node->type == TK_LESS || node->type == TK_GREAT || node->type == TK_DGREAT) { diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index 6b2c2562..da867828 100644 --- a/42sh/src/exec/exec_command.c +++ b/42sh/src/exec/exec_command.c @@ -6,13 +6,53 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 17:28:14 by jhalford #+# #+# */ -/* Updated: 2017/01/12 13:14:35 by jhalford ### ########.fr */ +/* Updated: 2017/01/31 19:12:23 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ #include "exec.h" -int exec_command(t_btree **ast) +static char **return_array_expand(t_ld *ld) +{ + char **my_tab; + int index; + char **expand; + char **content; + + my_tab = NULL; + while (ld) + { + content = ld->content; + DG("EXPANSION DE :"); + DG("%s", content[0]); + expand = glob(content[0], (unsigned char *)content[1]); + DG(); + index = -1; + while (expand[++index]) + my_tab = ft_sstradd(my_tab, expand[index]); + ft_tabdel(&expand); + ld = ld->next; + } + return (my_tab); +} + +static void ft_tabprint_fd(char **mytab, int fd) +{ + int i; + + DG("Affichage des parametres envoyes apres le glob"); + if (!mytab || !*mytab) + return ; + i = 0; + while (mytab[i]) + { + ft_putendl_fd(mytab[i], fd); + ++i; + } +} + + +int exec_command(t_btree **ast) { t_astnode *node; t_process *p; @@ -21,7 +61,8 @@ int exec_command(t_btree **ast) node = (*ast)->item; p = &data_singleton()->exec.process; job = &data_singleton()->exec.job; - p->av = ft_sstrdup(node->data.sstr); + p->av = return_array_expand(node->data.token); + ft_tabprint_fd(p->av, 4); process_setexec(node->type, p); if (!(launch_process(p))) { diff --git a/42sh/src/glob/dir_glob.c b/42sh/src/glob/dir_glob.c index 815e64d6..a16dcc38 100644 --- a/42sh/src/glob/dir_glob.c +++ b/42sh/src/glob/dir_glob.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/30 12:07:16 by wescande #+# #+# */ -/* Updated: 2017/01/30 12:07:19 by wescande ### ########.fr */ +/* Updated: 2017/01/31 18:00:16 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,16 +20,16 @@ int is_directory(const char *path) return (S_ISDIR(path_stat.st_mode)); } -void dir_research(t_glob *gl, char *p, const char *pat) +int dir_research(t_glob *gl, char *p, const char *pat) { DIR *dir; struct dirent *in; char *path_tmp; + int ret; - if (ft_strlen(p) <= 1 || p[ft_strlen(p) - 1] != '.') - { - if (!(dir = opendir(p))) - return ; + ret = 0; + if ((ft_strlen(p) <= 1 || p[ft_strlen(p) - 1] != '.') + && is_directory(p) && (dir = opendir(p))) while ((in = readdir(dir))) { if (ft_strcmp(in->d_name, ".") && ft_strcmp(in->d_name, "..")) @@ -39,25 +39,25 @@ void dir_research(t_glob *gl, char *p, const char *pat) else path_tmp = ft_strjoinf(ft_strjoin(p, "/"), in->d_name, 1); gl->pat = pat; - if (match_pattern(gl, in->d_name, path_tmp)) + if (match_pattern(gl, in->d_name, path_tmp) && ++ret) ft_ld_pushfront(&gl->match, ft_strdup(path_tmp + 2 * (path_tmp[0] == '.' && path_tmp[1] == '/'))); ft_strdel(&path_tmp); } } - } + return (ret); } -void dir_research_recursive(t_glob *gl, char *p, const char *pat) +int dir_research_recursive(t_glob *gl, char *p, const char *pat) { DIR *dir; struct dirent *in; char *path_tmp; + int ret; - if ((ft_strlen(p) <= 1 || p[ft_strlen(p) - 1] != '.') && is_directory(p)) - { - if (!(dir = opendir(p))) - return ; + ret = 0; + if ((ft_strlen(p) <= 1 || p[ft_strlen(p) - 1] != '.') + && is_directory(p) && (dir = opendir(p))) while ((in = readdir(dir))) { if (ft_strcmp(in->d_name, ".") && ft_strcmp(in->d_name, "..")) @@ -68,11 +68,11 @@ void dir_research_recursive(t_glob *gl, char *p, const char *pat) path_tmp = ft_strjoinf(ft_strjoin(p, "/"), in->d_name, 1); dir_research_recursive(gl, path_tmp, pat); gl->pat = pat; - if (match_pattern(gl, in->d_name, path_tmp)) + if (match_pattern(gl, in->d_name, path_tmp) && ++ret) ft_ld_pushfront(&gl->match, ft_strdup(path_tmp + 2 * (path_tmp[0] == '.' && path_tmp[1] == '/'))); ft_strdel(&path_tmp); } } - } + return (ret); } diff --git a/42sh/src/glob/expand_brace.c b/42sh/src/glob/expand_brace.c index 78211f16..c570b6aa 100644 --- a/42sh/src/glob/expand_brace.c +++ b/42sh/src/glob/expand_brace.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/12 19:00:29 by wescande #+# #+# */ -/* Updated: 2017/01/28 01:13:26 by wescande ### ########.fr */ +/* Updated: 2017/01/31 18:19:34 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,6 +38,7 @@ static char **gen_tab(const char *pat, my_tab[1] = (char *)esc; } my_tab[2] = NULL; + return (my_tab); } @@ -126,6 +127,7 @@ void expand_brace(t_glob *gl) int do_it; t_expand me; + ft_ld_pushfront(&gl->m_pat, gen_tab("", (const unsigned char *)"", 1)); ft_ld_pushfront(&gl->m_pat, gen_tab(gl->pat, gl->esc, 1)); me = (t_expand){NULL, NULL, NULL, NULL, NULL}; diff --git a/42sh/src/glob/glob.c b/42sh/src/glob/glob.c index e086c68e..bb1b2357 100644 --- a/42sh/src/glob/glob.c +++ b/42sh/src/glob/glob.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/04 16:29:54 by wescande #+# #+# */ -/* Updated: 2017/01/30 12:08:49 by wescande ### ########.fr */ +/* Updated: 2017/01/31 18:19:56 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,15 +22,6 @@ ** to just expanse in local directory and not in path dir */ -static void path_research(t_glob *gl, char **path) -{ - int i; - - i = -1; - while (path[++i]) - dir_research(gl, path[i], gl->pat); -} - static char **treat_glob(t_ld **match) { char **gl; @@ -43,33 +34,10 @@ static char **treat_glob(t_ld **match) return (gl); } -static void add_simple_pat(t_glob *gl) -{ - char *str; - int start; - - str = (char *)gl->pat; - start = 0; - while (*str) - { - if (!is_char_esc(gl->esc, gl->pat, str)) - { - if (*str == '[') - start = 1; - else if (*str == ']' && start == 1) - return ; - else if (*str == '*' || *str == '?') - return ; - } - ++str; - } - ft_ld_pushfront(&gl->match, ft_strdup(gl->pat)); -} - -char **glob(const char *pat, const unsigned char *esc, char **env) +char **glob(const char *pat, const unsigned char *esc) { t_glob gl; - char **path; + int ret; gl = (t_glob){pat, esc, NULL, NULL}; expand_brace(&gl); @@ -77,18 +45,12 @@ char **glob(const char *pat, const unsigned char *esc, char **env) { gl.pat = ((char **)gl.m_pat->content)[0]; gl.esc = ((unsigned char **)gl.m_pat->content)[1]; - add_simple_pat(&gl); - if (!(gl.pat[0] == '/' || (gl.pat[0] == '.' && gl.pat[1] == '/')) - && env && (path = ft_strsplit(ft_getenv(env, "PATH"), ':'))) - { - path_research(&gl, path); - ft_tabdel(&path); - } - gl.pat = ((char **)gl.m_pat->content)[0]; if (gl.pat[0] != '/') - dir_research(&gl, ".", gl.pat); + ret = dir_research(&gl, ".", gl.pat); else - dir_research(&gl, "/", gl.pat + 1); + ret = dir_research(&gl, "/", gl.pat + 1); + if (!ret) + ft_ld_pushfront(&gl.match, ft_strdup(gl.pat)); gl.m_pat = gl.m_pat->next; } ft_ld_clear(&gl.m_pat, &ft_tabdel); diff --git a/42sh/src/glob/glob_expand_token.c b/42sh/src/glob/glob_expand_token.c deleted file mode 100644 index d7eaf7c0..00000000 --- a/42sh/src/glob/glob_expand_token.c +++ /dev/null @@ -1,48 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* glob_expand_token.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: wescande +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2017/01/30 17:10:34 by wescande #+# #+# */ -/* Updated: 2017/01/30 17:12:02 by wescande ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "glob.h" - -static void ft_tabprint_fd(char **mytab, int fd) -{ - int i; - - if (!mytab || !*mytab) - return ; - i = 0; - while (mytab[i]) - { - ft_putendl_fd(mytab[i], fd); - ++i; - } -} - -void glob_expand_token(t_list *lst, t_data *data) -{ - t_token *token; - int i; - t_type type; - - while (lst) - { - i = 1; - token = lst->content; - type = token->type; - while (type >> (i++ + 2)) - ; - token->expand_data = glob(token->data, token->esc, data->env); - DG("EXPANSION DU TOKEN : %02i '%s'", i, token->data); - ft_tabprint_fd(token->expand_data, 4); - DG("FIN DE L'EXPANSION DU TOKEN"); - lst = lst->next; - } -} diff --git a/42sh/src/lexer/command_getoutput.c b/42sh/src/lexer/command_getoutput.c index 44b6c2e4..7e4b80f5 100644 --- a/42sh/src/lexer/command_getoutput.c +++ b/42sh/src/lexer/command_getoutput.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/12 14:01:59 by jhalford #+# #+# */ -/* Updated: 2017/01/12 15:31:34 by jhalford ### ########.fr */ +/* Updated: 2017/01/31 16:07:17 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/token_free.c b/42sh/src/lexer/token_free.c index 064f34bb..eda8f28b 100644 --- a/42sh/src/lexer/token_free.c +++ b/42sh/src/lexer/token_free.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:07:30 by jhalford #+# #+# */ -/* Updated: 2017/01/30 17:06:23 by wescande ### ########.fr */ +/* Updated: 2017/01/31 18:36:40 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,7 +21,6 @@ void token_free(void *data, size_t size) if (!(token->type & TK_NON_FREEABLE)) { ft_strdel(&token->data); - ft_tabdel(&token->expand_data); ft_memdel((void **)&token->esc); } free(token); diff --git a/42sh/src/lexer/token_init.c b/42sh/src/lexer/token_init.c index 81c94ca8..6547b15b 100644 --- a/42sh/src/lexer/token_init.c +++ b/42sh/src/lexer/token_init.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 15:30:25 by jhalford #+# #+# */ -/* Updated: 2017/01/30 17:05:55 by wescande ### ########.fr */ +/* Updated: 2017/01/31 18:36:31 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,6 +21,5 @@ t_token *token_init(void) token->size = 8; token->data = ft_strnew(token->size + 1); token->esc = (unsigned char *)ft_strnew(token->size / 8 + 1); - token->expand_data = NULL; return (token); } diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 9de6cd66..169a7e06 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */ -/* Updated: 2017/01/30 17:15:28 by wescande ### ########.fr */ +/* Updated: 2017/01/31 18:00:57 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,7 +30,7 @@ int shell_single_command(char *command) return (1); DG("after post_tokenize"); token_print(token); - glob_expand_token(token, data_singleton()); +// glob_expand_token(token, data_singleton()); if (ft_parse(&ast, &token)) return (1); btree_print(STDBUG, ast, &ft_putast); diff --git a/42sh/src/parser/ft_parse.c b/42sh/src/parser/ft_parse.c index 2dbdbb5d..d1a334c6 100644 --- a/42sh/src/parser/ft_parse.c +++ b/42sh/src/parser/ft_parse.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/30 17:14:58 by jhalford #+# #+# */ -/* Updated: 2017/01/12 15:07:31 by jhalford ### ########.fr */ +/* Updated: 2017/01/31 16:10:41 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,7 +41,7 @@ int ft_parse(t_btree **ast, t_list **start) if (!*ast) { *ast = btree_create_node(&item, sizeof(item)); - ((t_astnode *)(*ast)->item)->data.sstr = NULL; + ((t_astnode *)(*ast)->item)->data.token = NULL; } while (g_parser[i].type) { diff --git a/42sh/src/parser/parse_word.c b/42sh/src/parser/parse_word.c index 45c64039..b7b1743f 100644 --- a/42sh/src/parser/parse_word.c +++ b/42sh/src/parser/parse_word.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */ -/* Updated: 2017/01/11 17:24:55 by jhalford ### ########.fr */ +/* Updated: 2017/01/31 19:17:58 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,12 +16,16 @@ int parse_word(t_btree **ast, t_list **start, t_list **lst) { t_astnode *node; t_token *token; + char **my_tab; (void)start; + my_tab = NULL; token = (*lst)->content; node = (*ast)->item; node->type = TK_COMMAND; - node->data.sstr = ft_sstradd(node->data.sstr, token->data); + my_tab = ft_sstradd(my_tab, token->data); + my_tab = ft_sstradd(my_tab, (char *)token->esc); + ft_ld_pushback(&node->data.token, my_tab); ft_parse(ast, &(*lst)->next); ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free); return (0);