normed: folder sources to the norm

This commit is contained in:
Raphaël 2024-09-06 15:47:03 +02:00
parent 9ee2fa851c
commit b36e835dfb
3 changed files with 74 additions and 49 deletions

View file

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* _env_norm_helper.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/06 15:39:47 by rparodi #+# #+# */
/* Updated: 2024/09/06 15:40:16 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "app/env.h"
#include "me/string/string.h"
#include "me/hash/hasher.h"
#include "me/hashmap/hashmap_env.h"
#include "me/mem/mem.h"
#include "me/str/str.h"
#include "me/str/str.h"
#include "me/str/str.h"
#include "me/types.h"
#include "me/vec/vec_str.h"
#include <stdlib.h>
t_error build_envp(\
t_hashmap_env *envs, t_hashmap_env *tmp_vars, t_vec_str *envp)
{
struct s_build_envp_state state;
state.tmp_vars = tmp_vars;
state.buf = string_new(8096);
state.out = vec_str_new(1024, (void (*)(t_str))mem_free);
if (hmap_env_iter(envs, _build_envp_iterator, &state))
return (string_free(state.buf), ERROR);
if (vec_str_push(&state.out, NULL))
return (string_free(state.buf), ERROR);
*envp = state.out;
string_free(state.buf);
return (NO_ERROR);
}

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/04 18:32:50 by maiboyer #+# #+# */ /* Created: 2024/05/04 18:32:50 by maiboyer #+# #+# */
/* Updated: 2024/08/03 16:31:34 by maiboyer ### ########.fr */ /* Updated: 2024/09/06 15:40:19 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -71,19 +71,3 @@ t_error _build_envp_iterator(t_usize idx, const t_str *key, t_str *val,
string_clear(&s->buf); string_clear(&s->buf);
return (NO_ERROR); return (NO_ERROR);
} }
t_error build_envp(t_hashmap_env *envs, t_hashmap_env *tmp_vars, t_vec_str *envp)
{
struct s_build_envp_state state;
state.tmp_vars = tmp_vars;
state.buf = string_new(8096);
state.out = vec_str_new(1024, (void (*)(t_str))mem_free);
if (hmap_env_iter(envs, _build_envp_iterator, &state))
return (string_free(state.buf), ERROR);
if (vec_str_push(&state.out, NULL))
return (string_free(state.buf), ERROR);
*envp = state.out;
string_free(state.buf);
return (NO_ERROR);
}

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/28 14:40:38 by rparodi #+# #+# */ /* Created: 2024/03/28 14:40:38 by rparodi #+# #+# */
/* Updated: 2024/08/19 14:22:34 by maiboyer ### ########.fr */ /* Updated: 2024/09/06 15:46:18 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -27,21 +27,22 @@
#include <errno.h> #include <errno.h>
#include <sys/types.h> #include <sys/types.h>
t_error ast_from_node(t_parse_node node, t_str input, t_ast_node *out); t_error ast_from_node(t_parse_node node, t_str input, t_ast_node *out);
void ast_print_node(t_ast_node self); void ast_print_node(t_ast_node self);
void ft_exit(t_state *maiboyerlpb, t_u8 exit_status); void ft_exit(t_state *maiboyerlpb, t_u8 exit_status);
// Foutre envp dans env // Foutre envp dans env
// Chaque elemenet d'envp split au premier = // Chaque elemenet d'envp split au premier =
// cle avant le = // cle avant le =
// data apres le = // data apres le =
t_language *tree_sitter_sh(void); t_language *tree_sitter_sh(void);
void ast_free(t_ast_node node); void ast_free(t_ast_node node);
t_error split_str_first(t_const_str s, char splitter, t_str *before, t_str *after) t_error split_str_first(\
t_const_str s, char splitter, t_str *before, t_str *after)
{ {
t_usize i; t_usize i;
if (s == NULL || before == NULL || after == NULL || splitter == '\0') if (s == NULL || before == NULL || after == NULL || splitter == '\0')
return (ERROR); return (ERROR);
@ -57,9 +58,9 @@ t_error split_str_first(t_const_str s, char splitter, t_str *before, t_str *afte
return (NO_ERROR); return (NO_ERROR);
} }
t_error populate_env(t_hashmap_env *env, t_str envp[]) t_error populate_env(t_hashmap_env *env, t_str envp[])
{ {
t_usize i; t_usize i;
t_str temp[2]; t_str temp[2];
i = 0; i = 0;
@ -78,13 +79,13 @@ t_error populate_env(t_hashmap_env *env, t_str envp[])
return (NO_ERROR); return (NO_ERROR);
} }
void print_node_data(t_node *t, t_usize depth) void print_node_data(t_node *t, t_usize depth)
{ {
t_usize idx; t_usize idx;
idx = 0; idx = 0;
if (t->kind == 7) if (t->kind == 7)
return; return ;
printf("\x1b[%im[%-6s](%lu)\x1b[0m", t->field_str == NULL ? 90 : 32, t->field_str == NULL ? "nil" : t->field_str, t->field); printf("\x1b[%im[%-6s](%lu)\x1b[0m", t->field_str == NULL ? 90 : 32, t->field_str == NULL ? "nil" : t->field_str, t->field);
while (idx++ < depth + 1) while (idx++ < depth + 1)
printf("\t"); printf("\t");
@ -94,14 +95,15 @@ void print_node_data(t_node *t, t_usize depth)
print_node_data(&t->childs[idx++], depth + 1); print_node_data(&t->childs[idx++], depth + 1);
} }
t_node parse_str(t_state *state) t_node parse_str(t_state *state)
{ {
t_first_tree *tree; t_first_tree *tree;
t_parse_node node; t_parse_node node;
t_node ret; t_node ret;
t_ast_node out; t_ast_node out;
tree = ts_parser_parse_string(state->parser, state->str_input, str_len(state->str_input)); tree = ts_parser_parse_string(\
state->parser, state->str_input, str_len(state->str_input));
node = ts_tree_root_node(tree); node = ts_tree_root_node(tree);
if (ast_from_node(node, state->str_input, &out)) if (ast_from_node(node, state->str_input, &out))
(state->ast = NULL, printf("Error when building node\n")); (state->ast = NULL, printf("Error when building node\n"));
@ -112,21 +114,22 @@ t_node parse_str(t_state *state)
return (ret); return (ret);
} }
void exec_shcat(t_state *state) void exec_shcat(t_state *state)
{ {
t_program_result prog_res; t_program_result prog_res;
/* */
prog_res = (t_program_result){.exit = 0}; prog_res = (t_program_result){.exit = 0};
print_node_data(&state->current_node, 0); print_node_data(&state->current_node, 0);
free_node(state->current_node); free_node(state->current_node);
if (state->ast != NULL && run_program(&state->ast->data.program, state, &prog_res)) if (state->ast != NULL && run_program(\
&state->ast->data.program, state, &prog_res))
printf("Error when execting the Command \n"); printf("Error when execting the Command \n");
ast_free(state->ast); ast_free(state->ast);
} }
t_error get_user_input(t_state *state) t_error get_user_input(t_state *state)
{ {
t_line_state lstate; t_line_state lstate;
if (line_edit_start(&lstate, get_stdin(), get_stdout(), state->prompt)) if (line_edit_start(&lstate, get_stdin(), get_stdout(), state->prompt))
return (ERROR); return (ERROR);
@ -145,7 +148,7 @@ t_error get_user_input(t_state *state)
return (NO_ERROR); return (NO_ERROR);
} }
void ft_take_args(t_state *state) void ft_take_args(t_state *state)
{ {
while (true) while (true)
{ {
@ -157,15 +160,14 @@ void ft_take_args(t_state *state)
line_history_add(state->str_input); line_history_add(state->str_input);
state->current_node = parse_str(state); state->current_node = parse_str(state);
exec_shcat(state); exec_shcat(state);
/* ft_exit(state, 42); */
mem_free(state->str_input); mem_free(state->str_input);
} }
} }
t_first_parser *create_myparser(void) t_first_parser *create_myparser(void)
{ {
t_language *lang; t_language *lang;
t_first_parser *parser; t_first_parser *parser;
lang = tree_sitter_sh(); lang = tree_sitter_sh();
parser = ts_parser_new(); parser = ts_parser_new();
@ -173,13 +175,12 @@ t_first_parser *create_myparser(void)
return (parser); return (parser);
} }
t_i32 main(t_i32 argc, t_str argv[], t_str envp[]) t_i32 main(t_i32 argc, t_str argv[], t_str envp[])
{ {
t_state state; t_state state;
(void)argc; (void)argc;
(void)argv; (void)argv;
(void)envp;
if (install_signal()) if (install_signal())
me_abort("Unable to install signals"); me_abort("Unable to install signals");
state = (t_state){}; state = (t_state){};