summaryrefslogtreecommitdiffstats
path: root/src/p_subst_alias.c
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-10-02 22:06:45 +0200
committerJozanLeClerc <bousset.rudy@gmail.com>2020-10-02 22:06:45 +0200
commitb9f6679647508149bc8ce90ef6addb4ec0f36f67 (patch)
treea02024c69e73a45bb1865c2feb215bec0b9ec7ea /src/p_subst_alias.c
parentIn progress (diff)
download42-minishell-b9f6679647508149bc8ce90ef6addb4ec0f36f67.tar.gz
42-minishell-b9f6679647508149bc8ce90ef6addb4ec0f36f67.tar.bz2
42-minishell-b9f6679647508149bc8ce90ef6addb4ec0f36f67.tar.xz
42-minishell-b9f6679647508149bc8ce90ef6addb4ec0f36f67.tar.zst
42-minishell-b9f6679647508149bc8ce90ef6addb4ec0f36f67.zip
Norming p_subst_alias
Diffstat (limited to 'src/p_subst_alias.c')
-rw-r--r--src/p_subst_alias.c83
1 files changed, 40 insertions, 43 deletions
diff --git a/src/p_subst_alias.c b/src/p_subst_alias.c
index 9e25087..ff3f3dc 100644
--- a/src/p_subst_alias.c
+++ b/src/p_subst_alias.c
@@ -44,12 +44,9 @@ static char *p_set_ptr(char *ptr, char word[], t_bool *good, size_t locat[])
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;
+ 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)
{
@@ -69,6 +66,20 @@ static char *p_set_ptr(char *ptr, char word[], t_bool *good, size_t locat[])
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++;
+ }
+ *i = 0;
+ }
+}
+
size_t p_subst_alias(char word[], t_bool reset, t_msh *msh)
{
static size_t used[4096];
@@ -76,21 +87,12 @@ size_t p_subst_alias(char word[], t_bool reset, t_msh *msh)
char value[ARG_MAX];
char tmp[255];
size_t locat[2];
- size_t j;
- size_t usedcmp;
char *ptr;
+ ssize_t j;
+ size_t usedcmp;
t_bool good;
- if (reset == TRUE)
- {
- i = 0;
- while (i < 4096)
- {
- used[i] = 0;
- i++;
- }
- i = 0;
- }
+ p_reset_used(used, reset, &i);
ptr = word;
ptr = p_skip_whitespace(ptr);
good = TRUE;
@@ -99,33 +101,28 @@ size_t p_subst_alias(char word[], t_bool reset, t_msh *msh)
ptr = p_set_ptr(ptr, word, &good, locat);
if (*ptr == C_NUL && good == TRUE)
locat[1] = (ptr - word);
- if (good == TRUE)
+ if (good == FALSE)
+ return (0);
+ 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)
{
- 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)
+ good = TRUE;
+ j = -1;
+ while (++j < i)
+ good = (used[j] == usedcmp) ? (FALSE) : (good);
+ if (good == TRUE)
{
- 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);
- }
+ (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);