/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* p_subst_alias.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: rbousset +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ /* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ /* */ /* ************************************************************************** */ #include #include #include "p_subst_alias.h" #include "s_struct.h" #include "u_alias.h" #include "u_parse.h" #include "u_utils.h" #include "u_vars.h" static t_bool p_meet_whitespace(char *head, char *ptr, t_quote_mode mode) { if (mode == Q_NONE && u_is_not_escaped(head, ptr) == TRUE) { return (TRUE); } return (FALSE); } static char *p_set_ptr(char *ptr, char word[], t_bool *good, size_t locat[]) { t_quote_mode mode; mode = Q_NONE; while (*ptr != C_NUL) { mode = (*ptr == C_DQUOTE) ? (u_meet_dquote(word, ptr, mode)) : (mode); mode = (*ptr == C_SQUOTE) ? (u_meet_squote(word, ptr, mode)) : (mode); *good = (mode == Q_NONE && *ptr == C_EQUALS) ? (FALSE) : (*good); if (ft_iswhitespace(*ptr) == TRUE && p_meet_whitespace((char*)word, ptr, mode) == TRUE) { locat[1] = (ptr - word); if (*good == TRUE) break ; else { while (*ptr != C_NUL && ft_iswhitespace(*ptr)) ptr++; locat[0] = (ptr - word); ptr -= 1; *good = TRUE; } } ptr++; } return (ptr); } static void p_reset_used(size_t used[], t_bool reset, size_t *i) { if (reset == TRUE) { *i = 0; while (*i < 4096) { used[*i] = 0; *i += 1; } *i = 0; } } static size_t p_subst_alias_return(struct s_subst_alias s, char word[], char used[], size_t i) { if ((s.usedcmp = u_get_alias_value(s.value, s.tmp, ARG_MAX, msh)) != 0) { s.good = TRUE; j = -1; while (++j < (int64_t)i) s.good = (used[j] == s.usedcmp) ? (FALSE) : (s.good); if (s.good == TRUE) { (void)ft_memmove(word + (s.locat[0] + ft_strlen(s.value)), word + s.locat[1], ft_strlen(word + s.locat[1]) + 1 * sizeof(char)); (void)ft_memmove(word + s.locat[0], s.value, ft_strlen(s.value) * sizeof(char)); used[i] = s.usedcmp; i++; return (s.usedcmp); } } return (0); } size_t p_subst_alias(char word[], t_bool reset, t_msh *msh) { struct s_subst_alias s; static size_t used[4096]; static size_t i = 0; char *ptr; int64_t j; p_reset_used(used, reset, &i); ptr = word; while (*ptr != C_NUL && ft_iswhitespace(*ptr)) ptr++; s.good = TRUE; s.locat[0] = (ptr - word); s.locat[1] = (ptr - word); ptr = p_set_ptr(ptr, word, &s.good, s.locat); if (*ptr == C_NUL && s.good == TRUE) s.locat[1] = (ptr - word); if (s.good == FALSE) return (0); ft_strlcpy(s.tmp, word + s.locat[0], ((s.locat[1] - s.locat[0] < 253) ? (s.locat[1] - s.locat[0]) : (254)) + 1); return (p_subst_alias_return(s, word, used, i)); }