Merge branch 'master' of https://github.com/jzck/minishell
This commit is contained in:
commit
b7a1f9e92f
7 changed files with 103 additions and 62 deletions
|
|
@ -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,
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
|
|
|
||||||
|
|
@ -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 */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -14,51 +14,83 @@
|
||||||
#define BUF_SIZE 1024
|
#define BUF_SIZE 1024
|
||||||
|
|
||||||
/*static char *manage_output(int *fds)
|
/*static char *manage_output(int *fds)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
char buf[BUF_SIZE + 1];
|
char buf[BUF_SIZE + 1];
|
||||||
char *output;
|
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;
|
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;
|
buf[ret] = 0;
|
||||||
ft_strappend(&output, buf);
|
ft_strappend(&output, buf);
|
||||||
}
|
}
|
||||||
close(fds[PIPE_READ]);
|
DG();
|
||||||
|
close(exec->fdin);
|
||||||
|
DG("done with %s\033[0m", output);
|
||||||
|
|
||||||
return (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); */
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -21,40 +21,44 @@ 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))
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
static void expand_all_bquote(t_bquote *me, char *content,
|
static void expand_all_bquote(t_bquote *me, char *content,
|
||||||
char *ifs)
|
char *ifs)
|
||||||
{
|
{
|
||||||
char *content2;
|
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");
|
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);
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue