diff options
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | src/p_lblock.c | 4 | ||||
-rw-r--r-- | src/s_lpipes.c | 26 | ||||
-rw-r--r-- | src/s_lpipes_split.c | 40 | ||||
-rw-r--r-- | src/s_lpipes_split.h | 2 |
5 files changed, 54 insertions, 20 deletions
@@ -70,7 +70,7 @@ SRCS_NAME += s_lalias SRCS_NAME += s_line SRCS_NAME += s_lvars SRCS_NAME += s_lpipes -# SRCS_NAME += s_lpipes_split +SRCS_NAME += s_lpipes_split SRCS_NAME += s_lredir SRCS_NAME += p_args SRCS_NAME += p_args_next diff --git a/src/p_lblock.c b/src/p_lblock.c index 369eb5a..b8341cd 100644 --- a/src/p_lblock.c +++ b/src/p_lblock.c @@ -23,8 +23,8 @@ #include "p_split.h" #include "s_line.h" #include "s_lpipes.h" -#include "u_parse.h" #include "s_struct.h" +#include "u_parse.h" #include "u_utils.h" static uint8_t p_get_nextif(char *words[], size_t i) @@ -54,7 +54,7 @@ static t_bool p_find_good_pipe(const char word[]) if (*ptr == C_PIPE) { if (mode == Q_NONE && u_is_not_escaped(word, ptr) == TRUE - && *(ptr + 1) != C_PIPE) + && *(ptr + 1) != C_PIPE && *(ptr - 1) != C_PIPE) return (TRUE); } if (*ptr == C_DQUOTE) diff --git a/src/s_lpipes.c b/src/s_lpipes.c index 23d0476..22ed2a5 100644 --- a/src/s_lpipes.c +++ b/src/s_lpipes.c @@ -10,11 +10,15 @@ /* */ /* ************************************************************************** */ -#include <cstddef> #include <libft.h> #include <stdlib.h> #include <stdint.h> #include <sys/types.h> +#ifdef __linux__ +# include <linux/limits.h> +#else +# include <limits.h> +#endif #include "s_com.h" #include "s_line.h" @@ -76,30 +80,24 @@ struct s_lpipes *s_lpipes_new(const char pipedword[], t_msh *msh) return (link); } -/* -** TODO: Dangerous '|' split -*/ - struct s_lpipes *s_split_pipes(const char word[], t_msh *msh) { struct s_lpipes *lpipes; size_t pos[256]; - size_t i; + char tmp[ARG_MAX]; + int16_t i; ft_bzero(pos, 256 * sizeof(size_t)); s_get_split_pos(pos, word); - - i = 0; - while (words[i] != NULL) + while (++i < 256) { - if ((lpipes = s_lpipes_new(words[i], msh)) == NULL) - { + s_set_tmp(tmp, pos, i, word); + if (((lpipes = s_lpipes_new(tmp, msh))) == NULL) return (NULL); - } s_lpipes_add_back(&msh->pipes, lpipes); - i++; + if (pos[i] == 0) + break ; } - ft_delwords(words); return (lpipes); } diff --git a/src/s_lpipes_split.c b/src/s_lpipes_split.c index da23f47..4da4a03 100644 --- a/src/s_lpipes_split.c +++ b/src/s_lpipes_split.c @@ -13,20 +13,54 @@ #include <libft.h> #include <stdlib.h> #include <stdint.h> +#ifdef __linux__ +# include <linux/limits.h> +#else +# include <limits.h> +#endif #include "d_define.h" #include "d_enum.h" +#include "u_parse.h" +#include "u_utils.h" + +void s_set_tmp(char tmp[], size_t pos[], int16_t i, const char word[]) +{ + if (pos[i] != 0) + ft_strlcpy(tmp, + word + ((pos[i - 1] == 0) ? (0) : (pos[i - 1] + 1)), + pos[i] - ((pos[i - 1] == 0) ? (0) : (pos[i - 1])) + + ((pos[i - 1] == 0) ? (1) : (0))); + else + ft_strlcpy(tmp, + word + ((pos[i - 1] == 0) ? (0) : (pos[i - 1] + 1)), + ARG_MAX - pos[i] + 1); +} void s_get_split_pos(size_t pos[], const char word[]) { char *ptr; t_quote_mode mode; - uint8_t i; + uint16_t i; ptr = (char*)word; mode = Q_NONE; - i = 0; - while (*ptr != C_NUL) + i = 1; + while (*ptr != C_NUL && (uint16_t)i < 256) { + if (*ptr == C_PIPE) + { + if (mode == Q_NONE && u_is_not_escaped(word, ptr) == TRUE + && *(ptr + 1) != C_PIPE && *(ptr - 1) != C_PIPE) + { + pos[i] = (ptr - word); + i++; + } + } + if (*ptr == C_DQUOTE) + mode = u_meet_dquote(word, ptr, mode); + else if (*ptr == C_SQUOTE) + mode = u_meet_squote(word, ptr, mode); + ptr++; } } diff --git a/src/s_lpipes_split.h b/src/s_lpipes_split.h index 3a79dba..aa978f4 100644 --- a/src/s_lpipes_split.h +++ b/src/s_lpipes_split.h @@ -14,7 +14,9 @@ # define S_LPIPES_SPLIT_H # include <stddef.h> +# include <stdint.h> +void s_set_tmp(char tmp[], size_t pos[], int16_t i, const char word[]); void s_get_split_pos(size_t pos[], const char word[]); #endif |