xref: /original-bsd/usr.sbin/config/mkheaders.c (revision 557ac071)
1c5cae06fSdist /*
2*557ac071Sbostic  * Copyright (c) 1980, 1993
3*557ac071Sbostic  *	The Regents of the University of California.  All rights reserved.
4923d6574Sbostic  *
5d6136d1eSbostic  * %sccs.include.redist.c%
6c5cae06fSdist  */
7c5cae06fSdist 
8c6752a94Ssam #ifndef lint
9*557ac071Sbostic static char sccsid[] = "@(#)mkheaders.c	8.1 (Berkeley) 06/06/93";
10923d6574Sbostic #endif /* not lint */
1161e1a868Sroot 
12e4ebc0a3Stoy /*
1307201eebStoy  * Make all the .h files for the optional entries
14e4ebc0a3Stoy  */
15e4ebc0a3Stoy 
16e4ebc0a3Stoy #include <stdio.h>
17e4ebc0a3Stoy #include <ctype.h>
18e4ebc0a3Stoy #include "config.h"
1965b19a79Skre #include "y.tab.h"
20e4ebc0a3Stoy 
headers()21e4ebc0a3Stoy headers()
22e4ebc0a3Stoy {
2307201eebStoy 	register struct file_list *fl;
2407201eebStoy 
2561e1a868Sroot 	for (fl = ftab; fl != 0; fl = fl->f_next)
2661e1a868Sroot 		if (fl->f_needs != 0)
27cab430bfSroot 			do_count(fl->f_needs, fl->f_needs, 1);
2807201eebStoy }
2907201eebStoy 
3007201eebStoy /*
3161e1a868Sroot  * count all the devices of a certain type and recurse to count
3207201eebStoy  * whatever the device is connected to
3307201eebStoy  */
do_count(dev,hname,search)3407201eebStoy do_count(dev, hname, search)
3507201eebStoy 	register char *dev, *hname;
3665fc4421Sroot 	int search;
3707201eebStoy {
3807201eebStoy 	register struct device *dp, *mp;
39d4ee003aShibler 	register int count, hicount;
40e4ebc0a3Stoy 
41d4ee003aShibler 	/*
42d4ee003aShibler 	 * After this loop, "count" will be the actual number of units,
43d4ee003aShibler 	 * and "hicount" will be the highest unit declared.  do_header()
44d4ee003aShibler 	 * must use this higher of these values.
45d4ee003aShibler 	 */
46d4ee003aShibler 	for (hicount = count = 0, dp = dtab; dp != 0; dp = dp->d_next)
4761e1a868Sroot 		if (dp->d_unit != -1 && eq(dp->d_name, dev)) {
4865b19a79Skre 			if (dp->d_type == PSEUDO_DEVICE) {
4989c4b547Ssam 				count =
5089c4b547Ssam 				    dp->d_slave != UNKNOWN ? dp->d_slave : 1;
5165b19a79Skre 				break;
5265b19a79Skre 			}
53e4ebc0a3Stoy 			count++;
54e90754bcSsam 			/*
55e90754bcSsam 			 * Allow holes in unit numbering,
56e90754bcSsam 			 * assumption is unit numbering starts
57e90754bcSsam 			 * at zero.
58e90754bcSsam 			 */
59d4ee003aShibler 			if (dp->d_unit + 1 > hicount)
60d4ee003aShibler 				hicount = dp->d_unit + 1;
6161e1a868Sroot 			if (search) {
6207201eebStoy 				mp = dp->d_conn;
6389979ee8Skarels 				if (mp != 0 && mp != TO_NEXUS &&
6489979ee8Skarels 				    mp->d_conn != 0 && mp->d_conn != TO_NEXUS) {
65cab430bfSroot 					do_count(mp->d_name, hname, 0);
66cab430bfSroot 					search = 0;
6707201eebStoy 				}
6807201eebStoy 			}
6907201eebStoy 		}
70d4ee003aShibler 	do_header(dev, hname, count > hicount ? count : hicount);
71e4ebc0a3Stoy }
72e4ebc0a3Stoy 
do_header(dev,hname,count)7307201eebStoy do_header(dev, hname, count)
7407201eebStoy 	char *dev, *hname;
75e4ebc0a3Stoy 	int count;
76e4ebc0a3Stoy {
7707201eebStoy 	char *file, *name, *inw, *toheader(), *tomacro();
78cfd417d8Sbostic 	struct file_list *fl, *fl_head, *tflp;
7907201eebStoy 	FILE *inf, *outf;
8007201eebStoy 	int inc, oldcount;
81e4ebc0a3Stoy 
8207201eebStoy 	file = toheader(hname);
8307201eebStoy 	name = tomacro(dev);
8407201eebStoy 	inf = fopen(file, "r");
85e4ebc0a3Stoy 	oldcount = -1;
8661e1a868Sroot 	if (inf == 0) {
8707201eebStoy 		outf = fopen(file, "w");
8861e1a868Sroot 		if (outf == 0) {
899df349d9Swnj 			perror(file);
909df349d9Swnj 			exit(1);
919df349d9Swnj 		}
9207201eebStoy 		fprintf(outf, "#define %s %d\n", name, count);
93228dc850Sroot 		(void) fclose(outf);
9407201eebStoy 		return;
9507201eebStoy 	}
96cfd417d8Sbostic 	fl_head = NULL;
9761e1a868Sroot 	for (;;) {
9861e1a868Sroot 		char *cp;
99228dc850Sroot 		if ((inw = get_word(inf)) == 0 || inw == (char *)EOF)
10007201eebStoy 			break;
101228dc850Sroot 		if ((inw = get_word(inf)) == 0 || inw == (char *)EOF)
10261e1a868Sroot 			break;
10361e1a868Sroot 		inw = ns(inw);
10461e1a868Sroot 		cp = get_word(inf);
105228dc850Sroot 		if (cp == 0 || cp == (char *)EOF)
10661e1a868Sroot 			break;
10761e1a868Sroot 		inc = atoi(cp);
10861e1a868Sroot 		if (eq(inw, name)) {
10907201eebStoy 			oldcount = inc;
11007201eebStoy 			inc = count;
11107201eebStoy 		}
11261e1a868Sroot 		cp = get_word(inf);
11389c4b547Ssam 		if (cp == (char *)EOF)
11461e1a868Sroot 			break;
11507201eebStoy 		fl = (struct file_list *) malloc(sizeof *fl);
1166373cc2fSkarels 		bzero(fl, sizeof(*fl));
11707201eebStoy 		fl->f_fn = inw;
11807201eebStoy 		fl->f_type = inc;
11907201eebStoy 		fl->f_next = fl_head;
12007201eebStoy 		fl_head = fl;
12107201eebStoy 	}
122228dc850Sroot 	(void) fclose(inf);
12361e1a868Sroot 	if (count == oldcount) {
124cfd417d8Sbostic 		for (fl = fl_head; fl != NULL; fl = tflp) {
125cfd417d8Sbostic 			tflp = fl->f_next;
126cfd417d8Sbostic 			free(fl);
127cfd417d8Sbostic 		}
12807201eebStoy 		return;
12907201eebStoy 	}
13061e1a868Sroot 	if (oldcount == -1) {
13107201eebStoy 		fl = (struct file_list *) malloc(sizeof *fl);
1326373cc2fSkarels 		bzero(fl, sizeof(*fl));
13307201eebStoy 		fl->f_fn = name;
13407201eebStoy 		fl->f_type = count;
13507201eebStoy 		fl->f_next = fl_head;
13607201eebStoy 		fl_head = fl;
13707201eebStoy 	}
13807201eebStoy 	outf = fopen(file, "w");
13961e1a868Sroot 	if (outf == 0) {
1409df349d9Swnj 		perror(file);
1419df349d9Swnj 		exit(1);
1429df349d9Swnj 	}
143cfd417d8Sbostic 	for (fl = fl_head; fl != NULL; fl = tflp) {
144cfd417d8Sbostic 		fprintf(outf,
145cfd417d8Sbostic 		    "#define %s %u\n", fl->f_fn, count ? fl->f_type : 0);
146cfd417d8Sbostic 		tflp = fl->f_next;
147cfd417d8Sbostic 		free(fl);
14807201eebStoy 	}
149228dc850Sroot 	(void) fclose(outf);
15007201eebStoy }
15107201eebStoy 
152e4ebc0a3Stoy /*
15361e1a868Sroot  * convert a dev name to a .h file name
154e4ebc0a3Stoy  */
15561e1a868Sroot char *
toheader(dev)15661e1a868Sroot toheader(dev)
15707201eebStoy 	char *dev;
158e4ebc0a3Stoy {
15907201eebStoy 	static char hbuf[80];
16007201eebStoy 
161228dc850Sroot 	(void) strcpy(hbuf, path(dev));
162228dc850Sroot 	(void) strcat(hbuf, ".h");
16365fc4421Sroot 	return (hbuf);
164e4ebc0a3Stoy }
16507201eebStoy 
16607201eebStoy /*
16761e1a868Sroot  * convert a dev name to a macro name
16807201eebStoy  */
tomacro(dev)16907201eebStoy char *tomacro(dev)
17007201eebStoy 	register char *dev;
17107201eebStoy {
17207201eebStoy 	static char mbuf[20];
17307201eebStoy 	register char *cp;
17407201eebStoy 
17507201eebStoy 	cp = mbuf;
17607201eebStoy 	*cp++ = 'N';
17707201eebStoy 	while (*dev)
178528437f0Smckusick 		*cp++ = islower(*dev) ? toupper(*dev++) : *dev++;
17961e1a868Sroot 	*cp++ = 0;
18065fc4421Sroot 	return (mbuf);
181e4ebc0a3Stoy }
182