mkheaders.c revision 71866
1/*
2 * Copyright (c) 1980, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35#if 0
36static char sccsid[] = "@(#)mkheaders.c	8.1 (Berkeley) 6/6/93";
37#endif
38static const char rcsid[] =
39  "$FreeBSD: head/usr.sbin/config/mkheaders.c 71866 2001-01-31 08:42:35Z peter $";
40#endif /* not lint */
41
42/*
43 * Make all the .h files for the optional entries
44 */
45
46#include <ctype.h>
47#include <err.h>
48#include <stdio.h>
49#include <string.h>
50#include <sys/param.h>
51#include "config.h"
52#include "y.tab.h"
53
54static void do_header(char *, int);
55static void do_count(char *);
56static char *toheader(char *);
57static char *tomacro(char *);
58
59void
60headers(void)
61{
62	struct file_list *fl;
63	struct device *dp;
64	int match;
65
66	for (fl = ftab; fl != 0; fl = fl->f_next) {
67		if (fl->f_needs != 0) {
68			match = 0;
69			for (dp = dtab; dp != 0; dp = dp->d_next) {
70				if (eq(dp->d_name, fl->f_needs)) {
71					match++;
72					if ((dp->d_type & TYPEMASK) == DEVICE)
73						dp->d_type |= DEVDONE;
74				}
75			}
76			if (fl->f_flags & NEED_COUNT) {
77				if (match)
78printf("warning: static limits for %s are set\n", fl->f_needs);
79				do_count(fl->f_needs);
80			}
81		}
82	}
83	for (dp = dtab; dp != 0; dp = dp->d_next) {
84		if ((dp->d_type & TYPEMASK) == DEVICE) {
85			if (!(dp->d_type & DEVDONE))
86				errx(1, "Error: device \"%s\" is unknown",
87				       dp->d_name);
88		}
89	}
90}
91
92/*
93 * count all the devices of a certain type and recurse to count
94 * whatever the device is connected to
95 */
96static void
97do_count(char *dev)
98{
99	struct device *dp;
100	int count, hicount;
101
102	/*
103	 * After this loop, "count" will be the actual number of units,
104	 * and "hicount" will be the highest unit declared.  do_header()
105	 * must use this higher of these values.
106	 */
107	for (hicount = count = 0, dp = dtab; dp != 0; dp = dp->d_next) {
108		if (eq(dp->d_name, dev)) {
109			count =
110			    dp->d_count != UNKNOWN ? dp->d_count : 1;
111			break;
112		}
113	}
114	do_header(dev, count);
115}
116
117static void
118do_header(char *dev, int count)
119{
120	char *file, *name, *inw;
121	struct file_list *fl, *fl_head, *tflp;
122	FILE *inf, *outf;
123	int inc, oldcount;
124
125	file = toheader(dev);
126	name = tomacro(dev);
127	remember(file);
128	inf = fopen(file, "r");
129	oldcount = -1;
130	if (inf == 0) {
131		outf = fopen(file, "w");
132		if (outf == 0)
133			err(1, "%s", file);
134		fprintf(outf, "#define %s %d\n", name, count);
135		(void) fclose(outf);
136		return;
137	}
138	fl_head = NULL;
139	for (;;) {
140		char *cp;
141		if ((inw = get_word(inf)) == 0 || inw == (char *)EOF)
142			break;
143		if ((inw = get_word(inf)) == 0 || inw == (char *)EOF)
144			break;
145		inw = ns(inw);
146		cp = get_word(inf);
147		if (cp == 0 || cp == (char *)EOF)
148			break;
149		inc = atoi(cp);
150		if (eq(inw, name)) {
151			oldcount = inc;
152			inc = count;
153		}
154		cp = get_word(inf);
155		if (cp == (char *)EOF)
156			break;
157		fl = (struct file_list *) malloc(sizeof *fl);
158		bzero(fl, sizeof(*fl));
159		fl->f_fn = inw;		/* malloced */
160		fl->f_type = inc;
161		fl->f_next = fl_head;
162		fl_head = fl;
163	}
164	(void) fclose(inf);
165	if (count == oldcount) {
166		for (fl = fl_head; fl != NULL; fl = tflp) {
167			tflp = fl->f_next;
168			free(fl->f_fn);
169			free(fl);
170		}
171		return;
172	}
173	if (oldcount == -1) {
174		fl = (struct file_list *) malloc(sizeof *fl);
175		bzero(fl, sizeof(*fl));
176		fl->f_fn = ns(name);
177		fl->f_type = count;
178		fl->f_next = fl_head;
179		fl_head = fl;
180	}
181	outf = fopen(file, "w");
182	if (outf == 0)
183		err(1, "%s", file);
184	for (fl = fl_head; fl != NULL; fl = tflp) {
185		fprintf(outf,
186		    "#define %s %u\n", fl->f_fn, count ? fl->f_type : 0);
187		tflp = fl->f_next;
188		free(fl->f_fn);
189		free(fl);
190	}
191	(void) fclose(outf);
192}
193
194/*
195 * convert a dev name to a .h file name
196 */
197static char *
198toheader(char *dev)
199{
200	static char hbuf[MAXPATHLEN];
201
202	snprintf(hbuf, sizeof(hbuf), "%s.h", path(dev));
203	return (hbuf);
204}
205
206/*
207 * convert a dev name to a macro name
208 */
209static char *
210tomacro(char *dev)
211{
212	static char mbuf[20];
213	char *cp;
214
215	cp = mbuf;
216	*cp++ = 'N';
217	while (*dev)
218		*cp++ = islower(*dev) ? toupper(*dev++) : *dev++;
219	*cp++ = 0;
220	return (mbuf);
221}
222