diff --git a/42sh/Makefile b/42sh/Makefile index 158d7b2e..1a249d59 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -16,8 +16,7 @@ CC = gcc FLAGS = -Wall -Wextra -Werror D_FLAGS = -g -LEN_NAME = `printf "%s" $(NAME) |wc -c` -DELTA = $$(echo "$$(tput cols)-37-$(LEN_NAME)"|bc) +DELTA = $$(echo "$$(tput cols)-47"|bc) LIBFT_DIR = libft/ LIBFT_LIB = $(LIBFT_DIR)libft.a @@ -61,6 +60,9 @@ exec/process_setgroup.c\ exec/set_exitstatus.c\ glob/dir_glob.c\ glob/expand_brace.c\ +glob/expand_esc.c\ +glob/ft_strsplit_esc.c\ +glob/ft_strsplit_spe.c\ glob/glob.c\ glob/is_char_esc.c\ glob/lib_perso/ft_ld_back.c\ @@ -77,6 +79,7 @@ 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/lib_perso/ft_tablen.c\ glob/match_pattern.c\ job-control/builtin_bg.c\ job-control/builtin_fg.c\ @@ -215,7 +218,7 @@ $(OBJ_DIR)%.o : $(SRC_DIR)%.c | $(OBJ_DIR) @$(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) "$@" + @printf "\r\033[38;5;11m⌛ MAKE %10.10s : %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) diff --git a/42sh/includes/glob.h b/42sh/includes/glob.h index fb5efd35..f6beebb8 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/31 17:54:19 by wescande ### ########.fr */ +/* Updated: 2017/01/31 23:20:22 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,6 +39,7 @@ typedef struct s_expand char *str; unsigned char *esc; char **split; + unsigned char **m_esc; char *s1; } t_expand; @@ -61,10 +62,19 @@ int is_char_esc(const unsigned char *esc, /* ** Internal function. */ +unsigned char *ft_sub_esc(const unsigned char *esc, int start, int len); +unsigned char *calc_expand_esc(const unsigned char *esc, + int nb_start, int nb_middle, int *nb_end); +void modify_esc_split(unsigned char *esc_dest, + unsigned char *esc_src, int start, int len); void expand_brace(t_glob *tglob); int match_pattern(t_glob *tglob, char *str, char *full_word); int dir_research(t_glob *tglob, char *p, const char *pat); int dir_research_recursive(t_glob *tglob, char *p, const char *pat); +char **ft_strsplit_spe(const char *str, + const unsigned char *esc, char c); +unsigned char **ft_strsplit_esc(const char *str, + const unsigned char *esc, char c); /* ** LIST D: */ @@ -87,5 +97,6 @@ t_ld *ft_ld_order(t_ld *ld, int (*f)(), void (*del)()); char *ft_strjoinf(char *str, char *str2, int mode); void ft_tabdel(char ***mytab); +int ft_tablen(char **mytab); #endif diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index da867828..6a6924fc 100644 --- a/42sh/src/exec/exec_command.c +++ b/42sh/src/exec/exec_command.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 17:28:14 by jhalford #+# #+# */ -/* Updated: 2017/01/31 19:12:23 by wescande ### ########.fr */ +/* Updated: 2017/01/31 20:19:48 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,10 +23,7 @@ static char **return_array_expand(t_ld *ld) 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]); @@ -36,22 +33,6 @@ static char **return_array_expand(t_ld *ld) 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; @@ -62,7 +43,6 @@ int exec_command(t_btree **ast) p = &data_singleton()->exec.process; job = &data_singleton()->exec.job; 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/expand_brace.c b/42sh/src/glob/expand_brace.c index c570b6aa..6aa16d58 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/31 18:19:34 by wescande ### ########.fr */ +/* Updated: 2017/01/31 23:20:38 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,40 +38,9 @@ static char **gen_tab(const 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; @@ -89,31 +58,50 @@ static void iter_on_each(t_expand *me) ft_strlen(me->s1), ft_strlen(me->split[i]), (int[2]){me->str - CH(*me->wk)[0], ft_strlen(me->str + 1)}); + modify_esc_split(second, me->m_esc[i], + ft_strlen(me->s1), ft_strlen(me->split[i])); my_new = gen_tab(first, second, 0); ft_ld_pushfront(&wk_tmp, my_new); } me->wk = &wk_tmp; } +static void init_expand(t_expand *me, char *start) +{ + unsigned char *esc; + + me->s1 = ft_strsub(start, 1, me->str - start - 1); + esc = ft_sub_esc(me->esc, start - CH(*me->wk)[0] + 1, me->str - start); + me->split = ft_strsplit_spe(me->s1, esc, ','); + me->m_esc = ft_strsplit_esc(me->s1, esc, ','); + ft_strdel(&me->s1); + ft_strdel((char **)&esc); + 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); + ft_tabdel((char ***)&me->m_esc); +} + static int search_brace(t_expand *me) { - char *start; + char *start; + int nb; start = NULL; + nb = 0; 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)) + start = *me->str == '{' + && !is_char_esc(me->esc, CH(*me->wk)[0], me->str) + && nb == 0 ? me->str : start; + nb += *me->str == '{' + && !is_char_esc(me->esc, CH(*me->wk)[0], me->str); + nb -= *me->str == '}' + && !is_char_esc(me->esc, CH(*me->wk)[0], me->str); + if (!nb && start) { - 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); + init_expand(me, start); return (1); } ++me->str; @@ -127,15 +115,14 @@ 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}; + me = (t_expand){NULL, NULL, NULL, NULL, NULL, NULL}; do_it = 1; while (do_it) { do_it = 0; - while (gl->m_pat->next) + while (gl->m_pat->next && !do_it) { me.wk = &gl->m_pat; me.esc = UCH(gl->m_pat)[1]; diff --git a/42sh/src/glob/expand_esc.c b/42sh/src/glob/expand_esc.c new file mode 100644 index 00000000..9ad603e4 --- /dev/null +++ b/42sh/src/glob/expand_esc.c @@ -0,0 +1,76 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* expand_esc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/31 23:18:56 by wescande #+# #+# */ +/* Updated: 2017/01/31 23:19:45 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +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 = (unsigned char *)ft_strnew(((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] |= 0 << (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); +} + +void modify_esc_split(unsigned char *esc_dest, + unsigned char *esc_src, int start, int len) +{ + int index; + int wk; + + index = -1; + while (++index < len) + { + wk = index + start - 1; + esc_dest[wk / 8] |= + ((esc_src[index / 8] >> (7 - index % 8)) & 1) << (7 - wk % 8); + } +} + +unsigned char *ft_sub_esc(const unsigned char *esc, + int start, int len) +{ + unsigned char *new_esc; + int index; + + if (!(new_esc = (unsigned char *)ft_strnew(((len) / 8) + 1))) + return (NULL); + index = -1; + while (++index < len) + { + new_esc[index / 8] |= + ((esc[start / 8] >> (7 - start % 8)) & 1) << (7 - index % 8); + ++start; + } + return (new_esc); +} diff --git a/42sh/src/glob/ft_strsplit_esc.c b/42sh/src/glob/ft_strsplit_esc.c new file mode 100644 index 00000000..f37c7b14 --- /dev/null +++ b/42sh/src/glob/ft_strsplit_esc.c @@ -0,0 +1,86 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strsplit_esc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/31 22:18:46 by wescande #+# #+# */ +/* Updated: 2017/01/31 23:15:17 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +static int ft_strlen_c(const char *str, const char *fix, + const unsigned char *esc, char c) +{ + int len; + int nb; + + len = 0; + nb = 0; + while (*str && (*str != c || nb)) + { + nb += *str == '{' && !is_char_esc(esc, fix, str); + nb -= *str == '}' && !is_char_esc(esc, fix, str); + ++str; + ++len; + } + return (len); +} + +static int nb_c(const char *str, const unsigned char *esc, char c) +{ + int len; + int nb; + const char *fix; + + len = 0; + nb = 0; + fix = str; + while (*str) + { + while (*str && *str == c) + ++str; + if (*str) + ++len; + while (*str && (*str != c || nb)) + { + nb += *str == '{' && !is_char_esc(esc, fix, str); + nb -= *str == '}' && !is_char_esc(esc, fix, str); + ++str; + } + } + return (len); +} + +unsigned char **ft_strsplit_esc(const char *str, + const unsigned char *esc, char c) +{ + unsigned char **s1; + int i; + const char *fix; + int len; + int pos; + + if ((pos = -1) && !str) + return (NULL); + if (!(s1 = (unsigned char **)malloc(sizeof(*s1) * (nb_c(str, esc, c) + 1)))) + return (NULL); + i = 0; + fix = str; + while (*str && *str == c && ++pos > -1) + ++str; + while (*str && (len = ft_strlen_c(str, fix, esc, c))) + { + s1[i] = ft_sub_esc(esc, pos, len); + str += len; + pos += len; + ++i; + while (*str && *str == c && ++pos) + ++str; + } + s1[i] = 0; + return (s1); +} diff --git a/42sh/src/glob/ft_strsplit_spe.c b/42sh/src/glob/ft_strsplit_spe.c new file mode 100644 index 00000000..a76d475e --- /dev/null +++ b/42sh/src/glob/ft_strsplit_spe.c @@ -0,0 +1,86 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strsplit_spe.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/31 20:49:30 by wescande #+# #+# */ +/* Updated: 2017/01/31 23:15:41 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +static int ft_strlen_c(const char *str, const char *fix, + const unsigned char *esc, char c) +{ + int len; + int nb; + + len = 0; + nb = 0; + while (*str && (*str != c || nb)) + { + nb += *str == '{' && !is_char_esc(esc, fix, str); + nb -= *str == '}' && !is_char_esc(esc, fix, str); + ++str; + ++len; + } + return (len); +} + +static int ft_nbstr_c(const char *str, const unsigned char *esc, char c) +{ + int len; + int nb; + const char *fix; + + len = 0; + nb = 0; + fix = str; + while (*str) + { + while (*str && *str == c) + ++str; + if (*str) + ++len; + while (*str && (*str != c || nb)) + { + nb += *str == '{' && !is_char_esc(esc, fix, str); + nb -= *str == '}' && !is_char_esc(esc, fix, str); + ++str; + } + } + return (len); +} + +char **ft_strsplit_spe(const char *str, + const unsigned char *esc, char c) +{ + char **s1; + int i; + const char *fix; + int len; + + if (!str) + return (NULL); + if (!(s1 = (char**)malloc(sizeof(*s1) * (ft_nbstr_c(str, esc, c) + 1)))) + return (NULL); + i = 0; + fix = str; + while (*str && *str == c) + ++str; + while (*str && (len = ft_strlen_c(str, fix, esc, c))) + { + if (!(s1[i] = (char*)malloc(sizeof(**s1) * (len + 1)))) + return (NULL); + ft_strncpy(s1[i], str, len); + str = str + len; + ++i; + while (*str && *str == c) + ++str; + } + s1[i] = 0; + return (s1); +} diff --git a/42sh/src/glob/glob.c b/42sh/src/glob/glob.c index d56b6293..4e294d5a 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/31 19:40:49 by wescande ### ########.fr */ +/* Updated: 2017/01/31 23:15:27 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,7 +28,6 @@ static char **treat_glob(t_ld **match) 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); @@ -50,7 +49,8 @@ char **glob(const char *pat, const unsigned char *esc) else ret = dir_research(&gl, "/", gl.pat + 1); if (!ret) - ft_ld_pushfront(&gl.match, ft_strdup(gl.pat)); + ft_ld_pushfront(&gl.match, + ft_strdup(((char **)gl.m_pat->content)[0])); gl.m_pat = gl.m_pat->next; } ft_ld_clear(&gl.m_pat, &ft_tabdel); diff --git a/42sh/src/glob/lib_perso/ft_tablen.c b/42sh/src/glob/lib_perso/ft_tablen.c new file mode 100644 index 00000000..3efbee0b --- /dev/null +++ b/42sh/src/glob/lib_perso/ft_tablen.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_tablen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/28 16:54:07 by wescande #+# #+# */ +/* Updated: 2016/12/28 16:55:46 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_tablen(char **mytab) +{ + int i; + + if (!mytab || !*mytab) + return (0); + i = 0; + while (mytab[i]) + { + ++i; + } + return (i); +}