xref: /freebsd/usr.sbin/config/mkheaders.c (revision 17ee9d00bc1ae1e598c38f25826f861e4bc6c3ce)
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
35 static 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 <ctype.h>
44 #include "config.h"
45 #include "y.tab.h"
46 
47 headers()
48 {
49 	register struct file_list *fl;
50 
51 	for (fl = ftab; fl != 0; fl = fl->f_next)
52 		if (fl->f_needs != 0)
53 			do_count(fl->f_needs, fl->f_needs, 1);
54 }
55 
56 /*
57  * count all the devices of a certain type and recurse to count
58  * whatever the device is connected to
59  */
60 do_count(dev, hname, search)
61 	register char *dev, *hname;
62 	int search;
63 {
64 	register struct device *dp, *mp;
65 	register int count, hicount;
66 
67 	/*
68 	 * After this loop, "count" will be the actual number of units,
69 	 * and "hicount" will be the highest unit declared.  do_header()
70 	 * must use this higher of these values.
71 	 */
72 	for (hicount = count = 0, dp = dtab; dp != 0; dp = dp->d_next)
73 		if (dp->d_unit != -1 && eq(dp->d_name, dev)) {
74 			if (dp->d_type == PSEUDO_DEVICE) {
75 				count =
76 				    dp->d_slave != UNKNOWN ? dp->d_slave : 1;
77 				break;
78 			}
79 			count++;
80 			/*
81 			 * Allow holes in unit numbering,
82 			 * assumption is unit numbering starts
83 			 * at zero.
84 			 */
85 			if (dp->d_unit + 1 > hicount)
86 				hicount = dp->d_unit + 1;
87 			if (search) {
88 				mp = dp->d_conn;
89 				if (mp != 0 && mp != TO_NEXUS &&
90 				    mp->d_conn != 0 && mp->d_conn != TO_NEXUS) {
91 					do_count(mp->d_name, hname, 0);
92 					search = 0;
93 				}
94 			}
95 		}
96 	do_header(dev, hname, count > hicount ? count : hicount);
97 }
98 
99 do_header(dev, hname, count)
100 	char *dev, *hname;
101 	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  */
181 char *
182 toheader(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  */
195 char *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