Files
bshell/bshell.core/cmdlets/write-output.c
T

98 lines
2.8 KiB
C
Raw Normal View History

#include <bshell/command/cmdlet.h>
#include <bshell/command/command.h>
#include <bshell/runtime/pipeline.h>
#include <bshell/status.h>
2026-05-24 20:22:19 +01:00
#include <fx/collections/array.h>
#include <fx/reflection/function.h>
#include <fx/string.h>
#include <fx/value-type.h>
#include <fx/vector.h>
#define BSHELL_TYPE_WRITE_OUTPUT (bshell_write_output_get_type())
fx_type_id bshell_write_output_get_type(void);
FX_DECLARE_TYPE(bshell_write_output);
FX_TYPE_CLASS_DECLARATION_BEGIN(bshell_write_output)
FX_TYPE_CLASS_DECLARATION_END(bshell_write_output)
struct bshell_write_output_p {
size_t w_arg_count;
size_t w_arg_index;
2026-05-24 20:22:19 +01:00
};
static enum bshell_status begin_processing(
bshell_cmdlet *cmdlet,
enum bshell_command_position position)
2026-05-24 20:22:19 +01:00
{
struct bshell_write_output_p *p
= fx_object_get_private(cmdlet, BSHELL_TYPE_WRITE_OUTPUT);
p->w_arg_index = 1;
p->w_arg_count = bshell_command_get_arg_count(cmdlet);
2026-05-24 20:22:19 +01:00
return BSHELL_SUCCESS;
}
static enum bshell_status process_record(
bshell_cmdlet *cmdlet,
bshell_pipeline *pipeline,
struct bshell_runtime *rt)
{
struct bshell_write_output_p *p
= fx_object_get_private(cmdlet, BSHELL_TYPE_WRITE_OUTPUT);
2026-05-24 20:22:19 +01:00
if (p->w_arg_index < p->w_arg_count) {
const fx_value *arg
= bshell_command_get_arg(cmdlet, p->w_arg_index++);
bshell_pipeline_write_value(pipeline, *arg, false);
return BSHELL_SUCCESS;
}
2026-05-24 20:22:19 +01:00
fx_value in = bshell_pipeline_read_value(pipeline);
if (fx_value_is_set(&in)) {
bshell_pipeline_write_value(pipeline, in, false);
2026-05-24 20:22:19 +01:00
fx_value_unset(&in);
}
return BSHELL_SUCCESS;
}
static enum bshell_status end_processing(bshell_cmdlet *cmdlet)
{
struct bshell_write_output_p *p
= fx_object_get_private(cmdlet, BSHELL_TYPE_WRITE_OUTPUT);
2026-05-24 20:22:19 +01:00
return BSHELL_SUCCESS;
}
static void init(fx_object *obj, void *priv)
{
}
FX_TYPE_CLASS_BEGIN(bshell_write_output)
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_WRITE;
FX_INTERFACE_ENTRY(c_noun) = "Output";
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_cmdlet, BSHELL_TYPE_CMDLET)
FX_TYPE_CLASS_END(bshell_write_output)
FX_TYPE_DEFINITION_BEGIN(bshell_write_output)
FX_TYPE_ID(0xf7f9c42d, 0xc053, 0x4ffc, 0xa8ec, 0x78fef4844ac0);
FX_TYPE_NAME("bshell.core.write_output");
FX_TYPE_EXTENDS(BSHELL_TYPE_CMDLET);
FX_TYPE_CLASS(bshell_write_output_class);
FX_TYPE_INSTANCE_INIT(init);
FX_TYPE_INSTANCE_PRIVATE(struct bshell_write_output_p);
FX_TYPE_DEFINITION_END(bshell_write_output)