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 +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/15 14:18:21 by cchauvet #+# #+# */ /* 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]); temp = ft_env_filler(env, args[i]);
if (temp == NULL) if (temp == NULL)
{
ft_eprintf("minishell: malloc failed\n");
return (1); return (1);
}
free(args[i]); free(args[i]);
args[i] = temp; args[i] = temp;
ft_quote_remover(temp); ft_quote_remover(temp);

View File

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

3
main.c
View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/16 15:16:14 by cchauvet #+# #+# */ /* 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); data.env = init_env(env);
if (data.env == NULL) if (data.env == NULL)
return (1); return (1);
create_value_by_key("", "$", data.env);
line = ft_get_user_input(data.env); line = ft_get_user_input(data.env);
if (line == NULL) if (line == NULL)
{ {