finalisation du lexer avec les char echappé -> 1 unsigned char pour 8 char du token (1 bit pour 1 char). Module glob en place en tenant compte des echappé. fonction is_char_esc en place pour une utilisation plus globale
This commit is contained in:
parent
45bc317f97
commit
4f5c65b9a2
13 changed files with 218 additions and 155 deletions
|
|
@ -59,6 +59,7 @@ glob/dir_glob.c\
|
||||||
glob/expand_brace.c\
|
glob/expand_brace.c\
|
||||||
glob/glob.c\
|
glob/glob.c\
|
||||||
glob/glob_print.c\
|
glob/glob_print.c\
|
||||||
|
glob/is_char_esc.c\
|
||||||
glob/lib_perso/ft_ld_back.c\
|
glob/lib_perso/ft_ld_back.c\
|
||||||
glob/lib_perso/ft_ld_clear.c\
|
glob/lib_perso/ft_ld_clear.c\
|
||||||
glob/lib_perso/ft_ld_del.c\
|
glob/lib_perso/ft_ld_del.c\
|
||||||
|
|
@ -188,7 +189,8 @@ parser/parse_word.c
|
||||||
SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE))
|
SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE))
|
||||||
OBJS = $(addprefix $(OBJ_DIR), $(SRC_BASE:.c=.o))
|
OBJS = $(addprefix $(OBJ_DIR), $(SRC_BASE:.c=.o))
|
||||||
|
|
||||||
all : $(NAME)
|
all :
|
||||||
|
@make -j $(NAME)
|
||||||
|
|
||||||
$(NAME): $(LIBFT_LIB) $(OBJ_DIR) $(OBJS)
|
$(NAME): $(LIBFT_LIB) $(OBJ_DIR) $(OBJS)
|
||||||
@$(CC) $(FLAGS) $(D_FLAGS) \
|
@$(CC) $(FLAGS) $(D_FLAGS) \
|
||||||
|
|
|
||||||
|
|
@ -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/27 19:13:15 by wescande ### ########.fr */
|
/* Updated: 2017/01/28 00:14:11 by wescande ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -15,6 +15,9 @@
|
||||||
|
|
||||||
# include "minishell.h"
|
# include "minishell.h"
|
||||||
|
|
||||||
|
# define CH(x) ((char **)(x)->content)
|
||||||
|
# define UCH(x) ((unsigned char **)(x)->content)
|
||||||
|
|
||||||
typedef struct s_ld
|
typedef struct s_ld
|
||||||
{
|
{
|
||||||
void *content;
|
void *content;
|
||||||
|
|
@ -24,21 +27,30 @@ typedef struct s_ld
|
||||||
|
|
||||||
typedef struct s_glob
|
typedef struct s_glob
|
||||||
{
|
{
|
||||||
const char *pat;
|
const char *pat;
|
||||||
const char *esc;
|
const unsigned char *esc;
|
||||||
t_ld *match;
|
t_ld *match;
|
||||||
t_ld *m_pat;
|
t_ld *m_pat;
|
||||||
} t_glob;
|
} t_glob;
|
||||||
|
|
||||||
char **glob(const char *str, const char *esc, char **env);
|
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 expand_brace(t_glob *tglob);
|
||||||
void glob_print(t_list *token, t_data *data);
|
void glob_print(t_list *token, t_data *data);
|
||||||
int match_pattern(t_glob *tglob, char *str, char *full_word);
|
int match_pattern(t_glob *tglob, char *str, char *full_word);
|
||||||
void dir_research(t_glob *tglob, char *path);
|
void dir_research(t_glob *tglob, char *p, const char *pat);
|
||||||
void dir_research_recursive(t_glob *tglob, char *p);
|
void dir_research_recursive(t_glob *tglob, char *p, const char *pat);
|
||||||
int is_directory(const char *path);
|
int is_directory(const char *path);
|
||||||
|
|
||||||
int is_char_esc(const char *esc, const char *ini_str, const char *str_pos);
|
int is_char_esc(const unsigned char *esc, const char *ini_str, const char *str_pos);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** LIST D:
|
** LIST D:
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */
|
/* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */
|
||||||
/* Updated: 2017/01/27 15:52:11 by wescande ### ########.fr */
|
/* Updated: 2017/01/27 21:47:44 by wescande ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -61,10 +61,10 @@ enum e_lexstate
|
||||||
|
|
||||||
struct s_token
|
struct s_token
|
||||||
{
|
{
|
||||||
t_type type;
|
t_type type;
|
||||||
char *data;
|
char *data;
|
||||||
char *esc;
|
unsigned char *esc;
|
||||||
int size;
|
int size;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct s_data t_data;
|
typedef struct s_data t_data;
|
||||||
|
|
|
||||||
|
|
@ -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/27 18:32:54 by wescande ### ########.fr */
|
/* Updated: 2017/01/27 23:48:18 by wescande ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -20,7 +20,7 @@ int is_directory(const char *path)
|
||||||
return (S_ISDIR(path_stat.st_mode));
|
return (S_ISDIR(path_stat.st_mode));
|
||||||
}
|
}
|
||||||
|
|
||||||
void dir_research(t_glob *gl, char *p)
|
void dir_research(t_glob *gl, char *p, const char *pat)
|
||||||
{
|
{
|
||||||
DIR *dir;
|
DIR *dir;
|
||||||
struct dirent *in;
|
struct dirent *in;
|
||||||
|
|
@ -38,6 +38,7 @@ void dir_research(t_glob *gl, char *p)
|
||||||
path_tmp = ft_strjoin(p, in->d_name);
|
path_tmp = ft_strjoin(p, in->d_name);
|
||||||
else
|
else
|
||||||
path_tmp = ft_strjoinf(ft_strjoin(p, "/"), in->d_name, 1);
|
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))
|
||||||
ft_ld_pushfront(&gl->match, ft_strdup(path_tmp + 2 *
|
ft_ld_pushfront(&gl->match, ft_strdup(path_tmp + 2 *
|
||||||
(path_tmp[0] == '.' && path_tmp[1] == '/')));
|
(path_tmp[0] == '.' && path_tmp[1] == '/')));
|
||||||
|
|
@ -47,13 +48,13 @@ void dir_research(t_glob *gl, char *p)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void dir_research_recursive(t_glob *gl, char *p)
|
void dir_research_recursive(t_glob *gl, char *p, const char *pat)
|
||||||
{
|
{
|
||||||
DIR *dir;
|
DIR *dir;
|
||||||
struct dirent *in;
|
struct dirent *in;
|
||||||
char *path_tmp;
|
char *path_tmp;
|
||||||
|
|
||||||
if (ft_strlen(p) <= 1 || p[ft_strlen(p) - 1] != '.')
|
if ((ft_strlen(p) <= 1 || p[ft_strlen(p) - 1] != '.') && is_directory(p))
|
||||||
{
|
{
|
||||||
if (!(dir = opendir(p)))
|
if (!(dir = opendir(p)))
|
||||||
return ;
|
return ;
|
||||||
|
|
@ -65,8 +66,8 @@ void dir_research_recursive(t_glob *gl, char *p)
|
||||||
path_tmp = ft_strjoin(p, in->d_name);
|
path_tmp = ft_strjoin(p, in->d_name);
|
||||||
else
|
else
|
||||||
path_tmp = ft_strjoinf(ft_strjoin(p, "/"), in->d_name, 1);
|
path_tmp = ft_strjoinf(ft_strjoin(p, "/"), in->d_name, 1);
|
||||||
if (is_directory(path_tmp))
|
dir_research_recursive(gl, path_tmp, pat);
|
||||||
dir_research_recursive(gl, path_tmp);
|
gl->pat = pat;
|
||||||
if (match_pattern(gl, in->d_name, path_tmp))
|
if (match_pattern(gl, in->d_name, path_tmp))
|
||||||
ft_ld_pushfront(&gl->match, ft_strdup(path_tmp + 2 *
|
ft_ld_pushfront(&gl->match, ft_strdup(path_tmp + 2 *
|
||||||
(path_tmp[0] == '.' && path_tmp[1] == '/')));
|
(path_tmp[0] == '.' && path_tmp[1] == '/')));
|
||||||
|
|
|
||||||
|
|
@ -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/27 20:12:12 by wescande ### ########.fr */
|
/* Updated: 2017/01/28 01:13:26 by wescande ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -20,16 +20,17 @@
|
||||||
** -char *pat -> pattern string to be looking for expand
|
** -char *pat -> pattern string to be looking for expand
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static char **generate_tab(const char *pat, const char *esc, int dup)
|
static char **gen_tab(const char *pat,
|
||||||
|
const unsigned char *esc, int dup)
|
||||||
{
|
{
|
||||||
char **my_tab;
|
char **my_tab;
|
||||||
|
|
||||||
if (!(my_tab = (char **)malloc(sizeof(char*) * 3)))
|
if (!(my_tab = (char **)malloc(sizeof(char *) * 3)))
|
||||||
return (NULL);
|
return (NULL);
|
||||||
if (dup)
|
if (dup)
|
||||||
{
|
{
|
||||||
my_tab[0] = ft_strdup(pat);
|
my_tab[0] = ft_strdup(pat);
|
||||||
my_tab[1] = ft_strdup(esc);
|
my_tab[1] = ft_strdup((const char *)esc);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
@ -40,66 +41,104 @@ static char **generate_tab(const char *pat, const char *esc, int dup)
|
||||||
return (my_tab);
|
return (my_tab);
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *calc_expand_esc(const char *esc,
|
static unsigned char *calc_expand_esc(const unsigned char *esc,
|
||||||
int nb_start, int nb_middle, int nb_end)
|
int nb_start, int nb_middle, int *nb_end)
|
||||||
{
|
{
|
||||||
char *new_esc;
|
unsigned char *new_esc;
|
||||||
int index;
|
int index;
|
||||||
|
int pos;
|
||||||
|
|
||||||
new_esc = ft_memalloc(sizeof(char) * (nb_start + nb_middle + nb_end) / 8);
|
if (!(new_esc = ft_memalloc(sizeof(char) *
|
||||||
|
((nb_start + nb_middle + nb_end[1]) / 8) + 1)))
|
||||||
|
return (NULL);
|
||||||
index = -1;
|
index = -1;
|
||||||
while (++index < nb_start)
|
while (++index < nb_start)
|
||||||
new_esc[index / 8] |= (esc[index / 8] >> index % 8) & 1 << index % 8;
|
new_esc[index / 8] |=
|
||||||
//copy the nb_start first bit of esc.
|
((esc[index / 8] >> (7 - index % 8)) & 1) << (7 - index % 8);
|
||||||
//append nb_middle bit at status 0
|
pos = -1;
|
||||||
//append nb_end last bit from end of esc.
|
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);
|
return (new_esc);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int search_brace(t_ld **wk, char *str, char *esc, int index)
|
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;
|
char *start;
|
||||||
char *s1;
|
|
||||||
char **split;
|
|
||||||
|
|
||||||
start = NULL;
|
start = NULL;
|
||||||
while (*str)
|
while (*me->str)
|
||||||
{
|
{
|
||||||
start = *str == '{' ? str : start;
|
start = *me->str == '{' && !is_char_esc(me->esc,
|
||||||
if (*str == '}' && start)
|
CH(*me->wk)[0], me->str) ? me->str : start;
|
||||||
|
if (*me->str == '}' && start
|
||||||
|
&& !is_char_esc(me->esc, CH(*me->wk)[0], me->str))
|
||||||
{
|
{
|
||||||
s1 = ft_strsub(start, 1, str - start - 1);
|
me->s1 = ft_strsub(start, 1, me->str - start - 1);
|
||||||
split = ft_strsplit(s1, ',');
|
me->split = ft_strsplit(me->s1, ',');
|
||||||
ft_strdel(&s1);
|
ft_strdel(&me->s1);
|
||||||
s1 = ft_strsub((*wk)->content, 0, start - (char *)(*wk)->content);
|
me->s1 = ft_strsub(CH(*me->wk)[0], 0, start - CH(*me->wk)[0]);
|
||||||
while (split[++index])
|
iter_on_each(me);
|
||||||
ft_ld_pushfront(wk, generate_tab(ft_strjoinf(ft_strjoin(s1,
|
ft_strdel(&me->s1);
|
||||||
split[index]), str + 1, 1), calc_expand_esc(esc,
|
ft_tabdel(&me->split);
|
||||||
ft_strlen(s1), ft_strlen(split[index]), ft_strlen(str +1)), 0));
|
|
||||||
ft_strdel(&s1);
|
|
||||||
ft_tabdel(&split);
|
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
++str;
|
++me->str;
|
||||||
}
|
}
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void expand_brace(t_glob *gl)
|
void expand_brace(t_glob *gl)
|
||||||
{
|
{
|
||||||
t_ld *tmp;
|
t_ld *tmp;
|
||||||
int do_it;
|
int do_it;
|
||||||
|
t_expand me;
|
||||||
|
|
||||||
ft_ld_pushfront(&gl->m_pat, generate_tab("", "", 1));
|
ft_ld_pushfront(&gl->m_pat, gen_tab("", (const unsigned char *)"", 1));
|
||||||
ft_ld_pushfront(&gl->m_pat, generate_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};
|
||||||
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)
|
||||||
{
|
{
|
||||||
if ((tmp = gl->m_pat) && search_brace(&gl->m_pat,
|
me.wk = &gl->m_pat;
|
||||||
((char **)gl->m_pat->content)[0],((char **)gl->m_pat->content)[1], -1))
|
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);
|
ft_ld_del(&tmp, &ft_tabdel);
|
||||||
do_it = 1;
|
do_it = 1;
|
||||||
|
|
|
||||||
|
|
@ -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/27 19:05:38 by wescande ### ########.fr */
|
/* Updated: 2017/01/28 01:17:04 by wescande ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -21,13 +21,13 @@
|
||||||
** to just expanse in local directory and not in path
|
** to just expanse in local directory and not in path
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static void path_research(t_glob *tglob, char **path)
|
static void path_research(t_glob *gl, char **path)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
i = -1;
|
i = -1;
|
||||||
while (path[++i])
|
while (path[++i])
|
||||||
dir_research(tglob, path[i]);
|
dir_research(gl, path[i], gl->pat);
|
||||||
}
|
}
|
||||||
|
|
||||||
static char **treat_glob(t_ld **match)
|
static char **treat_glob(t_ld **match)
|
||||||
|
|
@ -47,49 +47,77 @@ static void add_simple_pat(t_glob *gl)
|
||||||
char *str;
|
char *str;
|
||||||
int start;
|
int start;
|
||||||
|
|
||||||
str = ((char **)gl->m_pat->content)[0];
|
str = (char *)gl->pat;
|
||||||
start = 0;
|
start = 0;
|
||||||
while (*str)
|
while (*str)
|
||||||
{
|
{
|
||||||
if (!is_char_esc(((char **)gl->m_pat->content)[1],
|
if (!is_char_esc(gl->esc, gl->pat, str))
|
||||||
((char **)gl->m_pat->content)[0], str))
|
|
||||||
{
|
{
|
||||||
if (*str == '[')
|
if (*str == '[')
|
||||||
start = 1;
|
start = 1;
|
||||||
else if (*str == ']' && start == 1)
|
else if (*str == ']' && start == 1)
|
||||||
return ;
|
return ;
|
||||||
else if (*str == '*' || *str == '?')
|
else if (*str == '*' || *str == '?')
|
||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
++str;
|
++str;
|
||||||
}
|
}
|
||||||
ft_ld_pushfront(&gl->match, ((char **)gl->m_pat->content)[0]);
|
ft_ld_pushfront(&gl->match, ft_strdup(gl->pat));
|
||||||
}
|
}
|
||||||
|
|
||||||
char **glob(const char *pat, const char *esc, char **env)
|
static void printme_me(const char *pat, const unsigned char *esc)
|
||||||
{
|
{
|
||||||
t_glob tglob;
|
char *str;
|
||||||
|
int index;
|
||||||
|
int len;
|
||||||
|
|
||||||
|
str = (char *)pat;
|
||||||
|
while (*str)
|
||||||
|
{
|
||||||
|
ft_dprintf(3, is_char_esc(esc, pat, str) ? "\\%c" : "%c", *str);
|
||||||
|
++str;
|
||||||
|
}
|
||||||
|
ft_dprintf(3, "\n");
|
||||||
|
len = ft_strlen(pat);
|
||||||
|
index = -1;
|
||||||
|
while (++index < len)
|
||||||
|
ft_dprintf(3, ((esc[index / 8] >> (7 - index % 8)) & 1) ? " 1" : "0");
|
||||||
|
ft_dprintf(3, "\n");
|
||||||
|
index = 0;
|
||||||
|
while (index < len)
|
||||||
|
{
|
||||||
|
ft_dprintf(3, "%08b", esc[index / 8]);
|
||||||
|
index += 8;
|
||||||
|
}
|
||||||
|
ft_dprintf(3, "\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
char **glob(const char *pat, const unsigned char *esc, char **env)
|
||||||
|
{
|
||||||
|
t_glob gl;
|
||||||
char **path;
|
char **path;
|
||||||
|
|
||||||
tglob = (t_glob){pat, esc, NULL, NULL};
|
gl = (t_glob){pat, esc, NULL, NULL};
|
||||||
expand_brace(&tglob, tglob.pat);
|
expand_brace(&gl);
|
||||||
while (tglob.m_pat->next)
|
while (gl.m_pat->next)
|
||||||
{
|
{
|
||||||
add_simple_pat(&tglob);
|
gl.pat = ((char **)gl.m_pat->content)[0];
|
||||||
if (!(((char **)tglob.m_pat->content)[0][0] == '/'
|
gl.esc = ((unsigned char **)gl.m_pat->content)[1];
|
||||||
|| (((char **)tglob.m_pat->content)[0][0] == '.'
|
printme_me(gl.pat, gl.esc);
|
||||||
&& ((char **)tglob.m_pat->content)[0][1] == '/'))
|
add_simple_pat(&gl);
|
||||||
&& env && (path = ft_strsplit(ft_getenv(env, "PATH"), ':')))
|
if (!(gl.pat[0] == '/' || (gl.pat[0] == '.' && gl.pat[1] == '/'))
|
||||||
|
&& env && (path = ft_strsplit(ft_getenv(env, "PATH"), ':')))
|
||||||
{
|
{
|
||||||
path_research(&tglob, path);
|
path_research(&gl, path);
|
||||||
ft_tabdel(&path);
|
ft_tabdel(&path);
|
||||||
}
|
}
|
||||||
if (((char **)tglob.m_pat->content)[0][0] != '/')
|
gl.pat = ((char **)gl.m_pat->content)[0];
|
||||||
dir_research(&tglob, ".");
|
if (gl.pat[0] != '/')
|
||||||
|
dir_research(&gl, ".", gl.pat);
|
||||||
else
|
else
|
||||||
dir_research(&tglob, "/");
|
dir_research(&gl, "/", gl.pat + 1);
|
||||||
tglob.m_pat = tglob.m_pat->next;
|
gl.m_pat = gl.m_pat->next;
|
||||||
}
|
}
|
||||||
ft_ld_clear(&tglob.m_pat, &ft_strdel);
|
ft_ld_clear(&gl.m_pat, &ft_tabdel);
|
||||||
return (treat_glob(&tglob.match));
|
return (treat_glob(&gl.match));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
|
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/01/05 16:09:40 by wescande #+# #+# */
|
/* Created: 2017/01/05 16:09:40 by wescande #+# #+# */
|
||||||
/* Updated: 2017/01/27 18:29:32 by wescande ### ########.fr */
|
/* Updated: 2017/01/27 21:57:24 by wescande ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -26,20 +26,6 @@ void ft_tabprint_fd(char **mytab, int fd)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_esc(t_token *token)
|
|
||||||
{
|
|
||||||
char *str = token->data;
|
|
||||||
while (*str)
|
|
||||||
{
|
|
||||||
if (is_char_esc(token->esc, token->data, str))
|
|
||||||
ft_dprintf(3, "\\%c", *str);
|
|
||||||
else
|
|
||||||
ft_dprintf(3, "%c", *str);
|
|
||||||
++str;
|
|
||||||
}
|
|
||||||
ft_dprintf(3, "\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
void glob_print(t_list *lst, t_data *data)
|
void glob_print(t_list *lst, t_data *data)
|
||||||
{
|
{
|
||||||
t_token *token;
|
t_token *token;
|
||||||
|
|
@ -55,7 +41,6 @@ void glob_print(t_list *lst, t_data *data)
|
||||||
while (type >> (i++ + 2))
|
while (type >> (i++ + 2))
|
||||||
;
|
;
|
||||||
glob_ret = glob(token->data, token->esc, data->env);
|
glob_ret = glob(token->data, token->esc, data->env);
|
||||||
print_esc(token);
|
|
||||||
DG("%02i '%s'", i, token->data);
|
DG("%02i '%s'", i, token->data);
|
||||||
ft_tabprint_fd(glob_ret, 3);
|
ft_tabprint_fd(glob_ret, 3);
|
||||||
lst = lst->next;
|
lst = lst->next;
|
||||||
|
|
|
||||||
|
|
@ -6,18 +6,19 @@
|
||||||
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
|
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/01/27 18:19:55 by wescande #+# #+# */
|
/* Created: 2017/01/27 18:19:55 by wescande #+# #+# */
|
||||||
/* Updated: 2017/01/27 18:23:22 by wescande ### ########.fr */
|
/* Updated: 2017/01/27 23:45:51 by wescande ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "glob.h"
|
#include "glob.h"
|
||||||
|
|
||||||
int is_char_esc(const char *esc, const char *ini_str, const char *str_pos)
|
int is_char_esc(const unsigned char *esc,
|
||||||
|
const char *ini_str, const char *str_pos)
|
||||||
{
|
{
|
||||||
int pos;
|
int pos;
|
||||||
|
|
||||||
pos = str_pos - ini_str;
|
pos = str_pos - ini_str;
|
||||||
if ((esc[pos / 8] >> pos % 8) & 1)
|
if ((esc[pos / 8] >> (7 - pos % 8)) & 1)
|
||||||
return (1);
|
return (1);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
|
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/01/24 17:30:23 by wescande #+# #+# */
|
/* Created: 2017/01/24 17:30:23 by wescande #+# #+# */
|
||||||
/* Updated: 2017/01/24 20:58:47 by wescande ### ########.fr */
|
/* Updated: 2017/01/27 23:45:39 by wescande ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -31,35 +31,34 @@ static int match_bracket_char(char **cmp, const char *pat, char c, int neg)
|
||||||
*cmp += 2;
|
*cmp += 2;
|
||||||
}
|
}
|
||||||
else if (!neg && **cmp == c)
|
else if (!neg && **cmp == c)
|
||||||
{
|
|
||||||
return (1);
|
return (1);
|
||||||
}
|
|
||||||
else if (neg && **cmp == c)
|
else if (neg && **cmp == c)
|
||||||
{
|
|
||||||
return (0);
|
return (0);
|
||||||
}
|
|
||||||
return (-1);
|
return (-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int match_bracket(const char **pat, char c)
|
static int match_bracket(t_glob *gl, char c)
|
||||||
{
|
{
|
||||||
char *cmp;
|
char *cmp;
|
||||||
int neg;
|
int neg;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
cmp = (char *)*pat + 1;
|
cmp = (char *)gl->pat + 1;
|
||||||
while (**pat != ']')
|
while (*gl->pat != ']'
|
||||||
|
|| is_char_esc(gl->esc, ((char **)gl->m_pat->content)[0], gl->pat))
|
||||||
{
|
{
|
||||||
if (!**pat)
|
if (!*gl->pat)
|
||||||
return (0);
|
return (0);
|
||||||
++*pat;
|
++gl->pat;
|
||||||
}
|
}
|
||||||
neg = 0;
|
neg = 0;
|
||||||
if ((*cmp == '^' || *cmp == '!') && ++neg)
|
if ((*cmp == '^' || *cmp == '!')
|
||||||
|
&& !is_char_esc(gl->esc, ((char **)gl->m_pat->content)[0], cmp)
|
||||||
|
&& ++neg)
|
||||||
++cmp;
|
++cmp;
|
||||||
while (cmp < *pat)
|
while (cmp < gl->pat)
|
||||||
{
|
{
|
||||||
ret = match_bracket_char(&cmp, *pat, c, neg);
|
ret = match_bracket_char(&cmp, gl->pat, c, neg);
|
||||||
if (ret != -1)
|
if (ret != -1)
|
||||||
return (ret);
|
return (ret);
|
||||||
++cmp;
|
++cmp;
|
||||||
|
|
@ -67,19 +66,19 @@ static int match_bracket(const char **pat, char c)
|
||||||
return (neg);
|
return (neg);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int match_star(const char *pat, char *str,
|
static int match_star(t_glob *gl, char *str, char *full_word)
|
||||||
char *full_word, t_ld **match)
|
|
||||||
{
|
{
|
||||||
char *fix;
|
char *fix;
|
||||||
|
|
||||||
if (pat[1] == '*')
|
if (gl->pat[1] == '*' &&
|
||||||
dir_research_recursive(pat, full_word, match);
|
!is_char_esc(gl->esc, ((char **)gl->m_pat->content)[0], gl->pat + 1))
|
||||||
if (!pat[1])
|
dir_research_recursive(gl, full_word, gl->pat + 1);
|
||||||
|
if (!*++gl->pat)
|
||||||
return (1);
|
return (1);
|
||||||
fix = str + ft_strlen(str);
|
fix = str + ft_strlen(str);
|
||||||
while (fix > str)
|
while (fix > str)
|
||||||
{
|
{
|
||||||
if (match_pattern(pat + 1, fix, full_word, match))
|
if (match_pattern(gl, fix, full_word))
|
||||||
return (1);
|
return (1);
|
||||||
--fix;
|
--fix;
|
||||||
}
|
}
|
||||||
|
|
@ -94,32 +93,31 @@ const char *manage_pat(const char *pat, char *str)
|
||||||
return (pat);
|
return (pat);
|
||||||
}
|
}
|
||||||
|
|
||||||
int match_pattern(const char *pat, char *str,
|
int match_pattern(t_glob *gl, char *str, char *full_word)
|
||||||
char *full_word, t_ld **match)
|
|
||||||
{
|
{
|
||||||
pat = manage_pat(pat, str);
|
gl->pat = manage_pat(gl->pat, str);
|
||||||
while (*pat)
|
while (*gl->pat)
|
||||||
{
|
{
|
||||||
if (*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++;
|
str++;
|
||||||
else if (*pat == '[')
|
else if (*gl->pat == '[')
|
||||||
{
|
{
|
||||||
if (!match_bracket(&pat, *str))
|
if (!match_bracket(gl, *str))
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
else if (*pat == '*')
|
else if (*gl->pat == '*')
|
||||||
return (match_star(pat, str, full_word, match));
|
return (match_star(gl, str, full_word));
|
||||||
else if (*pat == '\\')
|
else if (*gl->pat == '/' && !*str && is_directory(full_word))
|
||||||
{
|
dir_research(gl, full_word, gl->pat + 1);
|
||||||
if (!*++pat || *str != *pat)
|
else if (*gl->pat != *str)
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
else if (*pat == '/' && !*str && is_directory(full_word))
|
|
||||||
dir_research((pat + 1), full_word, match);
|
|
||||||
else if (*pat != *str)
|
|
||||||
return (0);
|
return (0);
|
||||||
++str;
|
++str;
|
||||||
++pat;
|
++gl->pat;
|
||||||
}
|
}
|
||||||
return (*str ? 0 : 1);
|
return (*str ? 0 : 1);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/11/11 17:18:42 by jhalford #+# #+# */
|
/* Created: 2016/11/11 17:18:42 by jhalford #+# #+# */
|
||||||
/* Updated: 2017/01/27 20:08:12 by wescande ### ########.fr */
|
/* Updated: 2017/01/27 21:54:32 by wescande ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -21,11 +21,11 @@ int token_append(t_token *token, char c, short int esc)
|
||||||
{
|
{
|
||||||
token->size += 8;
|
token->size += 8;
|
||||||
token->data = (char *)ft_realloc(token->data, token->size + 1);
|
token->data = (char *)ft_realloc(token->data, token->size + 1);
|
||||||
token->esc = (char *)ft_realloc(token->esc, token->size / 8);
|
token->esc = (unsigned char *)ft_realloc((char *)token->esc, token->size / 8 + 1);
|
||||||
token->esc[token->size / 8 - 1] = 0;
|
token->esc[token->size / 8 - 1] = 0;
|
||||||
}
|
}
|
||||||
ft_strcat(token->data, (char[2]){c, '\0'});
|
ft_strcat(token->data, (char[2]){c, '\0'});
|
||||||
if (esc)
|
if (esc)
|
||||||
token->esc[len / 8] |= 1 << len % 8;
|
token->esc[len / 8] |= 1 << (7 - len % 8);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/12/03 12:07:30 by jhalford #+# #+# */
|
/* Created: 2016/12/03 12:07:30 by jhalford #+# #+# */
|
||||||
/* Updated: 2017/01/27 15:35:37 by wescande ### ########.fr */
|
/* Updated: 2017/01/27 21:54:05 by wescande ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -21,7 +21,7 @@ void token_free(void *data, size_t size)
|
||||||
if (!(token->type & TK_NON_FREEABLE))
|
if (!(token->type & TK_NON_FREEABLE))
|
||||||
{
|
{
|
||||||
ft_strdel(&token->data);
|
ft_strdel(&token->data);
|
||||||
ft_strdel(&token->esc);
|
ft_memdel((void **)&token->esc);
|
||||||
}
|
}
|
||||||
free(token);
|
free(token);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/11/10 15:30:25 by jhalford #+# #+# */
|
/* Created: 2016/11/10 15:30:25 by jhalford #+# #+# */
|
||||||
/* Updated: 2017/01/27 15:35:04 by wescande ### ########.fr */
|
/* Updated: 2017/01/27 21:54:53 by wescande ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -20,6 +20,6 @@ t_token *token_init(void)
|
||||||
token->type = 0;
|
token->type = 0;
|
||||||
token->size = 8;
|
token->size = 8;
|
||||||
token->data = ft_strnew(token->size + 1);
|
token->data = ft_strnew(token->size + 1);
|
||||||
token->esc = ft_strnew(token->size / 8);
|
token->esc = (unsigned char *)ft_strnew(token->size / 8 + 1);
|
||||||
return (token);
|
return (token);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/11/28 14:39:01 by jhalford #+# #+# */
|
/* Created: 2016/11/28 14:39:01 by jhalford #+# #+# */
|
||||||
/* Updated: 2017/01/27 16:08:16 by wescande ### ########.fr */
|
/* Updated: 2017/01/27 21:57:05 by wescande ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -28,9 +28,6 @@ void token_print(t_list *lst)
|
||||||
;
|
;
|
||||||
DG("%02i '%s'", i, token->data);
|
DG("%02i '%s'", i, token->data);
|
||||||
index = -1;
|
index = -1;
|
||||||
while (++index < token->size / 8)
|
|
||||||
ft_dprintf(3, "|%b vs %x vs %c| ", token->esc[index], token->esc[index], token->esc[index]);
|
|
||||||
ft_dprintf(3, "\n");
|
|
||||||
lst = lst->next;
|
lst = lst->next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue