42_minishell/env/env_fill.c

118 lines
2.8 KiB
C
Raw Normal View History

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* env_fill.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/16 16:29:08 by cchauvet #+# #+# */
2023-04-05 11:32:04 -04:00
/* Updated: 2023/04/05 15:09:46 by alouis-j ### ########.fr */
/* */
/* ************************************************************************** */
#include "./env_private.h"
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;
2023-03-29 13:07:29 -04:00
while (str[i] != '\0' && !ft_is_in("$?\'\" ", str[i]))
2023-03-22 09:29:32 -04:00
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)
{
char *value;
2023-03-22 09:29:32 -04:00
if (ft_strcmp(key, "?") == 0)
2023-04-05 11:32:04 -04:00
value = ft_itoa(*data->exit_code);
2023-03-22 09:29:32 -04:00
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);
}
if (value == NULL)
2023-03-22 09:29:32 -04:00
ft_eprintf("minishell: malloc failed\n");
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-03-29 13:07:29 -04:00
char *ft_str_formator(t_data *data, char *str, size_t *i)
{
char *value;
size_t key_len;
char *out;
value = ft_getvalue_by_str(data, str + *i, &key_len);
if (value == NULL)
return (NULL);
out = ft_strreplace(str, value, *i, key_len + *i);
*i = *i + ft_strlen(value);
free(value);
return (out);
}
2023-02-24 14:30:01 -05:00
char *ft_env_filler(t_data *data, const char *str)
{
char *out;
char *temp;
2023-03-22 09:29:32 -04:00
size_t i;
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-29 13:07:29 -04:00
temp = ft_str_formator(data, out, &i);
if (temp == NULL)
return (NULL);
2023-03-29 13:07:29 -04:00
free(out);
out = temp;
}
2023-03-28 09:51:12 -04:00
if (out[i] != '\0')
i++;
}
return (out);
}