bebou t tro for

This commit is contained in:
Raphaël 2024-05-09 17:16:07 +02:00
parent b5c7344851
commit 1252ac48a8
15 changed files with 214 additions and 25 deletions

View file

@ -0,0 +1,69 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* handle_command.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/09 15:00:53 by rparodi #+# #+# */
/* Updated: 2024/05/09 17:14:32 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "app/env.h"
#include "app/state.h"
#include "me/types.h"
#include "gmr/symbols.h"
#include "app/node.h"
#include "me/vec/vec_str.h"
// #include "app/node/handle_program.h"
#include "app/node/handle_command.h"
#include "minishell.h"
#include "me/string/str_clone.h"
#include <time.h>
t_error handle_command(t_node *self, t_utils *shcat, t_i32 *out_exit_code)
{
t_usize i;
t_spawn_info spawn_info;
t_str tmp;
spawn_info.arguments = vec_str_new(self->childs_count, (void (*)(t_str))free);
if (self->kind != sym_command)
return (ERROR);
i = 0;
while (i < self->childs_count)
{
if (self->childs[i].kind == sym_command_name)
spawn_info.binary_path = str_clone(node_getstr(&self->childs[i]));
else if (self->childs[i].kind == sym_file_redirect)
printf("PAS ENCORE HANDLE FDP redirect!\n");
else if (self->childs[i].kind == sym_variable_assignment)
printf("PAS ENCORE HANDLE FDP asignement!\n");
else
{
printf("%s %s\n", self->childs[i].kind_str, node_getstr(&self->childs[i]));
if (handle_node_getstr(&self->childs[i], shcat, &tmp))
return (vec_str_free(spawn_info.arguments), ERROR);
if (vec_str_push(&spawn_info.arguments, tmp))
return (ERROR);
}
i++;
}
vec_str_push_front(&spawn_info.arguments, str_clone(spawn_info.binary_path));
vec_str_push(&spawn_info.arguments, NULL);
spawn_info.stdin = inherited();
spawn_info.stdout = inherited();
spawn_info.stderr = inherited();
spawn_info.forked_free = NULL;
if (build_envp(shcat->env, &spawn_info.environement))
return (vec_str_free(spawn_info.arguments), ERROR);
if (spawn_process(spawn_info, &shcat->ret))
return (ERROR);
waitpid(shcat->ret.pid, NULL, 0);
close(shcat->ret.stdin.vals.ro.fd);
close(shcat->ret.stdout.vals.ro.fd);
close(shcat->ret.stderr.vals.ro.fd);
*out_exit_code = 0;
return (NO_ERROR);
}

View file

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* handle_program.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/09 14:34:48 by rparodi #+# #+# */
/* Updated: 2024/05/09 15:06:42 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
# include "app/state.h"
# include "me/types.h"
# include "gmr/symbols.h"
# include "app/node.h"
# include "app/node/handle_program.h"
# include "app/node/handle_command.h"
t_error handle_program(t_node *self, t_utils *shcat, t_i32 *out_exit_code)
{
t_usize i;
if (self->kind != sym_program)
return (ERROR);
i = 0;
while (i < self->childs_count)
{
if (self->childs[i].kind == sym_command)
if (handle_command(&self->childs[i], shcat, out_exit_code))
return (ERROR);
i++;
}
return (NO_ERROR);
}

View file

@ -6,17 +6,20 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/28 14:40:38 by rparodi #+# #+# */
/* Updated: 2024/05/04 19:21:16 by maiboyer ### ########.fr */
/* Updated: 2024/05/09 16:51:52 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "app/env.h"
#include "app/node.h"
#include "app/node/handle_program.h"
#include "app/signal_handler.h"
#include "gmr/symbols.h"
#include "me/string/str_len.h"
#include "minishell.h"
#include "parser/api.h"
#include "app/node/handle_concat.h"
#include <sys/types.h>
t_first_parser *ts_parser_new();
void ts_tree_delete(t_first_tree *);
@ -26,6 +29,21 @@ t_first_tree *ts_parser_parse_string(t_first_parser *, t_first_tree *oldtree,
void ts_parser_delete(t_first_parser *self);
void ts_parser_set_language(t_first_parser *self, t_language *lang);
t_error handle_node_getstr(t_node *self, t_utils *shcat, t_str *out)
{
switch (self->kind)
{
case sym_word:
*out = node_getstr(self);
return (NO_ERROR);
case sym_concatenation:
return (handle_concat(self, shcat, out));
default:
return (ERROR);
}
}
void print_node_data(t_node *t, t_usize depth)
{
t_usize idx;
@ -98,9 +116,12 @@ void print_node_concat(t_node *self)
void exec_shcat(t_utils *shcat)
{
t_i32 ret;
print_node_data(&shcat->current_node, 0);
print_node_concat(&shcat->current_node);
handle_program(&shcat->current_node, shcat, &ret);
free_node(shcat->current_node);
(void)ret;
}
void ft_take_args(t_utils *shcat)
@ -122,7 +143,7 @@ void ft_find_path(t_str arge[], t_utils *utils)
{
t_i32 i;
i = 0;
i = 0;
while (arge[i] != NULL)
{
if (arge[i][0] == 'P' && arge[i][1] == 'A' && arge[i][2] == 'T' &&
@ -161,11 +182,11 @@ t_i32 main(t_i32 argc, t_str argv[], t_str envp[])
(void)argc;
(void)argv;
(void)envp;
utils = (t_utils){};
utils.parser = create_myparser();
utils.env = create_env_map();
if (install_signal())
return (1);
utils = (t_utils){};
utils.env = create_env_map();
utils.parser = create_myparser();
utils.name_shell = "\001\x1B[93m\002"
"42sh"
"\001\x1B[32m\002"

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/28 18:36:40 by maiboyer #+# #+# */
/* Updated: 2024/04/30 16:43:35 by maiboyer ### ########.fr */
/* Updated: 2024/05/09 17:07:22 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
@ -88,6 +88,6 @@ void free_node(t_node t)
while (idx < t.childs_count)
free_node(t.childs[idx++]);
free(t.childs);
if (t.single_str != NULL)
free(t.single_str);
// if (t.single_str != NULL)
// free(t.single_str);
}