core: env var replacer

This commit is contained in:
Camille Chauvet
2023-03-22 14:29:32 +01:00
parent a58f9a0c76
commit 0fa822f6db
8 changed files with 138 additions and 1145 deletions

129
env/env_fill.c vendored
View File

@ -11,88 +11,75 @@
/* ************************************************************************** */
#include "./env_private.h"
#include "env.h"
int ft_gen_exit_code_var(t_data *data)
static char *ft_getkey(const char *str)
{
char *str;
if (data->exit_code_str != NULL)
free(data->exit_code_str);
str = ft_itoa(data->exit_code);
if (str == NULL)
{
ft_printf("minishell: malloc failed");
return (1);
}
data->exit_code_str = str;
return (0);
}
static char *ft_get_value(t_data *data, char *key)
{
char *value;
if (key[0] == '\0')
return ("$");
if (ft_strcmp("?", key) == 0)
return (data->exit_code_str);
value = get_value_by_key(key, data->env);
if (value == NULL)
value = "";
return (value);
}
static char *ft_get_key(char *str)
{
char *key;
size_t i;
char *key;
i = 1;
if (ft_strncmp(str, "$$", 2) == 0)
{
key = ft_strdup("$");
if (key == NULL)
ft_eprintf("minishell: malloc failed\n");
return (key);
}
if (str[i] == ' ' || str[i] == '\0')
{
key = ft_strdup("");
if (key == NULL)
ft_eprintf("minishell: malloc failed\n");
return (key);
}
if (str[i] == '?')
{
else if (ft_strncmp(str, "$?", 2) == 0)
key = ft_strdup("?");
if (key == NULL)
ft_eprintf("minishell: malloc failed\n");
return (key);
}
while (str[i] != '\0' && str[i] != '$' && str[i] != ' '
&& str[i] != '"')
else
{
i = 1;
while (str[i] != '\0' && !ft_is_in("$?\'\"", str[i]))
i++;
key = ft_strndup(str + 1, i - 1);
key = ft_strndup(str + 1, i - 1);
}
if (key == NULL)
ft_eprintf("minishell: malloc failed\n");
return (key);
}
static char *ft_getvalue(t_data *data, char *key)
{
char *value;
if (ft_strcmp(key, "?") == 0)
value = ft_itoa(data->exit_code);
else if (ft_strcmp(key, "$") == 0)
value = ft_strdup("PID");
else
{
value = get_value_by_key(key, data->env);
if (value == NULL)
value = ft_strdup("");
else
value = ft_strdup(value);
}
if (value == NULL)
ft_eprintf("minishell: malloc failed\n");
return (value);
}
static char *ft_getvalue_by_str(t_data *data, const char *str, size_t *key_len)
{
char *key;
char *value;
key = ft_getkey(str);
if (key == NULL)
return (NULL);
*key_len = ft_strlen(key) + 1;
value = ft_getvalue(data, key);
free(key);
return (value);
}
char *ft_env_filler(t_data *data, const char *str)
{
size_t i;
char *key;
char *out;
char *temp;
char *value;
size_t key_len;
size_t i;
ft_gen_exit_code(data);
out = ft_strdup(str);
if (out == NULL)
{
ft_eprintf("minishell: malloc failed\n");
return (NULL);
}
i = 0;
while (out[i] != '\0')
{
@ -100,32 +87,18 @@ char *ft_env_filler(t_data *data, const char *str)
i++;
while (out[i] == '$')
{
key = ft_get_key(out + i);
if (key == NULL)
{
free(out);
return (NULL);
}
value = ft_get_value(data, key);
value = ft_getvalue_by_str(data, out + i, &key_len);
if (value == NULL)
{
free(key);
free(out);
return (NULL);
}
temp = ft_strreplace(out, value, i, ft_strlen(key) + 1 + i);
free(key);
temp = ft_strreplace(out, value, i, key_len + i);
i = i + ft_strlen(value);
free(out);
free(value);
if (temp == NULL)
{
ft_eprintf("minishell: malloc failed\n");
return (NULL);
}
out = temp;
}
if (out[i] != '\0')
i++;
i++;
}
return (out);
}