From 3d74e17ac9964df3c69324110cbc10080ae57a96 Mon Sep 17 00:00:00 2001 From: william Date: Fri, 10 Mar 2017 13:17:39 +0100 Subject: [PATCH 1/3] rectif de glob sur les dossier pour matcher uniquement avec le bon nombre d'inclusion de / --- 42sh/src/glob/dir_glob.c | 23 ++++++++++++----------- 42sh/src/glob/match_pattern.c | 2 +- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/42sh/src/glob/dir_glob.c b/42sh/src/glob/dir_glob.c index 67f87cbc..98d3eb47 100644 --- a/42sh/src/glob/dir_glob.c +++ b/42sh/src/glob/dir_glob.c @@ -21,27 +21,27 @@ int is_directory(const char *path) } static void dir_list_content(t_glob *gl, char **str, char *pat, - int recursive) + int recursive) { - char *path_tmp; + char *path; if (str[1][0] != '.') { if (*str[0] == '/' && !*(str[0] + 1)) - path_tmp = ft_strjoin(str[0], str[1]); + path = ft_strjoin(str[0], str[1]); else - path_tmp = ft_strjoinf(ft_strjoin(str[0], "/"), str[1], 1); + path = ft_strjoinf(ft_strjoin(str[0], "/"), str[1], 1); if (recursive) - dir_research(gl, path_tmp, pat, recursive); + dir_research(gl, path, pat, recursive); gl->pat = pat; - if (match_pattern(gl, str[1], path_tmp)) + if (match_pattern(gl, str[1], path)) { gl->found = 1; - ft_ld_pushfront(&gl->match_tmp, ft_strdup(path_tmp + gl->cur_dir * 2 * - (path_tmp[0] == '.' && path_tmp[1] == '/'))); + ft_ld_pushfront(&gl->match_tmp, ft_strdup(path + gl->cur_dir * 2 * + (path[0] == '.' && path[1] == '/'))); } gl->pat = pat; - ft_strdel(&path_tmp); + ft_strdel(&path); } } @@ -51,10 +51,11 @@ int dir_research(t_glob *gl, char *p, DIR *dir; struct dirent *in; - if (!pat) + if (!*pat) { gl->found = 1; - ft_ld_pushfront(&gl->match_tmp, ft_strjoin(p, "/")); + ft_ld_pushfront(&gl->match_tmp, ft_strjoin(p + gl->cur_dir * 2 * + (p[0] == '.' && p[1] == '/'), "/")); return (0); } if ((ft_strlen(p) <= 1 || p[ft_strlen(p) - 1] != '.') && is_directory(p)) diff --git a/42sh/src/glob/match_pattern.c b/42sh/src/glob/match_pattern.c index 59dbe552..29c23ae6 100644 --- a/42sh/src/glob/match_pattern.c +++ b/42sh/src/glob/match_pattern.c @@ -119,7 +119,7 @@ int match_pattern(t_glob *gl, char *str, char *full_word) else if (*gl->pat == '*') return (match_star(gl, str, full_word)); else if (*gl->pat == '/' && !*str && is_directory(full_word)) - dir_research(gl, full_word, gl->pat + 1, 0); + return (dir_research(gl, full_word, gl->pat + 1, 0)); else if (*gl->pat != *str) return (0); ++str; From a1c26314e052a788b4538fc9e51d6bdb2308d477 Mon Sep 17 00:00:00 2001 From: william Date: Fri, 10 Mar 2017 15:00:38 +0100 Subject: [PATCH 2/3] optimisation du glob sur les chaines vides + travail sur bquote mais marche po --- 42sh/includes/glob.h | 2 +- 42sh/src/glob/command_getoutput.c | 114 +++++++++++++++++++----------- 42sh/src/glob/dir_glob.c | 11 +-- 42sh/src/glob/expand_bquote.c | 11 ++- 42sh/src/glob/glob.c | 4 +- 42sh/src/glob/match_pattern.c | 4 +- 6 files changed, 93 insertions(+), 53 deletions(-) diff --git a/42sh/includes/glob.h b/42sh/includes/glob.h index efdc32c4..42cf7b26 100644 --- a/42sh/includes/glob.h +++ b/42sh/includes/glob.h @@ -100,7 +100,7 @@ void expand_home(t_glob *gl, char *str); void expand_var(t_glob *tglob); void expand_home(t_glob *gl, char *str); int match_pattern(t_glob *tglob, char *str, char *full_word); -int dir_research(t_glob *tglob, char *p, char *pat, int rec); +int dir_research(t_glob *tglob, char *p, char *pat, int rec, int first); char **gen_tab(const char *pat, const unsigned char *esc, const unsigned char *esc2, int dup); char **ft_strsplit_spe(const char *str, diff --git a/42sh/src/glob/command_getoutput.c b/42sh/src/glob/command_getoutput.c index 4ebb686d..9a01da8b 100644 --- a/42sh/src/glob/command_getoutput.c +++ b/42sh/src/glob/command_getoutput.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/12 14:01:59 by jhalford #+# #+# */ -/* Updated: 2017/03/08 23:46:06 by ariard ### ########.fr */ +/* Updated: 2017/03/08 23:46:06 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,51 +14,83 @@ #define BUF_SIZE 1024 /*static char *manage_output(int *fds) -{ - int ret; - char buf[BUF_SIZE + 1]; - char *output; + { + int ret; + char buf[BUF_SIZE + 1]; + char *output; + output = NULL; + while ((ret = read(fds[PIPE_READ], buf, BUF_SIZE)) > 0) + { + buf[ret] = 0; + ft_strappend(&output, buf); + } + close(fds[PIPE_READ]); + return (output); + } + */ + + +static t_btree *gen_t_btree(const char *command) +{ + t_btree *ast; + t_astnode item; + int len; + unsigned char *esc; + + ft_bzero(&item, sizeof(t_astnode)); + item.type = SUBSHELL; + ast = btree_create_node(&item, sizeof(t_astnode)); + ft_bzero(&item, sizeof(t_astnode)); + item.type = CMD; + len = ft_strlen(command); + esc = ft_memalloc((len >> 3) + 1); + ft_ld_pushfront(&item.data.cmd.token, gen_tab(command, esc, esc, 1)); + ast->right = btree_create_node(&item, sizeof(t_astnode)); + return (ast); +} + +char *command_getoutput(char *command) +{ + t_btree *ast; + char *output; + char buf[BUF_SIZE + 1]; + t_exec *exec; + t_process p; + int ret; + int pid; + + return (NULL); output = NULL; - while ((ret = read(fds[PIPE_READ], buf, BUF_SIZE)) > 0) + exec = &data_singleton()->exec; + DG("%p exec ?", exec); + DG("\033[38;5;46mbefore"); + ast = gen_t_btree(command); + DG(); + if (set_process(&p, ast)) + return (NULL); + DG(); + if (!(pid = launch_subshell(&p))) + return (NULL); + waitpid(pid, &ret, WUNTRACED); + DG(); +// exec_leaf(&ast); + /* + DG(); + set_process(&p, ast); + DG(); + launch_process(&p); + DG(); + close(p.fdout);*/ + DG(); + while ((ret = read(exec->fdin, buf, BUF_SIZE)) > 0) { buf[ret] = 0; ft_strappend(&output, buf); } - close(fds[PIPE_READ]); + DG(); + close(exec->fdin); + DG("done with %s\033[0m", output); + return (output); } -*/ -char *command_getoutput(char *command) -{ - return (ft_strdup(command)); - /* int fds[2]; */ - /* t_btree *ast; */ - /* t_astnode item; */ - /* char *output; */ - /* char buf[BUF_SIZE + 1]; */ - /* int ret; */ - /* t_exec *exec; */ - - /* output = NULL; */ - /* exec = &data_singleton()->exec; */ - /* item.type = SUBSHELL; */ - /* item.data.sstr = malloc(4 * sizeof(char *)); */ - /* item.data.sstr[0] = ft_strdup(data_singleton()->argv[0]); */ - /* item.data.sstr[1] = ft_strdup("-c"); */ - /* item.data.sstr[2] = ft_strdup(command); */ - /* item.data.sstr[3] = NULL; */ - /* ast = btree_create_node(&item, sizeof(item)); */ - /* pipe(fds); */ - /* exec->process.fdout = fds[PIPE_WRITE]; */ - /* exec_command(&ast); */ - /* exec->process.fdout = STDOUT; */ - /* close(fds[PIPE_WRITE]); */ - /* while ((ret = read(fds[PIPE_READ], buf, BUF_SIZE))) */ - /* { */ - /* buf[ret] = 0; */ - /* ft_strappend(&output, buf); */ - /* } */ - /* close(fds[PIPE_READ]); */ - /* return (output); */ -} diff --git a/42sh/src/glob/dir_glob.c b/42sh/src/glob/dir_glob.c index 98d3eb47..18401efe 100644 --- a/42sh/src/glob/dir_glob.c +++ b/42sh/src/glob/dir_glob.c @@ -32,7 +32,7 @@ static void dir_list_content(t_glob *gl, char **str, char *pat, else path = ft_strjoinf(ft_strjoin(str[0], "/"), str[1], 1); if (recursive) - dir_research(gl, path, pat, recursive); + dir_research(gl, path, pat, recursive, 0); gl->pat = pat; if (match_pattern(gl, str[1], path)) { @@ -46,7 +46,7 @@ static void dir_list_content(t_glob *gl, char **str, char *pat, } int dir_research(t_glob *gl, char *p, - char *pat, int recursive) + char *pat, int recursive, int first) { DIR *dir; struct dirent *in; @@ -54,8 +54,11 @@ int dir_research(t_glob *gl, char *p, if (!*pat) { gl->found = 1; - ft_ld_pushfront(&gl->match_tmp, ft_strjoin(p + gl->cur_dir * 2 * - (p[0] == '.' && p[1] == '/'), "/")); + if (!first) + ft_ld_pushfront(&gl->match_tmp, ft_strjoin(p + gl->cur_dir * 2 * + (p[0] == '.' && p[1] == '/'), "/")); + else + ft_ld_pushfront(&gl->match_tmp, ft_strdup("")); return (0); } if ((ft_strlen(p) <= 1 || p[ft_strlen(p) - 1] != '.') && is_directory(p)) diff --git a/42sh/src/glob/expand_bquote.c b/42sh/src/glob/expand_bquote.c index d09f7c17..cc5dfcb4 100644 --- a/42sh/src/glob/expand_bquote.c +++ b/42sh/src/glob/expand_bquote.c @@ -13,7 +13,7 @@ #include "minishell.h" static void expand_all_bquote(t_bquote *me, char *content, - char *ifs) + char *ifs) { char *content2; @@ -45,7 +45,8 @@ static void init_expand(t_bquote *me, char *content, int esc) ifs = esc ? NULL : ft_getenv(data_singleton()->env, "IFS"); content = ft_strtok(content, ifs); - if (!(content2 = ft_strtok(NULL, ifs))) + if (!content || !(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), @@ -56,6 +57,7 @@ static void init_expand(t_bquote *me, char *content, int esc) (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), @@ -72,7 +74,8 @@ static char *get_output(char *command) char *output; int len; - output = command_getoutput(command); + if (!(output = command_getoutput(command))) + return (ft_strnew(0)); len = ft_strlen(output); while (output[--len] == '\n') output[len] = '\0'; @@ -96,7 +99,9 @@ static int search_bquote(t_bquote *me) 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); + DG(); init_expand(me, content, is_char_esc(me->esc, CH(*me->wk)[0], sta)); + DG(); ft_strdel(&me->mid); ft_strdel(&me->s1); ft_strdel(&me->s2); diff --git a/42sh/src/glob/glob.c b/42sh/src/glob/glob.c index 1036544b..39b9c86c 100644 --- a/42sh/src/glob/glob.c +++ b/42sh/src/glob/glob.c @@ -96,9 +96,9 @@ char **glob(char *pat, unsigned char *esc, gl.cur_dir = 1; gl.pat = CH(gl.m_pat)[0]; if ((gl.esc = UCH(gl.m_pat)[1]) && gl.pat[0] != '/') - dir_research(&gl, ".", gl.pat, 0); + dir_research(&gl, ".", gl.pat, 0, 1); else - dir_research(&gl, "/", gl.pat + 1, 0); + dir_research(&gl, "/", gl.pat + 1, 0, 1); if (!gl.found) ft_ld_pushfront(&gl.match, ft_strdup(CH(gl.m_pat)[0])); else diff --git a/42sh/src/glob/match_pattern.c b/42sh/src/glob/match_pattern.c index 29c23ae6..17f5ae0d 100644 --- a/42sh/src/glob/match_pattern.c +++ b/42sh/src/glob/match_pattern.c @@ -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); + dir_research(gl, full_word, gl->pat + 1, 1, 0); if (!*gl->pat || (*gl->pat == '*' && !*++gl->pat)) return (1); pat = gl->pat; @@ -119,7 +119,7 @@ int match_pattern(t_glob *gl, char *str, char *full_word) else if (*gl->pat == '*') return (match_star(gl, str, full_word)); else if (*gl->pat == '/' && !*str && is_directory(full_word)) - return (dir_research(gl, full_word, gl->pat + 1, 0)); + return (dir_research(gl, full_word, gl->pat + 1, 0, 0)); else if (*gl->pat != *str) return (0); ++str; From d33f82d5fed8a85bb9b741327ec6eb1cda2ed4e3 Mon Sep 17 00:00:00 2001 From: william Date: Fri, 10 Mar 2017 15:01:12 +0100 Subject: [PATCH 3/3] =?UTF-8?q?trick=20pour=20le=20vrai=20printf=20qui=20g?= =?UTF-8?q?=C3=A8re=20pas=20le=20%b?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 42sh/src/builtin/bt_read_parse.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/42sh/src/builtin/bt_read_parse.c b/42sh/src/builtin/bt_read_parse.c index d79c754c..c12080c3 100644 --- a/42sh/src/builtin/bt_read_parse.c +++ b/42sh/src/builtin/bt_read_parse.c @@ -64,7 +64,7 @@ int bt_read_parse(t_read *data, char **av) i++; } data->names = av[i] ? av + i : NULL; - DG("read_opts: %b", data->opts); +// DG("read_opts: %b", data->opts); DG("\ndelim=%c\nnchars=%i\nprompt=%s\ntimeout=%i\nfd=%i", data->delim, data->nchars, data->prompt, data->timeout, data->fd); return (0);