aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--config.c134
-rw-r--r--config.h21
2 files changed, 127 insertions, 28 deletions
diff --git a/config.c b/config.c
index 2a87da5..298310c 100644
--- a/config.c
+++ b/config.c
@@ -7,6 +7,14 @@
#include "config.h"
+struct config_item ci[] = {
+ { "device", CFG_TYPE_STR, offsetof(struct config, device) },
+ { "repeat", CFG_TYPE_BOOL, offsetof(struct config, repeat) },
+ { "show_shifted", CFG_TYPE_BOOL,
+ offsetof(struct config, show_shifted) },
+ { "time", CFG_TYPE_BOOL, offsetof(struct config, time) }
+};
+
void fix_config_file_owner(const char *config_path)
{
char *sudo_uid_str = getenv("SUDO_UID");
@@ -55,59 +63,133 @@ struct config *prepare_config_file()
return cfg;
}
-struct config *parese_config(struct config *cfg)
-{
+
+void cmp_str(char** cfg, const char* val) {
+
+}
+
+void parse_bool(const char* val, bool* member_ptr) {
+ if (strcmp(val, "true") == 0) {
+ *member_ptr = true;
+ } else {
+ *member_ptr = false;
+ }
+}
+
+void parse_str(const char* val, char* member_ptr, size_t max_len) {
+ strncpy(member_ptr, val, max_len - 1);
+ member_ptr[max_len - 1] = '\0';
+}
+
+struct config *parese_config(struct config *cfg){
if (!cfg) {
return NULL;
}
- FILE *fp = fopen(cfg->config_file_path, "r");
+ FILE* fp = fopen(cfg->config_file_path, "r");
if (!fp) {
return NULL;
}
int line_count = 0;
char line[128];
- char key[64] = { 0 };
- char val[64] = { 0 };
+ char key[128] = "";
+ char val[128] = "";
+
while (fgets(line, sizeof(line), fp)) {
line_count++;
- /* Skip comment and blank lines */
- if (line[0] == '#' || line[0] == '\n' || line[0] == '\r') {
+ if (line[0] == '#' || line[0] == '\n' || line[0] == '\r')
continue;
- }
int count = sscanf(line, "%s = %s", key, val);
- if (count != 2) {
- printf("Error at %s:%d\n invalid token: %s",
- cfg->config_file_path, line_count, line);
+ if (count < 2) {
+ fprintf(stderr, "%s:%d: Invalid config\n", cfg->config_file_path, line_count);
+ fclose(fp);
return NULL;
}
- if (CFG_COMPLETE(key, "device")) {
- strncpy(cfg->device, val, sizeof(cfg->device) - 1);
- cfg->device[sizeof(cfg->device) - 1] = '\0';
- } else if (CFG_COMPLETE(key, "time")) {
- if (CFG_COMPLETE(val, "true")) {
- cfg->time = true;
- } else {
- cfg->time = false;
- }
- } else if (CFG_COMPLETE(key, "key_counter")) {
- if (CFG_COMPLETE(val, "true")) {
- cfg->keys_counter = true;
- } else {
- cfg->keys_counter = false;
+ for (int i = 0; i < CI_SIZE; i++) {
+ if (strcmp(key, ci[i].key) == 0) {
+ if (ci[i].type == CFG_TYPE_BOOL) {
+ parse_bool(val,(bool*)((char*)cfg + ci[i].offset));
+ }
+
+ if (ci[i].type == CFG_TYPE_STR) {
+ printf("key:%s\nval:%s\n", key, val);
+ parse_str(val, (char*)cfg + ci[i].offset, sizeof(cfg->device));
+ }
+
+ break;
}
}
}
-
fclose(fp);
return cfg;
}
+
+
+/* struct config *parese_config(struct config *cfg) */
+/* { */
+/* if (!cfg) { */
+/*
+/* } */
+
+/* FILE *fp = fopen(cfg->config_file_path, "r"); */
+/* if (!fp) { */
+/* return NULL; */
+/* } */
+
+/* int line_count = 0; */
+/* char line[128]; */
+/* char key[64] = { 0 }; */
+/* char val[64] = { 0 }; */
+
+/* while (fgets(line, sizeof(line), fp)) { */
+/* line_count++; */
+/* /\* Skip comment and blank lines *\/ */
+/* if (line[0] == '#' || line[0] == '\n' || line[0] == '\r') { */
+/* continue; */
+/* } */
+
+/* int count = sscanf(line, "%s = %s", key, val); */
+/* if (count != 2) { */
+/* printf("Error at %s:%d\n invalid token: %s", */
+/* cfg->config_file_path, line_count, line); */
+/* return NULL; */
+/* } */
+
+/* if (CFG_COMPLETE(key, "device")) { */
+/* strncpy(cfg->device, val, sizeof(cfg->device) - 1); */
+/* cfg->device[sizeof(cfg) - 1] = '\0'; */
+/* } else if (CFG_COMPLETE(key, "time")) { */
+/* if (CFG_COMPLETE(val, "true")) { */
+/* cfg->time = true; */
+/* } else { */
+/* cfg->time = false; */
+/* } */
+/* } else if (CFG_COMPLETE(key, "key_counter")) { */
+/* if (CFG_COMPLETE(val, "true")) { */
+/* cfg->keys_counter = true; */
+/* } else { */
+/* cfg->keys_counter = false; */
+/* } */
+/* } else if (CFG_COMPLETE(key, "repeat")) { */
+/* if (CFG_COMPLETE(val, "true")) { */
+/* cfg->repeat = true; */
+/* } else { */
+/* cfg->repeat = false; */
+/* } */
+/* } */
+/* } */
+
+/* fclose(fp); */
+
+/* return cfg; */
+/* } */
+
struct config *config_init()
{
struct config *cfg = prepare_config_file();
diff --git a/config.h b/config.h
index dfe754e..2b1825b 100644
--- a/config.h
+++ b/config.h
@@ -3,6 +3,7 @@
#include <stdbool.h>
#include <stdlib.h>
+#include <stddef.h>
#include <string.h>
#define PROGRAM_NAME "kl"
@@ -27,12 +28,28 @@ struct config {
bool time;
bool keys_counter;
bool show_shifted;
+ bool repeat;
};
struct config *parese_config(struct config *cfg);
-
struct config *prepare_config_file();
-
struct config *config_init();
+typedef enum {
+ CFG_TYPE_BOOL,
+ CFG_TYPE_STR,
+} cfg_type;
+
+struct config_item {
+ char *key;
+ cfg_type type;
+ size_t offset;
+};
+
+extern struct config_item ci[];
+
+void* get_member(struct config_item*, size_t offset);
+
+#define CI_SIZE (int)(sizeof(ci) / sizeof(ci[0]))
+
#endif