mkheaders.c revision 100461
11553Srgrimes/*
21553Srgrimes * Copyright (c) 1980, 1993
31553Srgrimes *	The Regents of the University of California.  All rights reserved.
41553Srgrimes *
51553Srgrimes * Redistribution and use in source and binary forms, with or without
61553Srgrimes * modification, are permitted provided that the following conditions
71553Srgrimes * are met:
81553Srgrimes * 1. Redistributions of source code must retain the above copyright
91553Srgrimes *    notice, this list of conditions and the following disclaimer.
101553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111553Srgrimes *    notice, this list of conditions and the following disclaimer in the
121553Srgrimes *    documentation and/or other materials provided with the distribution.
131553Srgrimes * 3. All advertising materials mentioning features or use of this software
141553Srgrimes *    must display the following acknowledgement:
151553Srgrimes *	This product includes software developed by the University of
161553Srgrimes *	California, Berkeley and its contributors.
171553Srgrimes * 4. Neither the name of the University nor the names of its contributors
181553Srgrimes *    may be used to endorse or promote products derived from this software
191553Srgrimes *    without specific prior written permission.
201553Srgrimes *
211553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241553Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311553Srgrimes * SUCH DAMAGE.
321553Srgrimes */
331553Srgrimes
341553Srgrimes#ifndef lint
3529451Scharnier#if 0
361553Srgrimesstatic char sccsid[] = "@(#)mkheaders.c	8.1 (Berkeley) 6/6/93";
3729451Scharnier#endif
3829451Scharnierstatic const char rcsid[] =
3950479Speter  "$FreeBSD: head/usr.sbin/config/mkheaders.c 100461 2002-07-21 22:23:56Z peter $";
401553Srgrimes#endif /* not lint */
411553Srgrimes
421553Srgrimes/*
431553Srgrimes * Make all the .h files for the optional entries
441553Srgrimes */
451553Srgrimes
4629451Scharnier#include <ctype.h>
4729451Scharnier#include <err.h>
481553Srgrimes#include <stdio.h>
4920458Sjoerg#include <string.h>
5069004Simp#include <sys/param.h>
511553Srgrimes#include "config.h"
5216073Sphk#include "y.tab.h"
531553Srgrimes
5461640Speterstatic void do_header(char *, int);
55100461Speterstatic void nocount(char *);
5661640Speterstatic char *toheader(char *);
5761640Speterstatic char *tomacro(char *);
5829451Scharnier
5929451Scharniervoid
6061640Speterheaders(void)
611553Srgrimes{
6261640Speter	struct file_list *fl;
6330796Sjoerg	struct device *dp;
6471866Speter	int match;
65100461Speter	int errors;
661553Srgrimes
67100461Speter	errors = 0;
6861523Speter	for (fl = ftab; fl != 0; fl = fl->f_next) {
6961523Speter		if (fl->f_needs != 0) {
7071866Speter			match = 0;
7161523Speter			for (dp = dtab; dp != 0; dp = dp->d_next) {
7261523Speter				if (eq(dp->d_name, fl->f_needs)) {
7371866Speter					match++;
7471879Speter					dp->d_done |= DEVDONE;
7561523Speter				}
7661523Speter			}
7771878Speter			if (fl->f_flags & NEED_COUNT)
7871878Speter				do_header(fl->f_needs, match);
7961523Speter		}
8061523Speter	}
8147715Speter	for (dp = dtab; dp != 0; dp = dp->d_next) {
82100461Speter		if (!(dp->d_done & DEVDONE)) {
83100461Speter			warnx("Error: device \"%s\" is unknown",
8471879Speter			       dp->d_name);
85100461Speter			       errors++;
86100461Speter			}
87100461Speter		if (dp->d_count == UNKNOWN)
88100461Speter			continue;
89100461Speter		match = 0;
90100461Speter		for (fl = ftab; fl != 0; fl = fl->f_next) {
91100461Speter			if (fl->f_needs == 0)
92100461Speter				continue;
93100461Speter			if ((fl->f_flags & NEED_COUNT) == 0)
94100461Speter				continue;
95100461Speter			if (eq(dp->d_name, fl->f_needs)) {
96100461Speter				match++;
97100461Speter				break;
98100461Speter			}
99100461Speter		}
100100461Speter		if (match == 0) {
101100461Speter			warnx("Error: device \"%s\" does not take a count",
102100461Speter			    dp->d_name);
103100461Speter			errors++;
104100461Speter		}
10547715Speter	}
106100461Speter	if (errors)
107100461Speter		errx(1, "%d errors", errors);
1081553Srgrimes}
1091553Srgrimes
11045744Speterstatic void
11171878Speterdo_header(char *dev, int match)
1121553Srgrimes{
11371878Speter	char *file, *name, *inw;
11471878Speter	struct file_list *fl, *fl_head, *tflp;
11561640Speter	struct device *dp;
11671878Speter	FILE *inf, *outf;
11771878Speter	int inc, oldcount;
11861640Speter	int count, hicount;
1191553Srgrimes
1201553Srgrimes	/*
1211553Srgrimes	 * After this loop, "count" will be the actual number of units,
1221553Srgrimes	 * and "hicount" will be the highest unit declared.  do_header()
1231553Srgrimes	 * must use this higher of these values.
1241553Srgrimes	 */
12555605Speter	for (hicount = count = 0, dp = dtab; dp != 0; dp = dp->d_next) {
12661640Speter		if (eq(dp->d_name, dev)) {
12761640Speter			count =
12861640Speter			    dp->d_count != UNKNOWN ? dp->d_count : 1;
12961640Speter			break;
1301553Srgrimes		}
13154205Speter	}
13261523Speter	file = toheader(dev);
1331553Srgrimes	name = tomacro(dev);
13471878Speter	if (match)
13571882Speter		printf("FYI: static unit limits for %s are set: %s=%d\n", dev, name, count);
13671866Speter	remember(file);
1371553Srgrimes	inf = fopen(file, "r");
1381553Srgrimes	oldcount = -1;
1391553Srgrimes	if (inf == 0) {
1401553Srgrimes		outf = fopen(file, "w");
14129451Scharnier		if (outf == 0)
14229451Scharnier			err(1, "%s", file);
1431553Srgrimes		fprintf(outf, "#define %s %d\n", name, count);
1441553Srgrimes		(void) fclose(outf);
1451553Srgrimes		return;
1461553Srgrimes	}
1471553Srgrimes	fl_head = NULL;
1481553Srgrimes	for (;;) {
1491553Srgrimes		char *cp;
1501553Srgrimes		if ((inw = get_word(inf)) == 0 || inw == (char *)EOF)
1511553Srgrimes			break;
1521553Srgrimes		if ((inw = get_word(inf)) == 0 || inw == (char *)EOF)
1531553Srgrimes			break;
1541553Srgrimes		inw = ns(inw);
1551553Srgrimes		cp = get_word(inf);
1561553Srgrimes		if (cp == 0 || cp == (char *)EOF)
1571553Srgrimes			break;
1581553Srgrimes		inc = atoi(cp);
1591553Srgrimes		if (eq(inw, name)) {
1601553Srgrimes			oldcount = inc;
1611553Srgrimes			inc = count;
1621553Srgrimes		}
1631553Srgrimes		cp = get_word(inf);
1641553Srgrimes		if (cp == (char *)EOF)
1651553Srgrimes			break;
1661553Srgrimes		fl = (struct file_list *) malloc(sizeof *fl);
1671553Srgrimes		bzero(fl, sizeof(*fl));
16812772Speter		fl->f_fn = inw;		/* malloced */
1691553Srgrimes		fl->f_type = inc;
1701553Srgrimes		fl->f_next = fl_head;
1711553Srgrimes		fl_head = fl;
1721553Srgrimes	}
1731553Srgrimes	(void) fclose(inf);
1741553Srgrimes	if (count == oldcount) {
1751553Srgrimes		for (fl = fl_head; fl != NULL; fl = tflp) {
1761553Srgrimes			tflp = fl->f_next;
17712772Speter			free(fl->f_fn);
1781553Srgrimes			free(fl);
1791553Srgrimes		}
1801553Srgrimes		return;
1811553Srgrimes	}
1821553Srgrimes	if (oldcount == -1) {
1831553Srgrimes		fl = (struct file_list *) malloc(sizeof *fl);
1841553Srgrimes		bzero(fl, sizeof(*fl));
18520458Sjoerg		fl->f_fn = ns(name);
1861553Srgrimes		fl->f_type = count;
1871553Srgrimes		fl->f_next = fl_head;
1881553Srgrimes		fl_head = fl;
1891553Srgrimes	}
1901553Srgrimes	outf = fopen(file, "w");
19129451Scharnier	if (outf == 0)
19229451Scharnier		err(1, "%s", file);
1931553Srgrimes	for (fl = fl_head; fl != NULL; fl = tflp) {
1941553Srgrimes		fprintf(outf,
1951553Srgrimes		    "#define %s %u\n", fl->f_fn, count ? fl->f_type : 0);
1961553Srgrimes		tflp = fl->f_next;
19712772Speter		free(fl->f_fn);
1981553Srgrimes		free(fl);
1991553Srgrimes	}
2001553Srgrimes	(void) fclose(outf);
2011553Srgrimes}
2021553Srgrimes
2031553Srgrimes/*
2041553Srgrimes * convert a dev name to a .h file name
2051553Srgrimes */
20645744Speterstatic char *
20761640Spetertoheader(char *dev)
2081553Srgrimes{
20969004Simp	static char hbuf[MAXPATHLEN];
2101553Srgrimes
21169004Simp	snprintf(hbuf, sizeof(hbuf), "%s.h", path(dev));
2121553Srgrimes	return (hbuf);
2131553Srgrimes}
2141553Srgrimes
2151553Srgrimes/*
2161553Srgrimes * convert a dev name to a macro name
2171553Srgrimes */
21845744Speterstatic char *
21961640Spetertomacro(char *dev)
2201553Srgrimes{
2211553Srgrimes	static char mbuf[20];
22261640Speter	char *cp;
2231553Srgrimes
2241553Srgrimes	cp = mbuf;
2251553Srgrimes	*cp++ = 'N';
2261553Srgrimes	while (*dev)
2271553Srgrimes		*cp++ = islower(*dev) ? toupper(*dev++) : *dev++;
2281553Srgrimes	*cp++ = 0;
2291553Srgrimes	return (mbuf);
2301553Srgrimes}
231