summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorlaforge <laforge>2000-09-09 18:27:23 +0000
committerlaforge <laforge>2000-09-09 18:27:23 +0000
commit86770627e59bb2739996583d75be1ab2bec9473d (patch)
tree9fde33a16cc480522f3143a587e9ee65aea0703d
parent60b47575a2bb0cc70a77ec86f28618914724b191 (diff)
added final flag to config_parse_file
-rw-r--r--ulogd/conffile.c59
-rw-r--r--ulogd/conffile.h26
2 files changed, 64 insertions, 21 deletions
diff --git a/ulogd/conffile.c b/ulogd/conffile.c
index 383384d..8227c67 100644
--- a/ulogd/conffile.c
+++ b/ulogd/conffile.c
@@ -1,7 +1,7 @@
/* config file parser functions
* (C) 2000 by Harald Welte <laforge@gnumonks.org>
*
- * $Id$
+ * $Id: conffile.c,v 1.1 2000/09/09 08:36:05 laforge Exp $
*
* This code is distributed under the terms of GNU GPL */
@@ -22,17 +22,39 @@ static char *get_word(const char *string)
int len;
char *word, *space;
space = strchr(string, ' ');
- if (!space)
- return NULL;
+ if (!space) {
+ space = strchr(string, '\t');
+ if (!space)
+ return NULL;
+ }
len = space - string;
+ if (!len)
+ return NULL;
+
word = (char *) malloc(len+1);
if (!word)
return NULL;
+
strncpy(word, string, len);
+ if (*(word + len) == '\n')
+ *(word + len) = '\0';
+
return word;
}
+static int config_iskey(char *name)
+{
+ config_entry_t *ce;
+
+ for (ce = config; ce; ce = ce->next) {
+ if (!strcmp(name, ce->key))
+ return 0;
+ }
+
+ return 1;
+}
+
int config_register_key(config_entry_t *ce)
{
ce->next = config;
@@ -41,11 +63,12 @@ int config_register_key(config_entry_t *ce)
return 0;
}
-int config_parse_file(const char *fname)
+int config_parse_file(const char *fname, int final)
{
FILE *cfile;
char *line, *word, *args;
config_entry_t *ce;
+ int err = 0;
line = (char *) malloc(LINE_LEN+1);
if (!line)
@@ -65,6 +88,12 @@ int config_parse_file(const char *fname)
word = get_word(line);
if (!word)
continue;
+
+ if (final && !config_iskey(word)) {
+ err = -ERRUNKN;
+ config_errce = ce;
+ goto cpf_error;
+ }
args = line + strlen(word) + 1;
@@ -72,18 +101,20 @@ int config_parse_file(const char *fname)
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;
+ config_errce = ce;
+ err = -ERRMULT;
+ goto cpf_error;
}
ce->hit++;
switch (ce->type) {
case CONFIG_TYPE_STRING:
- if (strlen(args) <= ce->u.str.maxlen) {
- strcpy(ce->u.str.string, args);
+ if (strlen(args) <
+ CONFIG_VAL_STRING_LEN) {
+ strcpy(ce->u.string, args);
}
break;
case CONFIG_TYPE_INT:
@@ -102,14 +133,16 @@ int config_parse_file(const char *fname)
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;
+ config_errce = ce;
+ err = -ERRMAND;
+ goto cpf_error;
}
}
+cpf_error:
+ free(line);
fclose(cfile);
- return 0;
+ return err;
}
diff --git a/ulogd/conffile.h b/ulogd/conffile.h
index 90fc501..773c932 100644
--- a/ulogd/conffile.h
+++ b/ulogd/conffile.h
@@ -1,10 +1,13 @@
/* config file parser functions
* (C) 2000 by Harald Welte <laforge@gnumonks.org>
*
- * $Id$
+ * $Id: conffile.h,v 1.1 2000/09/09 08:36:05 laforge Exp $
*
* This code is distributed under the terms of GNU GPL */
+#ifndef _CONFFILE_H
+#define _CONFFILE_H
+
#include <sys/types.h>
/* errors returned by config functions */
@@ -14,18 +17,23 @@ enum {
ERROOM, /* out of memory */
ERRMULT, /* non-multiple option occured more than once */
ERRMAND, /* mandatory option not found */
+ ERRUNKN, /* unknown key */
};
/* maximum line lenght of config file entries */
-#define LINE_LEN 255
+#define LINE_LEN 255
/* maximum lenght of config key name */
-#define CONFIG_KEY_LEN 30
+#define CONFIG_KEY_LEN 30
+#define CONFIG_VAL_STRING_LEN 225
+
+/* valid config types */
#define CONFIG_TYPE_INT 0x0001
#define CONFIG_TYPE_STRING 0x0002
#define CONFIG_TYPE_CALLBACK 0x0003
+/* valid config options */
#define CONFIG_OPT_MANDATORY 0x0001
#define CONFIG_OPT_MULTI 0x0002
@@ -36,14 +44,16 @@ typedef struct config_entry {
u_int8_t options;
u_int8_t hit;
union {
- struct {
- char *string;
- int maxlen;
- } str;
+ char string[CONFIG_VAL_STRING_LEN];
int value;
int (*parser)(char *argstr);
} u;
} config_entry_t;
+
+/* if an error occurs, config_errce is set to the erroneous ce */
+config_entry_t *config_errce = NULL;
-int config_parse_file(const char *fname);
+int config_parse_file(const char *fname, int final);
int config_register_key(config_entry_t *ce);
+
+#endif /* ifndef _CONFFILE_H */