From bea195a2e8d8e26af7511a65af4c8641cb4d1c4f Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 10 Sep 2020 17:28:06 +0200 Subject: Even better parse --- src/p_args.c | 4 +-- src/p_args_len.c | 93 ++++++++++++++--------------------------------------- src/p_args_next.c | 3 +- src/p_lblock_next.c | 12 +++---- src/s_com.c | 2 +- src/u_vars_next.c | 22 +++++++++---- 6 files changed, 49 insertions(+), 87 deletions(-) diff --git a/src/p_args.c b/src/p_args.c index a4b3d02..5276ae1 100644 --- a/src/p_args.c +++ b/src/p_args.c @@ -69,9 +69,9 @@ static uint16_t if (*ptr == C_BACKS) p_meet_bs(ptr, mode); if (*ptr == C_DQUOTE) - mode = u_meet_dquote((char*)word, ptr, mode); + mode = u_meet_dquote(word, ptr, mode); else if (*ptr == C_SQUOTE) - mode = u_meet_squote((char*)word, ptr, mode); + mode = u_meet_squote(word, ptr, mode); if (ft_iswhitespace(*ptr) && p_meet_whitespace((char*)word, ptr, mode) == TRUE) { diff --git a/src/p_args_len.c b/src/p_args_len.c index 801406f..3e5bfbf 100644 --- a/src/p_args_len.c +++ b/src/p_args_len.c @@ -14,87 +14,42 @@ #include #include "d_define.h" +#include "u_parse.h" +#include "u_utils.h" -static size_t - p_skip_delim_size(const char word[], char c, size_t end) +static t_bool + p_meet_whitespace(const char *head, char *ptr, t_quote_mode mode) { - end++; - if (word[end] == c) - return (end + 1); - while (word[end] != C_NUL && word[end] != c) + if (mode == Q_NONE && u_is_not_escaped(head, ptr) == TRUE) { - end++; - if (word[end] == c && c == C_DQUOTE) - { - if (word[end - 1] == C_BACKS && word[end - 2] != C_BACKS) - { - end++; - } - } + return (TRUE); } - if (word[end] != C_NUL) - { - end++; - } - return (end); -} - -static size_t - p_skip_unquote(const char word[], size_t end) -{ - while (word[end] != C_NUL && ft_iswhitespace(word[end]) == FALSE) - { - end++; - if (ft_iswhitespace(word[end]) == TRUE && end == 1 && - word[end - 1] == C_BACKS) - { - end++; - } - else if (ft_iswhitespace(word[end]) == TRUE && end > 1 && - word[end - 1] == C_BACKS && word[end - 2] != C_BACKS) - { - end++; - } - } - return (end); -} - -static size_t - p_skip_quote(const char word[], size_t end) -{ - while (word[end] != C_NUL && ft_iswhitespace(word[end]) == FALSE) - { - if (word[end] == C_SQUOTE || word[end] == C_DQUOTE) - { - end = p_skip_delim_size(word, word[end], end); - } - else - { - while (word[end] != C_NUL && - ft_iswhitespace(word[end]) == FALSE) - { - end++; - } - } - } - return (end); + return (FALSE); } size_t p_arg_len(const char word[], const size_t start) { - size_t end; + t_quote_mode mode; + char *ptr; + size_t end; + t_bool terminate; + mode = Q_NONE; end = start; - if (word[start] != C_SQUOTE && word[start] != C_DQUOTE && - word[start] != C_NUL) - { - end = p_skip_unquote(word, end); - } - else if (word[end] == C_SQUOTE || word[end] == C_DQUOTE) + ptr = (char*)word + start; + terminate = FALSE; + while (*ptr != C_NUL && terminate == FALSE) { - end = p_skip_quote(word, end); + if (*ptr == C_DQUOTE) + mode = u_meet_dquote(word, ptr, mode); + else if (*ptr == C_SQUOTE) + mode = u_meet_squote(word, ptr, mode); + else if (ft_iswhitespace(*ptr)) + terminate = p_meet_whitespace(word, ptr, mode); + ptr++; } - return (end); + ptr -= 1; + return (ptr - word); } diff --git a/src/p_args_next.c b/src/p_args_next.c index 6fd6b08..ea8dd81 100644 --- a/src/p_args_next.c +++ b/src/p_args_next.c @@ -12,6 +12,7 @@ #include #include +#include #include "d_define.h" #include "p_args.h" @@ -49,7 +50,7 @@ uint16_t const uint16_t argc, const size_t start[]) { - char tmp[4096]; + char tmp[ARG_MAX]; uint16_t i; i = 0; diff --git a/src/p_lblock_next.c b/src/p_lblock_next.c index 5777a3c..56a789d 100644 --- a/src/p_lblock_next.c +++ b/src/p_lblock_next.c @@ -59,7 +59,8 @@ char { if (*ptr == '$' && u_is_not_escaped(word, ptr) == TRUE) { - if ((word = p_subst_this_var(&ptr, (ptr - word), word, msh)) == NULL) + if ((word = p_subst_this_var(&ptr, (ptr - word), word, msh)) + == NULL) { return (NULL); } @@ -70,8 +71,7 @@ char } char - **p_subst_home(char *words[], - t_msh *msh) + **p_subst_home(char *words[], t_msh *msh) { char path[PATH_MAX]; char **ptr; @@ -92,8 +92,7 @@ char } static void - p_register_word(char word[], - t_msh *msh) + p_register_word(char word[], t_msh *msh) { char name[255]; char val[255]; @@ -182,8 +181,7 @@ static void } char - **p_check_args_equals(char *words[], - t_msh *msh) + **p_check_args_equals(char *words[], t_msh *msh) { char *ptr; t_bool reg; diff --git a/src/s_com.c b/src/s_com.c index 5bdb36a..0f206c4 100644 --- a/src/s_com.c +++ b/src/s_com.c @@ -118,9 +118,9 @@ t_com return (NULL); if ((words = p_split_args(word, com->redir)) == NULL) return (NULL); + words = p_check_args_equals(words, msh); if ((words = p_subst_home(words, msh)) == NULL) return (NULL); - words = p_check_args_equals(words, msh); if (msh->env_fork_tmp[0][0] != '\0') com_cpy_env_fork(&com, msh); if (fill_com(words, &com) < 0) diff --git a/src/u_vars_next.c b/src/u_vars_next.c index c6c351b..6f63fb4 100644 --- a/src/u_vars_next.c +++ b/src/u_vars_next.c @@ -13,10 +13,12 @@ #include #include #include +#include #include "d_enum.h" #include "s_lvars.h" #include "s_struct.h" +#include "u_parse.h" #include "u_utils.h" static int64_t @@ -63,6 +65,11 @@ static t_bool } } +/* static void */ +/* u_newval_subst(char newval_subst[], const char newval[]) */ +/* { */ +/* } */ + /* ** void ** u_subst_var_value(const char varname[], const char newval[], t_msh *msh); @@ -70,23 +77,24 @@ static t_bool ** DESCRIPTION ** The subst_var_value() changes the value of msh->envp ** variable varname[] with newval[]. If varname[] wasn't found -** in msh->envp, varname[] is searched in msh->vars. +** in msh->envp, varname[] is searched in msh->vars. Otherwise +** varname[] is added to msh->vars. */ void - u_subst_var_value(const char varname[], - const char newval[], - t_msh *msh) + u_subst_var_value(const char varname[], const char newval[], t_msh *msh) { - char new_line_fmt[2048]; + char new_line_fmt[ARG_MAX]; + /* char newval_subst[ARG_MAX]; */ int64_t env_line; if ((env_line = u_get_env_var_line(varname + 1, msh)) > -1) { ft_memdel((void*)&msh->envp[env_line]); + /* ft_sprintf(new_line_fmt, "%s=%s", varname + 1, newval_subst); */ ft_sprintf(new_line_fmt, "%s=%s", varname + 1, newval); - if (!(msh->envp[env_line] = - (char*)malloc((ft_strlen(new_line_fmt) + 1) * sizeof(char)))) + if ((msh->envp[env_line] = (char*)malloc((ft_strlen(new_line_fmt) + 1) * + sizeof(char))) == NULL) { /* TODO: Handle this fail */ } -- cgit v1.2.3