42_minishell/env/env_fill.c

110 lines
2.6 KiB
C
Raw Normal View History

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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 */
/* */
/* ************************************************************************** */
#include "./env_private.h"
2023-03-22 09:29:32 -04:00
#include "env.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 09:59:19 -04:00
if (str[1] == '\0')
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)
{
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);
}
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-02-24 14:30:01 -05:00
char *ft_env_filler(t_data *data, const char *str)
{
char *out;
char *temp;
char *value;
2023-03-22 09:29:32 -04:00
size_t key_len;
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-22 09:29:32 -04:00
value = ft_getvalue_by_str(data, out + i, &key_len);
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);
free(out);
2023-03-22 09:29:32 -04:00
free(value);
if (temp == NULL)
return (NULL);
out = temp;
}
2023-03-22 09:29:32 -04:00
i++;
}
return (out);
}