From: Kevin Day Date: Sun, 30 Mar 2025 03:12:49 +0000 (-0500) Subject: Progress: Continue working on completing the remove program. X-Git-Url: https://git.kevux.org/?a=commitdiff_plain;h=26a9aa85b0744bd4c9df15e5cd4b2ad9b88a5975;p=kevux-tools Progress: Continue working on completing the remove program. Implement most of the `rm` program. There are some remaining details and I need to also test the program. Move some of the repeated setup code into `kt_remove_setting_load_standard()`. Add the initial settings needed to support the `--preserve-root` and `--presserve-root-not` support via the newly added `root` flag. This work is not yet implemented and needs to be completed. Add support for all programs to return `2` instead of `1` when `F_support_not` is an error return to the main program. I might end up applying this behavior to all of my FLL and related programs. The `rm` program in particular has the `--` being manually processed. I figured given the simple nature of this program that it would be easier to manually pre-process than to utilize the custom callbacks supported by the `f_console_parameter_process()`. --- diff --git a/data/build/fakefile b/data/build/fakefile index 2b57ba4..ee821f9 100644 --- a/data/build/fakefile +++ b/data/build/fakefile @@ -14,6 +14,7 @@ main: remove: build remove/settings build remove/settings.remove + build remove/settings.rm build remove/settings.rmdir build remove/settings.unlink diff --git a/sources/c/program/kevux/tools/remove/main/common.c b/sources/c/program/kevux/tools/remove/main/common.c index 679551e..7f1dde7 100644 --- a/sources/c/program/kevux/tools/remove/main/common.c +++ b/sources/c/program/kevux/tools/remove/main/common.c @@ -9,57 +9,8 @@ extern "C" { if (!main) return; - main->setting.flag &= ~kt_remove_main_flag_option_used_d; - - main->setting.state.step_small = kt_remove_allocation_console_d; - - f_console_parameter_process(arguments, &main->program.parameters, &main->setting.state, 0); - - main->setting.state.step_small = kt_remove_allocation_small_d; - - if (F_status_is_error(main->setting.state.status)) { - kt_remove_print_error(&main->program.error, macro_kt_remove_f(f_console_parameter_process)); - - return; - } - - { - f_uint16s_t choices = f_uint16s_t_initialize; - - // Identify and prioritize "color context" parameters. - { - uint16_t choices_array[3] = { f_console_standard_parameter_no_color_e, f_console_standard_parameter_light_e, f_console_standard_parameter_dark_e }; - choices.array = choices_array; - choices.used = 3; - - const uint8_t modes[3] = { f_color_mode_not_e, f_color_mode_light_e, f_color_mode_dark_e }; - - main->setting.state.status = fll_program_parameter_process_context(choices, modes, F_true, &main->program); - - if (F_status_is_error(main->setting.state.status)) { - kt_remove_print_error(&main->program.error, macro_kt_remove_f(fll_program_parameter_process_context)); - - return; - } - } - - // Identify and prioritize "verbosity" parameters. - { - uint16_t choices_array[5] = { f_console_standard_parameter_verbosity_quiet_e, f_console_standard_parameter_verbosity_error_e, f_console_standard_parameter_verbosity_verbose_e, f_console_standard_parameter_verbosity_debug_e, f_console_standard_parameter_verbosity_normal_e }; - choices.array = choices_array; - choices.used = 5; - - const uint8_t verbosity[5] = { f_console_verbosity_quiet_e, f_console_verbosity_error_e, f_console_verbosity_verbose_e, f_console_verbosity_debug_e, f_console_verbosity_normal_e }; - - main->setting.state.status = fll_program_parameter_process_verbosity(choices, verbosity, F_true, &main->program); - - if (F_status_is_error(main->setting.state.status)) { - kt_remove_print_error(&main->program.error, macro_kt_remove_f(fll_program_parameter_process_verbosity)); - - return; - } - } - } + kt_remove_setting_load_standard(arguments, main); + if (F_status_is_error(main->setting.state.status)) return; f_number_unsigned_t i = 0; f_number_unsigned_t index = 0; @@ -69,20 +20,6 @@ extern "C" { uint8_t j = 0; - main->setting.flag &= ~kt_remove_main_flag_version_copyright_help_d; - - if (main->program.parameters.array[f_console_standard_parameter_help_e].result & f_console_result_found_e) { - main->setting.flag |= kt_remove_main_flag_help_d; - } - - if (main->program.parameters.array[f_console_standard_parameter_version_e].result & f_console_result_found_e) { - main->setting.flag |= kt_remove_main_flag_version_d; - } - - if (main->program.parameters.array[f_console_standard_parameter_copyright_e].result & f_console_result_found_e) { - main->setting.flag |= kt_remove_main_flag_copyright_d; - } - if (main->program.parameters.array[kt_remove_parameter_block_e].result & f_console_result_found_e) { main->setting.flag |= kt_remove_main_flag_block_d | kt_remove_main_flag_option_used_d; } @@ -625,6 +562,77 @@ extern "C" { } #endif // _di_kt_remove_setting_load_id_ +#ifndef _di_kt_remove_setting_load_standard_ + void kt_remove_setting_load_standard(const f_console_arguments_t arguments, kt_remove_main_t * const main) { + + if (!main) return; + + main->setting.flag &= ~kt_remove_main_flag_option_used_d; + + main->setting.state.step_small = kt_remove_allocation_console_d; + + f_console_parameter_process(arguments, &main->program.parameters, &main->setting.state, 0); + + main->setting.state.step_small = kt_remove_allocation_small_d; + + if (F_status_is_error(main->setting.state.status)) { + kt_remove_print_error(&main->program.error, macro_kt_remove_f(f_console_parameter_process)); + + return; + } + + { + f_uint16s_t choices = f_uint16s_t_initialize; + + // Identify and prioritize "color context" parameters. + { + uint16_t choices_array[3] = { f_console_standard_parameter_no_color_e, f_console_standard_parameter_light_e, f_console_standard_parameter_dark_e }; + choices.array = choices_array; + choices.used = 3; + + const uint8_t modes[3] = { f_color_mode_not_e, f_color_mode_light_e, f_color_mode_dark_e }; + + main->setting.state.status = fll_program_parameter_process_context(choices, modes, F_true, &main->program); + + if (F_status_is_error(main->setting.state.status)) { + kt_remove_print_error(&main->program.error, macro_kt_remove_f(fll_program_parameter_process_context)); + + return; + } + } + + // Identify and prioritize "verbosity" parameters. + { + uint16_t choices_array[5] = { f_console_standard_parameter_verbosity_quiet_e, f_console_standard_parameter_verbosity_error_e, f_console_standard_parameter_verbosity_verbose_e, f_console_standard_parameter_verbosity_debug_e, f_console_standard_parameter_verbosity_normal_e }; + choices.array = choices_array; + choices.used = 5; + + const uint8_t verbosity[5] = { f_console_verbosity_quiet_e, f_console_verbosity_error_e, f_console_verbosity_verbose_e, f_console_verbosity_debug_e, f_console_verbosity_normal_e }; + + main->setting.state.status = fll_program_parameter_process_verbosity(choices, verbosity, F_true, &main->program); + + if (F_status_is_error(main->setting.state.status)) { + kt_remove_print_error(&main->program.error, macro_kt_remove_f(fll_program_parameter_process_verbosity)); + } + } + } + + main->setting.flag &= ~kt_remove_main_flag_version_copyright_help_d; + + if (main->program.parameters.array[f_console_standard_parameter_help_e].result & f_console_result_found_e) { + main->setting.flag |= kt_remove_main_flag_help_d; + } + + if (main->program.parameters.array[f_console_standard_parameter_version_e].result & f_console_result_found_e) { + main->setting.flag |= kt_remove_main_flag_version_d; + } + + if (main->program.parameters.array[f_console_standard_parameter_copyright_e].result & f_console_result_found_e) { + main->setting.flag |= kt_remove_main_flag_copyright_d; + } + } +#endif // _di_kt_remove_setting_load_standard_ + #ifdef __cplusplus } // extern "C" #endif diff --git a/sources/c/program/kevux/tools/remove/main/common.h b/sources/c/program/kevux/tools/remove/main/common.h index bed82c0..3cf4ba5 100644 --- a/sources/c/program/kevux/tools/remove/main/common.h +++ b/sources/c/program/kevux/tools/remove/main/common.h @@ -88,6 +88,39 @@ extern "C" { extern void kt_remove_setting_load_id(kt_remove_main_t * const main, f_console_parameter_t * const parameter, f_ids_t * const ids, const f_string_static_t name, const uint64_t flag, const uint8_t is_user); #endif // _di_kt_remove_setting_load_id_ +/** + * Perform the standard program setting load process, specifically handling the FLL standard parameters. + * + * This prints error messages as appropriate. + * + * @param arguments + * The parameters passed to the process (often referred to as command line arguments). + * @param main + * The main program and settings data. + * + * The setting.flag has kt_remove_flag_option_used_d forcibly cleared on the start of this function. + * Other settings may be changed by this function so be sure to apply any special modifications after calling this rather than before. + * + * Must not be NULL. + * + * This alters setting.status: + * F_okay on success. + * F_data_not on success but nothing was provided to operate with. + * + * F_parameter (with error bit) on parameter error. + * + * Errors (with error bit) from: f_console_parameter_process(). + * Errors (with error bit) from: fll_program_parameter_process_context(). + * Errors (with error bit) from: fll_program_parameter_process_verbosity(). + * + * @see f_console_parameter_process() + * @see fll_program_parameter_process_context() + * @see fll_program_parameter_process_verbosity() + */ +#ifndef _di_kt_remove_setting_load_standard_ + extern void kt_remove_setting_load_standard(const f_console_arguments_t arguments, kt_remove_main_t * const main); +#endif // _di_kt_remove_setting_load_standard_ + #ifdef __cplusplus } // extern "C" #endif diff --git a/sources/c/program/kevux/tools/remove/main/common/define.h b/sources/c/program/kevux/tools/remove/main/common/define.h index 379ccac..1a077c0 100644 --- a/sources/c/program/kevux/tools/remove/main/common/define.h +++ b/sources/c/program/kevux/tools/remove/main/common/define.h @@ -216,90 +216,94 @@ extern "C" { * Flags passed to the main function or program. * * kt_remove_main_flag_*_d: - * - none: No flags set. - * - accessed: Remove by last accessed datetime. - * - accessed_changed_updated: A helper flag representing accessed, changed, and updated flag bits being set. - * - block: Remove by file type: block. - * - changed: Remove by changed datetime. - * - character: Remove by file type: character. - * - copyright: Print copyright. - * - different: Remove by user different from caller. - * - directory: Remove by file type: directory. - * - empty_all: Helper flag representing all empty flag bits. - * - empty_only: Remove empty directories. - * - empty_only_fail: Fail on empty directories. - * - empty_not: Remove not empty directories. - * - empty_not_fail: Fail on not empty directories. - * - fifo: Remove by file type: FIFO. - * - follow: Follow symbolic links for deleting the file being pointed to rather than the link itself (when not set the link itself is deleted). - * - force: Forcibly delete. - * - force_simulate: A helper flag representing force and simulate flag bits being set. - * - group: Remove by GID. - * - help: Print help. - * - link: Remove by file type: link. - * - mode: Remove by mode. - * - option_used: Designates that type match options are in use, overriding the default behavior. - * - prompt_all: Helper flag representing all prompt flag bits. - * - prompt_each: Operate in interactive mode, prompting for every file. - * - prompt_follow: Operate in interactive mode: prompting for every link that is being followed. - * - prompt_never: Do not operate in interactive mode. - * - prompt_once: Operate in interactive mode: prompting once if removing 3 or more files or when operating recursively. - * - recurse: Recurse directories. - * - regular: Remove by file type: regular. - * - remember: Enable remembering paths already processed. - * - same: Remove by same user as caller. - * - simulate: Do not actually perform deletes, instead print messages (when silent, should still return 0 or 1). - * - socket: Remove by file type: socket. - * - tree: Remove directory tree (parent directories) (remove a/b/c, removes a/b/c, then a/b/, then a). - * - updated: Remove by last updated datetime. - * - user: Remove by UID. - * - unknown: Remove by file type: unknown. - * - utc: Process dates in UTC mode. - * - version: Print version. - * - version_copyright_help: A helper flag representing version, copyright, and help flag bits being set. + * - none: No flags set. + * - accessed: Remove by last accessed datetime. + * - accessed_changed_updated: A helper flag representing accessed, changed, and updated flag bits being set. + * - block: Remove by file type: block. + * - changed: Remove by changed datetime. + * - character: Remove by file type: character. + * - copyright: Print copyright. + * - different: Remove by user different from caller. + * - directory: Remove by file type: directory. + * - empty_all: Helper flag representing all empty flag bits. + * - empty_only: Remove empty directories. + * - empty_only_fail: Fail on empty directories. + * - empty_not: Remove not empty directories. + * - empty_not_fail: Fail on not empty directories. + * - fifo: Remove by file type: FIFO. + * - file_types_except_directory: A helper flag representing all file types except for the directory type. + * - follow: Follow symbolic links for deleting the file being pointed to rather than the link itself (when not set the link itself is deleted). + * - force: Forcibly delete. + * - force_simulate: A helper flag representing force and simulate flag bits being set. + * - group: Remove by GID. + * - help: Print help. + * - link: Remove by file type: link. + * - mode: Remove by mode. + * - option_used: Designates that type match options are in use, overriding the default behavior. + * - prompt_all: Helper flag representing all prompt flag bits. + * - prompt_each: Operate in interactive mode, prompting for every file. + * - prompt_follow: Operate in interactive mode: prompting for every link that is being followed. + * - prompt_never: Do not operate in interactive mode. + * - prompt_once: Operate in interactive mode: prompting once if removing 3 or more files or when operating recursively. + * - recurse: Recurse directories. + * - regular: Remove by file type: regular. + * - remember: Enable remembering paths already processed. + * - root: Allow for deleting root directory (Dangerous!). + * - same: Remove by same user as caller. + * - simulate: Do not actually perform deletes, instead print messages (when silent, should still return 0 or 1). + * - socket: Remove by file type: socket. + * - tree: Remove directory tree (parent directories) (remove a/b/c, removes a/b/c, then a/b/, then a). + * - updated: Remove by last updated datetime. + * - user: Remove by UID. + * - unknown: Remove by file type: unknown. + * - utc: Process dates in UTC mode. + * - version: Print version. + * - version_copyright_help: A helper flag representing version, copyright, and help flag bits being set. */ #ifndef _di_kt_remove_main_flag_d_ - #define kt_remove_main_flag_none_d 0x0 - #define kt_remove_main_flag_accessed_d 0x1 - #define kt_remove_main_flag_accessed_changed_updated_d 0x40000005 - #define kt_remove_main_flag_block_d 0x2 - #define kt_remove_main_flag_changed_d 0x4 - #define kt_remove_main_flag_character_d 0x8 - #define kt_remove_main_flag_copyright_d 0x10 - #define kt_remove_main_flag_different_d 0x20 - #define kt_remove_main_flag_directory_d 0x40 - #define kt_remove_main_flag_empty_all_d 0x780 - #define kt_remove_main_flag_empty_only_d 0x80 - #define kt_remove_main_flag_empty_only_fail_d 0x100 - #define kt_remove_main_flag_empty_not_d 0x200 - #define kt_remove_main_flag_empty_not_fail_d 0x400 - #define kt_remove_main_flag_fifo_d 0x800 - #define kt_remove_main_flag_follow_d 0x1000 - #define kt_remove_main_flag_force_d 0x2000 - #define kt_remove_main_flag_force_simulate_d 0x8002000 - #define kt_remove_main_flag_group_d 0x4000 - #define kt_remove_main_flag_help_d 0x8000 - #define kt_remove_main_flag_link_d 0x10000 - #define kt_remove_main_flag_mode_d 0x20000 - #define kt_remove_main_flag_option_used_d 0x40000 - #define kt_remove_main_flag_prompt_all_d 0x780000 - #define kt_remove_main_flag_prompt_each_d 0x80000 - #define kt_remove_main_flag_prompt_follow_d 0x100000 - #define kt_remove_main_flag_prompt_never_d 0x200000 - #define kt_remove_main_flag_prompt_once_d 0x400000 - #define kt_remove_main_flag_recurse_d 0x800000 - #define kt_remove_main_flag_regular_d 0x1000000 - #define kt_remove_main_flag_remember_d 0x2000000 - #define kt_remove_main_flag_same_d 0x4000000 - #define kt_remove_main_flag_simulate_d 0x8000000 - #define kt_remove_main_flag_socket_d 0x10000000 - #define kt_remove_main_flag_tree_d 0x20000000 - #define kt_remove_main_flag_updated_d 0x40000000 - #define kt_remove_main_flag_user_d 0x80000000 - #define kt_remove_main_flag_unknown_d 0x100000000 - #define kt_remove_main_flag_utc_d 0x200000000 - #define kt_remove_main_flag_version_d 0x400000000 - #define kt_remove_main_flag_version_copyright_help_d 0x400008010 + #define kt_remove_main_flag_none_d 0x0 + #define kt_remove_main_flag_accessed_d 0x1 + #define kt_remove_main_flag_accessed_changed_updated_d 0x80000005 + #define kt_remove_main_flag_block_d 0x2 + #define kt_remove_main_flag_changed_d 0x4 + #define kt_remove_main_flag_character_d 0x8 + #define kt_remove_main_flag_copyright_d 0x10 + #define kt_remove_main_flag_different_d 0x20 + #define kt_remove_main_flag_directory_d 0x40 + #define kt_remove_main_flag_empty_all_d 0x780 + #define kt_remove_main_flag_empty_only_d 0x80 + #define kt_remove_main_flag_empty_only_fail_d 0x100 + #define kt_remove_main_flag_empty_not_d 0x200 + #define kt_remove_main_flag_empty_not_fail_d 0x400 + #define kt_remove_main_flag_fifo_d 0x800 + #define kt_remove_main_flag_file_types_except_directory_d 0x2101080a + #define kt_remove_main_flag_follow_d 0x1000 + #define kt_remove_main_flag_force_d 0x2000 + #define kt_remove_main_flag_force_simulate_d 0x10002000 + #define kt_remove_main_flag_group_d 0x4000 + #define kt_remove_main_flag_help_d 0x8000 + #define kt_remove_main_flag_link_d 0x10000 + #define kt_remove_main_flag_mode_d 0x20000 + #define kt_remove_main_flag_option_used_d 0x40000 + #define kt_remove_main_flag_prompt_all_d 0x780000 + #define kt_remove_main_flag_prompt_each_d 0x80000 + #define kt_remove_main_flag_prompt_follow_d 0x100000 + #define kt_remove_main_flag_prompt_never_d 0x200000 + #define kt_remove_main_flag_prompt_once_d 0x400000 + #define kt_remove_main_flag_recurse_d 0x800000 + #define kt_remove_main_flag_regular_d 0x1000000 + #define kt_remove_main_flag_remember_d 0x2000000 + #define kt_remove_main_flag_root_d 0x4000000 + #define kt_remove_main_flag_same_d 0x8000000 + #define kt_remove_main_flag_simulate_d 0x10000000 + #define kt_remove_main_flag_socket_d 0x20000000 + #define kt_remove_main_flag_tree_d 0x40000000 + #define kt_remove_main_flag_updated_d 0x80000000 + #define kt_remove_main_flag_user_d 0x100000000 + #define kt_remove_main_flag_unknown_d 0x200000000 + #define kt_remove_main_flag_utc_d 0x400000000 + #define kt_remove_main_flag_version_d 0x800000000 + #define kt_remove_main_flag_version_copyright_help_d 0x800008010 #endif // _di_kt_remove_main_flag_e_ /** diff --git a/sources/c/program/kevux/tools/remove/main/print/message.c b/sources/c/program/kevux/tools/remove/main/print/message.c index 5deb8ba..2023e09 100644 --- a/sources/c/program/kevux/tools/remove/main/print/message.c +++ b/sources/c/program/kevux/tools/remove/main/print/message.c @@ -39,7 +39,7 @@ extern "C" { fll_program_print_help_option(print, kt_remove_short_link_s, kt_remove_long_link_s, f_console_symbol_short_normal_s, f_console_symbol_long_normal_s, " Remove by file type of link."); fll_program_print_help_option(print, kt_remove_short_mode_s, kt_remove_long_mode_s, f_console_symbol_short_normal_s, f_console_symbol_long_normal_s, " Remove by file mode."); fll_program_print_help_option(print, kt_remove_short_prompt_s, kt_remove_long_prompt_s, f_console_symbol_short_normal_s, f_console_symbol_long_normal_s, " Prompt before removing."); - fll_program_print_help_option(print, kt_remove_short_recurse_s, kt_remove_long_recurse_s, f_console_symbol_short_normal_s, f_console_symbol_long_normal_s, " Traverse into sub-directories when removing."); + fll_program_print_help_option(print, kt_remove_short_recurse_s, kt_remove_long_recurse_s, f_console_symbol_short_normal_s, f_console_symbol_long_normal_s, " Traverse into directories when removing."); fll_program_print_help_option(print, kt_remove_short_regular_s, kt_remove_long_regular_s, f_console_symbol_short_normal_s, f_console_symbol_long_normal_s, " Remove by file type of regular."); fll_program_print_help_option(print, kt_remove_short_same_s, kt_remove_long_same_s, f_console_symbol_short_normal_s, f_console_symbol_long_normal_s, " Remove by file owner by the current calling user."); fll_program_print_help_option(print, kt_remove_short_simulate_s, kt_remove_long_simulate_s, f_console_symbol_short_normal_s, f_console_symbol_long_normal_s, " Simulate removal rather than actually removing."); diff --git a/sources/c/program/kevux/tools/remove/remove/main.c b/sources/c/program/kevux/tools/remove/remove/main.c index f6b0940..23bbde3 100644 --- a/sources/c/program/kevux/tools/remove/remove/main.c +++ b/sources/c/program/kevux/tools/remove/remove/main.c @@ -94,7 +94,11 @@ int fll_program_standard_set_down(&data.program); #endif // _kt_MOCK_MAIN_NAME - return (F_status_is_error(data.setting.state.status) || data.setting.state.status == F_false) ? 1 : 0; + if (F_status_is_error(data.setting.state.status)) { + return F_status_set_fine(data.setting.state.status) == F_support_not ? 2 : 1; + } + + return data.setting.state.status == F_false ? 1 : 0; } #ifdef __cplusplus diff --git a/sources/c/program/kevux/tools/remove/rm/enumeration.h b/sources/c/program/kevux/tools/remove/rm/enumeration.h index 8b22930..e9f7c12 100644 --- a/sources/c/program/kevux/tools/remove/rm/enumeration.h +++ b/sources/c/program/kevux/tools/remove/rm/enumeration.h @@ -22,13 +22,15 @@ extern "C" { #ifndef _di_kt_remove_rm_parameter_e_ enum { kt_remove_rm_parameter_directory_e = f_console_standard_parameter_last_e, - kt_remove_rm_parameter_each_e, // -i + kt_remove_rm_parameter_each_e, kt_remove_rm_parameter_force_e, - kt_remove_rm_parameter_once_e, // -I + kt_remove_rm_parameter_once_e, kt_remove_rm_parameter_one_filesystem_e, - kt_remove_rm_parameter_recursive_e, // -r, --recursive - kt_remove_rm_parameter_recursive_alt_e, // -R + kt_remove_rm_parameter_recursive_e, + kt_remove_rm_parameter_recursive_alt_e, kt_remove_rm_parameter_simulate_e, + kt_remove_rm_parameter_preserve_root_e, + kt_remove_rm_parameter_preserve_root_not_e, kt_remove_rm_parameter_verbose_alt_e, kt_remove_rm_parameter_version_alt_e, }; // enum @@ -37,19 +39,21 @@ extern "C" { { \ macro_fll_program_console_parameter_standard_initialize, \ \ - macro_f_console_parameter_t_initialize_3(kt_remove_short_directory_s, kt_remove_long_directory_s, 0, f_console_flag_normal_e), \ - macro_f_console_parameter_t_initialize_6( kt_remove_rm_short_each_s, 0, f_console_flag_normal_e), \ - macro_f_console_parameter_t_initialize_3(kt_remove_short_force_s, kt_remove_long_force_s, 0, f_console_flag_normal_e), \ - macro_f_console_parameter_t_initialize_6( kt_remove_rm_short_once_s, 0, f_console_flag_normal_e), \ - macro_f_console_parameter_t_initialize_5( kt_remove_rm_long_one_filesystem_s, 0, f_console_flag_normal_e), \ - macro_f_console_parameter_t_initialize_3(kt_remove_short_recursive_s, kt_remove_long_recursive_s, 0, f_console_flag_normal_e), \ - macro_f_console_parameter_t_initialize_6( kt_remove_rm_short_recursive_alt_s, 0, f_console_flag_normal_e), \ - macro_f_console_parameter_t_initialize_3(kt_remove_short_simulate_s, kt_remove_long_simulate_s, 0, f_console_flag_normal_e), \ - macro_f_console_parameter_t_initialize_3(f_console_standard_short_verbose_s, f_console_standard_long_verbose_s, 0, f_console_flag_normal_e), \ - macro_f_console_parameter_t_initialize_5( f_console_standard_long_version_s, 0, f_console_flag_normal_e), \ + macro_f_console_parameter_t_initialize_3(kt_remove_short_directory_s, kt_remove_rm_long_directory_s, 0, f_console_flag_normal_e), \ + macro_f_console_parameter_t_initialize_6( kt_remove_rm_short_each_s, 0, f_console_flag_normal_e), \ + macro_f_console_parameter_t_initialize_3(kt_remove_short_force_s, kt_remove_long_force_s, 0, f_console_flag_normal_e), \ + macro_f_console_parameter_t_initialize_6( kt_remove_rm_short_once_s, 0, f_console_flag_normal_e), \ + macro_f_console_parameter_t_initialize_5( kt_remove_rm_long_one_filesystem_s, 0, f_console_flag_normal_e), \ + macro_f_console_parameter_t_initialize_3(kt_remove_short_recurse_s, kt_remove_long_recurse_s, 0, f_console_flag_normal_e), \ + macro_f_console_parameter_t_initialize_6( kt_remove_rm_short_recursive_s, 0, f_console_flag_normal_e), \ + macro_f_console_parameter_t_initialize_3(kt_remove_short_simulate_s, kt_remove_long_simulate_s, 0, f_console_flag_normal_e), \ + macro_f_console_parameter_t_initialize_5( kt_remove_rm_long_preserve_root_s, 0, f_console_flag_normal_e), \ + macro_f_console_parameter_t_initialize_5( kt_remove_rm_long_preserve_root_not_s, 0, f_console_flag_normal_e), \ + macro_f_console_parameter_t_initialize_3(f_console_standard_short_verbose_s, f_console_standard_long_verbose_s, 0, f_console_flag_normal_e), \ + macro_f_console_parameter_t_initialize_5( f_console_standard_long_version_s, 0, f_console_flag_normal_e), \ } - #define kt_remove_rm_total_parameters_d (f_console_parameter_state_type_total_d + 10) + #define kt_remove_rm_total_parameters_d (f_console_parameter_state_type_total_d + 12) #endif // _di_kt_remove_rm_parameter_e_ #ifdef __cplusplus diff --git a/sources/c/program/kevux/tools/remove/rm/main.c b/sources/c/program/kevux/tools/remove/rm/main.c index 1c2f32b..29b0f9d 100644 --- a/sources/c/program/kevux/tools/remove/rm/main.c +++ b/sources/c/program/kevux/tools/remove/rm/main.c @@ -85,7 +85,11 @@ int main(const int argc, const f_string_t *argv, const f_string_t *envp) { fll_program_standard_set_down(&data.program); - return (F_status_is_error(data.setting.state.status) || data.setting.state.status == F_false) ? 1 : 0; + if (F_status_is_error(data.setting.state.status)) { + return F_status_set_fine(data.setting.state.status) == F_support_not ? 2 : 1; + } + + return data.setting.state.status == F_false ? 1 : 0; } #ifdef __cplusplus diff --git a/sources/c/program/kevux/tools/remove/rm/print.c b/sources/c/program/kevux/tools/remove/rm/print.c index 45a6e57..5e882e7 100644 --- a/sources/c/program/kevux/tools/remove/rm/print.c +++ b/sources/c/program/kevux/tools/remove/rm/print.c @@ -4,23 +4,41 @@ extern "C" { #endif -#ifndef _di_kt_remove_rm_print_error_directory_not_ - f_status_t kt_remove_rm_print_error_directory_not(fl_print_t * const print, const f_string_static_t path) { +#ifndef _di_kt_remove_rm_print_error_directory_ + f_status_t kt_remove_rm_print_error_directory(fl_print_t * const print, const f_string_static_t path) { if (!print) return F_status_set_error(F_output_not); if (print->verbosity < f_console_verbosity_error_e) return F_output_not; f_file_stream_lock(print->to); - fl_print_format("%[%QCannot remove directory '%]", print->to, print->set->error, print->prefix, print->set->error); + fl_print_format("%[%QCannot remove '%]", print->to, print->set->error, print->prefix, print->set->error); fl_print_format("%[%Q%]", print->to, print->set->notable, path, print->set->notable); - fl_print_format("%[', file is not a directory.%]%r", print->to, print->set->error, print->set->error, f_string_eol_s); + fl_print_format("%[', file is a directory.%]%r", print->to, print->set->error, print->set->error, f_string_eol_s); f_file_stream_unlock(print->to); return F_okay; } -#endif // _di_kt_remove_rm_print_error_directory_not_ +#endif // _di_kt_remove_rm_print_error_directory_ + +#ifndef _di_kt_remove_rm_print_error_directory_empty_not_ + f_status_t kt_remove_rm_print_error_directory_empty_not(fl_print_t * const print, const f_string_static_t path) { + + if (!print) return F_status_set_error(F_output_not); + if (print->verbosity < f_console_verbosity_error_e) return F_output_not; + + f_file_stream_lock(print->to); + + fl_print_format("%[%QCannot remove '%]", print->to, print->set->error, print->prefix, print->set->error); + fl_print_format("%[%Q%]", print->to, print->set->notable, path, print->set->notable); + fl_print_format("%[', directory is not empty.%]%r", print->to, print->set->error, print->set->error, f_string_eol_s); + + f_file_stream_unlock(print->to); + + return F_okay; + } +#endif // _di_kt_remove_rm_print_error_directory_empty_not_ #ifndef _di_kt_remove_rm_print_message_help_ f_status_t kt_remove_rm_print_message_help(fl_print_t * const print, const f_color_context_t context) { @@ -43,11 +61,19 @@ extern "C" { f_print_dynamic_raw(f_string_eol_s, print->to); - fll_program_print_help_option(print, kt_remove_rm_short_parents_s, kt_remove_rm_long_parents_s, f_console_symbol_short_normal_s, f_console_symbol_long_normal_s, " Remove directory and the parent directories in the path provided."); - fll_program_print_help_option(print, kt_remove_short_simulate_s, kt_remove_long_simulate_s, f_console_symbol_short_normal_s, f_console_symbol_long_normal_s, " Simulate removal rather than actually rming."); + fll_program_print_help_option(print, kt_remove_short_force_s, kt_remove_long_force_s, f_console_symbol_short_normal_s, f_console_symbol_long_normal_s, " Operate in interactive mode, prompting for every file."); + + fll_program_print_help_option_short(print, kt_remove_rm_short_each_s, f_console_symbol_short_normal_s, " Prompt for every file when removing."); + fll_program_print_help_option_short(print, kt_remove_rm_short_once_s, f_console_symbol_short_normal_s, " Operate in interactive mode: prompting if removing 3 or more files."); + + fll_program_print_help_option(print, kt_remove_short_recurse_s, f_string_empty_s, f_console_symbol_short_normal_s, f_string_empty_s, 0); + fll_program_print_help_option(print, kt_remove_rm_short_recursive_s, kt_remove_rm_long_recursive_s, f_console_symbol_short_normal_s, f_console_symbol_long_normal_s, " Traverse into directories when removing."); + fll_program_print_help_option(print, kt_remove_short_simulate_s, kt_remove_long_simulate_s, f_console_symbol_short_normal_s, f_console_symbol_long_normal_s, " Simulate removal rather than actually removing."); - fll_program_print_help_option_long(print, kt_remove_rm_long_ignore_empty_not_s, f_console_symbol_long_normal_s, "Ignore directories that are not empty rather than fail on error."); - fll_program_print_help_option_long(print, f_console_standard_long_version_s, f_console_symbol_long_normal_s, " Print this help message."); + fll_program_print_help_option_long(print, kt_remove_rm_long_preserve_root_s, f_console_symbol_long_normal_s, " Prevent deletion of the root directoy '/' (default)."); + fll_program_print_help_option_long(print, kt_remove_rm_long_preserve_root_not_s, f_console_symbol_long_normal_s, "Allow deletion of the root directory '/' (dangerous)."); + fll_program_print_help_option_long(print, f_console_standard_long_verbose_s, f_console_symbol_long_normal_s, " Increase verbosity beyond normal, printing more details."); + fll_program_print_help_option_long(print, f_console_standard_long_version_s, f_console_symbol_long_normal_s, " Print only the program version number and immediately exit."); f_print_dynamic_raw(f_string_eol_s, print->to); diff --git a/sources/c/program/kevux/tools/remove/rm/print.h b/sources/c/program/kevux/tools/remove/rm/print.h index 0b4c74f..6ac0b04 100644 --- a/sources/c/program/kevux/tools/remove/rm/print.h +++ b/sources/c/program/kevux/tools/remove/rm/print.h @@ -17,7 +17,7 @@ extern "C" { #endif /** - * Print error message about not being able to delete a file because that file is not a directory. + * Print error message about not being able to delete a file because that file is a directory. * * @param print * The output structure to print to. @@ -36,9 +36,33 @@ extern "C" { * * @see fll_error_print() */ -#ifndef _di_kt_remove_rm_print_error_directory_not_ - extern f_status_t kt_remove_rm_print_error_directory_not(fl_print_t * const print, const f_string_static_t path); -#endif // _di_kt_remove_rm_print_error_directory_not_ +#ifndef _di_kt_remove_rm_print_error_directory_ + extern f_status_t kt_remove_rm_print_error_directory(fl_print_t * const print, const f_string_static_t path); +#endif // _di_kt_remove_rm_print_error_directory_ + +/** + * Print error message about not being able to delete a file because that file is not an empty directory. + * + * @param print + * The output structure to print to. + * + * Must not be NULL. + * + * This does not alter print.custom.setting.state.status. + * @param path + * The file that cannot be deleted. + * + * @return + * F_okay on success. + * F_output_not on success, but no printing is performed. + * + * F_output_not (with error bit) if setting is NULL. + * + * @see fll_error_print() + */ +#ifndef _di_kt_remove_rm_print_error_directory_empty_not_ + extern f_status_t kt_remove_rm_print_error_directory_empty_not(fl_print_t * const print, const f_string_static_t path); +#endif // _di_kt_remove_rm_print_error_directory_empty_not_ /** * Print help. diff --git a/sources/c/program/kevux/tools/remove/rm/rm.c b/sources/c/program/kevux/tools/remove/rm/rm.c index e2c438c..03dafbe 100644 --- a/sources/c/program/kevux/tools/remove/rm/rm.c +++ b/sources/c/program/kevux/tools/remove/rm/rm.c @@ -12,12 +12,21 @@ extern "C" { if (!flag_operate) return F_status_set_error(F_parameter); if (flag_operate & kt_remove_flag_file_operate_directory_d) { - return kt_remove_operate_file_remove(main, path, flag_operate); - } + if (main->setting.flag & kt_remove_main_flag_empty_only_d) { + if (!(flag_operate & kt_remove_flag_file_operate_empty_d)) { + kt_remove_rm_print_error_directory_empty_not(&main->program.error, path); - kt_remove_rm_print_error_directory_not(&main->program.error, path); + return F_status_set_error(F_no); + } + } + else { + kt_remove_rm_print_error_directory(&main->program.error, path); - return F_status_set_error(F_no); + return F_status_set_error(F_no); + } + } + + return kt_remove_operate_file_remove(main, path, flag_operate); } #endif // _di_kt_remove_rm_operate_file_remove_ @@ -26,129 +35,166 @@ extern "C" { if (!main) return; - main->setting.flag &= ~kt_remove_main_flag_option_used_d; + f_number_unsigned_t argc = 0; + + // Pre-process the parameters to find any "--" such that everything after the "--" represents a literal file name. + if (arguments.argv) { + for (argc = 1; argc < arguments.argc; ++argc) { + if (f_compare_dynamic_string(arguments.argv[argc], kt_remove_rm_static_end_s, strnlen(arguments.argv[argc], F_console_parameter_size_d)) == F_equal_to) break; + } // for + } + + if (argc && argc < arguments.argc) { + const f_console_arguments_t arguments_shorter = macro_f_console_arguments_t_initialize_1(argc, arguments.argv, arguments.envp); - main->setting.state.step_small = kt_remove_allocation_console_d; + kt_remove_setting_load_standard(arguments_shorter, main); + } + else { + kt_remove_setting_load_standard(arguments, main); + } - f_console_parameter_process(arguments, &main->program.parameters, &main->setting.state, 0); + if (F_status_is_error(main->setting.state.status)) return; - main->setting.state.step_small = kt_remove_allocation_small_d; + if (main->program.parameters.array[kt_remove_rm_parameter_directory_e].result & f_console_result_found_e) { + main->setting.flag |= kt_remove_main_flag_directory_d | kt_remove_main_flag_empty_only_d; + } - if (F_status_is_error(main->setting.state.status)) { - kt_remove_print_error(&main->program.error, macro_kt_remove_f(f_console_parameter_process)); + if (main->program.parameters.array[kt_remove_rm_parameter_each_e].result & f_console_result_found_e) { + main->setting.flag |= kt_remove_main_flag_prompt_each_d; + } + + if (main->program.parameters.array[kt_remove_rm_parameter_force_e].result & f_console_result_found_e) { + main->setting.flag |= kt_remove_main_flag_force_d; + } + + if (main->program.parameters.array[kt_remove_rm_parameter_once_e].result & f_console_result_found_e) { + main->setting.flag |= kt_remove_main_flag_prompt_once_d; + } + + if (main->program.parameters.array[kt_remove_rm_parameter_one_filesystem_e].result & f_console_result_found_e) { + main->setting.state.status = F_status_set_error(F_support_not); + + fll_program_print_error_parameter_support_not(&main->program.error, f_console_symbol_long_normal_s, kt_remove_rm_long_one_filesystem_s); return; } - { - f_uint16s_t choices = f_uint16s_t_initialize; + if ((main->program.parameters.array[kt_remove_rm_parameter_recursive_e].result & f_console_result_found_e) || (main->program.parameters.array[kt_remove_rm_parameter_recursive_alt_e].result & f_console_result_found_e)) { + main->setting.flag |= kt_remove_main_flag_recurse_d; + } - // Identify and prioritize "color context" parameters. - { - uint16_t choices_array[3] = { kt_remove_rm_parameter_no_color_e, kt_remove_rm_parameter_light_e, kt_remove_rm_parameter_dark_e }; - choices.array = choices_array; - choices.used = 3; + // @todo the kt_remove_main_flag_root_d needs to be implemented in the base remove project. + if (main->program.parameters.array[kt_remove_rm_parameter_preserve_root_e].result & f_console_result_found_e) { + main->setting.flag &= ~kt_remove_main_flag_root_d; // @todo check if this or preserve_root_not is farther to the right. + } - const uint8_t modes[3] = { f_color_mode_not_e, f_color_mode_light_e, f_color_mode_dark_e }; + if (main->program.parameters.array[kt_remove_rm_parameter_preserve_root_not_e].result & f_console_result_found_e) { + main->setting.flag |= kt_remove_main_flag_root_d; // @todo check if this or preserve_root is farther to the right. + } - main->setting.state.status = fll_program_parameter_process_context(choices, modes, F_true, &main->program); + if (main->program.parameters.array[kt_remove_rm_parameter_simulate_e].result & f_console_result_found_e) { + main->setting.flag |= kt_remove_main_flag_simulate_d; + } - if (F_status_is_error(main->setting.state.status)) { - kt_remove_print_error(&main->program.error, macro_kt_remove_f(fll_program_parameter_process_context)); + if (main->program.parameters.array[kt_remove_rm_parameter_verbose_alt_e].result & f_console_result_found_e) { + main->program.warning.verbosity + = main->program.error.verbosity + = main->program.output.verbosity + = main->program.message.verbosity + = f_console_verbosity_verbose_e; + } - return; - } + if (main->program.parameters.array[kt_remove_rm_parameter_version_alt_e].result & f_console_result_found_e) { + main->setting.flag |= kt_remove_main_flag_version_d; + } + + // Load all remaining files as static strings (setting size to 0). + if (main->program.parameters.remaining.used || argc < arguments.argc) { + main->setting.state.status = f_memory_array_increase_by( + main->program.parameters.remaining.used + ((argc + 1 < arguments.argc) ? arguments.argc - (argc + 1) : 0), + sizeof(f_string_dynamic_t), + (void **) &main->setting.files.array, + &main->setting.files.used, + &main->setting.files.size + ); + + if (F_status_is_error(main->setting.state.status)) { + kt_remove_print_error(&main->program.error, macro_kt_remove_f(f_memory_array_increase_by)); + + return; } - // Identify and prioritize "verbosity" parameters. { - if (main->program.parameters.array[kt_remove_rm_parameter_verbose_alt_e].result & f_console_result_found_e) { - main->program.warning.verbosity - = main->program.error.verbosity - = main->program.output.verbosity - = main->program.message.verbosity - = f_console_verbosity_verbose_e; - } - else { - uint16_t choices_array[5] = { kt_remove_rm_parameter_verbosity_quiet_e, kt_remove_rm_parameter_verbosity_error_e, kt_remove_rm_parameter_verbosity_verbose_e, kt_remove_rm_parameter_verbosity_debug_e, kt_remove_rm_parameter_verbosity_normal_e }; - choices.array = choices_array; - choices.used = 5; + f_number_unsigned_t index = 0; - const uint8_t verbosity[5] = { f_console_verbosity_quiet_e, f_console_verbosity_error_e, f_console_verbosity_verbose_e, f_console_verbosity_debug_e, f_console_verbosity_normal_e }; + for (f_number_unsigned_t i = 0; i < main->program.parameters.remaining.used; ++i) { - main->setting.state.status = fll_program_parameter_process_verbosity(choices, verbosity, F_true, &main->program); + index = main->program.parameters.remaining.array[i]; - if (F_status_is_error(main->setting.state.status)) { - kt_remove_print_error(&main->program.error, macro_kt_remove_f(fll_program_parameter_process_verbosity)); + if (f_compare_dynamic(kt_remove_rm_static_interactive_always_s, main->program.parameters.arguments.array[index]) == F_equal_to) { + main->setting.flag &= ~kt_remove_main_flag_prompt_all_d; + main->setting.flag |= kt_remove_main_flag_prompt_each_d; - return; + continue; } - } - } - } - f_number_unsigned_t i = 0; - f_number_unsigned_t index = 0; - f_number_unsigned_t index2 = 0; - f_number_unsigned_t total_locations = 0; - f_number_unsigned_t total_arguments = 0; + if (f_compare_dynamic(kt_remove_rm_static_interactive_never_s, main->program.parameters.arguments.array[index]) == F_equal_to) { + main->setting.flag &= ~kt_remove_main_flag_prompt_all_d; + main->setting.flag |= kt_remove_main_flag_prompt_never_d; - uint8_t j = 0; + continue; + } - main->setting.flag &= ~kt_remove_main_flag_version_copyright_help_d; + if (f_compare_dynamic(kt_remove_rm_static_interactive_once_s, main->program.parameters.arguments.array[index]) == F_equal_to) { + main->setting.flag &= ~kt_remove_main_flag_prompt_all_d; + main->setting.flag |= kt_remove_main_flag_prompt_once_d; - if (main->program.parameters.array[kt_remove_rm_parameter_help_e].result & f_console_result_found_e) { - main->setting.flag |= kt_remove_main_flag_help_d; - } + continue; + } - if ((main->program.parameters.array[kt_remove_rm_parameter_version_e].result & f_console_result_found_e) || (main->program.parameters.array[kt_remove_rm_parameter_version_alt_e].result & f_console_result_found_e)) { - main->setting.flag |= kt_remove_main_flag_version_d; - } + if (f_compare_dynamic(kt_remove_rm_static_preserve_root_all_s, main->program.parameters.arguments.array[index]) == F_equal_to) { + main->setting.state.status = F_status_set_error(F_support_not); - if (main->program.parameters.array[kt_remove_rm_parameter_copyright_e].result & f_console_result_found_e) { - main->setting.flag |= kt_remove_main_flag_copyright_d; - } + fll_program_print_error_parameter_support_not(&main->program.error, f_console_symbol_long_normal_s, kt_remove_rm_long_one_filesystem_s); - if (main->program.parameters.array[kt_remove_rm_parameter_ignore_empty_not_e].result & f_console_result_found_e) { - main->setting.flag |= kt_remove_main_flag_empty_only_d; - } + return; + } - if (main->program.parameters.array[kt_remove_rm_parameter_parents_e].result & f_console_result_found_e) { - main->setting.flag |= kt_remove_main_flag_tree_d; - } + main->setting.files.array[main->setting.files.used].used = 0; - if (main->program.parameters.array[kt_remove_rm_parameter_simulate_e].result & f_console_result_found_e) { - main->setting.flag |= kt_remove_main_flag_simulate_d; - } + fl_path_clean(main->program.parameters.arguments.array[index], &main->setting.files.array[main->setting.files.used]); - // Load all remaining files as static strings (setting size to 0). - if (main->program.parameters.remaining.used) { - main->setting.state.status = f_memory_array_increase_by(main->program.parameters.remaining.used, sizeof(f_string_dynamic_t), (void **) &main->setting.files.array, &main->setting.files.used, &main->setting.files.size); + if (F_status_is_error(main->setting.state.status)) { + kt_remove_print_error_file(&main->program.error, macro_kt_remove_f(fl_path_clean), main->program.parameters.arguments.array[index], f_file_operation_process_s, fll_error_file_type_path_e); - if (F_status_is_error(main->setting.state.status)) { - kt_remove_print_error(&main->program.error, macro_kt_remove_f(f_memory_array_increase_by)); + return; + } - return; + ++main->setting.files.used; + } // for } - for (i = 0; i < main->program.parameters.remaining.used; ++i, ++main->setting.files.used) { + if (argc < arguments.argc) { + f_string_static_t args = f_string_static_t_initialize; - index = main->program.parameters.remaining.array[i]; + for (++argc; argc < arguments.argc; ++argc, ++main->setting.files.used) { - main->setting.files.array[main->setting.files.used].used = 0; + args.string = arguments.argv[argc]; + args.used = strnlen(arguments.argv[argc], F_console_parameter_size_d); - fl_path_clean(main->program.parameters.arguments.array[index], &main->setting.files.array[main->setting.files.used]); + fl_path_clean(args, &main->setting.files.array[main->setting.files.used]); - if (F_status_is_error(main->setting.state.status)) { - kt_remove_print_error_file(&main->program.error, macro_kt_remove_f(fl_path_clean), main->program.parameters.arguments.array[index], f_file_operation_process_s, fll_error_file_type_path_e); + if (F_status_is_error(main->setting.state.status)) { + kt_remove_print_error_file(&main->program.error, macro_kt_remove_f(fl_path_clean), args, f_file_operation_process_s, fll_error_file_type_path_e); - return; - } - } // for + return; + } + } // for + } } - // Make sure only directoreis are deleted. - main->setting.flag |= kt_remove_main_flag_directory_d | kt_remove_main_flag_option_used_d; + // Make sure all non-directory files are deleted. + main->setting.flag |= kt_remove_main_flag_file_types_except_directory_d | kt_remove_main_flag_option_used_d; } #endif // _di_kt_remove_rm_setting_load_ diff --git a/sources/c/program/kevux/tools/remove/rm/rm.h b/sources/c/program/kevux/tools/remove/rm/rm.h index 01b68d7..0c71225 100644 --- a/sources/c/program/kevux/tools/remove/rm/rm.h +++ b/sources/c/program/kevux/tools/remove/rm/rm.h @@ -80,13 +80,9 @@ extern "C" { * * F_parameter (with error bit) on parameter error. * - * Errors (with error bit) from: f_console_parameter_process(). - * Errors (with error bit) from: f_file_stream_open(). - * Errors (with error bit) from: f_memory_array_increase_by(). + * Errors (with error bit) from: kt_remove_setting_load_standard(). * - * @see f_console_parameter_process() - * @see f_file_stream_open() - * @see f_memory_array_increase_by() + * @see kt_remove_setting_load_standard() */ #ifndef _di_kt_remove_rm_setting_load_ extern void kt_remove_rm_setting_load(const f_console_arguments_t arguments, kt_remove_main_t * const main); diff --git a/sources/c/program/kevux/tools/remove/rm/string.c b/sources/c/program/kevux/tools/remove/rm/string.c index 5ffdac8..62061f0 100644 --- a/sources/c/program/kevux/tools/remove/rm/string.c +++ b/sources/c/program/kevux/tools/remove/rm/string.c @@ -10,10 +10,21 @@ extern "C" { #endif // _di_remove_program_name_s_ #ifndef _di_kt_remove_rm_parameter_s_ - const f_string_static_t kt_remove_rm_short_parents_s = macro_f_string_static_t_initialize_1(KT_REMOVE_rm_short_parents_s, 0, KT_REMOVE_rm_short_parents_s_length); + const f_string_static_t kt_remove_rm_short_each_s = macro_f_string_static_t_initialize_1(KT_REMOVE_rm_short_each_s, 0, KT_REMOVE_rm_short_each_s_length); + const f_string_static_t kt_remove_rm_short_once_s = macro_f_string_static_t_initialize_1(KT_REMOVE_rm_short_once_s, 0, KT_REMOVE_rm_short_once_s_length); + const f_string_static_t kt_remove_rm_short_recursive_s = macro_f_string_static_t_initialize_1(KT_REMOVE_rm_short_recursive_s, 0, KT_REMOVE_rm_short_recursive_s_length); - const f_string_static_t kt_remove_rm_long_ignore_empty_not_s = macro_f_string_static_t_initialize_1(KT_REMOVE_rm_long_ignore_empty_not_s, 0, KT_REMOVE_rm_long_ignore_empty_not_s_length); - const f_string_static_t kt_remove_rm_long_parents_s = macro_f_string_static_t_initialize_1(KT_REMOVE_rm_long_parents_s, 0, KT_REMOVE_rm_long_parents_s_length); + const f_string_static_t kt_remove_rm_long_directory_s = macro_f_string_static_t_initialize_1(KT_REMOVE_rm_long_directory_s, 0, KT_REMOVE_rm_long_directory_s_length); + const f_string_static_t kt_remove_rm_long_one_filesystem_s = macro_f_string_static_t_initialize_1(KT_REMOVE_rm_long_one_filesystem_s, 0, KT_REMOVE_rm_long_one_filesystem_s_length); + const f_string_static_t kt_remove_rm_long_recursive_s = macro_f_string_static_t_initialize_1(KT_REMOVE_rm_long_recursive_s, 0, KT_REMOVE_rm_long_recursive_s_length); + const f_string_static_t kt_remove_rm_long_preserve_root_s = macro_f_string_static_t_initialize_1(KT_REMOVE_rm_long_preserve_root_s, 0, KT_REMOVE_rm_long_preserve_root_s_length); + const f_string_static_t kt_remove_rm_long_preserve_root_not_s = macro_f_string_static_t_initialize_1(KT_REMOVE_rm_long_preserve_root_not_s, 0, KT_REMOVE_rm_long_preserve_root_not_s_length); + + const f_string_static_t kt_remove_rm_static_end_s = macro_f_string_static_t_initialize_1(KT_REMOVE_rm_static_end_s, 0, KT_REMOVE_rm_static_end_s_length); + const f_string_static_t kt_remove_rm_static_interactive_always_s = macro_f_string_static_t_initialize_1(KT_REMOVE_rm_static_interactive_always_s, 0, KT_REMOVE_rm_static_interactive_always_s_length); + const f_string_static_t kt_remove_rm_static_interactive_never_s = macro_f_string_static_t_initialize_1(KT_REMOVE_rm_static_interactive_never_s, 0, KT_REMOVE_rm_static_interactive_never_s_length); + const f_string_static_t kt_remove_rm_static_interactive_once_s = macro_f_string_static_t_initialize_1(KT_REMOVE_rm_static_interactive_once_s, 0, KT_REMOVE_rm_static_interactive_once_s_length); + const f_string_static_t kt_remove_rm_static_preserve_root_all_s = macro_f_string_static_t_initialize_1(KT_REMOVE_rm_static_preserve_root_all_s, 0, KT_REMOVE_rm_static_preserve_root_all_s_length); #endif // _di_kt_remove_rm_parameter_s_ #ifdef __cplusplus diff --git a/sources/c/program/kevux/tools/remove/rm/string.h b/sources/c/program/kevux/tools/remove/rm/string.h index a43b9ea..8e60ccd 100644 --- a/sources/c/program/kevux/tools/remove/rm/string.h +++ b/sources/c/program/kevux/tools/remove/rm/string.h @@ -21,32 +21,63 @@ extern "C" { */ #ifndef _di_kt_remove_program_name_s_ #define KT_REMOVE_program_name_s "rm" - #define KT_REMOVE_program_name_long_s "Remove Directory" + #define KT_REMOVE_program_name_long_s "Remove File" #define KT_REMOVE_program_name_s_length 5 - #define KT_REMOVE_program_name_long_s_length 16 + #define KT_REMOVE_program_name_long_s_length 11 #endif // _di_kt_remove_program_name_s_ /** * Additional strings used for various purposes for the rm program. - * - * kt_remove_rm_parameter_*_s: Parameter related strings. */ #ifndef _di_kt_remove_rm_parameter_s_ - #define KT_REMOVE_rm_short_parents_s "p" + #define KT_REMOVE_rm_short_each_s "i" + #define KT_REMOVE_rm_short_once_s "I" + #define KT_REMOVE_rm_short_recursive_s "R" + + #define KT_REMOVE_rm_long_directory_s "dir" + #define KT_REMOVE_rm_long_one_filesystem_s "one-file-system" + #define KT_REMOVE_rm_long_recursive_s "recursive" + #define KT_REMOVE_rm_long_preserve_root_s "preserve-root" + #define KT_REMOVE_rm_long_preserve_root_not_s "no-preserve-root" + + #define KT_REMOVE_rm_static_end_s "--" + #define KT_REMOVE_rm_static_interactive_always_s "--interactive=always" + #define KT_REMOVE_rm_static_interactive_never_s "--interactive=never" + #define KT_REMOVE_rm_static_interactive_once_s "--interactive=once" + #define KT_REMOVE_rm_static_preserve_root_all_s "--preserve-root=all" + + #define KT_REMOVE_rm_short_each_s_length 1 + #define KT_REMOVE_rm_short_once_s_length 1 + #define KT_REMOVE_rm_short_recursive_s_length 1 - #define KT_REMOVE_rm_long_ignore_empty_not_s "ignore-fail-on-non-empty" - #define KT_REMOVE_rm_long_parents_s "parents" + #define KT_REMOVE_rm_long_directory_s_length 3 + #define KT_REMOVE_rm_long_one_filesystem_s_length 15 + #define KT_REMOVE_rm_long_recursive_s_length 9 + #define KT_REMOVE_rm_long_preserve_root_s_length 13 + #define KT_REMOVE_rm_long_preserve_root_not_s_length 16 - #define KT_REMOVE_rm_short_parents_s_length 1 + #define KT_REMOVE_rm_static_end_s_length 2 + #define KT_REMOVE_rm_static_interactive_always_s_length 20 + #define KT_REMOVE_rm_static_interactive_never_s_length 19 + #define KT_REMOVE_rm_static_interactive_once_s_length 18 + #define KT_REMOVE_rm_static_preserve_root_all_s_length 19 - #define KT_REMOVE_rm_long_ignore_empty_not_s_length 24 - #define KT_REMOVE_rm_long_parents_s_length 7 + extern const f_string_static_t kt_remove_rm_short_each_s; + extern const f_string_static_t kt_remove_rm_short_once_s; + extern const f_string_static_t kt_remove_rm_short_recursive_s; - extern const f_string_static_t kt_remove_rm_short_parents_s; + extern const f_string_static_t kt_remove_rm_long_directory_s; + extern const f_string_static_t kt_remove_rm_long_one_filesystem_s; + extern const f_string_static_t kt_remove_rm_long_recursive_s; + extern const f_string_static_t kt_remove_rm_long_preserve_root_s; + extern const f_string_static_t kt_remove_rm_long_preserve_root_not_s; - extern const f_string_static_t kt_remove_rm_long_ignore_empty_not_s; - extern const f_string_static_t kt_remove_rm_long_parents_s; + extern const f_string_static_t kt_remove_rm_static_end_s; + extern const f_string_static_t kt_remove_rm_static_interactive_always_s; + extern const f_string_static_t kt_remove_rm_static_interactive_never_s; + extern const f_string_static_t kt_remove_rm_static_interactive_once_s; + extern const f_string_static_t kt_remove_rm_static_preserve_root_all_s; #endif // _di_kt_remove_rm_parameter_s_ #ifdef __cplusplus diff --git a/sources/c/program/kevux/tools/remove/rmdir/main.c b/sources/c/program/kevux/tools/remove/rmdir/main.c index 8e70673..e6c29b5 100644 --- a/sources/c/program/kevux/tools/remove/rmdir/main.c +++ b/sources/c/program/kevux/tools/remove/rmdir/main.c @@ -85,7 +85,11 @@ int main(const int argc, const f_string_t *argv, const f_string_t *envp) { fll_program_standard_set_down(&data.program); - return (F_status_is_error(data.setting.state.status) || data.setting.state.status == F_false) ? 1 : 0; + if (F_status_is_error(data.setting.state.status)) { + return F_status_set_fine(data.setting.state.status) == F_support_not ? 2 : 1; + } + + return data.setting.state.status == F_false ? 1 : 0; } #ifdef __cplusplus diff --git a/sources/c/program/kevux/tools/remove/rmdir/print.c b/sources/c/program/kevux/tools/remove/rmdir/print.c index 55730b4..9827be0 100644 --- a/sources/c/program/kevux/tools/remove/rmdir/print.c +++ b/sources/c/program/kevux/tools/remove/rmdir/print.c @@ -12,7 +12,7 @@ extern "C" { f_file_stream_lock(print->to); - fl_print_format("%[%QCannot remove directory '%]", print->to, print->set->error, print->prefix, print->set->error); + fl_print_format("%[%QCannot remove '%]", print->to, print->set->error, print->prefix, print->set->error); fl_print_format("%[%Q%]", print->to, print->set->notable, path, print->set->notable); fl_print_format("%[', file is not a directory.%]%r", print->to, print->set->error, print->set->error, f_string_eol_s); @@ -44,10 +44,11 @@ extern "C" { f_print_dynamic_raw(f_string_eol_s, print->to); fll_program_print_help_option(print, kt_remove_rmdir_short_parents_s, kt_remove_rmdir_long_parents_s, f_console_symbol_short_normal_s, f_console_symbol_long_normal_s, " Remove directory and the parent directories in the path provided."); - fll_program_print_help_option(print, kt_remove_short_simulate_s, kt_remove_long_simulate_s, f_console_symbol_short_normal_s, f_console_symbol_long_normal_s, " Simulate removal rather than actually rmdiring."); + fll_program_print_help_option(print, kt_remove_short_simulate_s, kt_remove_long_simulate_s, f_console_symbol_short_normal_s, f_console_symbol_long_normal_s, " Simulate removal rather than actually removing."); fll_program_print_help_option_long(print, kt_remove_rmdir_long_ignore_empty_not_s, f_console_symbol_long_normal_s, "Ignore directories that are not empty rather than fail on error."); - fll_program_print_help_option_long(print, f_console_standard_long_version_s, f_console_symbol_long_normal_s, " Print this help message."); + fll_program_print_help_option_long(print, f_console_standard_long_verbose_s, f_console_symbol_long_normal_s, " Increase verbosity beyond normal, printing more details."); + fll_program_print_help_option_long(print, f_console_standard_long_version_s, f_console_symbol_long_normal_s, " Print only the program version number and immediately exit."); f_print_dynamic_raw(f_string_eol_s, print->to); diff --git a/sources/c/program/kevux/tools/remove/rmdir/rmdir.c b/sources/c/program/kevux/tools/remove/rmdir/rmdir.c index e322035..d01dba4 100644 --- a/sources/c/program/kevux/tools/remove/rmdir/rmdir.c +++ b/sources/c/program/kevux/tools/remove/rmdir/rmdir.c @@ -26,88 +26,8 @@ extern "C" { if (!main) return; - main->setting.flag &= ~kt_remove_main_flag_option_used_d; - - main->setting.state.step_small = kt_remove_allocation_console_d; - - f_console_parameter_process(arguments, &main->program.parameters, &main->setting.state, 0); - - main->setting.state.step_small = kt_remove_allocation_small_d; - - if (F_status_is_error(main->setting.state.status)) { - kt_remove_print_error(&main->program.error, macro_kt_remove_f(f_console_parameter_process)); - - return; - } - - { - f_uint16s_t choices = f_uint16s_t_initialize; - - // Identify and prioritize "color context" parameters. - { - uint16_t choices_array[3] = { f_console_standard_parameter_no_color_e, f_console_standard_parameter_light_e, f_console_standard_parameter_dark_e }; - choices.array = choices_array; - choices.used = 3; - - const uint8_t modes[3] = { f_color_mode_not_e, f_color_mode_light_e, f_color_mode_dark_e }; - - main->setting.state.status = fll_program_parameter_process_context(choices, modes, F_true, &main->program); - - if (F_status_is_error(main->setting.state.status)) { - kt_remove_print_error(&main->program.error, macro_kt_remove_f(fll_program_parameter_process_context)); - - return; - } - } - - // Identify and prioritize "verbosity" parameters. - { - if (main->program.parameters.array[kt_remove_rmdir_parameter_verbose_alt_e].result & f_console_result_found_e) { - main->program.warning.verbosity - = main->program.error.verbosity - = main->program.output.verbosity - = main->program.message.verbosity - = f_console_verbosity_verbose_e; - } - else { - uint16_t choices_array[5] = { f_console_standard_parameter_verbosity_quiet_e, f_console_standard_parameter_verbosity_error_e, f_console_standard_parameter_verbosity_verbose_e, f_console_standard_parameter_verbosity_debug_e, f_console_standard_parameter_verbosity_normal_e }; - choices.array = choices_array; - choices.used = 5; - - const uint8_t verbosity[5] = { f_console_verbosity_quiet_e, f_console_verbosity_error_e, f_console_verbosity_verbose_e, f_console_verbosity_debug_e, f_console_verbosity_normal_e }; - - main->setting.state.status = fll_program_parameter_process_verbosity(choices, verbosity, F_true, &main->program); - - if (F_status_is_error(main->setting.state.status)) { - kt_remove_print_error(&main->program.error, macro_kt_remove_f(fll_program_parameter_process_verbosity)); - - return; - } - } - } - } - - f_number_unsigned_t i = 0; - f_number_unsigned_t index = 0; - f_number_unsigned_t index2 = 0; - f_number_unsigned_t total_locations = 0; - f_number_unsigned_t total_arguments = 0; - - uint8_t j = 0; - - main->setting.flag &= ~kt_remove_main_flag_version_copyright_help_d; - - if (main->program.parameters.array[f_console_standard_parameter_help_e].result & f_console_result_found_e) { - main->setting.flag |= kt_remove_main_flag_help_d; - } - - if ((main->program.parameters.array[f_console_standard_parameter_version_e].result & f_console_result_found_e) || (main->program.parameters.array[kt_remove_rmdir_parameter_version_alt_e].result & f_console_result_found_e)) { - main->setting.flag |= kt_remove_main_flag_version_d; - } - - if (main->program.parameters.array[f_console_standard_parameter_copyright_e].result & f_console_result_found_e) { - main->setting.flag |= kt_remove_main_flag_copyright_d; - } + kt_remove_setting_load_standard(arguments, main); + if (F_status_is_error(main->setting.state.status)) return; if (main->program.parameters.array[kt_remove_rmdir_parameter_ignore_empty_not_e].result & f_console_result_found_e) { main->setting.flag |= kt_remove_main_flag_empty_only_d; @@ -121,6 +41,18 @@ extern "C" { main->setting.flag |= kt_remove_main_flag_simulate_d; } + if (main->program.parameters.array[kt_remove_rmdir_parameter_verbose_alt_e].result & f_console_result_found_e) { + main->program.warning.verbosity + = main->program.error.verbosity + = main->program.output.verbosity + = main->program.message.verbosity + = f_console_verbosity_verbose_e; + } + + if (main->program.parameters.array[kt_remove_rmdir_parameter_version_alt_e].result & f_console_result_found_e) { + main->setting.flag |= kt_remove_main_flag_version_d; + } + // Load all remaining files as static strings (setting size to 0). if (main->program.parameters.remaining.used) { main->setting.state.status = f_memory_array_increase_by(main->program.parameters.remaining.used, sizeof(f_string_dynamic_t), (void **) &main->setting.files.array, &main->setting.files.used, &main->setting.files.size); @@ -131,7 +63,9 @@ extern "C" { return; } - for (i = 0; i < main->program.parameters.remaining.used; ++i, ++main->setting.files.used) { + f_number_unsigned_t index = 0; + + for (f_number_unsigned_t i = 0; i < main->program.parameters.remaining.used; ++i, ++main->setting.files.used) { index = main->program.parameters.remaining.array[i]; diff --git a/sources/c/program/kevux/tools/remove/rmdir/rmdir.h b/sources/c/program/kevux/tools/remove/rmdir/rmdir.h index 7878b47..ee2264f 100644 --- a/sources/c/program/kevux/tools/remove/rmdir/rmdir.h +++ b/sources/c/program/kevux/tools/remove/rmdir/rmdir.h @@ -80,13 +80,9 @@ extern "C" { * * F_parameter (with error bit) on parameter error. * - * Errors (with error bit) from: f_console_parameter_process(). - * Errors (with error bit) from: f_file_stream_open(). - * Errors (with error bit) from: f_memory_array_increase_by(). + * Errors (with error bit) from: kt_remove_setting_load_standard(). * - * @see f_console_parameter_process() - * @see f_file_stream_open() - * @see f_memory_array_increase_by() + * @see kt_remove_setting_load_standard() */ #ifndef _di_kt_remove_rmdir_setting_load_ extern void kt_remove_rmdir_setting_load(const f_console_arguments_t arguments, kt_remove_main_t * const main); diff --git a/sources/c/program/kevux/tools/remove/rmdir/string.h b/sources/c/program/kevux/tools/remove/rmdir/string.h index 492c8c0..c8fe707 100644 --- a/sources/c/program/kevux/tools/remove/rmdir/string.h +++ b/sources/c/program/kevux/tools/remove/rmdir/string.h @@ -29,8 +29,6 @@ extern "C" { /** * Additional strings used for various purposes for the rmdir program. - * - * kt_remove_rmdir_parameter_*_s: Parameter related strings. */ #ifndef _di_kt_remove_rmdir_parameter_s_ #define KT_REMOVE_rmdir_short_parents_s "p" diff --git a/sources/c/program/kevux/tools/remove/unlink/main.c b/sources/c/program/kevux/tools/remove/unlink/main.c index 9464481..1c0d0c0 100644 --- a/sources/c/program/kevux/tools/remove/unlink/main.c +++ b/sources/c/program/kevux/tools/remove/unlink/main.c @@ -85,7 +85,11 @@ int main(const int argc, const f_string_t *argv, const f_string_t *envp) { fll_program_standard_set_down(&data.program); - return (F_status_is_error(data.setting.state.status) || data.setting.state.status == F_false) ? 1 : 0; + if (F_status_is_error(data.setting.state.status)) { + return F_status_set_fine(data.setting.state.status) == F_support_not ? 2 : 1; + } + + return data.setting.state.status == F_false ? 1 : 0; } #ifdef __cplusplus diff --git a/sources/c/program/kevux/tools/remove/unlink/unlink.c b/sources/c/program/kevux/tools/remove/unlink/unlink.c index c323e4d..5a90dd1 100644 --- a/sources/c/program/kevux/tools/remove/unlink/unlink.c +++ b/sources/c/program/kevux/tools/remove/unlink/unlink.c @@ -26,79 +26,8 @@ extern "C" { if (!main) return; - main->setting.flag &= ~kt_remove_main_flag_option_used_d; - - main->setting.state.step_small = kt_remove_allocation_console_d; - - f_console_parameter_process(arguments, &main->program.parameters, &main->setting.state, 0); - - main->setting.state.step_small = kt_remove_allocation_small_d; - - if (F_status_is_error(main->setting.state.status)) { - kt_remove_print_error(&main->program.error, macro_kt_remove_f(f_console_parameter_process)); - - return; - } - - { - f_uint16s_t choices = f_uint16s_t_initialize; - - // Identify and prioritize "color context" parameters. - { - uint16_t choices_array[3] = { f_console_standard_parameter_no_color_e, f_console_standard_parameter_light_e, f_console_standard_parameter_dark_e }; - choices.array = choices_array; - choices.used = 3; - - const uint8_t modes[3] = { f_color_mode_not_e, f_color_mode_light_e, f_color_mode_dark_e }; - - main->setting.state.status = fll_program_parameter_process_context(choices, modes, F_true, &main->program); - - if (F_status_is_error(main->setting.state.status)) { - kt_remove_print_error(&main->program.error, macro_kt_remove_f(fll_program_parameter_process_context)); - - return; - } - } - - // Identify and prioritize "verbosity" parameters. - { - uint16_t choices_array[5] = { f_console_standard_parameter_verbosity_quiet_e, f_console_standard_parameter_verbosity_error_e, f_console_standard_parameter_verbosity_verbose_e, f_console_standard_parameter_verbosity_debug_e, f_console_standard_parameter_verbosity_normal_e }; - choices.array = choices_array; - choices.used = 5; - - const uint8_t verbosity[5] = { f_console_verbosity_quiet_e, f_console_verbosity_error_e, f_console_verbosity_verbose_e, f_console_verbosity_debug_e, f_console_verbosity_normal_e }; - - main->setting.state.status = fll_program_parameter_process_verbosity(choices, verbosity, F_true, &main->program); - - if (F_status_is_error(main->setting.state.status)) { - kt_remove_print_error(&main->program.error, macro_kt_remove_f(fll_program_parameter_process_verbosity)); - - return; - } - } - } - - f_number_unsigned_t i = 0; - f_number_unsigned_t index = 0; - f_number_unsigned_t index2 = 0; - f_number_unsigned_t total_locations = 0; - f_number_unsigned_t total_arguments = 0; - - uint8_t j = 0; - - main->setting.flag &= ~kt_remove_main_flag_version_copyright_help_d; - - if (main->program.parameters.array[f_console_standard_parameter_help_e].result & f_console_result_found_e) { - main->setting.flag |= kt_remove_main_flag_help_d; - } - - if ((main->program.parameters.array[f_console_standard_parameter_version_e].result & f_console_result_found_e) || (main->program.parameters.array[kt_remove_unlink_parameter_version_alt_e].result & f_console_result_found_e)) { - main->setting.flag |= kt_remove_main_flag_version_d; - } - - if (main->program.parameters.array[f_console_standard_parameter_copyright_e].result & f_console_result_found_e) { - main->setting.flag |= kt_remove_main_flag_copyright_d; - } + kt_remove_setting_load_standard(arguments, main); + if (F_status_is_error(main->setting.state.status)) return; if (main->program.parameters.array[kt_remove_unlink_parameter_simulate_e].result & f_console_result_found_e) { main->setting.flag |= kt_remove_main_flag_simulate_d; @@ -114,7 +43,9 @@ extern "C" { return; } - for (i = 0; i < main->program.parameters.remaining.used; ++i, ++main->setting.files.used) { + f_number_unsigned_t index = 0; + + for (f_number_unsigned_t i = 0; i < main->program.parameters.remaining.used; ++i, ++main->setting.files.used) { index = main->program.parameters.remaining.array[i]; diff --git a/sources/c/program/kevux/tools/remove/unlink/unlink.h b/sources/c/program/kevux/tools/remove/unlink/unlink.h index adf0c63..e87c395 100644 --- a/sources/c/program/kevux/tools/remove/unlink/unlink.h +++ b/sources/c/program/kevux/tools/remove/unlink/unlink.h @@ -80,13 +80,9 @@ extern "C" { * * F_parameter (with error bit) on parameter error. * - * Errors (with error bit) from: f_console_parameter_process(). - * Errors (with error bit) from: f_file_stream_open(). - * Errors (with error bit) from: f_memory_array_increase_by(). + * Errors (with error bit) from: kt_remove_setting_load_standard(). * - * @see f_console_parameter_process() - * @see f_file_stream_open() - * @see f_memory_array_increase_by() + * @see kt_remove_setting_load_standard() */ #ifndef _di_kt_remove_unlink_setting_load_ extern void kt_remove_unlink_setting_load(const f_console_arguments_t arguments, kt_remove_main_t * const main);