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