diff --git a/42sh/Makefile b/42sh/Makefile index 8dcf5189..fe295d2b 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -1,90 +1,237 @@ -NAME = minishell -CC = gcc -TAGFILE = .tags +#* ************************************************************************** *# +#* *# +#* ::: :::::::: *# +#* Makefile :+: :+: :+: *# +#* +:+ +:+ +:+ *# +#* By: wescande +#+ +:+ +#+ *# +#* +#+#+#+#+#+ +#+ *# +#* Created: 2016/08/29 21:32:58 by wescande #+# #+# *# +#* Updated: 2016/09/27 20:14:55 by wescande ### ########.fr *# +#* *# +#* ************************************************************************** *# -D_SRC = src -F_SRC := $(shell find $(D_SRC) -type f -regex ".*\.c$$") +NAME = 42sh -D_OBJ = obj -F_OBJ = $(notdir $(F_SRC:.c=.o)) -DF_OBJ := $(addprefix $(D_OBJ)/, $(F_OBJ)) +CC = gcc +FLAGS = -Wall -Wextra -Werror +D_FLAGS = -g -D_INC = includes libft/includes -F_INC := $(shell find $(D_INC) -type f -regex ".*\.h$$") -O_INC = $(addprefix -I, $(D_INC)) +LEN_NAME = `printf "%s" $(NAME) |wc -c` +DELTA = $$(echo "$$(tput cols)-24-$(LEN_NAME)"|bc) -D_SER = libft/ -O_SER = $(addprefix -L, $(D_SER)) +LIBFT_DIR = libft/ +LIBFT_LIB = $(LIBFT_DIR)libft.a +LIBFT_INC = $(LIBFT_DIR)includes/ -D_LIB = ft ncurses -O_LIB = $(addprefix -l, $(D_LIB)) +LIBS = -ltermcap -W_FLAGS = -Wall -Wextra -Werror -D_FLAGS = -g +SRC_DIR = src/ +INC_DIR = includes/ +OBJ_DIR = objs/ -MKDIR = mkdir -p -RM = /bin/rm -rf +SRC_BASE = \ +builtin/builtin_cd.c\ +builtin/builtin_echo.c\ +builtin/builtin_env.c\ +builtin/builtin_exit.c\ +builtin/builtin_setenv.c\ +builtin/builtin_unsetenv.c\ +builtin/is_builtin.c\ +exec/ast_free.c\ +exec/exec_ampersand.c\ +exec/exec_and_if.c\ +exec/exec_command.c\ +exec/exec_dgreat.c\ +exec/exec_great.c\ +exec/exec_less.c\ +exec/exec_or_if.c\ +exec/exec_pipe.c\ +exec/exec_semi.c\ +exec/ft_exec.c\ +exec/ft_findexec.c\ +exec/launch_process.c\ +exec/process_redirect.c\ +exec/process_reset.c\ +exec/process_setexec.c\ +exec/process_setgroup.c\ +exec/set_exitstatus.c\ +glob/dir_glob.c\ +glob/expand_brace.c\ +glob/glob.c\ +glob/glob_print.c\ +glob/is_char_esc.c\ +glob/lib_perso/ft_ld_back.c\ +glob/lib_perso/ft_ld_clear.c\ +glob/lib_perso/ft_ld_del.c\ +glob/lib_perso/ft_ld_front.c\ +glob/lib_perso/ft_ld_new.c\ +glob/lib_perso/ft_ld_order.c\ +glob/lib_perso/ft_ld_pushback.c\ +glob/lib_perso/ft_ld_pushfront.c\ +glob/lib_perso/ft_ld_reverse.c\ +glob/lib_perso/ft_ld_size.c\ +glob/lib_perso/ft_ld_swap.c\ +glob/lib_perso/ft_ld_to_tab.c\ +glob/lib_perso/ft_strjoinf.c\ +glob/lib_perso/ft_tabdel.c\ +glob/match_pattern.c\ +job-control/builtin_bg.c\ +job-control/builtin_fg.c\ +job-control/builtin_jobs.c\ +job-control/do_job_notification.c\ +job-control/job_addprocess.c\ +job-control/job_cmp_id.c\ +job-control/job_format.c\ +job-control/job_format_head.c\ +job-control/job_free.c\ +job-control/job_getprocess.c\ +job-control/job_getrank.c\ +job-control/job_is_completed.c\ +job-control/job_is_stopped.c\ +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_update_id.c\ +job-control/job_update_status.c\ +job-control/job_wait.c\ +job-control/mark_job_as_running.c\ +job-control/process_cmp_pid.c\ +job-control/process_format.c\ +job-control/process_free.c\ +job-control/process_mark_status.c\ +job-control/put_job_in_background.c\ +job-control/put_job_in_foreground.c\ +job-control/sigchld_handler.c\ +job-control/sigint_handler.c\ +job-control/sigtstp_handler.c\ +job-control/sigttin_handler.c\ +job-control/sigttou_handler.c\ +lexer/command_getoutput.c\ +lexer/ft_post_tokenize.c\ +lexer/ft_tokenize.c\ +lexer/get_lexer_state.c\ +lexer/lexer_backslash.c\ +lexer/lexer_default.c\ +lexer/lexer_delim.c\ +lexer/lexer_dquote.c\ +lexer/lexer_great.c\ +lexer/lexer_greatand.c\ +lexer/lexer_less.c\ +lexer/lexer_lessand.c\ +lexer/lexer_number.c\ +lexer/lexer_quote.c\ +lexer/lexer_sep.c\ +lexer/lexer_special.c\ +lexer/lexer_var.c\ +lexer/lexer_word.c\ +lexer/reduce_bquotes.c\ +lexer/reduce_parens.c\ +lexer/token_append.c\ +lexer/token_cmp_type.c\ +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\ +main/data_exit.c\ +main/data_init.c\ +main/data_singleton.c\ +main/ft_cleanup.c\ +main/ft_putast.c\ +main/ft_putast2.c\ +main/lib_expansion.c\ +main/main.c\ +main/shell_exit.c\ +main/shell_get_avdata.c\ +main/shell_get_opts.c\ +main/shell_init.c\ +main/sig_handler.c\ +parser/ft_parse.c\ +parser/parse_dgreat.c\ +parser/parse_dless.c\ +parser/parse_great.c\ +parser/parse_greatand.c\ +parser/parse_less.c\ +parser/parse_lessand.c\ +parser/parse_separator.c\ +parser/parse_subshell.c\ +parser/parse_word.c -.PHONY: all clean fclean re +SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE)) +OBJS = $(addprefix $(OBJ_DIR), $(SRC_BASE:.c=.o)) -all: $(NAME) +all : + @make -j $(NAME) -$(NAME): libft/libft.a $(DF_OBJ) - $(CC) $(O_INC) $(O_SER) $(O_LIB) $(W_FLAGS) $(DF_OBJ) -o $@ $(D_FLAGS) +$(NAME): $(LIBFT_LIB) $(OBJ_DIR) $(OBJS) + @$(CC) $(FLAGS) $(D_FLAGS) \ + -I $(INC_DIR) \ + -I $(LIBFT_INC) \ + $(LIBS) \ + $(LIBFT_LIB) $(OBJS) \ + -o $(NAME) + @echo "\r\033[48;5;15;38;5;25m✅ MAKE $(NAME)\033[0m\033[K" -$(D_OBJ)/%.o: $(D_SRC)/main/%.c includes/minishell.h - @$(MKDIR) $(D_OBJ) - @$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS) - @echo "Compiling "$<"..." +$(LIBFT_LIB): + @make -j -C $(LIBFT_DIR) -$(D_OBJ)/%.o: $(D_SRC)/builtin/%.c includes/minishell.h - @$(MKDIR) $(D_OBJ) - @$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS) - @echo "Compiling "$<"..." +$(OBJ_DIR) : + @mkdir -p $(OBJ_DIR) + @mkdir -p $(dir $(OBJS)) -$(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 "$<"..." +$(OBJ_DIR)%.o : $(SRC_DIR)%.c | $(OBJ_DIR) + @$(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) "$@" -$(D_OBJ)/%.o: $(D_SRC)/line-editing/%.c includes/ft_readline.h - @$(MKDIR) $(D_OBJ) - @$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS) - @echo "Compiling "$<"..." +clean: cleanlib + @rm -rf $(OBJ_DIR) + @echo "\r\033[38;5;202m✖ clean $(NAME).\033[0m\033[K" -$(D_OBJ)/%.o: $(D_SRC)/lexer/%.c includes/lexer.h - @$(MKDIR) $(D_OBJ) - @$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS) - @echo "Compiling "$<"..." +cleanlib: + @make -C $(LIBFT_DIR) clean -$(D_OBJ)/%.o: $(D_SRC)/parser/%.c includes/parser.h - @$(MKDIR) $(D_OBJ) - @$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS) - @echo "Compiling "$<"..." +fclean: clean fcleanlib + @rm -f $(NAME) + @echo "\r\033[38;5;196m❌ fclean $(NAME).\033[0m\033[K" -$(D_OBJ)/%.o: $(D_SRC)/exec/%.c includes/exec.h - @$(MKDIR) $(D_OBJ) - @$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS) - @echo "Compiling "$<"..." +fcleanlib: cleanlib + @make -C $(LIBFT_DIR) fclean -$(D_OBJ)/%.o: $(D_SRC)/job-control/%.c includes/job_control.h - @$(MKDIR) $(D_OBJ) - @$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS) - @echo "Compiling "$<"..." +re: fclean all -libft/libft.a: libft/src/*/*.c - @echo "libft/libft.a" - @$(MAKE) -C libft 2>/dev/null +relib: fcleanlib $(LIBFT_LIB) -libft: - @echo "libft" - @$(MAKE) -C libft 2>/dev/null +.PHONY : fclean clean re relib cleanlib fcleanlib -clean: - $(RM) $(D_OBJ) - -fclean: clean - $(RM) $(NAME) - @$(MAKE) fclean -C libft/ 2>/dev/null - -re: fclean all +-include $(OBJS:.o=.d) diff --git a/42sh/includes/glob.h b/42sh/includes/glob.h new file mode 100644 index 00000000..4ba55910 --- /dev/null +++ b/42sh/includes/glob.h @@ -0,0 +1,84 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* glob.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/04 16:31:18 by wescande #+# #+# */ +/* Updated: 2017/01/30 12:12:23 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef GLOB_H +# define GLOB_H + +# include "minishell.h" + +# define CH(x) ((char **)(x)->content) +# define UCH(x) ((unsigned char **)(x)->content) + +typedef struct s_ld +{ + void *content; + struct s_ld *next; + struct s_ld *prev; +} t_ld; + +typedef struct s_glob +{ + const char *pat; + const unsigned char *esc; + t_ld *match; + t_ld *m_pat; +} t_glob; + +typedef struct s_expand +{ + t_ld **wk; + char *str; + unsigned char *esc; + char **split; + 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); +/* +** 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. +*/ +int is_char_esc(const unsigned char *esc, + const char *ini_str, const char *str_pos); + +/* +** LIST D: +*/ +void ft_ld_new(t_ld **alst, void *content); +t_ld *ft_ld_front(t_ld *ld); +void ft_ld_pushfront(t_ld **alst, void *content); +void ft_ld_pushback(t_ld **alst, void *content); +size_t ft_ld_size(t_ld *ld); +void ft_ld_del(t_ld **ld, void (*del)()); +void ft_ld_clear(t_ld **ld, void (*del)()); +void ft_ld_reverse(t_ld **lst); +t_ld *ft_ld_back(t_ld *ld); +t_ld *ft_ld_swap(t_ld *l_cur); +char **ft_ld_to_tab(t_ld *ld); +t_ld *ft_ld_order(t_ld *ld, int (*f)(), void (*del)()); + +/* +** str: +*/ + +char *ft_strjoinf(char *str, char *str2, int mode); +void ft_tabdel(char ***mytab); + +#endif diff --git a/42sh/includes/lexer.h b/42sh/includes/lexer.h index 32641bb4..1df99c39 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/20 15:24:55 by jhalford ### ########.fr */ +/* Updated: 2017/01/30 13:07:23 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -61,9 +61,10 @@ enum e_lexstate struct s_token { - t_type type; - char *data; - int size; + t_type type; + char *data; + unsigned char *esc; + int size; }; typedef struct s_data t_data; @@ -75,7 +76,7 @@ extern int (*g_lexer[])(t_list **alst, char *str); t_token *token_init(); int ft_tokenize(t_list **alst, char *str, t_lexstate state); int ft_post_tokenize(t_list **alst, char *str); -int token_append(t_token *token, char c); +int token_append(t_token *token, char c, short int esc); void token_free(void *data, size_t size); int token_cmp_type(t_token *token, t_type *ref); void token_print(t_list *lst); diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index 2895c065..775ced98 100644 --- a/42sh/includes/minishell.h +++ b/42sh/includes/minishell.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */ -/* Updated: 2017/01/19 15:21:51 by gwojda ### ########.fr */ +/* Updated: 2017/01/30 13:07:38 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,6 +23,7 @@ # include "exec.h" # include "builtin.h" # include "job_control.h" +# include "glob.h" # include # include diff --git a/42sh/libft b/42sh/libft index ab92f0e5..4c497745 160000 --- a/42sh/libft +++ b/42sh/libft @@ -1 +1 @@ -Subproject commit ab92f0e5d817c9d726a8ccf2f11c084ba446bbdf +Subproject commit 4c4977452745481166749b813e8db51dcf0caf44 diff --git a/42sh/src/glob/dir_glob.c b/42sh/src/glob/dir_glob.c new file mode 100644 index 00000000..815e64d6 --- /dev/null +++ b/42sh/src/glob/dir_glob.c @@ -0,0 +1,78 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* dir_glob.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/30 12:07:16 by wescande #+# #+# */ +/* Updated: 2017/01/30 12:07:19 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +int is_directory(const char *path) +{ + struct stat path_stat; + + stat(path, &path_stat); + return (S_ISDIR(path_stat.st_mode)); +} + +void dir_research(t_glob *gl, char *p, const char *pat) +{ + DIR *dir; + struct dirent *in; + char *path_tmp; + + if (ft_strlen(p) <= 1 || p[ft_strlen(p) - 1] != '.') + { + if (!(dir = opendir(p))) + return ; + while ((in = readdir(dir))) + { + if (ft_strcmp(in->d_name, ".") && ft_strcmp(in->d_name, "..")) + { + if (*p == '/' && !*(p + 1)) + path_tmp = ft_strjoin(p, in->d_name); + else + path_tmp = ft_strjoinf(ft_strjoin(p, "/"), in->d_name, 1); + gl->pat = pat; + if (match_pattern(gl, in->d_name, path_tmp)) + ft_ld_pushfront(&gl->match, ft_strdup(path_tmp + 2 * + (path_tmp[0] == '.' && path_tmp[1] == '/'))); + ft_strdel(&path_tmp); + } + } + } +} + +void dir_research_recursive(t_glob *gl, char *p, const char *pat) +{ + DIR *dir; + struct dirent *in; + char *path_tmp; + + if ((ft_strlen(p) <= 1 || p[ft_strlen(p) - 1] != '.') && is_directory(p)) + { + if (!(dir = opendir(p))) + return ; + while ((in = readdir(dir))) + { + if (ft_strcmp(in->d_name, ".") && ft_strcmp(in->d_name, "..")) + { + if (*p == '/' && !*(p + 1)) + path_tmp = ft_strjoin(p, in->d_name); + else + 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)) + ft_ld_pushfront(&gl->match, ft_strdup(path_tmp + 2 * + (path_tmp[0] == '.' && path_tmp[1] == '/'))); + ft_strdel(&path_tmp); + } + } + } +} diff --git a/42sh/src/glob/expand_brace.c b/42sh/src/glob/expand_brace.c new file mode 100644 index 00000000..78211f16 --- /dev/null +++ b/42sh/src/glob/expand_brace.c @@ -0,0 +1,150 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* expand_brace.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/12 19:00:29 by wescande #+# #+# */ +/* Updated: 2017/01/28 01:13:26 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +/* +** expand_brace return expansion of a string. +** pattern searched are {ab, cd}. +** return is t_ld which first param is ab and second is cd +** input parameters are : +** -char *pat -> pattern string to be looking for expand +*/ + +static char **gen_tab(const char *pat, + const unsigned char *esc, int dup) +{ + char **my_tab; + + if (!(my_tab = (char **)malloc(sizeof(char *) * 3))) + return (NULL); + if (dup) + { + my_tab[0] = ft_strdup(pat); + my_tab[1] = ft_strdup((const char *)esc); + } + else + { + my_tab[0] = (char *)pat; + my_tab[1] = (char *)esc; + } + my_tab[2] = NULL; + return (my_tab); +} + +static unsigned char *calc_expand_esc(const unsigned char *esc, + int nb_start, int nb_middle, int *nb_end) +{ + unsigned char *new_esc; + int index; + int pos; + + if (!(new_esc = ft_memalloc(sizeof(char) * + ((nb_start + nb_middle + nb_end[1]) / 8) + 1))) + return (NULL); + index = -1; + while (++index < nb_start) + new_esc[index / 8] |= + ((esc[index / 8] >> (7 - index % 8)) & 1) << (7 - index % 8); + pos = -1; + while (++pos < nb_middle) + { + new_esc[index / 8] |= 1 << (7 - index % 8); + ++index; + } + pos = nb_end[0]; + while (++pos <= nb_end[0] + nb_end[1]) + { + new_esc[index / 8] |= + ((esc[pos / 8] >> (7 - pos % 8)) & 1) << (7 - index % 8); + ++index; + } + return (new_esc); +} + +static void iter_on_each(t_expand *me) +{ + int i; + char **my_new; + char *first; + unsigned char *second; + t_ld *wk_tmp; + + i = -1; + wk_tmp = *me->wk; + while (me->split[++i]) + { + first = ft_strjoinf(ft_strjoin(me->s1, me->split[i]), me->str + 1, 1); + second = calc_expand_esc(me->esc, + ft_strlen(me->s1), + ft_strlen(me->split[i]), + (int[2]){me->str - CH(*me->wk)[0], ft_strlen(me->str + 1)}); + my_new = gen_tab(first, second, 0); + ft_ld_pushfront(&wk_tmp, my_new); + } + me->wk = &wk_tmp; +} + +static int search_brace(t_expand *me) +{ + char *start; + + start = NULL; + while (*me->str) + { + start = *me->str == '{' && !is_char_esc(me->esc, + CH(*me->wk)[0], me->str) ? me->str : start; + if (*me->str == '}' && start + && !is_char_esc(me->esc, CH(*me->wk)[0], me->str)) + { + me->s1 = ft_strsub(start, 1, me->str - start - 1); + me->split = ft_strsplit(me->s1, ','); + ft_strdel(&me->s1); + me->s1 = ft_strsub(CH(*me->wk)[0], 0, start - CH(*me->wk)[0]); + iter_on_each(me); + ft_strdel(&me->s1); + ft_tabdel(&me->split); + return (1); + } + ++me->str; + } + return (0); +} + +void expand_brace(t_glob *gl) +{ + t_ld *tmp; + 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}; + do_it = 1; + while (do_it) + { + do_it = 0; + while (gl->m_pat->next) + { + me.wk = &gl->m_pat; + me.esc = UCH(gl->m_pat)[1]; + me.str = CH(gl->m_pat)[0]; + if ((tmp = gl->m_pat) && search_brace(&me)) + { + ft_ld_del(&tmp, &ft_tabdel); + do_it = 1; + } + gl->m_pat = gl->m_pat->next; + } + gl->m_pat = ft_ld_front(gl->m_pat); + } +} diff --git a/42sh/src/glob/glob.c b/42sh/src/glob/glob.c new file mode 100644 index 00000000..e086c68e --- /dev/null +++ b/42sh/src/glob/glob.c @@ -0,0 +1,96 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* glob.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/04 16:29:54 by wescande #+# #+# */ +/* Updated: 2017/01/30 12:08:49 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +/* +** glob return expansion of a string. +** pattern searched are *, ?, [a-z], [!a-z], [^a-z], {ab, ac}, ** +** input parameters are : +** -char *pat -> pattern string to be looking for match +** -char *esc -> escape binary sequence of *pat +** -char **env -> env var. could be NULL +** 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; + + gl = NULL; + ft_ld_reverse(match); + *match = ft_ld_order(*match, &ft_strcmp, &ft_strdel); + gl = ft_ld_to_tab(*match); + ft_ld_clear(match, &ft_strdel); + 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) +{ + t_glob gl; + char **path; + + gl = (t_glob){pat, esc, NULL, NULL}; + expand_brace(&gl); + while (gl.m_pat->next) + { + 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); + else + dir_research(&gl, "/", gl.pat + 1); + gl.m_pat = gl.m_pat->next; + } + ft_ld_clear(&gl.m_pat, &ft_tabdel); + return (treat_glob(&gl.match)); +} diff --git a/42sh/src/glob/glob_print.c b/42sh/src/glob/glob_print.c new file mode 100644 index 00000000..46fcdd8d --- /dev/null +++ b/42sh/src/glob/glob_print.c @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* glob_print.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/05 16:09:40 by wescande #+# #+# */ +/* Updated: 2017/01/27 21:57:24 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +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_print(t_list *lst, t_data *data) +{ + t_token *token; + int i; + t_type type; + char **glob_ret; + + while (lst) + { + i = 1; + token = lst->content; + 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); + lst = lst->next; + } +} diff --git a/42sh/src/glob/is_char_esc.c b/42sh/src/glob/is_char_esc.c new file mode 100644 index 00000000..5c06a546 --- /dev/null +++ b/42sh/src/glob/is_char_esc.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* is_char_esc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/27 18:19:55 by wescande #+# #+# */ +/* Updated: 2017/01/27 23:45:51 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +int is_char_esc(const unsigned char *esc, + const char *ini_str, const char *str_pos) +{ + int pos; + + pos = str_pos - ini_str; + if ((esc[pos / 8] >> (7 - pos % 8)) & 1) + return (1); + return (0); +} diff --git a/42sh/src/glob/lib_perso/ft_ld_back.c b/42sh/src/glob/lib_perso/ft_ld_back.c new file mode 100644 index 00000000..d448f449 --- /dev/null +++ b/42sh/src/glob/lib_perso/ft_ld_back.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ld_back.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/16 16:23:26 by wescande #+# #+# */ +/* Updated: 2017/01/05 14:16:53 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +t_ld *ft_ld_back(t_ld *ld) +{ + if (ld) + while (ld->next) + ld = ld->next; + return (ld); +} diff --git a/42sh/src/glob/lib_perso/ft_ld_clear.c b/42sh/src/glob/lib_perso/ft_ld_clear.c new file mode 100644 index 00000000..962d3c11 --- /dev/null +++ b/42sh/src/glob/lib_perso/ft_ld_clear.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ld_clear.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/06 19:56:49 by wescande #+# #+# */ +/* Updated: 2017/01/05 14:17:11 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +void ft_ld_clear(t_ld **ld, void (*del)()) +{ + if (!ld || !*ld) + return ; + *ld = ft_ld_front(*ld); + while (*ld) + { + ft_ld_del(ld, del); + } +} diff --git a/42sh/src/glob/lib_perso/ft_ld_del.c b/42sh/src/glob/lib_perso/ft_ld_del.c new file mode 100644 index 00000000..69852e67 --- /dev/null +++ b/42sh/src/glob/lib_perso/ft_ld_del.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ld_del.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/06 15:54:53 by wescande #+# #+# */ +/* Updated: 2017/01/05 14:17:18 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +void ft_ld_del(t_ld **ld, void (*del)()) +{ + t_ld *next; + t_ld *prev; + + if (!ld || !*ld) + return ; + next = (*ld)->next; + prev = (*ld)->prev; + if ((*ld)->content && del) + del(&(*ld)->content); + free(*ld); + if (next) + next->prev = prev; + if (prev) + { + prev->next = next; + *ld = prev; + } + else + *ld = next; +} diff --git a/42sh/src/glob/lib_perso/ft_ld_front.c b/42sh/src/glob/lib_perso/ft_ld_front.c new file mode 100644 index 00000000..1599f49e --- /dev/null +++ b/42sh/src/glob/lib_perso/ft_ld_front.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ld_front.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/01 18:26:11 by wescande #+# #+# */ +/* Updated: 2017/01/05 14:17:27 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +t_ld *ft_ld_front(t_ld *ld) +{ + if (ld) + while (ld->prev) + ld = ld->prev; + return (ld); +} diff --git a/42sh/src/glob/lib_perso/ft_ld_new.c b/42sh/src/glob/lib_perso/ft_ld_new.c new file mode 100644 index 00000000..eece5e18 --- /dev/null +++ b/42sh/src/glob/lib_perso/ft_ld_new.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ld_new.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/01 18:17:34 by wescande #+# #+# */ +/* Updated: 2017/01/05 14:17:34 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +void ft_ld_new(t_ld **alst, void *content) +{ + t_ld *new; + + if ((new = (t_ld *)malloc(sizeof(t_ld)))) + { + new->content = content; + new->prev = NULL; + new->next = NULL; + *alst = new; + } +} diff --git a/42sh/src/glob/lib_perso/ft_ld_order.c b/42sh/src/glob/lib_perso/ft_ld_order.c new file mode 100644 index 00000000..6f6b2a9c --- /dev/null +++ b/42sh/src/glob/lib_perso/ft_ld_order.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ld_order.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/24 19:20:45 by wescande #+# #+# */ +/* Updated: 2017/01/24 19:40:54 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +t_ld *ft_ld_order(t_ld *ld, int (*f)(), void (*del)()) +{ + int swap; + + swap = 1; + ld = ft_ld_front(ld); + while (swap) + { + swap = 0; + while (ld && ld->next) + { + if (f(ld->content, ld->next->content) > 0 && (swap = 1)) + ld = ft_ld_swap(ld); + else if (!f(ld->content, ld->next->content)) + ft_ld_del(&ld, del); + ld = ld->next; + } + ld = ft_ld_front(ld); + } + return (ld); +} diff --git a/42sh/src/glob/lib_perso/ft_ld_pushback.c b/42sh/src/glob/lib_perso/ft_ld_pushback.c new file mode 100644 index 00000000..25a1d019 --- /dev/null +++ b/42sh/src/glob/lib_perso/ft_ld_pushback.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ld_pushback.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/16 15:59:38 by wescande #+# #+# */ +/* Updated: 2017/01/05 14:17:40 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +void ft_ld_pushback(t_ld **alst, void *content) +{ + t_ld *new; + t_ld *back; + + if (!alst || !*alst) + return (ft_ld_new(alst, content)); + if ((new = (t_ld *)malloc(sizeof(t_ld)))) + { + back = ft_ld_back(*alst); + new->prev = back; + new->next = NULL; + new->content = content; + back->next = new; + } +} diff --git a/42sh/src/glob/lib_perso/ft_ld_pushfront.c b/42sh/src/glob/lib_perso/ft_ld_pushfront.c new file mode 100644 index 00000000..5f830ee1 --- /dev/null +++ b/42sh/src/glob/lib_perso/ft_ld_pushfront.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ld_pushfront.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/01 18:32:40 by wescande #+# #+# */ +/* Updated: 2017/01/05 14:17:46 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +void ft_ld_pushfront(t_ld **alst, void *content) +{ + t_ld *new; + t_ld *front; + + if (!alst || !*alst) + return (ft_ld_new(alst, content)); + if ((new = (t_ld *)malloc(sizeof(t_ld)))) + { + front = ft_ld_front(*alst); + new->next = front; + new->prev = NULL; + new->content = content; + front->prev = new; + *alst = new; + } +} diff --git a/42sh/src/glob/lib_perso/ft_ld_reverse.c b/42sh/src/glob/lib_perso/ft_ld_reverse.c new file mode 100644 index 00000000..62ffe670 --- /dev/null +++ b/42sh/src/glob/lib_perso/ft_ld_reverse.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ld_reverse.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/12 14:16:04 by wescande #+# #+# */ +/* Updated: 2017/01/05 14:17:53 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +void ft_ld_reverse(t_ld **lst) +{ + t_ld *l_prev; + t_ld *l_next; + t_ld *l_cur; + + l_cur = ft_ld_front(*lst); + l_prev = NULL; + while (l_cur) + { + l_next = l_cur->next; + l_cur->next = l_prev; + l_cur->prev = l_next; + l_prev = l_cur; + l_cur = l_next; + } + *lst = l_prev; +} diff --git a/42sh/src/glob/lib_perso/ft_ld_size.c b/42sh/src/glob/lib_perso/ft_ld_size.c new file mode 100644 index 00000000..aab41b06 --- /dev/null +++ b/42sh/src/glob/lib_perso/ft_ld_size.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ld_size.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/01 18:33:48 by wescande #+# #+# */ +/* Updated: 2017/01/05 14:17:59 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +size_t ft_ld_size(t_ld *ld) +{ + size_t size; + + size = 0; + ld = ft_ld_front(ld); + while (ld) + { + ++size; + ld = ld->next; + } + return (size); +} diff --git a/42sh/src/glob/lib_perso/ft_ld_swap.c b/42sh/src/glob/lib_perso/ft_ld_swap.c new file mode 100644 index 00000000..6e6ff87b --- /dev/null +++ b/42sh/src/glob/lib_perso/ft_ld_swap.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ld_swap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/24 21:12:53 by wescande #+# #+# */ +/* Updated: 2017/01/05 14:18:06 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +t_ld *ft_ld_swap(t_ld *l_cur) +{ + t_ld *l_next; + t_ld *l_prev; + + l_next = l_cur; + if (l_cur && l_cur->next) + { + l_prev = l_cur->prev; + l_next = l_cur->next; + l_cur->next = l_next->next; + if (l_cur->next) + l_cur->next->prev = l_cur; + l_cur->prev = l_next; + l_next->next = l_cur; + l_next->prev = l_prev; + if (l_prev) + l_prev->next = l_next; + } + return (l_next); +} diff --git a/42sh/src/glob/lib_perso/ft_ld_to_tab.c b/42sh/src/glob/lib_perso/ft_ld_to_tab.c new file mode 100644 index 00000000..e6db262d --- /dev/null +++ b/42sh/src/glob/lib_perso/ft_ld_to_tab.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ld_to_tab.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/05 16:17:00 by wescande #+# #+# */ +/* Updated: 2017/01/05 16:22:12 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +char **ft_ld_to_tab(t_ld *ld) +{ + char **my_tab; + int nb_item; + int index; + + if (!ld) + return (NULL); + nb_item = ft_ld_size(ld); + if (!(my_tab = (char **)malloc(sizeof(char *) * (nb_item + 1)))) + return (NULL); + index = 0; + while (ld) + { + my_tab[index++] = ft_strdup((char *)ld->content); + ld = ld->next; + } + my_tab[index] = NULL; + return (my_tab); +} diff --git a/42sh/src/glob/lib_perso/ft_strjoinf.c b/42sh/src/glob/lib_perso/ft_strjoinf.c new file mode 100644 index 00000000..3bc72d56 --- /dev/null +++ b/42sh/src/glob/lib_perso/ft_strjoinf.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoinf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/05 13:33:24 by wescande #+# #+# */ +/* Updated: 2017/01/24 16:53:13 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +char *ft_strjoinf(char *s1, char *s2, int state) +{ + char *ans; + + ans = ft_strjoin((const char *)s1, (const char *)s2); + if (state == 1 || state == 3) + ft_strdel(&s1); + if (state == 2 || state == 3) + ft_strdel(&s2); + return (ans); +} diff --git a/42sh/src/glob/lib_perso/ft_tabdel.c b/42sh/src/glob/lib_perso/ft_tabdel.c new file mode 100644 index 00000000..ac72a1a1 --- /dev/null +++ b/42sh/src/glob/lib_perso/ft_tabdel.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* glob.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/04 16:29:54 by wescande #+# #+# */ +/* Updated: 2017/01/24 16:52:50 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +void ft_tabdel(char ***mytab) +{ + char **erase; + int i; + + if (!mytab || !*mytab) + return ; + erase = *mytab; + i = 0; + while (erase[i]) + { + ft_strdel(&erase[i]); + ++i; + } + free(*mytab); + *mytab = NULL; +} diff --git a/42sh/src/glob/match_pattern.c b/42sh/src/glob/match_pattern.c new file mode 100644 index 00000000..8707c3c4 --- /dev/null +++ b/42sh/src/glob/match_pattern.c @@ -0,0 +1,123 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* match_pattern.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/24 17:30:23 by wescande #+# #+# */ +/* Updated: 2017/01/27 23:45:39 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +static int match_bracket_char(char **cmp, const char *pat, char c, int neg) +{ + int dir; + char s; + + if ((*cmp + 2) < pat && (*cmp)[1] == '-' && (s = (*cmp)[0])) + { + dir = s > (*cmp)[2]; + while ((dir && s >= (*cmp)[2]) || (!dir && s <= (*cmp)[2])) + { + if (!neg && s == c) + return (1); + else if (neg && s == c) + return (0); + s += (dir * -2 + 1); + } + *cmp += 2; + } + else if (!neg && **cmp == c) + return (1); + else if (neg && **cmp == c) + return (0); + return (-1); +} + +static int match_bracket(t_glob *gl, char c) +{ + char *cmp; + int neg; + int ret; + + cmp = (char *)gl->pat + 1; + while (*gl->pat != ']' + || is_char_esc(gl->esc, ((char **)gl->m_pat->content)[0], gl->pat)) + { + if (!*gl->pat) + return (0); + ++gl->pat; + } + neg = 0; + if ((*cmp == '^' || *cmp == '!') + && !is_char_esc(gl->esc, ((char **)gl->m_pat->content)[0], cmp) + && ++neg) + ++cmp; + while (cmp < gl->pat) + { + ret = match_bracket_char(&cmp, gl->pat, c, neg); + if (ret != -1) + return (ret); + ++cmp; + } + return (neg); +} + +static int match_star(t_glob *gl, char *str, char *full_word) +{ + char *fix; + + if (gl->pat[1] == '*' && + !is_char_esc(gl->esc, ((char **)gl->m_pat->content)[0], gl->pat + 1)) + dir_research_recursive(gl, full_word, gl->pat + 1); + if (!*++gl->pat) + return (1); + fix = str + ft_strlen(str); + while (fix > str) + { + if (match_pattern(gl, fix, full_word)) + return (1); + --fix; + } + return (0); +} + +const char *manage_pat(const char *pat, char *str) +{ + if (pat[0] == '.' && pat[1] == '/' + && ((str[0] == '.' && str[1] != '/') || str[0] != '.')) + return (pat + 2); + return (pat); +} + +int match_pattern(t_glob *gl, char *str, char *full_word) +{ + gl->pat = manage_pat(gl->pat, str); + while (*gl->pat) + { + if (is_char_esc(gl->esc, ((char **)gl->m_pat->content)[0], gl->pat)) + { + if (*str != *gl->pat) + return (0); + } + else if (*gl->pat == '?') + str++; + else if (*gl->pat == '[') + { + if (!match_bracket(gl, *str)) + return (0); + } + else if (*gl->pat == '*') + return (match_star(gl, str, full_word)); + else if (*gl->pat == '/' && !*str && is_directory(full_word)) + dir_research(gl, full_word, gl->pat + 1); + else if (*gl->pat != *str) + return (0); + ++str; + ++gl->pat; + } + return (*str ? 0 : 1); +} diff --git a/42sh/src/lexer/ft_tokenize.c b/42sh/src/lexer/ft_tokenize.c index 3955993f..de620b65 100644 --- a/42sh/src/lexer/ft_tokenize.c +++ b/42sh/src/lexer/ft_tokenize.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 13:37:11 by jhalford #+# #+# */ -/* Updated: 2017/01/11 16:11:02 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 15:43:46 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_backslash.c b/42sh/src/lexer/lexer_backslash.c index 8439d549..d1da0d6c 100644 --- a/42sh/src/lexer/lexer_backslash.c +++ b/42sh/src/lexer/lexer_backslash.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 11:56:49 by jhalford #+# #+# */ -/* Updated: 2017/01/26 16:30:42 by jhalford ### ########.fr */ +/* Updated: 2017/01/30 13:07:52 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,6 +18,6 @@ int lexer_backslash(t_list **alst, char *str) token = (*alst)->content; token->type = TK_WORD; - token_append(token, str[1]); + token_append(token, str[1], 1); return (ft_tokenize(alst, str + 2, WORD)); } diff --git a/42sh/src/lexer/lexer_default.c b/42sh/src/lexer/lexer_default.c index 04c87fca..08ccabcf 100644 --- a/42sh/src/lexer/lexer_default.c +++ b/42sh/src/lexer/lexer_default.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 18:36:21 by jhalford #+# #+# */ -/* Updated: 2017/01/11 16:10:53 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 15:53:10 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,7 +28,7 @@ int lexer_default(t_list **alst, char *str) else state = WORD; token = (*alst)->content; - token_append(token, *str); + token_append(token, *str, 0); token->type = TK_N_WORD; return (ft_tokenize(alst, str + 1, state)); } diff --git a/42sh/src/lexer/lexer_dquote.c b/42sh/src/lexer/lexer_dquote.c index d5c1b2e3..56a49ab2 100644 --- a/42sh/src/lexer/lexer_dquote.c +++ b/42sh/src/lexer/lexer_dquote.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 18:36:58 by jhalford #+# #+# */ -/* Updated: 2017/01/10 15:15:24 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 15:53:31 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,9 +23,9 @@ int lexer_dquote(t_list **alst, char *str) return (ft_tokenize(alst, str + 1, DEFAULT)); if (*str == '\\') { - token_append(token, *(str + 1)); + token_append(token, *(str + 1), 1); return (lexer_dquote(alst, str + 1)); } - token_append(token, *str); + token_append(token, *str, 1); return (lexer_dquote(alst, str)); } diff --git a/42sh/src/lexer/lexer_great.c b/42sh/src/lexer/lexer_great.c index d02a183b..82b9e4b4 100644 --- a/42sh/src/lexer/lexer_great.c +++ b/42sh/src/lexer/lexer_great.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:06:35 by jhalford #+# #+# */ -/* Updated: 2017/01/10 14:48:49 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 15:54:53 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,17 +17,17 @@ int lexer_great(t_list **alst, char *str) t_token *token; token = (*alst)->content; - token_append(token, str[0]); + token_append(token, str[0], 0); if (*(str + 1) == '&') { token->type = TK_GREATAND; - token_append(token, str[1]); + token_append(token, str[1], 0); return (lexer_greatand(alst, str + 2)); } else if (*(str + 1) == '>') { token->type = TK_DGREAT; - token_append(token, str[1]); + token_append(token, str[1], 0); return (ft_tokenize(&(*alst)->next, str + 2, DEFAULT)); } else diff --git a/42sh/src/lexer/lexer_greatand.c b/42sh/src/lexer/lexer_greatand.c index 7a32ef24..b803f0f1 100644 --- a/42sh/src/lexer/lexer_greatand.c +++ b/42sh/src/lexer/lexer_greatand.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 11:56:58 by jhalford #+# #+# */ -/* Updated: 2016/12/03 11:57:09 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 15:55:04 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,12 +20,12 @@ int lexer_greatand(t_list **alst, char *str) token->type = TK_GREATAND; if (ft_isdigit(*str)) { - token_append(token, *str); + token_append(token, *str, 0); return (lexer_greatand(alst, str + 1)); } else if (*str == '-') { - token_append(token, *str); + token_append(token, *str, 0); return (ft_tokenize(&(*alst)->next, str + 1, DEFAULT)); } return (ft_tokenize(alst, str, DEFAULT)); diff --git a/42sh/src/lexer/lexer_less.c b/42sh/src/lexer/lexer_less.c index bc3fa958..ea7c8398 100644 --- a/42sh/src/lexer/lexer_less.c +++ b/42sh/src/lexer/lexer_less.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:06:53 by jhalford #+# #+# */ -/* Updated: 2017/01/10 14:27:51 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 15:55:16 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,17 +17,17 @@ int lexer_less(t_list **alst, char *str) t_token *token; token = (*alst)->content; - token_append(token, str[0]); + token_append(token, str[0], 0); if (*(str + 1) == '&') { token->type = TK_LESSAND; - token_append(token, str[1]); + token_append(token, str[1], 0); return (lexer_lessand(alst, str + 2)); } else if (*(str + 1) == '<') { token->type = TK_DLESS; - token_append(token, str[1]); + token_append(token, str[1], 0); return (ft_tokenize(&(*alst)->next, str + 2, DEFAULT)); } else diff --git a/42sh/src/lexer/lexer_lessand.c b/42sh/src/lexer/lexer_lessand.c index 089c78bd..fc6eef56 100644 --- a/42sh/src/lexer/lexer_lessand.c +++ b/42sh/src/lexer/lexer_lessand.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 11:58:51 by jhalford #+# #+# */ -/* Updated: 2016/12/03 11:58:52 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 15:55:27 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,12 +20,12 @@ int lexer_lessand(t_list **alst, char *str) token->type = TK_LESSAND; if (ft_isdigit(*str)) { - token_append(token, *str); + token_append(token, *str, 0); return (lexer_lessand(alst, str + 1)); } else if (*str == '-') { - token_append(token, *str); + token_append(token, *str, 0); return (ft_tokenize(&(*alst)->next, str + 1, DEFAULT)); } return (ft_tokenize(alst, str, DEFAULT)); diff --git a/42sh/src/lexer/lexer_number.c b/42sh/src/lexer/lexer_number.c index 722f1c1e..1309e780 100644 --- a/42sh/src/lexer/lexer_number.c +++ b/42sh/src/lexer/lexer_number.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:06:45 by jhalford #+# #+# */ -/* Updated: 2017/01/22 17:40:20 by jhalford ### ########.fr */ +/* Updated: 2017/01/30 13:08:05 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,7 +26,7 @@ int lexer_number(t_list **alst, char *str) return (ft_tokenize(alst, str, LESS)); else if (ft_isdigit(*str)) { - token_append(token, *str); + token_append(token, *str, 0); return (lexer_number(alst, str + 1)); } return (ft_tokenize(alst, str, DEFAULT)); diff --git a/42sh/src/lexer/lexer_quote.c b/42sh/src/lexer/lexer_quote.c index 401fd07f..4b99e6fe 100644 --- a/42sh/src/lexer/lexer_quote.c +++ b/42sh/src/lexer/lexer_quote.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:07:08 by jhalford #+# #+# */ -/* Updated: 2017/01/10 15:13:06 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 15:52:21 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,6 +21,6 @@ int lexer_quote(t_list **alst, char *str) str++; if (*str == '\'') return (ft_tokenize(alst, str + 1, WORD)); - token_append(token, *str); + token_append(token, *str, 1); return (lexer_quote(alst, str)); } diff --git a/42sh/src/lexer/lexer_var.c b/42sh/src/lexer/lexer_var.c index 61011338..ab6e0b79 100644 --- a/42sh/src/lexer/lexer_var.c +++ b/42sh/src/lexer/lexer_var.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/10 14:54:57 by jhalford #+# #+# */ -/* Updated: 2017/01/10 16:36:15 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 15:55:54 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,7 +21,7 @@ int lexer_var(t_list **alst, char *str) token->type = TK_N_WORD; str++; if (!ft_strchr(token->data, '$')) - token_append(token, '$'); + token_append(token, '$', 0); if (!*str) { token_expand_var(token); @@ -32,6 +32,6 @@ int lexer_var(t_list **alst, char *str) token_expand_var(token); return (ft_tokenize(alst, str, state)); } - token_append(token, *str); + token_append(token, *str, 0); return (lexer_var(alst, str)); } diff --git a/42sh/src/lexer/lexer_word.c b/42sh/src/lexer/lexer_word.c index 8844b7db..e0081f8b 100644 --- a/42sh/src/lexer/lexer_word.c +++ b/42sh/src/lexer/lexer_word.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:07:11 by jhalford #+# #+# */ -/* Updated: 2017/01/26 15:56:19 by jhalford ### ########.fr */ +/* Updated: 2017/01/30 13:08:16 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,6 +25,6 @@ int lexer_word(t_list **alst, char *str) return (ft_tokenize(&(*alst)->next, str, GREAT)); else if (*str == '<') return (ft_tokenize(&(*alst)->next, str, LESS)); - token_append(token, *str); + token_append(token, *str, 0); return (ft_tokenize(alst, str + 1, WORD)); } diff --git a/42sh/src/lexer/token_append.c b/42sh/src/lexer/token_append.c index 2c422840..9cc197fb 100644 --- a/42sh/src/lexer/token_append.c +++ b/42sh/src/lexer/token_append.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/11 17:18:42 by jhalford #+# #+# */ -/* Updated: 2017/01/26 18:32:42 by jhalford ### ########.fr */ +/* Updated: 2017/01/30 13:09:01 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,13 +14,21 @@ #define TOKEN_INCR 10 -int token_append(t_token *token, char c) +int token_append(t_token *token, char c, short int esc) { - if ((int)ft_strlen(token->data) >= token->size) + int len; + + len = ft_strlen(token->data); + if (len >= token->size) { - token->data = (char *)ft_realloc(token->data, token->size + TOKEN_INCR); - token->size += TOKEN_INCR - 1; + token->size += 8; + token->data = (char *)ft_realloc(token->data, token->size + 1); + token->esc = (unsigned char *)ft_realloc((char *)token->esc, + token->size / 8 + 1); + token->esc[token->size / 8 - 1] = 0; } - ft_strcat(token->data, (char[]){c, 0}); + ft_strcat(token->data, (char[2]){c, '\0'}); + if (esc) + token->esc[len / 8] |= 1 << (7 - len % 8); return (0); } diff --git a/42sh/src/lexer/token_expand_var.c b/42sh/src/lexer/token_expand_var.c index b729611c..9743d062 100644 --- a/42sh/src/lexer/token_expand_var.c +++ b/42sh/src/lexer/token_expand_var.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/10 14:57:53 by jhalford #+# #+# */ -/* Updated: 2017/01/10 16:37:33 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 15:56:40 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,5 +24,5 @@ void token_expand_var(t_token *token) *dollar = 0; if (val) while (*val) - token_append(token, *val++); + token_append(token, *val++, 1); } diff --git a/42sh/src/lexer/token_free.c b/42sh/src/lexer/token_free.c index 50ef0d0f..466d4407 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/12 13:18:46 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 21:54:05 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,6 +19,9 @@ void token_free(void *data, size_t size) (void)size; token = data; if (!(token->type & TK_NON_FREEABLE)) + { ft_strdel(&token->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 cf9fe1b7..009f27dd 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/11 15:45:10 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 21:54:53 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,8 @@ t_token *token_init(void) token = (t_token *)malloc(sizeof(t_token)); token->type = 0; - token->size = 10; - token->data = ft_strnew(token->size); + token->size = 8; + token->data = ft_strnew(token->size + 1); + token->esc = (unsigned char *)ft_strnew(token->size / 8 + 1); return (token); } diff --git a/42sh/src/lexer/token_print.c b/42sh/src/lexer/token_print.c index 97876ddb..343fe5bc 100644 --- a/42sh/src/lexer/token_print.c +++ b/42sh/src/lexer/token_print.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:39:01 by jhalford #+# #+# */ -/* Updated: 2017/01/12 14:48:33 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 21:57:05 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,7 @@ void token_print(t_list *lst) t_token *token; int i; t_type type; + int index; while (lst) { @@ -26,6 +27,7 @@ void token_print(t_list *lst) while (type >> (i++ + 2)) ; DG("%02i '%s'", i, token->data); + index = -1; lst = lst->next; } } diff --git a/42sh/src/main/ft_cleanup.c b/42sh/src/main/ft_cleanup.c new file mode 100644 index 00000000..7cf6aa3b --- /dev/null +++ b/42sh/src/main/ft_cleanup.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_cleanup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/01 14:42:42 by jhalford #+# #+# */ +/* Updated: 2016/12/09 21:50:38 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +extern char **environ; +extern char PC; +extern char *UP; +extern char *BC; + +void ft_cleanup(void) +{ + struct termios term; + + DG("cleanup. char * UP at %p", UP); + DG("cleanup. char * BC at %p", BC); + if (tcgetattr(0, &term) == -1) + return ; + term.c_lflag |= ICANON | ISIG | ECHO; + if (tcsetattr(0, TCSANOW, &term) == -1) + return ; +} diff --git a/42sh/src/main/lib_expansion.c b/42sh/src/main/lib_expansion.c new file mode 100644 index 00000000..8905c284 --- /dev/null +++ b/42sh/src/main/lib_expansion.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* lib_expansion.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/03 13:37:41 by jhalford #+# #+# */ +/* Updated: 2016/12/03 13:37:41 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void ft_expand_dollar(char **av, char **env) +{ + char *dollar; + + while (*av) + { + if ((dollar = ft_strchr(*av, '$'))) + { + *dollar = '\0'; + *av = ft_strjoin(*av, ft_getenv(env, dollar + 1)); + } + av++; + } +} diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 7cdd2a95..55c960f3 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/24 15:04:06 by gwojda ### ########.fr */ +/* Updated: 2017/01/30 13:09:12 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,11 +26,11 @@ int shell_single_command(char *command) return (1); if (!token) return (0); - token_print(token); if (ft_post_tokenize(&token, command)) return (1); DG("after post_tokenize"); token_print(token); + glob_print(token, data_singleton()); if (ft_parse(&ast, &token)) return (1); btree_print(STDBUG, ast, &ft_putast); diff --git a/42sh/src/main/sig_handler.c b/42sh/src/main/sig_handler.c new file mode 100644 index 00000000..d56b03ee --- /dev/null +++ b/42sh/src/main/sig_handler.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sig_handler.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/01 12:43:22 by jhalford #+# #+# */ +/* Updated: 2016/12/03 13:31:33 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +pid_t g_pid; + +void sig_handler(int signo) +{ + (void)signo; + if (signo == SIGINT) + { + if (g_pid) + kill(g_pid, SIGINT); + if (kill(g_pid, 0) == 0) + ft_putendl(""); + } +} diff --git a/42sh/update_makefile.sh b/42sh/update_makefile.sh new file mode 100755 index 00000000..a1b3228d --- /dev/null +++ b/42sh/update_makefile.sh @@ -0,0 +1,17 @@ +MYPATH=$(pwd) +CUR_MAKEFILE=$MYPATH/Makefile +if [ -e $CUR_MAKEFILE ] +then + echo "regenerate Makefile" + sed "`grep -n 'SRC_BASE =' $CUR_MAKEFILE | sed 's/:.*//'`, \$d" $CUR_MAKEFILE > NEWMAKEFILE + grep 'SRC_BASE =' $CUR_MAKEFILE >> NEWMAKEFILE + expr "$(find ./src | grep "\.c" | sed -e 's/src\///' -e 's/\.\///' -e 's/$/\\/')" : "\(.*\).$" >> NEWMAKEFILE + echo "" >> NEWMAKEFILE + grep 'SRCS =' $CUR_MAKEFILE >> NEWMAKEFILE + sed "1, `grep -n 'SRCS =' $CUR_MAKEFILE | sed 's/:.*//'`d" $CUR_MAKEFILE >> NEWMAKEFILE + mv $CUR_MAKEFILE ~/Documents/.OLDMakefile + mv NEWMAKEFILE $CUR_MAKEFILE + echo "Makefile done (copy still alive in ~/Documents/.OLDMakefile)" +else + echo "Makefile not found." +fi