gros merge

This commit is contained in:
gazhonsepaskwa
2025-02-03 13:00:47 +01:00
parent 30dd017198
commit 2fdfa68256
37 changed files with 109 additions and 372 deletions

View File

@@ -0,0 +1,93 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* linked_list.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/15 13:38:49 by lderidde #+# #+# */
/* Updated: 2025/01/31 13:20:13 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../../includes/minishell.h"
t_node *create_node(char *val, t_token token)
{
t_node *node;
if (!val)
return (NULL);
node = malloc(sizeof(t_node));
if (!node)
return (NULL);
node->val = ft_strdup(val);
node->token = token;
node->pressision = 0;
node->next = NULL;
return (node);
}
int add_node_back(t_node **head, char *val, t_token token, t_pres pres)
{
t_node *tmp;
tmp = *head;
if (!val)
return (0);
if (!head || !(*head))
{
(*head) = create_node(val, token);
(*head)->pressision = pres;
return (1);
}
while (tmp->next != NULL)
tmp = tmp->next;
tmp->next = create_node(val, token);
tmp->next->pressision = pres;
if (tmp->next == NULL)
return (0);
return (1);
}
void free_linked_list(t_node *head)
{
t_node *tmp;
while (head)
{
tmp = head;
head = head->next;
free(tmp->val);
free(tmp);
}
}
int create_node_after(t_node *elem, char *val)
{
t_node *tmp_next;
tmp_next = elem->next;
elem->next = create_node(val, 0);
if (!elem->next)
return (0);
elem->next->next = tmp_next;
return (1);
}
int merge_with_next_node(t_node *node)
{
char *tmp_val;
t_node *tmp_next;
tmp_val = ft_strjoin(node->val, node->next->val);
if (!tmp_val)
return (0);
ft_free(&node->val);
node->val = tmp_val;
ft_free(&node->next->val);
tmp_next = node->next->next;
free(node->next);
node->next = tmp_next;
return (1);
}

View File

@@ -0,0 +1,64 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* token_and_pres.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/21 08:54:32 by nalebrun #+# #+# */
/* Updated: 2025/01/21 08:54:32 by nalebrun ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../../includes/minishell.h"
static t_pres get_operator(char *s)
{
if (s[0] == '&' && s[1] && s[1] == '&')
return (AND);
else if (s[0] == '|' && s[1] && s[1] == '|')
return (OR);
else if (s[0] == '|' && !s[1])
return (PIPE);
else if (s[0] == '(' && !s[1])
return (SUBSH_S);
else if (s[0] == ')' && !s[1])
return (SUBSH_E);
else if (s[0] == '<' && s[1] && s[1] == '<')
return (HEREDOC);
else if (s[0] == '>' && s[1] && s[1] == '>')
return (D_RED_R);
else if (s[0] == '<' && !s[1])
return (RED_L);
else if (s[0] == '>' && !s[1])
return (RED_R);
return (UNDEFINED);
}
t_token get_token(char *str)
{
t_token token;
if (!ft_strncmp(str, "&", 1) || !ft_strncmp(str, "|", 1)
|| !ft_strncmp(str, "(", 1) || !ft_strncmp(str, ")", 1)
|| !ft_strncmp(str, "<", 1) || !ft_strncmp(str, ">", 1))
token = OPERATOR;
else
token = WORD;
return (token);
}
t_pres get_pressision(char *s, t_token token,
t_token last_token, t_pres last_pres)
{
if (token == OPERATOR)
return (get_operator(s));
else if (last_token == OPERATOR && (last_pres == RED_R
|| last_pres == RED_L || last_pres == D_RED_R))
return (RED_FILE);
else if (last_token == OPERATOR && last_pres == HEREDOC)
return (LIM);
else if (last_token == OPERATOR || last_token == UNSET)
return (COMMAND);
return (PARAMETER);
}

View File

@@ -0,0 +1,120 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tokenizer.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/15 13:27:57 by lderidde #+# #+# */
/* Updated: 2025/01/31 13:24:28 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../../includes/minishell.h"
static t_node *tokenize_base(char *str)
{
int i;
t_node *head;
char **tab;
tab = ft_split_keep(str, " \t\n");
if (!tab)
return (NULL);
head = NULL;
i = 0;
while (tab[i])
{
if (!add_node_back(&head, tab[i], 0, 0))
return (free(tab), NULL);
i++;
}
free_tab(tab);
return (head);
}
static void set_token(t_node *head)
{
t_node *it;
t_token last_token;
t_pres last_pres;
it = head;
last_token = UNSET;
last_pres = UNDEFINED;
while (it != NULL)
{
it->token = get_token(it->val);
it->pressision = get_pressision(it->val, it->token, last_token, last_pres);
last_token = it->token;
last_pres = it->pressision;
it = it->next;
}
}
static int unstick_nodes(t_node *head)
{
t_node *it;
char *first_str;
char *second_str;
int copied;
it = head;
while (it != NULL)
{
if (is_sticked(it->val))
{
if (is_meta(it->val[0]))
first_str = copy_meta_xor(it->val, &copied, 0);
else
first_str = copy_meta_xor(it->val, &copied, 1);
second_str = ft_substr(it->val, copied, ft_strlen(it->val)
- copied);
ft_free(&it->val);
it->val = ft_strdup(first_str);
create_node_after(it, second_str);
ft_free(&first_str);
ft_free(&second_str);
}
it = it->next;
}
return (1);
}
static int stick_quote_node(t_node *head, char q)
{
t_node *it;
it = head;
while (it != NULL)
{
if (it->val[0] == q && !ft_strchr(&it->val[1], q)
&& find_quote_node(it->next, q))
{
while (it->next->val[0] != q)
if (!merge_with_next_node(it))
return (0);
if (!merge_with_next_node(it))
return (0);
}
it = it->next;
}
return (1);
}
t_node *tokenize(char *str)
{
t_node *head;
head = tokenize_base(str);
if (!head)
return (NULL);
if (!trim_nodes(head))
return (NULL);
if (!unstick_nodes(head))
return (NULL);
stick_quote_node(head, 39);
stick_quote_node(head, '"');
set_token(head);
return (head);
}

View File

@@ -0,0 +1,101 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tokenizer_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/22 14:24:05 by nalebrun #+# #+# */
/* Updated: 2025/01/22 14:24:05 by nalebrun ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../../includes/minishell.h"
int is_meta(char c)
{
if (c == '&' || c == '|' || c == '<' || c == '>' || c == '(' || c == ')'
|| c == '"' || c == 39)
return (1);
return (0);
}
int is_sticked(char *val)
{
int i;
int meta;
int unmeta;
i = 0;
meta = 0;
unmeta = 0;
while (val[i])
{
if (is_meta(val[i]))
meta = 1;
if (!is_meta(val[i]))
unmeta = 1;
i++;
}
if (meta && unmeta)
return (1);
return (0);
}
int ft_str_count(char *s, char c)
{
int i;
int count;
i = -1;
count = 0;
while (s[++i])
if (s[i] == c)
count++;
return (count);
}
int trim_nodes(t_node *head)
{
t_node *it;
char *tmp;
char in_quote;
it = head;
in_quote = 0;
while (it != NULL)
{
if (ft_str_count(it->val, 39) == 1
|| ft_str_count(it->val, '"') == 1)
{
if (!in_quote)
in_quote = it->val[0];
else if (it->val[0] == in_quote)
in_quote = 0;
}
if (!in_quote)
{
tmp = ft_strtrim(it->val, " \t\n");
if (!tmp)
return (0);
free(it->val);
it->val = tmp;
}
it = it->next;
}
return (1);
}
int find_quote_node(t_node *head, char q)
{
t_node *it;
it = head;
while (it != NULL)
{
if (it->val[0] == q)
return (1);
it = it->next;
}
return (0);
}

View File

@@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* unstick_node_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/03 12:53:08 by nalebrun #+# #+# */
/* Updated: 2025/02/03 12:53:08 by nalebrun ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../../includes/minishell.h"
char *copy_meta_xor(char *val, int *copied, int rev)
{
int i;
int j;
char *out;
i = 0;
while (is_meta(val[i]) ^ rev)
i++;
*copied = i;
out = malloc(i + 1);
j = -1;
while (++j < i)
out[j] = val[j];
out[i] = 0;
return (out);
}