Normed the builtins

This commit is contained in:
Raphael 2024-08-30 16:59:32 +02:00
parent efb18f605c
commit ef7677f2ee
5 changed files with 70 additions and 57 deletions

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/07 14:13:41 by rparodi #+# #+# */
/* Updated: 2024/08/18 22:54:44 by maiboyer ### ########.fr */
/* Updated: 2024/08/30 16:59:21 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,31 +20,32 @@
struct s_assign_export_state
{
t_state *state;
t_builtin_spawn_info *info;
t_error err;
t_state *state;
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)
{
struct s_assign_export_state *ctx;
const char *first_eq;
t_str key;
t_str value;
struct s_assign_export_state *ctx;
const char *first_eq;
t_str key;
t_str value;
if (arg == NULL || *arg == NULL || idx == 0)
return;
return ;
ctx = vctx;
first_eq = str_find_chr(*arg, '=');
if (first_eq == NULL || first_eq == *arg)
return;
return ;
key = str_substring(*arg, 0, first_eq - *arg);
value = str_substring(first_eq, 1, ~0llu);
if (hmap_env_insert(ctx->state->env, key, value))
ctx->err = 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)(_idx);
@ -54,10 +55,10 @@ static t_error _append_key_to_vec(t_usize _idx, const t_str *key, t_str *value,
return (NO_ERROR);
}
static bool _sort_str(t_str *_lhs, t_str *_rhs)
static bool _sort_str(t_str *_lhs, t_str *_rhs)
{
t_str lhs;
t_str rhs;
t_str lhs;
t_str rhs;
if (_lhs == NULL && _rhs != NULL)
return (true);
@ -75,10 +76,10 @@ static bool _sort_str(t_str *_lhs, t_str *_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_string ret;
t_usize i;
t_string ret;
i = 0;
if (!raw)
@ -95,12 +96,14 @@ static t_error handle_quotes(t_str raw, t_string *out)
return (*out = ret, NO_ERROR);
}
t_error _print_export_env(t_state *state, t_builtin_spawn_info info, t_i32 *exit_code)
t_error _print_export_env(\
t_state *state, t_builtin_spawn_info info, t_i32 *exit_code)
{
t_vec_str keys;
t_vec_str keys_uniq;
t_usize i;
t_str *value;
t_vec_str keys;
t_vec_str keys_uniq;
t_usize i;
t_str *value;
t_string buf;
keys = vec_str_new(16, NULL);
hmap_env_iter(state->env, _append_key_to_vec, &keys);
@ -112,13 +115,13 @@ t_error _print_export_env(t_state *state, t_builtin_spawn_info info, t_i32 *exit
vec_str_sort(&keys, _sort_str);
while (i < keys.len)
{
while (i < keys.len - 1 && str_compare(keys.buffer[i], keys.buffer[i + 1]))
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)
{
@ -126,15 +129,13 @@ t_error _print_export_env(t_state *state, t_builtin_spawn_info info, t_i32 *exit
if (value == NULL)
value = hmap_env_get(state->env, &keys_uniq.buffer[i]);
if (value == NULL || *value == NULL)
{
me_printf_fd(info.stdout, "export %s\n", keys_uniq.buffer[i]);
}
else
{
t_string buf;
if (!handle_quotes(*value, &buf))
{
me_printf_fd(info.stdout, "export %s='%s'\n", keys_uniq.buffer[i], buf.buf);
me_printf_fd(\
info.stdout, "export %s='%s'\n", keys_uniq.buffer[i], buf.buf);
string_free(buf);
}
}
@ -145,9 +146,10 @@ t_error _print_export_env(t_state *state, t_builtin_spawn_info info, t_i32 *exit
return (NO_ERROR);
}
t_error builtin_export(t_state *state, t_builtin_spawn_info info, t_i32 *exit_code)
t_error builtin_export(\
t_state *state, t_builtin_spawn_info info, t_i32 *exit_code)
{
struct s_assign_export_state assign_ctx;
struct s_assign_export_state assign_ctx;
(void)(state);
assign_ctx.info = &info;
@ -155,5 +157,6 @@ t_error builtin_export(t_state *state, t_builtin_spawn_info info, t_i32 *exit_co
assign_ctx.err = NO_ERROR;
if (info.args.len == 1)
return (_print_export_env(state, info, exit_code));
return (*exit_code = 0, vec_str_iter(&info.args, _assign_export, &assign_ctx), assign_ctx.err);
return (*exit_code = 0, vec_str_iter(\
&info.args, _assign_export, &assign_ctx), assign_ctx.err);
}