diff options
author | gramanas <anastasis.gramm2@gmail.com> | 2018-11-14 16:42:40 +0200 |
---|---|---|
committer | gramanas <anastasis.gramm2@gmail.com> | 2018-11-14 16:42:40 +0200 |
commit | 7b3eb51ec3ea3f96dc3d794e799f0d61c3e1d7be (patch) | |
tree | c12f2c690cddfc68316a201fc5596d9d5295c8c9 /src | |
parent | ca76f40ba05cb8b067a6e7b0e0cd31fa31b7dff1 (diff) | |
download | ck-7b3eb51ec3ea3f96dc3d794e799f0d61c3e1d7be.tar.gz ck-7b3eb51ec3ea3f96dc3d794e799f0d61c3e1d7be.tar.bz2 ck-7b3eb51ec3ea3f96dc3d794e799f0d61c3e1d7be.zip |
XDG_CONFIG_HOME support
Diffstat (limited to 'src')
-rw-r--r-- | src/actionparser.c | 14 | ||||
-rw-r--r-- | src/confparser.c | 42 | ||||
-rw-r--r-- | src/confparser.h | 1 |
3 files changed, 47 insertions, 10 deletions
diff --git a/src/actionparser.c b/src/actionparser.c index 5c6abf7..9ca2867 100644 --- a/src/actionparser.c +++ b/src/actionparser.c @@ -10,6 +10,7 @@ * -------------------------------------------------------------------------- */ #include "ckutil.h" #include "actionparser.h" +#include "confparser.h" #include "ckinfo.h" #include "ckerrlog.h" @@ -236,10 +237,9 @@ void free_user_opt(UserOpt *opt) { list_free(opt->args); } -/* If the used has specified a config file other - * than the default get it now */ +/* find the correct config */ int get_config(UserOpt *opt) { - /* get first token */ + /* If it's a cli option */ if (next_token()) { for (int i = 1; i < atoi(strConfDir[0]) + 1; i++) { if (strcmp(token, strConfDir[i]) == 0) { @@ -262,16 +262,12 @@ int get_config(UserOpt *opt) { return 0; } } - // rewind + /* rewind */ pos = pos - 1; token = opts[pos]; } - char defaultConf[STR_S] = ".ck"; - char * home = getenv("HOME"); - opt->confDir = malloc(strlen(defaultConf) + 1 /* '/' */ + strlen(home) + 1); - str_join_dirname_with_basename(opt->confDir, home, defaultConf); - return 0; + return find_config(opt); } int version() { diff --git a/src/confparser.c b/src/confparser.c index 019cf14..046bee7 100644 --- a/src/confparser.c +++ b/src/confparser.c @@ -14,7 +14,7 @@ ERRLOG(configfile); -const char * const CONFIG_NAME = "/ckrc"; +static const char * const CONFIG_NAME = "/ckrc"; void initialize_conf(Conf *c) { #define X(var, str, name) \ @@ -53,6 +53,46 @@ void make_config_name(char * ret, const char *confPath) { strcpy(ret, tmp); } +int find_config(UserOpt *opt) { + /* If env CK_CONFIG is set */ + char *config_home = getenv("CK_CONFIG"); + if (config_home) { + if (util_is_dir(config_home)) { + opt->confDir = strdup(config_home); + LOG("Using $CK_CONFIG: %s", config_home); + return 0; + } + } + LOG("$CK_CONFIG not avaliable.") + + /* If XDG_CONFIG_HOME exists*/ + config_home = getenv("XDG_CONFIG_HOME"); + if (config_home) { + if (util_is_dir(config_home)) { + char defaultConf[STR_S] = "ck"; + opt->confDir = malloc(strlen(config_home) + 1 /* '/' */ + strlen(defaultConf) + 1); + str_join_dirname_with_basename(opt->confDir, config_home, defaultConf); + LOG("Using $XDG_CONFIG_HOME: %s", config_home); + return 0; + } + } + LOG("$XDG_CONFIG_HOME not avaliable.") + + /* fallback to HOME/.ck */ + config_home = getenv("HOME"); + if (config_home) { + if (util_is_dir(config_home)) { + char defaultConf[STR_S] = ".ck"; + opt->confDir = malloc(strlen(config_home) + 1 /* '/' */ + strlen(defaultConf) + 1); + str_join_dirname_with_basename(opt->confDir, config_home, defaultConf); + LOG("Using $HOME: %s", config_home); + return 0; + } + } + LOG("$XDG_CONFIG_HOME not avaliable."); + return -1; +} + int config_file_parse(Conf *conf, UserOpt *opt) { LOG("Using '%s' for ck configuration directory", opt->confDir); FILE *confPtr; diff --git a/src/confparser.h b/src/confparser.h index d8013e0..11ada7f 100644 --- a/src/confparser.h +++ b/src/confparser.h @@ -43,6 +43,7 @@ struct ConfigValues { int config_file_parse(Conf *conf, UserOpt *opt); void make_config_name(char * ret, const char *confPath); +int find_config(UserOpt *opt); void initialize_conf(Conf *conf); void free_conf(Conf *conf); #endif // CONFPARSER_H |