From b2ffd2aeb4a91501ae7931cb43a9679b01eb0351 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Fri, 11 Sep 2020 16:25:51 +0200 Subject: Nice fix --- TODO.org | 3 ++- src/d_define.h | 1 + src/p_args_len.c | 4 ++-- src/p_lblock_next.c | 33 +++++++++++++++------------------ src/s_com.c | 13 ++++++++----- 5 files changed, 28 insertions(+), 26 deletions(-) diff --git a/TODO.org b/TODO.org index e52ad7d..b256a0f 100644 --- a/TODO.org +++ b/TODO.org @@ -11,8 +11,9 @@ ** DONE [#A] Variables quotes qwe="okokoko kkok" asd='bav' ** DONE [#A] Fix PSX \stuff subst with "" '' ** DONE [#A] ls qweqwe; echo $? <---- substitute +** TODO [#A] Don't fuck with '$vars' ** TODO [#A] <>> -** TODO [#A] SEGV on pipes +** DONE [#A] SEGV on pipes ** DONE [#B] forked write(2) stuff on cd ** DONE [#B] Multiline && || ** DONE [#B] ./qwe.sh <=== handle bad shebang diff --git a/src/d_define.h b/src/d_define.h index 6d3cc45..0e62f36 100644 --- a/src/d_define.h +++ b/src/d_define.h @@ -71,6 +71,7 @@ #define C_SUB 0x1a #define C_ESC 0x1b #define C_DQUOTE 0x22 +#define C_DOLLAR 0x24 #define C_SQUOTE 0x27 #define C_AMP 0x26 #define C_SEMIC 0x3b diff --git a/src/p_args_len.c b/src/p_args_len.c index e624b37..729e5c5 100644 --- a/src/p_args_len.c +++ b/src/p_args_len.c @@ -45,11 +45,11 @@ size_t mode = u_meet_dquote(word, ptr, mode); else if (*ptr == C_SQUOTE) mode = u_meet_squote(word, ptr, mode); - else if (ft_iswhitespace(*ptr)) + else if (ft_iswhitespace(*ptr) == TRUE) terminate = p_meet_whitespace(word, ptr, mode); ptr++; } - if (*ptr != C_NUL) + if (terminate == TRUE) ptr -= 1; return (ptr - word); } diff --git a/src/p_lblock_next.c b/src/p_lblock_next.c index 8d89cf3..a5314f2 100644 --- a/src/p_lblock_next.c +++ b/src/p_lblock_next.c @@ -19,6 +19,7 @@ #include "s_destroy.h" #include "f_fail.h" #include "s_struct.h" +#include "u_parse.h" #include "u_utils.h" #include "u_vars.h" #include "u_vars_next.h" @@ -43,14 +44,13 @@ static char return (varval); } -static char - *p_subst_this_var(char **p, int64_t i, char word[], t_msh *msh) +static void + p_subst_this_var(char **p, int64_t i, char word[], t_msh *msh) { char tmp[ARG_MAX]; char varval[ARG_MAX]; char *ptr; size_t varlen; - int32_t count; ptr = word; varlen = i + 1; @@ -61,32 +61,29 @@ static char ft_strlcpy(tmp, ptr + i, varlen + 1 - i); u_get_var_value(varval, tmp, ARG_MAX, msh); p_double_them_bs(varval); - ft_strlcpy(tmp, ptr + varlen, ft_strlen(ptr + varlen) + 1); - if ((word = ft_nrealloc(word, i, i + ft_strlen(varval) + - ft_strlen(tmp) + 1)) == NULL) - return (NULL); - ft_strlcpy(word + i, varval, ft_strlen(varval) + 1); - ft_strlcpy(word + (i + ft_strlen(varval)), tmp, ft_strlen(tmp) + 1); - count = 0; + (void)ft_memmove(ptr + (i + ft_strlen(tmp)), ptr + varlen, (ft_strlen(ptr + varlen) + 1) * sizeof(char)); + (void)ft_memmove(word + i, varval, ft_strlen(varval) * sizeof(char)); *(p) = word + (i + ft_strlen(varval) - 1); - return (word); } char *p_subst_vars(char word[], t_msh *msh) { - char *ptr; + char *ptr; + t_quote_mode mode; + mode = Q_NONE; ptr = word; while (*ptr != C_NUL) { - if (*ptr == '$' && u_is_not_escaped(word, ptr) == TRUE) + if (*ptr == C_DQUOTE) + mode = u_meet_dquote(word, ptr, mode); + else if (*ptr == C_SQUOTE) + mode = u_meet_squote(word, ptr, mode); + if ((mode == Q_NONE || mode == Q_DOUBLE) && *ptr == C_DOLLAR && + u_is_not_escaped(word, ptr) == TRUE) { - if ((word = p_subst_this_var(&ptr, (ptr - word), word, msh)) - == NULL) - { - return (NULL); - } + p_subst_this_var(&ptr, (ptr - word), word, msh); } ptr++; } diff --git a/src/s_com.c b/src/s_com.c index 6d6ee09..277d996 100644 --- a/src/s_com.c +++ b/src/s_com.c @@ -12,7 +12,9 @@ #include #include +#include +#include "d_define.h" #include "f_fail.h" #include "p_args.h" #include "p_lblock.h" @@ -98,6 +100,7 @@ void t_com *s_com_new(char word[], t_msh *msh) { + char nword[ARG_MAX]; t_com *com; char **words; @@ -109,12 +112,12 @@ t_com com->rdrfd = 0; com->rdrpath = NULL; com->env_fork = NULL; - if (p_get_redir(word, &com) != 0) + nword[0] = C_NUL; + ft_strlcpy(nword, word, ARG_MAX); + if (p_get_redir(nword, &com) != 0) return (NULL); - if ((word = p_subst_vars(word, msh)) == NULL) - return (NULL); - msh->curr->lblock = word; - if ((words = p_split_args(word, com->redir)) == NULL) + p_subst_vars(nword, msh); + if ((words = p_split_args(nword, com->redir)) == NULL) return (NULL); if ((words = p_subst_home(words, msh)) == NULL) return (NULL); -- cgit v1.2.3