cleenup
This commit is contained in:
83
test/tokenizer/linked_list.c
Normal file
83
test/tokenizer/linked_list.c
Normal file
@@ -0,0 +1,83 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* linked_list.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/01/15 13:38:49 by nalebrun #+# #+# */
|
||||
/* Updated: 2025/01/20 13:15:03 by nalebrun ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "tokenizer.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)
|
||||
{
|
||||
if (!val)
|
||||
return (0);
|
||||
while (head->next != NULL)
|
||||
head = head->next;
|
||||
head->next = create_node(val, token);
|
||||
if (head->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);
|
||||
}
|
||||
64
test/tokenizer/token_and_pres.c
Normal file
64
test/tokenizer/token_and_pres.c
Normal 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 "./tokenizer.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);
|
||||
}
|
||||
122
test/tokenizer/tokenizer.c
Normal file
122
test/tokenizer/tokenizer.c
Normal file
@@ -0,0 +1,122 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* tokenizer.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/01/15 13:27:57 by nalebrun #+# #+# */
|
||||
/* Updated: 2025/01/20 13:15:25 by nalebrun ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "tokenizer.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 = create_node(tab[0], 0);
|
||||
if (!head)
|
||||
return (free(tab), NULL);
|
||||
i = 1;
|
||||
while (tab[i])
|
||||
{
|
||||
if (!add_node_back(head, tab[i], 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);
|
||||
}
|
||||
66
test/tokenizer/tokenizer.h
Normal file
66
test/tokenizer/tokenizer.h
Normal file
@@ -0,0 +1,66 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* tokenizer.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/01/15 13:30:12 by nalebrun #+# #+# */
|
||||
/* Updated: 2025/01/20 13:15:34 by nalebrun ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef TOKENIZER_H
|
||||
# define TOKENIZER_H
|
||||
|
||||
# include "../../lib/libft/libft.h"
|
||||
|
||||
typedef enum e_token
|
||||
{
|
||||
UNSET,
|
||||
OPERATOR,
|
||||
WORD
|
||||
} t_token;
|
||||
|
||||
typedef enum e_pres
|
||||
{
|
||||
UNDEFINED,
|
||||
COMMAND,
|
||||
AND,
|
||||
OR,
|
||||
PIPE,
|
||||
SUBSH_S,
|
||||
SUBSH_E,
|
||||
RED_L,
|
||||
RED_R,
|
||||
HEREDOC,
|
||||
D_RED_R,
|
||||
PARAMETER,
|
||||
RED_FILE,
|
||||
LIM
|
||||
} t_pres;
|
||||
|
||||
typedef struct s_node
|
||||
{
|
||||
struct s_node *next;
|
||||
char *val;
|
||||
enum e_token token;
|
||||
enum e_pres pressision;
|
||||
} t_node;
|
||||
|
||||
t_node *tokenize(char *str);
|
||||
t_node *create_node(char *val, t_token token);
|
||||
int add_node_back(t_node *head, char *val, t_token token);
|
||||
int merge_with_next_node(t_node *node);
|
||||
void free_linked_list(t_node *stack);
|
||||
t_token get_token(char *str);
|
||||
t_pres get_pressision(char *s, t_token token, t_token last_token, t_pres last_pres);
|
||||
int create_node_after(t_node *elem, char *val);
|
||||
char *copy_meta_xor(char *val, int *copied, int rev);
|
||||
int is_meta(char c);
|
||||
int is_sticked(char *val);
|
||||
int trim_nodes(t_node *head);
|
||||
void debug_linked_list(t_node *head, char *msg);
|
||||
int find_quote_node(t_node *head, char q);
|
||||
|
||||
#endif
|
||||
101
test/tokenizer/tokenizer_utils.c
Normal file
101
test/tokenizer/tokenizer_utils.c
Normal 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 "tokenizer.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);
|
||||
}
|
||||
19
test/tokenizer/unstick_node_utils.c
Normal file
19
test/tokenizer/unstick_node_utils.c
Normal file
@@ -0,0 +1,19 @@
|
||||
#include "tokenizer.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);
|
||||
}
|
||||
Reference in New Issue
Block a user