Updated export and env to use similar printing methods

This commit is contained in:
Maix0 2024-08-18 22:58:40 +02:00
parent f22e420b9b
commit a6246a52a0
2 changed files with 65 additions and 51 deletions

View file

@ -6,43 +6,82 @@
/* 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/14 18:14:46 by maiboyer ### ########.fr */ /* Updated: 2024/08/18 22:54:31 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "exec/builtins.h" #include "exec/builtins.h"
#include "me/hashmap/hashmap_env.h" #include "me/hashmap/hashmap_env.h"
#include "me/printf/printf.h" #include "me/printf/printf.h"
#include "me/str/str.h"
#include "me/types.h" #include "me/types.h"
struct s_print_env_state static t_error _append_key_to_vec(t_usize _idx, const t_str *key, t_str *value, void *vec)
{ {
t_state *state; (void)(value);
t_builtin_spawn_info *info;
};
static t_error _print_env(t_usize _idx, const t_str *key, t_str *value, void *vctx)
{
const struct s_print_env_state *ctx = vctx;
t_str *val;
(void)(_idx); (void)(_idx);
val = hmap_env_get(ctx->state->tmp_var, (t_str *)key); if (key == NULL || *key == NULL)
if (val == NULL)
val = value;
if (val == NULL || *val == NULL)
return (NO_ERROR); return (NO_ERROR);
// TODO: Fix this to handle the corrrect output vec_str_push(vec, *key);
me_printf_fd(ctx->info->stdout, "%s=%s\n", *key, *val);
return (NO_ERROR); 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);
}
t_error builtin_env___(t_state *state, t_builtin_spawn_info info, t_i32 *exit_code) t_error builtin_env___(t_state *state, t_builtin_spawn_info info, t_i32 *exit_code)
{ {
struct s_print_env_state ctx; t_vec_str keys;
t_vec_str keys_uniq;
t_usize i;
t_str *value;
ctx.info = &info; keys = vec_str_new(16, NULL);
ctx.state = state; hmap_env_iter(state->env, _append_key_to_vec, &keys);
hmap_env_iter(state->env, _print_env, &ctx); hmap_env_iter(state->tmp_var, _append_key_to_vec, &keys);
return (*exit_code = 0, NO_ERROR); keys_uniq = vec_str_new(keys.len, NULL);
i = 0;
if (keys.len == 0)
return (NO_ERROR);
vec_str_sort(&keys, _sort_str);
while (i < keys.len)
{
while (i < keys.len - 1 && 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)
value = hmap_env_get(state->env, &keys_uniq.buffer[i]);
if (value != NULL && *value != NULL)
me_printf_fd(info.stdout, "%s=%s\n", keys_uniq.buffer[i], *value);
i++;
}
vec_str_free(keys_uniq);
*exit_code = 0;
return (NO_ERROR);
} }

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/18 21:51:31 by maiboyer ### ########.fr */ /* Updated: 2024/08/18 22:54:44 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -18,12 +18,6 @@
#include "me/types.h" #include "me/types.h"
#include "me/vec/vec_str.h" #include "me/vec/vec_str.h"
struct s_print_export_state
{
t_state *state;
t_builtin_spawn_info *info;
};
struct s_assign_export_state struct s_assign_export_state
{ {
t_state *state; t_state *state;
@ -50,25 +44,6 @@ static void _assign_export(t_usize idx, t_str *arg, void *vctx)
ctx->err = ERROR; ctx->err = ERROR;
} }
t_error _print_export(t_usize _idx, const t_str *key, t_str *value, void *vctx)
{
const struct s_print_export_state *ctx = vctx;
t_str *val;
t_str true_val;
(void)(_idx);
val = hmap_env_get(ctx->state->tmp_var, (t_str *)key);
if (val == NULL)
val = value;
if (val == NULL)
return (NO_ERROR);
true_val = *val;
if (true_val == NULL)
true_val = "";
me_printf_fd(ctx->info->stdout, "export %s='%s'\n", *key, true_val);
return (NO_ERROR);
}
static t_error _append_key_to_vec(t_usize _idx, const t_str *key, t_str *value, void *vec) static t_error _append_key_to_vec(t_usize _idx, const t_str *key, t_str *value, void *vec)
{ {
(void)(value); (void)(value);
@ -79,7 +54,7 @@ static t_error _append_key_to_vec(t_usize _idx, const t_str *key, t_str *value,
return (NO_ERROR); return (NO_ERROR);
} }
bool _sort_str(t_str *_lhs, t_str *_rhs) static bool _sort_str(t_str *_lhs, t_str *_rhs)
{ {
t_str lhs; t_str lhs;
t_str rhs; t_str rhs;
@ -100,7 +75,7 @@ bool _sort_str(t_str *_lhs, t_str *_rhs)
return (*lhs < *rhs); return (*lhs < *rhs);
} }
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;
t_string ret; t_string ret;
@ -112,7 +87,7 @@ t_error handle_quotes(t_str raw, t_string *out)
while (raw[i] != '\0') while (raw[i] != '\0')
{ {
if (raw[i] == '\'') if (raw[i] == '\'')
printf("Pushing stuff\n"), string_push(&ret, "'\"'\"'"); string_push(&ret, "'\"'\"'");
else else
string_push_char(&ret, raw[i]); string_push_char(&ret, raw[i]);
i++; i++;