xref: /freebsd/usr.sbin/config/mkheaders.c (revision 0de89efe5c443f213c7ea28773ef2dc6cf3af2ed)
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 	"$Id$";
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 "config.h"
51 #include "y.tab.h"
52 
53 #define ns(s) strdup(s)
54 
55 void do_header __P((char *, char *, int));
56 void do_count __P((char *, char *, int));
57 
58 void
59 headers()
60 {
61 	register struct file_list *fl;
62 
63 	for (fl = ftab; fl != 0; fl = fl->f_next)
64 		if (fl->f_needs != 0)
65 			do_count(fl->f_needs, fl->f_needs, 1);
66 }
67 
68 /*
69  * count all the devices of a certain type and recurse to count
70  * whatever the device is connected to
71  */
72 void
73 do_count(dev, hname, search)
74 	register char *dev, *hname;
75 	int search;
76 {
77 	register struct device *dp, *mp;
78 	register int count, hicount;
79 
80 	/*
81 	 * After this loop, "count" will be the actual number of units,
82 	 * and "hicount" will be the highest unit declared.  do_header()
83 	 * must use this higher of these values.
84 	 */
85 	for (hicount = count = 0, dp = dtab; dp != 0; dp = dp->d_next)
86 		if (dp->d_unit != -1 && eq(dp->d_name, dev)) {
87 			if (dp->d_type == PSEUDO_DEVICE) {
88 				count =
89 				    dp->d_slave != UNKNOWN ? dp->d_slave : 1;
90 				break;
91 			}
92 			count++;
93 			/*
94 			 * Allow holes in unit numbering,
95 			 * assumption is unit numbering starts
96 			 * at zero.
97 			 */
98 			if (dp->d_unit + 1 > hicount)
99 				hicount = dp->d_unit + 1;
100 			if (search) {
101 				mp = dp->d_conn;
102 				if (mp != 0 && mp != TO_NEXUS &&
103 				    mp->d_conn != 0 && mp->d_conn != TO_NEXUS) {
104 					do_count(mp->d_name, hname, 0);
105 					search = 0;
106 				}
107 			}
108 		}
109 	do_header(dev, hname, count > hicount ? count : hicount);
110 }
111 
112 void
113 do_header(dev, hname, count)
114 	char *dev, *hname;
115 	int count;
116 {
117 	char *file, *name, *inw, *toheader(), *tomacro();
118 	struct file_list *fl, *fl_head, *tflp;
119 	FILE *inf, *outf;
120 	int inc, oldcount;
121 
122 	file = toheader(hname);
123 	name = tomacro(dev);
124 	inf = fopen(file, "r");
125 	oldcount = -1;
126 	if (inf == 0) {
127 		outf = fopen(file, "w");
128 		if (outf == 0)
129 			err(1, "%s", file);
130 		fprintf(outf, "#define %s %d\n", name, count);
131 		(void) fclose(outf);
132 		return;
133 	}
134 	fl_head = NULL;
135 	for (;;) {
136 		char *cp;
137 		if ((inw = get_word(inf)) == 0 || inw == (char *)EOF)
138 			break;
139 		if ((inw = get_word(inf)) == 0 || inw == (char *)EOF)
140 			break;
141 		inw = ns(inw);
142 		cp = get_word(inf);
143 		if (cp == 0 || cp == (char *)EOF)
144 			break;
145 		inc = atoi(cp);
146 		if (eq(inw, name)) {
147 			oldcount = inc;
148 			inc = count;
149 		}
150 		cp = get_word(inf);
151 		if (cp == (char *)EOF)
152 			break;
153 		fl = (struct file_list *) malloc(sizeof *fl);
154 		bzero(fl, sizeof(*fl));
155 		fl->f_fn = inw;		/* malloced */
156 		fl->f_type = inc;
157 		fl->f_next = fl_head;
158 		fl_head = fl;
159 	}
160 	(void) fclose(inf);
161 	if (count == oldcount) {
162 		for (fl = fl_head; fl != NULL; fl = tflp) {
163 			tflp = fl->f_next;
164 			free(fl->f_fn);
165 			free(fl);
166 		}
167 		return;
168 	}
169 	if (oldcount == -1) {
170 		fl = (struct file_list *) malloc(sizeof *fl);
171 		bzero(fl, sizeof(*fl));
172 		fl->f_fn = ns(name);
173 		fl->f_type = count;
174 		fl->f_next = fl_head;
175 		fl_head = fl;
176 	}
177 	outf = fopen(file, "w");
178 	if (outf == 0)
179 		err(1, "%s", file);
180 	for (fl = fl_head; fl != NULL; fl = tflp) {
181 		fprintf(outf,
182 		    "#define %s %u\n", fl->f_fn, count ? fl->f_type : 0);
183 		tflp = fl->f_next;
184 		free(fl->f_fn);
185 		free(fl);
186 	}
187 	(void) fclose(outf);
188 }
189 
190 /*
191  * convert a dev name to a .h file name
192  */
193 char *
194 toheader(dev)
195 	char *dev;
196 {
197 	static char hbuf[80];
198 
199 	(void) strcpy(hbuf, path(dev));
200 	(void) strcat(hbuf, ".h");
201 	return (hbuf);
202 }
203 
204 /*
205  * convert a dev name to a macro name
206  */
207 char *tomacro(dev)
208 	register char *dev;
209 {
210 	static char mbuf[20];
211 	register char *cp;
212 
213 	cp = mbuf;
214 	*cp++ = 'N';
215 	while (*dev)
216 		*cp++ = islower(*dev) ? toupper(*dev++) : *dev++;
217 	*cp++ = 0;
218 	return (mbuf);
219 }
220