diff options
author | salad <fmoenne-@student.le-101.fr> | 2020-10-26 13:42:56 +0100 |
---|---|---|
committer | salad <fmoenne-@student.le-101.fr> | 2020-10-26 13:42:56 +0100 |
commit | 0277ddfac754ab4ad5bdd2b692e31a717efbe569 (patch) | |
tree | 49d7c5fd3a12248af85e2c3a3254bc1538ae5775 /src/p_split.c | |
parent | reqdy for MERGE (diff) | |
parent | TODO update (diff) | |
download | 42-minishell-0277ddfac754ab4ad5bdd2b692e31a717efbe569.tar.gz 42-minishell-0277ddfac754ab4ad5bdd2b692e31a717efbe569.tar.bz2 42-minishell-0277ddfac754ab4ad5bdd2b692e31a717efbe569.tar.xz 42-minishell-0277ddfac754ab4ad5bdd2b692e31a717efbe569.tar.zst 42-minishell-0277ddfac754ab4ad5bdd2b692e31a717efbe569.zip |
merge wif master
Diffstat (limited to 'src/p_split.c')
-rw-r--r-- | src/p_split.c | 177 |
1 files changed, 86 insertions, 91 deletions
diff --git a/src/p_split.c b/src/p_split.c index bbe9bf5..dc26c04 100644 --- a/src/p_split.c +++ b/src/p_split.c @@ -12,68 +12,77 @@ #include <libft.h> #include <stdlib.h> +#include <limits.h> -static size_t - p_count_semi_words(const char line[]) -{ - size_t count; - size_t i; +#include "f_fail.h" +#include "d_define.h" +#include "p_split.h" +#include "s_struct.h" +#include "u_parse.h" +#include "u_utils.h" - count = 0; - i = 0; - while (line[i] != '\0') +static void + p_meet_splitter(char *ptr, + const char line[], + t_split_block *sp, + t_quote_mode mode) +{ + if (mode == Q_NONE && *ptr == C_SEMIC && *(ptr + 1) != C_SEMIC && + u_is_not_escaped(line, ptr) == TRUE) + { + sp->pos[sp->count] = (ptr - line); + sp->nextif[sp->count] = 0; + sp->count += 1; + } + else if (mode == Q_NONE && *ptr == C_AMP && *(ptr + 1) == C_AMP && + (*ptr + 2) != C_PIPE && u_is_not_escaped(line, ptr) == TRUE) { - if (line[i] == ';' && line[i + 1] != ';' && line[i + 1] != '\0') - count += 1; - i++; + sp->pos[sp->count] = (ptr - line); + sp->nextif[sp->count] = 1; + sp->count += 1; + } + else if (mode == Q_NONE && *ptr == C_PIPE && *(ptr + 1) == C_PIPE && + *(ptr + 2) != C_PIPE && u_is_not_escaped(line, ptr) == TRUE) + { + sp->pos[sp->count] = (ptr - line); + sp->nextif[sp->count] = 2; + sp->count += 1; } - return (count); } -static size_t - p_count_and_or_words(const char line[], const char c) +static void + p_fill_sp(t_split_block *sp, const char line[]) { - size_t count; - size_t i; + char *ptr; + t_quote_mode mode; - count = 0; - i = 0; - while (line[i] != '\0') + sp->pos[0] = 0; + sp->nextif[0] = 0; + sp->count = 0; + mode = Q_NONE; + ptr = (char*)line; + while (*ptr != C_NUL) { - if (line[i] == c && line[i + 1] == c && - line[i + 2] != c && line[i + 2] != '\0') - count += 1; - i++; + if (*ptr == C_SQUOTE) + mode = u_meet_squote(line, ptr, mode); + 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); + ptr++; } - return (count); + sp->pos[sp->count] = ptr - line; + sp->nextif[sp->count] = 0; + sp->count += 1; } -static char - *p_get_first_occur(const char *line_ptr) +static void + *p_del_split(char *words[], size_t todel) { - 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); -} + size_t i; -static char - **p_split_destroy(char **words, - size_t i) -{ - while (i > 0) + i = 0; + while (i < todel) { ft_memdel((void*)&words[i]); } @@ -82,62 +91,48 @@ static char } static char - **p_split_to_stuff(const char line[], - const size_t count) + **p_get_words(const char line[], const t_split_block *sp) { char **words; - char *line_ptr; - char *need_ptr; - size_t i; - char c; + int64_t i; + size_t oldpos; + int8_t oldif; - if ((words = (char**)malloc((count + 1) * sizeof(char*))) == NULL) + if ((words = (char**)malloc((sp->count + 1) * sizeof(char*))) == NULL) return (NULL); - line_ptr = (char*)line; - i = 0; - while (i < count) + oldpos = 0; + oldif = -1; + i = -1; + while (++i < sp->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++; + 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 + 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); } char - **p_split_line(const char line[]) + **p_split_line(char line[]) { - char **words; - size_t count; + t_split_block sp; + char **words; - 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) + words = NULL; + p_fill_sp(&sp, line); + if ((words = p_get_words(line, &sp)) == NULL) + { + ft_memdel((void*)&line); return (NULL); + } + ft_memdel((void*)&line); return (words); } |