aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTristan Riehs <tristan.riehs@inria.fr>2026-06-07 15:16:47 +0200
committerTristan Riehs <tristan.riehs@inria.fr>2026-06-07 15:16:47 +0200
commit0aea9d47490ff391ec5859dc8110041a143c8dae (patch)
tree7be3250096d22804c98426fece786608ed100a69 /src
parent9f0b0355e8836a79dc1031f18744ff1945991981 (diff)
Rewrite ftag_config_init
Diffstat (limited to 'src')
-rw-r--r--src/config.c52
-rw-r--r--src/config.h2
-rw-r--r--src/main.c8
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);
diff --git a/src/main.c b/src/main.c
index a84cb4b..1b0a7d3 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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);