aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2019-12-10 10:14:07 -0500
committerJoseph Hunkeler <jhunkeler@gmail.com>2019-12-10 10:14:07 -0500
commit217bd310b9da9ea39e0ff41473b27b0b2fc1d5e8 (patch)
treea240980fbd82900d39d9d35e91c6565360bd89ce
parente9e16cc286ae1bd89af50d0c29e00866e1826b6f (diff)
downloadspmc-217bd310b9da9ea39e0ff41473b27b0b2fc1d5e8.tar.gz
Start implementing configuration
-rw-r--r--config.c32
-rw-r--r--spm.c9
-rw-r--r--spm.h17
3 files changed, 35 insertions, 23 deletions
diff --git a/config.c b/config.c
index c6417f6..b0bfc81 100644
--- a/config.c
+++ b/config.c
@@ -55,7 +55,7 @@ int isquoted(char *sptr) {
return 1;
}
-Config **config_read(const char *filename) {
+ConfigItem **config_read(const char *filename) {
const char sep = '=';
char line[CONFIG_BUFFER_SIZE];
FILE *fp = fopen(filename, "r");
@@ -64,7 +64,7 @@ Config **config_read(const char *filename) {
return NULL;
}
int record_initial = 2;
- Config **config = (Config **) calloc(record_initial, sizeof(Config *));
+ ConfigItem **config = (ConfigItem **) calloc(record_initial, sizeof(ConfigItem *));
int record = 0;
@@ -95,8 +95,8 @@ Config **config_read(const char *filename) {
size_t key_length = strcspn(lptr, &sep);
size_t value_length = strlen(sep_pos);
- // Allocate a Config record
- config[record] = (Config *)calloc(1, sizeof(Config));
+ // Allocate a ConfigItem record
+ config[record] = (ConfigItem *)calloc(1, sizeof(ConfigItem));
config[record]->key = (char *)calloc(key_length + 1, sizeof(char));
config[record]->value = (char *)calloc(value_length + 1, sizeof(char));
@@ -137,40 +137,40 @@ Config **config_read(const char *filename) {
// increment record count
record++;
// Expand config by another record
- config = (Config **)reallocarray(config, record + record_initial + 1, sizeof(Config *));
+ config = (ConfigItem **)reallocarray(config, record + record_initial + 1, sizeof(ConfigItem *));
}
return config;
}
-void config_free(Config **config) {
- for (int i = 0; config[i] != NULL; i++) {
- free(config[i]);
+void config_free(ConfigItem **item) {
+ for (int i = 0; item[i] != NULL; i++) {
+ free(item[i]);
}
- free(config);
+ free(item);
}
/// If the configuration contains `key` return a pointer to that record
-/// \param config pointer to array of config records
+/// \param item pointer to array of config records
/// \param key search for key in config records
/// \return success=pointer to record, failure=NULL
-Config *config_get(Config **config, const char *key) {
- for (int i = 0; config[i] != NULL; i++) {
- if (!strcmp(config[i]->key, key)) {
- return config[i];
+ConfigItem *config_get(ConfigItem **item, const char *key) {
+ for (int i = 0; item[i] != NULL; i++) {
+ if (!strcmp(item[i]->key, key)) {
+ return item[i];
}
}
return NULL;
}
void config_test(void) {
- Config **config = config_read("program.conf");
+ ConfigItem **config = config_read("program.conf");
printf("Data Parsed:\n");
for (int i = 0; config[i] != NULL; i++) {
printf("key: '%s', value: '%s'\n", config[i]->key, config[i]->value);
}
printf("Testing config_get():\n");
- Config *cptr = NULL;
+ ConfigItem *cptr = NULL;
if ((cptr = config_get(config, "integer_value"))) {
printf("%s = %d\n", cptr->key, atoi(cptr->value));
}
diff --git a/spm.c b/spm.c
index 3c18cf9..1cfc737 100644
--- a/spm.c
+++ b/spm.c
@@ -1191,6 +1191,15 @@ int install(const char *destroot, const char *_package) {
free(ucd);
}
+int init_config_global() {
+ SPM_GLOBAL.user_config_basedir = get_user_conf_dir();
+ SPM_GLOBAL.user_config_file = get_user_config_file();
+ SPM_GLOBAL.package_dir = realpath(PKG_DIR, NULL);
+ if (SPM_GLOBAL.user_config_file) {
+ config_read(SPM_GLOBAL.user_config_file);
+ }
+}
+
int main(int argc, char *argv[]) {
// not much to see here yet
// at the moment this will all be random tests, for better or worse
diff --git a/spm.h b/spm.h
index 24d2835..e7b9d4b 100644
--- a/spm.h
+++ b/spm.h
@@ -36,12 +36,19 @@
#define SHELL_BENCHMARK 1 << 2
typedef struct {
+ char *key;
+ char *value;
+} ConfigItem;
+
+typedef struct {
char *platform;
char *arch;
char *package_dir;
char *user_config_basedir;
char *user_config_file;
+ ConfigItem **config;
} spm_vars;
+static spm_vars SPM_GLOBAL;
typedef struct {
int count;
@@ -99,17 +106,13 @@ void check_runtime_environment(void);
// config.c
#define CONFIG_BUFFER_SIZE 1024
-typedef struct {
- char *key;
- char *value;
-} Config;
-
char *lstrip(char *sptr);
char *strip(char *sptr);
int isempty(char *sptr);
int isquoted(char *sptr);
-Config **config_read(const char *filename);
-void config_free(Config **config);
+ConfigItem **config_read(const char *filename);
+ConfigItem *config_get(ConfigItem **item, const char *key);
+void config_free(ConfigItem **item);
void config_test(void);
#endif //SPM_SPM_H