extension du globbing implemente SUPER MAKEFILE QUI DECHIRE TA M*RE

This commit is contained in:
wescande 2017-01-31 19:36:29 +01:00
parent 3c46e10736
commit ad7d814c74
16 changed files with 105 additions and 146 deletions

View file

@ -13,11 +13,11 @@
NAME = 42sh
CC = gcc
FLAGS = -Wall -Wextra -Werror
FLAGS = -Wall -Wextra -Werror -fsanitize=address -g
D_FLAGS = -g
LEN_NAME = `printf "%s" $(NAME) |wc -c`
DELTA = $$(echo "$$(tput cols)-24-$(LEN_NAME)"|bc)
DELTA = $$(echo "$$(tput cols)-37-$(LEN_NAME)"|bc)
LIBFT_DIR = libft/
LIBFT_LIB = $(LIBFT_DIR)libft.a
@ -62,7 +62,6 @@ exec/set_exitstatus.c\
glob/dir_glob.c\
glob/expand_brace.c\
glob/glob.c\
glob/glob_expand_token.c\
glob/is_char_esc.c\
glob/lib_perso/ft_ld_back.c\
glob/lib_perso/ft_ld_clear.c\
@ -189,6 +188,8 @@ parser/parse_word.c
SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE))
OBJS = $(addprefix $(OBJ_DIR), $(SRC_BASE:.c=.o))
NB = $(words $(SRC_BASE))
INDEX = 0
all :
@make -j $(NAME)
@ -210,10 +211,15 @@ $(OBJ_DIR) :
@mkdir -p $(dir $(OBJS))
$(OBJ_DIR)%.o : $(SRC_DIR)%.c | $(OBJ_DIR)
@$(eval DONE=$(shell echo $$(($(INDEX)*20/$(NB)))))
@$(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) "$@"
@$(CC) $(FLAGS) -MMD -c $< -o $@\
-I $(INC_DIR)\
-I $(LIBFT_INC)
@printf "\r\033[38;5;11m⌛ MAKE %s plz wait : %*s\033[0m\033[K" $(NAME) $(DELTA) "$@"
@$(eval INDEX=$(shell echo $$(($(INDEX)+1))))
clean: cleanlib
@rm -rf $(OBJ_DIR)

View file

@ -6,7 +6,7 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/04 16:31:18 by wescande #+# #+# */
/* Updated: 2017/01/30 17:14:48 by wescande ### ########.fr */
/* Updated: 2017/01/31 17:54:19 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
@ -44,9 +44,8 @@ typedef struct s_expand
/*
** interface of glob.
** It fill all token->expand_data with correspind expansion
*/
void glob_expand_token(t_list *token, t_data *data);
char **glob(const char *str, const unsigned char *esc);
/*
** return TRUE if path file is a directory.
@ -61,19 +60,11 @@ int is_char_esc(const unsigned char *esc,
/*
** Internal function.
**
**
**
**
**
**
**
*/
char **glob(const char *str, const unsigned char *esc, char **env);
void expand_brace(t_glob *tglob);
int match_pattern(t_glob *tglob, char *str, char *full_word);
void dir_research(t_glob *tglob, char *p, const char *pat);
void dir_research_recursive(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);
/*
** LIST D:
*/

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */
/* Updated: 2017/01/30 17:05:20 by wescande ### ########.fr */
/* Updated: 2017/01/31 18:35:34 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
@ -63,7 +63,6 @@ struct s_token
{
t_type type;
char *data;
char **expand_data;
unsigned char *esc;
int size;
};

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 12:15:54 by jhalford #+# #+# */
/* Updated: 2017/01/11 17:06:17 by jhalford ### ########.fr */
/* Updated: 2017/01/31 19:17:25 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,6 +16,7 @@
# include "minishell.h"
typedef struct s_parser t_parser;
typedef struct s_ld t_ld;
typedef struct s_astnode t_astnode;
typedef struct s_redir t_redir;
typedef union u_astdata t_astdata;
@ -44,6 +45,7 @@ struct s_redir
union u_astdata
{
t_redir redir;
t_ld *token;
char **sstr;
char *str;
};

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/05 11:50:51 by jhalford #+# #+# */
/* Updated: 2017/01/11 17:50:03 by jhalford ### ########.fr */
/* Updated: 2017/01/31 19:15:28 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,9 +19,11 @@ void ast_free(void *data, size_t content_size)
(void)content_size;
node = data;
if (node->type == TK_COMMAND)
ft_ld_clear(&node->data.token, &ft_tabdel);
else if (node->type == TK_SUBSHELL)
{
if (node->data.sstr)
ft_sstrfree(node->data.sstr);
ft_sstrfree(node->data.sstr);
node->data.sstr = NULL;
}
else if (node->type == TK_LESS || node->type == TK_GREAT || node->type == TK_DGREAT)
{

View file

@ -6,13 +6,53 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 17:28:14 by jhalford #+# #+# */
/* Updated: 2017/01/12 13:14:35 by jhalford ### ########.fr */
/* Updated: 2017/01/31 19:12:23 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
#include "exec.h"
int exec_command(t_btree **ast)
static char **return_array_expand(t_ld *ld)
{
char **my_tab;
int index;
char **expand;
char **content;
my_tab = NULL;
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]);
ft_tabdel(&expand);
ld = ld->next;
}
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;
t_process *p;
@ -21,7 +61,8 @@ int exec_command(t_btree **ast)
node = (*ast)->item;
p = &data_singleton()->exec.process;
job = &data_singleton()->exec.job;
p->av = ft_sstrdup(node->data.sstr);
p->av = return_array_expand(node->data.token);
ft_tabprint_fd(p->av, 4);
process_setexec(node->type, p);
if (!(launch_process(p)))
{

View file

@ -6,7 +6,7 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/30 12:07:16 by wescande #+# #+# */
/* Updated: 2017/01/30 12:07:19 by wescande ### ########.fr */
/* Updated: 2017/01/31 18:00:16 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,16 +20,16 @@ int is_directory(const char *path)
return (S_ISDIR(path_stat.st_mode));
}
void dir_research(t_glob *gl, char *p, const char *pat)
int dir_research(t_glob *gl, char *p, const char *pat)
{
DIR *dir;
struct dirent *in;
char *path_tmp;
int ret;
if (ft_strlen(p) <= 1 || p[ft_strlen(p) - 1] != '.')
{
if (!(dir = opendir(p)))
return ;
ret = 0;
if ((ft_strlen(p) <= 1 || p[ft_strlen(p) - 1] != '.')
&& is_directory(p) && (dir = opendir(p)))
while ((in = readdir(dir)))
{
if (ft_strcmp(in->d_name, ".") && ft_strcmp(in->d_name, ".."))
@ -39,25 +39,25 @@ void dir_research(t_glob *gl, char *p, const char *pat)
else
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) && ++ret)
ft_ld_pushfront(&gl->match, ft_strdup(path_tmp + 2 *
(path_tmp[0] == '.' && path_tmp[1] == '/')));
ft_strdel(&path_tmp);
}
}
}
return (ret);
}
void dir_research_recursive(t_glob *gl, char *p, const char *pat)
int dir_research_recursive(t_glob *gl, char *p, const char *pat)
{
DIR *dir;
struct dirent *in;
char *path_tmp;
int ret;
if ((ft_strlen(p) <= 1 || p[ft_strlen(p) - 1] != '.') && is_directory(p))
{
if (!(dir = opendir(p)))
return ;
ret = 0;
if ((ft_strlen(p) <= 1 || p[ft_strlen(p) - 1] != '.')
&& is_directory(p) && (dir = opendir(p)))
while ((in = readdir(dir)))
{
if (ft_strcmp(in->d_name, ".") && ft_strcmp(in->d_name, ".."))
@ -68,11 +68,11 @@ void dir_research_recursive(t_glob *gl, char *p, const char *pat)
path_tmp = ft_strjoinf(ft_strjoin(p, "/"), in->d_name, 1);
dir_research_recursive(gl, path_tmp, pat);
gl->pat = pat;
if (match_pattern(gl, in->d_name, path_tmp))
if (match_pattern(gl, in->d_name, path_tmp) && ++ret)
ft_ld_pushfront(&gl->match, ft_strdup(path_tmp + 2 *
(path_tmp[0] == '.' && path_tmp[1] == '/')));
ft_strdel(&path_tmp);
}
}
}
return (ret);
}

View file

@ -6,7 +6,7 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/12 19:00:29 by wescande #+# #+# */
/* Updated: 2017/01/28 01:13:26 by wescande ### ########.fr */
/* Updated: 2017/01/31 18:19:34 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
@ -38,6 +38,7 @@ static char **gen_tab(const char *pat,
my_tab[1] = (char *)esc;
}
my_tab[2] = NULL;
return (my_tab);
}
@ -126,6 +127,7 @@ 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};

View file

@ -6,7 +6,7 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/04 16:29:54 by wescande #+# #+# */
/* Updated: 2017/01/30 12:08:49 by wescande ### ########.fr */
/* Updated: 2017/01/31 18:19:56 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
@ -22,15 +22,6 @@
** to just expanse in local directory and not in path dir
*/
static void path_research(t_glob *gl, char **path)
{
int i;
i = -1;
while (path[++i])
dir_research(gl, path[i], gl->pat);
}
static char **treat_glob(t_ld **match)
{
char **gl;
@ -43,33 +34,10 @@ static char **treat_glob(t_ld **match)
return (gl);
}
static void add_simple_pat(t_glob *gl)
{
char *str;
int start;
str = (char *)gl->pat;
start = 0;
while (*str)
{
if (!is_char_esc(gl->esc, gl->pat, str))
{
if (*str == '[')
start = 1;
else if (*str == ']' && start == 1)
return ;
else if (*str == '*' || *str == '?')
return ;
}
++str;
}
ft_ld_pushfront(&gl->match, ft_strdup(gl->pat));
}
char **glob(const char *pat, const unsigned char *esc, char **env)
char **glob(const char *pat, const unsigned char *esc)
{
t_glob gl;
char **path;
int ret;
gl = (t_glob){pat, esc, NULL, NULL};
expand_brace(&gl);
@ -77,18 +45,12 @@ char **glob(const char *pat, const unsigned char *esc, char **env)
{
gl.pat = ((char **)gl.m_pat->content)[0];
gl.esc = ((unsigned char **)gl.m_pat->content)[1];
add_simple_pat(&gl);
if (!(gl.pat[0] == '/' || (gl.pat[0] == '.' && gl.pat[1] == '/'))
&& env && (path = ft_strsplit(ft_getenv(env, "PATH"), ':')))
{
path_research(&gl, path);
ft_tabdel(&path);
}
gl.pat = ((char **)gl.m_pat->content)[0];
if (gl.pat[0] != '/')
dir_research(&gl, ".", gl.pat);
ret = dir_research(&gl, ".", gl.pat);
else
dir_research(&gl, "/", gl.pat + 1);
ret = dir_research(&gl, "/", gl.pat + 1);
if (!ret)
ft_ld_pushfront(&gl.match, ft_strdup(gl.pat));
gl.m_pat = gl.m_pat->next;
}
ft_ld_clear(&gl.m_pat, &ft_tabdel);

View file

@ -1,48 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* glob_expand_token.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/30 17:10:34 by wescande #+# #+# */
/* Updated: 2017/01/30 17:12:02 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
#include "glob.h"
static void ft_tabprint_fd(char **mytab, int fd)
{
int i;
if (!mytab || !*mytab)
return ;
i = 0;
while (mytab[i])
{
ft_putendl_fd(mytab[i], fd);
++i;
}
}
void glob_expand_token(t_list *lst, t_data *data)
{
t_token *token;
int i;
t_type type;
while (lst)
{
i = 1;
token = lst->content;
type = token->type;
while (type >> (i++ + 2))
;
token->expand_data = glob(token->data, token->esc, data->env);
DG("EXPANSION DU TOKEN : %02i '%s'", i, token->data);
ft_tabprint_fd(token->expand_data, 4);
DG("FIN DE L'EXPANSION DU TOKEN");
lst = lst->next;
}
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/12 14:01:59 by jhalford #+# #+# */
/* Updated: 2017/01/12 15:31:34 by jhalford ### ########.fr */
/* Updated: 2017/01/31 16:07:17 by wescande ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/03 12:07:30 by jhalford #+# #+# */
/* Updated: 2017/01/30 17:06:23 by wescande ### ########.fr */
/* Updated: 2017/01/31 18:36:40 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
@ -21,7 +21,6 @@ void token_free(void *data, size_t size)
if (!(token->type & TK_NON_FREEABLE))
{
ft_strdel(&token->data);
ft_tabdel(&token->expand_data);
ft_memdel((void **)&token->esc);
}
free(token);

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 15:30:25 by jhalford #+# #+# */
/* Updated: 2017/01/30 17:05:55 by wescande ### ########.fr */
/* Updated: 2017/01/31 18:36:31 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
@ -21,6 +21,5 @@ t_token *token_init(void)
token->size = 8;
token->data = ft_strnew(token->size + 1);
token->esc = (unsigned char *)ft_strnew(token->size / 8 + 1);
token->expand_data = NULL;
return (token);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */
/* Updated: 2017/01/30 17:15:28 by wescande ### ########.fr */
/* Updated: 2017/01/31 18:00:57 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
@ -30,7 +30,7 @@ int shell_single_command(char *command)
return (1);
DG("after post_tokenize");
token_print(token);
glob_expand_token(token, data_singleton());
// glob_expand_token(token, data_singleton());
if (ft_parse(&ast, &token))
return (1);
btree_print(STDBUG, ast, &ft_putast);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/30 17:14:58 by jhalford #+# #+# */
/* Updated: 2017/01/12 15:07:31 by jhalford ### ########.fr */
/* Updated: 2017/01/31 16:10:41 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
@ -41,7 +41,7 @@ int ft_parse(t_btree **ast, t_list **start)
if (!*ast)
{
*ast = btree_create_node(&item, sizeof(item));
((t_astnode *)(*ast)->item)->data.sstr = NULL;
((t_astnode *)(*ast)->item)->data.token = NULL;
}
while (g_parser[i].type)
{

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */
/* Updated: 2017/01/11 17:24:55 by jhalford ### ########.fr */
/* Updated: 2017/01/31 19:17:58 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,12 +16,16 @@ int parse_word(t_btree **ast, t_list **start, t_list **lst)
{
t_astnode *node;
t_token *token;
char **my_tab;
(void)start;
my_tab = NULL;
token = (*lst)->content;
node = (*ast)->item;
node->type = TK_COMMAND;
node->data.sstr = ft_sstradd(node->data.sstr, token->data);
my_tab = ft_sstradd(my_tab, token->data);
my_tab = ft_sstradd(my_tab, (char *)token->esc);
ft_ld_pushback(&node->data.token, my_tab);
ft_parse(ast, &(*lst)->next);
ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free);
return (0);