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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -39,7 +39,8 @@ void ft_exit(t_state *maiboyerlpb, t_u8 exit_status);
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;
@ -84,7 +85,7 @@ void print_node_data(t_node *t, t_usize depth)
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");
@ -101,7 +102,8 @@ t_node parse_str(t_state *state)
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"));
@ -115,11 +117,12 @@ t_node parse_str(t_state *state)
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);
} }
@ -157,7 +160,6 @@ 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);
} }
} }
@ -179,7 +181,6 @@ t_i32 main(t_i32 argc, t_str argv[], t_str envp[])
(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){};