Start work on exec

This commit is contained in:
Raphaël 2024-04-30 15:49:40 +02:00
parent 1b2f6e4225
commit 24d122dc54
3 changed files with 59 additions and 79 deletions

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/28 14:40:38 by rparodi #+# #+# */
/* Updated: 2024/04/30 13:02:39 by maiboyer ### ########.fr */
/* Updated: 2024/04/30 15:46:55 by rparodi ### ########.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,11 @@ void ft_check(t_utils *shcat, char **input) {
}
}
void exec_shcat(t_utils *shcat)
{
print_node_data(&shcat->current_node, 0);
}
void ft_take_args(t_utils *shcat)
{
t_i32 i;
@ -43,11 +78,9 @@ 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);
ft_check(shcat, shcat->strs_input);
add_history(shcat->str_input);
shcat->current_node = parse_str(&shcat->parser, shcat->str_input);
exec_shcat(shcat);
add_history(shcat->str_input);
ft_free_strs(shcat->strs_input);
free(shcat->str_input);
i++;
@ -75,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)
{
@ -122,23 +127,17 @@ 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);