1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <uconfig.h>
#include "config.h"
#include "system.h"
#include "utils.h"
#define CONFIG_STR_SIZE 128
static struct uconfig_s *uconfig;
static char *ftag_config_keys[FC_COUNT];
static char ftag_config_values[FC_COUNT][CONFIG_STR_SIZE];
static void ftag_config_read(enum ftag_config_e key)
{
char *new_value = uconfig_get(uconfig, ftag_config_keys[key]);
if (new_value != NULL) {
assert(strlen(new_value) < CONFIG_STR_SIZE);
strcpy(ftag_config_values[key], new_value);
}
}
void ftag_config_init(char *config_file)
{
const char *env_home = getenv("HOME");
/* Keys */
ftag_config_keys[FC_DATABASE_PATH] = "database_path";
ftag_config_keys[FC_CACHE_DIR] = "cache_dir";
ftag_config_keys[FC_REMOTE_HOST] = "remote_host";
ftag_config_keys[FC_REMOTE_ROOT] = "remote_root";
/* Defaults */
strbuild(ftag_config_values[FC_DATABASE_PATH],
"%s/.cache/ftag/ftag.sqlite3", env_home);
strbuild(ftag_config_values[FC_CACHE_DIR],
"%s/.cache/ftag", env_home);
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);
}
}
}
const char *ftag_config_get(enum ftag_config_e key)
{
return ftag_config_values[key];
}
void ftag_config_finalize(void)
{
uconfig_destroy(uconfig);
}
|