finition sur le globbing (expansion des accolades, gestion des characteres echappe) => normalement aucun bug + mise a la norme

This commit is contained in:
wescande 2017-01-31 23:25:41 +01:00
parent 3c8c143143
commit 43f1295324
9 changed files with 331 additions and 75 deletions

View file

@ -16,8 +16,7 @@ CC = gcc
FLAGS = -Wall -Wextra -Werror FLAGS = -Wall -Wextra -Werror
D_FLAGS = -g D_FLAGS = -g
LEN_NAME = `printf "%s" $(NAME) |wc -c` DELTA = $$(echo "$$(tput cols)-47"|bc)
DELTA = $$(echo "$$(tput cols)-37-$(LEN_NAME)"|bc)
LIBFT_DIR = libft/ LIBFT_DIR = libft/
LIBFT_LIB = $(LIBFT_DIR)libft.a LIBFT_LIB = $(LIBFT_DIR)libft.a
@ -61,6 +60,9 @@ exec/process_setgroup.c\
exec/set_exitstatus.c\ exec/set_exitstatus.c\
glob/dir_glob.c\ glob/dir_glob.c\
glob/expand_brace.c\ glob/expand_brace.c\
glob/expand_esc.c\
glob/ft_strsplit_esc.c\
glob/ft_strsplit_spe.c\
glob/glob.c\ glob/glob.c\
glob/is_char_esc.c\ glob/is_char_esc.c\
glob/lib_perso/ft_ld_back.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_ld_to_tab.c\
glob/lib_perso/ft_strjoinf.c\ glob/lib_perso/ft_strjoinf.c\
glob/lib_perso/ft_tabdel.c\ glob/lib_perso/ft_tabdel.c\
glob/lib_perso/ft_tablen.c\
glob/match_pattern.c\ glob/match_pattern.c\
job-control/builtin_bg.c\ job-control/builtin_bg.c\
job-control/builtin_fg.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 PERCENT=$(shell echo $$(($(INDEX)*100/$(NB)))))
@$(eval COLOR=$(shell echo $$(($(PERCENT)%35+196)))) @$(eval COLOR=$(shell echo $$(($(PERCENT)%35+196))))
@$(eval TO_DO=$(shell echo $$((20-$(INDEX)*20/$(NB))))) @$(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 $@\ @$(CC) $(FLAGS) -MMD -c $< -o $@\
-I $(INC_DIR)\ -I $(INC_DIR)\
-I $(LIBFT_INC) -I $(LIBFT_INC)

View file

@ -6,7 +6,7 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */ /* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/04 16:31:18 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; char *str;
unsigned char *esc; unsigned char *esc;
char **split; char **split;
unsigned char **m_esc;
char *s1; char *s1;
} t_expand; } t_expand;
@ -61,10 +62,19 @@ int is_char_esc(const unsigned char *esc,
/* /*
** Internal function. ** 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); void expand_brace(t_glob *tglob);
int match_pattern(t_glob *tglob, char *str, char *full_word); 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(t_glob *tglob, char *p, const char *pat);
int dir_research_recursive(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: ** 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); char *ft_strjoinf(char *str, char *str2, int mode);
void ft_tabdel(char ***mytab); void ft_tabdel(char ***mytab);
int ft_tablen(char **mytab);
#endif #endif

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 17:28:14 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) while (ld)
{ {
content = ld->content; content = ld->content;
DG("EXPANSION DE :");
DG("%s", content[0]);
expand = glob(content[0], (unsigned char *)content[1]); expand = glob(content[0], (unsigned char *)content[1]);
DG();
index = -1; index = -1;
while (expand[++index]) while (expand[++index])
my_tab = ft_sstradd(my_tab, 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); 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) int exec_command(t_btree **ast)
{ {
t_astnode *node; t_astnode *node;
@ -62,7 +43,6 @@ int exec_command(t_btree **ast)
p = &data_singleton()->exec.process; p = &data_singleton()->exec.process;
job = &data_singleton()->exec.job; job = &data_singleton()->exec.job;
p->av = return_array_expand(node->data.token); p->av = return_array_expand(node->data.token);
ft_tabprint_fd(p->av, 4);
process_setexec(node->type, p); process_setexec(node->type, p);
if (!(launch_process(p))) if (!(launch_process(p)))
{ {

View file

@ -6,7 +6,7 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */ /* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/12 19:00:29 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[1] = (char *)esc;
} }
my_tab[2] = NULL; my_tab[2] = NULL;
return (my_tab); 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) static void iter_on_each(t_expand *me)
{ {
int i; int i;
@ -89,31 +58,50 @@ static void iter_on_each(t_expand *me)
ft_strlen(me->s1), ft_strlen(me->s1),
ft_strlen(me->split[i]), ft_strlen(me->split[i]),
(int[2]){me->str - CH(*me->wk)[0], ft_strlen(me->str + 1)}); (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); my_new = gen_tab(first, second, 0);
ft_ld_pushfront(&wk_tmp, my_new); ft_ld_pushfront(&wk_tmp, my_new);
} }
me->wk = &wk_tmp; 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) static int search_brace(t_expand *me)
{ {
char *start; char *start;
int nb;
start = NULL; start = NULL;
nb = 0;
while (*me->str) while (*me->str)
{ {
start = *me->str == '{' && !is_char_esc(me->esc, start = *me->str == '{'
CH(*me->wk)[0], me->str) ? me->str : start; && !is_char_esc(me->esc, CH(*me->wk)[0], me->str)
if (*me->str == '}' && start && nb == 0 ? me->str : start;
&& !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);
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); init_expand(me, start);
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); return (1);
} }
++me->str; ++me->str;
@ -127,15 +115,14 @@ void expand_brace(t_glob *gl)
int do_it; int do_it;
t_expand me; t_expand me;
ft_ld_pushfront(&gl->m_pat, gen_tab("", (const unsigned char *)"", 1)); 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)); 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; do_it = 1;
while (do_it) while (do_it)
{ {
do_it = 0; do_it = 0;
while (gl->m_pat->next) while (gl->m_pat->next && !do_it)
{ {
me.wk = &gl->m_pat; me.wk = &gl->m_pat;
me.esc = UCH(gl->m_pat)[1]; me.esc = UCH(gl->m_pat)[1];

View file

@ -0,0 +1,76 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* expand_esc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

@ -0,0 +1,86 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit_esc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

@ -0,0 +1,86 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit_spe.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

@ -6,7 +6,7 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */ /* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/04 16:29:54 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; gl = NULL;
ft_ld_reverse(match); ft_ld_reverse(match);
*match = ft_ld_order(*match, &ft_strcmp, &ft_strdel);
gl = ft_ld_to_tab(*match); gl = ft_ld_to_tab(*match);
ft_ld_clear(match, &ft_strdel); ft_ld_clear(match, &ft_strdel);
return (gl); return (gl);
@ -50,7 +49,8 @@ char **glob(const char *pat, const unsigned char *esc)
else else
ret = dir_research(&gl, "/", gl.pat + 1); ret = dir_research(&gl, "/", gl.pat + 1);
if (!ret) 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; gl.m_pat = gl.m_pat->next;
} }
ft_ld_clear(&gl.m_pat, &ft_tabdel); ft_ld_clear(&gl.m_pat, &ft_tabdel);

View file

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_tablen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}