mkheaders.c revision 55605
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 55605 2000-01-08 15:57:22Z 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 "config.h"
51#include "y.tab.h"
52
53static void do_header __P((char *, char *, int));
54static void do_count __P((char *, char *, int));
55static char *toheader __P((char *));
56static char *tomacro __P((char *));
57
58void
59headers()
60{
61	register struct file_list *fl;
62	struct device *dp;
63
64	for (fl = ftab; fl != 0; fl = fl->f_next)
65		if (fl->f_needs != 0)
66			do_count(fl->f_needs, fl->f_needs, 1);
67	for (dp = dtab; dp != 0; dp = dp->d_next) {
68		if ((dp->d_type & TYPEMASK) == PSEUDO_DEVICE) {
69			if (!(dp->d_type & DEVDONE))
70				printf("Warning: pseudo-device \"%s\" is unknown\n",
71				       dp->d_name);
72		}
73		if ((dp->d_type & TYPEMASK) == DEVICE) {
74			if (!(dp->d_type & DEVDONE))
75				printf("Warning: device \"%s\" is unknown\n",
76				       dp->d_name);
77		}
78	}
79}
80
81/*
82 * count all the devices of a certain type and recurse to count
83 * whatever the device is connected to
84 */
85static void
86do_count(dev, hname, search)
87	register char *dev, *hname;
88	int search;
89{
90	register struct device *dp;
91	register int count, hicount;
92	char *mp;
93
94	/*
95	 * After this loop, "count" will be the actual number of units,
96	 * and "hicount" will be the highest unit declared.  do_header()
97	 * must use this higher of these values.
98	 */
99	for (dp = dtab; dp != 0; dp = dp->d_next) {
100		if (eq(dp->d_name, dev)) {
101			if ((dp->d_type & TYPEMASK) == PSEUDO_DEVICE)
102				dp->d_type |= DEVDONE;
103			else if ((dp->d_type & TYPEMASK) == DEVICE)
104				dp->d_type |= DEVDONE;
105		}
106	}
107	for (hicount = count = 0, dp = dtab; dp != 0; dp = dp->d_next) {
108		if (dp->d_unit != -1 && eq(dp->d_name, dev)) {
109			if ((dp->d_type & TYPEMASK) == PSEUDO_DEVICE) {
110				count =
111				    dp->d_count != UNKNOWN ? dp->d_count : 1;
112				break;
113			}
114			count++;
115			/*
116			 * Allow holes in unit numbering,
117			 * assumption is unit numbering starts
118			 * at zero.
119			 */
120			if (dp->d_unit + 1 > hicount)
121				hicount = dp->d_unit + 1;
122			if (search) {
123				mp = dp->d_conn;
124				if (mp != 0 && dp->d_connunit < 0)
125					mp = 0;
126				if (mp != 0 && eq(mp, "nexus"))
127					mp = 0;
128				if (mp != 0) {
129					do_count(mp, hname, 0);
130					search = 0;
131				}
132			}
133		}
134	}
135	do_header(dev, hname, count > hicount ? count : hicount);
136}
137
138static void
139do_header(dev, hname, count)
140	char *dev, *hname;
141	int count;
142{
143	char *file, *name, *inw;
144	struct file_list *fl, *fl_head, *tflp;
145	FILE *inf, *outf;
146	int inc, oldcount;
147
148	file = toheader(hname);
149	name = tomacro(dev);
150	inf = fopen(file, "r");
151	oldcount = -1;
152	if (inf == 0) {
153		outf = fopen(file, "w");
154		if (outf == 0)
155			err(1, "%s", file);
156		fprintf(outf, "#define %s %d\n", name, count);
157		(void) fclose(outf);
158		return;
159	}
160	fl_head = NULL;
161	for (;;) {
162		char *cp;
163		if ((inw = get_word(inf)) == 0 || inw == (char *)EOF)
164			break;
165		if ((inw = get_word(inf)) == 0 || inw == (char *)EOF)
166			break;
167		inw = ns(inw);
168		cp = get_word(inf);
169		if (cp == 0 || cp == (char *)EOF)
170			break;
171		inc = atoi(cp);
172		if (eq(inw, name)) {
173			oldcount = inc;
174			inc = count;
175		}
176		cp = get_word(inf);
177		if (cp == (char *)EOF)
178			break;
179		fl = (struct file_list *) malloc(sizeof *fl);
180		bzero(fl, sizeof(*fl));
181		fl->f_fn = inw;		/* malloced */
182		fl->f_type = inc;
183		fl->f_next = fl_head;
184		fl_head = fl;
185	}
186	(void) fclose(inf);
187	if (count == oldcount) {
188		for (fl = fl_head; fl != NULL; fl = tflp) {
189			tflp = fl->f_next;
190			free(fl->f_fn);
191			free(fl);
192		}
193		return;
194	}
195	if (oldcount == -1) {
196		fl = (struct file_list *) malloc(sizeof *fl);
197		bzero(fl, sizeof(*fl));
198		fl->f_fn = ns(name);
199		fl->f_type = count;
200		fl->f_next = fl_head;
201		fl_head = fl;
202	}
203	outf = fopen(file, "w");
204	if (outf == 0)
205		err(1, "%s", file);
206	for (fl = fl_head; fl != NULL; fl = tflp) {
207		fprintf(outf,
208		    "#define %s %u\n", fl->f_fn, count ? fl->f_type : 0);
209		tflp = fl->f_next;
210		free(fl->f_fn);
211		free(fl);
212	}
213	(void) fclose(outf);
214}
215
216/*
217 * convert a dev name to a .h file name
218 */
219static char *
220toheader(dev)
221	char *dev;
222{
223	static char hbuf[80];
224
225	(void) strcpy(hbuf, path(dev));
226	(void) strcat(hbuf, ".h");
227	return (hbuf);
228}
229
230/*
231 * convert a dev name to a macro name
232 */
233static char *
234tomacro(dev)
235	register char *dev;
236{
237	static char mbuf[20];
238	register char *cp;
239
240	cp = mbuf;
241	*cp++ = 'N';
242	while (*dev)
243		*cp++ = islower(*dev) ? toupper(*dev++) : *dev++;
244	*cp++ = 0;
245	return (mbuf);
246}
247