summaryrefslogtreecommitdiffstats
path: root/ulogd/conffile.c
blob: 10a537d93a7f12c648b563c1be1e559686f53b20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* config file parser functions
 * (C) 2000 by Harald Welte <laforge@gnumonks.org>
 *
 * $Id: conffile.c,v 1.3 2000/09/09 18:35:26 laforge Exp $
 * 
 * This code is distributed under the terms of GNU GPL */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "conffile.h"

#ifdef DEBUG_CONF
#define DEBUGC(format, args...) fprintf(stderr, format, ## args)
#else
#define DEBUGC(format, args...)
#endif

static config_entry_t *config = NULL;

config_entry_t *config_errce = NULL;

static char *get_word(const char *string)
{
	int len;
	char *word, *space;
	space = strchr(string, ' ');

	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;
}

/***********************************************************************
 * PUBLIC INTERFACE
 ***********************************************************************/

int config_register_key(config_entry_t *ce)
{
	config_entry_t *myentry;

	if (!ce)
		return 1;

	/* prepend our list to the global config list */
	for (myentry = ce; myentry->next; myentry = myentry->next) {
	}
	myentry->next = config;
	config = ce;

	return 0;
}

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) 
		return -ERROOM;
	
	cfile = fopen(fname, "r");
	if (!cfile) {
		free(line);
		return -ERROPEN;
	}
	
	while (fgets(line, LINE_LEN, cfile))
	{
		DEBUGC("line read\n");
		if (*line == '#')
			continue;

		word = get_word(line);
		if (!word)
			continue;

		/* if we do the final parse and word is not a config key */
		if (final && config_iskey(word)) {
			err = -ERRUNKN;
			config_errce = ce;
			goto cpf_error;
		}

		args = line + strlen(word) + 1;
		*(args + strlen(args) - 1 ) = '\0';

		for (ce = config; ce; ce = ce->next) {
			DEBUGC("parse main loop\n");
			if (strcmp(ce->key, word)) {
				continue;
			}

			if (ce->hit && !(ce->options & CONFIG_OPT_MULTI))
			{
				DEBUGC("->ce-hit and option not multi!\n");
				config_errce = ce;
				err = -ERRMULT;
				goto cpf_error;
			}
			if (final) 
				ce->hit++;
			switch (ce->type) {
				case CONFIG_TYPE_STRING:
					if (strlen(args) < 
					    CONFIG_VAL_STRING_LEN ) {
						strcpy(ce->u.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) {
		DEBUGC("ce post loop, ce=%s\n", ce->key);
		if ((ce->options & CONFIG_OPT_MANDATORY) && (ce->hit == 0)) {
			DEBUGC("mandatory config directive %s not found\n",
				ce->key);
			config_errce = ce;
			err = -ERRMAND;
			goto cpf_error;
		}

	}

cpf_error:
	free(line);
	fclose(cfile);
	return err;
}