conf-file.c 6.24 KB
Newer Older
Per Cederqvist's avatar
Per Cederqvist committed
1
/*
2
 * $Id: conf-file.c,v 1.4 1994/03/05 22:15:20 ceder Exp $
Per Cederqvist's avatar
Per Cederqvist committed
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
 * Copyright (C) 1994  Lysator Academic Computer Association.
 *
 * This file is part of the LysKOM server.
 * 
 * LysKOM 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 1, or (at your option) 
 * any later version.
 * 
 * LysKOM 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 LysKOM; see the file COPYING.  If not, write to
 * Lysator, c/o ISY, Linkoping University, S-581 83 Linkoping, SWEDEN,
 * or the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
 * MA 02139, USA.
 *
 * Please mail bug reports to bug-lyskom@lysator.liu.se. 
 */
/* 
 * Read configuration files.
 */

29
static char *rcsid = "$Id: conf-file.c,v 1.4 1994/03/05 22:15:20 ceder Exp $";
Per Cederqvist's avatar
Per Cederqvist committed
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
#include "rcs.h"
USE(rcsid);

#include <stdio.h>
#include <sys/types.h>
#include <time.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>

#include "s-string.h"
#include "smalloc.h"
#include "log.h"
#include "misc-types.h"
#include "kom-types.h"
#include "conf-file.h"
#include "lyskomd.h"

#define MAXLINE 80

static int *assignment_count = NULL;
static int  npar = 0;

static void
init_init(const struct parameter *par)
{
    int ix;

Per Cederqvist's avatar
Per Cederqvist committed
59
60
61
    for (npar = 0; par[npar].name != NULL; npar++)
      ;

Per Cederqvist's avatar
Per Cederqvist committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
    assert (assignment_count == NULL);
    assignment_count = smalloc(npar * sizeof (*assignment_count));
    
    for (ix = 0; ix < npar; ix++)
	assignment_count[ix] = 0;
}

/* Check that each parameter is assigned at least as many times
   as the struct parameter says.  Assign the default value if it
   is not assigned at all. */
static void
assign_defaults(const struct parameter *par,
		int *err)
{
    int ix;

    for (ix = 0; ix < npar; ix++)
    {
	if (assignment_count[ix] < par[ix].min_assignments)
	{
Per Cederqvist's avatar
Per Cederqvist committed
82
	    log ("Parameter %s only assigned %d times (%d times needed)\n",
83
		 par[ix].name, assignment_count[ix], par[ix].min_assignments);
Per Cederqvist's avatar
Per Cederqvist committed
84
85
86
87
88
	    (*err)++;
	}
	else if (assignment_count[ix] == 0)
	{
	    if ((*par[ix].assigner)(par[ix].default_val, &par[ix]) != OK)
Per Cederqvist's avatar
Per Cederqvist committed
89
90
	    {
		log ("default assigner failed for %s\n", par[ix].name);
Per Cederqvist's avatar
Per Cederqvist committed
91
		(*err)++;
Per Cederqvist's avatar
Per Cederqvist committed
92
	    }
Per Cederqvist's avatar
Per Cederqvist committed
93
94
95
96
	}
    }
}

Per Cederqvist's avatar
Per Cederqvist committed
97
98
99
100
/* Perform the assignments on one line. 
   Returns EOF on end-of-file, 0 otherwise. 
   A log message is printed, and (*err) is incremented,
   if errors are detected. */
Per Cederqvist's avatar
Per Cederqvist committed
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
static int
configure_line(FILE *fp,
	       const struct parameter *par,
	       int *err)
{
    char line[MAXLINE];
    char *start;
    char *val;
    char *end;
    int   found;
    int   ix;

    if (fgets(line, MAXLINE, fp) == NULL)
	return EOF;

    /* Separate the line into its two parts.  Set up start to point 
       to the beginning of the variable name, val to point to the
       beginning of the value, and remove the training newline (using
       end). */
    for (start = line; *start == ' ' || *start == '\t'; start++)
	;
    switch (*start)
    {
    case '#':			/* Comment line */
	return 0;
    case '\0':			/* Too long */
Per Cederqvist's avatar
Per Cederqvist committed
127
	log ("line too long (max %d chars allowed): %s\n", MAXLINE-1, line);
Per Cederqvist's avatar
Per Cederqvist committed
128
129
130
131
132
133
134
135
136
	(*err)++;
	return 0;
    case '\n':			/* Empty line */
	return 0;
    }
    
    val = strchr(line, ':');
    if (val == NULL)
    {
Per Cederqvist's avatar
Per Cederqvist committed
137
	log("missing colon: %s\n", line);
Per Cederqvist's avatar
Per Cederqvist committed
138
139
140
141
142
143
144
145
146
	(*err)++;
	return 0;
    }
    *val = '\0';		/* NUL-terminate key */
    for (val++; *val == ' ' || *val == '\t'; val++)
	;
    switch (*val)
    {
    case '\n':
Per Cederqvist's avatar
Per Cederqvist committed
147
	log ("empty value: %s\n", line);
Per Cederqvist's avatar
Per Cederqvist committed
148
149
150
	(*err)++;
	return 0;
    case '\0':
Per Cederqvist's avatar
Per Cederqvist committed
151
	log ("line too long (max %d chars allowed): %s\n", MAXLINE-1, line);
Per Cederqvist's avatar
Per Cederqvist committed
152
153
154
155
156
157
158
	(*err)++;
	return 0;
    }

    end = strchr(val, '\n');
    if (end == NULL)
    {
Per Cederqvist's avatar
Per Cederqvist committed
159
	log ("line too long (max %d chars allowed): %s\n", MAXLINE-1, line);
Per Cederqvist's avatar
Per Cederqvist committed
160
161
162
163
164
165
166
167
168
	(*err)++;
	return 0;
    }	
    *end = '\0';

    /* Set the variable which match this line. */
    found = 0;
    for (ix = 0; ix < npar; ix++)
    {
169
	if (!strcmp(start, par[ix].name))
Per Cederqvist's avatar
Per Cederqvist committed
170
171
	{
	    found++;
Per Cederqvist's avatar
Per Cederqvist committed
172
173
	    if (assignment_count[ix] > par[ix].max_assignments
		&& par[ix].max_assignments != -1)
Per Cederqvist's avatar
Per Cederqvist committed
174
175
	    {
		(*err)++;
Per Cederqvist's avatar
Per Cederqvist committed
176
		log ("variable already assigned %d times: %s\n", 
Per Cederqvist's avatar
Per Cederqvist committed
177
178
179
180
181
182
		     assignment_count[ix], line);
	    }
	    else
	    {
		assignment_count[ix]++;
		if ((*par[ix].assigner)(val, &par[ix]) != OK)
Per Cederqvist's avatar
Per Cederqvist committed
183
184
		{
		    log ("assigner for %s failed\n", par[ix].name);
Per Cederqvist's avatar
Per Cederqvist committed
185
		    (*err)++;
Per Cederqvist's avatar
Per Cederqvist committed
186
		}
Per Cederqvist's avatar
Per Cederqvist committed
187
188
189
190
	    }
	}
    }

Per Cederqvist's avatar
Per Cederqvist committed
191
    if (found != 1)
Per Cederqvist's avatar
Per Cederqvist committed
192
    {
Per Cederqvist's avatar
Per Cederqvist committed
193
	log ("line matches %d times: %s\n", found, line);
Per Cederqvist's avatar
Per Cederqvist committed
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
	(*err)++;
	return 0;
    }
    return 0;
}

void
read_config(const char *config_file, 
	    const struct parameter *par)
{
    FILE *fp;
    int errs = 0;

    init_init(par);

    fp = fopen(config_file, "r");
    if (fp == NULL)
	restart_kom("cannot open config file %s", config_file);

    while (configure_line(fp, par, &errs) != EOF)
	;
    assign_defaults(par, &errs);

    if (errs)
	restart_kom("Please fix the above errors in %s and restart lyskomd",
		    config_file);

    sfree(assignment_count);
    assignment_count = 0;
}

Success
assign_text_no(const char *val, 
	       const struct parameter *par)
{
Per Cederqvist's avatar
Per Cederqvist committed
229
230
    if (val != NULL)
	*(Text_no*)par->value = atol(val);
Per Cederqvist's avatar
Per Cederqvist committed
231
232
233
234
235
236
237
    return OK;
}

Success
assign_conf_no(const char *val, 
	       const struct parameter *par)
{
Per Cederqvist's avatar
Per Cederqvist committed
238
239
    if (val != NULL)
	*(Conf_no*)par->value = atol(val);
Per Cederqvist's avatar
Per Cederqvist committed
240
241
242
243
244
245
246
    return OK;
}

Success
assign_int(const char *val, 
	   const struct parameter *par)
{
Per Cederqvist's avatar
Per Cederqvist committed
247
248
    if (val != NULL)
	*(int*)par->value = atol(val);
Per Cederqvist's avatar
Per Cederqvist committed
249
250
251
252
253
254
255
    return OK;
}

Success
assign_string(const char *val, 
	      const struct parameter *par)
{
Per Cederqvist's avatar
Per Cederqvist committed
256
257
258
259
260
261
262
    if (val == NULL)
	*(char**)par->value = NULL;
    else
    {
	*(char**)par->value = smalloc(strlen(val) + 1);
	strcpy(*(char**)par->value, val);
    }
Per Cederqvist's avatar
Per Cederqvist committed
263
264
265
266
267
268
269
    return OK;
}

Success
assign_bool(const char *val, 
	    const struct parameter *par)
{
Per Cederqvist's avatar
Per Cederqvist committed
270
    if (val == NULL 
271
272
273
274
	||!strcmp(val, "false") 
	|| !strcmp(val, "off")
	|| !strcmp(val, "no")
	|| !strcmp(val, "0"))
Per Cederqvist's avatar
Per Cederqvist committed
275
276
277
    {
	*(Bool*)par->value = FALSE;
    }
278
279
280
281
    else if (!strcmp(val, "true") 
	|| !strcmp(val, "on")
	|| !strcmp(val, "yes")
	|| !strcmp(val, "1"))
Per Cederqvist's avatar
Per Cederqvist committed
282
    {
Per Cederqvist's avatar
Per Cederqvist committed
283
	*(Bool*)par->value = TRUE;
Per Cederqvist's avatar
Per Cederqvist committed
284
    }
Per Cederqvist's avatar
Per Cederqvist committed
285
    else
Per Cederqvist's avatar
Per Cederqvist committed
286
    {
Per Cederqvist's avatar
Per Cederqvist committed
287
	return FAILURE;
Per Cederqvist's avatar
Per Cederqvist committed
288
    }
Per Cederqvist's avatar
Per Cederqvist committed
289
290
291

    return OK;
}