2023-02-16 12:28:10 -05:00
|
|
|
/* ************************************************************************** */
|
|
|
|
/* */
|
|
|
|
/* ::: :::::::: */
|
|
|
|
/* env_fill.c :+: :+: :+: */
|
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
|
|
|
/* Created: 2023/02/16 16:29:08 by cchauvet #+# #+# */
|
2023-02-20 09:41:03 -05:00
|
|
|
/* Updated: 2023/02/20 15:39:28 by starnakin ### ########.fr */
|
2023-02-16 12:28:10 -05:00
|
|
|
/* */
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
2023-03-10 06:32:39 -05:00
|
|
|
#include "./env_private.h"
|
2023-02-16 12:28:10 -05:00
|
|
|
|
2023-02-24 14:30:01 -05:00
|
|
|
int ft_gen_exit_code_var(t_data *data)
|
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
|
2023-03-06 11:43:29 -05:00
|
|
|
if (data->exit_code_str != NULL)
|
|
|
|
free(data->exit_code_str);
|
2023-02-24 14:30:01 -05:00
|
|
|
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)
|
2023-02-16 12:28:10 -05:00
|
|
|
{
|
|
|
|
char *value;
|
|
|
|
|
2023-02-24 14:30:01 -05:00
|
|
|
if (key[0] == '\0')
|
|
|
|
return ("$");
|
|
|
|
if (ft_strcmp("?", key) == 0)
|
|
|
|
return (data->exit_code_str);
|
|
|
|
value = get_value_by_key(key, data->env);
|
2023-02-16 12:28:10 -05:00
|
|
|
if (value == NULL)
|
|
|
|
value = "";
|
|
|
|
return (value);
|
|
|
|
}
|
|
|
|
|
2023-02-20 09:41:03 -05:00
|
|
|
static char *ft_get_key(char *str)
|
|
|
|
{
|
|
|
|
char *key;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2023-02-24 14:30:01 -05:00
|
|
|
if (str[i] == '?')
|
|
|
|
{
|
|
|
|
key = ft_strdup("?");
|
|
|
|
if (key == NULL)
|
|
|
|
ft_eprintf("minishell: malloc failed\n");
|
|
|
|
return (key);
|
|
|
|
}
|
2023-02-20 09:41:03 -05:00
|
|
|
while (str[i] != '\0' && str[i] != '$' && str[i] != ' '
|
|
|
|
&& str[i] != '"')
|
|
|
|
i++;
|
|
|
|
key = ft_strndup(str + 1, i - 1);
|
|
|
|
if (key == NULL)
|
|
|
|
ft_eprintf("minishell: malloc failed\n");
|
|
|
|
return (key);
|
|
|
|
}
|
|
|
|
|
2023-02-24 14:30:01 -05:00
|
|
|
char *ft_env_filler(t_data *data, const char *str)
|
2023-02-16 12:28:10 -05:00
|
|
|
{
|
|
|
|
size_t i;
|
2023-02-20 09:41:03 -05:00
|
|
|
char *key;
|
2023-02-16 12:28:10 -05:00
|
|
|
char *out;
|
|
|
|
char *temp;
|
|
|
|
char *value;
|
|
|
|
|
|
|
|
out = ft_strdup(str);
|
|
|
|
if (out == NULL)
|
|
|
|
{
|
|
|
|
ft_eprintf("minishell: malloc failed\n");
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
i = 0;
|
|
|
|
while (out[i] != '\0')
|
|
|
|
{
|
|
|
|
while (ft_is_in_quote(out, i) == 1)
|
|
|
|
i++;
|
|
|
|
while (out[i] == '$')
|
|
|
|
{
|
2023-02-20 09:41:03 -05:00
|
|
|
key = ft_get_key(out + i);
|
|
|
|
if (key == NULL)
|
|
|
|
{
|
|
|
|
free(out);
|
|
|
|
return (NULL);
|
|
|
|
}
|
2023-02-24 14:30:01 -05:00
|
|
|
value = ft_get_value(data, key);
|
2023-02-16 12:28:10 -05:00
|
|
|
if (value == NULL)
|
2023-02-20 09:41:03 -05:00
|
|
|
{
|
|
|
|
free(key);
|
|
|
|
free(out);
|
2023-02-16 12:28:10 -05:00
|
|
|
return (NULL);
|
2023-02-20 09:41:03 -05:00
|
|
|
}
|
|
|
|
temp = ft_strreplace(out, value, i, ft_strlen(key) + 1 + i);
|
|
|
|
free(key);
|
|
|
|
i = i + ft_strlen(value);
|
2023-02-16 12:28:10 -05:00
|
|
|
free(out);
|
|
|
|
if (temp == NULL)
|
|
|
|
{
|
|
|
|
ft_eprintf("minishell: malloc failed\n");
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
out = temp;
|
|
|
|
}
|
2023-02-17 07:42:53 -05:00
|
|
|
if (out[i] != '\0')
|
|
|
|
i++;
|
2023-02-16 12:28:10 -05:00
|
|
|
}
|
|
|
|
return (out);
|
|
|
|
}
|