xref: /dragonfly/usr.sbin/config/mkoptions.c (revision 2d8a3be7)
1 /*
2  * Copyright (c) 1995  Peter Wemm
3  * Copyright (c) 1980, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by the University of
17  *	California, Berkeley and its contributors.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * @(#)mkheaders.c	8.1 (Berkeley) 6/6/93
35  * $FreeBSD: src/usr.sbin/config/mkoptions.c,v 1.17.2.3 2001/12/13 19:18:01 dillon Exp $
36  * $DragonFly: src/usr.sbin/config/mkoptions.c,v 1.3 2003/11/03 19:31:36 eirikn Exp $
37  */
38 
39 /*
40  * Make all the .h files for the optional entries
41  */
42 
43 #include <ctype.h>
44 #include <err.h>
45 #include <stdio.h>
46 #include <string.h>
47 #include <sys/param.h>
48 #include "config.h"
49 #include "y.tab.h"
50 
51 static	struct users {
52 	int	u_default;
53 	int	u_min;
54 	int	u_max;
55 } users[] = {
56 	{ 8, 2, 512 },			/* MACHINE_I386 */
57 	{ 8, 2, 512 },			/* MACHINE_PC98 */
58 	{ 8, 2, 512 },			/* MACHINE_ALPHA */
59 };
60 #define	NUSERS	(sizeof (users) / sizeof (users[0]))
61 
62 static char *lower(char *);
63 static void read_options(void);
64 static void do_option(char *);
65 static char *tooption(char *);
66 
67 void
68 options()
69 {
70 	char buf[40];
71 	struct cputype *cp;
72 	struct opt_list *ol;
73 	struct opt *op;
74 	struct users *up;
75 
76 	/* Fake the cpu types as options. */
77 	for (cp = cputype; cp != NULL; cp = cp->cpu_next) {
78 		op = (struct opt *)malloc(sizeof(*op));
79 		memset(op, 0, sizeof(*op));
80 		op->op_name = ns(cp->cpu_name);
81 		op->op_next = opt;
82 		opt = op;
83 	}
84 
85 	/* Initialize `maxusers'. */
86 	if ((unsigned)machine > NUSERS) {
87 		printf("maxusers config info isn't present, using i386\n");
88 		up = &users[MACHINE_I386 - 1];
89 	} else
90 		up = &users[machine - 1];
91 	if (maxusers == 0) {
92 		/* printf("maxusers not specified; will auto-size\n"); */
93 		/* maxusers = 0; */
94 	} else if (maxusers < up->u_min) {
95 		printf("minimum of %d maxusers assumed\n", up->u_min);
96 		maxusers = up->u_min;
97 	} else if (maxusers > up->u_max)
98 		printf("warning: maxusers > %d (%d)\n", up->u_max, maxusers);
99 
100 	/* Fake MAXUSERS as an option. */
101 	op = (struct opt *)malloc(sizeof(*op));
102 	memset(op, 0, sizeof(*op));
103 	op->op_name = "MAXUSERS";
104 	snprintf(buf, sizeof(buf), "%d", maxusers);
105 	op->op_value = ns(buf);
106 	op->op_next = opt;
107 	opt = op;
108 
109 	read_options();
110 	for (ol = otab; ol != 0; ol = ol->o_next)
111 		do_option(ol->o_name);
112 	for (op = opt; op; op = op->op_next) {
113 		if (!op->op_ownfile) {
114 			printf("%s:%d: unknown option \"%s\"\n",
115 			       PREFIX, op->op_line, op->op_name);
116 			exit(1);
117 		}
118 	}
119 }
120 
121 /*
122  * Generate an <options>.h file
123  */
124 
125 static void
126 do_option(name)
127 	char *name;
128 {
129 	char *basefile, *file, *inw;
130 	struct opt_list *ol;
131 	struct opt *op, *op_head, *topp;
132 	FILE *inf, *outf;
133 	char *value;
134 	char *oldvalue;
135 	int seen;
136 	int tidy;
137 
138 	file = tooption(name);
139 
140 	/*
141 	 * Check to see if the option was specified..
142 	 */
143 	value = NULL;
144 	for (op = opt; op; op = op->op_next) {
145 		if (eq(name, op->op_name)) {
146 			oldvalue = value;
147 			value = op->op_value;
148 			if (value == NULL)
149 				value = ns("1");
150 			if (oldvalue != NULL && !eq(value, oldvalue))
151 				printf(
152 			    "%s:%d: option \"%s\" redefined from %s to %s\n",
153 				   PREFIX, op->op_line, op->op_name, oldvalue,
154 				   value);
155 			op->op_ownfile++;
156 		}
157 	}
158 
159 	inf = fopen(file, "r");
160 	if (inf == 0) {
161 		outf = fopen(file, "w");
162 		if (outf == 0)
163 			err(1, "%s", file);
164 
165 		/* was the option in the config file? */
166 		if (value) {
167 			fprintf(outf, "#define %s %s\n", name, value);
168 		} /* else empty file */
169 
170 		(void) fclose(outf);
171 		return;
172 	}
173 	basefile = "";
174 	for (ol = otab; ol != 0; ol = ol->o_next)
175 		if (eq(name, ol->o_name)) {
176 			basefile = ol->o_file;
177 			break;
178 		}
179 	oldvalue = NULL;
180 	op_head = NULL;
181 	seen = 0;
182 	tidy = 0;
183 	for (;;) {
184 		char *cp;
185 		char *invalue;
186 
187 		/* get the #define */
188 		if ((inw = get_word(inf)) == 0 || inw == (char *)EOF)
189 			break;
190 		/* get the option name */
191 		if ((inw = get_word(inf)) == 0 || inw == (char *)EOF)
192 			break;
193 		inw = ns(inw);
194 		/* get the option value */
195 		if ((cp = get_word(inf)) == 0 || cp == (char *)EOF)
196 			break;
197 		/* option value */
198 		invalue = ns(cp); /* malloced */
199 		if (eq(inw, name)) {
200 			oldvalue = invalue;
201 			invalue = value;
202 			seen++;
203 		}
204 		for (ol = otab; ol != 0; ol = ol->o_next)
205 			if (eq(inw, ol->o_name))
206 				break;
207 		if (!eq(inw, name) && !ol) {
208 			printf("WARNING: unknown option `%s' removed from %s\n",
209 				inw, file);
210 			tidy++;
211 		} else if (ol != NULL && !eq(basefile, ol->o_file)) {
212 			printf("WARNING: option `%s' moved from %s to %s\n",
213 				inw, basefile, ol->o_file);
214 			tidy++;
215 		} else {
216 			op = (struct opt *) malloc(sizeof *op);
217 			bzero(op, sizeof(*op));
218 			op->op_name = inw;
219 			op->op_value = invalue;
220 			op->op_next = op_head;
221 			op_head = op;
222 		}
223 
224 		/* EOL? */
225 		cp = get_word(inf);
226 		if (cp == (char *)EOF)
227 			break;
228 	}
229 	(void) fclose(inf);
230 	if (!tidy && ((value == NULL && oldvalue == NULL) ||
231 	    (value && oldvalue && eq(value, oldvalue)))) {
232 		for (op = op_head; op != NULL; op = topp) {
233 			topp = op->op_next;
234 			free(op->op_name);
235 			free(op->op_value);
236 			free(op);
237 		}
238 		return;
239 	}
240 
241 	if (value && !seen) {
242 		/* New option appears */
243 		op = (struct opt *) malloc(sizeof *op);
244 		bzero(op, sizeof(*op));
245 		op->op_name = ns(name);
246 		op->op_value = value ? ns(value) : NULL;
247 		op->op_next = op_head;
248 		op_head = op;
249 	}
250 
251 	outf = fopen(file, "w");
252 	if (outf == 0)
253 		err(1, "%s", file);
254 	for (op = op_head; op != NULL; op = topp) {
255 		/* was the option in the config file? */
256 		if (op->op_value) {
257 			fprintf(outf, "#define %s %s\n",
258 				op->op_name, op->op_value);
259 		}
260 		topp = op->op_next;
261 		free(op->op_name);
262 		free(op->op_value);
263 		free(op);
264 	}
265 	(void) fclose(outf);
266 }
267 
268 /*
269  * Find the filename to store the option spec into.
270  */
271 static char *
272 tooption(name)
273 	char *name;
274 {
275 	static char hbuf[MAXPATHLEN];
276 	char nbuf[MAXPATHLEN];
277 	struct opt_list *po;
278 
279 	/* "cannot happen"?  the otab list should be complete.. */
280 	(void) strlcpy(nbuf, "options.h", sizeof(nbuf));
281 
282 	for (po = otab ; po != 0; po = po->o_next) {
283 		if (eq(po->o_name, name)) {
284 			strlcpy(nbuf, po->o_file, sizeof(nbuf));
285 			break;
286 		}
287 	}
288 
289 	(void) strlcpy(hbuf, path(nbuf), sizeof(hbuf));
290 	return (hbuf);
291 }
292 
293 /*
294  * read the options and options.<machine> files
295  */
296 static void
297 read_options()
298 {
299 	FILE *fp;
300 	char fname[MAXPATHLEN];
301 	char *wd, *this, *val;
302 	struct opt_list *po;
303 	int first = 1;
304 	char genopt[MAXPATHLEN];
305 
306 	otab = 0;
307 	if (ident == NULL) {
308 		printf("no ident line specified\n");
309 		exit(1);
310 	}
311 	(void) snprintf(fname, sizeof(fname), "../../conf/options");
312 openit:
313 	fp = fopen(fname, "r");
314 	if (fp == 0) {
315 		return;
316 	}
317 next:
318 	wd = get_word(fp);
319 	if (wd == (char *)EOF) {
320 		(void) fclose(fp);
321 		if (first == 1) {
322 			first++;
323 			(void) snprintf(fname, sizeof fname, "../../conf/options.%s", machinename);
324 			fp = fopen(fname, "r");
325 			if (fp != 0)
326 				goto next;
327 			(void) snprintf(fname, sizeof fname, "options.%s", machinename);
328 			goto openit;
329 		}
330 		if (first == 2) {
331 			first++;
332 			(void) snprintf(fname, sizeof fname, "options.%s", raisestr(ident));
333 			fp = fopen(fname, "r");
334 			if (fp != 0)
335 				goto next;
336 		}
337 		return;
338 	}
339 	if (wd == 0)
340 		goto next;
341 	if (wd[0] == '#')
342 	{
343 		while (((wd = get_word(fp)) != (char *)EOF) && wd)
344 		;
345 		goto next;
346 	}
347 	this = ns(wd);
348 	val = get_word(fp);
349 	if (val == (char *)EOF)
350 		return;
351 	if (val == 0) {
352 		char *s = ns(this);
353 		(void) snprintf(genopt, sizeof(genopt), "opt_%s.h", lower(s));
354 		val = genopt;
355 		free(s);
356 	}
357 	val = ns(val);
358 
359 	for (po = otab ; po != 0; po = po->o_next) {
360 		if (eq(po->o_name, this)) {
361 			printf("%s: Duplicate option %s.\n",
362 			       fname, this);
363 			exit(1);
364 		}
365 	}
366 
367 	po = (struct opt_list *) malloc(sizeof *po);
368 	bzero(po, sizeof(*po));
369 	po->o_name = this;
370 	po->o_file = val;
371 	po->o_next = otab;
372 	otab = po;
373 
374 	goto next;
375 }
376 
377 static char *
378 lower(str)
379 	register char *str;
380 {
381 	register char *cp = str;
382 
383 	while (*str) {
384 		if (isupper(*str))
385 			*str = tolower(*str);
386 		str++;
387 	}
388 	return (cp);
389 }
390 
391