diff options
| author | Tristan Riehs <tristan.riehs@inria.fr> | 2026-06-07 15:16:47 +0200 |
|---|---|---|
| committer | Tristan Riehs <tristan.riehs@inria.fr> | 2026-06-07 15:16:47 +0200 |
| commit | 0aea9d47490ff391ec5859dc8110041a143c8dae (patch) | |
| tree | 7be3250096d22804c98426fece786608ed100a69 | |
| parent | 9f0b0355e8836a79dc1031f18744ff1945991981 (diff) | |
Rewrite ftag_config_init
| -rw-r--r-- | src/config.c | 52 | ||||
| -rw-r--r-- | src/config.h | 2 | ||||
| -rw-r--r-- | src/main.c | 8 |
3 files changed, 36 insertions, 26 deletions
diff --git a/src/config.c b/src/config.c index 740b18e..e4151eb 100644 --- a/src/config.c +++ b/src/config.c @@ -23,9 +23,32 @@ static void ftag_config_read(enum ftag_config_e key) } } -void ftag_config_init(char *config_file) +static int ftag_config_load_file(const char *config_file) +{ + if (!(config_file && file_exists(config_file))) + return 0; + + uconfig = uconfig_new(config_file); + if (uconfig == NULL) { + fprintf(stderr, + "%s: error during configuration file parsing\n", + __func__); + exit(EXIT_FAILURE); + } + for (enum ftag_config_e key = FC_FIRST; key < FC_COUNT; key++) { + ftag_config_read(key); + } + return 1; +} + +void ftag_config_init(const char *config_file) { const char *env_home = getenv("HOME"); + char default_config_file[512]; + + /* TODO: prioritize XDG_CONFIG_HOME */ + strbuild(default_config_file, + "%s/.config/ftag/ftag.conf", env_home); /* Keys */ ftag_config_keys[FC_DATABASE_PATH] = "database_path"; @@ -41,27 +64,14 @@ void ftag_config_init(char *config_file) strcpy(ftag_config_values[FC_REMOTE_HOST], "localhost"); strcpy(ftag_config_values[FC_REMOTE_ROOT], "ftag"); - if (config_file == NULL) { - /* Use default config file */ - /* TODO: prioritize XDG_CONFIG_HOME */ - config_file = malloc(CONFIG_STR_SIZE); - strbuild_with_size(config_file, CONFIG_STR_SIZE, - "%s/.config/ftag/ftag.conf", env_home); - } - uconfig = NULL; - if (file_exists(config_file)) { - uconfig = uconfig_new(config_file); - if (uconfig == NULL) { - fprintf(stderr, - "%s: error during configuration file parsing\n", - __func__); - exit(EXIT_FAILURE); - } - for (enum ftag_config_e key = FC_FIRST; key < FC_COUNT; key++) { - ftag_config_read(key); - } - } + int config_loaded = 0; + + config_loaded = ftag_config_load_file(config_file); + if (config_loaded) + return; + + ftag_config_load_file(default_config_file); } const char *ftag_config_get(enum ftag_config_e key) diff --git a/src/config.h b/src/config.h index d8ec431..85a3872 100644 --- a/src/config.h +++ b/src/config.h @@ -11,7 +11,7 @@ enum ftag_config_e { FC_FIRST = FC_DATABASE_PATH }; -void ftag_config_init(char *config_file); +void ftag_config_init(const char *config_file); const char *ftag_config_get(enum ftag_config_e key); @@ -1085,18 +1085,18 @@ int main(int argc, char *argv[]) able to only transfer the new files, maybe using rsync. */ }; const int toplevel_command_count = sizeof(toplevel_commands) / sizeof(struct ftag_command); - char* config_file = NULL; + const char *config_file = NULL; const char *optstring = "c:h"; int opt; while ((opt = getopt(argc, argv, optstring)) != -1) { switch (opt) { - case 'h': - ftag_help(0, NULL); - exit(EXIT_SUCCESS); case 'c': config_file = optarg; break; + case 'h': + ftag_help(0, NULL); + exit(EXIT_SUCCESS); case'?': default: ftag_help(0, NULL); |
