xref: /csrg-svn/usr.sbin/config/mkheaders.c (revision 61816)
119988Sdist /*
2*61816Sbostic  * Copyright (c) 1980, 1993
3*61816Sbostic  *	The Regents of the University of California.  All rights reserved.
434131Sbostic  *
542795Sbostic  * %sccs.include.redist.c%
619988Sdist  */
719988Sdist 
814557Ssam #ifndef lint
9*61816Sbostic static char sccsid[] = "@(#)mkheaders.c	8.1 (Berkeley) 06/06/93";
1034131Sbostic #endif /* not lint */
118893Sroot 
122651Stoy /*
132679Stoy  * Make all the .h files for the optional entries
142651Stoy  */
152651Stoy 
162651Stoy #include <stdio.h>
172651Stoy #include <ctype.h>
182651Stoy #include "config.h"
197480Skre #include "y.tab.h"
202651Stoy 
headers()212651Stoy headers()
222651Stoy {
238893Sroot 	register struct file_list *fl;
242679Stoy 
258893Sroot 	for (fl = ftab; fl != 0; fl = fl->f_next)
268893Sroot 		if (fl->f_needs != 0)
278895Sroot 			do_count(fl->f_needs, fl->f_needs, 1);
282679Stoy }
292679Stoy 
302679Stoy /*
318893Sroot  * count all the devices of a certain type and recurse to count
328893Sroot  * whatever the device is connected to
332679Stoy  */
do_count(dev,hname,search)342679Stoy do_count(dev, hname, search)
358893Sroot 	register char *dev, *hname;
368897Sroot 	int search;
372679Stoy {
388893Sroot 	register struct device *dp, *mp;
3959852Shibler 	register int count, hicount;
402651Stoy 
4159852Shibler 	/*
4259852Shibler 	 * After this loop, "count" will be the actual number of units,
4359852Shibler 	 * and "hicount" will be the highest unit declared.  do_header()
4459852Shibler 	 * must use this higher of these values.
4559852Shibler 	 */
4659852Shibler 	for (hicount = count = 0, dp = dtab; dp != 0; dp = dp->d_next)
478893Sroot 		if (dp->d_unit != -1 && eq(dp->d_name, dev)) {
488893Sroot 			if (dp->d_type == PSEUDO_DEVICE) {
499598Ssam 				count =
509598Ssam 				    dp->d_slave != UNKNOWN ? dp->d_slave : 1;
518893Sroot 				break;
528893Sroot 			}
538893Sroot 			count++;
548893Sroot 			/*
558893Sroot 			 * Allow holes in unit numbering,
568893Sroot 			 * assumption is unit numbering starts
578893Sroot 			 * at zero.
588893Sroot 			 */
5959852Shibler 			if (dp->d_unit + 1 > hicount)
6059852Shibler 				hicount = dp->d_unit + 1;
618893Sroot 			if (search) {
628893Sroot 				mp = dp->d_conn;
6334468Skarels 				if (mp != 0 && mp != TO_NEXUS &&
6434468Skarels 				    mp->d_conn != 0 && mp->d_conn != TO_NEXUS) {
658895Sroot 					do_count(mp->d_name, hname, 0);
668895Sroot 					search = 0;
678893Sroot 				}
688893Sroot 			}
692679Stoy 		}
7059852Shibler 	do_header(dev, hname, count > hicount ? count : hicount);
712679Stoy }
722679Stoy 
do_header(dev,hname,count)732679Stoy do_header(dev, hname, count)
748893Sroot 	char *dev, *hname;
758893Sroot 	int count;
762679Stoy {
778893Sroot 	char *file, *name, *inw, *toheader(), *tomacro();
7853362Sbostic 	struct file_list *fl, *fl_head, *tflp;
798893Sroot 	FILE *inf, *outf;
808893Sroot 	int inc, oldcount;
812679Stoy 
828893Sroot 	file = toheader(hname);
838893Sroot 	name = tomacro(dev);
848893Sroot 	inf = fopen(file, "r");
858893Sroot 	oldcount = -1;
868893Sroot 	if (inf == 0) {
878893Sroot 		outf = fopen(file, "w");
888893Sroot 		if (outf == 0) {
898893Sroot 			perror(file);
908893Sroot 			exit(1);
918893Sroot 		}
928893Sroot 		fprintf(outf, "#define %s %d\n", name, count);
938900Sroot 		(void) fclose(outf);
948893Sroot 		return;
958893Sroot 	}
9653362Sbostic 	fl_head = NULL;
978893Sroot 	for (;;) {
988893Sroot 		char *cp;
998900Sroot 		if ((inw = get_word(inf)) == 0 || inw == (char *)EOF)
1008893Sroot 			break;
1018900Sroot 		if ((inw = get_word(inf)) == 0 || inw == (char *)EOF)
1028893Sroot 			break;
1038893Sroot 		inw = ns(inw);
1048893Sroot 		cp = get_word(inf);
1058900Sroot 		if (cp == 0 || cp == (char *)EOF)
1068893Sroot 			break;
1078893Sroot 		inc = atoi(cp);
1088893Sroot 		if (eq(inw, name)) {
1098893Sroot 			oldcount = inc;
1108893Sroot 			inc = count;
1118893Sroot 		}
1128893Sroot 		cp = get_word(inf);
1139598Ssam 		if (cp == (char *)EOF)
1148893Sroot 			break;
1158893Sroot 		fl = (struct file_list *) malloc(sizeof *fl);
11650348Skarels 		bzero(fl, sizeof(*fl));
1178893Sroot 		fl->f_fn = inw;
1188893Sroot 		fl->f_type = inc;
1198893Sroot 		fl->f_next = fl_head;
1208893Sroot 		fl_head = fl;
1218893Sroot 	}
1228900Sroot 	(void) fclose(inf);
1238893Sroot 	if (count == oldcount) {
12453362Sbostic 		for (fl = fl_head; fl != NULL; fl = tflp) {
12553362Sbostic 			tflp = fl->f_next;
12653362Sbostic 			free(fl);
12753362Sbostic 		}
1288893Sroot 		return;
1298893Sroot 	}
1308893Sroot 	if (oldcount == -1) {
1318893Sroot 		fl = (struct file_list *) malloc(sizeof *fl);
13250348Skarels 		bzero(fl, sizeof(*fl));
1338893Sroot 		fl->f_fn = name;
1348893Sroot 		fl->f_type = count;
1358893Sroot 		fl->f_next = fl_head;
1368893Sroot 		fl_head = fl;
1378893Sroot 	}
1382679Stoy 	outf = fopen(file, "w");
1398893Sroot 	if (outf == 0) {
1408893Sroot 		perror(file);
1418893Sroot 		exit(1);
1422716Swnj 	}
14353362Sbostic 	for (fl = fl_head; fl != NULL; fl = tflp) {
14453362Sbostic 		fprintf(outf,
14553362Sbostic 		    "#define %s %u\n", fl->f_fn, count ? fl->f_type : 0);
14653362Sbostic 		tflp = fl->f_next;
14753362Sbostic 		free(fl);
1488893Sroot 	}
1498900Sroot 	(void) fclose(outf);
1502651Stoy }
1512651Stoy 
1522651Stoy /*
1538893Sroot  * convert a dev name to a .h file name
1542651Stoy  */
1558893Sroot char *
toheader(dev)1568893Sroot toheader(dev)
1578893Sroot 	char *dev;
1582651Stoy {
1598893Sroot 	static char hbuf[80];
1602651Stoy 
1618900Sroot 	(void) strcpy(hbuf, path(dev));
1628900Sroot 	(void) strcat(hbuf, ".h");
1638897Sroot 	return (hbuf);
1642651Stoy }
1652651Stoy 
1662651Stoy /*
1678893Sroot  * convert a dev name to a macro name
1682651Stoy  */
tomacro(dev)1692679Stoy char *tomacro(dev)
1708893Sroot 	register char *dev;
1712651Stoy {
1728893Sroot 	static char mbuf[20];
1738893Sroot 	register char *cp;
1742651Stoy 
1758893Sroot 	cp = mbuf;
1768893Sroot 	*cp++ = 'N';
1778893Sroot 	while (*dev)
17854433Smckusick 		*cp++ = islower(*dev) ? toupper(*dev++) : *dev++;
1798893Sroot 	*cp++ = 0;
1808897Sroot 	return (mbuf);
1812651Stoy }
182