norm
This commit is contained in:
@@ -6,33 +6,26 @@
|
||||
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/07 12:58:25 by lderidde #+# #+# */
|
||||
/* Updated: 2025/02/08 10:53:48 by lderidde ### ########.fr */
|
||||
/* Updated: 2025/02/11 15:31:05 by lderidde ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
static int is_validchar(char c)
|
||||
static int get_var_len(t_ast_n *node, int j, int *i)
|
||||
{
|
||||
if (ft_isalnum(c) || c == '_')
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int get_var_len(t_ast_n *node, int j , int *i)
|
||||
{
|
||||
int len;
|
||||
int ret;
|
||||
char *str;
|
||||
int len;
|
||||
int ret;
|
||||
char *str;
|
||||
|
||||
len = *i + 1;
|
||||
while (node->args[j][len] && is_validchar(node->args[j][len]))
|
||||
while (node->args[j][len] && is_exvalidchar(node->args[j][len]))
|
||||
len++;
|
||||
str = ft_substr(node->args[j], *i + 1, (size_t)len - (*i + 1));
|
||||
*i = len;
|
||||
ret = ft_strlen(get_var_value(str, node->msh->env));
|
||||
ft_free(&str);
|
||||
return (ret - (len - (*i + 1)));
|
||||
return (ret - (len - (*i + 1)));
|
||||
}
|
||||
|
||||
static char *extract_env(char *str, char **envp)
|
||||
@@ -42,7 +35,7 @@ static char *extract_env(char *str, char **envp)
|
||||
char *tmp;
|
||||
|
||||
i = 1;
|
||||
while (str[i] && is_validchar(str[i]))
|
||||
while (str[i] && is_exvalidchar(str[i]))
|
||||
i++;
|
||||
if (i > 1)
|
||||
tmp = ft_substr(str, 1, i - 1);
|
||||
@@ -62,7 +55,7 @@ static int get_new_len(t_ast_n *node, int j)
|
||||
{
|
||||
if (node->args[j][i] == '$')
|
||||
{
|
||||
len += get_var_len(node, j , &i);
|
||||
len += get_var_len(node, j, &i);
|
||||
i--;
|
||||
}
|
||||
i++;
|
||||
@@ -70,18 +63,11 @@ static int get_new_len(t_ast_n *node, int j)
|
||||
return (len);
|
||||
}
|
||||
|
||||
static int valid_next(char c)
|
||||
{
|
||||
if (c != '\0' && is_validchar(c))
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
void expander_var(t_ast_n *node, int j)
|
||||
{
|
||||
int i;
|
||||
int k;
|
||||
int len;
|
||||
int i;
|
||||
int k;
|
||||
int len;
|
||||
char *new;
|
||||
|
||||
i = -1;
|
||||
@@ -94,11 +80,11 @@ void expander_var(t_ast_n *node, int j)
|
||||
{
|
||||
if (node->args[j][k] != '$')
|
||||
new[i] = node->args[j][k++];
|
||||
else if (node->args[j][k] == '$' && valid_next(node->args[j][k + 1]))
|
||||
else if (node->args[j][k] == '$' && exvalid_next(node->args[j][k + 1]))
|
||||
{
|
||||
ft_strlcat(new, extract_env(&node->args[j][k], node->msh->env), -1);
|
||||
i = ft_strlen(new) - 1;
|
||||
while (node->args[j][++k] && is_validchar(node->args[j][k]))
|
||||
while (node->args[j][++k] && is_exvalidchar(node->args[j][k]))
|
||||
continue ;
|
||||
}
|
||||
}
|
||||
@@ -106,53 +92,6 @@ void expander_var(t_ast_n *node, int j)
|
||||
node->args[j] = new;
|
||||
}
|
||||
|
||||
int in_squote(char *str, char *ch)
|
||||
{
|
||||
if (!ft_strchr(str, '\'') && !ft_strchr(str, '\"'))
|
||||
return (0);
|
||||
else if (ft_strchr(str, '\'') && !ft_strchr(str, '\"'))
|
||||
{
|
||||
if ((ch > ft_strchr(str, '\'')) && ch < ft_strrchr(str, '\''))
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
else if (!ft_strchr(str, '\'') && ft_strchr(str, '\"'))
|
||||
return (0);
|
||||
else
|
||||
{
|
||||
if (ft_strchr(str, '\'') < ft_strchr(str, '\"'))
|
||||
{
|
||||
if ((ch > ft_strchr(str, '\'')) && ch < ft_strrchr(str, '\''))
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
}
|
||||
|
||||
int in_dquote(char *str, char *ch)
|
||||
{
|
||||
if (!ft_strchr(str, '\'') && !ft_strchr(str, '\"'))
|
||||
return (0);
|
||||
else if (ft_strchr(str, '\"') && !ft_strchr(str, '\''))
|
||||
{
|
||||
if ((ch > ft_strchr(str, '\"')) && ch < ft_strrchr(str, '\"'))
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
else if (!ft_strchr(str, '\"') && ft_strchr(str, '\''))
|
||||
return (0);
|
||||
else
|
||||
{
|
||||
if (ft_strchr(str, '\"') < ft_strchr(str, '\''))
|
||||
{
|
||||
if ((ch > ft_strchr(str, '\"')) && ch < ft_strrchr(str, '\"'))
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
}
|
||||
int expand_var(t_ast_n *node, int j)
|
||||
{
|
||||
int i;
|
||||
@@ -160,7 +99,8 @@ int expand_var(t_ast_n *node, int j)
|
||||
i = 0;
|
||||
while (node->args[j][i])
|
||||
{
|
||||
if (node->args[j][i] == '$' && !in_squote(node->args[j], &node->args[j][i]))
|
||||
if (node->args[j][i] == '$' &&
|
||||
!in_squote(node->args[j], &node->args[j][i]))
|
||||
return (expander_var(node, j), 1);
|
||||
i++;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user