summaryrefslogtreecommitdiffstats
path: root/libconfig/config.m
blob: b0d13a9f2e6eef7ee904962822ff30637b61efbd (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include "conf.h"
#include "config.h"
#include "nastipc.h"
#include "thread.h"

#include "BuffIO.h"
#include "ArrayData.h"
#include "DictData.h"
#include "NumData.h"
#include "StringData.h"

#include <netdb.h>
#include <stdlib.h>

RCSID("$Id: config.m,v 1.5 2001/11/09 15:54:38 shmit Exp $");

/*
 * Global variables.
 */
static DictData *root;
char config_filen[MAXPATHLEN];
struct nast_config config;

static int
config_read(const char *filename)
{
	BuffIO *confFile;

	confFile = [[[BuffIO new] init: filename] setEOL: ';'];
	if (confFile == nil) {
		fprintf(stderr, "ERROR: Couldn't load config file.");
		return -1;
	}

	root = [[DictData new] setFromBuffer: [confFile getCurOff]
			       withLength: [confFile getLength]];

	/* We've duplicated all the data in here into private storage. */
	[confFile free];

	if (root == nil) {
		fprintf(stderr, "ERROR: Couldn't parse config file.");
		return -1;
	}
	return 0;
}

static int
get_val(const char *name, void *dst, enum data_type type)
{
	id node;

	node = [[root findChild: name] getData];
	if (node == nil)
		return 0;

	switch (type) {
	case NUMBER:
		if (![node isKindOf: [NumData class]]) {
			fprintf(stderr, "Node `%s' in `%s' should be a number.",
				name, config_filen);
			return -1;
		}
		*(int *)dst = [node getNum];
		break;
	case STRING:
		if (![node isKindOf: [StringData class]]) {
			fprintf(stderr, "Node `%s' in `%s' should be a string.",
				name, config_filen);
			return -1;
		}
		*(char **)dst = [node createCStr];
		break;
	case ARRAY:
		fprintf(stderr, "ERROR: Can't load array into C yet.");
		return -1;
	case DICTIONARY:
		fprintf(stderr, "ERROR: Can't load dictionary into C yet.");
		return -1;
	default:
		fprintf(stderr, "ERROR: Don't know what to do with type `%d'.",
			type);
		return -1;
	}

	return 0;
}

/*
 * Sets configuration variables to defaults, in case they're not set
 * elsewhere.
 */
void
config_setdefaults()
{
	/* Set the defaults. */
	config.description = "Description not set in config file.";
	config.nast_dir = DATADIR;
	config.nast_sock = NASTHOLE;
	config.nast_cdb_file = "nast.cdb";
	config.mysql_host = DBHOST;
	config.mysql_user = DBUSER;
	config.mysql_pass = DBPASS;
	config.nofork_flag = 0;
	config.null_cache_timeout = 60;
	config.tcp_port = 31337;

	return;
}

/*
 * Initializes configuration variables from config file.
 */
int
config_init()
{
	snprintf(config_filen, sizeof(config_filen),
		 "%s/nastd.conf", config.nast_dir);
	if (config_read(config_filen) == -1)
		return -1;

	/*
	 * Now grab all the top-level config information.
	 */
	if (get_val("description", &config.description, STRING) == -1)
		return -1;

	if (get_val("nast_dir", &config.nast_dir, STRING) == -1)
		return -1;

	if (get_val("nast_sock", &config.nast_sock, STRING) == -1)
		return -1;

	if (get_val("nast_cdb_file", &config.nast_cdb_file, STRING) == -1)
		return -1;

	if (get_val("mysql_host", &config.mysql_host, STRING) == -1)
		return -1;

	if (get_val("mysql_user", &config.mysql_user, STRING) == -1)
		return -1;

	if (get_val("mysql_pass", &config.mysql_pass, STRING) == -1)
		return -1;

	if (get_val("null_cache_timeout", &config.null_cache_timeout,
		    NUMBER) == -1)
		return -1;

	if (get_val("tcp_port", &config.tcp_port, NUMBER) == -1)
		return -1;

	return 0;
}

void
config_delete()
{
	[root free];
}

static enum data_type
getctype(id node)
{
	if ([node isKindOf: [StringData class]])
		return STRING;
	else if ([node isKindOf: [NumData class]])
		return NUMBER;
	else if ([node isKindOf: [ArrayData class]])
		return ARRAY;
	else if ([node isKindOf: [DictData class]])
		return DICTIONARY;
	else {
		return -1;
	}
}

void *
config_find(void *root_node, const char *name, enum data_type *type)
{
	id rootNode, dstNode;

	if (root_node == NULL)
		root_node = root;

	rootNode = (id)root_node;
	if ([rootNode isKindOf: [DictData class]] == NO) {
		fprintf(stderr, "Trying to find `%s' in non-dictionary.", name);
		return NULL;
	}

	dstNode = [[rootNode findChild: name] getData];
	if (dstNode == nil)
		return NULL;
	*type = getctype(dstNode);
	return dstNode;
}

void *
config_arrayitemat(void *array, int index, enum data_type *type)
{
	id arrayNode, dstNode;

	if (array == NULL)
		return NULL;

	arrayNode = (id)array;
	if ([arrayNode isKindOf: [ArrayData class]] == NO) {
		fprintf(stderr, "Trying to index in non-array.");
		return NULL;
	}

	if (index >= [arrayNode numObjects])
		return NULL;

	dstNode = [arrayNode objectAt: index];
	if (dstNode == nil)
		return NULL;
	*type = getctype(dstNode);
	return dstNode;
}

void *
config_dictitemat(void *dict, const char *name, enum data_type *type)
{
	id dictNode, dstNode;

	if (dict == NULL)
		return NULL;

	dictNode = (id)dict;
	if ([dictNode isKindOf: [DictData class]] == NO) {
		fprintf(stderr, "Trying to find `%s' in non-dictionary.", name);
		return NULL;
	}

	dstNode = [[dictNode findChild: name] getData];
	if (dstNode == nil)
		return NULL;
	*type = getctype(dstNode);
	return dstNode;
}

void *
getdata(void *itemref, void *dst, enum data_type want_type)
{
	id item;

	if (itemref == NULL)
		return NULL;

	item = (id)itemref;
	if ([item isKindOf: [DataObject class]] == NO) {
		fprintf(stderr, "Can only deref DataObject types "
				"or sub-classes.");
		return NULL;
	}

	switch (want_type) {
	case NUMBER:
		if ([item isKindOf: [NumData class]] == NO)
			return NULL;
		*(int *)dst = [item getNum];
		break;
	case STRING:
		if ([item isKindOf: [StringData class]] == NO)
			return NULL;
		*(char **)dst = [item createCStr];
		break;
	case ARRAY:
		if ([item isKindOf: [ArrayData class]] == NO)
			return NULL;
		dst = [item getData];
		break;
	case DICTIONARY:
		if ([item isKindOf: [DictData class]] == NO)
			return NULL;
		dst = [item getData];
		break;
	default:
		fprintf(stderr,
			"Can't deref type `%d': unknown type.", want_type);
		return NULL;
	}

	return dst;
}