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

@@ -1,247 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ast.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/24 08:22:16 by nalebrun #+# #+# */
/* Updated: 2025/02/01 11:13:58 by nalebrun ### ########.fr */
/* */
/* ************************************************************************** */
#include "ast.h"
// ===================================================================
t_ast_n *create_ast_n(t_node *lst, t_ast_n *parent);
// ====================================================================
void create_and_or(t_ast_n *parrent, t_node *lst, t_node *token)
{
t_nodell *nodell;
nodell = cutll(lst, token, 1);
parrent->left = create_ast_n(nodell->node, parrent);
parrent->right = create_ast_n(nodell->next->node, parrent);
// free_lltab(sublsts);
}
// ===================================================================
t_redir is_redir(t_node *node)
{
if (!node)
return (_NR);
if (!ft_strncmp(node->val, ">", 1))
return (_RED_R);
if (!ft_strncmp(node->val, ">>", 1))
return (_RED_DR);
if (!ft_strncmp(node->val, "<", 1))
return (_RED_L);
return (_NR);
}
char **lltotab(t_node *lst, t_node *limiter)
{
char **out;
int count;
t_node *cpy;
count = 0;
cpy = lst;
while (cpy && cpy != limiter)
{
count++;
cpy = cpy->next;
}
out = ft_calloc(count + 1, sizeof(char *));
if (!out)
return (NULL);
cpy = lst;
count = 0;
while (cpy && cpy != limiter)
{
out[count] = ft_strdup(cpy->val);
count++;
cpy = cpy->next;
}
out[count] = NULL;
return (out);
}
t_node *get_redir_node(t_node *head)
{
t_node *cpy;
cpy = head;
while (cpy && !is_redir(cpy))
cpy = cpy->next;
return (cpy);
}
void create_cmd(t_ast_n *self, t_node *lst)
{
t_node *sec_redir_node;
self->state = _CMD;
self->infile = NULL;
self->outfile = NULL;
if (is_redir(lst) != _NR)
{
self->cmd = ft_strdup(lst->next->next->val);
self->args = lltotab(lst->next->next, NULL);
self->infile = ft_strdup(lst->next->val);
}
else
{
self->cmd = ft_strdup(lst->val);
self->args = lltotab(lst, get_redir_node(lst->next));
}
sec_redir_node = get_redir_node(lst->next);
if (lst->next && sec_redir_node)
{
self->outfile = ft_strdup(sec_redir_node->next->val);
}
self->redir = is_redir(get_redir_node(lst));
}
//=====================================================================
void create_pline(t_ast_n *self, t_node *lst, t_node *token)
{
t_nodell *nodell;
t_nodell *cpy;
int count;
int i;
nodell = cutll(lst, token, -1);
cpy = nodell;
count = 0;
while (cpy)
{
count++;
cpy = cpy->next;
}
self->pline = ft_calloc(count + 1, sizeof(t_ast_n *));
cpy = nodell;
i = 0;
while (cpy)
{
self->pline[i] = create_ast_n(cpy->node, self);
cpy = cpy->next;
i++;
}
cpy = NULL;
// free_lltab(sublsts);
}
//======================================================================
void remove_parentheses(t_node **lst)
{
t_node *tmp;
t_node *cpy;
if (!lst || !*lst || !(*lst)->next)
return;
tmp = *lst;
*lst = (*lst)->next;
free(tmp->val);
free(tmp);
cpy = *lst;
while (cpy->next && cpy->next->next)
cpy = cpy->next;
if (cpy->next)
{
free(cpy->next->val);
free(cpy->next);
cpy->next = NULL;
}
}
void create_subsh(t_ast_n *parent, t_node *lst)
{
t_node *cpy;
cpy = lst;
while (cpy)
{
ft_printf("%s\n", cpy->val);
cpy = cpy->next;
}
remove_parentheses(&lst);
ft_printf("\n\n");
cpy = lst;
while (cpy)
{
ft_printf("%s\n", cpy->val);
cpy = cpy->next;
}
parent->left = create_ast_n(lst, parent);
}
//==================================================================
t_ast_n *create_ast_n(t_node *lst, t_ast_n *parent)
{
t_ast_n *node;
t_node *token;
node = malloc(sizeof(t_ast_n));
token = get_top_token(lst, &node->state);
ft_debug("================\n");
ft_debug("NEW NODE CREATED\n");
if (token && token->val)
ft_debug("token val: %s\n", token->val);
else
ft_debug("token val : NULL\n");
ft_debug("node state : %d\n", node->state);
ft_debug("================\n\n");
node->left = NULL;
node->right = NULL;
node->pline = NULL;
node->parent = parent;
if (node->state == _AND || node->state == _OR)
create_and_or(node, lst, token);
else if (node->state == _SUBSH)
create_subsh(node, lst);
else if (node->state == _PLINE)
create_pline(node, lst, token);
else
create_cmd(node, lst);
return (node);
}
t_ast_n *get_ast(char **envp, t_node *lst)
{
t_ast_n *head;
(void)envp;
head = create_ast_n(lst, NULL);
return (head);
}

View File

@@ -1,72 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ast.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/24 08:23:27 by nalebrun #+# #+# */
/* Updated: 2025/01/24 08:23:27 by nalebrun ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef AST_H
# define AST_H
/*# include "../../includes/env.h"*/
# include "../../lib/libft/libft.h"
# include "../tokenizer/tokenizer.h"
typedef enum e_state
{
UNDEF,
_AND,
_OR,
_CMD,
_PLINE,
_SUBSH
} t_state;
typedef enum e_redir
{
_NR,
_RED_L,
_RED_R,
_RED_DR
} t_redir;
typedef struct s_ast_n
{
t_state state;
struct s_ast_n *parent;
struct s_ast_n *left;
struct s_ast_n *right;
struct s_ast_n **pline;
int ex_code;
struct s_ast_n *head;
char *cmd;
char **args;
int fds[2];
int _stdout;
int _stdin;
t_redir redir;
char *infile;
char *outfile;
int shlvl;
char **env;
} t_ast_n;
typedef struct s_nodell
{
t_node *node;
struct s_nodell *next;
} t_nodell;
t_ast_n *get_ast(char **envp, t_node *lst);
t_nodell *cutll(t_node *lst, t_node *expected, size_t limiter);
t_node *get_top_token(t_node *lst, t_state *state);
// env TMP
char **init_env(char **envp);
#endif

View File

@@ -1,74 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cutll.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/31 09:56:34 by lderidde #+# #+# */
/* Updated: 2025/01/31 14:05:12 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
#include "ast.h"
static void add_nodell(t_nodell **nodell, t_node *node)
{
t_nodell *tmp;
if (!nodell || !(*nodell))
{
*nodell = malloc(sizeof(t_nodell));
(*nodell)->node = node;
(*nodell)->next = NULL;
return ;
}
tmp = *nodell;
while (tmp->next)
tmp = tmp->next;
tmp->next = malloc(sizeof(t_nodell));
tmp->next->node = node;
tmp->next->next = NULL;
}
static t_node *get_node(t_node **lst, t_node *expected, int limiter)
{
t_node *node;
node = NULL;
while (limiter != -1 && (*lst) && (*lst) != expected)
{
add_node_back(&node, (*lst)->val, (*lst)->token, (*lst)->pressision);
(*lst) = (*lst)->next;
}
while (limiter == -1 && (*lst) && ft_strncmp((*lst)->val, expected->val, ft_strlen((*lst)->val)))
{
add_node_back(&node, (*lst)->val, (*lst)->token, (*lst)->pressision);
(*lst) = (*lst)->next;
}
return (node);
}
t_nodell *cutll(t_node *lst, t_node *expected, size_t limiter)
{
t_nodell *out;
t_node *node;
size_t i;
i = 0;
out = NULL;
while (i <= limiter)
{
node = get_node(&lst, expected, limiter);
if (!node)
break;
add_nodell(&out, node);
t_nodell *tmp = out;
while (tmp)
tmp = tmp->next;
if(lst && lst->next)
lst = lst->next;
i++;
}
return (out);
}

View File

@@ -1,76 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* top_token.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/31 17:14:26 by nalebrun #+# #+# */
/* Updated: 2025/01/31 17:14:26 by nalebrun ### ########.fr */
/* */
/* ************************************************************************** */
#include "ast.h"
static int last_tok_subsh(t_node *lst)
{
while (lst)
{
if (lst->next == NULL && !ft_strncmp(lst->val, ")", 1))
return (1);
lst = lst->next;
}
return (0);
}
static void skip_parentheses(t_node **lst)
{
if (!ft_strncmp((*lst)->val, "(", 1))
{
while ((*lst)->next && ft_strncmp((*lst)->next->val, ")", 1))
{
if (!ft_strncmp((*lst)->val, "(", 1))
skip_parentheses(&(*lst)->next);
*lst = (*lst)->next;
}
}
}
static t_node *find_token(char *tok, t_node *lst)
{
while (lst)
{
skip_parentheses(&lst);
if (!ft_strncmp(lst->val, tok, ft_strlen(tok)))
return (lst);
lst = lst->next;
}
return (NULL);
}
t_node *get_top_token(t_node *lst, t_state *state)
{
*state = _SUBSH;
if (!ft_strncmp(lst->val, "(", 1) && last_tok_subsh(lst))
return (lst);
else if (find_token("&&", lst))
{
*state = _AND;
return (find_token("&&", lst));
}
else if (find_token("||", lst))
{
*state = _OR;
return (find_token("||", lst));
}
else if (find_token("|", lst))
{
*state = _PLINE;
return (find_token("|", lst));
}
else
{
*state = UNDEF;
return (NULL);
}
}

View File

@@ -1,45 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* drawio.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/27 14:20:35 by nalebrun #+# #+# */
/* Updated: 2025/01/27 14:20:35 by nalebrun ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef DRAWIO_H
# define DRAWIO_H
# include "../../lib/libft/libft.h"
# include "../tokenizer/tokenizer.h"
# include "../ast/ast.h"
typedef struct s_dio_node
{
const char *st;
const char *redir;
char *cmd;
char *args;
char *inf;
char *outf;
} t_dio_node;
typedef struct s_elems
{
t_dio_elem rect;
t_dio_elem arrow;
} t_elems;
// internal
char *replace_ampercent(char *src);
char *replace_left_red(char *src);
t_dio_node get_cmd_txt(t_ast_n *node);
int print_ast(t_ast_n *node, t_elems *e, int fd);
// external
void gen_dio_linked_list(t_node *head, int fd);
void gen_dio_ast(t_ast_n *head, int fd);
#endif

View File

@@ -1,35 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* drawio_ast.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/27 17:15:39 by nalebrun #+# #+# */
/* Updated: 2025/01/27 17:15:39 by nalebrun ### ########.fr */
/* */
/* ************************************************************************** */
#include "drawio.h"
void set_ast_rect(t_dio_elem *rect)
{
rect->type = RECT;
rect->rounded = 0;
rect->x = 50;
rect->y = 150;
rect->w = 150;
rect->h = 120;
}
void gen_dio_ast(t_ast_n *head, int fd)
{
t_elems e;
set_ast_rect(&e.rect);
e.arrow.type = ARROW;
e.arrow.id_src = 0;
e.arrow.id_dst = 0;
print_ast(head, &e, fd);
return ;
}

View File

@@ -1,75 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* drawio_ast_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/29 09:21:09 by nalebrun #+# #+# */
/* Updated: 2025/01/29 09:21:09 by nalebrun ### ########.fr */
/* */
/* ************************************************************************** */
#include "drawio.h"
const char *translate_state(t_state state)
{
const char *out;
if (state == _AND)
out = "CMD_AND";
else if (state == _OR)
out = "CMD_OR";
else if (state == _PLINE)
out = "CMD_PIPELINE";
else if (state == _CMD)
out = "SIMPLE_CMD";
else if (state == _SUBSH)
out = "SUBSHELL";
else
out = "UNDEFINED";
return (out);
}
const char *translate_redir(t_redir redir)
{
const char *out;
if (redir == _RED_L)
out = "redir : RED_L&#10;";
else if (redir == _RED_R)
out = "redir : RED_R&#10;";
else if (redir == _RED_DR)
out = "redir : _RED_DR &#10;";
else
out = "Not redirected &#10;";
return (out);
}
t_dio_node get_cmd_txt(t_ast_n *node)
{
t_dio_node txt;
char *args;
txt.st = translate_state(node->state);
if (node->state == _CMD)
{
txt.cmd = ft_sprintf("%s%s", NL, node->cmd);
txt.cmd = replace_left_red(txt.cmd);
args = ft_tabstr(node->args);
txt.args = ft_sprintf("%s%s%s", NL, args, NL);
free(args);
txt.redir = translate_redir(node->redir);
txt.inf = ft_sprintf("Infile : %s%s", node->infile, NL);
txt.outf = ft_sprintf("Outfile : %s", node->outfile);
}
else
{
txt.cmd = ft_calloc(1, 1);
txt.args = ft_calloc(1, 1);
txt.redir = "";
txt.inf = ft_calloc(1, 1);
txt.outf = ft_calloc(1, 1);
}
return (txt);
}

View File

@@ -1,50 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* drawio_linklist.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/24 14:41:02 by nalebrun #+# #+# */
/* Updated: 2025/01/27 17:16:53 by nalebrun ### ########.fr */
/* */
/* ************************************************************************** */
#include "drawio.h"
void set_ll_rect(t_dio_elem *rect)
{
static int y = 50;
rect->type = RECT;
rect->rounded = 0;
rect->x = 50;
rect->y = y;
rect->w = 100;
rect->h = 50;
y += 100;
}
void gen_dio_linked_list(t_node *head, int fd)
{
t_dio_elem rect;
t_dio_elem arrow;
t_node *current;
current = head;
set_ll_rect(&rect);
arrow.id_src = 0;
arrow.type = ARROW;
while (current != NULL)
{
rect.text = ft_sprintf("%s", current->val);
rect.text = replace_ampercent(rect.text);
rect.text = replace_left_red(rect.text);
arrow.id_dst = drawio_create_elem(fd, &rect);
drawio_create_elem(fd, &arrow);
arrow.id_src = arrow.id_dst;
rect.x += 150;
free(rect.text);
current = current->next;
}
}

View File

@@ -1,73 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* drawio_print_ast.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/29 09:54:31 by nalebrun #+# #+# */
/* Updated: 2025/01/29 09:54:31 by nalebrun ### ########.fr */
/* */
/* ************************************************************************** */
#include "drawio.h"
char *get_node_txt(t_ast_n *node)
{
t_dio_node txt;
char *out;
txt = get_cmd_txt(node);
out = ft_sprintf("%s%s%s%s%s%s%s", txt.st, txt.cmd, txt.args,
NL, txt.redir, txt.inf, txt.outf);
free(txt.cmd);
free(txt.args);
free(txt.inf);
free(txt.outf);
return (out);
}
void draw_bin_part(t_ast_n *node, t_elems *e, int fd, int node_id)
{
e->rect.y += e->rect.h + 50;
e->arrow.id_dst = print_ast(node->left, e, fd);
e->arrow.id_src = node_id;
drawio_create_elem(fd, &e->arrow);
e->rect.x += e->rect.w + 50;
e->arrow.id_dst = print_ast(node->right, e, fd);
e->arrow.id_src = node_id;
drawio_create_elem(fd, &e->arrow);
e->rect.y -= e->rect.h + 50;
if (node->state == _CMD)
e->rect.x -= e->rect.w + 50;
}
void draw_pline_part(t_ast_n *node, t_elems *e, int fd, int node_id)
{
int i;
i = 0;
e->rect.y += e->rect.h + 50;
while (node->pline && node->pline[i])
{
e->arrow.id_dst = print_ast(node->pline[i++], e, fd);
e->arrow.id_src = node_id;
drawio_create_elem(fd, &e->arrow);
e->rect.x += e->rect.w + 50;
}
}
int print_ast(t_ast_n *node, t_elems *e, int fd)
{
int node_id;
if (!node)
return (-1);
e->rect.text = get_node_txt(node);
node_id = drawio_create_elem(fd, &e->rect);
if (node->state != _PLINE)
draw_bin_part(node, e, fd, node_id);
else
draw_pline_part(node, e, fd, node_id);
return (node_id);
}

View File

@@ -1,92 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* drawio_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/27 15:09:16 by nalebrun #+# #+# */
/* Updated: 2025/01/27 15:09:16 by nalebrun ### ########.fr */
/* */
/* ************************************************************************** */
#include "drawio.h"
static int get_char_count(char *str, char c)
{
int i;
int count;
i = 0;
count = 0;
while (str[i])
{
if (str[i] == c)
count++;
i++;
}
return (count);
}
char *replace_ampercent(char *src)
{
int i;
int j;
int amp_count;
char *out;
i = -1;
j = 0;
amp_count = get_char_count(src, '&');
out = malloc(ft_strlen(src) + amp_count * 4 + 1);
while (src[++i])
{
if (src[i] == '&')
{
out[j] = '&';
out[j + 1] = 'a';
out[j + 2] = 'm';
out[j + 3] = 'p';
out[j + 4] = ';';
j += 5;
}
else
out[j++] = src[i];
}
out[j] = 0;
free(src);
return (out);
}
char *replace_left_red(char *src)
{
int i;
int j;
int redl_count;
char *out;
i = -1;
j = 0;
redl_count = get_char_count(src, '<');
out = malloc(ft_strlen(src) + redl_count * 7 + 1);
while (src[++i])
{
if (src[i] == '<')
{
out[j] = '&';
out[j + 1] = 'a';
out[j + 2] = 'm';
out[j + 3] = 'p';
out[j + 4] = ';';
out[j + 5] = 'l';
out[j + 6] = 't';
out[j + 7] = ';';
j += 8;
}
else
out[j++] = src[i];
}
out[j] = 0;
free(src);
return (out);
}

View File

@@ -1,27 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/24 14:45:28 by nalebrun #+# #+# */
/* Updated: 2025/01/24 14:45:28 by nalebrun ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PARSING_H
# define PARSING_H
# define DEBUG 1
# include "tokenizer/tokenizer.h"
# include "ast/ast.h"
// drawio
# include "drawio/drawio.h"
// tmp_env
char **init_env(char **envp);
#endif

View File

@@ -1,71 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/15 08:23:41 by lderidde #+# #+# */
/* Updated: 2025/01/31 14:05:34 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
#include "parsing.h"
void truncate_comment(char *str)
{
int i;
char inquote;
if (!str)
return ;
i = 0;
inquote = 0;
while (str[i])
{
if (!inquote && (str[i] == 39 || str[i] == '"'))
inquote = str[i];
else if (inquote && (str[i] == inquote))
inquote = 0;
if (str[i] == '#' && !inquote && str[i - 1] && str[i - 1] != '\\')
{
str[i] = 0;
return ;
}
i++;
}
}
int main(int ac, char **av, char **envp)
{
t_node *lst;
t_ast_n *ast;
int dio;
(void)envp;
if (ac != 3)
{
ft_error("./test drawio_file command_str\n");
return (1);
}
truncate_comment(av[1]);
lst = tokenize(av[2]);
if (!lst)
return (1);
if (DEBUG)
{
dio = drawio_init(av[1]);
gen_dio_linked_list(lst, dio);
}
ast = get_ast(envp, lst);
if (!ast)
return (1);
if (DEBUG)
{
gen_dio_ast(ast, dio);
drawio_end_file(dio);
ft_debug(" draw.io file generated !\n");
}
free_linked_list(lst);
}

View File

@@ -1,44 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tmp_env.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/27 12:35:29 by nalebrun #+# #+# */
/* Updated: 2025/01/27 12:35:29 by nalebrun ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "../lib/libft/libft.h"
static int count_var(char **envp)
{
int i;
i = 0;
if (!envp)
return (-1);
while (envp[i])
i++;
return (i);
}
char **init_env(char **envp)
{
char **env;
int i;
i = 0;
env = malloc(sizeof(char *) * (count_var(envp) + 1));
if (!env)
return (NULL);
env[count_var(envp)] = NULL;
while (envp[i])
{
env[i] = ft_strdup(envp[i]);
i++;
}
return (env);
}

View File

@@ -1,93 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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 "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, 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

@@ -1,64 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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);
}

View File

@@ -1,120 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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 "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 = 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

@@ -1,66 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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, t_pres pres);
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

View File

@@ -1,101 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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);
}

View File

@@ -1,19 +0,0 @@
#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);
}