From: Kevin Day Date: Sat, 11 Dec 2021 03:57:44 +0000 (-0600) Subject: Bugfix: Error handling should not exit for certain errors. X-Git-Tag: 0.5.7~54 X-Git-Url: https://git.kevux.org/?a=commitdiff_plain;h=f5dcbcc9a2c18ae6bc16483866b017e686a0d5a2;p=fll Bugfix: Error handling should not exit for certain errors. When a process signal is being received, F_signal is being set with the error bit. This should not have the error bit set. Move the conditional logic inside the appropriate printing functions. Add utf8_print_character_invalid() for printing an error character. Invalid UTF-8 fragments should not result in an exit on error. Instead, these should be handled by either appropriate printing or by setting the is valid property on exit. --- diff --git a/level_3/utf8/c/private-print.c b/level_3/utf8/c/private-print.c index 23b75c5..70b1795 100644 --- a/level_3/utf8/c/private-print.c +++ b/level_3/utf8/c/private-print.c @@ -33,6 +33,17 @@ extern "C" { } #endif // _di_utf8_print_character_ +#ifndef _di_utf8_print_character_invalid_ + void utf8_print_character_invalid(utf8_data_t * const data, const f_string_static_t character) { + + if (!character.used) return; + if (data->main->parameters[utf8_parameter_strip_invalid].result == f_console_result_found) return; + if (data->main->parameters[utf8_parameter_verify].result == f_console_result_found) return; + + utf8_print_character(data, character, data->valid_not); + } +#endif // _di_utf8_print_character_invalid_ + #ifndef _di_utf8_print_codepoint_ void utf8_print_codepoint(utf8_data_t * const data, const uint32_t codepoint) { @@ -52,6 +63,8 @@ extern "C" { void utf8_print_error_decode(utf8_data_t * const data, const f_status_t status, const f_string_static_t character) { if (data->main->error.verbosity == f_console_verbosity_quiet) return; + if (data->main->parameters[utf8_parameter_strip_invalid].result == f_console_result_found) return; + if (data->main->parameters[utf8_parameter_verify].result == f_console_result_found) return; fl_print_format("%c%[%SFailed to decode character code '%]", data->main->error.to.stream, f_string_eol_s[0], data->main->context.set.error, data->main->error.prefix, data->main->context.set.error); @@ -68,6 +81,9 @@ extern "C" { if (F_status_set_fine(status) == F_utf) { fl_print_format("%[', not a valid UTF-8 character sequence.%]%c", data->main->error.to.stream, data->main->context.set.error, data->main->context.set.error, f_string_eol_s[0]); } + else if (F_status_set_fine(status) == F_utf_fragment) { + fl_print_format("%[', invalid UTF-8 fragment.%]%c", data->main->error.to.stream, data->main->context.set.error, data->main->context.set.error, f_string_eol_s[0]); + } else { fl_print_format("%[', error status code%] ", data->main->error.to.stream, data->main->context.set.error, data->main->context.set.error, f_string_eol_s[0]); fl_print_format("%[%ui%]", data->main->error.to.stream, data->main->context.set.notable, F_status_set_fine(status), data->main->context.set.notable); diff --git a/level_3/utf8/c/private-print.h b/level_3/utf8/c/private-print.h index bcf1bcd..18f462d 100644 --- a/level_3/utf8/c/private-print.h +++ b/level_3/utf8/c/private-print.h @@ -40,6 +40,20 @@ extern "C" { #endif // _di_utf8_print_character_ /** + * Print an invalid character either as a Unicode codeblock or as a binary. + * + * This handles whether or not the invalid character should be printed or not based on program parameters. + * + * @param data + * The program data. + * @param character + * The character block to print. + */ +#ifndef _di_utf8_print_character_invalid_ + extern void utf8_print_character_invalid(utf8_data_t * const data, const f_string_static_t character) F_attribute_visibility_internal_d; +#endif // _di_utf8_print_character_invalid_ + +/** * Print the codepoint number as a codepoint string (such as U+8C78). * * @param data diff --git a/level_3/utf8/c/private-utf8_binary.c b/level_3/utf8/c/private-utf8_binary.c index dae939c..2a387b1 100644 --- a/level_3/utf8/c/private-utf8_binary.c +++ b/level_3/utf8/c/private-utf8_binary.c @@ -25,17 +25,17 @@ extern "C" { } if (F_status_is_error(status)) { - if (F_status_set_fine(status) == F_failure || F_status_set_fine(status) == F_utf || F_status_set_fine(status) == F_valid_not) { + status = F_status_set_fine(status); + + if (status == F_failure || status == F_utf || status == F_utf_fragment || status == F_valid_not) { valid_not = F_true; - if (data->main->parameters[utf8_parameter_strip_invalid].result == f_console_result_none && data->main->parameters[utf8_parameter_verify].result == f_console_result_none) { - utf8_print_character(data, character, data->valid_not); - } + utf8_print_character_invalid(data, data->text); } else { - if (data->main->parameters[utf8_parameter_strip_invalid].result == f_console_result_none && data->main->parameters[utf8_parameter_verify].result == f_console_result_none) { - utf8_print_error_decode(data, status, character); - } + status = F_status_set_error(status); + + utf8_print_error_decode(data, status, character); return status; } @@ -84,7 +84,7 @@ extern "C" { if (utf8_signal_received(data)) { utf8_print_signal_received(data, status); - status = F_status_set_error(F_signal); + status = F_signal; break; } } @@ -149,7 +149,7 @@ extern "C" { data->buffer.used = 0; - if (F_status_is_error(status)) { + if (F_status_is_error(status) || status == F_signal) { return status; } diff --git a/level_3/utf8/c/private-utf8_codepoint.c b/level_3/utf8/c/private-utf8_codepoint.c index 79c12b9..fbe9fe4 100644 --- a/level_3/utf8/c/private-utf8_codepoint.c +++ b/level_3/utf8/c/private-utf8_codepoint.c @@ -36,17 +36,17 @@ extern "C" { status = f_utf_unicode_string_to(data->text.string, data->text.used, &codepoint); if (F_status_is_error(status)) { - if (F_status_set_fine(status) == F_failure || F_status_set_fine(status) == F_utf || F_status_set_fine(status) == F_valid_not) { + status = F_status_set_fine(status); + + if (status == F_failure || status == F_utf || status == F_utf_fragment || status == F_valid_not) { valid_not = F_true; - if (data->main->parameters[utf8_parameter_strip_invalid].result == f_console_result_none && data->main->parameters[utf8_parameter_verify].result == f_console_result_none) { - fl_print_format("%s%[%r%]%s", data->file.stream, data->prepend, data->valid_not, data->text, data->valid_not, data->append); - } + utf8_print_character_invalid(data, data->text); } else { - if (data->main->parameters[utf8_parameter_strip_invalid].result == f_console_result_none && data->main->parameters[utf8_parameter_verify].result == f_console_result_none) { - utf8_print_error_decode(data, status, character); - } + status = F_status_set_error(status); + + utf8_print_error_decode(data, status, character); return status; } @@ -76,9 +76,7 @@ extern "C" { else { status = F_none; - if (data->main->parameters[utf8_parameter_verify].result == f_console_result_none) { - fl_print_format("%s%[%r%]%s", data->file.stream, data->prepend, data->valid_not, data->text, data->valid_not, data->append); - } + utf8_print_character_invalid(data, data->text); } *mode = utf8_codepoint_mode_ready; @@ -110,7 +108,15 @@ extern "C" { } else if (macro_f_utf_byte_width_is(*character.string)) { status = f_utf_is_whitespace(character.string, 4); - if (F_status_is_error(status)) return status; + + if (F_status_is_error(status)) { + if (F_status_set_fine(status) == F_utf_fragment) { + status = F_valid_not; + } + else { + return status; + } + } if (status == F_true) { status = F_space; @@ -121,9 +127,16 @@ extern "C" { } else { if (character.string[0] < 0x30 || character.string[0] > 0x39 && character.string[0] < 0x41 || character.string[0] > 0x46 && character.string[0] < 0x61 || character.string[0] > 0x66) { - status = f_utf_is_whitespace(character.string, 4); - if (F_status_is_error(status)) return status; + + if (F_status_is_error(status)) { + if (F_status_set_fine(status) == F_utf_fragment) { + status = F_valid_not; + } + else { + return status; + } + } if (status == F_true) { status = F_space; @@ -207,7 +220,7 @@ extern "C" { if (utf8_signal_received(data)) { utf8_print_signal_received(data, status); - status = F_status_set_error(F_signal); + status = F_signal; break; } } @@ -272,7 +285,7 @@ extern "C" { data->buffer.used = 0; - if (F_status_is_error(status)) { + if (F_status_is_error(status) || status == F_signal) { return status; } diff --git a/level_3/utf8/c/utf8.c b/level_3/utf8/c/utf8.c index 7c521e8..eaae881 100644 --- a/level_3/utf8/c/utf8.c +++ b/level_3/utf8/c/utf8.c @@ -361,7 +361,7 @@ extern "C" { status = utf8_process_file_codepoint(&data, file); } - if (F_status_is_error(status)) { + if (F_status_is_error(status) && F_status_set_fine(status) != F_utf_fragment) { fll_error_file_print(main->error, F_status_set_fine(status), data.mode & utf8_mode_from_binary_d ? "utf8_process_file_binary" : "utf8_process_file_codepoint", F_true, 0, utf8_string_process_s, fll_error_file_type_pipe); } } @@ -410,7 +410,7 @@ extern "C" { } } - if (F_status_is_error(status)) { + if (F_status_is_error(status) && F_status_set_fine(status) != F_utf_fragment) { fll_error_file_print(main->error, F_status_set_fine(status), data.mode & utf8_mode_from_binary_d ? "utf8_process_file_binary" : "utf8_process_file_codepoint", F_true, arguments->argv[index], utf8_string_process_s, fll_error_file_type_file); break; @@ -449,7 +449,7 @@ extern "C" { } if (main->output.verbosity != f_console_verbosity_quiet && main->parameters[utf8_parameter_verify].result == f_console_result_none) { - if (F_status_set_fine(status) == F_interrupt) { + if (status == F_signal) { fflush(data.file.stream); if (data.file.stream != main->output.to.stream) { @@ -463,7 +463,7 @@ extern "C" { utf8_data_delete(&data); utf8_main_delete(main); - if (F_status_is_error(status)) { + if (F_status_is_error(status) || status == F_signal) { return status; }