xref: /dflybsd-src/usr.sbin/config/mkheaders.c (revision 984263bcb83ad82313113c6ac840d99124d8f90c)
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 #if 0
36 static char sccsid[] = "@(#)mkheaders.c	8.1 (Berkeley) 6/6/93";
37 #endif
38 static const char rcsid[] =
39   "$FreeBSD: src/usr.sbin/config/mkheaders.c,v 1.14.2.2 2001/01/23 00:09:32 peter Exp $";
40 #endif /* not lint */
41 
42 /*
43  * Make all the .h files for the optional entries
44  */
45 
46 #include <ctype.h>
47 #include <err.h>
48 #include <stdio.h>
49 #include <string.h>
50 #include <sys/param.h>
51 #include "config.h"
52 #include "y.tab.h"
53 
54 static void do_header __P((char *, char *, int));
55 static void do_count __P((char *, char *, int));
56 static char *toheader __P((char *));
57 static char *tomacro __P((char *));
58 
59 void
60 headers()
61 {
62 	register struct file_list *fl;
63 	struct device *dp;
64 
65 	for (fl = ftab; fl != 0; fl = fl->f_next)
66 		if (fl->f_needs != 0)
67 			do_count(fl->f_needs, fl->f_needs, 1);
68 	for (dp = dtab; dp != 0; dp = dp->d_next) {
69 		if ((dp->d_type & TYPEMASK) == PSEUDO_DEVICE) {
70 			if (!(dp->d_type & DEVDONE)) {
71 				printf("Warning: pseudo-device \"%s\" is unknown\n",
72 				       dp->d_name);
73 				exit(1);
74 			}
75 		}
76 		if ((dp->d_type & TYPEMASK) == DEVICE) {
77 			if (!(dp->d_type & DEVDONE)) {
78 				printf("Warning: device \"%s\" is unknown\n",
79 				       dp->d_name);
80 				exit(1);
81 			}
82 		}
83 	}
84 }
85 
86 /*
87  * count all the devices of a certain type and recurse to count
88  * whatever the device is connected to
89  */
90 static void
91 do_count(dev, hname, search)
92 	register char *dev, *hname;
93 	int search;
94 {
95 	register struct device *dp;
96 	register int count, hicount;
97 	char *mp;
98 
99 	/*
100 	 * After this loop, "count" will be the actual number of units,
101 	 * and "hicount" will be the highest unit declared.  do_header()
102 	 * must use this higher of these values.
103 	 */
104 	for (dp = dtab; dp != 0; dp = dp->d_next) {
105 		if (eq(dp->d_name, dev)) {
106 			if ((dp->d_type & TYPEMASK) == PSEUDO_DEVICE)
107 				dp->d_type |= DEVDONE;
108 			else if ((dp->d_type & TYPEMASK) == DEVICE)
109 				dp->d_type |= DEVDONE;
110 		}
111 	}
112 	for (hicount = count = 0, dp = dtab; dp != 0; dp = dp->d_next) {
113 		if (dp->d_unit != -1 && eq(dp->d_name, dev)) {
114 			if ((dp->d_type & TYPEMASK) == PSEUDO_DEVICE) {
115 				count =
116 				    dp->d_count != UNKNOWN ? dp->d_count : 1;
117 				break;
118 			}
119 			count++;
120 			/*
121 			 * Allow holes in unit numbering,
122 			 * assumption is unit numbering starts
123 			 * at zero.
124 			 */
125 			if (dp->d_unit + 1 > hicount)
126 				hicount = dp->d_unit + 1;
127 			if (search) {
128 				mp = dp->d_conn;
129 				if (mp != 0 && dp->d_connunit < 0)
130 					mp = 0;
131 				if (mp != 0 && eq(mp, "nexus"))
132 					mp = 0;
133 				if (mp != 0) {
134 					do_count(mp, hname, 0);
135 					search = 0;
136 				}
137 			}
138 		}
139 	}
140 	do_header(dev, hname, count > hicount ? count : hicount);
141 }
142 
143 static void
144 do_header(dev, hname, count)
145 	char *dev, *hname;
146 	int count;
147 {
148 	char *file, *name, *inw;
149 	struct file_list *fl, *fl_head, *tflp;
150 	FILE *inf, *outf;
151 	int inc, oldcount;
152 
153 	file = toheader(hname);
154 	name = tomacro(dev);
155 	inf = fopen(file, "r");
156 	oldcount = -1;
157 	if (inf == 0) {
158 		outf = fopen(file, "w");
159 		if (outf == 0)
160 			err(1, "%s", file);
161 		fprintf(outf, "#define %s %d\n", name, count);
162 		(void) fclose(outf);
163 		return;
164 	}
165 	fl_head = NULL;
166 	for (;;) {
167 		char *cp;
168 		if ((inw = get_word(inf)) == 0 || inw == (char *)EOF)
169 			break;
170 		if ((inw = get_word(inf)) == 0 || inw == (char *)EOF)
171 			break;
172 		inw = ns(inw);
173 		cp = get_word(inf);
174 		if (cp == 0 || cp == (char *)EOF)
175 			break;
176 		inc = atoi(cp);
177 		if (eq(inw, name)) {
178 			oldcount = inc;
179 			inc = count;
180 		}
181 		cp = get_word(inf);
182 		if (cp == (char *)EOF)
183 			break;
184 		fl = (struct file_list *) malloc(sizeof *fl);
185 		bzero(fl, sizeof(*fl));
186 		fl->f_fn = inw;		/* malloced */
187 		fl->f_type = inc;
188 		fl->f_next = fl_head;
189 		fl_head = fl;
190 	}
191 	(void) fclose(inf);
192 	if (count == oldcount) {
193 		for (fl = fl_head; fl != NULL; fl = tflp) {
194 			tflp = fl->f_next;
195 			free(fl->f_fn);
196 			free(fl);
197 		}
198 		return;
199 	}
200 	if (oldcount == -1) {
201 		fl = (struct file_list *) malloc(sizeof *fl);
202 		bzero(fl, sizeof(*fl));
203 		fl->f_fn = ns(name);
204 		fl->f_type = count;
205 		fl->f_next = fl_head;
206 		fl_head = fl;
207 	}
208 	outf = fopen(file, "w");
209 	if (outf == 0)
210 		err(1, "%s", file);
211 	for (fl = fl_head; fl != NULL; fl = tflp) {
212 		fprintf(outf,
213 		    "#define %s %u\n", fl->f_fn, count ? fl->f_type : 0);
214 		tflp = fl->f_next;
215 		free(fl->f_fn);
216 		free(fl);
217 	}
218 	(void) fclose(outf);
219 }
220 
221 /*
222  * convert a dev name to a .h file name
223  */
224 static char *
225 toheader(dev)
226 	char *dev;
227 {
228 	static char hbuf[MAXPATHLEN];
229 
230 	snprintf(hbuf, sizeof(hbuf), "%s.h", path(dev));
231 	return (hbuf);
232 }
233 
234 /*
235  * convert a dev name to a macro name
236  */
237 static char *
238 tomacro(dev)
239 	register char *dev;
240 {
241 	static char mbuf[20];
242 	register char *cp;
243 
244 	cp = mbuf;
245 	*cp++ = 'N';
246 	while (*dev)
247 		*cp++ = islower(*dev) ? toupper(*dev++) : *dev++;
248 	*cp++ = 0;
249 	return (mbuf);
250 }
251