This commit is contained in:
Jack Halford 2017-03-10 15:41:09 +01:00
commit b7a1f9e92f
7 changed files with 103 additions and 62 deletions

View file

@ -100,7 +100,7 @@ void expand_home(t_glob *gl, char *str);
void expand_var(t_glob *tglob); void expand_var(t_glob *tglob);
void expand_home(t_glob *gl, char *str); void expand_home(t_glob *gl, char *str);
int match_pattern(t_glob *tglob, char *str, char *full_word); 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, char **gen_tab(const char *pat, const unsigned char *esc,
const unsigned char *esc2, int dup); const unsigned char *esc2, int dup);
char **ft_strsplit_spe(const char *str, char **ft_strsplit_spe(const char *str,

View file

@ -64,7 +64,7 @@ int bt_read_parse(t_read *data, char **av)
i++; i++;
} }
data->names = av[i] ? av + i : NULL; 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", DG("\ndelim=%c\nnchars=%i\nprompt=%s\ntimeout=%i\nfd=%i",
data->delim, data->nchars, data->prompt, data->timeout, data->fd); data->delim, data->nchars, data->prompt, data->timeout, data->fd);
return (0); return (0);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/12 14:01:59 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -29,36 +29,68 @@
return (output); 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) char *command_getoutput(char *command)
{ {
return (ft_strdup(command)); t_btree *ast;
/* int fds[2]; */ char *output;
/* t_btree *ast; */ char buf[BUF_SIZE + 1];
/* t_astnode item; */ t_exec *exec;
/* char *output; */ t_process p;
/* char buf[BUF_SIZE + 1]; */ int ret;
/* int ret; */ int pid;
/* t_exec *exec; */
/* output = NULL; */ return (NULL);
/* exec = &data_singleton()->exec; */ output = NULL;
/* item.type = SUBSHELL; */ exec = &data_singleton()->exec;
/* item.data.sstr = malloc(4 * sizeof(char *)); */ DG("%p exec ?", exec);
/* item.data.sstr[0] = ft_strdup(data_singleton()->argv[0]); */ DG("\033[38;5;46mbefore");
/* item.data.sstr[1] = ft_strdup("-c"); */ ast = gen_t_btree(command);
/* item.data.sstr[2] = ft_strdup(command); */ DG();
/* item.data.sstr[3] = NULL; */ if (set_process(&p, ast))
/* ast = btree_create_node(&item, sizeof(item)); */ return (NULL);
/* pipe(fds); */ DG();
/* exec->process.fdout = fds[PIPE_WRITE]; */ if (!(pid = launch_subshell(&p)))
/* exec_command(&ast); */ return (NULL);
/* exec->process.fdout = STDOUT; */ waitpid(pid, &ret, WUNTRACED);
/* close(fds[PIPE_WRITE]); */ DG();
/* while ((ret = read(fds[PIPE_READ], buf, BUF_SIZE))) */ // exec_leaf(&ast);
/* { */ /*
/* buf[ret] = 0; */ DG();
/* ft_strappend(&output, buf); */ set_process(&p, ast);
/* } */ DG();
/* close(fds[PIPE_READ]); */ launch_process(&p);
/* return (output); */ DG();
close(p.fdout);*/
DG();
while ((ret = read(exec->fdin, buf, BUF_SIZE)) > 0)
{
buf[ret] = 0;
ft_strappend(&output, buf);
}
DG();
close(exec->fdin);
DG("done with %s\033[0m", output);
return (output);
} }

View file

@ -23,38 +23,42 @@ int is_directory(const char *path)
static void dir_list_content(t_glob *gl, char **str, char *pat, 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[1][0] != '.')
{ {
if (*str[0] == '/' && !*(str[0] + 1)) if (*str[0] == '/' && !*(str[0] + 1))
path_tmp = ft_strjoin(str[0], str[1]); path = ft_strjoin(str[0], str[1]);
else else
path_tmp = ft_strjoinf(ft_strjoin(str[0], "/"), str[1], 1); path = ft_strjoinf(ft_strjoin(str[0], "/"), str[1], 1);
if (recursive) if (recursive)
dir_research(gl, path_tmp, pat, recursive); dir_research(gl, path, pat, recursive, 0);
gl->pat = pat; gl->pat = pat;
if (match_pattern(gl, str[1], path_tmp)) if (match_pattern(gl, str[1], path))
{ {
gl->found = 1; gl->found = 1;
ft_ld_pushfront(&gl->match_tmp, ft_strdup(path_tmp + gl->cur_dir * 2 * ft_ld_pushfront(&gl->match_tmp, ft_strdup(path + gl->cur_dir * 2 *
(path_tmp[0] == '.' && path_tmp[1] == '/'))); (path[0] == '.' && path[1] == '/')));
} }
gl->pat = pat; gl->pat = pat;
ft_strdel(&path_tmp); ft_strdel(&path);
} }
} }
int dir_research(t_glob *gl, char *p, int dir_research(t_glob *gl, char *p,
char *pat, int recursive) char *pat, int recursive, int first)
{ {
DIR *dir; DIR *dir;
struct dirent *in; struct dirent *in;
if (!pat) if (!*pat)
{ {
gl->found = 1; gl->found = 1;
ft_ld_pushfront(&gl->match_tmp, ft_strjoin(p, "/")); 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); return (0);
} }
if ((ft_strlen(p) <= 1 || p[ft_strlen(p) - 1] != '.') && is_directory(p)) if ((ft_strlen(p) <= 1 || p[ft_strlen(p) - 1] != '.') && is_directory(p))

View file

@ -45,7 +45,8 @@ static void init_expand(t_bquote *me, char *content, int esc)
ifs = esc ? NULL : ft_getenv(data_singleton()->env, "IFS"); ifs = esc ? NULL : ft_getenv(data_singleton()->env, "IFS");
content = ft_strtok(content, 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), ft_ld_pushfront(me->wk, gen_tab(ft_strjoinf(ft_strjoin(me->s1, content),
me->s2, 1), me->s2, 1),
calc_expand_esc(me->esc, ft_strlen(me->s1), 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(content), 1},
(int[2]){ft_strlen(me->s1) + ft_strlen(me->mid), (int[2]){ft_strlen(me->s1) + ft_strlen(me->mid),
ft_strlen(me->s2)}), 0)); ft_strlen(me->s2)}), 0));
}
else else
{ {
ft_ld_pushfront(me->wk, gen_tab(ft_strjoin(me->s1, content), ft_ld_pushfront(me->wk, gen_tab(ft_strjoin(me->s1, content),
@ -72,7 +74,8 @@ static char *get_output(char *command)
char *output; char *output;
int len; int len;
output = command_getoutput(command); if (!(output = command_getoutput(command)))
return (ft_strnew(0));
len = ft_strlen(output); len = ft_strlen(output);
while (output[--len] == '\n') while (output[--len] == '\n')
output[len] = '\0'; 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->s1 = ft_strsub(CH(*me->wk)[0], 0, sta - CH(*me->wk)[0]);
me->s2 = ft_strdup(me->str + 1); me->s2 = ft_strdup(me->str + 1);
content = get_output(me->mid); content = get_output(me->mid);
DG();
init_expand(me, content, is_char_esc(me->esc, CH(*me->wk)[0], sta)); init_expand(me, content, is_char_esc(me->esc, CH(*me->wk)[0], sta));
DG();
ft_strdel(&me->mid); ft_strdel(&me->mid);
ft_strdel(&me->s1); ft_strdel(&me->s1);
ft_strdel(&me->s2); ft_strdel(&me->s2);

View file

@ -96,9 +96,9 @@ char **glob(char *pat, unsigned char *esc,
gl.cur_dir = 1; gl.cur_dir = 1;
gl.pat = CH(gl.m_pat)[0]; gl.pat = CH(gl.m_pat)[0];
if ((gl.esc = UCH(gl.m_pat)[1]) && gl.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 else
dir_research(&gl, "/", gl.pat + 1, 0); dir_research(&gl, "/", gl.pat + 1, 0, 1);
if (!gl.found) if (!gl.found)
ft_ld_pushfront(&gl.match, ft_strdup(CH(gl.m_pat)[0])); ft_ld_pushfront(&gl.match, ft_strdup(CH(gl.m_pat)[0]));
else else

View file

@ -73,7 +73,7 @@ static int match_star(t_glob *gl, char *str, char *full_word)
if (gl->pat[1] == '*' && if (gl->pat[1] == '*' &&
!is_char_esc(gl->esc, ((char **)gl->m_pat->content)[0], 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)) if (!*gl->pat || (*gl->pat == '*' && !*++gl->pat))
return (1); return (1);
pat = gl->pat; pat = gl->pat;
@ -119,7 +119,7 @@ int match_pattern(t_glob *gl, char *str, char *full_word)
else if (*gl->pat == '*') else if (*gl->pat == '*')
return (match_star(gl, str, full_word)); return (match_star(gl, str, full_word));
else if (*gl->pat == '/' && !*str && is_directory(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, 0));
else if (*gl->pat != *str) else if (*gl->pat != *str)
return (0); return (0);
++str; ++str;