mkheaders.c revision 1.1 1 /* $NetBSD: mkheaders.c,v 1.1 2005/06/05 18:19:53 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This software was developed by the Computer Systems Engineering group
8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 * contributed to Berkeley.
10 *
11 * All advertising materials mentioning features or use of this software
12 * must display the following acknowledgement:
13 * This product includes software developed by the University of
14 * California, Lawrence Berkeley Laboratories.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * from: @(#)mkheaders.c 8.1 (Berkeley) 6/6/93
41 */
42
43 #if HAVE_NBTOOL_CONFIG_H
44 #include "nbtool_config.h"
45 #endif
46
47 #include <sys/param.h>
48 #include <ctype.h>
49 #include <errno.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <time.h>
54 #include "defs.h"
55
56 static int emitcnt(struct nvlist *);
57 static int emitlocs(void);
58 static int emitopts(void);
59 static int emitioconfh(void);
60 static int emittime(void);
61 static int herr(const char *, const char *, FILE *);
62 static int locators_print(const char *, void *, void *);
63 static int defopts_print(const char *, void *, void *);
64 static char *cntname(const char *);
65 static int fprintcnt(FILE *, struct nvlist *);
66 static int fprintstr(FILE *, const char *);
67
68 /*
69 * Make the various config-generated header files.
70 */
71 int
72 mkheaders(void)
73 {
74 struct files *fi;
75
76 /*
77 * Make headers containing counts, as needed.
78 */
79 TAILQ_FOREACH(fi, &allfiles, fi_next) {
80 if (fi->fi_flags & FI_HIDDEN)
81 continue;
82 if (fi->fi_flags & (FI_NEEDSCOUNT | FI_NEEDSFLAG) &&
83 emitcnt(fi->fi_optf))
84 return (1);
85 }
86
87 if (emitopts() || emitlocs() || emitioconfh() || emittime())
88 return (1);
89
90 return (0);
91 }
92
93
94 static int
95 fprintcnt(FILE *fp, struct nvlist *nv)
96 {
97 return (fprintf(fp, "#define\t%s\t%d\n",
98 cntname(nv->nv_name), nv->nv_int));
99 }
100
101 static int
102 emitcnt(struct nvlist *head)
103 {
104 char nfname[BUFSIZ], tfname[BUFSIZ];
105 struct nvlist *nv;
106 FILE *fp;
107
108 (void)snprintf(nfname, sizeof(nfname), "%s.h", head->nv_name);
109 (void)snprintf(tfname, sizeof(tfname), "tmp_%s", nfname);
110
111 if ((fp = fopen(tfname, "w")) == NULL)
112 return (herr("open", tfname, NULL));
113
114 for (nv = head; nv != NULL; nv = nv->nv_next)
115 if (fprintcnt(fp, nv) < 0)
116 return (herr("writ", tfname, fp));
117
118 if (fclose(fp) == EOF)
119 return (herr("clos", tfname, NULL));
120
121 return (moveifchanged(tfname, nfname));
122 }
123
124 /*
125 * Output a string, preceded by a tab and possibly unescaping any quotes.
126 * The argument will be output as is if it doesn't start with \".
127 * Otherwise the first backslash in a \? sequence will be dropped.
128 */
129 static int
130 fprintstr(FILE *fp, const char *str)
131 {
132 int n;
133
134 if (strncmp(str, "\\\"", 2))
135 return fprintf(fp, "\t%s", str);
136
137 if (fputc('\t', fp) < 0)
138 return -1;
139
140 for (n = 1; *str; str++, n++) {
141 switch (*str) {
142 case '\\':
143 if (!*++str) /* XXX */
144 str--;
145 default:
146 if (fputc(*str, fp) < 0)
147 return -1;
148 break;
149 }
150 }
151 return n;
152 }
153
154 /*
155 * Callback function for walking the option file hash table. We write out
156 * the options defined for this file.
157 */
158 static int
159 defopts_print(const char *name, void *value, void *arg)
160 {
161 char tfname[BUFSIZ];
162 struct nvlist *nv, *option;
163 int isfsoption;
164 FILE *fp;
165
166 (void)snprintf(tfname, sizeof(tfname), "tmp_%s", name);
167 if ((fp = fopen(tfname, "w")) == NULL)
168 return (herr("open", tfname, NULL));
169
170 for (nv = value; nv != NULL; nv = nv->nv_next) {
171 isfsoption = OPT_FSOPT(nv->nv_name);
172
173 if ((option = ht_lookup(opttab, nv->nv_name)) == NULL &&
174 (option = ht_lookup(fsopttab, nv->nv_name)) == NULL) {
175 if (fprintf(fp, "/* %s `%s' not defined */\n",
176 isfsoption ? "file system" : "option",
177 nv->nv_name) < 0)
178 goto bad;
179 } else {
180 if (fprintf(fp, "#define\t%s", option->nv_name) < 0)
181 goto bad;
182 if (option->nv_str != NULL && isfsoption == 0 &&
183 fprintstr(fp, option->nv_str) < 0)
184 goto bad;
185 if (fputc('\n', fp) < 0)
186 goto bad;
187 }
188 }
189
190 if (fclose(fp) == EOF)
191 return (herr("clos", tfname, NULL));
192
193 return (moveifchanged(tfname, name));
194
195 bad:
196 return (herr("writ", tfname, fp));
197 }
198
199 /*
200 * Emit the option header files.
201 */
202 static int
203 emitopts(void)
204 {
205
206 return (ht_enumerate(optfiletab, defopts_print, NULL));
207 }
208
209 /*
210 * A callback function for walking the attribute hash table.
211 * Emit CPP definitions of manifest constants for the locators on the
212 * "name" attribute node (passed as the "value" parameter).
213 */
214 static int
215 locators_print(const char *name, void *value, void *arg)
216 {
217 struct attr *a;
218 struct nvlist *nv;
219 int i;
220 char *locdup, *namedup;
221 char *cp;
222 FILE *fp = arg;
223
224 a = value;
225 if (a->a_locs) {
226 if (strchr(name, ' ') != NULL || strchr(name, '\t') != NULL)
227 /*
228 * name contains a space; we can't generate
229 * usable defines, so ignore it.
230 */
231 return 0;
232 locdup = estrdup(name);
233 for (cp = locdup; *cp; cp++)
234 if (islower((unsigned char)*cp))
235 *cp = toupper((unsigned char)*cp);
236 if (fprintf(fp, "extern const char *%scf_locnames[];\n",
237 name) < 0)
238 return 1;
239 for (i = 0, nv = a->a_locs; nv; nv = nv->nv_next, i++) {
240 if (strchr(nv->nv_name, ' ') != NULL ||
241 strchr(nv->nv_name, '\t') != NULL)
242 /*
243 * name contains a space; we can't generate
244 * usable defines, so ignore it.
245 */
246 continue;
247 namedup = estrdup(nv->nv_name);
248 for (cp = namedup; *cp; cp++)
249 if (islower((unsigned char)*cp))
250 *cp = toupper((unsigned char)*cp);
251 else if (*cp == ARRCHR)
252 *cp = '_';
253 if (fprintf(fp, "#define %sCF_%s %d\n",
254 locdup, namedup, i) < 0)
255 return 1;
256 if (nv->nv_str &&
257 fprintf(fp, "#define %sCF_%s_DEFAULT %s\n",
258 locdup, namedup, nv->nv_str) < 0)
259 return 1;
260 free(namedup);
261 }
262 free(locdup);
263 }
264 return 0;
265 }
266
267 /*
268 * Build the "locators.h" file with manifest constants for all potential
269 * locators in the configuration. Do this by enumerating the attribute
270 * hash table and emitting all the locators for each attribute.
271 */
272 static int
273 emitlocs(void)
274 {
275 char *tfname;
276 int rval;
277 FILE *tfp;
278
279 tfname = "tmp_locators.h";
280 if ((tfp = fopen(tfname, "w")) == NULL)
281 return (herr("open", tfname, NULL));
282
283 rval = ht_enumerate(attrtab, locators_print, tfp);
284 if (fclose(tfp) == EOF)
285 return (herr("clos", tfname, NULL));
286 if (rval)
287 return (rval);
288 return (moveifchanged(tfname, "locators.h"));
289 }
290
291 /*
292 * Build the "ioconf.h" file with extern declarations for all configured
293 * cfdrivers.
294 */
295 static int
296 emitioconfh(void)
297 {
298 const char *tfname;
299 FILE *tfp;
300 struct devbase *d;
301
302 tfname = "tmp_ioconf.h";
303 if ((tfp = fopen(tfname, "w")) == NULL)
304 return (herr("open", tfname, NULL));
305
306 TAILQ_FOREACH(d, &allbases, d_next) {
307 if (!devbase_has_instances(d, WILD))
308 continue;
309 if (fprintf(tfp, "extern struct cfdriver %s_cd;\n",
310 d->d_name) < 0)
311 return (1);
312 }
313
314 if (fclose(tfp) == EOF)
315 return (herr("clos", tfname, NULL));
316
317 return (moveifchanged(tfname, "ioconf.h"));
318 }
319
320 /*
321 * Make a file that config_time.h can use as a source, if required.
322 */
323 static int
324 emittime(void)
325 {
326 FILE *fp;
327 time_t t;
328 struct tm *tm;
329 char buf[128];
330
331 t = time(NULL);
332 tm = gmtime(&t);
333
334 if ((fp = fopen("config_time.src", "w")) == NULL)
335 return (herr("open", "config_time.src", NULL));
336
337 if (strftime(buf, sizeof(buf), "%c %Z", tm) == 0)
338 return (herr("strftime", "config_time.src", NULL));
339
340 if (fprintf(fp, "/* %s */\n"
341 "#define CONFIG_TIME\t%2lld\n"
342 "#define CONFIG_YEAR\t%2d\n"
343 "#define CONFIG_MONTH\t%2d\n"
344 "#define CONFIG_DATE\t%2d\n"
345 "#define CONFIG_HOUR\t%2d\n"
346 "#define CONFIG_MINS\t%2d\n"
347 "#define CONFIG_SECS\t%2d\n",
348 buf, (long long)t,
349 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
350 tm->tm_hour, tm->tm_min, tm->tm_sec) < 0)
351 return (herr("fprintf", "config_time.src", NULL));
352
353 if (fclose(fp) != 0)
354 return (herr("close", "config_time.src", NULL));
355
356 /*
357 * *Don't* moveifchanged this file. Makefile.kern.inc will
358 * handle that if it determines such a move is necessary.
359 */
360 return (0);
361 }
362
363 /*
364 * Compare two files. If nfname doesn't exist, or is different from
365 * tfname, move tfname to nfname. Otherwise, delete tfname.
366 */
367 int
368 moveifchanged(const char *tfname, const char *nfname)
369 {
370 char tbuf[BUFSIZ], nbuf[BUFSIZ];
371 FILE *tfp, *nfp;
372
373 if ((tfp = fopen(tfname, "r")) == NULL)
374 return (herr("open", tfname, NULL));
375
376 if ((nfp = fopen(nfname, "r")) == NULL)
377 goto moveit;
378
379 while (fgets(tbuf, sizeof(tbuf), tfp) != NULL) {
380 if (fgets(nbuf, sizeof(nbuf), nfp) == NULL) {
381 /*
382 * Old file has fewer lines.
383 */
384 goto moveit;
385 }
386 if (strcmp(tbuf, nbuf) != 0)
387 goto moveit;
388 }
389
390 /*
391 * We've reached the end of the new file. Check to see if new file
392 * has fewer lines than old.
393 */
394 if (fgets(nbuf, sizeof(nbuf), nfp) != NULL) {
395 /*
396 * New file has fewer lines.
397 */
398 goto moveit;
399 }
400
401 (void) fclose(nfp);
402 (void) fclose(tfp);
403 if (remove(tfname) == -1)
404 return(herr("remov", tfname, NULL));
405 return (0);
406
407 moveit:
408 /*
409 * They're different, or the file doesn't exist.
410 */
411 if (nfp)
412 (void) fclose(nfp);
413 if (tfp)
414 (void) fclose(tfp);
415 if (rename(tfname, nfname) == -1)
416 return (herr("renam", tfname, NULL));
417 return (0);
418 }
419
420 static int
421 herr(const char *what, const char *fname, FILE *fp)
422 {
423
424 (void)fprintf(stderr, "%s: error %sing %s: %s\n",
425 getprogname(), what, fname, strerror(errno));
426 if (fp)
427 (void)fclose(fp);
428 return (1);
429 }
430
431 static char *
432 cntname(const char *src)
433 {
434 char *dst;
435 unsigned char c;
436 static char buf[100];
437
438 dst = buf;
439 *dst++ = 'N';
440 while ((c = *src++) != 0)
441 *dst++ = islower(c) ? toupper(c) : c;
442 *dst = 0;
443 return (buf);
444 }
445