mkoptions.c revision 16073
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
35#ifndef lint
36static char sccsid[] = "@(#)mkheaders.c	8.1 (Berkeley) 6/6/93";
37#endif /* not lint */
38
39/*
40 * Make all the .h files for the optional entries
41 */
42
43#include <stdio.h>
44#include <ctype.h>
45#include "config.h"
46#include "y.tab.h"
47
48options()
49{
50	struct opt_list *ol;
51
52	/* fake the cpu types as options */
53	/* Please forgive me for this hack.. :-) */
54	struct opt *op;
55	struct cputype *cp;
56
57	for (cp = cputype; cp; cp = cp->cpu_next) {
58		struct opt *op = (struct opt *)malloc(sizeof (struct opt));
59		memset(op, 0, sizeof(*op));
60		op->op_name = cp->cpu_name;
61		op->op_value = 0;
62		op->op_next = opt;
63		opt = op;
64	}
65
66	read_options();
67	for (ol = otab; ol != 0; ol = ol->o_next)
68		do_option(ol->o_name);
69}
70
71/*
72 * Generate an <options>.h file
73 */
74
75do_option(name)
76	char *name;
77{
78	char *file, *inw, *tooption();
79	struct opt *op, *op_head, *topp;
80	FILE *inf, *outf;
81	char *value;
82	char *oldvalue;
83	int seen;
84
85	file = tooption(name);
86
87	/*
88	 * Check to see if the option was specified..
89	 */
90	value = NULL;
91	for (op = opt; op; op = op->op_next) {
92		if (eq(name, op->op_name)) {
93			value = op->op_value;
94			if (!value)
95				value = ns("1");
96			op->op_ownfile++;
97		}
98	}
99
100	inf = fopen(file, "r");
101	if (inf == 0) {
102		outf = fopen(file, "w");
103		if (outf == 0) {
104			perror(file);
105			exit(1);
106		}
107
108		/* was the option in the config file? */
109		if (value) {
110			fprintf(outf, "#define %s %s\n", name, value);
111		} /* else empty file */
112
113		(void) fclose(outf);
114		return;
115	}
116	oldvalue = NULL;
117	op_head = NULL;
118	seen = 0;
119	for (;;) {
120		char *cp;
121		char *invalue;
122
123		/* get the #define */
124		if ((inw = get_word(inf)) == 0 || inw == (char *)EOF)
125			break;
126		/* get the option name */
127		if ((inw = get_word(inf)) == 0 || inw == (char *)EOF)
128			break;
129		inw = ns(inw);
130		cp = get_word(inf);
131		if (cp == 0 || cp == (char *)EOF)
132			break;
133		/* option value */
134		invalue = ns(cp); /* malloced */
135		if (eq(inw, name)) {
136			oldvalue = invalue;
137			invalue = value;
138			seen++;
139		}
140		op = (struct opt *) malloc(sizeof *op);
141		bzero(op, sizeof(*op));
142		op->op_name = inw;
143		op->op_value = invalue;
144		op->op_next = op_head;
145		op_head = op;
146
147		/* EOL? */
148		cp = get_word(inf);
149		if (cp == (char *)EOF)
150			break;
151	}
152	(void) fclose(inf);
153	if ((value == NULL && oldvalue == NULL) ||
154	    (value && oldvalue && eq(value,oldvalue))) {
155		for (op = op_head; op != NULL; op = topp) {
156			topp = op->op_next;
157			free(op->op_name);
158			free(op->op_value);
159			free(op);
160		}
161		return;
162	}
163
164	if (value && !seen) {
165		/* New option appears */
166		op = (struct opt *) malloc(sizeof *op);
167		bzero(op, sizeof(*op));
168		op->op_name = ns(name);
169		op->op_value = value ? ns(value) : NULL;
170		op->op_next = op_head;
171		op_head = op;
172	}
173
174	outf = fopen(file, "w");
175	if (outf == 0) {
176		perror(file);
177		exit(1);
178	}
179	for (op = op_head; op != NULL; op = topp) {
180		/* was the option in the config file? */
181		if (op->op_value) {
182			fprintf(outf, "#define %s %s\n",
183				op->op_name, op->op_value);
184		}
185		topp = op->op_next;
186		free(op->op_name);
187		free(op->op_value);
188		free(op);
189	}
190	(void) fclose(outf);
191}
192
193/*
194 * Find the filename to store the option spec into.
195 */
196char *
197tooption(name)
198	char *name;
199{
200	static char hbuf[80];
201	char nbuf[80];
202	struct opt_list *po;
203
204	/* "cannot happen"?  the otab list should be complete.. */
205	(void) strcpy(nbuf, "options.h");
206
207	for (po = otab ; po != 0; po = po->o_next) {
208		if (eq(po->o_name, name)) {
209			strcpy(nbuf, po->o_file);
210			break;
211		}
212	}
213
214	(void) strcpy(hbuf, path(nbuf));
215	return (hbuf);
216}
217
218/*
219 * read the options and options.<machine> files
220 */
221read_options()
222{
223	FILE *fp;
224	char fname[32];
225	char *wd, *this, *val;
226	struct opt_list *po;
227	int first = 1;
228	char genopt[80];
229	char *lower();
230
231	otab = 0;
232	(void) strcpy(fname, "../../conf/options");
233openit:
234	fp = fopen(fname, "r");
235	if (fp == 0) {
236		return;
237	}
238	if(ident == NULL) {
239		printf("no ident line specified\n");
240		exit(1);
241	}
242next:
243	wd = get_word(fp);
244	if (wd == (char *)EOF) {
245		(void) fclose(fp);
246		if (first == 1) {
247			(void) sprintf(fname, "options.%s", machinename);
248			first++;
249			goto openit;
250		}
251		if (first == 2) {
252			(void) sprintf(fname, "options.%s", raise(ident));
253			first++;
254			fp = fopen(fname, "r");
255			if (fp != 0)
256				goto next;
257		}
258		return;
259	}
260	if (wd == 0)
261		goto next;
262	/*************************************************\
263	* If it's a comment ignore to the end of the line *
264	\*************************************************/
265	if(wd[0] == '#')
266	{
267		while( ((wd = get_word(fp)) != (char *)EOF) && wd)
268		;
269		goto next;
270	}
271	this = ns(wd);
272	val = get_word(fp);
273	if (val == (char *)EOF)
274		return;
275	if (val == 0) {
276		char *s = ns(this);
277		(void) strcpy(genopt, "opt_");
278		(void) strcat(genopt, lower(s));
279		(void) strcat(genopt, ".h");
280		val = genopt;
281		free(s);
282	}
283	val = ns(val);
284
285	for (po = otab ; po != 0; po = po->o_next) {
286		if (eq(po->o_name, this)) {
287			printf("%s: Duplicate option %s.\n",
288			       fname, this);
289			exit(1);
290		}
291	}
292
293	po = (struct opt_list *) malloc(sizeof *po);
294	bzero(po, sizeof(*po));
295	po->o_name = this;
296	po->o_file = val;
297	po->o_next = otab;
298	otab = po;
299
300	goto next;
301}
302
303char *
304lower(str)
305	register char *str;
306{
307	register char *cp = str;
308
309	while (*str) {
310		if (isupper(*str))
311			*str = tolower(*str);
312		str++;
313	}
314	return (cp);
315}
316
317