Update so '$BANANE=truc' works

This commit is contained in:
Maix0 2024-08-12 17:02:46 +02:00
parent ecf01a6b8b
commit 11ec5cfa3c
14 changed files with 183 additions and 120 deletions

View file

@ -6,17 +6,75 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/07 14:13:41 by rparodi #+# #+# */
/* Updated: 2024/08/11 11:26:08 by maiboyer ### ########.fr */
/* Updated: 2024/08/12 15:58:55 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "exec/builtins.h"
#include "me/hashmap/hashmap_env.h"
#include "me/printf/printf.h"
#include "me/str/str.h"
#include "me/types.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
{
t_state *state;
t_builtin_spawn_info *info;
t_error err;
};
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;
if (arg == NULL || *arg == NULL || idx == 0)
return;
ctx = vctx;
first_eq = str_find_chr(*arg, '=');
if (first_eq == NULL || first_eq == *arg)
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 _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;
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);
}
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;
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 (builtin_env___(state, info, exit_code));
return (*exit_code = 0, NO_ERROR);
return (*exit_code = 0, hmap_env_iter(state->env, _print_export, &print_ctx));
return (*exit_code = 0, vec_str_iter(&info.args, _assign_export, &assign_ctx), assign_ctx.err);
}

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/11 17:22:29 by maiboyer #+# #+# */
/* Updated: 2024/08/11 12:48:42 by maiboyer ### ########.fr */
/* Updated: 2024/08/12 16:57:47 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -304,6 +304,25 @@ t_error _ast_get_str__command_substitution(t_ast_node elem, t_word_iterator *sta
// vec_estr_push(out, (t_expandable_str){.do_expand = state->res.kind == AST_WORD_NO_QUOTE, .value = str_clone(exp_out.value)});
return (ERROR);
}
t_error _ast_get_str(t_ast_node elem, t_word_iterator *state, t_vec_estr *out);
t_error _ast_into_str(t_ast_node self, t_state *state, t_vec_str *append);
t_error _ast_get_str__word(t_ast_node elem, t_word_iterator *state, t_vec_estr *out)
{
t_usize i;
t_vec_str res;
t_str tmp;
if (elem == NULL || state == NULL || out == NULL || elem->kind != AST_WORD)
return (ERROR);
res = vec_str_new(16, str_free);
if (_ast_into_str(elem, state->state, &res))
return (vec_str_free(res), ERROR);
while (!vec_str_pop_front(&res, &tmp))
vec_estr_push(out, (t_expandable_str){.do_expand = false, .value = tmp});
vec_str_free(res);
return (NO_ERROR);
}
t_error _ast_get_str(t_ast_node elem, t_word_iterator *state, t_vec_estr *out)
{
@ -317,7 +336,9 @@ t_error _ast_get_str(t_ast_node elem, t_word_iterator *state, t_vec_estr *out)
return (_ast_get_str__arimethic_expansion(elem, state, out));
if (elem->kind == AST_COMMAND_SUBSTITUTION)
return (_ast_get_str__command_substitution(elem, state, out));
return (NO_ERROR);
if (elem->kind == AST_WORD)
return (_ast_get_str__word(elem, state, out));
return (ERROR);
}
void _run_word_into_str(t_usize idx, t_ast_node *elem, t_word_iterator *state)
@ -398,9 +419,9 @@ t_error _word_into_str(t_ast_node self, t_state *state, t_vec_str *append)
else
{
ifs = _get_ifs_value(state);
while (*ifs != '\0' && str_find_chr(res.value.buffer[i].value, *ifs) == NULL)
while (ifs != NULL && *ifs != '\0' && str_find_chr(res.value.buffer[i].value, *ifs) == NULL)
ifs++;
if (*ifs == '\0')
if (ifs == NULL || *ifs == '\0')
string_push(&tmp, res.value.buffer[i].value);
else
{
@ -445,7 +466,9 @@ t_error _word_into_str(t_ast_node self, t_state *state, t_vec_str *append)
tmp = string_new(64);
i = 0;
while (i < res.value.len)
{
string_push(&tmp, res.value.buffer[i++].value);
}
vec_str_push(append, tmp.buf);
}
vec_estr_free(res.value);