From 4c04ca6de894bc1727f833766605a8f69a0579f3 Mon Sep 17 00:00:00 2001 From: Max Wash Date: Sat, 13 Jun 2026 13:40:20 +0100 Subject: [PATCH] runtime: command: add an arg to begin_processing() to indicate pipeline position --- bshell.runtime/command/command.c | 22 ++++++++++++++++--- bshell.runtime/command/function.c | 4 +++- .../include/bshell/command/command.h | 15 +++++++++++-- .../include/bshell/runtime/cmdcall.h | 5 ++++- bshell.runtime/runtime/cmdcall.c | 19 ++++++---------- bshell.runtime/runtime/pipeline.c | 20 ++++++++++++++++- 6 files changed, 65 insertions(+), 20 deletions(-) diff --git a/bshell.runtime/command/command.c b/bshell.runtime/command/command.c index e373ab2..ad78609 100644 --- a/bshell.runtime/command/command.c +++ b/bshell.runtime/command/command.c @@ -57,6 +57,11 @@ static const fx_value *command_get_arg( return &cmd->cmd_args[index]; } +static size_t command_get_arg_count(struct bshell_command_p *cmd) +{ + return cmd->cmd_nr_args; +} + static fx_status get_command_type( const fx_value *cmd_v, const fx_property *prop, @@ -236,14 +241,25 @@ const fx_value *bshell_command_get_arg(bshell_command *cmd, size_t index) index); } -enum bshell_status bshell_command_begin_processing(bshell_command *command) +size_t bshell_command_get_arg_count(const bshell_command *cmd) { - FX_CLASS_DISPATCH_VIRTUAL_0( + FX_CLASS_DISPATCH_STATIC_0( + BSHELL_TYPE_COMMAND, + command_get_arg_count, + cmd); +} + +enum bshell_status bshell_command_begin_processing( + bshell_command *command, + enum bshell_command_position position) +{ + FX_CLASS_DISPATCH_VIRTUAL( bshell_command, BSHELL_TYPE_COMMAND, BSHELL_ERR_NOT_SUPPORTED, c_begin_processing, - command); + command, + position); } enum bshell_status bshell_command_process_record( diff --git a/bshell.runtime/command/function.c b/bshell.runtime/command/function.c index 0d5fa5b..9fd40be 100644 --- a/bshell.runtime/command/function.c +++ b/bshell.runtime/command/function.c @@ -43,7 +43,9 @@ static enum bshell_status get_callable_name( return BSHELL_SUCCESS; } -static enum bshell_status begin_processing(bshell_command *cmd) +static enum bshell_status begin_processing( + bshell_command *cmd, + enum bshell_command_position position) { return BSHELL_SUCCESS; } diff --git a/bshell.runtime/include/bshell/command/command.h b/bshell.runtime/include/bshell/command/command.h index b0b3d31..4281785 100644 --- a/bshell.runtime/include/bshell/command/command.h +++ b/bshell.runtime/include/bshell/command/command.h @@ -8,6 +8,13 @@ FX_DECLS_BEGIN; +enum bshell_command_position { + BSHELL_COMMAND_POSITION_SINGLE = 0, + BSHELL_COMMAND_POSITION_BEGINNING, + BSHELL_COMMAND_POSITION_MIDDLE, + BSHELL_COMMAND_POSITION_END, +}; + struct bshell_runtime; #define BSHELL_TYPE_COMMAND (bshell_command_get_type()) @@ -24,7 +31,9 @@ FX_TYPE_CLASS_DECLARATION_BEGIN(bshell_command) *c_get_description)(const bshell_command *, fx_string *); enum bshell_status ( *c_get_description_static)(const fx_type *, fx_string *); - enum bshell_status (*c_begin_processing)(bshell_command *); + enum bshell_status (*c_begin_processing)( + bshell_command *, + enum bshell_command_position); enum bshell_status (*c_process_record)( bshell_command *, FX_TYPE_FWDREF(bshell_pipeline) * pipeline, @@ -47,9 +56,11 @@ extern void bshell_command_set_args_reverse( extern const fx_value *bshell_command_get_arg( bshell_command *cmd, size_t index); +extern size_t bshell_command_get_arg_count(const bshell_command *cmd); extern enum bshell_status bshell_command_begin_processing( - bshell_command *command); + bshell_command *command, + enum bshell_command_position position); extern enum bshell_status bshell_command_process_record( bshell_command *command, FX_TYPE_FWDREF(bshell_pipeline) * pipeline, diff --git a/bshell.runtime/include/bshell/runtime/cmdcall.h b/bshell.runtime/include/bshell/runtime/cmdcall.h index 2bced0e..7be301f 100644 --- a/bshell.runtime/include/bshell/runtime/cmdcall.h +++ b/bshell.runtime/include/bshell/runtime/cmdcall.h @@ -8,6 +8,8 @@ FX_DECLS_BEGIN; struct bshell_runtime; +enum bshell_command_position; + #define BSHELL_TYPE_CMDCALL (bshell_cmdcall_get_type()) FX_DECLARE_TYPE(bshell_cmdcall); @@ -29,7 +31,8 @@ extern enum bshell_status bshell_cmdcall_resolve( extern enum bshell_status bshell_cmdcall_process_record( bshell_cmdcall *cmdcall, FX_TYPE_FWDREF(bshell_pipeline) * pipeline, - struct bshell_runtime *rt); + struct bshell_runtime *rt, + enum bshell_command_position position); FX_DECLS_END; diff --git a/bshell.runtime/runtime/cmdcall.c b/bshell.runtime/runtime/cmdcall.c index 3b33f1d..905415b 100644 --- a/bshell.runtime/runtime/cmdcall.c +++ b/bshell.runtime/runtime/cmdcall.c @@ -195,14 +195,15 @@ static enum bshell_status cmdcall_execute(struct bshell_cmdcall_p *cmdcall) static enum bshell_status cmdcall_process_record( struct bshell_cmdcall_p *cmdcall, bshell_pipeline *pipeline, - struct bshell_runtime *rt) + struct bshell_runtime *rt, + enum bshell_command_position position) { if (!cmdcall->cmd_target) { return BSHELL_ERR_NOT_SUPPORTED; } if (!cmdcall->cmd_initialised) { - bshell_command_begin_processing(cmdcall->cmd_target); + bshell_command_begin_processing(cmdcall->cmd_target, position); cmdcall->cmd_initialised = true; } @@ -231,25 +232,19 @@ enum bshell_status bshell_cmdcall_resolve( rt); } -enum bshell_status bshell_cmdcall_execute(bshell_cmdcall *cmdcall) -{ - FX_CLASS_DISPATCH_STATIC_0( - BSHELL_TYPE_CMDCALL, - cmdcall_execute, - cmdcall); -} - enum bshell_status bshell_cmdcall_process_record( bshell_cmdcall *cmdcall, bshell_pipeline *pipeline, - struct bshell_runtime *rt) + struct bshell_runtime *rt, + enum bshell_command_position position) { FX_CLASS_DISPATCH_STATIC( BSHELL_TYPE_CMDCALL, cmdcall_process_record, cmdcall, pipeline, - rt); + rt, + position); } FX_TYPE_CLASS_BEGIN(bshell_cmdcall) diff --git a/bshell.runtime/runtime/pipeline.c b/bshell.runtime/runtime/pipeline.c index 1edcbec..e43543c 100644 --- a/bshell.runtime/runtime/pipeline.c +++ b/bshell.runtime/runtime/pipeline.c @@ -7,6 +7,7 @@ #include struct bshell_pipeline_p { + bool p_input_values_provided; bshell_pipeline *p_self; fx_array *p_in, *p_out; FX_VECTOR_DECLARE(bshell_cmdcall *, p_cmds); @@ -72,6 +73,7 @@ static enum bshell_status pipeline_add_input_value( fx_value value, bool enumerate) { + pipeline->p_input_values_provided = true; return write_value(pipeline, pipeline->p_in, value, enumerate); } @@ -118,10 +120,26 @@ static enum bshell_status pipeline_pump_record( size_t i = 0; for (i = 0; i < pipeline->p_cmds.count;) { + enum bshell_command_position position; + if (pipeline->p_cmds.count == 1) { + position = pipeline->p_input_values_provided + ? BSHELL_COMMAND_POSITION_END + : BSHELL_COMMAND_POSITION_SINGLE; + } else if (i == 0) { + position = pipeline->p_input_values_provided + ? BSHELL_COMMAND_POSITION_END + : BSHELL_COMMAND_POSITION_BEGINNING; + } else if (i == pipeline->p_cmds.count - 1) { + position = BSHELL_COMMAND_POSITION_END; + } else { + position = BSHELL_COMMAND_POSITION_MIDDLE; + } + status = bshell_cmdcall_process_record( pipeline->p_cmds.items[i], pipeline->p_self, - rt); + rt, + position); if (status != BSHELL_SUCCESS) { break;