refactoring expander
This commit is contained in:
parent
1cccbad890
commit
ce028b7b6f
@ -6,7 +6,7 @@
|
|||||||
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/02/18 19:00:51 by qmennen #+# #+# */
|
/* Created: 2025/02/18 19:00:51 by qmennen #+# #+# */
|
||||||
/* Updated: 2025/02/18 19:05:14 by qmennen ### ########.fr */
|
/* Updated: 2025/02/18 20:40:04 by qmennen ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -15,6 +15,8 @@
|
|||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
|
|
||||||
|
size_t expander_count_vars(const char *s);
|
||||||
char *expander_parse_string(char *s, t_minishell *minishell);
|
char *expander_parse_string(char *s, t_minishell *minishell);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -6,89 +6,58 @@
|
|||||||
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/02/18 19:00:35 by qmennen #+# #+# */
|
/* Created: 2025/02/18 19:00:35 by qmennen #+# #+# */
|
||||||
/* Updated: 2025/02/18 20:31:36 by qmennen ### ########.fr */
|
/* Updated: 2025/02/18 21:04:00 by qmennen ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "environment.h"
|
||||||
|
#include "libft.h"
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
#include <stdio.h>
|
#include "typedef.h"
|
||||||
|
|
||||||
static size_t len_prefix(const char *s)
|
static size_t len_var_name(const char *s, int idx)
|
||||||
{
|
{
|
||||||
size_t i;
|
size_t i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (s[i] != '$' && s[i])
|
while (s[idx + i] && ft_isalpha(s[idx + i]))
|
||||||
i++;
|
i++;
|
||||||
return (i);
|
return (i);
|
||||||
}
|
}
|
||||||
|
|
||||||
static size_t len_var_name(const char *s)
|
t_environment *get_var(const char *s, int idx, t_minishell *minishell)
|
||||||
{
|
{
|
||||||
size_t start;
|
int i;
|
||||||
size_t i;
|
t_environment *env;
|
||||||
|
char *name;
|
||||||
|
|
||||||
start = len_prefix(s);
|
|
||||||
if (start == ft_strlen(s))
|
|
||||||
return (0);
|
|
||||||
start++;
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (s[start + i] && !ft_isspace(s[start + i]))
|
while (s[idx + i] && ft_isalpha(s[idx + i]))
|
||||||
i++;
|
i++;
|
||||||
return (i);
|
name = ft_substr(s, idx, i);
|
||||||
}
|
if (!name)
|
||||||
|
perror("name");
|
||||||
static char *get_env_value(char *s)
|
env = environment_get(minishell->environment, name);
|
||||||
{
|
free(name);
|
||||||
int start;
|
return (env);
|
||||||
|
|
||||||
start = 0;
|
|
||||||
while (s[start] != '$' && s[start])
|
|
||||||
start++;
|
|
||||||
if (s[start] != '$' || !s[start + 1])
|
|
||||||
return (NULL);
|
|
||||||
start++;
|
|
||||||
return ft_substr(s, start, len_var_name(s));
|
|
||||||
}
|
|
||||||
|
|
||||||
static char *expand_string(char *s, t_environment *env)
|
|
||||||
{
|
|
||||||
char *s1;
|
|
||||||
char *s2;
|
|
||||||
char *s3;
|
|
||||||
char *final_str;
|
|
||||||
|
|
||||||
final_str = ft_calloc(1, ft_strlen(env->value) + ft_strlen(s) - len_var_name(s));
|
|
||||||
if (!final_str)
|
|
||||||
{
|
|
||||||
perror("malloc");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
s1 = ft_substr(s, 0, len_prefix(s));
|
|
||||||
s2 = ft_strdup(env->value);
|
|
||||||
s3 = ft_substr(s, len_prefix(s) + 1 + len_var_name(s),len_prefix(s) + len_var_name(s) - ft_strlen(s));
|
|
||||||
if (!s1 || !s2 || !s3)
|
|
||||||
perror("expand");
|
|
||||||
return (ft_strjoin(ft_strjoin(s1, s2), s3));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char *expander_parse_string(char *s, t_minishell *minishell)
|
char *expander_parse_string(char *s, t_minishell *minishell)
|
||||||
{
|
{
|
||||||
size_t l1;
|
int i;
|
||||||
char *result;
|
|
||||||
char *name;
|
|
||||||
t_environment *env;
|
t_environment *env;
|
||||||
|
t_list *var_list;
|
||||||
|
|
||||||
l1 = len_prefix(s);
|
i = 0;
|
||||||
if (l1 == ft_strlen(s))
|
while (s[i])
|
||||||
return (ft_strdup(s));
|
{
|
||||||
name = get_env_value(s);
|
if (s[i] == '$')
|
||||||
if (!name)
|
{
|
||||||
return (ft_strdup(""));
|
env = get_var(s, i, minishell);
|
||||||
env = environment_get(minishell->environment, name);
|
if (env)
|
||||||
if (!env)
|
ft_lstadd_back(&var_list, ft_lstnew(env->value));
|
||||||
return (ft_strdup(""));
|
}
|
||||||
result = expand_string(s, env);
|
i++;
|
||||||
free(name);
|
}
|
||||||
return (result);
|
return ft_strdup("");
|
||||||
}
|
}
|
||||||
|
|||||||
29
src/expander/expander_count_vars.c
Normal file
29
src/expander/expander_count_vars.c
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* expander_count_vars.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/02/18 20:38:17 by qmennen #+# #+# */
|
||||||
|
/* Updated: 2025/02/18 20:39:53 by qmennen ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
size_t expander_count_vars(const char *s)
|
||||||
|
{
|
||||||
|
size_t vars;
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
vars = 0;
|
||||||
|
while (s[i])
|
||||||
|
{
|
||||||
|
if (s[i] == '$' && ft_isalpha(s[i + 1]))
|
||||||
|
vars++;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (vars);
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user