From a2a3630e56f5441c811bd5644a166c7573b28447 Mon Sep 17 00:00:00 2001 From: Pierre Pronchery Date: Sun, 22 Nov 2015 15:52:47 +0100 Subject: [PATCH] Implement new helpers to load preferences --- config.h | 10 ++++++ include/System/config.h | 9 +++++ project.conf | 2 +- src/config.c | 79 +++++++++++++++++++++++++++++++++++++++++ src/project.conf | 3 ++ 5 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 config.h diff --git a/config.h b/config.h new file mode 100644 index 0000000..0ae9eee --- /dev/null +++ b/config.h @@ -0,0 +1,10 @@ +#define PACKAGE "libSystem" +#define VERSION "0.2.3" + +#ifndef PREFIX +# define PREFIX "/usr/local" +#endif + +#ifndef LIBDIR +# define LIBDIR PREFIX "/lib" +#endif diff --git a/include/System/config.h b/include/System/config.h index d3a0034..5c8eb36 100644 --- a/include/System/config.h +++ b/include/System/config.h @@ -47,7 +47,16 @@ void config_foreach_section(Config * config, String const * section, ConfigForeachSectionCallback callback, void * priv); int config_load(Config * config, String const * filename); + +int config_load_preferences(Config * config, String const * vendor, + String const * package, String const * filename); +int config_load_preferences_system(Config * config, String const * vendor, + String const * package, String const * filename); +int config_load_preferences_user(Config * config, String const * vendor, + String const * package, String const * filename); + int config_reset(Config * config); + int config_save(Config * config, String const * filename); #endif /* !LIBSYSTEM_SYSTEM_CONFIG_H */ diff --git a/project.conf b/project.conf index ed40ea1..f8adfa5 100644 --- a/project.conf +++ b/project.conf @@ -2,7 +2,7 @@ package=libSystem version=0.2.3 subdirs=data,doc,include,src,tests,tools -config=sh +config=h,sh dist=Makefile,COPYING,config.sh,README.md [README.md] diff --git a/src/config.c b/src/config.c index c10fe51..45838ab 100644 --- a/src/config.c +++ b/src/config.c @@ -25,8 +25,16 @@ #include "System/error.h" #include "System/mutator.h" #include "System/config.h" +#include "../config.h" /* constants */ +#ifndef PREFIX +# define PREFIX "/usr/local" +#endif +#ifndef SYSCONFDIR +# define SYSCONFDIR PREFIX "/etc" +#endif + #define CONFIG_COMMENT '#' @@ -342,6 +350,77 @@ static String * _load_value(FILE * fp) } +/* config_load_preferences */ +int config_load_preferences(Config * config, String const * vendor, + String const * package, String const * filename) +{ + int ret; + + if((ret = config_load_preferences_system(config, vendor, package, + filename)) != 0 + && ret != -ENOENT) + return ret; + if((ret = config_load_preferences_user(config, vendor, package, + filename)) != 0 + && ret != -ENOENT) + return ret; + return 0; +} + + +/* config_load_preferences_system */ +int config_load_preferences_system(Config * config, String const * vendor, + String const * package, String const * filename) +{ + int ret; + String * f; + + if(filename == NULL) + return error_set_code(-EINVAL, "%s", strerror(EINVAL)); + if(vendor != NULL && string_find(vendor, "/") != NULL) + return error_set_code(-EPERM, "%s", strerror(EPERM)); + if(package != NULL && string_find(package, "/") != NULL) + return error_set_code(-EPERM, "%s", strerror(EPERM)); + if((f = string_new_append(SYSCONFDIR, "/", + (vendor != NULL) ? vendor : "", "/", + (package != NULL) ? package : "", "/", + filename, NULL)) == NULL) + return error_get_code(); + ret = config_load(config, f); + string_delete(f); + return ret; +} + + +/* config_load_preferences_user */ +int config_load_preferences_user(Config * config, String const * vendor, + String const * package, String const * filename) +{ + int ret; + String const * homedir; + String * f; + + if(filename == NULL) + return error_set_code(-EINVAL, "%s", strerror(EINVAL)); + if(vendor != NULL && string_find(vendor, "/") != NULL) + return error_set_code(-EPERM, "%s", strerror(EPERM)); + if(package != NULL && string_find(package, "/") != NULL) + return error_set_code(-EPERM, "%s", strerror(EPERM)); + if(filename != NULL && string_find(filename, "/") != NULL) + return error_set_code(-EPERM, "%s", strerror(EPERM)); + if((homedir = getenv("HOME")) == NULL) + return error_set_code(-errno, "%s", strerror(errno)); + if((f = string_new_append(homedir, "/.config/", + (vendor != NULL) ? vendor : "", "/", + (package != NULL) ? package : "", "/", + filename, NULL)) == NULL) + return error_get_code(); + ret = config_load(config, f); + string_delete(f); + return ret; +} + + /* config_reset */ static void _delete_foreach(String const * key, void * value, void * data); static void _delete_foreach_section(String const * key, void * value, diff --git a/src/project.conf b/src/project.conf index 5e7c231..b48d3f2 100644 --- a/src/project.conf +++ b/src/project.conf @@ -13,6 +13,9 @@ sources=array.c,buffer.c,config.c,error.c,event.c,hash.c,mutator.c,object.c,pars ldflags=-ldl -lws2_32 install=$(LIBDIR) +[config.c] +depends=../config.h + [parser.c] depends=token.h