summaryrefslogtreecommitdiffstats
path: root/ulogd
diff options
context:
space:
mode:
Diffstat (limited to 'ulogd')
-rw-r--r--ulogd/conffile.c115
-rw-r--r--ulogd/conffile.h49
2 files changed, 164 insertions, 0 deletions
diff --git a/ulogd/conffile.c b/ulogd/conffile.c
new file mode 100644
index 0000000..383384d
--- /dev/null
+++ b/ulogd/conffile.c
@@ -0,0 +1,115 @@
+/* config file parser functions
+ * (C) 2000 by Harald Welte <laforge@gnumonks.org>
+ *
+ * $Id$
+ *
+ * This code is distributed under the terms of GNU GPL */
+
+#include <stdio.h>
+#include <string.h>
+#include "conffile.h"
+
+#ifdef DEBUG_CONF
+#define DEBUGC(format, args...) fprintf(stderr, format ## args)
+#else
+#define DEBUGC
+#endif
+
+static config_entry_t *config = NULL;
+
+static char *get_word(const char *string)
+{
+ int len;
+ char *word, *space;
+ space = strchr(string, ' ');
+ if (!space)
+ return NULL;
+ len = space - string;
+ word = (char *) malloc(len+1);
+ if (!word)
+ return NULL;
+ strncpy(word, string, len);
+
+ return word;
+}
+
+int config_register_key(config_entry_t *ce)
+{
+ ce->next = config;
+ ce->hit = 0;
+ config = ce;
+ return 0;
+}
+
+int config_parse_file(const char *fname)
+{
+ FILE *cfile;
+ char *line, *word, *args;
+ config_entry_t *ce;
+
+ line = (char *) malloc(LINE_LEN+1);
+ if (!line)
+ return -ERROOM;
+
+ cfile = fopen(fname, "r");
+ if (!cfile) {
+ free(line);
+ return -ERROPEN;
+ }
+
+ while (line = fgets(line, LINE_LEN, cfile))
+ {
+ if (*line == '#')
+ continue;
+
+ word = get_word(line);
+ if (!word)
+ continue;
+
+ args = line + strlen(word) + 1;
+
+ for (ce = config; ce; ce = ce->next) {
+ if (strcmp(ce->key, word)) {
+ continue;
+ }
+ if (ce->hit && !(ce->options & CONFIG_OPT_MULTI))
+ {
+ DEBUGC("->ce-hit and option not multi!\n");
+ free(line);
+ fclose(cfile);
+ return -ERRMULT;
+ }
+ ce->hit++;
+ switch (ce->type) {
+ case CONFIG_TYPE_STRING:
+ if (strlen(args) <= ce->u.str.maxlen) {
+ strcpy(ce->u.str.string, args);
+ }
+ break;
+ case CONFIG_TYPE_INT:
+ ce->u.value = atoi(args);
+ break;
+ case CONFIG_TYPE_CALLBACK:
+ (ce->u.parser)(args);
+ break;
+ }
+ break;
+ }
+ }
+
+
+ for (ce = config; ce; ce = ce->next) {
+ if ((ce->options & CONFIG_OPT_MANDATORY) && (ce->hit == 0)) {
+ DEBUGC("mandatory config directive %s not found\n",
+ ce->key);
+ free(line);
+ fclose(cfile);
+ return -ERRMAND;
+ }
+
+ }
+
+ fclose(cfile);
+ return 0;
+}
+
diff --git a/ulogd/conffile.h b/ulogd/conffile.h
new file mode 100644
index 0000000..90fc501
--- /dev/null
+++ b/ulogd/conffile.h
@@ -0,0 +1,49 @@
+/* config file parser functions
+ * (C) 2000 by Harald Welte <laforge@gnumonks.org>
+ *
+ * $Id$
+ *
+ * This code is distributed under the terms of GNU GPL */
+
+#include <sys/types.h>
+
+/* errors returned by config functions */
+enum {
+ ERRNONE = 0,
+ ERROPEN, /* unable to open config file */
+ ERROOM, /* out of memory */
+ ERRMULT, /* non-multiple option occured more than once */
+ ERRMAND, /* mandatory option not found */
+};
+
+/* maximum line lenght of config file entries */
+#define LINE_LEN 255
+
+/* maximum lenght of config key name */
+#define CONFIG_KEY_LEN 30
+
+#define CONFIG_TYPE_INT 0x0001
+#define CONFIG_TYPE_STRING 0x0002
+#define CONFIG_TYPE_CALLBACK 0x0003
+
+#define CONFIG_OPT_MANDATORY 0x0001
+#define CONFIG_OPT_MULTI 0x0002
+
+typedef struct config_entry {
+ struct config_entry *next;
+ char key[CONFIG_KEY_LEN];
+ u_int8_t type;
+ u_int8_t options;
+ u_int8_t hit;
+ union {
+ struct {
+ char *string;
+ int maxlen;
+ } str;
+ int value;
+ int (*parser)(char *argstr);
+ } u;
+} config_entry_t;
+
+int config_parse_file(const char *fname);
+int config_register_key(config_entry_t *ce);