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