ajout de l'expand des brace, reste a travailler sur le tri du tableau de retour pour l'avoir par ordre ascii
This commit is contained in:
parent
9cfecf1130
commit
6102eda2df
4 changed files with 123 additions and 6 deletions
|
|
@ -143,6 +143,7 @@ exec/ft_cmd.c\
|
||||||
exec/ft_exec.c\
|
exec/ft_exec.c\
|
||||||
exec/ft_findexec.c\
|
exec/ft_findexec.c\
|
||||||
exec/set_exitstatus.c\
|
exec/set_exitstatus.c\
|
||||||
|
glob/expand_brace.c\
|
||||||
glob/glob.c\
|
glob/glob.c\
|
||||||
glob/glob_print.c\
|
glob/glob_print.c\
|
||||||
glob/ld/ft_ld_back.c\
|
glob/ld/ft_ld_back.c\
|
||||||
|
|
|
||||||
|
|
@ -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/12 17:14:38 by wescande ### ########.fr */
|
/* Updated: 2017/01/12 19:00:08 by wescande ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -23,6 +23,7 @@ typedef struct s_ld
|
||||||
} t_ld;
|
} t_ld;
|
||||||
|
|
||||||
char **glob(const char *str, char **env);
|
char **glob(const char *str, char **env);
|
||||||
|
t_ld *expand_brace(const char *pat);
|
||||||
void glob_print(t_list *token, t_data *data);
|
void glob_print(t_list *token, t_data *data);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
||||||
108
42sh/srcs/glob/expand_brace.c
Normal file
108
42sh/srcs/glob/expand_brace.c
Normal file
|
|
@ -0,0 +1,108 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* expand_brace.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2017/01/12 19:00:29 by wescande #+# #+# */
|
||||||
|
/* Updated: 2017/01/12 20:24:00 by wescande ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "glob.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
** expand_brace return expansion of a string.
|
||||||
|
** pattern searched are {ab, cd}.
|
||||||
|
** return is t_ld which first param is ab and second is cd
|
||||||
|
** input parameters are :
|
||||||
|
** -char *pat -> pattern string to be looking for expand
|
||||||
|
*/
|
||||||
|
|
||||||
|
static char *ft_strjoinf(char *s1, char *s2, int state)
|
||||||
|
{
|
||||||
|
char *ans;
|
||||||
|
|
||||||
|
ans = ft_strjoin((const char *)s1, (const char *)s2);
|
||||||
|
if (state == 1 || state == 3)
|
||||||
|
ft_strdel(&s1);
|
||||||
|
if (state == 2 || state == 3)
|
||||||
|
ft_strdel(&s2);
|
||||||
|
return (ans);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ft_tabdel(char ***mytab)
|
||||||
|
{
|
||||||
|
char **erase;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
if (!mytab || !*mytab)
|
||||||
|
return ;
|
||||||
|
erase = *mytab;
|
||||||
|
i = 0;
|
||||||
|
while (erase[i])
|
||||||
|
{
|
||||||
|
ft_strdel(&erase[i]);
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
free(*mytab);
|
||||||
|
*mytab = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int search_brace(t_ld **wk, char *str, int index)
|
||||||
|
{
|
||||||
|
char *start;
|
||||||
|
char *s1;
|
||||||
|
char **split;
|
||||||
|
|
||||||
|
start = NULL;
|
||||||
|
while (*str)
|
||||||
|
{
|
||||||
|
if (*str == '{')
|
||||||
|
start = str;
|
||||||
|
else if (*str == '}' && start)
|
||||||
|
{
|
||||||
|
s1 = ft_strsub(start, 1, str - start - 1);
|
||||||
|
split = ft_strsplit(s1, ',');
|
||||||
|
ft_strdel(&s1);
|
||||||
|
s1 = ft_strsub((*wk)->content, 0, start - (char *)(*wk)->content);
|
||||||
|
while (split[++index])
|
||||||
|
ft_ld_pushfront(wk, ft_strjoinf(ft_strjoin(s1, split[index]),
|
||||||
|
str + 1, 1));
|
||||||
|
ft_strdel(&s1);
|
||||||
|
ft_tabdel(&split);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
++str;
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_ld *expand_brace(const char *pat)
|
||||||
|
{
|
||||||
|
t_ld *ret;
|
||||||
|
t_ld *tmp;
|
||||||
|
int do_it;
|
||||||
|
|
||||||
|
ret = NULL;
|
||||||
|
ft_ld_pushfront(&ret, ft_strdup(pat));
|
||||||
|
do_it = 1;
|
||||||
|
while (do_it)
|
||||||
|
{
|
||||||
|
do_it = 0;
|
||||||
|
while (ret)
|
||||||
|
{
|
||||||
|
if ((tmp = ret) && search_brace(&ret, ret->content, -1))
|
||||||
|
{
|
||||||
|
ft_ld_del(&tmp, &ft_strdel);
|
||||||
|
do_it = 1;
|
||||||
|
}
|
||||||
|
if (!ret->next)
|
||||||
|
break;
|
||||||
|
ret = ret->next;
|
||||||
|
}
|
||||||
|
ret = ft_ld_front(ret);
|
||||||
|
}
|
||||||
|
return (ret);
|
||||||
|
}
|
||||||
|
|
@ -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/12 18:48:26 by wescande ### ########.fr */
|
/* Updated: 2017/01/12 19:00:12 by wescande ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -176,15 +176,22 @@ char **glob(const char *pat, char **env)
|
||||||
t_ld *match;
|
t_ld *match;
|
||||||
char **gl;
|
char **gl;
|
||||||
char **path;
|
char **path;
|
||||||
|
t_ld *mul_pat;
|
||||||
|
|
||||||
match = NULL;
|
match = NULL;
|
||||||
gl = NULL;
|
gl = NULL;
|
||||||
if (env && (path = ft_strsplit(ft_getenv(env, "PATH"), ':')))
|
mul_pat = expand_brace(pat);
|
||||||
|
while (mul_pat)
|
||||||
{
|
{
|
||||||
path_research(pat, path, &match);
|
if (env && (path = ft_strsplit(ft_getenv(env, "PATH"), ':')))
|
||||||
ft_tabdel(&path);
|
{
|
||||||
|
path_research(mul_pat->content, path, &match);
|
||||||
|
ft_tabdel(&path);
|
||||||
|
}
|
||||||
|
dir_research(mul_pat->content, "./", &match);
|
||||||
|
mul_pat = mul_pat->next;
|
||||||
}
|
}
|
||||||
dir_research(pat, "./", &match);
|
ft_ld_clear(&mul_pat, &ft_strdel);
|
||||||
if (match)
|
if (match)
|
||||||
{
|
{
|
||||||
gl = ft_ld_to_tab(match);
|
gl = ft_ld_to_tab(match);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue