File: config.c

package info (click to toggle)
netboot 0.8.1-4
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,728 kB
  • ctags: 4,740
  • sloc: ansic: 15,152; asm: 11,623; yacc: 2,248; makefile: 1,110; pascal: 1,108; lex: 748; sh: 233
file content (511 lines) | stat: -rw-r--r-- 11,468 bytes parent folder | download | duplicates (3)
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/*
 * config.c  -  Read configuration files
 *
 * Copyright (C) 1997,1998 Gero Kuhlmann <gero@gkminix.han.de>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#define NO_BINARY 1	/* No need to include binary support here */
#include "common.h"
#include "nblib.h"
#include "privlib.h"



/*
 * Variables private to this module
 */
static int curline;			/* current line in input file */
static int errors;			/* number of errors found */
static char *inbuf = NULL;		/* input buffer pointer */
static char *curpos = NULL;		/* current write position in buffer */
static int inbufsize = 0;		/* input buffer size */



/*
 * Put a character at the end of the input buffer
 */
static void putbuf(c)
char c;
{
#define BUFSIZE 1024

  int i;
  char *cp;

  /* Allocate new line buffer if necessary */
  if (inbuf == NULL) {
	inbuf = curpos = (char *)nbmalloc(BUFSIZE);
	inbufsize = BUFSIZE;
  }

  /* Check if we have to start with a fresh buffer */
  if (curpos == NULL)
	curpos = inbuf;

  /* Check that we don't get at the end of the input buffer */
  i = (curpos - inbuf);
  if (i >= (inbufsize - 1)) {
	cp = (char *)nbmalloc(inbufsize + BUFSIZE);
	memcpy(cp, inbuf, inbufsize);
	free(inbuf);
	inbuf = cp;
	inbufsize += BUFSIZE;
	curpos = inbuf + i;
  }

  /* Finally put new character into line buffer */
  *curpos++ = c;

#undef BUFSIZE
}



/*
 * Read a line from the setup file
 */
static char *readline(fd)
FILE *fd;
{
  int inquote = FALSE;
  int backslash = FALSE;
  int c = 0;
  char *cp;

  /* Check for EOF on input file */
  if (feof(fd))
	return(NULL);

  /* Read one line */
  while (TRUE) {
	/* Reset input line buffer write pointer */
	curpos = NULL;

	/* Read one line */
	while (TRUE) {
		if ((c = fgetc(fd)) == EOF)
			break;
		if (backslash) {
			/*
			 * If preceding character was a backslash, we need
			 * special handling
			 */
			switch (c) {
				case 'n':
					putbuf('\n');
					break;
				case 'r':
					putbuf('\r');
					break;
				case 't':
					putbuf('\t');
					break;
				case '\n':
					/* Ignore newline */
					curline++;
					break;
				default:
					putbuf(c);
					break;
			}
			backslash = FALSE;
			continue;
		}
		if (inquote) {
			/*
			 * Within quotes we handle all characters verbatim
			 * and don't do any processing except backslash which
			 * has already been taken care of
			 */
			if (inquote && c == '"')
				inquote = FALSE;
			putbuf(c);
			continue;
		}
		if (c == '#') {
			/*
			 * When encountering a comment sign, skip the rest of
			 * the line until newline
			 */
			while ((c = fgetc(fd)) != '\n' && c != EOF)
				;
			if (c == EOF)
				break;
			continue;
		}
		/*
		 * Check for end of line and special characters, and then
		 * put the character into the input buffer
		 */
		if (c == '\n') {
			curline++;
			break;
		} else if (c == '"') {
			inquote = TRUE;
			putbuf(c);
		} else if (c == '\\') {
			backslash = TRUE;
		} else
			putbuf(c);
	}
	putbuf('\0');

	/* Remove trailing whitespace */
	cp = curpos - 2;
	while (cp >= inbuf && (*cp == ' ' || *cp == '\t'))
		cp--;
	*(++cp) = '\0';

	/* Terminate reading if not an empty line */
	if (cp > inbuf) {
		/* Skip leading whitespace */
		for (cp = inbuf; *cp && (*cp == ' ' || *cp == '\t'); cp++)
			;
		return(cp);
	} else if (c == EOF)
		return(NULL);
  }
  return(NULL);
}



/*
 * Decode one line of the configuration file
 */
static void decode(params, buf)
struct paramdef *params;
char *buf;
{
  char *cp, **cpp;
  char *arg;
  int i, j;

  /* Find equal sign and isolate parameter name */
  if ((cp = strchr(buf, '=')) == NULL) {
	fprintf(stderr, "%s: [%d] invalid line\n", progname, curline);
	errors++;
	return;
  }
  arg = cp + 1;
  for (--cp; cp > buf && (*cp == ' ' || *cp == '\t'); cp--)
	;
  *(++cp) = '\0';

  /* Find parameter name in list */
  for (i = 0; params[i].name != NULL; i++)
	if (!strcmp(buf, params[i].name))
		break;
  if (params[i].name == NULL) {
	fprintf(stderr, "%s: [%d] invalid parameter\n", progname, curline);
	errors++;
	return;
  }

  /* Decode argument */
  while (*arg && (*arg == ' ' || *arg == '\t'))
	arg++;
  switch (params[i].type) {
	case par_string:
		if (arg[0] == '"') {
			j = strlen(arg);
			if (arg[j - 1] != '"') {
				fprintf(stderr, "%s: [%d] missing end quote\n",
							progname, curline);
				errors++;
			} else {
				arg[j - 1] = '\0';
				arg++;
			}
		}
		copystr(params[i].valptr.strptr, arg);
		break;
	case par_bool:
		if (!strcmp(arg, "true"))
			*(params[i].valptr.boolptr) = TRUE;
		else if (!strcmp(arg, "false"))
			*(params[i].valptr.boolptr) = FALSE;
		else {
			fprintf(stderr, "%s: [%d] invalid boolean argument\n",
							progname, curline);
			errors++;
		}
		break;
	case par_int:
		if (sscanf(arg, "%d", params[i].valptr.intptr) != 1) {
			fprintf(stderr, "%s: [%d] invalid integer argument\n",
							progname, curline);
			errors++;
		}
		break;
	case par_enum:
		if ((cpp = params[i].enumlist) == NULL) {
			fprintf(stderr, "%s: Internal error: missing enumeration definition\n",
								progname);
			exit(EXIT_INTERNAL);
		}
		/* Find argument in enumeration list */
		for (j = 1; *cpp != NULL; cpp++, j++)
			if (!strcmp(*cpp, arg))
				break;
		/* Use position number in list as value */
		if (*cpp != NULL)
			*(params[i].valptr.enumptr) = j;
		else {
			fprintf(stderr, "%s: [%d] invalid argument <%s>\n",
							progname, curline, arg);
			errors++;
		}
		break;
	case par_proc:
		if ((cp = (params[i].valptr.procptr)(params[i].name, arg)) != NULL) {
			fprintf(stderr, "%s: [%d] %s\n", progname, curline, cp);
			errors++;
		}
		break;
	case par_null:
	default:
		break;
  }
}



/*
 * Find section name in section list
 */
static struct sectdef *findsect(sectlist, name)
struct sectdef *sectlist;
const char *name;
{
  struct sectdef *cursect;
  int len;

  /* Scan through section list */
  for (cursect = sectlist; cursect->name != NULL; cursect++) {
	len = strlen(cursect->name);
	if (cursect->name[len - 1] == '*') {
		len--;
		if (!strncmp(cursect->name, name, len)) {
			if (!name[len]) {
				fprintf(stderr, "%s: [%d] missing subsection name\n",
							progname, curline);
				errors++;
			} else if (strchr(&(name[len]), ':') != NULL) {
				fprintf(stderr, "%s: [%d] too many subsection names\n",
							progname, curline);
				errors++;
			} else
				return(cursect);
		}
	} else if (!strcmp(cursect->name, name))
		return(cursect);
  }
  return(NULL);
}



/*
 * Read configuration file
 */
void readconfig(sects)
struct sectdef *sects;
{
  struct sectdef *cursect = NULL;
  char *sectname = NULL;
  char *buf, *cp;
  FILE *fd;

  /* Nothing to do if we cannot open the setup file */
  if (configname == NULL || (fd = fopen(configname, "r")) == NULL)
	return;
  curline = 0;
  errors = 0;

  /* Read all lines in input file */
  while ((buf = readline(fd)) != NULL) {
	if (*buf == '[') {
		/* Terminate previous section */
		if (cursect != NULL && cursect->endsect != NULL) {
			cp = (cursect->endsect)(sectname, cursect);
			if (cp != NULL) {
				fprintf(stderr, "%s: [%d] %s\n",
						progname, curline, cp);
				errors++;
			}
		}

		/* Clear old section name */
		if (sectname != NULL) {
			free(sectname);
			sectname = NULL;
		}

		/* Isolate section name and find section record */
		if ((cp = strchr(buf, ']')) == NULL) {
			fprintf(stderr, "%s: [%d] unterminated section name\n",
							progname, curline);
			errors++;
			cursect = NULL;
		} else {
			buf++;
			if (*(cp + 1)) {
				fprintf(stderr, "%s: [%d] junk after section name\n",
							progname, curline);
				errors++;
			}
			*cp = '\0';
			copystr(&sectname, buf);
			cursect = findsect(sects, sectname);
		}

		/* Startup new section */
		if (cursect != NULL && cursect->startsect != NULL) {
			cp = (cursect->startsect)(sectname, cursect);
			if (cp != NULL) {
				fprintf(stderr, "%s: [%d] %s\n",
						progname, curline, cp);
				errors++;
			}
		}
	} else if (sectname == NULL) {
		fprintf(stderr, "%s: [%d] definition outside of section\n",
							progname, curline);
		errors++;
	} else if (cursect != NULL)
		decode(cursect->params, buf);
  }
  if (cursect != NULL && cursect->endsect != NULL) {
	cp = (cursect->endsect)(sectname, cursect);
	if (cp != NULL) {
		fprintf(stderr, "%s: [%d] %s\n", progname, curline, cp);
		errors++;
	}
  }

  /* Free all memory and report about errors */
  if (errors > 0) {
	fprintf(stderr, "%s: found %d errors in file %s, terminating\n",
					progname, errors, configname);
	exit(EXIT_CONFIG);
  }
  if (inbuf != NULL) {
	free(inbuf);
	inbuf = NULL;
  }
  if (sectname != NULL)
	free(sectname);
  fclose(fd);
}



/*
 * Read a section from a database
 */
void readdb(sect)
struct sectdef *sect;
{
  struct sectdef *cursect = NULL;
  int foundsect = FALSE;
  char *sectname = NULL;
  char *buf, *cp;
  FILE *fd;

  /* Nothing to do if we cannot open the database file */
  if (dbname == NULL || (fd = fopen(dbname, "r")) == NULL) {
	fprintf(stderr, "%s: error opening database file\n", progname);
	exit(EXIT_NODB);
  }
  curline = 0;
  errors = 0;

  /* Read all lines in input file */
  while ((buf = readline(fd)) != NULL) {
	if (*buf == '[') {
		/* Terminate previous section */
		if (cursect != NULL && cursect->endsect != NULL)
			(cursect->endsect)(sectname, cursect);

		/* Clear old section name */
		if (sectname != NULL) {
			free(sectname);
			sectname = NULL;
		}

		/* Isolate section name and check if section found */
		cursect = NULL;
		if ((cp = strchr(buf, ']')) == NULL) {
			fprintf(stderr, "%s: [%d] unterminated section name\n",
							progname, curline);
			errors++;
		} else {
			buf++;
			if (*(cp + 1)) {
				fprintf(stderr, "%s: [%d] junk after section name\n",
							progname, curline);
				errors++;
			}
			*cp = '\0';
			copystr(&sectname, buf);
			if (!strcmp(sectname, sect->name)) {
				if (foundsect) {
					fprintf(stderr, "%s: [%d] section <%s> multiply defined\n",
						progname, curline, sectname);
					errors++;
				} else {
					cursect = sect;
					foundsect = TRUE;
				}
			}
		}

		/* Startup new section */
		if (cursect != NULL && cursect->startsect != NULL)
			(cursect->startsect)(sectname, cursect);
	} else if (sectname == NULL) {
		fprintf(stderr, "%s: [%d] definition outside of section\n",
							progname, curline);
		errors++;
	} else if (cursect != NULL)
		decode(cursect->params, buf);
  }
  if (cursect != NULL && cursect->endsect != NULL)
	(cursect->endsect)(sectname, cursect);

  /* Free all memory and report about errors */
  if (errors > 0) {
	fprintf(stderr, "%s: found %d errors in file %s, terminating\n",
					progname, errors, configname);
	exit(EXIT_DB);
  }
  if (!foundsect) {
	fprintf(stderr, "%s: section <%s> not found in database\n",
						progname, sect->name);
	exit(EXIT_NOSYS);
  }
  if (inbuf != NULL) {
	free(inbuf);
	inbuf = NULL;
  }
  if (sectname != NULL)
	free(sectname);
  fclose(fd);
}