optimisation du glob sur les chaines vides + travail sur bquote mais marche po
This commit is contained in:
parent
3d74e17ac9
commit
a1c26314e0
6 changed files with 93 additions and 53 deletions
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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); */
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Reference in a new issue