core: rebuild of parsing and the execution
This commit is contained in:
24
env/env.h
vendored
Normal file
24
env/env.h
vendored
Normal file
@ -0,0 +1,24 @@
|
||||
#ifndef ENV_H
|
||||
# define ENV_H
|
||||
# include "../libftx/libft/list.h"
|
||||
# include "../data/data.h"
|
||||
|
||||
typedef struct s_env
|
||||
{
|
||||
char *key;
|
||||
char *value;
|
||||
} t_env;
|
||||
|
||||
char *ft_env_filler(t_data *data, const char *str);
|
||||
void env_del(void *content);
|
||||
t_list **init_env(char **env);
|
||||
char **env_to_strs(t_list **head);
|
||||
int create_value_by_key(char *key, char *value, t_list **head);
|
||||
int create_value_by_key_dup(char *key, char *value, t_list **head);
|
||||
int set_value_by_key(char *key, char *value, t_list **head);
|
||||
char *get_value_by_key(char *key, t_list **head);
|
||||
void env_del(void *ptr);
|
||||
int delete_by_key(char *key, t_list **head);
|
||||
int possible_key(char *key);
|
||||
|
||||
#endif
|
110
env/env1.c
vendored
Normal file
110
env/env1.c
vendored
Normal file
@ -0,0 +1,110 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* env.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/02 14:39:56 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/02/23 13:34:41 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "env_private.h"
|
||||
|
||||
void add_sort(t_list **head, t_env *var)
|
||||
{
|
||||
t_list *current;
|
||||
t_env *last;
|
||||
|
||||
current = *head;
|
||||
while (current->next != NULL
|
||||
&& ft_strcmp(var->key, ((t_env *)(current->content))->key) > 0)
|
||||
current = current->next;
|
||||
if (current->content == NULL)
|
||||
current->content = var;
|
||||
else
|
||||
{
|
||||
last = NULL;
|
||||
swap_env_3((void **)&last, ¤t->content, (void **)&var);
|
||||
while (current->next != NULL)
|
||||
{
|
||||
current = current->next;
|
||||
swap_env(¤t->content, (void **)&last);
|
||||
}
|
||||
}
|
||||
if (current->next == NULL)
|
||||
current->next = ft_calloc(1, sizeof(t_list));
|
||||
}
|
||||
|
||||
char *get_value_by_key(char *key, t_list **head)
|
||||
{
|
||||
t_list *current;
|
||||
|
||||
current = *head;
|
||||
while (current->next != NULL)
|
||||
{
|
||||
if (ft_strcmp(((t_env *)current->content)->key, key) == 0)
|
||||
return (((t_env *)current->content)->value);
|
||||
current = current->next;
|
||||
}
|
||||
return ("");
|
||||
}
|
||||
|
||||
int set_value_by_key(char *key, char *value, t_list **head)
|
||||
{
|
||||
t_list *current;
|
||||
|
||||
current = *head;
|
||||
while (current->next != NULL)
|
||||
{
|
||||
if (ft_strcmp(((t_env *)current->content)->key, key) == 0)
|
||||
{
|
||||
free(((t_env *)current->content)->value);
|
||||
((t_env *)current->content)->value = value;
|
||||
return (0);
|
||||
}
|
||||
current = current->next;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
int create_value_by_key(char *key, char *value, t_list **head)
|
||||
{
|
||||
t_env *content;
|
||||
|
||||
if (set_value_by_key(key, value, head) == 0)
|
||||
return (0);
|
||||
content = ft_calloc(1, sizeof(t_env));
|
||||
if (content == NULL)
|
||||
return (1);
|
||||
content->key = key;
|
||||
content->value = value;
|
||||
add_sort(head, content);
|
||||
return (0);
|
||||
}
|
||||
|
||||
t_list **init_env(char **env)
|
||||
{
|
||||
t_list **head;
|
||||
int i;
|
||||
t_env *var;
|
||||
|
||||
head = ft_calloc(1, sizeof(t_list *));
|
||||
if (head == NULL)
|
||||
return (NULL);
|
||||
*head = ft_calloc(1, sizeof(t_list));
|
||||
if (*head == NULL)
|
||||
return (NULL);
|
||||
i = -1;
|
||||
while (env[++i])
|
||||
{
|
||||
var = ft_calloc(1, sizeof(t_env));
|
||||
if (var == NULL)
|
||||
return (NULL);
|
||||
var->key = get_key(env[i]);
|
||||
var->value = get_value(env[i]);
|
||||
add_sort(head, var);
|
||||
}
|
||||
return (head);
|
||||
}
|
76
env/env2.c
vendored
Normal file
76
env/env2.c
vendored
Normal file
@ -0,0 +1,76 @@
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* env2.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/17 17:22:01 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/02/17 17:24:57 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "env_private.h"
|
||||
|
||||
int get_index(char *s, char c)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = -1;
|
||||
while (s[++i])
|
||||
if (s[i] == c)
|
||||
return (i);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
void swap_env_3(void **a, void **b, void **c)
|
||||
{
|
||||
void *d;
|
||||
|
||||
d = *a;
|
||||
*a = *b;
|
||||
*b = *c;
|
||||
*c = d;
|
||||
}
|
||||
|
||||
void swap_env(void **a, void **b)
|
||||
{
|
||||
void *c;
|
||||
|
||||
c = *a;
|
||||
*a = *b;
|
||||
*b = c;
|
||||
}
|
||||
|
||||
void env_del(void *ptr)
|
||||
{
|
||||
t_env *content;
|
||||
|
||||
if (ptr == NULL)
|
||||
return ;
|
||||
content = ptr;
|
||||
if (content->key != NULL)
|
||||
free(content->key);
|
||||
if (content->key != NULL)
|
||||
free(content->value);
|
||||
if (content != NULL)
|
||||
free(content);
|
||||
}
|
||||
|
||||
char **env_to_strs(t_list **head)
|
||||
{
|
||||
t_list *current;
|
||||
t_env *content;
|
||||
char **env;
|
||||
int i;
|
||||
|
||||
current = *head;
|
||||
env = ft_calloc(ft_lstsize(*head), sizeof(char *));
|
||||
i = 0;
|
||||
while (current->content)
|
||||
{
|
||||
content = current->content;
|
||||
env[i++] = ft_strmerger(3, content->key, "=", content->value);
|
||||
current = current->next;
|
||||
}
|
||||
return (env);
|
||||
}
|
97
env/env3.c
vendored
Normal file
97
env/env3.c
vendored
Normal file
@ -0,0 +1,97 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* env3.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/17 17:25:09 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/02/28 12:46:33 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "env_private.h"
|
||||
|
||||
char *get_value(char *str)
|
||||
{
|
||||
char *s;
|
||||
int i;
|
||||
int start;
|
||||
|
||||
s = ft_calloc(ft_strlen(str), sizeof(char));
|
||||
start = get_index(str, '=');
|
||||
i = start;
|
||||
while (str[++i])
|
||||
s[i - start - 1] = str[i];
|
||||
return (s);
|
||||
}
|
||||
|
||||
char *get_key(char *str)
|
||||
{
|
||||
char *s;
|
||||
int i;
|
||||
|
||||
s = ft_calloc(ft_strlen(str), sizeof(char));
|
||||
i = -1;
|
||||
while (str[++i] != '=')
|
||||
s[i] = str[i];
|
||||
return (s);
|
||||
}
|
||||
|
||||
int create_value_by_key_dup(char *key, char *value, t_list **env)
|
||||
{
|
||||
char *key_dup;
|
||||
char *value_dup;
|
||||
|
||||
key_dup = ft_strdup(key);
|
||||
if (key_dup == NULL)
|
||||
{
|
||||
ft_eprintf("minishell: malloc failed\n");
|
||||
return (1);
|
||||
}
|
||||
value_dup = ft_strdup(value);
|
||||
if (value_dup == NULL)
|
||||
{
|
||||
free(key);
|
||||
ft_eprintf("minishell: malloc failed\n");
|
||||
return (1);
|
||||
}
|
||||
if (create_value_by_key(key_dup, value_dup, env))
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int delete_by_key(char *key, t_list **head)
|
||||
{
|
||||
t_list *last;
|
||||
t_list *current;
|
||||
|
||||
current = *head;
|
||||
while (current->next != NULL)
|
||||
{
|
||||
if (ft_strcmp(((t_env *)current->content)->key, key) == 0)
|
||||
{
|
||||
if (last->next != NULL)
|
||||
last->next = current->next;
|
||||
else
|
||||
*head = current->next;
|
||||
return (0);
|
||||
}
|
||||
last = current;
|
||||
current = current->next;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
int possible_key(char *key)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = -1;
|
||||
if (ft_isdigit(key[i + 1]))
|
||||
return (0);
|
||||
while (key[++i])
|
||||
if (!ft_isalnum(key[i]) && key[i] != '_')
|
||||
return (0);
|
||||
return (1);
|
||||
}
|
130
env/env_fill.c
vendored
Normal file
130
env/env_fill.c
vendored
Normal file
@ -0,0 +1,130 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* env_fill.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/16 16:29:08 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/02/20 15:39:28 by starnakin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "./env_private.h"
|
||||
|
||||
int ft_gen_exit_code_var(t_data *data)
|
||||
{
|
||||
char *str;
|
||||
|
||||
if (data->exit_code_str != NULL)
|
||||
free(data->exit_code_str);
|
||||
str = ft_itoa(data->exit_code);
|
||||
if (str == NULL)
|
||||
{
|
||||
ft_printf("minishell: malloc failed");
|
||||
return (1);
|
||||
}
|
||||
data->exit_code_str = str;
|
||||
return (0);
|
||||
}
|
||||
|
||||
static char *ft_get_value(t_data *data, char *key)
|
||||
{
|
||||
char *value;
|
||||
|
||||
if (key[0] == '\0')
|
||||
return ("$");
|
||||
if (ft_strcmp("?", key) == 0)
|
||||
return (data->exit_code_str);
|
||||
value = get_value_by_key(key, data->env);
|
||||
if (value == NULL)
|
||||
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);
|
||||
}
|
||||
if (str[i] == '?')
|
||||
{
|
||||
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_data *data, const char *str)
|
||||
{
|
||||
size_t i;
|
||||
char *key;
|
||||
char *out;
|
||||
char *temp;
|
||||
char *value;
|
||||
|
||||
out = ft_strdup(str);
|
||||
if (out == NULL)
|
||||
{
|
||||
ft_eprintf("minishell: malloc failed\n");
|
||||
return (NULL);
|
||||
}
|
||||
i = 0;
|
||||
while (out[i] != '\0')
|
||||
{
|
||||
while (ft_is_in_quote(out, i) == 1)
|
||||
i++;
|
||||
while (out[i] == '$')
|
||||
{
|
||||
key = ft_get_key(out + i);
|
||||
if (key == NULL)
|
||||
{
|
||||
free(out);
|
||||
return (NULL);
|
||||
}
|
||||
value = ft_get_value(data, 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)
|
||||
{
|
||||
ft_eprintf("minishell: malloc failed\n");
|
||||
return (NULL);
|
||||
}
|
||||
out = temp;
|
||||
}
|
||||
if (out[i] != '\0')
|
||||
i++;
|
||||
}
|
||||
return (out);
|
||||
}
|
14
env/env_private.h
vendored
Normal file
14
env/env_private.h
vendored
Normal file
@ -0,0 +1,14 @@
|
||||
#ifndef ENV_PRIVATE_H
|
||||
# define ENV_PRIVATE_H
|
||||
# include "./env.h"
|
||||
# include "../libftx/libftx.h"
|
||||
# include "../utils/utils.h"
|
||||
|
||||
void swap_env_3(void **a, void **b, void **c);
|
||||
void swap_env(void **a, void **b);
|
||||
char *get_value(char *str);
|
||||
char *get_key(char *str);
|
||||
int get_index(char *str, char c);
|
||||
char *ft_env_filler(t_data *data, const char *str);
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user