xref: /original-bsd/usr.sbin/config/mkheaders.c (revision c3e32dec)
1 /*
2  * Copyright (c) 1980, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)mkheaders.c	8.1 (Berkeley) 06/06/93";
10 #endif /* not lint */
11 
12 /*
13  * Make all the .h files for the optional entries
14  */
15 
16 #include <stdio.h>
17 #include <ctype.h>
18 #include "config.h"
19 #include "y.tab.h"
20 
21 headers()
22 {
23 	register struct file_list *fl;
24 
25 	for (fl = ftab; fl != 0; fl = fl->f_next)
26 		if (fl->f_needs != 0)
27 			do_count(fl->f_needs, fl->f_needs, 1);
28 }
29 
30 /*
31  * count all the devices of a certain type and recurse to count
32  * whatever the device is connected to
33  */
34 do_count(dev, hname, search)
35 	register char *dev, *hname;
36 	int search;
37 {
38 	register struct device *dp, *mp;
39 	register int count, hicount;
40 
41 	/*
42 	 * After this loop, "count" will be the actual number of units,
43 	 * and "hicount" will be the highest unit declared.  do_header()
44 	 * must use this higher of these values.
45 	 */
46 	for (hicount = count = 0, dp = dtab; dp != 0; dp = dp->d_next)
47 		if (dp->d_unit != -1 && eq(dp->d_name, dev)) {
48 			if (dp->d_type == PSEUDO_DEVICE) {
49 				count =
50 				    dp->d_slave != UNKNOWN ? dp->d_slave : 1;
51 				break;
52 			}
53 			count++;
54 			/*
55 			 * Allow holes in unit numbering,
56 			 * assumption is unit numbering starts
57 			 * at zero.
58 			 */
59 			if (dp->d_unit + 1 > hicount)
60 				hicount = dp->d_unit + 1;
61 			if (search) {
62 				mp = dp->d_conn;
63 				if (mp != 0 && mp != TO_NEXUS &&
64 				    mp->d_conn != 0 && mp->d_conn != TO_NEXUS) {
65 					do_count(mp->d_name, hname, 0);
66 					search = 0;
67 				}
68 			}
69 		}
70 	do_header(dev, hname, count > hicount ? count : hicount);
71 }
72 
73 do_header(dev, hname, count)
74 	char *dev, *hname;
75 	int count;
76 {
77 	char *file, *name, *inw, *toheader(), *tomacro();
78 	struct file_list *fl, *fl_head, *tflp;
79 	FILE *inf, *outf;
80 	int inc, oldcount;
81 
82 	file = toheader(hname);
83 	name = tomacro(dev);
84 	inf = fopen(file, "r");
85 	oldcount = -1;
86 	if (inf == 0) {
87 		outf = fopen(file, "w");
88 		if (outf == 0) {
89 			perror(file);
90 			exit(1);
91 		}
92 		fprintf(outf, "#define %s %d\n", name, count);
93 		(void) fclose(outf);
94 		return;
95 	}
96 	fl_head = NULL;
97 	for (;;) {
98 		char *cp;
99 		if ((inw = get_word(inf)) == 0 || inw == (char *)EOF)
100 			break;
101 		if ((inw = get_word(inf)) == 0 || inw == (char *)EOF)
102 			break;
103 		inw = ns(inw);
104 		cp = get_word(inf);
105 		if (cp == 0 || cp == (char *)EOF)
106 			break;
107 		inc = atoi(cp);
108 		if (eq(inw, name)) {
109 			oldcount = inc;
110 			inc = count;
111 		}
112 		cp = get_word(inf);
113 		if (cp == (char *)EOF)
114 			break;
115 		fl = (struct file_list *) malloc(sizeof *fl);
116 		bzero(fl, sizeof(*fl));
117 		fl->f_fn = inw;
118 		fl->f_type = inc;
119 		fl->f_next = fl_head;
120 		fl_head = fl;
121 	}
122 	(void) fclose(inf);
123 	if (count == oldcount) {
124 		for (fl = fl_head; fl != NULL; fl = tflp) {
125 			tflp = fl->f_next;
126 			free(fl);
127 		}
128 		return;
129 	}
130 	if (oldcount == -1) {
131 		fl = (struct file_list *) malloc(sizeof *fl);
132 		bzero(fl, sizeof(*fl));
133 		fl->f_fn = name;
134 		fl->f_type = count;
135 		fl->f_next = fl_head;
136 		fl_head = fl;
137 	}
138 	outf = fopen(file, "w");
139 	if (outf == 0) {
140 		perror(file);
141 		exit(1);
142 	}
143 	for (fl = fl_head; fl != NULL; fl = tflp) {
144 		fprintf(outf,
145 		    "#define %s %u\n", fl->f_fn, count ? fl->f_type : 0);
146 		tflp = fl->f_next;
147 		free(fl);
148 	}
149 	(void) fclose(outf);
150 }
151 
152 /*
153  * convert a dev name to a .h file name
154  */
155 char *
156 toheader(dev)
157 	char *dev;
158 {
159 	static char hbuf[80];
160 
161 	(void) strcpy(hbuf, path(dev));
162 	(void) strcat(hbuf, ".h");
163 	return (hbuf);
164 }
165 
166 /*
167  * convert a dev name to a macro name
168  */
169 char *tomacro(dev)
170 	register char *dev;
171 {
172 	static char mbuf[20];
173 	register char *cp;
174 
175 	cp = mbuf;
176 	*cp++ = 'N';
177 	while (*dev)
178 		*cp++ = islower(*dev) ? toupper(*dev++) : *dev++;
179 	*cp++ = 0;
180 	return (mbuf);
181 }
182