Working now without leaks :D

This commit is contained in:
Maieul BOYER 2024-04-30 16:22:11 +02:00
commit 4518f5a727
No known key found for this signature in database
5 changed files with 70 additions and 92 deletions

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/28 14:41:15 by rparodi #+# #+# */
/* Updated: 2024/04/29 15:07:54 by rparodi ### ########.fr */
/* Updated: 2024/04/30 15:42:51 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,6 +14,7 @@
#define MINISHELL_H
# include "me/types.h"
# include "app/node.h"
# include <unistd.h>
# include <fcntl.h>
@ -29,6 +30,11 @@
# define PATH_FILES "/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games"
typedef struct s_myparser
{
t_parser *parser;
} t_myparser;
typedef struct s_utils
{
t_str name_shell;
@ -36,6 +42,8 @@ typedef struct s_utils
t_str *strs_input;
t_str *path;
t_str *envp;
t_myparser parser;
t_node current_node;
} t_utils;
t_i32 main(t_i32 argc, t_str argv[], t_str arge[]);

View file

@ -2011,13 +2011,9 @@ void ts_parser_delete(t_parser *self)
ts_parser_set_language(self, NULL);
ts_stack_delete(self->stack);
if (self->reduce_actions.buffer)
{
array_delete(&self->reduce_actions);
}
vec_reduce_action_free(self->reduce_actions);
if (self->included_range_differences.buffer)
{
array_delete(&self->included_range_differences);
}
if (self->old_tree.ptr)
{
ts_subtree_release(&self->tree_pool, self->old_tree);

View file

@ -6,39 +6,12 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/13 20:26:13 by rparodi #+# #+# */
/* Updated: 2024/04/13 20:37:13 by rparodi ### ########.fr */
/* Updated: 2024/04/30 15:30:52 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/minishell.h"
char *ft_check_cmds(t_utils *utils, char *command)
{
size_t i;
char *str;
i = 0;
str = NULL;
if (access(command, X_OK) != -1)
return (command);
else if (ft_strlen(command) != 1)
{
while (utils->path[i] != NULL)
{
if (str)
ft_free(str);
str = ft_strjoin(utils->path[i], command);
if (str == NULL)
ft_exit(utils, 1);
else if (access(str, F_OK) == -1 || access(str, X_OK) == -1)
i++;
else
return (str);
}
}
return (ft_exit(utils, 1), NULL);
}
void ft_exec_cmd(t_utils *utils, t_str cmd, t_str cmd_args[])
{
if (execve(cmd, cmd_args, utils->envp) == -1)
@ -56,7 +29,7 @@ void ft_other_cmd(t_utils *shcat, t_usize i, t_usize prev_i)
t_str *args;
t_usize k;
t_usize tmp;
t_str cmd;
// t_str cmd;
k = prev_i;
tmp = prev_i;
@ -70,12 +43,12 @@ void ft_other_cmd(t_utils *shcat, t_usize i, t_usize prev_i)
k++;
}
args[k] = NULL;
cmd = ft_check_cmds(shcat, ft_strjoin("/", args[tmp]));
// cmd = ft_check_cmds(shcat, args[tmp]);
options = 0;
pid = fork();
if (pid == -1)
ft_exit(shcat, 1);
if (pid == 0)
ft_exec_cmd(shcat, cmd, args);
// if (pid == 0)
// ft_exec_cmd(shcat, cmd, args);
waitpid(pid, NULL, options);
}

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/29 11:35:51 by rparodi #+# #+# */
/* Updated: 2024/04/30 16:07:48 by maiboyer ### ########.fr */
/* Updated: 2024/04/30 16:16:55 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -35,6 +35,9 @@ void ft_free_strs(t_str *strs)
void ft_free_utils(t_utils *s)
{
(void)(s);
if (s->str_input)
free(s->str_input);
ts_parser_delete(s->parser.parser);
}
void ft_exit(t_utils *maiboyerlpb, t_u8 exit_status)

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/28 14:40:38 by rparodi #+# #+# */
/* Updated: 2024/04/30 16:06:00 by maiboyer ### ########.fr */
/* Updated: 2024/04/30 16:15:53 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,6 +16,36 @@
#include "parser/parser.h"
#include "../includes/minishell.h"
void print_node_data(t_node *t, t_usize depth)
{
t_usize idx;
idx = 0;
while (idx++ < depth)
printf("\t");
idx = 0;
printf("%s = %s\n", t->kind_str, node_getstr(t));
while (idx < t->childs_count)
print_node_data(&t->childs[idx++], depth + 1);
}
t_node parse_to_nodes(t_parser *parser, t_const_str input)
{
t_parse_tree *tree;
t_parse_node node;
t_node ret;
tree = ts_parser_parse_string(parser, NULL, input, str_len(input));
node = ts_tree_root_node(tree);
ret = build_node(node, input);
ts_tree_delete(tree);
return (ret);
}
t_node parse_str(t_myparser *parser, t_const_str input)
{
return (parse_to_nodes(parser->parser, input));
}
void ft_check(t_utils *shcat, char **input) {
t_usize i;
t_usize prev_i;
@ -34,6 +64,12 @@ void ft_check(t_utils *shcat, char **input) {
}
}
void exec_shcat(t_utils *shcat)
{
print_node_data(&shcat->current_node, 0);
free_node(shcat->current_node);
}
void ft_take_args(t_utils *shcat)
{
t_i32 i;
@ -43,9 +79,8 @@ void ft_take_args(t_utils *shcat)
shcat->str_input = readline((t_const_str)shcat->name_shell);
if (!shcat->str_input)
ft_exit(shcat, 0);
shcat->strs_input = ft_split(shcat->str_input, ' ');
if (!shcat->strs_input)
exit(1);
shcat->current_node = parse_str(&shcat->parser, shcat->str_input);
exec_shcat(shcat);
add_history(shcat->str_input);
free(shcat->str_input);
i++;
@ -73,36 +108,8 @@ void ft_find_path(t_str arge[], t_utils *utils)
t_language *tree_sitter_bash(void);
t_node parse_to_nodes(t_parser *parser, t_const_str input)
{
t_parse_tree *tree;
t_parse_node node;
t_node ret;
tree = ts_parser_parse_string(parser, NULL, input, str_len(input));
node = ts_tree_root_node(tree);
ret = build_node(node, input);
ts_tree_delete(tree);
return (ret);
}
void print_node_data(t_node *t, t_usize depth)
{
t_usize idx;
idx = 0;
while (idx++ < depth)
printf("\t");
idx = 0;
printf("%s = %s\n", t->kind_str, node_getstr(t));
while (idx < t->childs_count)
print_node_data(&t->childs[idx++], depth + 1);
}
typedef struct s_myparser
{
t_parser *parser;
} t_myparser;
t_myparser create_myparser(void)
{
@ -120,24 +127,15 @@ void free_myparser(t_myparser self)
ts_parser_delete(self.parser);
}
t_node parse_string(t_myparser *parser, t_const_str input)
{
return (parse_to_nodes(parser->parser, input));
}
t_i32 main(t_i32 argc, t_str argv[], t_str arge[])
{
// t_myparser parser;
// t_node node;
t_utils utils;
(void)argc;
(void)argv;
utils.parser = create_myparser();
ft_find_path(arge, &utils);
utils.name_shell = "42sh > ";
ft_take_args(&utils);
// parser = create_myparser();
// node = parse_string(&parser, "banane \"$VAR\"'truc'");
// print_node_data(&node, 0);
// free_node(node);
}