From d4ecd8dc1e2577f2bbc7f5090078921812a5260b Mon Sep 17 00:00:00 2001 From: Aoran Zeng Date: Thu, 21 Nov 2024 08:24:43 +0800 Subject: [PATCH] Redefine exit code --- src/chsrc-framework.h | 22 ++++++++++++---------- src/chsrc-main.c | 42 +++++++++++++++++++++--------------------- 2 files changed, 33 insertions(+), 31 deletions(-) diff --git a/src/chsrc-framework.h b/src/chsrc-framework.h index 98e2eb0..87567f0 100644 --- a/src/chsrc-framework.h +++ b/src/chsrc-framework.h @@ -52,11 +52,13 @@ bool CliOpt_NoColor = false; * 3. 最终效果本质由第三方软件决定,如 poetry 默认实现的就是项目级的换源 */ -#define Exit_UserCause 1 -#define Exit_Unsupported 2 -#define Exit_MatinerIssue 3 -#define Exit_FatalBug 4 -#define Exit_FatalUnkownError 5 +#define Exit_OK 0 +#define Exit_Fatal 1 +#define Exit_Unknown 2 +#define Exit_Unsupported 3 +#define Exit_UserCause 4 +#define Exit_MaintainerCause 5 +#define Exit_ExternalError 6 #define chsrc_log(str) xy_log(App_Name,str) #define chsrc_succ(str) xy_succ(App_Name,str) @@ -322,7 +324,7 @@ query_mirror_exist (SourceInfo *sources, size_t size, char *target, char *input) char *msg1 = CliOpt_InEnglish ? "Currently " : "当前 "; char *msg2 = CliOpt_InEnglish ? " doesn't have any source available, please contact the maintainer" : " 无任何可用源,请联系维护者"; chsrc_error (xy_strjoin (3, msg1, target, msg2)); - exit (Exit_MatinerIssue); + exit (Exit_MaintainerCause); } if (2==size) @@ -609,7 +611,7 @@ select_mirror_autoly (SourceInfo *sources, size_t size, const char *target_name) char *msg1 = CliOpt_InEnglish ? "Currently " : "当前 "; char *msg2 = CliOpt_InEnglish ? "No any source, please contact maintainers: chsrc issue" : " 无任何可用源,请联系维护者: chsrc issue"; chsrc_error (xy_strjoin (3, msg1, target_name, msg2)); - exit (Exit_MatinerIssue); + exit (Exit_MaintainerCause); } if (CliOpt_DryRun) @@ -766,9 +768,9 @@ confirm_source (SourceInfo *source) else if (source_has_empty_url (source)) { char *msg = CliOpt_InEnglish ? "URL of the source doesn't exist, please report a bug to the dev team" : \ - "该源URL不存在,请向开发团队提交bug"; + "该源URL不存在,请向维护团队提交bug"; chsrc_error (msg); - exit (Exit_FatalBug); + exit (Exit_MaintainerCause); } else { @@ -992,7 +994,7 @@ chsrc_run (const char *cmd, int run_option) { char *msg = CliOpt_InEnglish ? "Fatal error, forced end" : "关键错误,强制结束"; chsrc_error (msg); - exit (Exit_FatalUnkownError); + exit (Exit_ExternalError); } } diff --git a/src/chsrc-main.c b/src/chsrc-main.c index 8dca1a0..05b9612 100644 --- a/src/chsrc-main.c +++ b/src/chsrc-main.c @@ -584,7 +584,7 @@ main (int argc, char const *argv[]) if (0==argc) { cli_print_help (); - return 0; + return Exit_OK; } const char *command = argv[1]; @@ -648,7 +648,7 @@ main (int argc, char const *argv[]) else { char *msg = CliOpt_InEnglish ? "Unknown option: " : "未识别的命令行选项 "; - chsrc_error (xy_2strjoin (msg, argv[i])); return 1; + chsrc_error (xy_2strjoin (msg, argv[i])); return Exit_Unknown; } cli_arg_Target_pos++; cli_arg_Mirror_pos++; @@ -675,7 +675,7 @@ main (int argc, char const *argv[]) || xy_streql (command, "--help")) { cli_print_help (); - return 0; + return Exit_OK; } /* chsrc -v */ @@ -686,7 +686,7 @@ main (int argc, char const *argv[]) || xy_streql (command, "version")) { cli_print_version (); - return 0; + return Exit_OK; } /* chsrc list */ @@ -705,29 +705,29 @@ main (int argc, char const *argv[]) target = argv[cli_arg_Target_pos]; if (xy_streql (target, "mirrors") || xy_streql (target, "mirror")) { - cli_print_available_mirrors (); return 0; + cli_print_available_mirrors (); return Exit_OK; } else if (xy_streql (target, "targets") || xy_streql (target, "target")) { - cli_print_supported_targets (); return 0; + cli_print_supported_targets (); return Exit_OK; } else if (xy_streql (target, "os")) { - cli_print_supported_os (); return 0; + cli_print_supported_os (); return Exit_OK; } else if (xy_streql (target, "lang") || xy_streql (target, "pl") || xy_streql (target, "language")) { - cli_print_supported_pl(); return 0; + cli_print_supported_pl(); return Exit_OK; } else if (xy_streql (target, "ware") || xy_streql (target, "software")) { - cli_print_supported_wr (); return 0; + cli_print_supported_wr (); return Exit_OK; } matched = get_target (target, TargetOp_List_Config, NULL); if (!matched) goto not_matched; } - return 0; + return Exit_OK; } #define MSG_EN_USE_LIST_TARGETS "Use `chsrc list targets` to see all supported targets" @@ -746,13 +746,13 @@ main (int argc, char const *argv[]) char *msg = CliOpt_InEnglish ? "Please provide the target name you want to measure. " MSG_EN_USE_LIST_TARGETS : "请您提供想要测速源的目标名。" MSG_CN_USE_LIST_TARGETS; chsrc_error (msg); - return 1; + return Exit_Unknown; } ProgMode_CMD_Measure = true; target = argv[cli_arg_Target_pos]; matched = get_target (target, TargetOp_Measure_Source, NULL); if (!matched) goto not_matched; - return 0; + return Exit_OK; } @@ -765,12 +765,12 @@ main (int argc, char const *argv[]) char *msg = CliOpt_InEnglish ? "Please provide the target name you want to view the source. " MSG_EN_USE_LIST_TARGETS : "请您提供想要查看源的目标名。" MSG_CN_USE_LIST_TARGETS; chsrc_error (msg); - return 1; + return Exit_Unknown; } target = argv[cli_arg_Target_pos]; matched = get_target (target, TargetOp_Get_Source, NULL); if (!matched) goto not_matched; - return 0; + return Exit_OK; } /* chsrc set */ @@ -782,7 +782,7 @@ main (int argc, char const *argv[]) char *msg = CliOpt_InEnglish ? "Please provide the target name you want to set the source. " MSG_EN_USE_LIST_TARGETS : "请您提供想要设置源的目标名。" MSG_CN_USE_LIST_TARGETS; chsrc_error (msg); - return 1; + return Exit_Unknown } target = argv[cli_arg_Target_pos]; @@ -794,7 +794,7 @@ main (int argc, char const *argv[]) matched = get_target (target, TargetOp_Set_Source, mirrorCode_or_url); if (!matched) goto not_matched; - return 0; + return Exit_OK; } /* chsrc reset */ @@ -807,14 +807,14 @@ main (int argc, char const *argv[]) char *msg = CliOpt_InEnglish ? "Please provide the target name you want to reset the source. " MSG_EN_USE_LIST_TARGETS : "请您提供想要重置源的目标名。" MSG_CN_USE_LIST_TARGETS; chsrc_error (msg); - return 1; + return Exit_Unknown; } ProgMode_CMD_Reset = true; target = argv[cli_arg_Target_pos]; matched = get_target (target, TargetOp_Reset_Source, NULL); if (!matched) goto not_matched; - return 0; + return Exit_OK; } /* chsrc issue */ @@ -824,7 +824,7 @@ main (int argc, char const *argv[]) || xy_streql (command, "i")) { cli_print_issues (); - return 0; + return Exit_OK; } else @@ -832,7 +832,7 @@ main (int argc, char const *argv[]) char *msg1 = CliOpt_InEnglish ? "Unknown command `" : "不支持的命令 "; char *msg2 = CliOpt_InEnglish ? "`. Use `chsrc help` to view usage" : ". 请使用 chsrc help 查看使用方式"; chsrc_error (xy_strjoin (3, msg1, command, msg2)); - return 1; + return Exit_Unknown; } not_matched: @@ -841,6 +841,6 @@ not_matched: char *msg = CliOpt_InEnglish ? "Unknown target. " MSG_EN_USE_LIST_TARGETS : "暂不支持的换源目标。" MSG_CN_USE_LIST_TARGETS; chsrc_error (msg); - return 1; + return Exit_Unknown; } }