diff options
author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-10-02 21:39:47 +0200 |
---|---|---|
committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-10-02 21:39:47 +0200 |
commit | 3d696077d245713c921956a2be8fd6e12a052d4a (patch) | |
tree | fbe8ffd276fe67a113cbec0b6ce65177f4bd7d4f /src/p_subst_alias.c | |
parent | Update (diff) | |
download | 42-minishell-3d696077d245713c921956a2be8fd6e12a052d4a.tar.gz 42-minishell-3d696077d245713c921956a2be8fd6e12a052d4a.tar.bz2 42-minishell-3d696077d245713c921956a2be8fd6e12a052d4a.tar.xz 42-minishell-3d696077d245713c921956a2be8fd6e12a052d4a.tar.zst 42-minishell-3d696077d245713c921956a2be8fd6e12a052d4a.zip |
In progress
Diffstat (limited to 'src/p_subst_alias.c')
-rw-r--r-- | src/p_subst_alias.c | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/src/p_subst_alias.c b/src/p_subst_alias.c new file mode 100644 index 0000000..9e25087 --- /dev/null +++ b/src/p_subst_alias.c @@ -0,0 +1,132 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* p_subst_alias.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stddef.h> + +#include "s_struct.h" +#include "u_alias.h" +#include "u_parse.h" +#include "u_utils.h" +#include "u_vars.h" + +static char *p_skip_whitespace(char *ptr) +{ + while (*ptr != C_NUL && ft_iswhitespace(*ptr)) + { + ptr++; + } + return (ptr); +} + +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) + { + if (*ptr == C_DQUOTE) + mode = u_meet_dquote(word, ptr, mode); + if (*ptr == C_SQUOTE) + mode = u_meet_squote(word, ptr, mode); + if (mode == Q_NONE && *ptr == C_EQUALS) + *good = FALSE; + if (ft_iswhitespace(*ptr) == TRUE + && p_meet_whitespace((char*)word, ptr, mode) == TRUE) + { + locat[1] = (ptr - word); + if (*good == TRUE) + break ; + else + { + ptr = p_skip_whitespace(ptr); + locat[0] = (ptr - word); + ptr -= 1; + *good = TRUE; + } + } + ptr++; + } + return (ptr); +} + +size_t p_subst_alias(char word[], t_bool reset, t_msh *msh) +{ + static size_t used[4096]; + static size_t i = 0; + char value[ARG_MAX]; + char tmp[255]; + size_t locat[2]; + size_t j; + size_t usedcmp; + char *ptr; + t_bool good; + + if (reset == TRUE) + { + i = 0; + while (i < 4096) + { + used[i] = 0; + i++; + } + i = 0; + } + ptr = word; + ptr = p_skip_whitespace(ptr); + good = TRUE; + locat[0] = (ptr - word); + locat[1] = (ptr - word); + ptr = p_set_ptr(ptr, word, &good, locat); + if (*ptr == C_NUL && good == TRUE) + locat[1] = (ptr - word); + if (good == TRUE) + { + ft_strlcpy(tmp, + word + locat[0], + ((locat[1] - locat[0] < 253) ? (locat[1] - locat[0]) : (254)) + 1); + if ((usedcmp = u_get_alias_value(value, tmp, ARG_MAX, msh)) != 0) + { + j = 0; + good = TRUE; + while (j < i) + { + if (used[j] == usedcmp) + good = FALSE; + j++; + } + if (good == TRUE) + { + (void)ft_memmove(word + (locat[0] + ft_strlen(value)), + word + locat[1], + ft_strlen(word + locat[1]) + 1 * sizeof(char)); + (void)ft_memmove(word + locat[0], + value, + ft_strlen(value) * sizeof(char)); + used[i] = usedcmp; + i++; + return (usedcmp); + } + } + } + return (0); +} |