From d8ca72d60d74e23e948d805e400208330d6c8e15 Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Tue, 29 Mar 2022 21:47:47 -0500 Subject: [PATCH] Update: Restructure iki_write to use fll_program_data_t. Create iki_write_data_t for private data and passing the fll_program_data_t pointer. Move parameter initialization and deallocation for FLL program data into main.c. --- level_3/iki_write/c/common.c | 13 -------- level_3/iki_write/c/common.h | 64 ------------------------------------ level_3/iki_write/c/iki_write.c | 43 ++++++++++-------------- level_3/iki_write/c/iki_write.h | 4 +-- level_3/iki_write/c/main.c | 8 ++++- level_3/iki_write/c/private-common.c | 31 ++++++++++------- level_3/iki_write/c/private-common.h | 48 +++++++++++++++++++++++---- level_3/iki_write/c/private-write.c | 34 +++++++++---------- level_3/iki_write/c/private-write.h | 6 ++-- 9 files changed, 108 insertions(+), 143 deletions(-) diff --git a/level_3/iki_write/c/common.c b/level_3/iki_write/c/common.c index cdd85e6..95d8b88 100644 --- a/level_3/iki_write/c/common.c +++ b/level_3/iki_write/c/common.c @@ -27,19 +27,6 @@ extern "C" { const f_string_static_t iki_write_long_single_s = macro_f_string_static_t_initialize(IKI_WRITE_long_single_s, 0, IKI_WRITE_long_single_s_length); #endif // _di_iki_write_parameters_ -#ifndef _di_iki_write_main_delete_ - f_status_t iki_write_main_delete(iki_write_main_t * const main) { - - f_console_parameters_delete(&main->parameters); - - f_string_dynamic_resize(0, &main->buffer); - - macro_f_color_context_t_delete_simple(main->context); - - return F_none; - } -#endif // _di_iki_write_main_delete_ - #ifdef __cplusplus } // extern "C" #endif diff --git a/level_3/iki_write/c/common.h b/level_3/iki_write/c/common.h index 7d10eb1..eff9b94 100644 --- a/level_3/iki_write/c/common.h +++ b/level_3/iki_write/c/common.h @@ -145,70 +145,6 @@ extern "C" { #define iki_write_total_parameters_d 14 #endif // _di_iki_write_parameters_ -/** - * The main program data. - * - * parameters: The state of pre-defined parameters passed to the program. - * process_pipe: Designate whether or not to process the input pipe. - * output: The output file for general printing. - * error: The output file for error printing. - * warning: The output file for warning printing. - * signal: The process signal management structure. - * context: The color context. - */ -#ifndef _di_iki_write_main_t_ - typedef struct { - f_console_parameters_t parameters; - - uint16_t signal_check; - bool process_pipe; - - fl_print_t output; - fl_print_t error; - fl_print_t warning; - - f_signal_t signal; - - f_string_static_t quote; - f_string_dynamic_t buffer; - - f_color_context_t context; - } iki_write_main_t; - - #define iki_write_main_t_initialize \ - { \ - f_console_parameters_t_initialize, \ - 0, \ - F_false, \ - fl_print_t_initialize, \ - macro_fl_print_t_initialize_error(), \ - macro_fl_print_t_initialize_warning(), \ - f_signal_t_initialize, \ - f_string_static_t_initialize, \ - f_string_dynamic_t_initialize, \ - f_color_context_t_initialize, \ - } -#endif // _di_iki_write_main_t_ - -/** - * Deallocate main. - * - * Be sure to call this after executing iki_write_main(). - * - * @param main - * The main program data. - * - * @return - * F_none on success. - * - * Status codes (with error bit) are returned on any problem. - * - * @see iki_write_main() - */ -#ifndef _di_iki_write_main_delete_ - extern f_status_t iki_write_main_delete(iki_write_main_t * const main); -#endif // _di_iki_write_main_delete_ - #ifdef __cplusplus } // extern "C" #endif diff --git a/level_3/iki_write/c/iki_write.c b/level_3/iki_write/c/iki_write.c index 39e4719..d5fbd40 100644 --- a/level_3/iki_write/c/iki_write.c +++ b/level_3/iki_write/c/iki_write.c @@ -48,14 +48,10 @@ extern "C" { #endif // _di_iki_write_print_help_ #ifndef _di_iki_write_main_ - f_status_t iki_write_main(iki_write_main_t * const main, const f_console_arguments_t *arguments) { + f_status_t iki_write_main(fll_program_data_t * const main, const f_console_arguments_t *arguments) { f_status_t status = F_none; - f_console_parameter_t parameters[] = iki_write_console_parameter_t_initialize; - main->parameters.array = parameters; - main->parameters.used = iki_write_total_parameters_d; - { f_console_parameter_id_t ids[3] = { iki_write_parameter_no_color_e, iki_write_parameter_light_e, iki_write_parameter_dark_e }; const f_console_parameter_ids_t choices = macro_f_console_parameter_ids_t_initialize(ids, 3); @@ -88,8 +84,6 @@ extern "C" { f_print_dynamic_raw(f_string_eol_s, main->error.to.stream); } - iki_write_main_delete(main); - return F_status_set_error(status); } } @@ -102,11 +96,7 @@ extern "C" { status = f_console_parameter_prioritize_right(main->parameters, choices, &choice); - if (F_status_is_error(status)) { - iki_write_main_delete(main); - - return status; - } + if (F_status_is_error(status)) return status; if (choice == iki_write_parameter_verbosity_quiet_e) { main->output.verbosity = f_console_verbosity_quiet_e; @@ -130,14 +120,16 @@ extern "C" { } } - f_string_static_t * const argv = main->parameters.arguments.array; + iki_write_data_t data = iki_write_data_t_initialize; + data.main = main; + data.argv = main->parameters.arguments.array; status = F_none; if (main->parameters.array[iki_write_parameter_help_e].result == f_console_result_found_e) { iki_write_print_help(main->output.to, main->context); - iki_write_main_delete(main); + iki_write_data_delete(&data); return F_none; } @@ -145,7 +137,7 @@ extern "C" { if (main->parameters.array[iki_write_parameter_version_e].result == f_console_result_found_e) { fll_program_print_version(main->output.to, iki_write_program_version_s); - iki_write_main_delete(main); + iki_write_data_delete(&data); return F_none; } @@ -177,10 +169,10 @@ extern "C" { file.id = -1; file.stream = 0; - status = f_file_stream_open(argv[index], f_string_empty_s, &file); + status = f_file_stream_open(data.argv[index], f_string_empty_s, &file); if (F_status_is_error(status)) { - fll_error_file_print(main->error, F_status_set_fine(status), "f_file_stream_open", F_true, argv[index], f_file_operation_open_s, fll_error_file_type_file_e); + fll_error_file_print(main->error, F_status_set_fine(status), "f_file_stream_open", F_true, data.argv[index], f_file_operation_open_s, fll_error_file_type_file_e); } } } @@ -263,18 +255,18 @@ extern "C" { } } - main->quote = f_iki_syntax_quote_double_s; + data.quote = f_iki_syntax_quote_double_s; if (F_status_is_error_not(status)) { if (main->parameters.array[iki_write_parameter_double_e].result == f_console_result_found_e) { if (main->parameters.array[iki_write_parameter_single_e].result == f_console_result_found_e) { if (main->parameters.array[iki_write_parameter_double_e].location < main->parameters.array[iki_write_parameter_single_e].location) { - main->quote = f_iki_syntax_quote_single_s; + data.quote = f_iki_syntax_quote_single_s; } } } else if (main->parameters.array[iki_write_parameter_single_e].result == f_console_result_found_e) { - main->quote = f_iki_syntax_quote_single_s; + data.quote = f_iki_syntax_quote_single_s; } } @@ -301,7 +293,7 @@ extern "C" { for (f_status_t status_pipe = F_none; ; ) { if (!((++main->signal_check) % iki_write_signal_check_d)) { - if (iki_write_signal_received(main)) { + if (iki_write_signal_received(&data)) { status = F_status_set_error(F_interrupt); break; @@ -377,7 +369,7 @@ extern "C" { } } - status = iki_write_process(main, file, object, content, &escaped); + status = iki_write_process(&data, file, object, content, &escaped); if (F_status_is_error(status)) break; fll_print_dynamic_raw(f_string_eol_s, file.stream); @@ -428,15 +420,16 @@ extern "C" { for (f_array_length_t i = 0; i < main->parameters.array[iki_write_parameter_object_e].values.used; ++i) { if (!((++main->signal_check) % iki_write_signal_check_d)) { - if (iki_write_signal_received(main)) { + if (iki_write_signal_received(&data)) { status = F_status_set_error(F_interrupt); + break; } main->signal_check = 0; } - status = iki_write_process(main, file, argv[main->parameters.array[iki_write_parameter_object_e].values.array[i]], argv[main->parameters.array[iki_write_parameter_content_e].values.array[i]], &escaped); + status = iki_write_process(&data, file, data.argv[main->parameters.array[iki_write_parameter_object_e].values.array[i]], data.argv[main->parameters.array[iki_write_parameter_content_e].values.array[i]], &escaped); if (F_status_is_error(status)) break; fll_print_dynamic_raw(f_string_eol_s, file.stream); @@ -468,7 +461,7 @@ extern "C" { } } - iki_write_main_delete(main); + iki_write_data_delete(&data); return status; } diff --git a/level_3/iki_write/c/iki_write.h b/level_3/iki_write/c/iki_write.h index d8b8422..aefd2e0 100644 --- a/level_3/iki_write/c/iki_write.h +++ b/level_3/iki_write/c/iki_write.h @@ -8,7 +8,7 @@ * This is the IKI Write program. * * This program utilizes the Featureless Linux Library. - * This program processes files or other input in fss format and stores the results in the iki_write_main_t. + * This program processes files or other input in fss format. * * This processes in accordance to the IKI specification. */ @@ -98,7 +98,7 @@ extern "C" { * @see iki_write_main_delete() */ #ifndef _di_iki_write_main_ - extern f_status_t iki_write_main(iki_write_main_t * const main, const f_console_arguments_t *arguments); + extern f_status_t iki_write_main(fll_program_data_t * const main, const f_console_arguments_t *arguments); #endif // _di_iki_write_main_ #ifdef __cplusplus diff --git a/level_3/iki_write/c/main.c b/level_3/iki_write/c/main.c index f60d34a..4bbf3f1 100644 --- a/level_3/iki_write/c/main.c +++ b/level_3/iki_write/c/main.c @@ -3,7 +3,11 @@ int main(const int argc, const f_string_t *argv, const f_string_t *envp) { const f_console_arguments_t arguments = macro_f_console_arguments_t_initialize(argc, argv, envp); - iki_write_main_t data = iki_write_main_t_initialize; + fll_program_data_t data = fll_program_data_t_initialize; + + f_console_parameter_t parameters[] = iki_write_console_parameter_t_initialize; + data.parameters.array = parameters; + data.parameters.used = iki_write_total_parameters_d; if (f_pipe_input_exists()) { data.process_pipe = F_true; @@ -15,6 +19,8 @@ int main(const int argc, const f_string_t *argv, const f_string_t *envp) { fll_program_standard_setdown(&data.signal); + fll_program_data_delete(&data); + if (F_status_is_error(status)) return 1; return 0; diff --git a/level_3/iki_write/c/private-common.c b/level_3/iki_write/c/private-common.c index 21f91d3..c169f47 100644 --- a/level_3/iki_write/c/private-common.c +++ b/level_3/iki_write/c/private-common.c @@ -5,28 +5,35 @@ extern "C" { #endif +#ifndef _di_iki_write_data_delete_ + void iki_write_data_delete(iki_write_data_t *data) { + + // Nothing to deallocate. + } +#endif // _di_iki_write_data_delete_ + #ifndef _di_iki_write_print_signal_received_ - void iki_write_print_signal_received(iki_write_main_t * const main, const f_status_t signal) { + void iki_write_print_signal_received(iki_write_data_t * const data, const f_status_t signal) { - if (main->warning.verbosity != f_console_verbosity_verbose_e) return; + if (data->main->warning.verbosity != f_console_verbosity_verbose_e) return; // Must flush and reset color because the interrupt may have interrupted the middle of a print function. - fflush(main->warning.to.stream); + fflush(data->main->warning.to.stream); - flockfile(main->warning.to.stream); + flockfile(data->main->warning.to.stream); - fl_print_format("%]%r%r%[Received signal code %]", main->warning.to.stream, main->context.set.reset, f_string_eol_s, f_string_eol_s, main->context.set.warning, main->context.set.warning); - fl_print_format("%[%i%]", main->warning.to.stream, main->context.set.notable, signal, main->context.set.notable); - fl_print_format("%[.%]%r", main->warning.to.stream, main->context.set.warning, main->context.set.warning, f_string_eol_s); + fl_print_format("%]%r%r%[Received signal code %]", data->main->warning.to.stream, data->main->context.set.reset, f_string_eol_s, f_string_eol_s, data->main->context.set.warning, data->main->context.set.warning); + fl_print_format("%[%i%]", data->main->warning.to.stream, data->main->context.set.notable, signal, data->main->context.set.notable); + fl_print_format("%[.%]%r", data->main->warning.to.stream, data->main->context.set.warning, data->main->context.set.warning, f_string_eol_s); - funlockfile(main->warning.to.stream); + funlockfile(data->main->warning.to.stream); } #endif // _di_iki_write_print_signal_received_ #ifndef _di_iki_write_signal_received_ - f_status_t iki_write_signal_received(iki_write_main_t * const main) { + f_status_t iki_write_signal_received(iki_write_data_t * const data) { - if (main->signal.id == -1) { + if (data->main->signal.id == -1) { return F_false; } @@ -34,7 +41,7 @@ extern "C" { memset(&information, 0, sizeof(struct signalfd_siginfo)); - if (f_signal_read(main->signal, 0, &information) == F_signal) { + if (f_signal_read(data->main->signal, 0, &information) == F_signal) { switch (information.ssi_signo) { case F_signal_abort: case F_signal_broken_pipe: @@ -42,7 +49,7 @@ extern "C" { case F_signal_interrupt: case F_signal_quit: case F_signal_termination: - iki_write_print_signal_received(main, information.ssi_signo); + iki_write_print_signal_received(data, information.ssi_signo); return information.ssi_signo; } diff --git a/level_3/iki_write/c/private-common.h b/level_3/iki_write/c/private-common.h index 97a5c4a..27f1afe 100644 --- a/level_3/iki_write/c/private-common.h +++ b/level_3/iki_write/c/private-common.h @@ -13,6 +13,29 @@ extern "C" { #endif /** + * The program data. + * + * argv: The argument structure in the progam data parameters for simplifying syntax. + * main: The main program data. + * quote: The quote to use when writing. + */ +#ifndef _di_iki_write_data_t_ + typedef struct { + fll_program_data_t *main; + f_string_static_t *argv; + + f_string_static_t quote; + } iki_write_data_t; + + #define iki_write_data_t_initialize \ + { \ + 0, \ + 0, \ + f_string_static_t_initialize, \ + } +#endif // _di_iki_write_data_t_ + +/** * Provide common/generic definitions. * * iki_write_common_allocation_*: @@ -25,15 +48,28 @@ extern "C" { #endif // _di_iki_write_common_ /** + * Deallocate program data. + * + * @param data + * The program data. + * + * @return + * F_none on success. + */ +#ifndef _di_iki_write_data_delete_ + extern void iki_write_data_delete(iki_write_data_t *data) F_attribute_visibility_internal_d; +#endif // _di_iki_write_data_delete_ + +/** * Print a message about a process signal being recieved, such as an interrupt signal. * - * @param main - * The main program data. + * @param data + * The program data. * @param signal * The signal received. */ #ifndef _di_iki_write_print_signal_received_ - extern void iki_write_print_signal_received(iki_write_main_t * const main, const f_status_t signal) F_attribute_visibility_internal_d; + extern void iki_write_print_signal_received(iki_write_data_t * const data, const f_status_t signal) F_attribute_visibility_internal_d; #endif // _di_iki_write_print_signal_received_ /** @@ -41,8 +77,8 @@ extern "C" { * * Only signals that are blocked via main.signal will be received. * - * @param main - * The main program data. + * @param data + * The program data. * * @return * A positive number representing a valid signal on signal received. @@ -51,7 +87,7 @@ extern "C" { * @see f_signal_read() */ #ifndef _di_iki_write_signal_received_ - extern f_status_t iki_write_signal_received(iki_write_main_t * const main) F_attribute_visibility_internal_d; + extern f_status_t iki_write_signal_received(iki_write_data_t * const data) F_attribute_visibility_internal_d; #endif // _di_iki_write_signal_received_ #ifdef __cplusplus diff --git a/level_3/iki_write/c/private-write.c b/level_3/iki_write/c/private-write.c index 4b7d27e..28c1976 100644 --- a/level_3/iki_write/c/private-write.c +++ b/level_3/iki_write/c/private-write.c @@ -7,17 +7,17 @@ extern "C" { #endif #ifndef _di_iki_write_process_ - f_status_t iki_write_process(iki_write_main_t * const main, const f_file_t output, const f_string_static_t object, const f_string_static_t content, f_string_dynamic_t *escaped) { + f_status_t iki_write_process(iki_write_data_t * const data, const f_file_t output, const f_string_static_t object, const f_string_static_t content, f_string_dynamic_t *escaped) { if (!object.used) { - if (main->error.verbosity != f_console_verbosity_quiet_e) { - flockfile(main->error.to.stream); + if (data->main->error.verbosity != f_console_verbosity_quiet_e) { + flockfile(data->main->error.to.stream); - fl_print_format("%r%[%QThe object is missing, it must not have a length of %]", main->error.to.stream, f_string_eol_s, main->error.context, main->error.prefix, main->error.context); - fl_print_format("%[0%]", main->error.to.stream, main->error.notable, main->error.notable); - fl_print_format("%[.%]%r", main->error.to.stream, main->error.context, main->error.context, f_string_eol_s); + fl_print_format("%r%[%QThe object is missing, it must not have a length of %]", data->main->error.to.stream, f_string_eol_s, data->main->error.context, data->main->error.prefix, data->main->error.context); + fl_print_format("%[0%]", data->main->error.to.stream, data->main->error.notable, data->main->error.notable); + fl_print_format("%[.%]%r", data->main->error.to.stream, data->main->error.context, data->main->error.context, f_string_eol_s); - funlockfile(main->error.to.stream); + funlockfile(data->main->error.to.stream); } return F_status_set_error(F_failure); @@ -26,35 +26,35 @@ extern "C" { f_status_t status = f_iki_object_is(object); if (status == F_false) { - if (main->error.verbosity != f_console_verbosity_quiet_e) { - flockfile(main->error.to.stream); + if (data->main->error.verbosity != f_console_verbosity_quiet_e) { + flockfile(data->main->error.to.stream); - fl_print_format("%r%[%QThe object '%]", main->error.to.stream, f_string_eol_s, main->error.context, main->error.prefix, main->error.context); - fl_print_format("%[%Q%]", main->error.to.stream, main->error.notable, object, main->error.notable); - fl_print_format("%[' is not a valid IKI object.%]%r", main->error.to.stream, main->error.context, main->error.context, f_string_eol_s); + fl_print_format("%r%[%QThe object '%]", data->main->error.to.stream, f_string_eol_s, data->main->error.context, data->main->error.prefix, data->main->error.context); + fl_print_format("%[%Q%]", data->main->error.to.stream, data->main->error.notable, object, data->main->error.notable); + fl_print_format("%[' is not a valid IKI object.%]%r", data->main->error.to.stream, data->main->error.context, data->main->error.context, f_string_eol_s); - funlockfile(main->error.to.stream); + funlockfile(data->main->error.to.stream); } return F_status_set_error(F_failure); } else if (F_status_is_error(status)) { - fll_error_print(main->error, F_status_set_fine(status), "f_iki_object_is", F_true); + fll_error_print(data->main->error, F_status_set_fine(status), "f_iki_object_is", F_true); return F_status_set_error(F_failure); } escaped->used = 0; - status = fll_iki_content_escape(content, main->quote, escaped); + status = fll_iki_content_escape(content, data->quote, escaped); if (F_status_is_error(status)) { - fll_error_print(main->error, F_status_set_fine(status), "fll_iki_content_escape", F_true); + fll_error_print(data->main->error, F_status_set_fine(status), "fll_iki_content_escape", F_true); return F_status_set_error(F_failure); } - fl_print_format("%Q%r%r%Q%r", output.stream, object, f_iki_syntax_separator_s, main->quote, *escaped, main->quote); + fl_print_format("%Q%r%r%Q%r", output.stream, object, f_iki_syntax_separator_s, data->quote, *escaped, data->quote); return F_none; } diff --git a/level_3/iki_write/c/private-write.h b/level_3/iki_write/c/private-write.h index 0fb86c5..8c69e05 100644 --- a/level_3/iki_write/c/private-write.h +++ b/level_3/iki_write/c/private-write.h @@ -15,8 +15,8 @@ extern "C" { /** * Process a given object and content, printing the IKI if valid or an error if invalid. * - * @param main - * The main program data. + * @param data + * The program data. * @param output * The file to output to. * @param object @@ -31,7 +31,7 @@ extern "C" { * F_failure (with error bit) for any othe failure. */ #ifndef _di_iki_write_process_ - extern f_status_t iki_write_process(iki_write_main_t * const main, const f_file_t output, const f_string_static_t object, const f_string_static_t content, f_string_dynamic_t *escaped) F_attribute_visibility_internal_d; + extern f_status_t iki_write_process(iki_write_data_t * const data, const f_file_t output, const f_string_static_t object, const f_string_static_t content, f_string_dynamic_t *escaped) F_attribute_visibility_internal_d; #endif // _di_iki_write_process_ #ifdef __cplusplus -- 1.8.3.1