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-03-22 09:29:32 -04:00
|
|
|
#include "env.h"
|
2023-02-16 12:28:10 -05:00
|
|
|
|
2023-03-22 09:29:32 -04:00
|
|
|
static char *ft_getkey(const char *str)
|
2023-02-24 14:30:01 -05:00
|
|
|
{
|
2023-03-22 09:29:32 -04:00
|
|
|
size_t i;
|
|
|
|
char *key;
|
2023-02-24 14:30:01 -05:00
|
|
|
|
2023-03-22 09:29:32 -04:00
|
|
|
if (ft_strncmp(str, "$$", 2) == 0)
|
|
|
|
key = ft_strdup("$");
|
|
|
|
else if (ft_strncmp(str, "$?", 2) == 0)
|
|
|
|
key = ft_strdup("?");
|
|
|
|
else
|
2023-02-24 14:30:01 -05:00
|
|
|
{
|
2023-03-22 09:29:32 -04:00
|
|
|
i = 1;
|
|
|
|
while (str[i] != '\0' && !ft_is_in("$?\'\"", str[i]))
|
|
|
|
i++;
|
|
|
|
key = ft_strndup(str + 1, i - 1);
|
2023-02-24 14:30:01 -05:00
|
|
|
}
|
2023-03-22 09:29:32 -04:00
|
|
|
if (key == NULL)
|
|
|
|
ft_eprintf("minishell: malloc failed\n");
|
|
|
|
return (key);
|
2023-02-24 14:30:01 -05:00
|
|
|
}
|
|
|
|
|
2023-03-22 09:29:32 -04:00
|
|
|
static char *ft_getvalue(t_data *data, char *key)
|
2023-02-16 12:28:10 -05:00
|
|
|
{
|
|
|
|
char *value;
|
|
|
|
|
2023-03-22 09:29:32 -04:00
|
|
|
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);
|
|
|
|
}
|
2023-02-16 12:28:10 -05:00
|
|
|
if (value == NULL)
|
2023-03-22 09:29:32 -04:00
|
|
|
ft_eprintf("minishell: malloc failed\n");
|
2023-02-16 12:28:10 -05:00
|
|
|
return (value);
|
|
|
|
}
|
|
|
|
|
2023-03-22 09:29:32 -04:00
|
|
|
static char *ft_getvalue_by_str(t_data *data, const char *str, size_t *key_len)
|
2023-02-20 09:41:03 -05:00
|
|
|
{
|
|
|
|
char *key;
|
2023-03-22 09:29:32 -04:00
|
|
|
char *value;
|
2023-02-20 09:41:03 -05:00
|
|
|
|
2023-03-22 09:29:32 -04:00
|
|
|
key = ft_getkey(str);
|
2023-02-20 09:41:03 -05:00
|
|
|
if (key == NULL)
|
2023-03-22 09:29:32 -04:00
|
|
|
return (NULL);
|
|
|
|
*key_len = ft_strlen(key) + 1;
|
|
|
|
value = ft_getvalue(data, key);
|
|
|
|
free(key);
|
|
|
|
return (value);
|
2023-02-20 09:41:03 -05:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
char *out;
|
|
|
|
char *temp;
|
|
|
|
char *value;
|
2023-03-22 09:29:32 -04:00
|
|
|
size_t key_len;
|
|
|
|
size_t i;
|
2023-02-16 12:28:10 -05:00
|
|
|
|
|
|
|
out = ft_strdup(str);
|
|
|
|
if (out == NULL)
|
|
|
|
return (NULL);
|
|
|
|
i = 0;
|
|
|
|
while (out[i] != '\0')
|
|
|
|
{
|
|
|
|
while (ft_is_in_quote(out, i) == 1)
|
|
|
|
i++;
|
|
|
|
while (out[i] == '$')
|
|
|
|
{
|
2023-03-22 09:29:32 -04:00
|
|
|
value = ft_getvalue_by_str(data, out + i, &key_len);
|
2023-02-16 12:28:10 -05:00
|
|
|
if (value == NULL)
|
|
|
|
return (NULL);
|
2023-03-22 09:29:32 -04:00
|
|
|
temp = ft_strreplace(out, value, i, key_len + i);
|
2023-02-20 09:41:03 -05:00
|
|
|
i = i + ft_strlen(value);
|
2023-02-16 12:28:10 -05:00
|
|
|
free(out);
|
2023-03-22 09:29:32 -04:00
|
|
|
free(value);
|
2023-02-16 12:28:10 -05:00
|
|
|
if (temp == NULL)
|
|
|
|
return (NULL);
|
|
|
|
out = temp;
|
|
|
|
}
|
2023-03-22 09:29:32 -04:00
|
|
|
i++;
|
2023-02-16 12:28:10 -05:00
|
|
|
}
|
|
|
|
return (out);
|
|
|
|
}
|