mkheaders.c revision 100461
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 100461 2002-07-21 22:23:56Z 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 nocount(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	int errors;
66
67	errors = 0;
68	for (fl = ftab; fl != 0; fl = fl->f_next) {
69		if (fl->f_needs != 0) {
70			match = 0;
71			for (dp = dtab; dp != 0; dp = dp->d_next) {
72				if (eq(dp->d_name, fl->f_needs)) {
73					match++;
74					dp->d_done |= DEVDONE;
75				}
76			}
77			if (fl->f_flags & NEED_COUNT)
78				do_header(fl->f_needs, match);
79		}
80	}
81	for (dp = dtab; dp != 0; dp = dp->d_next) {
82		if (!(dp->d_done & DEVDONE)) {
83			warnx("Error: device \"%s\" is unknown",
84			       dp->d_name);
85			       errors++;
86			}
87		if (dp->d_count == UNKNOWN)
88			continue;
89		match = 0;
90		for (fl = ftab; fl != 0; fl = fl->f_next) {
91			if (fl->f_needs == 0)
92				continue;
93			if ((fl->f_flags & NEED_COUNT) == 0)
94				continue;
95			if (eq(dp->d_name, fl->f_needs)) {
96				match++;
97				break;
98			}
99		}
100		if (match == 0) {
101			warnx("Error: device \"%s\" does not take a count",
102			    dp->d_name);
103			errors++;
104		}
105	}
106	if (errors)
107		errx(1, "%d errors", errors);
108}
109
110static void
111do_header(char *dev, int match)
112{
113	char *file, *name, *inw;
114	struct file_list *fl, *fl_head, *tflp;
115	struct device *dp;
116	FILE *inf, *outf;
117	int inc, oldcount;
118	int count, hicount;
119
120	/*
121	 * After this loop, "count" will be the actual number of units,
122	 * and "hicount" will be the highest unit declared.  do_header()
123	 * must use this higher of these values.
124	 */
125	for (hicount = count = 0, dp = dtab; dp != 0; dp = dp->d_next) {
126		if (eq(dp->d_name, dev)) {
127			count =
128			    dp->d_count != UNKNOWN ? dp->d_count : 1;
129			break;
130		}
131	}
132	file = toheader(dev);
133	name = tomacro(dev);
134	if (match)
135		printf("FYI: static unit limits for %s are set: %s=%d\n", dev, name, count);
136	remember(file);
137	inf = fopen(file, "r");
138	oldcount = -1;
139	if (inf == 0) {
140		outf = fopen(file, "w");
141		if (outf == 0)
142			err(1, "%s", file);
143		fprintf(outf, "#define %s %d\n", name, count);
144		(void) fclose(outf);
145		return;
146	}
147	fl_head = NULL;
148	for (;;) {
149		char *cp;
150		if ((inw = get_word(inf)) == 0 || inw == (char *)EOF)
151			break;
152		if ((inw = get_word(inf)) == 0 || inw == (char *)EOF)
153			break;
154		inw = ns(inw);
155		cp = get_word(inf);
156		if (cp == 0 || cp == (char *)EOF)
157			break;
158		inc = atoi(cp);
159		if (eq(inw, name)) {
160			oldcount = inc;
161			inc = count;
162		}
163		cp = get_word(inf);
164		if (cp == (char *)EOF)
165			break;
166		fl = (struct file_list *) malloc(sizeof *fl);
167		bzero(fl, sizeof(*fl));
168		fl->f_fn = inw;		/* malloced */
169		fl->f_type = inc;
170		fl->f_next = fl_head;
171		fl_head = fl;
172	}
173	(void) fclose(inf);
174	if (count == oldcount) {
175		for (fl = fl_head; fl != NULL; fl = tflp) {
176			tflp = fl->f_next;
177			free(fl->f_fn);
178			free(fl);
179		}
180		return;
181	}
182	if (oldcount == -1) {
183		fl = (struct file_list *) malloc(sizeof *fl);
184		bzero(fl, sizeof(*fl));
185		fl->f_fn = ns(name);
186		fl->f_type = count;
187		fl->f_next = fl_head;
188		fl_head = fl;
189	}
190	outf = fopen(file, "w");
191	if (outf == 0)
192		err(1, "%s", file);
193	for (fl = fl_head; fl != NULL; fl = tflp) {
194		fprintf(outf,
195		    "#define %s %u\n", fl->f_fn, count ? fl->f_type : 0);
196		tflp = fl->f_next;
197		free(fl->f_fn);
198		free(fl);
199	}
200	(void) fclose(outf);
201}
202
203/*
204 * convert a dev name to a .h file name
205 */
206static char *
207toheader(char *dev)
208{
209	static char hbuf[MAXPATHLEN];
210
211	snprintf(hbuf, sizeof(hbuf), "%s.h", path(dev));
212	return (hbuf);
213}
214
215/*
216 * convert a dev name to a macro name
217 */
218static char *
219tomacro(char *dev)
220{
221	static char mbuf[20];
222	char *cp;
223
224	cp = mbuf;
225	*cp++ = 'N';
226	while (*dev)
227		*cp++ = islower(*dev) ? toupper(*dev++) : *dev++;
228	*cp++ = 0;
229	return (mbuf);
230}
231