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