mirror of
https://github.com/DreamMaoMao/maomaowm.git
synced 2026-03-22 05:34:18 -04:00
fix: -p exits with failure if there are parse failures
This commit is contained in:
parent
0d413a5503
commit
ebd83d8e13
2 changed files with 16 additions and 12 deletions
|
|
@ -342,7 +342,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) {
|
||||||
|
|
@ -2556,7 +2556,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];
|
||||||
|
|
||||||
|
|
@ -2575,7 +2575,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);
|
||||||
|
|
@ -2590,7 +2590,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");
|
||||||
|
|
@ -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 "
|
"\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"
|
||||||
|
|
@ -2627,6 +2629,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) {
|
||||||
|
|
@ -3248,7 +3251,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];
|
||||||
|
|
||||||
|
|
@ -3301,7 +3304,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",
|
||||||
|
|
@ -3315,10 +3318,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 reapply_monitor_rules(void) {
|
void reapply_monitor_rules(void) {
|
||||||
|
|
|
||||||
|
|
@ -6123,8 +6123,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;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue