diff options
| -rw-r--r-- | src/p_redirs.c | 58 | ||||
| -rw-r--r-- | src/p_redirs.h | 7 | ||||
| -rw-r--r-- | src/s_struct.h | 1 | 
3 files changed, 38 insertions, 28 deletions
| diff --git a/src/p_redirs.c b/src/p_redirs.c index 581ac48..c978212 100644 --- a/src/p_redirs.c +++ b/src/p_redirs.c @@ -17,6 +17,7 @@  #include <limits.h>  #include "d_define.h" +#include "p_redirs.h"  #include "s_lredir.h"  #include "s_struct.h"  #include "s_lredir.h" @@ -37,38 +38,38 @@ static void		p_append_redir(const char path[],  		return ;  	}  	s_lredir_add_back(&com->rdr, new); - +	if (redir == -2) +		p_get_heredoc(path, msh->fd);  }  static size_t	p_get_path(char path[], -						char *ptr, -						const char word[], +						struct s_rdr_tmp tmp,  						t_msh *msh)  { -	char	home[PATH_MAX]; +	char	h[PATH_MAX];  	size_t	pos[2];  	size_t	hlen;  	size_t	len;  	hlen = 0; -	while (ft_ischarset("<>", *ptr) == TRUE) -		ptr++; -	while (*ptr != C_NUL && ft_iswhitespace(*ptr) == TRUE) -		ptr++; -	if (*ptr == C_TILDE && u_get_var_value(home, "$HOME", PATH_MAX, msh) == 0) +	tmp.redir = (tmp.redir < 0) ? (-tmp.redir) : (tmp.redir); +	tmp.ptr += tmp.redir; +	while (*tmp.ptr != C_NUL && ft_iswhitespace(*tmp.ptr) == TRUE) +		tmp.ptr++; +	if (*tmp.ptr == C_TILDE && u_get_var_value(h, "$HOME", PATH_MAX, msh) == 0)  	{ -		ft_strlcpy(path, home, PATH_MAX); -		hlen = ft_strlen(home); -		ptr++; +		ft_strlcpy(path, h, PATH_MAX); +		hlen = ft_strlen(h); +		tmp.ptr++;  	} -	pos[0] = (ptr - word); -	while (*ptr != C_NUL && ft_iswhitespace(*ptr) == FALSE && -		u_is_not_escaped(word, ptr) == TRUE) -		ptr++; -	pos[1] = (ptr - word); +	pos[0] = (tmp.ptr - tmp.word); +	while (*tmp.ptr != C_NUL && ft_iswhitespace(*tmp.ptr) == FALSE && +		u_is_not_escaped(tmp.word, tmp.ptr) == TRUE) +		tmp.ptr++; +	pos[1] = (tmp.ptr - tmp.word);  	len = (pos[1] - pos[0]);  	len = ((hlen + len + 1) > PATH_MAX) ? (PATH_MAX - 1) : (len); -	ft_strlcpy(path + hlen, word + pos[0], len + 1); +	ft_strlcpy(path + hlen, tmp.word + pos[0], len + 1);  	return (pos[1]);  } @@ -95,15 +96,15 @@ static int32_t	p_get_fd(const char word[], char *ptr)  	return (ft_atoi(digit));  } -static int8_t	p_get_redir(char word[], char *ptr, t_com *com, t_msh *msh) +static void		p_get_redir(char word[], char *ptr, t_com *com, t_msh *msh)  { -	char	path[PATH_MAX]; -	size_t	pos[2]; -	int32_t	fd; -	int8_t	redir; +	struct s_rdr_tmp	tmp; +	char				path[PATH_MAX]; +	size_t				pos[2]; +	int32_t				fd; +	int8_t				redir;  	pos[0] = 0; -	pos[1] = 0;  	if ((fd = p_get_fd(word, ptr)) <= 0)  		fd = STDOUT_FILENO;  	redir = (*ptr == '>') ? (1) : (-1); @@ -113,11 +114,13 @@ static int8_t	p_get_redir(char word[], char *ptr, t_com *com, t_msh *msh)  		pos[0] = (ptr - word);  	else  		pos[0] = (ptr - word) - ft_intlen(fd); -	pos[1] = p_get_path(path, ptr, word, msh); +	tmp.word = word; +	tmp.ptr = ptr; +	tmp.redir = redir; +	pos[1] = p_get_path(path, tmp, msh);  	(void)ft_memmove(word + pos[0],  		word + pos[1], (ft_strlen(word + pos[1]) + 1) * sizeof(char));  	p_append_redir(path, fd, redir, com); -	return (0);  }  int8_t			p_redirs(char word[], t_com **com, t_msh *msh) @@ -135,8 +138,7 @@ int8_t			p_redirs(char word[], t_com **com, t_msh *msh)  			mode = u_meet_squote(word, ptr, mode);  		else if (mode == Q_NONE && (*ptr == '<' || *ptr == '>') == 1)  		{ -			if (p_get_redir(word, ptr, *com, msh) != 0) -				return (1); +			p_get_redir(word, ptr, *com, msh);  			ptr = word;  		}  		ptr++; diff --git a/src/p_redirs.h b/src/p_redirs.h index 67e8ab1..3845a65 100644 --- a/src/p_redirs.h +++ b/src/p_redirs.h @@ -17,6 +17,13 @@  #include "s_struct.h" +struct s_rdr_tmp +{ +	const char	*word; +	char		*ptr; +	int8_t		redir; +}; +  int8_t	p_redirs(char word[], t_com **com, t_msh *msh);  #endif diff --git a/src/s_struct.h b/src/s_struct.h index 0b83158..37d6726 100644 --- a/src/s_struct.h +++ b/src/s_struct.h @@ -47,6 +47,7 @@ typedef struct			s_lalias  typedef struct			s_lredir  {  	char				path[PATH_MAX]; +	char				*heredoc;  	int32_t				fd;  	int8_t				redir;      struct s_lredir		*next; | 
