Merge remote-tracking branch 'origin/glob_bquote'
This commit is contained in:
commit
4ea6fb5801
16 changed files with 255 additions and 191 deletions
|
|
@ -6,7 +6,7 @@
|
|||
# By: wescande <wescande@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2016/08/29 21:32:58 by wescande #+# #+# #
|
||||
# Updated: 2017/02/16 22:19:03 by alao ### ########.fr #
|
||||
# Updated: 2017/02/17 17:16:16 by wescande ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
|
|
@ -84,8 +84,10 @@ exec/redirect_greatand.c\
|
|||
exec/redirect_less.c\
|
||||
exec/redirect_lessand.c\
|
||||
exec/set_exitstatus.c\
|
||||
glob/command_getoutput.c\
|
||||
glob/dir_glob.c\
|
||||
glob/esc_print.c\
|
||||
glob/expand_bquote.c\
|
||||
glob/expand_brace.c\
|
||||
glob/expand_esc.c\
|
||||
glob/expand_var.c\
|
||||
|
|
@ -156,8 +158,6 @@ 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/expand_bquotes.c\
|
||||
lexer/get_lexer_stack.c\
|
||||
lexer/get_state_global.c\
|
||||
lexer/get_state_redir.c\
|
||||
|
|
@ -238,7 +238,7 @@ NB = $(words $(SRC_BASE))
|
|||
INDEX = 0
|
||||
|
||||
all :
|
||||
@make $(NAME)
|
||||
@make -j $(NAME)
|
||||
|
||||
$(NAME): $(LIBFT_LIB) $(OBJ_DIR) $(OBJS)
|
||||
@$(CC) $(FLAGS) $(D_FLAGS) \
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/01/04 16:31:18 by wescande #+# #+# */
|
||||
/* Updated: 2017/02/17 15:42:09 by jhalford ### ########.fr */
|
||||
/* Updated: 2017/02/20 19:03:45 by wescande ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -17,6 +17,8 @@
|
|||
|
||||
# define CH(x) ((char **)(x)->content)
|
||||
# define UCH(x) ((unsigned char **)(x)->content)
|
||||
# define SCH(x) ((char **)(x).content)
|
||||
# define SUCH(x) ((unsigned char **)(x).content)
|
||||
|
||||
typedef struct s_ld
|
||||
{
|
||||
|
|
@ -46,6 +48,16 @@ typedef struct s_expand
|
|||
char *s1;
|
||||
} t_expand;
|
||||
|
||||
typedef struct s_bquote
|
||||
{
|
||||
t_ld **wk;
|
||||
char *str;
|
||||
unsigned char *esc;
|
||||
unsigned char *esc2;
|
||||
char *s1;
|
||||
char *s2;
|
||||
char *mid;
|
||||
} t_bquote;
|
||||
/*
|
||||
** interface of glob.
|
||||
*/
|
||||
|
|
@ -82,9 +94,12 @@ unsigned char *calc_expand_esc(const unsigned char *esc,
|
|||
void modify_esc_split(unsigned char *esc_dest,
|
||||
unsigned char *esc_src, int start, int len);
|
||||
void expand_brace(t_glob *tglob);
|
||||
void expand_bquote(t_glob *gl);
|
||||
void expand_var(t_glob *tglob);
|
||||
int match_pattern(t_glob *tglob, char *str, char *full_word);
|
||||
int dir_research(t_glob *tglob, char *p, char *pat, int rec);
|
||||
char **gen_tab(const char *pat, const unsigned char *esc,
|
||||
const unsigned char *esc2, int dup);
|
||||
char **ft_strsplit_spe(const char *str,
|
||||
const unsigned char *esc, char c);
|
||||
unsigned char **ft_strsplit_esc(const char *str,
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
Subproject commit 0ca8ca817f32fc0345ef93ef74a3abe2583bd89c
|
||||
Subproject commit 9aa3049dd23424807316a6f027589a95cdf1fcdd
|
||||
|
|
@ -6,20 +6,20 @@
|
|||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/02/17 15:38:14 by jhalford #+# #+# */
|
||||
/* Updated: 2017/02/17 16:29:42 by jhalford ### ########.fr */
|
||||
/* Updated: 2017/02/20 18:49:02 by wescande ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "glob.h"
|
||||
|
||||
void esc_print(char *str, unsigned char *esc)
|
||||
void esc_print(char *str, unsigned char *esc)
|
||||
{
|
||||
char *cur;
|
||||
|
||||
cur = str;
|
||||
while (*cur)
|
||||
{
|
||||
if (is_char_esc(esc,str,cur))
|
||||
if (is_char_esc(esc, str, cur))
|
||||
printf("\\%c", *cur);
|
||||
else
|
||||
printf("%c", *cur);
|
||||
|
|
@ -27,4 +27,3 @@ void esc_print(char *str, unsigned char *esc)
|
|||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
|
|
|
|||
138
42sh/src/glob/expand_bquote.c
Normal file
138
42sh/src/glob/expand_bquote.c
Normal file
|
|
@ -0,0 +1,138 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* expand_bquote.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/02/17 17:47:53 by wescande #+# #+# */
|
||||
/* Updated: 2017/02/20 19:11:11 by wescande ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "glob.h"
|
||||
|
||||
static void expand_all_bquote(t_bquote *me, char *content,
|
||||
char *ifs)
|
||||
{
|
||||
char *content2;
|
||||
|
||||
while ((content2 = ft_strtok(NULL, ifs)))
|
||||
{
|
||||
ft_ld_pushfront(me->wk, gen_tab(ft_strdup(content),
|
||||
calc_expand_esc(me->esc, 0,
|
||||
(int[2]){ft_strlen(content), 1}, (int[2]){0, 0}),
|
||||
calc_expand_esc(me->esc2, 0,
|
||||
(int[2]){ft_strlen(content), 1}, (int[2]){0, 0}), 0));
|
||||
content = content2;
|
||||
}
|
||||
ft_ld_pushfront(me->wk, gen_tab(ft_strjoin(content, me->s2),
|
||||
calc_expand_esc(me->esc, 0,
|
||||
(int[2]){ft_strlen(content), 1},
|
||||
(int[2]){ft_strlen(me->s1) + ft_strlen(me->mid),
|
||||
ft_strlen(me->s2)}),
|
||||
calc_expand_esc(me->esc2, ft_strlen(me->s1),
|
||||
(int[2]){ft_strlen(content), 1},
|
||||
(int[2]){ft_strlen(me->s1) + ft_strlen(me->mid),
|
||||
ft_strlen(me->s2)}),
|
||||
0));
|
||||
}
|
||||
|
||||
static void init_expand(t_bquote *me, char *content, int esc)
|
||||
{
|
||||
char *ifs;
|
||||
char *content2;
|
||||
|
||||
ifs = esc ? NULL : ft_getenv(data_singleton()->env, "IFS");
|
||||
content = ft_strtok(content, ifs);
|
||||
if (!(content2 = ft_strtok(NULL, ifs)))
|
||||
ft_ld_pushfront(me->wk, gen_tab(ft_strjoinf(ft_strjoin(me->s1, content),
|
||||
me->s2, 1),
|
||||
calc_expand_esc(me->esc, ft_strlen(me->s1),
|
||||
(int[2]){ft_strlen(content), 1},
|
||||
(int[2]){ft_strlen(me->s1) + ft_strlen(me->mid),
|
||||
ft_strlen(me->s2)}),
|
||||
calc_expand_esc(me->esc2, ft_strlen(me->s1),
|
||||
(int[2]){ft_strlen(content), 1},
|
||||
(int[2]){ft_strlen(me->s1) + ft_strlen(me->mid),
|
||||
ft_strlen(me->s2)}), 0));
|
||||
else
|
||||
{
|
||||
ft_ld_pushfront(me->wk, gen_tab(ft_strjoin(me->s1, content),
|
||||
calc_expand_esc(me->esc, ft_strlen(me->s1),
|
||||
(int[2]){ft_strlen(content), 1}, (int[2]){0, 0}),
|
||||
calc_expand_esc(me->esc2, ft_strlen(me->s1),
|
||||
(int[2]){ft_strlen(content), 1}, (int[2]){0, 0}), 0));
|
||||
expand_all_bquote(me, content2, ifs);
|
||||
}
|
||||
}
|
||||
|
||||
static char *get_output(char *command)
|
||||
{
|
||||
char *output;
|
||||
int len;
|
||||
|
||||
output = command_getoutput(command);
|
||||
len = ft_strlen(output);
|
||||
while (output[--len] == '\n')
|
||||
output[len] = '\0';
|
||||
return (output);
|
||||
}
|
||||
|
||||
static int search_bquote(t_bquote *me)
|
||||
{
|
||||
char *sta;
|
||||
char *content;
|
||||
|
||||
sta = NULL;
|
||||
while (*me->str)
|
||||
{
|
||||
sta = *me->str == '`' && !sta
|
||||
&& !is_char_esc(me->esc2, CH(*me->wk)[0], me->str) ? me->str : sta;
|
||||
if (sta && *me->str == '`' && me->str != sta
|
||||
&& !is_char_esc(me->esc2, CH(*me->wk)[0], me->str))
|
||||
{
|
||||
me->mid = ft_strsub(sta, 1, me->str - sta - 1);
|
||||
me->s1 = ft_strsub(CH(*me->wk)[0], 0, sta - CH(*me->wk)[0]);
|
||||
me->s2 = ft_strdup(me->str + 1);
|
||||
content = get_output(me->mid);
|
||||
init_expand(me, content, is_char_esc(me->esc, CH(*me->wk)[0], sta));
|
||||
ft_strdel(&me->mid);
|
||||
ft_strdel(&me->s1);
|
||||
ft_strdel(&me->s2);
|
||||
ft_strdel(&content);
|
||||
return (1);
|
||||
}
|
||||
++me->str;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
void expand_bquote(t_glob *gl)
|
||||
{
|
||||
t_ld *tmp;
|
||||
t_bquote me;
|
||||
int do_it;
|
||||
|
||||
ft_ld_pushfront(&gl->m_pat, gen_tab(gl->pat, gl->esc, gl->esc2, 0));
|
||||
me = (t_bquote){NULL, NULL, NULL, NULL, NULL, NULL, NULL};
|
||||
do_it = 1;
|
||||
while ((gl->m_pat = ft_ld_front(gl->m_pat)) && do_it)
|
||||
{
|
||||
do_it = 0;
|
||||
while (gl->m_pat && !do_it)
|
||||
{
|
||||
me.wk = &gl->m_pat;
|
||||
me.esc = UCH(gl->m_pat)[1];
|
||||
me.esc2 = UCH(gl->m_pat)[2];
|
||||
me.str = CH(gl->m_pat)[0];
|
||||
if ((tmp = gl->m_pat) &&
|
||||
(do_it = search_bquote(&me)) == 1)
|
||||
ft_ld_del(&tmp, &ft_tabdel);
|
||||
if (!gl->m_pat->next)
|
||||
break ;
|
||||
gl->m_pat = gl->m_pat->next;
|
||||
}
|
||||
}
|
||||
ft_ld_reverse(&gl->m_pat);
|
||||
}
|
||||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/01/12 19:00:29 by wescande #+# #+# */
|
||||
/* Updated: 2017/02/07 16:10:20 by wescande ### ########.fr */
|
||||
/* Updated: 2017/02/20 18:54:19 by wescande ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -20,24 +20,33 @@
|
|||
** -t_glob *gl -> struct of expanding
|
||||
*/
|
||||
|
||||
static char **gen_tab(const char *pat,
|
||||
const unsigned char *esc, int dup)
|
||||
char **gen_tab(const char *pat,
|
||||
const unsigned char *esc,
|
||||
const unsigned char *esc2,
|
||||
int dup)
|
||||
{
|
||||
char **my_tab;
|
||||
|
||||
if (!(my_tab = (char **)malloc(sizeof(char *) * 3)))
|
||||
if (!(my_tab = (char **)malloc(sizeof(char *) * (esc2 ? 4 : 3))))
|
||||
return (NULL);
|
||||
if (dup)
|
||||
{
|
||||
my_tab[0] = ft_strdup(pat);
|
||||
my_tab[1] = ft_strdup((const char *)esc);
|
||||
if (esc2)
|
||||
my_tab[2] = ft_strdup((const char *)esc2);
|
||||
}
|
||||
else
|
||||
{
|
||||
my_tab[0] = (char *)pat;
|
||||
my_tab[1] = (char *)esc;
|
||||
if (esc2)
|
||||
my_tab[2] = (char *)esc2;
|
||||
}
|
||||
my_tab[2] = NULL;
|
||||
if (esc2)
|
||||
my_tab[3] = NULL;
|
||||
else
|
||||
my_tab[2] = NULL;
|
||||
return (my_tab);
|
||||
}
|
||||
|
||||
|
|
@ -56,12 +65,12 @@ static void iter_on_each(t_expand *me)
|
|||
first = ft_strjoinf(ft_strjoin(me->s1, me->split[i]), me->str + 1, 1);
|
||||
second = calc_expand_esc(me->esc,
|
||||
ft_strlen(me->s1),
|
||||
(int [2]){ft_strlen(me->split[i]), 0},
|
||||
(int [2]){me->str - CH(*me->wk)[0], ft_strlen(me->str + 1)});
|
||||
(int[2]){ft_strlen(me->split[i]), 0},
|
||||
(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);
|
||||
my_new = gen_tab(first, second, NULL, 0);
|
||||
ft_ld_pushfront(me->wk, my_new);
|
||||
}
|
||||
me->wk = &wk_tmp;
|
||||
}
|
||||
|
|
@ -87,28 +96,28 @@ static int init_expand(t_expand *me, char *start)
|
|||
static int search_brace(t_expand *me)
|
||||
{
|
||||
char *start;
|
||||
int comma;
|
||||
int com;
|
||||
int nb;
|
||||
|
||||
start = NULL;
|
||||
nb = 0;
|
||||
comma = 0;
|
||||
while (*me->str)
|
||||
com = 0;
|
||||
while (*++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);
|
||||
comma += *me->str == ',' && nb == 1;
|
||||
com += *me->str == ',' && !is_char_esc(me->esc, CH(*me->wk)[0], me->str)
|
||||
&& nb == 1;
|
||||
if (!nb && start)
|
||||
{
|
||||
if (comma)
|
||||
if (com)
|
||||
return (init_expand(me, start));
|
||||
set_char_esc(me->esc, CH(*me->wk)[0], start);
|
||||
set_char_esc(me->esc, CH(*me->wk)[0], me->str);
|
||||
return (2);
|
||||
}
|
||||
++me->str;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
|
@ -119,21 +128,22 @@ 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, 0));
|
||||
me = (t_expand){NULL, NULL, NULL, NULL, NULL, NULL};
|
||||
do_it = 1;
|
||||
while (do_it)
|
||||
{
|
||||
do_it = 0;
|
||||
while (gl->m_pat->next && !do_it)
|
||||
while (gl->m_pat && !do_it)
|
||||
{
|
||||
me.wk = &gl->m_pat;
|
||||
me.esc = UCH(gl->m_pat)[1];
|
||||
me.str = CH(gl->m_pat)[0];
|
||||
--me.str;
|
||||
if ((tmp = gl->m_pat) &&
|
||||
(do_it = search_brace(&me)) == 1)
|
||||
ft_ld_del(&tmp, &ft_tabdel);
|
||||
if (!gl->m_pat->next)
|
||||
break ;
|
||||
gl->m_pat = gl->m_pat->next;
|
||||
}
|
||||
gl->m_pat = ft_ld_front(gl->m_pat);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/01/31 23:18:56 by wescande #+# #+# */
|
||||
/* Updated: 2017/02/07 15:49:15 by wescande ### ########.fr */
|
||||
/* Updated: 2017/02/20 17:25:43 by wescande ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -20,23 +20,23 @@ unsigned char *calc_expand_esc(const unsigned char *esc,
|
|||
int pos;
|
||||
|
||||
if (!(new_esc = (unsigned char *)ft_strnew(((nb_start + nb_middle[0]
|
||||
+ nb_end[1]) / 8) + 1)))
|
||||
+ nb_end[1]) >> 3) + 1)))
|
||||
return (NULL);
|
||||
index = -1;
|
||||
while (++index < nb_start)
|
||||
new_esc[index / 8] |=
|
||||
((esc[index / 8] >> (7 - index % 8)) & 1) << (7 - index % 8);
|
||||
new_esc[index >> 3] |=
|
||||
((esc[index >> 3] >> (7 - index % 8)) & 1) << (7 - index % 8);
|
||||
pos = -1;
|
||||
while (++pos < nb_middle[0])
|
||||
{
|
||||
new_esc[index / 8] |= nb_middle[1] << (7 - index % 8);
|
||||
new_esc[index >> 3] |= nb_middle[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);
|
||||
new_esc[index >> 3] |=
|
||||
((esc[pos >> 3] >> (7 - pos % 8)) & 1) << (7 - index % 8);
|
||||
++index;
|
||||
}
|
||||
return (new_esc);
|
||||
|
|
@ -52,8 +52,8 @@ void modify_esc_split(unsigned char *esc_dest,
|
|||
while (++index < len)
|
||||
{
|
||||
wk = index + start - 1;
|
||||
esc_dest[wk / 8] |=
|
||||
((esc_src[index / 8] >> (7 - index % 8)) & 1) << (7 - wk % 8);
|
||||
esc_dest[wk >> 3] |=
|
||||
((esc_src[index >> 3] >> (7 - index % 8)) & 1) << (7 - wk % 8);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -63,13 +63,13 @@ unsigned char *ft_sub_esc(const unsigned char *esc,
|
|||
unsigned char *new_esc;
|
||||
int index;
|
||||
|
||||
if (!(new_esc = (unsigned char *)ft_strnew(((len) / 8) + 1)))
|
||||
if (!(new_esc = (unsigned char *)ft_strnew(((len) >> 3) + 1)))
|
||||
return (NULL);
|
||||
index = -1;
|
||||
while (++index < len)
|
||||
{
|
||||
new_esc[index / 8] |=
|
||||
((esc[start / 8] >> (7 - start % 8)) & 1) << (7 - index % 8);
|
||||
new_esc[index >> 3] |=
|
||||
((esc[start >> 3] >> (7 - start % 8)) & 1) << (7 - index % 8);
|
||||
++start;
|
||||
}
|
||||
return (new_esc);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/02/06 16:09:27 by wescande #+# #+# */
|
||||
/* Updated: 2017/02/07 17:18:33 by wescande ### ########.fr */
|
||||
/* Updated: 2017/02/20 18:56:50 by wescande ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -29,17 +29,20 @@ static void insert_var(t_glob *gl, char *pos, char *name, char *content)
|
|||
s1 = ft_strsub(gl->pat, 0, delta);
|
||||
delta += ft_strlen(name) + 1;
|
||||
s2 = ft_strsubf(gl->pat, delta, ft_strlen(gl->pat) - delta, 1);
|
||||
gl->pat = ft_strjoinf(ft_strjoinf(s1, content, 1), s2, 3);
|
||||
gl->pat = ft_strjoinf(ft_strjoin(s1, content), s2, 1);
|
||||
new_esc = calc_expand_esc(gl->esc, ft_strlen(s1),
|
||||
(int [2]){ft_strlen(content), 1},
|
||||
(int [2]){delta, ft_strlen(s2)});
|
||||
(int[2]){ft_strlen(content), 1},
|
||||
(int[2]){delta, ft_strlen(s2)});
|
||||
ft_memdel((void **)&gl->esc);
|
||||
gl->esc = new_esc;
|
||||
new_esc = calc_expand_esc(gl->esc2, ft_strlen(s1),
|
||||
(int [2]){ft_strlen(content), 1},
|
||||
(int [2]){delta, ft_strlen(s2)});
|
||||
(int[2]){ft_strlen(content), 1},
|
||||
(int[2]){delta, ft_strlen(s2)});
|
||||
ft_memdel((void **)&gl->esc2);
|
||||
gl->esc2 = new_esc;
|
||||
ft_strdel(&s1);
|
||||
ft_strdel(&s2);
|
||||
ft_strdel(&content);
|
||||
}
|
||||
|
||||
static char *get_name(t_glob *gl, const char *pat)
|
||||
|
|
@ -49,11 +52,11 @@ static char *get_name(t_glob *gl, const char *pat)
|
|||
str = pat;
|
||||
if (ft_isdigit(*str))
|
||||
return (ft_strsub(pat, 0, str - pat + 1));
|
||||
if (*str == '?')
|
||||
return (ft_strsub(pat, 0, str - pat + 1));
|
||||
while ((ft_isalnum(*str) || *str == '_' || *str == '-')
|
||||
&& !is_char_esc(gl->esc2, gl->pat, str))
|
||||
++str;
|
||||
if (is_char_esc(gl->esc2, gl->pat, str))
|
||||
return (ft_strsub(pat, 0, str - pat));
|
||||
return (ft_strsub(pat, 0, str - pat));
|
||||
}
|
||||
|
||||
|
|
@ -69,7 +72,7 @@ void expand_var(t_glob *gl)
|
|||
if (*pat == '$' && !is_char_esc(gl->esc2, gl->pat, pat) &&
|
||||
(var = get_name(gl, pat + 1)))
|
||||
{
|
||||
if (ft_strlen(var))
|
||||
if (var && *var)
|
||||
{
|
||||
content = ft_getenv(data_singleton()->env, var);
|
||||
insert_var(gl, pat, var, content);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* 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 */
|
||||
/* Updated: 2017/02/20 18:57:58 by wescande ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -63,9 +63,8 @@ char **ft_strsplit_spe(const char *str,
|
|||
const char *fix;
|
||||
int len;
|
||||
|
||||
if (!str)
|
||||
return (NULL);
|
||||
if (!(s1 = (char**)malloc(sizeof(*s1) * (ft_nbstr_c(str, esc, c) + 1))))
|
||||
if (!str || !(s1 =
|
||||
(char**)malloc(sizeof(*s1) * (ft_nbstr_c(str, esc, c) + 1))))
|
||||
return (NULL);
|
||||
i = 0;
|
||||
fix = str;
|
||||
|
|
@ -76,6 +75,7 @@ char **ft_strsplit_spe(const char *str,
|
|||
if (!(s1[i] = (char*)malloc(sizeof(**s1) * (len + 1))))
|
||||
return (NULL);
|
||||
ft_strncpy(s1[i], str, len);
|
||||
s1[i][len] = '\0';
|
||||
str = str + len;
|
||||
++i;
|
||||
while (*str && *str == c)
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/01/04 16:29:54 by wescande #+# #+# */
|
||||
/* Updated: 2017/02/07 19:57:44 by wescande ### ########.fr */
|
||||
/* Updated: 2017/02/20 19:04:44 by wescande ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -22,43 +22,44 @@
|
|||
** to just expanse in local directory and not in path dir
|
||||
*/
|
||||
|
||||
static char **treat_glob(t_ld **match)
|
||||
static char **treat_glob(t_glob *gl)
|
||||
{
|
||||
char **gl;
|
||||
char **ret;
|
||||
|
||||
gl = NULL;
|
||||
ft_ld_reverse(match);
|
||||
gl = ft_ld_to_tab(*match);
|
||||
ft_ld_clear(match, &ft_strdel);
|
||||
return (gl);
|
||||
ret= NULL;
|
||||
ft_ld_clear(&gl->m_pat, &ft_tabdel);
|
||||
ft_ld_reverse(&gl->match);
|
||||
ret = ft_ld_to_tab(gl->match);
|
||||
ft_ld_clear(&gl->match, &ft_strdel);
|
||||
return (ret);
|
||||
}
|
||||
|
||||
char **glob(char *pat, unsigned char *esc,
|
||||
unsigned char *esc2)
|
||||
unsigned char *esc2)
|
||||
{
|
||||
t_glob gl;
|
||||
int len;
|
||||
|
||||
len = ft_strlen(pat);
|
||||
gl = (t_glob){0, 0, ft_strdup(pat), dup_char_esc(esc, (len >> 3) + 1),
|
||||
dup_char_esc(esc2, (len >> 3) +1), NULL, NULL};
|
||||
dup_char_esc(esc2, (len >> 3) + 1), NULL, NULL};
|
||||
expand_var(&gl);
|
||||
ft_memdel((void **)&gl.esc2);
|
||||
expand_bquote(&gl);
|
||||
expand_brace(&gl);
|
||||
while (gl.m_pat->next && !(gl.found = 0))
|
||||
while (gl.m_pat && !(gl.found = 0))
|
||||
{
|
||||
gl.cur_dir = 1;
|
||||
gl.pat = ((char **)gl.m_pat->content)[0];
|
||||
gl.esc = ((unsigned char **)gl.m_pat->content)[1];
|
||||
if (gl.pat[0] != '/')
|
||||
gl.pat = CH(gl.m_pat)[0];
|
||||
if ((gl.esc = UCH(gl.m_pat)[1]) && gl.pat[0] != '/')
|
||||
dir_research(&gl, ".", gl.pat, 0);
|
||||
else
|
||||
dir_research(&gl, "/", gl.pat + 1, 0);
|
||||
if (!gl.found)
|
||||
ft_ld_pushfront(&gl.match,
|
||||
ft_strjoin(gl.cur_dir ? "" : "./", ((char **)gl.m_pat->content)[0]));
|
||||
ft_strjoin(gl.cur_dir ? "" : "./", CH(gl.m_pat)[0]));
|
||||
if (!gl.m_pat->next)
|
||||
break ;
|
||||
gl.m_pat = gl.m_pat->next;
|
||||
}
|
||||
ft_ld_clear(&gl.m_pat, &ft_tabdel);
|
||||
return (treat_glob(&gl.match));
|
||||
return (treat_glob(&gl));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,13 +6,13 @@
|
|||
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/01/27 18:19:55 by wescande #+# #+# */
|
||||
/* Updated: 2017/02/17 16:29:20 by jhalford ### ########.fr */
|
||||
/* Updated: 2017/02/20 18:51:47 by wescande ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "glob.h"
|
||||
|
||||
int is_char_esc(const unsigned char *esc,
|
||||
int is_char_esc(const unsigned char *esc,
|
||||
const char *ini_str, const char *str_pos)
|
||||
{
|
||||
int pos;
|
||||
|
|
@ -27,7 +27,7 @@ int is_char_esc(const unsigned char *esc,
|
|||
return (0);
|
||||
}
|
||||
|
||||
void set_char_esc_mode(unsigned char *esc,
|
||||
void set_char_esc_mode(unsigned char *esc,
|
||||
const char *ini_str, const char *str_pos, int mode)
|
||||
{
|
||||
int pos;
|
||||
|
|
@ -38,7 +38,7 @@ void set_char_esc_mode(unsigned char *esc,
|
|||
esc[pos >> 3] |= mode << (7 - pos % 8);
|
||||
}
|
||||
|
||||
void set_char_esc(unsigned char *esc,
|
||||
void set_char_esc(unsigned char *esc,
|
||||
const char *ini_str, const char *str_pos)
|
||||
{
|
||||
int pos;
|
||||
|
|
@ -49,7 +49,7 @@ void set_char_esc(unsigned char *esc,
|
|||
esc[pos >> 3] |= 1 << (7 - pos % 8);
|
||||
}
|
||||
|
||||
void set_char_no_esc(unsigned char *esc,
|
||||
void set_char_no_esc(unsigned char *esc,
|
||||
const char *ini_str, const char *str_pos)
|
||||
{
|
||||
int pos;
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/01/24 17:30:23 by wescande #+# #+# */
|
||||
/* Updated: 2017/02/07 19:45:54 by wescande ### ########.fr */
|
||||
/* Updated: 2017/02/20 14:00:41 by wescande ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -73,7 +73,7 @@ static int match_star(t_glob *gl, char *str, char *full_word)
|
|||
if (gl->pat[1] == '*' &&
|
||||
!is_char_esc(gl->esc, ((char **)gl->m_pat->content)[0], gl->pat + 1))
|
||||
dir_research(gl, full_word, gl->pat + 1, 1);
|
||||
if (!*++gl->pat)
|
||||
if (!*gl->pat || (*gl->pat == '*' && !*++gl->pat))
|
||||
return (1);
|
||||
fix = str + ft_strlen(str);
|
||||
while (fix > str)
|
||||
|
|
@ -107,7 +107,7 @@ int match_pattern(t_glob *gl, char *str, char *full_word)
|
|||
return (0);
|
||||
}
|
||||
else if (*gl->pat == '?')
|
||||
str++;
|
||||
;
|
||||
else if (*gl->pat == '[')
|
||||
{
|
||||
if (!match_bracket(gl, *str))
|
||||
|
|
|
|||
|
|
@ -1,101 +0,0 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* expand_bquotes.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/01/11 16:46:27 by jhalford #+# #+# */
|
||||
/* Updated: 2017/02/17 16:34:34 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "lexer.h"
|
||||
|
||||
int bquotes_insert_words(t_list *cur_word, char *word, char *after_bq, char *ifs)
|
||||
{
|
||||
t_list *new_word;
|
||||
t_list *after_word;
|
||||
t_token *token;
|
||||
|
||||
after_word = cur_word->next;
|
||||
if (ifs)
|
||||
word = ft_strtok(word, ifs);
|
||||
token = cur_word->content;
|
||||
token_append_str(token, word, 0, 0);
|
||||
if (ifs)
|
||||
while ((word = ft_strtok(NULL, ifs)))
|
||||
{
|
||||
token = token_init();
|
||||
token->type = TK_WORD;
|
||||
token_append_str(token, word, 0, 0);
|
||||
new_word = ft_lstnew(token, sizeof(*token));
|
||||
cur_word->next = new_word;
|
||||
new_word->next = after_word;
|
||||
cur_word = new_word;
|
||||
}
|
||||
token = cur_word->content;
|
||||
ft_strappend(&token->data, after_bq);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int bquotes_substitute(t_list *cur_word, char *bq_start, char *bq_end)
|
||||
{
|
||||
char *output;
|
||||
char *last_char;
|
||||
char *after_bq;
|
||||
char *ifs;
|
||||
t_token *token;
|
||||
|
||||
*bq_start = 0;
|
||||
*bq_end = 0;
|
||||
if ((output = command_getoutput(bq_start + 1)))
|
||||
{
|
||||
last_char = output + ft_strlen(output) - 1;
|
||||
while (*last_char == '\n')
|
||||
*last_char++ = 0;
|
||||
}
|
||||
DG("output = [%s]", output);
|
||||
after_bq = ft_strdup(bq_end + 1);
|
||||
token = cur_word->content;
|
||||
ifs = is_char_esc(token->esc, token->data, bq_start) ?
|
||||
NULL : ft_getenv(data_singleton()->env, "IFS");
|
||||
bquotes_insert_words(cur_word, output ? output : "", after_bq, ifs);
|
||||
ft_strdel(&output);
|
||||
ft_strdel(&after_bq);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int bquotes_expand(t_list **alst)
|
||||
{
|
||||
t_list *cur_word;
|
||||
char *bq_start;
|
||||
char *bq_end;
|
||||
t_flag tk_word;
|
||||
t_token *token;
|
||||
|
||||
tk_word = TK_WORD;
|
||||
cur_word = *alst;
|
||||
while ((cur_word = ft_lst_find(cur_word, &tk_word, token_cmp_type)))
|
||||
{
|
||||
token = cur_word->content;
|
||||
if (!(bq_start = ft_strchr(token->data, '`'))
|
||||
|| is_char_esc(token->esc2, token->data, bq_start))
|
||||
{
|
||||
cur_word = cur_word->next;
|
||||
return (0);
|
||||
}
|
||||
if (!(bq_end = ft_strchr(bq_start + 1, '`')))
|
||||
{
|
||||
ft_dprintf(2, "{red}%s: parse error near '`'{eoc}\n", SHELL_NAME);
|
||||
return (-1);
|
||||
}
|
||||
if (bquotes_substitute(cur_word, bq_start, bq_end))
|
||||
return (-1);
|
||||
if (!(*token->data))
|
||||
ft_lst_delif(alst, cur_word->content, ft_addrcmp, token_free);
|
||||
cur_word = *alst;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */
|
||||
/* Updated: 2017/02/17 16:35:24 by jhalford ### ########.fr */
|
||||
/* Updated: 2017/02/20 19:13:50 by wescande ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -31,9 +31,9 @@ int non_interactive_shell(char *command)
|
|||
} while (lexer.str[lexer.pos] == '\n');
|
||||
if (!token)
|
||||
return (0);
|
||||
if (bquotes_expand(&token))
|
||||
return (1);
|
||||
token_print(token);
|
||||
// if (bquotes_expand(&token))
|
||||
// return (1);
|
||||
//token_print(token);
|
||||
if (ft_parse(&ast, &token))
|
||||
return (1);
|
||||
if (ft_exec(&ast))
|
||||
|
|
@ -62,11 +62,10 @@ int interactive_shell()
|
|||
ltoken = ft_lstlast(token);
|
||||
if (lexer_lex(token ? <oken : &token, &lexer))
|
||||
return (1);
|
||||
DG("[{mag}%s{eoc}] stack=[%i] state=[%i]", lexer.str, lexer.stack ? *(int*)lexer.stack->content : 0, lexer.state);
|
||||
token_print(token);
|
||||
//token_print(token);
|
||||
} while (get_lexer_stack(lexer));
|
||||
if (bquotes_expand(&token))
|
||||
return (1);
|
||||
// if (bquotes_expand(&token))
|
||||
// return (1);
|
||||
if (!token)
|
||||
return (0);
|
||||
ft_add_str_in_history(lexer.str);
|
||||
|
|
@ -86,7 +85,7 @@ int main(int ac, char **av)
|
|||
data = data_singleton();
|
||||
setlocale(LC_ALL, "");
|
||||
shell_init(ac, av);
|
||||
DG("{inv}{bol}{gre}start of shell{eoc} JOBC is %s", SH_HAS_JOBC(data->opts)?"ON":"OFF");
|
||||
// DG("{inv}{bol}{gre}start of shell{eoc} JOBC is %s", SH_HAS_JOBC(data->opts)?"ON":"OFF");
|
||||
if (SH_IS_INTERACTIVE(data->opts))
|
||||
{
|
||||
while (1)
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2016/12/12 17:37:04 by jhalford #+# #+# */
|
||||
/* Updated: 2017/01/10 16:51:45 by jhalford ### ########.fr */
|
||||
/* Updated: 2017/02/20 18:37:46 by wescande ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -14,7 +14,7 @@
|
|||
|
||||
void shell_exit(void)
|
||||
{
|
||||
DG("shell_exit()");
|
||||
// DG("shell_exit()");
|
||||
data_exit();
|
||||
if (SH_HAS_JOBC(data_singleton()->opts))
|
||||
job_kill_all();
|
||||
|
|
|
|||
Loading…
Reference in a new issue