fix: -p exits with failure if there are parse failures

This commit is contained in:
Lin Xianyi 2026-02-07 12:12:13 +08:00
parent 4586319bef
commit 818652d20f
2 changed files with 16 additions and 12 deletions

View file

@ -362,7 +362,7 @@ typedef struct {
typedef int32_t (*FuncType)(const Arg *); typedef int32_t (*FuncType)(const Arg *);
Config config; 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 // Helper function to trim whitespace from start and end of a string
void trim_whitespace(char *str) { void trim_whitespace(char *str) {
@ -2639,7 +2639,7 @@ bool parse_config_line(Config *config, const char *line) {
return parse_option(config, key, value); 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; FILE *file;
char full_path[1024]; char full_path[1024];
@ -2658,7 +2658,7 @@ void parse_config_file(Config *config, const char *file_path) {
fprintf(stderr, fprintf(stderr,
"\033[1m\033[31m[ERROR]:\033[33m HOME environment " "\033[1m\033[31m[ERROR]:\033[33m HOME environment "
"variable not set.\n"); "variable not set.\n");
return; return false;
} }
snprintf(full_path, sizeof(full_path), "%s/.config/mango/%s", home, snprintf(full_path, sizeof(full_path), "%s/.config/mango/%s", home,
file_path + 1); file_path + 1);
@ -2673,7 +2673,7 @@ void parse_config_file(Config *config, const char *file_path) {
if (!home) { if (!home) {
fprintf(stderr, "\033[1m\033[31m[ERROR]:\033[33m HOME environment " fprintf(stderr, "\033[1m\033[31m[ERROR]:\033[33m HOME environment "
"variable not set.\n"); "variable not set.\n");
return; return false;
} }
snprintf(full_path, sizeof(full_path), "%s%s", home, file_path + 1); snprintf(full_path, sizeof(full_path), "%s%s", home, file_path + 1);
file = fopen(full_path, "r"); file = fopen(full_path, "r");
@ -2688,19 +2688,21 @@ void parse_config_file(Config *config, const char *file_path) {
"\033[1;31m\033[1;33m[ERROR]:\033[0m Failed to open " "\033[1;31m\033[1;33m[ERROR]:\033[0m Failed to open "
"config file: %s\n", "config file: %s\n",
file_path); file_path);
return; return false;
} }
char line[512]; char line[512];
bool parse_correct = true; bool parse_correct = true;
bool parse_line_correct = true;
uint32_t line_count = 0; uint32_t line_count = 0;
while (fgets(line, sizeof(line), file)) { while (fgets(line, sizeof(line), file)) {
line_count++; line_count++;
if (line[0] == '#' || line[0] == '\n') { if (line[0] == '#' || line[0] == '\n') {
continue; continue;
} }
parse_correct = parse_config_line(config, line); parse_line_correct = parse_config_line(config, line);
if (!parse_correct) { if (!parse_line_correct) {
parse_correct = false;
fprintf(stderr, fprintf(stderr,
"\033[1;31m╰─\033[1;33m[Index]\033[0m " "\033[1;31m╰─\033[1;33m[Index]\033[0m "
"\033[1;36m%s\033[0m:\033[1;35m%d\033[0m\n" "\033[1;36m%s\033[0m:\033[1;35m%d\033[0m\n"
@ -2710,6 +2712,7 @@ void parse_config_file(Config *config, const char *file_path) {
} }
fclose(file); fclose(file);
return parse_correct;
} }
void free_circle_layout(Config *config) { void free_circle_layout(Config *config) {
@ -3369,7 +3372,7 @@ void set_default_key_bindings(Config *config) {
config->key_bindings_count += default_key_bindings_count; config->key_bindings_count += default_key_bindings_count;
} }
void parse_config(void) { bool parse_config(void) {
char filename[1024]; char filename[1024];
@ -3422,7 +3425,7 @@ void parse_config(void) {
const char *homedir = getenv("HOME"); const char *homedir = getenv("HOME");
if (!homedir) { if (!homedir) {
// 如果获取失败,则无法继续 // 如果获取失败,则无法继续
return; return false;
} }
// 构建日志文件路径 // 构建日志文件路径
snprintf(filename, sizeof(filename), "%s/.config/mango/config.conf", snprintf(filename, sizeof(filename), "%s/.config/mango/config.conf",
@ -3436,10 +3439,12 @@ void parse_config(void) {
} }
} }
bool parse_correct = true;
set_value_default(); set_value_default();
parse_config_file(&config, filename); parse_correct = parse_config_file(&config, filename);
set_default_key_bindings(&config); set_default_key_bindings(&config);
override_config(); override_config();
return parse_correct;
} }
void reset_blur_params(void) { void reset_blur_params(void) {

View file

@ -6168,8 +6168,7 @@ int32_t main(int32_t argc, char *argv[]) {
} else if (c == 'c') { } else if (c == 'c') {
cli_config_path = optarg; cli_config_path = optarg;
} else if (c == 'p') { } else if (c == 'p') {
parse_config(); return parse_config() ? EXIT_SUCCESS : EXIT_FAILURE;
return EXIT_SUCCESS;
} else { } else {
goto usage; goto usage;
} }