2026-05-25 10:33:29 +01:00
|
|
|
#include <bshell/command/cmdlet.h>
|
|
|
|
|
#include <bshell/command/command.h>
|
|
|
|
|
#include <bshell/runtime/pipeline.h>
|
|
|
|
|
#include <bshell/runtime/runtime.h>
|
|
|
|
|
#include <bshell/runtime/script-block.h>
|
|
|
|
|
#include <bshell/status.h>
|
2026-05-24 20:22:19 +01:00
|
|
|
#include <fx/reflection/function.h>
|
|
|
|
|
#include <fx/string.h>
|
|
|
|
|
#include <fx/vector.h>
|
|
|
|
|
|
|
|
|
|
#define BSHELL_TYPE_FOREACH_OBJECT (bshell_foreach_object_get_type())
|
|
|
|
|
|
|
|
|
|
fx_type_id bshell_foreach_object_get_type(void);
|
|
|
|
|
|
|
|
|
|
FX_DECLARE_TYPE(bshell_foreach_object);
|
|
|
|
|
|
|
|
|
|
FX_TYPE_CLASS_DECLARATION_BEGIN(bshell_foreach_object)
|
|
|
|
|
FX_TYPE_CLASS_DECLARATION_END(bshell_foreach_object)
|
|
|
|
|
|
|
|
|
|
struct bshell_foreach_object_p {
|
|
|
|
|
bshell_scriptblock *f_block;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static enum bshell_status begin_processing(bshell_cmdlet *cmdlet)
|
|
|
|
|
{
|
2026-05-25 10:33:29 +01:00
|
|
|
struct bshell_foreach_object_p *p
|
|
|
|
|
= fx_object_get_private(cmdlet, BSHELL_TYPE_FOREACH_OBJECT);
|
2026-05-24 20:22:19 +01:00
|
|
|
|
|
|
|
|
const fx_value *block_v = bshell_command_get_arg(cmdlet, 1);
|
|
|
|
|
if (!block_v) {
|
|
|
|
|
return BSHELL_ERR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fx_value_get_object(block_v, &p->f_block);
|
|
|
|
|
return BSHELL_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static enum bshell_status process_record(
|
|
|
|
|
bshell_cmdlet *cmdlet,
|
|
|
|
|
bshell_pipeline *pipeline,
|
|
|
|
|
struct bshell_runtime *rt)
|
|
|
|
|
{
|
2026-05-25 10:33:29 +01:00
|
|
|
struct bshell_foreach_object_p *p
|
|
|
|
|
= fx_object_get_private(cmdlet, BSHELL_TYPE_FOREACH_OBJECT);
|
2026-05-24 20:22:19 +01:00
|
|
|
|
|
|
|
|
fx_value in = bshell_pipeline_read_value(pipeline);
|
|
|
|
|
if (!fx_value_is_set(&in)) {
|
|
|
|
|
return BSHELL_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bshell_runtime_push_scope(rt, p->f_block);
|
|
|
|
|
bshell_variable *item = bshell_runtime_define_var(rt, "_");
|
2026-05-30 19:53:15 +01:00
|
|
|
bshell_variable_set_value(item, in);
|
2026-05-24 20:22:19 +01:00
|
|
|
fx_value out = bshell_runtime_eval(rt);
|
|
|
|
|
bshell_runtime_pop_scope(rt);
|
|
|
|
|
fx_value_unset(&in);
|
|
|
|
|
bshell_pipeline_write_value(pipeline, out, false);
|
|
|
|
|
fx_value_unset(&out);
|
|
|
|
|
|
|
|
|
|
return BSHELL_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static enum bshell_status end_processing(bshell_cmdlet *cmdlet)
|
|
|
|
|
{
|
2026-05-25 10:33:29 +01:00
|
|
|
struct bshell_foreach_object_p *p
|
|
|
|
|
= fx_object_get_private(cmdlet, BSHELL_TYPE_FOREACH_OBJECT);
|
2026-05-24 20:22:19 +01:00
|
|
|
return BSHELL_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void init(fx_object *obj, void *priv)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FX_TYPE_CLASS_BEGIN(bshell_foreach_object)
|
|
|
|
|
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
|
|
|
|
FX_INTERFACE_ENTRY(to_string) = NULL;
|
|
|
|
|
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
|
|
|
|
|
|
|
|
|
FX_TYPE_VTABLE_INTERFACE_BEGIN(bshell_cmdlet, BSHELL_TYPE_CMDLET)
|
|
|
|
|
FX_INTERFACE_ENTRY(c_verb) = BSHELL_VERB_FOREACH;
|
|
|
|
|
FX_INTERFACE_ENTRY(c_noun) = "Object";
|
|
|
|
|
FX_TYPE_VTABLE_INTERFACE_END(bshell_cmdlet, BSHELL_TYPE_CMDLET)
|
|
|
|
|
|
|
|
|
|
FX_TYPE_VTABLE_INTERFACE_BEGIN(bshell_command, BSHELL_TYPE_COMMAND)
|
|
|
|
|
FX_INTERFACE_ENTRY(c_begin_processing) = begin_processing;
|
|
|
|
|
FX_INTERFACE_ENTRY(c_process_record) = process_record;
|
|
|
|
|
FX_INTERFACE_ENTRY(c_end_processing) = end_processing;
|
|
|
|
|
FX_TYPE_VTABLE_INTERFACE_END(bshell_command, BSHELL_TYPE_COMMAND)
|
|
|
|
|
FX_TYPE_CLASS_END(bshell_foreach_object)
|
|
|
|
|
|
|
|
|
|
FX_TYPE_DEFINITION_BEGIN(bshell_foreach_object)
|
|
|
|
|
FX_TYPE_ID(0x44ae3223, 0x14b3, 0x4c89, 0x9753, 0x6c7901ae00e9);
|
|
|
|
|
FX_TYPE_NAME("bshell.core.foreach_object");
|
|
|
|
|
FX_TYPE_EXTENDS(BSHELL_TYPE_CMDLET);
|
|
|
|
|
FX_TYPE_CLASS(bshell_foreach_object_class);
|
|
|
|
|
FX_TYPE_INSTANCE_INIT(init);
|
|
|
|
|
FX_TYPE_INSTANCE_PRIVATE(struct bshell_foreach_object_p);
|
|
|
|
|
FX_TYPE_DEFINITION_END(bshell_foreach_object)
|