Updated so export sorts the output
This commit is contained in:
parent
eaa381e8b7
commit
6909456ce5
5 changed files with 119 additions and 7 deletions
|
|
@ -6,7 +6,7 @@
|
|||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/08/07 14:13:41 by rparodi #+# #+# */
|
||||
/* Updated: 2024/08/14 18:16:28 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/08/15 14:01:44 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -47,7 +47,7 @@ static void _assign_export(t_usize idx, t_str *arg, void *vctx)
|
|||
ctx->err = ERROR;
|
||||
}
|
||||
|
||||
static t_error _print_export(t_usize _idx, const t_str *key, t_str *value, void *vctx)
|
||||
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;
|
||||
|
|
@ -66,18 +66,86 @@ static t_error _print_export(t_usize _idx, const t_str *key, t_str *value, void
|
|||
return (NO_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);
|
||||
}
|
||||
|
||||
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 _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;
|
||||
|
||||
keys = vec_str_new(16, NULL);
|
||||
hmap_env_iter(state->env, _append_key_to_vec, &keys);
|
||||
hmap_env_iter(state->tmp_var, _append_key_to_vec, &keys);
|
||||
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)
|
||||
me_printf_fd(info.stdout, "export %s=''\n", keys_uniq.buffer[i]);
|
||||
me_printf_fd(info.stdout, "export %s='%s'\n", keys_uniq.buffer[i], *value);
|
||||
i++;
|
||||
}
|
||||
vec_str_free(keys_uniq);
|
||||
*exit_code = 0;
|
||||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
t_error builtin_export(t_state *state, t_builtin_spawn_info info, t_i32 *exit_code)
|
||||
{
|
||||
struct s_print_export_state print_ctx;
|
||||
struct s_assign_export_state assign_ctx;
|
||||
|
||||
|
||||
(void)(state);
|
||||
print_ctx.info = &info;
|
||||
print_ctx.state = state;
|
||||
assign_ctx.info = &info;
|
||||
assign_ctx.state = state;
|
||||
assign_ctx.err = NO_ERROR;
|
||||
if (info.args.len == 1)
|
||||
return (*exit_code = 0, hmap_env_iter(state->env, _print_export, &print_ctx));
|
||||
return (_print_export_env(state, info, exit_code));
|
||||
return (*exit_code = 0, vec_str_iter(&info.args, _assign_export, &assign_ctx), assign_ctx.err);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue