Updated: normed everything except the big ass file

This commit is contained in:
Maieul BOYER 2024-08-30 19:30:20 +02:00
parent fa5990b00c
commit 0ebc161857
No known key found for this signature in database
6 changed files with 53 additions and 86 deletions

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/27 21:13:15 by rparodi #+# #+# */ /* Created: 2024/07/27 21:13:15 by rparodi #+# #+# */
/* Updated: 2024/08/14 16:38:32 by rparodi ### ########.fr */ /* Updated: 2024/08/30 19:29:43 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -54,4 +54,8 @@ t_ast_node _arith_postfix_to_ast_node(t_ast_arithmetic_postfix *self);
t_ast_node _arith_ternary_to_ast_node(t_ast_arithmetic_ternary *self); t_ast_node _arith_ternary_to_ast_node(t_ast_arithmetic_ternary *self);
t_ast_node _arith_unary_to_ast_node(t_ast_arithmetic_unary *self); t_ast_node _arith_unary_to_ast_node(t_ast_arithmetic_unary *self);
t_error _unary_get_op(t_ast_arithmetic_operator op, t_arith_op_func *out);
t_error _binary_get_op(t_ast_arithmetic_operator op, t_arith_op_func *out);
t_error _postfix_get_op(t_ast_arithmetic_operator op, t_arith_op_func *out);
#endif #endif

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/30 19:12:38 by maiboyer #+# #+# */ /* Created: 2024/08/30 19:12:38 by maiboyer #+# #+# */
/* Updated: 2024/08/30 19:13:26 by maiboyer ### ########.fr */ /* Updated: 2024/08/30 19:25:57 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,4 +16,11 @@
# include "exec/_builtins.h" # include "exec/_builtins.h"
# include "exec/_builtins_func.h" # include "exec/_builtins_func.h"
struct s_assign_export_state
{
t_state *state;
t_builtin_spawn_info *info;
t_error err;
};
#endif /* BUILTINS_H */ #endif /* BUILTINS_H */

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/10 18:43:18 by maiboyer #+# #+# */ /* Created: 2024/08/10 18:43:18 by maiboyer #+# #+# */
/* Updated: 2024/08/30 16:51:53 by rparodi ### ########.fr */ /* Updated: 2024/08/30 19:17:18 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,9 +17,7 @@
#include "me/string/string.h" #include "me/string/string.h"
#include "me/types.h" #include "me/types.h"
/*#define DEBUG_USAGE\*/ #define USG "Usage:\n - print_fd: print the opened file descritors informations"
/* "Usage:\n"\*/
/* " - print_fd: print the opened file descritors informations"*/
static t_error _debug_fd(\ static t_error _debug_fd(\
t_state *state, t_builtin_spawn_info info, t_i32 *exit_code) t_state *state, t_builtin_spawn_info info, t_i32 *exit_code)
@ -50,7 +48,7 @@ t_error builtin_debug_(\
t_state *state, t_builtin_spawn_info info, t_i32 *exit_code) t_state *state, t_builtin_spawn_info info, t_i32 *exit_code)
{ {
if (info.args.len != 2) if (info.args.len != 2)
return (me_printf_fd(info.stdout, DEBUG_USAGE), *exit_code = 1, ERROR); return (me_printf_fd(info.stdout, USG), *exit_code = 1, ERROR);
if (str_compare(info.args.buffer[1], "print_fd")) if (str_compare(info.args.buffer[1], "print_fd"))
return (_debug_fd(state, info, exit_code)); return (_debug_fd(state, info, exit_code));
*exit_code = 2; *exit_code = 2;

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/07 14:22:50 by rparodi #+# #+# */ /* Created: 2024/08/07 14:22:50 by rparodi #+# #+# */
/* Updated: 2024/08/30 17:53:25 by rparodi ### ########.fr */ /* Updated: 2024/08/30 19:23:36 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,7 +16,11 @@
#include "me/str/str.h" #include "me/str/str.h"
#include "me/types.h" #include "me/types.h"
static t_error _append_key_to_vec(\ t_error _append_key_to_vec(t_usize _, const t_str *key, t_str *v, void *vec);
bool _sort_str(t_str *_lhs, t_str *_rhs);
t_error get_uniq_keys(t_state *state, t_vec_str *out);
t_error _append_key_to_vec(\
t_usize _idx, const t_str *key, t_str *value, void *vec) t_usize _idx, const t_str *key, t_str *value, void *vec)
{ {
(void)(value); (void)(value);
@ -27,7 +31,7 @@ static t_error _append_key_to_vec(\
return (NO_ERROR); return (NO_ERROR);
} }
static bool _sort_str(t_str *_lhs, t_str *_rhs) bool _sort_str(t_str *_lhs, t_str *_rhs)
{ {
t_str lhs; t_str lhs;
t_str rhs; t_str rhs;
@ -48,13 +52,11 @@ static bool _sort_str(t_str *_lhs, t_str *_rhs)
return (*lhs < *rhs); return (*lhs < *rhs);
} }
t_error builtin_env___(\ t_error get_uniq_keys(t_state *state, t_vec_str *out)
t_state *state, t_builtin_spawn_info info, t_i32 *exit_code)
{ {
t_usize i;
t_vec_str keys; t_vec_str keys;
t_vec_str keys_uniq; t_vec_str keys_uniq;
t_usize i;
t_str *value;
keys = vec_str_new(16, NULL); keys = vec_str_new(16, NULL);
hmap_env_iter(state->env, _append_key_to_vec, &keys); hmap_env_iter(state->env, _append_key_to_vec, &keys);
@ -62,7 +64,7 @@ t_error builtin_env___(\
keys_uniq = vec_str_new(keys.len, NULL); keys_uniq = vec_str_new(keys.len, NULL);
i = 0; i = 0;
if (keys.len == 0) if (keys.len == 0)
return (NO_ERROR); return (vec_str_free(keys), *out = keys_uniq, NO_ERROR);
vec_str_sort(&keys, _sort_str); vec_str_sort(&keys, _sort_str);
while (i < keys.len) while (i < keys.len)
{ {
@ -73,6 +75,18 @@ t_error builtin_env___(\
i++; i++;
} }
vec_str_free(keys); vec_str_free(keys);
return (*out = keys_uniq, NO_ERROR);
}
t_error builtin_env___(\
t_state *state, t_builtin_spawn_info info, t_i32 *exit_code)
{
t_usize i;
t_str *value;
t_vec_str keys_uniq;
if (get_uniq_keys(state, &keys_uniq))
return (ERROR);
i = 0; i = 0;
while (i < keys_uniq.len) while (i < keys_uniq.len)
{ {

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/07 14:13:41 by rparodi #+# #+# */ /* Created: 2024/08/07 14:13:41 by rparodi #+# #+# */
/* Updated: 2024/08/30 16:59:21 by rparodi ### ########.fr */ /* Updated: 2024/08/30 19:25:49 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -18,12 +18,9 @@
#include "me/types.h" #include "me/types.h"
#include "me/vec/vec_str.h" #include "me/vec/vec_str.h"
struct s_assign_export_state t_error _append_key_to_vec(t_usize _, const t_str *key, t_str *v, void *vec);
{ bool _sort_str(t_str *_lhs, t_str *_rhs);
t_state *state; t_error get_uniq_keys(t_state *state, t_vec_str *out);
t_builtin_spawn_info *info;
t_error err;
};
static void _assign_export(t_usize idx, t_str *arg, void *vctx) static void _assign_export(t_usize idx, t_str *arg, void *vctx)
{ {
@ -44,38 +41,6 @@ static void _assign_export(t_usize idx, t_str *arg, void *vctx)
ctx->err = ERROR; ctx->err = ERROR;
} }
static t_error _append_key_to_vec(\
t_usize _idx, const t_str *key, t_str *value, void *vec)
{
(void)(value);
(void)(_idx);
if (key == NULL || *key == NULL)
return (NO_ERROR);
vec_str_push(vec, *key);
return (NO_ERROR);
}
static bool _sort_str(t_str *_lhs, t_str *_rhs)
{
t_str lhs;
t_str rhs;
if (_lhs == NULL && _rhs != NULL)
return (true);
if (_lhs != NULL && _rhs == NULL)
return (true);
if (_lhs == NULL && _rhs == NULL)
return (false);
lhs = *_lhs;
rhs = *_rhs;
while (*lhs && *lhs == *rhs)
{
lhs++;
rhs++;
}
return (*lhs < *rhs);
}
static t_error handle_quotes(t_str raw, t_string *out) static t_error handle_quotes(t_str raw, t_string *out)
{ {
t_usize i; t_usize i;
@ -99,51 +64,30 @@ static t_error handle_quotes(t_str raw, t_string *out)
t_error _print_export_env(\ t_error _print_export_env(\
t_state *state, t_builtin_spawn_info info, t_i32 *exit_code) t_state *state, t_builtin_spawn_info info, t_i32 *exit_code)
{ {
t_vec_str keys; t_vec_str uniq;
t_vec_str keys_uniq;
t_usize i; t_usize i;
t_str *value; t_str *value;
t_string buf; t_string buf;
keys = vec_str_new(16, NULL); if (get_uniq_keys(state, &uniq))
hmap_env_iter(state->env, _append_key_to_vec, &keys); return (ERROR);
hmap_env_iter(state->tmp_var, _append_key_to_vec, &keys);
keys_uniq = vec_str_new(keys.len, NULL);
i = 0; i = 0;
if (keys.len == 0) while (i < uniq.len)
return (NO_ERROR);
vec_str_sort(&keys, _sort_str);
while (i < keys.len)
{ {
while (i < keys.len - 1 && \ value = hmap_env_get(state->tmp_var, &uniq.buffer[i]);
str_compare(keys.buffer[i], keys.buffer[i + 1]))
i++;
vec_str_push(&keys_uniq, keys.buffer[i]);
i++;
}
vec_str_free(keys);
i = 0;
while (i < keys_uniq.len)
{
value = hmap_env_get(state->tmp_var, &keys_uniq.buffer[i]);
if (value == NULL) if (value == NULL)
value = hmap_env_get(state->env, &keys_uniq.buffer[i]); value = hmap_env_get(state->env, &uniq.buffer[i]);
if (value == NULL || *value == NULL) if (value == NULL || *value == NULL)
me_printf_fd(info.stdout, "export %s\n", keys_uniq.buffer[i]); me_printf_fd(info.stdout, "export %s\n", uniq.buffer[i]);
else else
{ {
if (!handle_quotes(*value, &buf)) if (!handle_quotes(*value, &buf))
{ (me_printf_fd(\
me_printf_fd(\ info.stdout, "export %s='%s'\n", uniq.buffer[i], buf.buf), string_free(buf));
info.stdout, "export %s='%s'\n", keys_uniq.buffer[i], buf.buf);
string_free(buf);
}
} }
i++; i++;
} }
vec_str_free(keys_uniq); return (vec_str_free(uniq), *exit_code = 0, NO_ERROR);
*exit_code = 0;
return (NO_ERROR);
} }
t_error builtin_export(\ t_error builtin_export(\

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/26 15:14:50 by maiboyer #+# #+# */ /* Created: 2024/07/26 15:14:50 by maiboyer #+# #+# */
/* Updated: 2024/08/30 18:02:38 by rparodi ### ########.fr */ /* Updated: 2024/08/30 19:26:44 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */