cleenup
This commit is contained in:
102
test/ast/ast.c
Normal file
102
test/ast/ast.c
Normal file
@@ -0,0 +1,102 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ast.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/01/24 08:22:16 by nalebrun #+# #+# */
|
||||
/* Updated: 2025/01/24 08:22:16 by nalebrun ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ast.h"
|
||||
|
||||
t_ast_n *created_ast_n(t_state st, t_ast_n *prt, t_ast_n *he)
|
||||
{
|
||||
t_ast_n *node;
|
||||
|
||||
node = malloc(sizeof(t_ast_n));
|
||||
if (!node)
|
||||
return (NULL);
|
||||
node->state = st;
|
||||
node->parent = prt;
|
||||
node->head = he;
|
||||
node->ex_code = 0;
|
||||
node->cmd = NULL;
|
||||
node->args = NULL;
|
||||
node->_stdout = 1;
|
||||
node->_stdin = 0;
|
||||
node->redir = _NR;
|
||||
node->infile = NULL;
|
||||
node->outfile = NULL;
|
||||
node->left = NULL;
|
||||
node->right = NULL;
|
||||
node->pline = NULL;
|
||||
if (prt)
|
||||
node->shlvl = prt->shlvl;
|
||||
else
|
||||
node->shlvl = 0;
|
||||
node->env = NULL;
|
||||
return (node);
|
||||
}
|
||||
|
||||
void setup_cmd(t_ast_n *node, char *cmd, char *args)
|
||||
{
|
||||
node->cmd = ft_strdup(cmd);
|
||||
node->args = ft_split(args, " ");
|
||||
}
|
||||
|
||||
t_ast_n *get_ast(char **envp, t_node *lst)
|
||||
{
|
||||
t_ast_n *head;
|
||||
|
||||
(void)lst;
|
||||
// head
|
||||
head = created_ast_n(_AND, NULL, NULL);
|
||||
head->env = init_env(envp);
|
||||
|
||||
// left
|
||||
head->left = created_ast_n(_AND, head, head);
|
||||
|
||||
// left
|
||||
head->left->left = created_ast_n(_CMD, head->left, head);
|
||||
setup_cmd(head->left->left, "echo", "echo coucou");
|
||||
//
|
||||
// right
|
||||
head->left->right = created_ast_n(_OR, head->left, head);
|
||||
//
|
||||
// left
|
||||
head->left->right->left = created_ast_n(_CMD, head->left->right, head);
|
||||
setup_cmd(head->left->right->left, "echo", "echo coucou");
|
||||
|
||||
// right
|
||||
head->left->right->right = created_ast_n(_CMD, head->left->right, head);
|
||||
setup_cmd(head->left->right->right, "echo", "echo coucou");
|
||||
|
||||
// right
|
||||
head->right = created_ast_n(_PLINE, head, head);
|
||||
head->right->pline = malloc(sizeof(t_ast_n *) * 4);
|
||||
// ===0===
|
||||
head->right->pline[0] = created_ast_n(_CMD, head->right, head);
|
||||
setup_cmd(head->right->pline[0], "ls", "ls");
|
||||
// ===1===
|
||||
head->right->pline[1] = created_ast_n(_AND, head->right, head);
|
||||
|
||||
// left
|
||||
head->right->pline[1]->left = created_ast_n(_CMD, head->right->pline[1], head);
|
||||
setup_cmd(head->right->pline[1]->left, "echo", "echo coucou");
|
||||
|
||||
// right
|
||||
head->right->pline[1]->right = created_ast_n(_CMD, head->right->pline[1], head);
|
||||
setup_cmd(head->right->pline[1]->right, "echo", "echo coucou");
|
||||
|
||||
// ===2===
|
||||
head->right->pline[2] = created_ast_n(_CMD, head->right, head);
|
||||
setup_cmd(head->right->pline[2], "cat", "cat -e");
|
||||
|
||||
// ===NULL===
|
||||
head->right->pline[3] = NULL;
|
||||
|
||||
return (head);
|
||||
}
|
||||
63
test/ast/ast.h
Normal file
63
test/ast/ast.h
Normal file
@@ -0,0 +1,63 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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
|
||||
} 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;
|
||||
|
||||
t_ast_n *get_ast(char **envp, t_node *lst);
|
||||
|
||||
// env TMP
|
||||
char **init_env(char **envp);
|
||||
|
||||
#endif
|
||||
44
test/drawio/drawio.h
Normal file
44
test/drawio/drawio.h
Normal file
@@ -0,0 +1,44 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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);
|
||||
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
|
||||
35
test/drawio/drawio_ast.c
Normal file
35
test/drawio/drawio_ast.c
Normal file
@@ -0,0 +1,35 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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 ;
|
||||
}
|
||||
69
test/drawio/drawio_ast_utils.c
Normal file
69
test/drawio/drawio_ast_utils.c
Normal file
@@ -0,0 +1,69 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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
|
||||
out = "UNDEFINED";
|
||||
return (out);
|
||||
}
|
||||
|
||||
const char *translate_redir(t_redir redir)
|
||||
{
|
||||
const char *out;
|
||||
|
||||
if (redir == _RED_L)
|
||||
out = "redir : < ";
|
||||
else if (redir == _RED_R)
|
||||
out = "redir : > ";
|
||||
else if (redir == _RED_DR)
|
||||
out = "redir : >> ";
|
||||
else
|
||||
out = "Not redirected ";
|
||||
return (out);
|
||||
}
|
||||
|
||||
t_dio_node get_cmd_txt(t_ast_n *node)
|
||||
{
|
||||
t_dio_node txt;
|
||||
|
||||
txt.st = translate_state(node->state);
|
||||
if (node->state == _CMD)
|
||||
{
|
||||
txt.cmd = ft_sprintf("%s%s%s", NL, node->cmd, NL);
|
||||
txt.args = ft_sprintf(ft_tabstr(node->args), NL);
|
||||
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);
|
||||
}
|
||||
46
test/drawio/drawio_linklist.c
Normal file
46
test/drawio/drawio_linklist.c
Normal file
@@ -0,0 +1,46 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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)
|
||||
{
|
||||
rect->type = RECT;
|
||||
rect->rounded = 0;
|
||||
rect->x = 50;
|
||||
rect->y = 50;
|
||||
rect->w = 100;
|
||||
rect->h = 50;
|
||||
}
|
||||
|
||||
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);
|
||||
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;
|
||||
}
|
||||
}
|
||||
73
test/drawio/drawio_print_ast.c
Normal file
73
test/drawio/drawio_print_ast.c
Normal file
@@ -0,0 +1,73 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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[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);
|
||||
}
|
||||
58
test/drawio/drawio_utils.c
Normal file
58
test/drawio/drawio_utils.c
Normal file
@@ -0,0 +1,58 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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_amp_count(char *str)
|
||||
{
|
||||
int i;
|
||||
int count;
|
||||
|
||||
i = 0;
|
||||
count = 0;
|
||||
while (str[i])
|
||||
{
|
||||
if (str[i] == '&')
|
||||
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_amp_count(src);
|
||||
out = malloc(ft_strlen(src) + amp_count * 4);
|
||||
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;
|
||||
return (out);
|
||||
}
|
||||
27
test/parsing.h
Normal file
27
test/parsing.h
Normal file
@@ -0,0 +1,27 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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
|
||||
69
test/test.c
Normal file
69
test/test.c
Normal file
@@ -0,0 +1,69 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* parse.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/01/15 08:23:41 by nalebrun #+# #+# */
|
||||
/* Updated: 2025/01/20 13:15:47 by nalebrun ### ########.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;
|
||||
|
||||
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);
|
||||
}
|
||||
44
test/tmp_env.c
Normal file
44
test/tmp_env.c
Normal file
@@ -0,0 +1,44 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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);
|
||||
}
|
||||
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