diff --git a/config.h b/config.h index fd81573..883a6df 100644 --- a/config.h +++ b/config.h @@ -10,9 +10,9 @@ const int cfg_log_facility = LOG_CRON; const char cfg_log_ident[] = "kairos"; const int cfg_log_opt = LOG_PID | LOG_NDELAY; -const char cfg_trie_file[] = "/home/ledoian/.kairos/trie" -const char cfg_tasks_file[] = "/home/ledoian/.kairos/tasks" -const char cfg_socket[] = "/tmp/kairos.sock" +const char cfg_trie_file[] = "/home/ledoian/.kairos/trie"; +const char cfg_tasks_file[] = "/home/ledoian/.kairos/tasks"; +const char cfg_socket[] = "/tmp/kairos.sock"; const int cfg_sock_maxclients = 16; const int cfg_var_name_max_len = 16; diff --git a/rpn.c b/rpn.c new file mode 100644 index 0000000..b7c2e83 --- /dev/null +++ b/rpn.c @@ -0,0 +1,40 @@ +/* + * rpn.c: evalutor of RPN formulas + */ + +#include +#include +#include + +#include "config.h" +#include "rpn.h" +#include "trie.h" + +// Growing stack implementation +struct grow { + struct trie_retval *arr; + int64_t elems; + int64_t alloc; +} + +static struct grow *grow_init(void) { + struct grow *g = malloc(sizeof(struct grow)); + if (g == NULL) { + syslog(cfg_log_facility | LOG_ERR, "rpn: Could not initialize growing array: %m"); + return NULL; + } + g -> arr = NULL; + g -> elems = 0; + g -> alloc = 0; + return g; +} + +static bool grow_push (int64_t val, struct grow *stack) { + if (stack -> alloc <= stack -> elems) { + stack + } +} + +bool rpn_eval (char rpn[]) { + +} diff --git a/rpn.h b/rpn.h new file mode 100644 index 0000000..33d286b --- /dev/null +++ b/rpn.h @@ -0,0 +1,12 @@ +/* + * rpn.h: a header file for rpn.c + */ + +#ifndef RPN_H +#define RPN_H + +#include + +bool rpn_eval (char rpn[]); + +#endif diff --git a/trie.c b/trie.c index c4777e6..df2069d 100644 --- a/trie.c +++ b/trie.c @@ -5,6 +5,7 @@ #include #include #include +#include #include "trie.h" #include "config.h" @@ -21,11 +22,25 @@ static int indexof(char c) { return 36; } else { // Something weird happened - syslog(cfg_log_facility|LOG_ERR, "trie: weird input character: %c", c); + syslog(cfg_log_facility|LOG_NOTICE, "trie: weird character: %c", c); return -1; } } +static char charof(int i) { + //ASCII again + if (i <= 25){ + return ('A' + i); + } else if (26 <= i && i <= 35) { + return ('0' + i - 26); + } else if (i == 36) { + return '_'; + } else { + syslog(cfg_log_facility|LOG_NOTICE, "trie: weird character index: %d", i); + return '\0'; + } +} + static bool trie_set_deep(char string[], int64_t val, struct trie *vertex) { // Recursive function to find and set a value if (string[0] = '\0') { @@ -60,7 +75,7 @@ bool trie_set(char string[], int64_t val) { static struct trie_retval trie_lookup_deep(char string[], struct trie *vertex) { if (string[0] == '\0') { - return (struct trie_retval) {vertex -> val, vertex -> def}; + return (struct trie_retval) {vertex -> val, vertex -> defined}; } else { int chld_index = indexof(string[0]); if (chld_index == -1 || vertex -> children[chld_index] == NULL) { @@ -94,12 +109,31 @@ bool trie_load(void) { return false; } if(trie_set(name, val) == false) { - syslog(cfg_log_facility | LOG_WARN, "trie: setting a variable failed: %s = %d", name, val); + syslog(cfg_log_facility | LOG_WARNING, "trie: setting a variable failed: %s = %d", name, val); } } return true; } +static bool trie_dfs(char prefix[], int prefixlen, FILE *f, struct trie *vertex) { + if (vertex == NULL) {// Very special case; + return true; + } else { + bool retval = true; + if (vertex -> defined) { + prefix[prefixlen] = '\0'; + fprintf(f, "%s %d\n", prefix, vertex -> val); + } + for (int i = 0; i<37; i++) { + if (vertex -> children[i] != NULL) { + prefix[prefixlen] = charof(i); + retval &= trie_dfs(prefix, prefixlen + 1, f, vertex -> children[i]); + } + } + return retval; + } +} + bool trie_save(void) { FILE *f = fopen(cfg_trie_file, "w"); if (f == NULL) { @@ -108,5 +142,9 @@ bool trie_save(void) { } // DFS, write every found variable into f - + char prefix[cfg_var_name_max_len +1]; + prefix[cfg_var_name_max_len +1] = '\0'; + int prefixlen = 0; + + return trie_dfs(prefix, prefixlen, f, trie_base); }