diff --git a/42sh/Makefile b/42sh/Makefile index 4fd57af5..e8a964d8 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -90,6 +90,7 @@ glob/esc_print.c\ glob/expand_bquote.c\ glob/expand_brace.c\ glob/expand_esc.c\ +glob/expand_home.c\ glob/expand_var.c\ glob/ft_strsplit_esc.c\ glob/ft_strsplit_spe.c\ diff --git a/42sh/includes/glob.h b/42sh/includes/glob.h index 33019718..80287b48 100644 --- a/42sh/includes/glob.h +++ b/42sh/includes/glob.h @@ -6,7 +6,7 @@ /* 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); void expand_brace(t_glob *tglob); void expand_bquote(t_glob *gl); +void expand_home(t_glob *gl, char *str); void expand_var(t_glob *tglob); int match_pattern(t_glob *tglob, char *str, char *full_word); int dir_research(t_glob *tglob, char *p, char *pat, int rec); diff --git a/42sh/src/glob/command_getoutput.c b/42sh/src/glob/command_getoutput.c index 114216c8..db94d540 100644 --- a/42sh/src/glob/command_getoutput.c +++ b/42sh/src/glob/command_getoutput.c @@ -6,24 +6,36 @@ /* 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" #define BUF_SIZE 1024 -char *command_getoutput(char *command) +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) { 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 = TK_SUBSHELL; item.data.sstr = malloc(4 * sizeof(char *)); @@ -37,11 +49,5 @@ char *command_getoutput(char *command) 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); + return (manage_output(fds)); } diff --git a/42sh/src/glob/expand_home.c b/42sh/src/glob/expand_home.c new file mode 100644 index 00000000..f6619624 --- /dev/null +++ b/42sh/src/glob/expand_home.c @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* expand_home.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/42sh/src/glob/glob.c b/42sh/src/glob/glob.c index d5832efd..ee862418 100644 --- a/42sh/src/glob/glob.c +++ b/42sh/src/glob/glob.c @@ -6,7 +6,7 @@ /* 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 */ /* */ /* ************************************************************************** */ @@ -26,7 +26,7 @@ static char **treat_glob(t_glob *gl) { char **ret; - ret= NULL; + ret = NULL; ft_ld_clear(&gl->m_pat, &ft_tabdel); ft_ld_reverse(&gl->match); ret = ft_ld_to_tab(gl->match); @@ -34,6 +34,17 @@ static char **treat_glob(t_glob *gl) 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, unsigned char *esc2) { @@ -43,9 +54,7 @@ char **glob(char *pat, unsigned char *esc, len = ft_strlen(pat); gl = (t_glob){0, 0, ft_strdup(pat), dup_char_esc(esc, (len >> 3) + 1), dup_char_esc(esc2, (len >> 3) + 1), NULL, NULL}; - expand_var(&gl); - expand_bquote(&gl); - expand_brace(&gl); + normal_expand_before_match(&gl); while (gl.m_pat && !(gl.found = 0)) { gl.cur_dir = 1;