xref: /freebsd/usr.sbin/config/mkheaders.c (revision 729362425c09cf6b362366aabc6fb547eee8035a)
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 int do_header(char *, int);
55 static char *toheader(char *);
56 static char *tomacro(char *);
57 
58 void
59 headers(void)
60 {
61 	struct file_list *fl;
62 	struct device *dp;
63 	int match;
64 	int errors;
65 
66 	errors = 0;
67 	STAILQ_FOREACH(fl, &ftab, f_next) {
68 		if (fl->f_needs != 0) {
69 			match = 0;
70 			STAILQ_FOREACH(dp, &dtab, d_next) {
71 				if (eq(dp->d_name, fl->f_needs)) {
72 					match++;
73 					dp->d_done |= DEVDONE;
74 				}
75 			}
76 			if (fl->f_flags & NEED_COUNT)
77 				errors += do_header(fl->f_needs, match);
78 		}
79 	}
80 	STAILQ_FOREACH(dp, &dtab, d_next) {
81 		if (!(dp->d_done & DEVDONE)) {
82 			warnx("Error: device \"%s\" is unknown",
83 			       dp->d_name);
84 			       errors++;
85 			}
86 		if (dp->d_count == UNKNOWN)
87 			continue;
88 		match = 0;
89 		STAILQ_FOREACH(fl, &ftab, f_next) {
90 			if (fl->f_needs == 0)
91 				continue;
92 			if ((fl->f_flags & NEED_COUNT) == 0)
93 				continue;
94 			if (eq(dp->d_name, fl->f_needs)) {
95 				match++;
96 				break;
97 			}
98 		}
99 		if (match == 0) {
100 			warnx("Error: device \"%s\" does not take a count",
101 			    dp->d_name);
102 			errors++;
103 		}
104 	}
105 	if (errors)
106 		errx(1, "%d errors", errors);
107 }
108 
109 static int
110 do_header(char *dev, int match)
111 {
112 	char *file, *name, *inw;
113 	struct file_list *fl, *tflp;
114 	struct file_list_head fl_head;
115 	struct device *dp;
116 	FILE *inf, *outf;
117 	int inc, oldcount;
118 	int count, hicount;
119 	int errors;
120 
121 	/*
122 	 * After this loop, "count" will be the actual number of units,
123 	 * and "hicount" will be the highest unit declared.  do_header()
124 	 * must use this higher of these values.
125 	 */
126 	errors = 0;
127 	hicount = count = 0;
128 	STAILQ_FOREACH(dp, &dtab, d_next) {
129 		if (eq(dp->d_name, dev)) {
130 			if (dp->d_count == UNKNOWN) {
131 				warnx("Device \"%s\" requires a count", dev);
132 				return 1;
133 			}
134 			count = dp->d_count;
135 			break;
136 		}
137 	}
138 	file = toheader(dev);
139 	name = tomacro(dev);
140 	if (match)
141 		fprintf(stderr,
142 		    "FYI: static unit limits for %s are set: %s=%d\n",
143 		    dev, name, count);
144 	remember(file);
145 	inf = fopen(file, "r");
146 	oldcount = -1;
147 	if (inf == 0) {
148 		outf = fopen(file, "w");
149 		if (outf == 0)
150 			err(1, "%s", file);
151 		fprintf(outf, "#define %s %d\n", name, count);
152 		(void) fclose(outf);
153 		return 0;
154 	}
155 	STAILQ_INIT(&fl_head);
156 	for (;;) {
157 		char *cp;
158 		if ((inw = get_word(inf)) == 0 || inw == (char *)EOF)
159 			break;
160 		if ((inw = get_word(inf)) == 0 || inw == (char *)EOF)
161 			break;
162 		inw = ns(inw);
163 		cp = get_word(inf);
164 		if (cp == 0 || cp == (char *)EOF)
165 			break;
166 		inc = atoi(cp);
167 		if (eq(inw, name)) {
168 			oldcount = inc;
169 			inc = count;
170 		}
171 		cp = get_word(inf);
172 		if (cp == (char *)EOF)
173 			break;
174 		fl = (struct file_list *) malloc(sizeof *fl);
175 		bzero(fl, sizeof(*fl));
176 		fl->f_fn = inw;		/* malloced */
177 		fl->f_type = inc;
178 		STAILQ_INSERT_HEAD(&fl_head, fl, f_next);
179 	}
180 	(void) fclose(inf);
181 	if (count == oldcount) {
182 		for (fl = STAILQ_FIRST(&fl_head); fl != NULL; fl = tflp) {
183 			tflp = STAILQ_NEXT(fl, f_next);
184 			free(fl->f_fn);
185 			free(fl);
186 		}
187 		return 0;
188 	}
189 	if (oldcount == -1) {
190 		fl = (struct file_list *) malloc(sizeof *fl);
191 		bzero(fl, sizeof(*fl));
192 		fl->f_fn = ns(name);
193 		fl->f_type = count;
194 		STAILQ_INSERT_HEAD(&fl_head, fl, f_next);
195 	}
196 	outf = fopen(file, "w");
197 	if (outf == 0)
198 		err(1, "%s", file);
199 	for (fl = STAILQ_FIRST(&fl_head); fl != NULL; fl = tflp) {
200 		fprintf(outf,
201 		    "#define %s %u\n", fl->f_fn, count ? fl->f_type : 0);
202 		tflp = STAILQ_NEXT(fl, f_next);
203 		free(fl->f_fn);
204 		free(fl);
205 	}
206 	(void) fclose(outf);
207 	return 0;
208 }
209 
210 /*
211  * convert a dev name to a .h file name
212  */
213 static char *
214 toheader(char *dev)
215 {
216 	static char hbuf[MAXPATHLEN];
217 
218 	snprintf(hbuf, sizeof(hbuf), "%s.h", path(dev));
219 	return (hbuf);
220 }
221 
222 /*
223  * convert a dev name to a macro name
224  */
225 static char *
226 tomacro(char *dev)
227 {
228 	static char mbuf[20];
229 	char *cp;
230 
231 	cp = mbuf;
232 	*cp++ = 'N';
233 	while (*dev)
234 		*cp++ = islower(*dev) ? toupper(*dev++) : *dev++;
235 	*cp++ = 0;
236 	return (mbuf);
237 }
238