fix: '$' variable

This commit is contained in:
starnakin 2023-02-20 15:41:03 +01:00
parent 7eedf74847
commit cc74fda339
3 changed files with 50 additions and 26 deletions

5
cmd.c
View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/15 14:18:21 by cchauvet #+# #+# */
/* Updated: 2023/02/17 17:30:22 by cchauvet ### ########.fr */
/* Updated: 2023/02/20 14:47:29 by starnakin ### ########.fr */
/* */
/* ************************************************************************** */
@ -39,10 +39,7 @@ int ft_cmd_filler(t_list *element, char **args, t_list **env)
{
temp = ft_env_filler(env, args[i]);
if (temp == NULL)
{
ft_eprintf("minishell: malloc failed\n");
return (1);
}
free(args[i]);
args[i] = temp;
ft_quote_remover(temp);

View File

@ -6,37 +6,55 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/16 16:29:08 by cchauvet #+# #+# */
/* Updated: 2023/02/17 17:31:16 by cchauvet ### ########.fr */
/* Updated: 2023/02/20 15:39:28 by starnakin ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftx/libftx.h"
#include "minishell.h"
#include "utils/utils.h"
static char *ft_get_value(t_list **env, const char *str, size_t start,
size_t stop)
static char *ft_get_value(t_list **env, char *key)
{
char *key;
char *value;
key = ft_strndup(str + start, stop);
if (key == NULL)
{
ft_eprintf("minishell: malloc failed\n");
return (NULL);
}
value = get_value_by_key(key, env);
if (value == NULL)
value = "";
free(key);
return (value);
}
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);
}
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);
}
char *ft_env_filler(t_list **env, const char *str)
{
size_t i;
size_t y;
char *key;
char *out;
char *temp;
char *value;
@ -54,14 +72,22 @@ char *ft_env_filler(t_list **env, const char *str)
i++;
while (out[i] == '$')
{
y = i + 1;
while (out[y] != '\0' && out[y] != '$' && out[y] != ' '
&& out[y] != '"')
y++;
value = ft_get_value(env, out, i + 1, y - i - 1);
if (value == NULL)
key = ft_get_key(out + i);
if (key == NULL)
{
free(out);
return (NULL);
temp = ft_strreplace(out, value, i, y);
}
value = ft_get_value(env, key);
if (value == NULL)
{
free(key);
free(out);
return (NULL);
}
temp = ft_strreplace(out, value, i, ft_strlen(key) + 1 + i);
free(key);
i = i + ft_strlen(value);
free(out);
if (temp == NULL)
{

3
main.c
View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/16 15:16:14 by cchauvet #+# #+# */
/* Updated: 2023/02/17 18:59:50 by cchauvet ### ########.fr */
/* Updated: 2023/02/20 15:24:42 by starnakin ### ########.fr */
/* */
/* ************************************************************************** */
@ -108,6 +108,7 @@ int main(int ac, char **av, char **env)
data.env = init_env(env);
if (data.env == NULL)
return (1);
create_value_by_key("", "$", data.env);
line = ft_get_user_input(data.env);
if (line == NULL)
{