mkheaders.c revision 1553
117706Sjulian/*
217706Sjulian * Copyright (c) 1980, 1993
317706Sjulian *	The Regents of the University of California.  All rights reserved.
417706Sjulian *
517706Sjulian * Redistribution and use in source and binary forms, with or without
617706Sjulian * modification, are permitted provided that the following conditions
717706Sjulian * are met:
817706Sjulian * 1. Redistributions of source code must retain the above copyright
917706Sjulian *    notice, this list of conditions and the following disclaimer.
1017706Sjulian * 2. Redistributions in binary form must reproduce the above copyright
1117706Sjulian *    notice, this list of conditions and the following disclaimer in the
1217706Sjulian *    documentation and/or other materials provided with the distribution.
1317706Sjulian * 3. All advertising materials mentioning features or use of this software
1417706Sjulian *    must display the following acknowledgement:
1517706Sjulian *	This product includes software developed by the University of
1617706Sjulian *	California, Berkeley and its contributors.
1717706Sjulian * 4. Neither the name of the University nor the names of its contributors
1817706Sjulian *    may be used to endorse or promote products derived from this software
1917706Sjulian *    without specific prior written permission.
2017706Sjulian *
2117706Sjulian * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2217706Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2317706Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2417706Sjulian * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2517706Sjulian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2617706Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2717706Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2817706Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2917706Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3017706Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3150476Speter * SUCH DAMAGE.
3248794Snik */
3317706Sjulian
3417706Sjulian#ifndef lint
3579531Srustatic char sccsid[] = "@(#)mkheaders.c	8.1 (Berkeley) 6/6/93";
3617706Sjulian#endif /* not lint */
3717706Sjulian
3817706Sjulian/*
3959501Sphantom * Make all the .h files for the optional entries
40124535Sru */
4117706Sjulian
4284306Sru#include <stdio.h>
4317706Sjulian#include <ctype.h>
4417706Sjulian#include "config.h"
4517706Sjulian#include "y.tab.h"
4617706Sjulian
4717706Sjulianheaders()
4817706Sjulian{
4917706Sjulian	register struct file_list *fl;
5017706Sjulian
5117706Sjulian	for (fl = ftab; fl != 0; fl = fl->f_next)
52139386Strhodes		if (fl->f_needs != 0)
5357686Ssheldonh			do_count(fl->f_needs, fl->f_needs, 1);
5417706Sjulian}
5517706Sjulian
5617706Sjulian/*
5717706Sjulian * count all the devices of a certain type and recurse to count
5817706Sjulian * whatever the device is connected to
5917706Sjulian */
6017706Sjuliando_count(dev, hname, search)
6117706Sjulian	register char *dev, *hname;
6217706Sjulian	int search;
6317706Sjulian{
6417706Sjulian	register struct device *dp, *mp;
6517706Sjulian	register int count, hicount;
6617706Sjulian
67112542Scharnier	/*
6817706Sjulian	 * After this loop, "count" will be the actual number of units,
69112542Scharnier	 * and "hicount" will be the highest unit declared.  do_header()
70283977Spkelsey	 * must use this higher of these values.
71283977Spkelsey	 */
72283977Spkelsey	for (hicount = count = 0, dp = dtab; dp != 0; dp = dp->d_next)
73283977Spkelsey		if (dp->d_unit != -1 && eq(dp->d_name, dev)) {
7417706Sjulian			if (dp->d_type == PSEUDO_DEVICE) {
75112542Scharnier				count =
7617706Sjulian				    dp->d_slave != UNKNOWN ? dp->d_slave : 1;
7757686Ssheldonh				break;
7857686Ssheldonh			}
7917706Sjulian			count++;
8017706Sjulian			/*
81112542Scharnier			 * Allow holes in unit numbering,
8217706Sjulian			 * assumption is unit numbering starts
83112542Scharnier			 * at zero.
8417706Sjulian			 */
8517706Sjulian			if (dp->d_unit + 1 > hicount)
8617706Sjulian				hicount = dp->d_unit + 1;
8717706Sjulian			if (search) {
8817706Sjulian				mp = dp->d_conn;
8917706Sjulian				if (mp != 0 && mp != TO_NEXUS &&
9017706Sjulian				    mp->d_conn != 0 && mp->d_conn != TO_NEXUS) {
9117706Sjulian					do_count(mp->d_name, hname, 0);
9217706Sjulian					search = 0;
9317706Sjulian				}
9421907Swosch			}
9517706Sjulian		}
9621907Swosch	do_header(dev, hname, count > hicount ? count : hicount);
9717706Sjulian}
98112542Scharnier
9917706Sjuliando_header(dev, hname, count)
100112542Scharnier	char *dev, *hname;
10173093Sru	int count;
102{
103	char *file, *name, *inw, *toheader(), *tomacro();
104	struct file_list *fl, *fl_head, *tflp;
105	FILE *inf, *outf;
106	int inc, oldcount;
107
108	file = toheader(hname);
109	name = tomacro(dev);
110	inf = fopen(file, "r");
111	oldcount = -1;
112	if (inf == 0) {
113		outf = fopen(file, "w");
114		if (outf == 0) {
115			perror(file);
116			exit(1);
117		}
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;
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);
153		}
154		return;
155	}
156	if (oldcount == -1) {
157		fl = (struct file_list *) malloc(sizeof *fl);
158		bzero(fl, sizeof(*fl));
159		fl->f_fn = name;
160		fl->f_type = count;
161		fl->f_next = fl_head;
162		fl_head = fl;
163	}
164	outf = fopen(file, "w");
165	if (outf == 0) {
166		perror(file);
167		exit(1);
168	}
169	for (fl = fl_head; fl != NULL; fl = tflp) {
170		fprintf(outf,
171		    "#define %s %u\n", fl->f_fn, count ? fl->f_type : 0);
172		tflp = fl->f_next;
173		free(fl);
174	}
175	(void) fclose(outf);
176}
177
178/*
179 * convert a dev name to a .h file name
180 */
181char *
182toheader(dev)
183	char *dev;
184{
185	static char hbuf[80];
186
187	(void) strcpy(hbuf, path(dev));
188	(void) strcat(hbuf, ".h");
189	return (hbuf);
190}
191
192/*
193 * convert a dev name to a macro name
194 */
195char *tomacro(dev)
196	register char *dev;
197{
198	static char mbuf[20];
199	register char *cp;
200
201	cp = mbuf;
202	*cp++ = 'N';
203	while (*dev)
204		*cp++ = islower(*dev) ? toupper(*dev++) : *dev++;
205	*cp++ = 0;
206	return (mbuf);
207}
208