diff options
| -rw-r--r-- | src/m_loop.c | 1 | ||||
| -rw-r--r-- | src/p_lblock.c | 6 | ||||
| -rw-r--r-- | src/p_split.c | 194 | ||||
| -rw-r--r-- | src/p_split.h | 9 | 
4 files changed, 41 insertions, 169 deletions
| diff --git a/src/m_loop.c b/src/m_loop.c index 5641686..50357eb 100644 --- a/src/m_loop.c +++ b/src/m_loop.c @@ -44,6 +44,7 @@ static void  			if ((msh->com = s_com_new(ptr->lblock, msh)) == NULL)  				break ;  			e_line(msh); +			s_com_destroy(&msh->com);  		}  		else if (msh->pipes != NULL)  			s_lpipes_clear(&msh->pipes); diff --git a/src/p_lblock.c b/src/p_lblock.c index 856a546..377757d 100644 --- a/src/p_lblock.c +++ b/src/p_lblock.c @@ -133,13 +133,13 @@ int8_t  	i = 0;  	nextif = 0; -	if ((words = p_split_line(line)) == NULL) +	if ((words = p_split_line((char*)line)) == NULL)  		return (-1);  	while (words[i] != NULL)  	{ -		if (words[i][ft_strlen(words[i]) - 1] == ';') +		if (words[i][ft_strlen(words[i]) - 1] == '0')  			nextif = 0; -		else if (words[i][ft_strlen(words[i]) - 1] == '&') +		else if (words[i][ft_strlen(words[i]) - 1] == '1')  			nextif = 1;  		else  			nextif = 2; diff --git a/src/p_split.c b/src/p_split.c index d39a4cb..dc26c04 100644 --- a/src/p_split.c +++ b/src/p_split.c @@ -16,151 +16,15 @@  #include "f_fail.h"  #include "d_define.h" +#include "p_split.h"  #include "s_struct.h"  #include "u_parse.h"  #include "u_utils.h" -/* static size_t */ -/* 	p_count_semi_words(const char line[]) */ -/* { */ -/* 	size_t	count; */ -/* 	size_t	i; */ - -/* 	count = 0; */ -/* 	i = 0; */ -/* 	while (line[i] != '\0') */ -/* 	{ */ -/* 		if (line[i] == ';' && line[i + 1] != ';' && line[i + 1] != '\0' && */ -/* 			u_is_not_escaped(line, line + i) == TRUE) */ -/* 			count += 1; */ -/* 		i++; */ -/* 	} */ -/* 	return (count); */ -/* } */ - -/* static size_t */ -/* 	p_count_and_or_words(const char line[], const char c) */ -/* { */ -/* 	size_t	count; */ -/* 	size_t	i; */ - -/* 	count = 0; */ -/* 	i = 0; */ -/* 	while (line[i] != '\0') */ -/* 	{ */ -/* 		if (line[i] == c && line[i + 1] == c && */ -/* 			line[i + 2] != c && line[i + 2] != '\0') */ -/* 			count += 1; */ -/* 		i++; */ -/* 	} */ -/* 	return (count); */ -/* } */ - -/* static char */ -/* 	*p_get_first_occur(const char *line_ptr) */ -/* { */ -/* 	char	*ptr[3]; */ -/* 	size_t	len[3]; */ - -/* 	ptr[0] = ft_strnstr(line_ptr, ";", ft_strlen(line_ptr) + 1); */ -/* 	ptr[1] = ft_strnstr(line_ptr, "&&", ft_strlen(line_ptr) + 1); */ -/* 	ptr[2] = ft_strnstr(line_ptr, "||", ft_strlen(line_ptr) + 1); */ -/* 	len[0] = ft_strlen(ptr[0]); */ -/* 	len[1] = ft_strlen(ptr[1]); */ -/* 	len[2] = ft_strlen(ptr[2]); */ -/* 	if (len[0] > len[1] && len[0] > len[2]) */ -/* 		return (ptr[0]); */ -/* 	else if (len[1] > len[0] && len[1] > len[2]) */ -/* 		return (ptr[1]); */ -/* 	else if (len[2] > len[0] && len[2] > len[1]) */ -/* 		return (ptr[2]); */ -/* 	return ((char*)line_ptr); */ -/* } */ - -/* static char */ -/* 	**p_split_destroy(char **words, */ -/* 					size_t i) */ -/* { */ -/* 	while (i > 0) */ -/* 	{ */ -/* 		ft_memdel((void*)&words[i]); */ -/* 	} */ -/* 	ft_memdel((void*)&words); */ -/* 	return (NULL); */ -/* } */ - -/* static char */ -/* 	**p_split_to_stuff(const char line[], */ -/* 					const size_t count) */ -/* { */ -/* 	char	**words; */ -/* 	char	*line_ptr; */ -/* 	char	*need_ptr; */ -/* 	size_t	i; */ -/* 	char	c; */ - -/* 	if ((words = (char**)malloc((count + 1) * sizeof(char*))) == NULL) */ -/* 		return (NULL); */ -/* 	line_ptr = (char*)line; */ -/* 	i = 0; */ -/* 	while (i < count) */ -/* 	{ */ -/* 		need_ptr = p_get_first_occur(line_ptr); */ -/* 		c = need_ptr[0]; */ -/* 		need_ptr += (c == ';') ? (1) : (0); */ -/* 		need_ptr += (c == '&') ? (2) : (0); */ -/* 		need_ptr += (c == '|') ? (2) : (0); */ -/* 		if (need_ptr - line_ptr == 0) */ -/* 		{ */ -/* 			if ((words[i] = (char*)malloc(((ft_strlen(line_ptr) + 2) * */ -/* 				sizeof(char)))) == NULL) */ -/* 				return (p_split_destroy(words, i)); */ -/* 			ft_memcpy(words[i], line_ptr, ft_strlen(line_ptr)); */ -/* 			words[i][ft_strlen(line_ptr)] = ';'; */ -/* 			words[i][ft_strlen(line_ptr) + 1] = '\0'; */ -/* 		} */ -/* 		else */ -/* 		{ */ -/* 			if ((words[i] = (char*)malloc(((need_ptr - line_ptr) + 1) * */ -/* 				sizeof(char))) == NULL) */ -/* 				return (p_split_destroy(words, i)); */ -/* 			ft_memcpy(words[i], line_ptr, (need_ptr - line_ptr) - 1); */ -/* 			words[i][(need_ptr - line_ptr) - ((c == ';') ? (1) : (2))] = c; */ -/* 			words[i][(need_ptr - line_ptr) - ((c == ';') ? (0) : (1))] = '\0'; */ -/* 			line_ptr = need_ptr; */ -/* 		} */ -/* 		i++; */ -/* 	} */ -/* 	words[i] = NULL; */ -/* 	return (words); */ -/* } */ - -/* char */ -/* 	**p_split_line(const char line[]) */ -/* { */ -/* 	char	**words; */ -/* 	size_t	count; */ - -/* 	count = p_count_semi_words(line); */ -/* 	count += p_count_and_or_words(line, '&'); */ -/* 	count += p_count_and_or_words(line, '|'); */ -/* 	count += 1; */ -/* 	if ((words = p_split_to_stuff(line, count)) == NULL) */ -/* 		return (NULL); */ -/* 	return (words); */ -/* } */ - -typedef struct	s_split -{ -	size_t		pos[ARG_MAX / 2]; -	int8_t		nextif[ARG_MAX / 2]; -	uint32_t	count; -}				t_split; -  static void  	p_meet_splitter(char *ptr,  					const char line[], -					t_split *sp, +					t_split_block *sp,  					t_quote_mode mode)  {  	if (mode == Q_NONE && *ptr == C_SEMIC && *(ptr + 1) != C_SEMIC && @@ -186,16 +50,15 @@ static void  	}  } -static t_split -	p_fill_sp(const char line[]) +static void +	p_fill_sp(t_split_block *sp, const char line[])  { -	t_split			sp;  	char			*ptr;  	t_quote_mode	mode; -	sp.pos[0] = 0; -	sp.nextif[0] = 0; -	sp.count = 0; +	sp->pos[0] = 0; +	sp->nextif[0] = 0; +	sp->count = 0;  	mode = Q_NONE;  	ptr = (char*)line;  	while (*ptr != C_NUL) @@ -205,13 +68,12 @@ static t_split  		else if (*ptr == C_DQUOTE)  			mode = u_meet_dquote(line, ptr, mode);  		else if (*ptr == C_SEMIC || *ptr == C_AMP || *ptr == C_PIPE) -			p_meet_splitter(ptr, line, &sp, mode); +			p_meet_splitter(ptr, line, sp, mode);  		ptr++;  	} -	sp.pos[sp.count] = ptr - line; -	sp.nextif[sp.count] = 0; -	sp.count += 1; -	return (sp); +	sp->pos[sp->count] = ptr - line; +	sp->nextif[sp->count] = 0; +	sp->count += 1;  }  static void @@ -229,28 +91,30 @@ static void  }  static char -	**p_get_words(const char line[], const t_split sp) +	**p_get_words(const char line[], const t_split_block *sp)  {  	char	**words; -	size_t	i; +	int64_t	i;  	size_t	oldpos;  	int8_t	oldif; -	if ((words = (char**)malloc((sp.count + 1) * sizeof(char*))) == NULL) +	if ((words = (char**)malloc((sp->count + 1) * sizeof(char*))) == NULL)  		return (NULL);  	oldpos = 0;  	oldif = -1; -	i = 0; -	while (i < sp.count) +	i = -1; +	while (++i < sp->count)  	{ -		if ((words[i] = -			(char*)malloc(((sp.pos[i] - oldpos) + 1) * sizeof(char))) == NULL) +		if ((words[i] = (char*)malloc(((sp->pos[i] - oldpos) + 2) * +			sizeof(char))) == NULL)  			return ((char**)p_del_split(words, i)); -		ft_strlcpy(words[i], line + oldpos + ((oldif > 0) ? (2) : (oldif) + 1), sp.pos[i] - oldpos); -		ft_printf("[%s]\n", words[i]); -		oldpos = sp.pos[i]; -		oldif = sp.nextif[i]; -		i++; +		ft_strlcpy(words[i], line + oldpos + ((oldif > 0) ? (2) : (oldif) + 1), +			sp->pos[i] - oldpos + 1 - ((oldif > 0) ? (2) : (1)) + +			((oldif < 0) ? (1) : (0))); +		words[i][ft_strlen(words[i]) + 1] = C_NUL; +		words[i][ft_strlen(words[i])] = sp->nextif[i] + 0x30; +		oldpos = sp->pos[i]; +		oldif = sp->nextif[i];  	}  	words[i] = NULL;  	return (words); @@ -259,12 +123,12 @@ static char  char  	**p_split_line(char line[])  { -	t_split	sp; -	char	**words; +	t_split_block	sp; +	char			**words;  	words = NULL; -	sp = p_fill_sp(line); -	if ((words = p_get_words(line, sp)) == NULL) +	p_fill_sp(&sp, line); +	if ((words = p_get_words(line, &sp)) == NULL)  	{  		ft_memdel((void*)&line);  		return (NULL); diff --git a/src/p_split.h b/src/p_split.h index 648e01c..00da660 100644 --- a/src/p_split.h +++ b/src/p_split.h @@ -13,6 +13,13 @@  #ifndef P_SPLIT_H  #define P_SPLIT_H -char	**p_split_line(const char line[]); +typedef struct	s_split_block +{ +	size_t		pos[ARG_MAX / 2]; +	int8_t		nextif[ARG_MAX / 2]; +	uint32_t	count; +}				t_split_block; + +char	**p_split_line(char line[]);  #endif | 
