ajout de l'expansion du ~ + mise a la norme de command_get_output et correctif appliqué en cas de read invalid

This commit is contained in:
wescande 2017-03-03 14:35:30 +01:00
parent ca1e060463
commit 7a72910a97
5 changed files with 87 additions and 19 deletions

View file

@ -90,6 +90,7 @@ glob/esc_print.c\
glob/expand_bquote.c\ glob/expand_bquote.c\
glob/expand_brace.c\ glob/expand_brace.c\
glob/expand_esc.c\ glob/expand_esc.c\
glob/expand_home.c\
glob/expand_var.c\ glob/expand_var.c\
glob/ft_strsplit_esc.c\ glob/ft_strsplit_esc.c\
glob/ft_strsplit_spe.c\ glob/ft_strsplit_spe.c\

View file

@ -6,7 +6,7 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */ /* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/04 16:31:18 by wescande #+# #+# */ /* Created: 2017/01/04 16:31:18 by wescande #+# #+# */
/* Updated: 2017/02/20 19:03:45 by wescande ### ########.fr */ /* Updated: 2017/03/03 12:10:17 by wescande ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -95,6 +95,7 @@ void modify_esc_split(unsigned char *esc_dest,
unsigned char *esc_src, int start, int len); unsigned char *esc_src, int start, int len);
void expand_brace(t_glob *tglob); void expand_brace(t_glob *tglob);
void expand_bquote(t_glob *gl); void expand_bquote(t_glob *gl);
void expand_home(t_glob *gl, char *str);
void expand_var(t_glob *tglob); void expand_var(t_glob *tglob);
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);

View file

@ -6,24 +6,36 @@
/* 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/02/02 15:16:25 by jhalford ### ########.fr */ /* Updated: 2017/03/03 14:27:40 by wescande ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
#define BUF_SIZE 1024 #define BUF_SIZE 1024
static char *manage_output(int *fds)
{
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);
}
char *command_getoutput(char *command) char *command_getoutput(char *command)
{ {
int fds[2]; int fds[2];
t_btree *ast; t_btree *ast;
t_astnode item; t_astnode item;
char *output;
char buf[BUF_SIZE + 1];
int ret;
t_exec *exec; t_exec *exec;
output = NULL;
exec = &data_singleton()->exec; exec = &data_singleton()->exec;
item.type = TK_SUBSHELL; item.type = TK_SUBSHELL;
item.data.sstr = malloc(4 * sizeof(char *)); item.data.sstr = malloc(4 * sizeof(char *));
@ -37,11 +49,5 @@ char *command_getoutput(char *command)
exec_command(&ast); exec_command(&ast);
exec->process.fdout = STDOUT; exec->process.fdout = STDOUT;
close(fds[PIPE_WRITE]); close(fds[PIPE_WRITE]);
while ((ret = read(fds[PIPE_READ], buf, BUF_SIZE))) return (manage_output(fds));
{
buf[ret] = 0;
ft_strappend(&output, buf);
}
close(fds[PIPE_READ]);
return (output);
} }

View file

@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* expand_home.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/03 14:11:46 by wescande #+# #+# */
/* Updated: 2017/03/03 14:19:22 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
#include "glob.h"
void do_expand_home(t_bquote *me, char *home)
{
char **old_tab;
char **new_tab;
old_tab = CH(*me->wk);
new_tab = gen_tab(ft_strjoin(home, me->str + 1),
calc_expand_esc(me->esc, 0,
(int[2]){ft_strlen(home), 1},
(int[2]){1, ft_strlen(me->str) - 1}),
calc_expand_esc(me->esc2, 0,
(int[2]){ft_strlen(home), 1},
(int[2]){1, ft_strlen(me->str) - 1}), 0);
(*me->wk)->content = new_tab;
ft_tabdel(&old_tab);
}
void expand_home(t_glob *gl, char *home)
{
t_bquote me;
me = (t_bquote){NULL, NULL, NULL, NULL, NULL, NULL, NULL};
gl->m_pat = ft_ld_front(gl->m_pat);
while (gl->m_pat)
{
me.wk = &gl->m_pat;
me.esc = UCH(gl->m_pat)[1];
me.esc2 = UCH(gl->m_pat)[2];
me.str = CH(gl->m_pat)[0];
if (!is_char_esc(me.esc, me.str, me.str) && me.str[0] == '~')
do_expand_home(&me, home);
if (!gl->m_pat->next)
break ;
gl->m_pat = gl->m_pat->next;
}
gl->m_pat = ft_ld_front(gl->m_pat);
}

View file

@ -6,7 +6,7 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */ /* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/04 16:29:54 by wescande #+# #+# */ /* Created: 2017/01/04 16:29:54 by wescande #+# #+# */
/* Updated: 2017/02/20 19:04:44 by wescande ### ########.fr */ /* Updated: 2017/03/03 12:11:17 by wescande ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -34,6 +34,17 @@ static char **treat_glob(t_glob *gl)
return (ret); return (ret);
} }
static void normal_expand_before_match(t_glob *gl)
{
char *home;
expand_var(gl);
expand_bquote(gl);
expand_brace(gl);
if ((home = ft_getenv(data_singleton()->env, "HOME")))
expand_home(gl, home);
}
char **glob(char *pat, unsigned char *esc, char **glob(char *pat, unsigned char *esc,
unsigned char *esc2) unsigned char *esc2)
{ {
@ -43,9 +54,7 @@ char **glob(char *pat, unsigned char *esc,
len = ft_strlen(pat); len = ft_strlen(pat);
gl = (t_glob){0, 0, ft_strdup(pat), dup_char_esc(esc, (len >> 3) + 1), gl = (t_glob){0, 0, ft_strdup(pat), dup_char_esc(esc, (len >> 3) + 1),
dup_char_esc(esc2, (len >> 3) + 1), NULL, NULL}; dup_char_esc(esc2, (len >> 3) + 1), NULL, NULL};
expand_var(&gl); normal_expand_before_match(&gl);
expand_bquote(&gl);
expand_brace(&gl);
while (gl.m_pat && !(gl.found = 0)) while (gl.m_pat && !(gl.found = 0))
{ {
gl.cur_dir = 1; gl.cur_dir = 1;