From 1ffdc1ef38c476cdeabc928d71b3343afbcea069 Mon Sep 17 00:00:00 2001 From: DreamMaoMao <2523610504@qq.com> Date: Wed, 3 Dec 2025 16:12:05 +0800 Subject: [PATCH] feat: support -c option to specified config file --- src/config/parse_config.h | 14 ++++++++++++-- src/fetch/common.h | 7 ++++++- src/mango.c | 5 ++++- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/config/parse_config.h b/src/config/parse_config.h index ec27e76..45c4993 100644 --- a/src/config/parse_config.h +++ b/src/config/parse_config.h @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -2307,7 +2308,14 @@ void parse_config_file(Config *config, const char *file_path) { // Relative path const char *mangoconfig = getenv("MANGOCONFIG"); - if (mangoconfig && mangoconfig[0] != '\0') { + + if (cli_config_path) { + char *config_path = strdup(cli_config_path); + char *config_dir = dirname(config_path); + snprintf(full_path, sizeof(full_path), "%s/%s", config_dir, + file_path + 1); + free(config_path); + } else if (mangoconfig && mangoconfig[0] != '\0') { snprintf(full_path, sizeof(full_path), "%s/%s", mangoconfig, file_path + 1); } else { @@ -3040,7 +3048,9 @@ void parse_config(void) { const char *mangoconfig = getenv("MANGOCONFIG"); // 如果 MANGOCONFIG 环境变量不存在或为空,则使用 HOME 环境变量 - if (!mangoconfig || mangoconfig[0] == '\0') { + if (cli_config_path) { + snprintf(filename, sizeof(filename), "%s", cli_config_path); + } else if (!mangoconfig || mangoconfig[0] == '\0') { // 获取当前用户家目录 const char *homedir = getenv("HOME"); if (!homedir) { diff --git a/src/fetch/common.h b/src/fetch/common.h index c86a3fe..86d8549 100644 --- a/src/fetch/common.h +++ b/src/fetch/common.h @@ -29,7 +29,12 @@ int isdescprocess(pid_t p, pid_t c) { char *get_autostart_path(char *autostart_path, uint32_t buf_size) { const char *mangoconfig = getenv("MANGOCONFIG"); - if (mangoconfig && mangoconfig[0] != '\0') { + if (cli_config_path) { + char *config_path = strdup(cli_config_path); + char *config_dir = dirname(config_path); + snprintf(autostart_path, buf_size, "%s/autostart.sh", config_dir); + free(config_path); + } else if (mangoconfig && mangoconfig[0] != '\0') { snprintf(autostart_path, buf_size, "%s/autostart.sh", mangoconfig); } else { const char *homedir = getenv("HOME"); diff --git a/src/mango.c b/src/mango.c index f27b902..25e19ca 100644 --- a/src/mango.c +++ b/src/mango.c @@ -852,6 +852,7 @@ struct dvec2 *baked_points_focus; static struct wl_event_source *hide_source; static bool cursor_hidden = false; static bool tag_combo = false; +static const char *cli_config_path = NULL; static KeyMode keymode = { .mode = {'d', 'e', 'f', 'a', 'u', 'l', 't', '\0'}, .isdefault = true, @@ -5908,13 +5909,15 @@ int main(int argc, char *argv[]) { char *startup_cmd = NULL; int c; - while ((c = getopt(argc, argv, "s:hdv")) != -1) { + while ((c = getopt(argc, argv, "s:c:hdv")) != -1) { if (c == 's') startup_cmd = optarg; else if (c == 'd') log_level = WLR_DEBUG; else if (c == 'v') die("mango " VERSION); + else if (c == 'c') + cli_config_path = optarg; else goto usage; }