From c75434256658e3009a5c61d02524cb49492bd120 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sat, 5 Sep 2020 18:40:28 +0200 Subject: Clean af --- src/m_loop_next.c | 2 +- src/p_args.c | 121 +++++++++++++++++++++++++++------------------------- src/p_args.h | 4 -- src/p_args_escape.c | 32 ++++++++------ src/p_line.c | 17 -------- src/u_utils.c | 2 +- src/u_utils.h | 2 +- 7 files changed, 86 insertions(+), 94 deletions(-) (limited to 'src') diff --git a/src/m_loop_next.c b/src/m_loop_next.c index 3bc85e8..fb95236 100644 --- a/src/m_loop_next.c +++ b/src/m_loop_next.c @@ -199,7 +199,7 @@ static t_bool { if (*ptr == C_SQUOTE || *ptr == C_DQUOTE) { - if (u_is_true_quote(line, ptr) == TRUE) + if (u_is_not_escaped(line, ptr) == TRUE) { mode = (*ptr == C_SQUOTE) ? (Q_SINGLE) : (Q_DOUBLE); if (m_find_next_quote(&ptr, line, mode) == FALSE) diff --git a/src/p_args.c b/src/p_args.c index fa01472..f3e7403 100644 --- a/src/p_args.c +++ b/src/p_args.c @@ -18,6 +18,7 @@ #include "p_args.h" #include "p_args_next.h" #include "p_args_escape.h" +#include "u_utils.h" /* ================= */ /* TODO: DELETE THIS */ @@ -40,82 +41,87 @@ p_print(char *words[]) /* TODO: DELETE ABOVE */ /* ================== */ -static char - *p_skip_delim(char *ptr, char c) +static t_quote_mode + p_meet_dquote(char *head, char *ptr, t_quote_mode mode) { - ptr++; - if (*ptr == c) - return (++ptr); - while (*ptr != C_NULL && *ptr != c) + if (mode == Q_NONE) { - ptr++; - if (*ptr == c && c == C_DQUOTE) + if (u_is_not_escaped(head, ptr) == TRUE) { - if (*(ptr - 1) == C_BS) - { - if (*(ptr - 2) != C_BS) - ptr++; - } + return (Q_DOUBLE); + } + else + { + return (Q_NONE); } } - if (*ptr != C_NULL) - ptr++; - return (ptr); + else if (mode == Q_DOUBLE && u_is_not_escaped(head, ptr) == TRUE) + { + return (Q_NONE); + } + return (mode); } -static int32_t - p_skip_clean_arg(const char *head, - char **ptr, - uint16_t argc, - size_t start[]) +static t_quote_mode + p_meet_squote(char *head, char *ptr, t_quote_mode mode) { - while (*(*ptr) != C_NULL && ft_iswhitespace(*(*ptr)) == FALSE) + if (mode == Q_NONE) { - (*ptr)++; - if ((*(*ptr) == C_SQUOTE || *(*ptr) == C_DQUOTE) && - *(*ptr - 1) != C_BS) + if (u_is_not_escaped(head, ptr) == TRUE) { - (*ptr) += 1; - return (p_count_args(head, *ptr, argc, start)); + return (Q_SINGLE); } - if (ft_iswhitespace(*(*ptr)) == TRUE && *(*ptr - 1) == C_BS && - *(*ptr - 2) != C_BS) + else { - (*ptr) += 1; - return (-1); + return (Q_NONE); } } - return (0); + else if (mode == Q_SINGLE) + { + return (Q_NONE); + } + return (mode); } -uint16_t - p_count_args(const char *head, char *ptr, uint16_t argc, size_t start[]) +static t_bool + p_meet_whitespace(char *head, char *ptr, t_quote_mode mode) { - static int32_t preset = -1; - - if (*ptr == C_NULL) - return (argc); - while (*ptr != C_NULL && ft_iswhitespace(*ptr) == TRUE) - ptr++; - if (preset != argc) - start[argc] = ptr - head; - preset = argc; - if (*ptr != C_SQUOTE && *ptr != C_DQUOTE && *ptr != C_NULL) + if (mode == Q_NONE && u_is_not_escaped(head, ptr) == TRUE) { - if (p_skip_clean_arg(head, &ptr, argc, start) == -1) - argc -= 1; + return (TRUE); } - else if (*ptr == C_SQUOTE || *ptr == C_DQUOTE) + return (FALSE); +} + +static uint16_t + p_count_args(const char word[], size_t start[]) +{ + char *ptr; + t_quote_mode mode; + uint16_t count; + + (void)start; + ptr = (char *)word; + mode = Q_NONE; + count = 0; + while (*ptr != C_NULL) { - ptr = p_skip_delim(ptr, *ptr); - if (*ptr == C_NULL) - return (argc += 1); - else if (ft_iswhitespace(*ptr) == FALSE) - return (p_count_args(head, ptr, argc, start)); + if (*ptr == C_DQUOTE) + mode = p_meet_dquote((char*)word, ptr, mode); + else if (*ptr == C_SQUOTE) + mode = p_meet_squote((char*)word, ptr, mode); + if (ft_iswhitespace(*ptr) && + p_meet_whitespace((char*)word, ptr, mode) == TRUE) + { + count += 1; + while (*ptr != C_NULL && ft_iswhitespace(*ptr)) + ptr++; + ptr -= 1; + ft_printf("[%s]\n", ptr); + } + ptr++; } - else if (*ptr == C_NULL) - return (argc); - return (p_count_args(head, ptr, argc + 1, start)); + return (count); /* TODO: quotes parse error */ } @@ -123,14 +129,13 @@ static char **p_split_words_no_rdr(const char word[]) { char **words; - char *ptr; size_t start[512]; uint16_t argc; uint16_t to_del; - ptr = (char*)word; - argc = p_count_args(word, ptr, 0, start); + argc = p_count_args(word, start); ft_printf("argc: %hu\n", argc); + exit(0); if ((words = (char**)malloc((argc + 1) * sizeof(char*))) == NULL) return (NULL); words[argc] = NULL; diff --git a/src/p_args.h b/src/p_args.h index 27f5729..899f7ea 100644 --- a/src/p_args.h +++ b/src/p_args.h @@ -15,10 +15,6 @@ #include -uint16_t p_count_args(const char *head, - char *ptr, - uint16_t argc, - size_t start[]); char **p_split_args(const char word[], int8_t redir); #endif diff --git a/src/p_args_escape.c b/src/p_args_escape.c index 182ef2f..2df2e0f 100644 --- a/src/p_args_escape.c +++ b/src/p_args_escape.c @@ -21,9 +21,16 @@ static t_quote_mode { if (mode == Q_NONE) { - (void)ft_memmove(word + (ptr - word), ptr + 1, - (ft_strlen(ptr + 1) + 1) * sizeof(char)); - return (Q_SINGLE); + if (u_is_not_escaped(word, ptr) == TRUE) + { + (void)ft_memmove(word + (ptr - word), ptr + 1, + (ft_strlen(ptr + 1) + 1) * sizeof(char)); + return (Q_SINGLE); + } + else + { + return (Q_NONE); + } } else if (mode == Q_SINGLE) { @@ -39,20 +46,21 @@ static t_quote_mode { if (mode == Q_NONE) { - if (u_is_true_quote(word, ptr) == TRUE) + if (u_is_not_escaped(word, ptr) == TRUE) { (void)ft_memmove(word + (ptr - word), ptr + 1, (ft_strlen(ptr + 1) + 1) * sizeof(char)); + return (Q_DOUBLE); } - return (Q_DOUBLE); - } - else if (mode == Q_DOUBLE) - { - if (u_is_true_quote(word, ptr) == TRUE) + else { - (void)ft_memmove(word + (ptr - word), ptr + 1, - (ft_strlen(ptr + 1) + 1) * sizeof(char)); + return (Q_NONE); } + } + else if (mode == Q_DOUBLE && u_is_not_escaped(word, ptr) == TRUE) + { + (void)ft_memmove(word + (ptr - word), ptr + 1, + (ft_strlen(ptr + 1) + 1) * sizeof(char)); return (Q_NONE); } return (mode); @@ -118,8 +126,8 @@ void { /* TODO: escape \ */ /* TODO: escape $# special vars */ + /* TODO: comments ################ */ /* TODO: escape my life */ - /* TODO: comments */ char **ptr; ptr = words; diff --git a/src/p_line.c b/src/p_line.c index 0224bbb..9c96610 100644 --- a/src/p_line.c +++ b/src/p_line.c @@ -37,22 +37,6 @@ static void } } -static void - p_subst_spaces(char line[]) -{ - char *ptr; - - ptr = line; - while (*ptr != '\0') - { - if (ft_iswhitespace(*ptr) == TRUE) - { - *ptr = ' '; - } - ptr++; - } -} - static t_bool p_check_whitespaces_only(char line[]) { @@ -75,7 +59,6 @@ void t_msh *msh) { p_delete_comments(line); - p_subst_spaces(line); if (p_check_whitespaces_only(line) == TRUE) { return ; diff --git a/src/u_utils.c b/src/u_utils.c index d554a98..fb1dcf6 100644 --- a/src/u_utils.c +++ b/src/u_utils.c @@ -20,7 +20,7 @@ #include "s_struct.h" t_bool - u_is_true_quote(char *head, char *ptr) + u_is_not_escaped(char *head, char *ptr) { if (((ptr - head) == 0) || ((ptr - head) >= 1 && *(ptr - 1) != C_BS) || diff --git a/src/u_utils.h b/src/u_utils.h index 1b83d65..2aa2c72 100644 --- a/src/u_utils.h +++ b/src/u_utils.h @@ -18,7 +18,7 @@ #include "s_struct.h" -t_bool u_is_true_quote(char *head, char *ptr); +t_bool u_is_not_escaped(char *head, char *ptr); void u_eof_fd(int32_t fd); uint64_t u_builtins_get_argc(const char *args[]); char **u_get_env_var_names(t_msh *msh); -- cgit v1.2.3