mkheaders.c revision 20458
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
35static char sccsid[] = "@(#)mkheaders.c	8.1 (Berkeley) 6/6/93";
36#endif /* not lint */
37
38/*
39 * Make all the .h files for the optional entries
40 */
41
42#include <stdio.h>
43#include <string.h>
44#include <ctype.h>
45#include "config.h"
46#include "y.tab.h"
47
48#define ns(s) strdup(s)
49
50headers()
51{
52	register struct file_list *fl;
53
54	for (fl = ftab; fl != 0; fl = fl->f_next)
55		if (fl->f_needs != 0)
56			do_count(fl->f_needs, fl->f_needs, 1);
57}
58
59/*
60 * count all the devices of a certain type and recurse to count
61 * whatever the device is connected to
62 */
63do_count(dev, hname, search)
64	register char *dev, *hname;
65	int search;
66{
67	register struct device *dp, *mp;
68	register int count, hicount;
69
70	/*
71	 * After this loop, "count" will be the actual number of units,
72	 * and "hicount" will be the highest unit declared.  do_header()
73	 * must use this higher of these values.
74	 */
75	for (hicount = count = 0, dp = dtab; dp != 0; dp = dp->d_next)
76		if (dp->d_unit != -1 && eq(dp->d_name, dev)) {
77			if (dp->d_type == PSEUDO_DEVICE) {
78				count =
79				    dp->d_slave != UNKNOWN ? dp->d_slave : 1;
80				break;
81			}
82			count++;
83			/*
84			 * Allow holes in unit numbering,
85			 * assumption is unit numbering starts
86			 * at zero.
87			 */
88			if (dp->d_unit + 1 > hicount)
89				hicount = dp->d_unit + 1;
90			if (search) {
91				mp = dp->d_conn;
92				if (mp != 0 && mp != TO_NEXUS &&
93				    mp->d_conn != 0 && mp->d_conn != TO_NEXUS) {
94					do_count(mp->d_name, hname, 0);
95					search = 0;
96				}
97			}
98		}
99	do_header(dev, hname, count > hicount ? count : hicount);
100}
101
102do_header(dev, hname, count)
103	char *dev, *hname;
104	int count;
105{
106	char *file, *name, *inw, *toheader(), *tomacro();
107	struct file_list *fl, *fl_head, *tflp;
108	FILE *inf, *outf;
109	int inc, oldcount;
110
111	file = toheader(hname);
112	name = tomacro(dev);
113	inf = fopen(file, "r");
114	oldcount = -1;
115	if (inf == 0) {
116		outf = fopen(file, "w");
117		if (outf == 0) {
118			perror(file);
119			exit(1);
120		}
121		fprintf(outf, "#define %s %d\n", name, count);
122		(void) fclose(outf);
123		return;
124	}
125	fl_head = NULL;
126	for (;;) {
127		char *cp;
128		if ((inw = get_word(inf)) == 0 || inw == (char *)EOF)
129			break;
130		if ((inw = get_word(inf)) == 0 || inw == (char *)EOF)
131			break;
132		inw = ns(inw);
133		cp = get_word(inf);
134		if (cp == 0 || cp == (char *)EOF)
135			break;
136		inc = atoi(cp);
137		if (eq(inw, name)) {
138			oldcount = inc;
139			inc = count;
140		}
141		cp = get_word(inf);
142		if (cp == (char *)EOF)
143			break;
144		fl = (struct file_list *) malloc(sizeof *fl);
145		bzero(fl, sizeof(*fl));
146		fl->f_fn = inw;		/* malloced */
147		fl->f_type = inc;
148		fl->f_next = fl_head;
149		fl_head = fl;
150	}
151	(void) fclose(inf);
152	if (count == oldcount) {
153		for (fl = fl_head; fl != NULL; fl = tflp) {
154			tflp = fl->f_next;
155			free(fl->f_fn);
156			free(fl);
157		}
158		return;
159	}
160	if (oldcount == -1) {
161		fl = (struct file_list *) malloc(sizeof *fl);
162		bzero(fl, sizeof(*fl));
163		fl->f_fn = ns(name);
164		fl->f_type = count;
165		fl->f_next = fl_head;
166		fl_head = fl;
167	}
168	outf = fopen(file, "w");
169	if (outf == 0) {
170		perror(file);
171		exit(1);
172	}
173	for (fl = fl_head; fl != NULL; fl = tflp) {
174		fprintf(outf,
175		    "#define %s %u\n", fl->f_fn, count ? fl->f_type : 0);
176		tflp = fl->f_next;
177		free(fl->f_fn);
178		free(fl);
179	}
180	(void) fclose(outf);
181}
182
183/*
184 * convert a dev name to a .h file name
185 */
186char *
187toheader(dev)
188	char *dev;
189{
190	static char hbuf[80];
191
192	(void) strcpy(hbuf, path(dev));
193	(void) strcat(hbuf, ".h");
194	return (hbuf);
195}
196
197/*
198 * convert a dev name to a macro name
199 */
200char *tomacro(dev)
201	register char *dev;
202{
203	static char mbuf[20];
204	register char *cp;
205
206	cp = mbuf;
207	*cp++ = 'N';
208	while (*dev)
209		*cp++ = islower(*dev) ? toupper(*dev++) : *dev++;
210	*cp++ = 0;
211	return (mbuf);
212}
213