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-03-28 09:51:12 -04:00
|
|
|
/* Updated: 2023/03/27 16:42:27 by cchauvet ### ########.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-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("?");
|
2023-03-22 11:07:27 -04:00
|
|
|
else if (str[1] == '\0')
|
2023-03-22 09:59:19 -04:00
|
|
|
key = ft_strdup("");
|
2023-03-22 09:29:32 -04:00
|
|
|
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");
|
2023-03-22 09:59:19 -04:00
|
|
|
else if (key[0] == '\0')
|
|
|
|
value = ft_strdup("$");
|
2023-03-22 09:29:32 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
value = get_value_by_key(key, data->env);
|
|
|
|
if (value == NULL)
|
|
|
|
value = ft_strdup("");
|
2023-03-22 09:59:19 -04:00
|
|
|
else
|
2023-03-22 09:29:32 -04:00
|
|
|
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:59:19 -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-03-28 09:51:12 -04:00
|
|
|
ft_freer_ultimate(2, value, out);
|
2023-02-16 12:28:10 -05:00
|
|
|
if (temp == NULL)
|
|
|
|
return (NULL);
|
|
|
|
out = temp;
|
|
|
|
}
|
2023-03-28 09:51:12 -04:00
|
|
|
if (out[i] != '\0')
|
|
|
|
i++;
|
2023-02-16 12:28:10 -05:00
|
|
|
}
|
|
|
|
return (out);
|
|
|
|
}
|