From ebd83d8e1343da2e25f41ed0c87383722e75db36 Mon Sep 17 00:00:00 2001 From: Lin Xianyi Date: Sat, 7 Feb 2026 12:12:13 +0800 Subject: [PATCH] fix: -p exits with failure if there are parse failures --- src/config/parse_config.h | 25 +++++++++++++++---------- src/mango.c | 3 +-- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/config/parse_config.h b/src/config/parse_config.h index 4d51db7..72e77bb 100644 --- a/src/config/parse_config.h +++ b/src/config/parse_config.h @@ -342,7 +342,7 @@ typedef struct { typedef int32_t (*FuncType)(const Arg *); Config config; -void parse_config_file(Config *config, const char *file_path); +bool parse_config_file(Config *config, const char *file_path); // Helper function to trim whitespace from start and end of a string void trim_whitespace(char *str) { @@ -2556,7 +2556,7 @@ bool parse_config_line(Config *config, const char *line) { return parse_option(config, key, value); } -void parse_config_file(Config *config, const char *file_path) { +bool parse_config_file(Config *config, const char *file_path) { FILE *file; char full_path[1024]; @@ -2575,7 +2575,7 @@ void parse_config_file(Config *config, const char *file_path) { fprintf(stderr, "\033[1m\033[31m[ERROR]:\033[33m HOME environment " "variable not set.\n"); - return; + return false; } snprintf(full_path, sizeof(full_path), "%s/.config/mango/%s", home, file_path + 1); @@ -2590,7 +2590,7 @@ void parse_config_file(Config *config, const char *file_path) { if (!home) { fprintf(stderr, "\033[1m\033[31m[ERROR]:\033[33m HOME environment " "variable not set.\n"); - return; + return false; } snprintf(full_path, sizeof(full_path), "%s%s", home, file_path + 1); file = fopen(full_path, "r"); @@ -2605,19 +2605,21 @@ void parse_config_file(Config *config, const char *file_path) { "\033[1;31m\033[1;33m[ERROR]:\033[0m Failed to open " "config file: %s\n", file_path); - return; + return false; } char line[512]; bool parse_correct = true; + bool parse_line_correct = true; uint32_t line_count = 0; while (fgets(line, sizeof(line), file)) { line_count++; if (line[0] == '#' || line[0] == '\n') { continue; } - parse_correct = parse_config_line(config, line); - if (!parse_correct) { + parse_line_correct = parse_config_line(config, line); + if (!parse_line_correct) { + parse_correct = false; fprintf(stderr, "\033[1;31m╰─\033[1;33m[Index]\033[0m " "\033[1;36m%s\033[0m:\033[1;35m%d\033[0m\n" @@ -2627,6 +2629,7 @@ void parse_config_file(Config *config, const char *file_path) { } fclose(file); + return parse_correct; } void free_circle_layout(Config *config) { @@ -3248,7 +3251,7 @@ void set_default_key_bindings(Config *config) { config->key_bindings_count += default_key_bindings_count; } -void parse_config(void) { +bool parse_config(void) { char filename[1024]; @@ -3301,7 +3304,7 @@ void parse_config(void) { const char *homedir = getenv("HOME"); if (!homedir) { // 如果获取失败,则无法继续 - return; + return false; } // 构建日志文件路径 snprintf(filename, sizeof(filename), "%s/.config/mango/config.conf", @@ -3315,10 +3318,12 @@ void parse_config(void) { } } + bool parse_correct = true; set_value_default(); - parse_config_file(&config, filename); + parse_correct = parse_config_file(&config, filename); set_default_key_bindings(&config); override_config(); + return parse_correct; } void reapply_monitor_rules(void) { diff --git a/src/mango.c b/src/mango.c index 94a8443..3d461e7 100644 --- a/src/mango.c +++ b/src/mango.c @@ -6123,8 +6123,7 @@ int32_t main(int32_t argc, char *argv[]) { } else if (c == 'c') { cli_config_path = optarg; } else if (c == 'p') { - parse_config(); - return EXIT_SUCCESS; + return parse_config() ? EXIT_SUCCESS : EXIT_FAILURE; } else { goto usage; }