mkheaders.c revision 1.8 1 /* $NetBSD: mkheaders.c,v 1.8 2006/09/04 18:42:14 christos 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 <util.h>
55 #include "defs.h"
56
57 #ifdef notyet
58 #include <crc_extern.h>
59 #else
60 #define fprint_global(fp, name, value) 1
61 #endif
62
63 static int emitcnt(struct nvlist *);
64 static int emitlocs(void);
65 static int emitopts(void);
66 static int emitioconfh(void);
67 static int emittime(void);
68 static int herr(const char *, const char *, FILE *);
69 static int locators_print(const char *, void *, void *);
70 static int defopts_print(const char *, void *, void *);
71 static char *cntname(const char *);
72 static int fprintcnt(FILE *, struct nvlist *);
73 static int fprintstr(FILE *, const char *);
74
75 #define UNDEFINED ~0u
76
77 /*
78 * Make the various config-generated header files.
79 */
80 int
81 mkheaders(void)
82 {
83 struct files *fi;
84
85 /*
86 * Make headers containing counts, as needed.
87 */
88 TAILQ_FOREACH(fi, &allfiles, fi_next) {
89 if (fi->fi_flags & FI_HIDDEN)
90 continue;
91 if (fi->fi_flags & (FI_NEEDSCOUNT | FI_NEEDSFLAG) &&
92 emitcnt(fi->fi_optf))
93 return (1);
94 }
95
96 if (emitopts() || emitlocs() || emitioconfh() || emittime())
97 return (1);
98
99 return (0);
100 }
101
102 #ifdef notyet
103 static int
104 fprint_global(FILE *fp, const char *name, unsigned int value)
105 {
106 return fprintf(fp, "#ifdef _LOCORE\n"
107 " .global _KERNEL_OPT_%s\n"
108 " .set _KERNEL_OPT_%s,0x%x\n"
109 "#else\n"
110 "__asm(\" .global _KERNEL_OPT_%s\\n"
111 " .set _KERNEL_OPT_%s,0x%x\");\n"
112 "#endif\n",
113 name, name, value,
114 name, name, value);
115 }
116
117 /* Convert the option argument to a 32bit numder */
118 static unsigned int
119 global_hash(const char *str)
120 {
121 unsigned int h;
122 char *ep;
123
124 /* If the value is a valid numeric, just use it */
125 h = strtoul(str, &ep, 0);
126 if (*ep != 0)
127 /* Otherwise shove through a 32bit CRC function */
128 h = crc_buf(0, str, strlen(str));
129
130 /* Avoid colliding with the value used for undefined options. */
131 /* At least until I stop any options being set to zero */
132 return h != UNDEFINED ? h : 0xcafebabe;
133 }
134 #endif
135
136 static int
137 fprintcnt(FILE *fp, struct nvlist *nv)
138 {
139 const char *name = cntname(nv->nv_name);
140
141 if (fprintf(fp, "#define\t%s\t%d\n", name, nv->nv_int) < 0)
142 return -1;
143 if (fprint_global(fp, name, nv->nv_int) < 0)
144 return -1;
145 return 0;
146 }
147
148 static int
149 emitcnt(struct nvlist *head)
150 {
151 char nfname[BUFSIZ], tfname[BUFSIZ];
152 struct nvlist *nv;
153 FILE *fp;
154
155 (void)snprintf(nfname, sizeof(nfname), "%s.h", head->nv_name);
156 (void)snprintf(tfname, sizeof(tfname), "tmp_%s", nfname);
157
158 if ((fp = fopen(tfname, "w")) == NULL)
159 return (herr("open", tfname, NULL));
160
161 for (nv = head; nv != NULL; nv = nv->nv_next)
162 if (fprintcnt(fp, nv) < 0)
163 return (herr("writ", tfname, fp));
164
165 if (fclose(fp) == EOF)
166 return (herr("clos", tfname, NULL));
167
168 return (moveifchanged(tfname, nfname));
169 }
170
171 /*
172 * Output a string, preceded by a tab and possibly unescaping any quotes.
173 * The argument will be output as is if it doesn't start with \".
174 * Otherwise the first backslash in a \? sequence will be dropped.
175 */
176 static int
177 fprintstr(FILE *fp, const char *str)
178 {
179 int n;
180
181 if (strncmp(str, "\\\"", 2))
182 return fprintf(fp, "\t%s", str);
183
184 if (fputc('\t', fp) < 0)
185 return -1;
186
187 for (n = 1; *str; str++, n++) {
188 switch (*str) {
189 case '\\':
190 if (!*++str) /* XXX */
191 str--;
192 default:
193 if (fputc(*str, fp) < 0)
194 return -1;
195 break;
196 }
197 }
198 return n;
199 }
200
201 /*
202 * Callback function for walking the option file hash table. We write out
203 * the options defined for this file.
204 */
205 static int
206 defopts_print(const char *name, void *value, void *arg)
207 {
208 char tfname[BUFSIZ];
209 struct nvlist *nv, *option;
210 const char *opt_value;
211 int isfsoption;
212 FILE *fp;
213
214 (void)snprintf(tfname, sizeof(tfname), "tmp_%s", name);
215 if ((fp = fopen(tfname, "w")) == NULL)
216 return (herr("open", tfname, NULL));
217
218 for (nv = value; nv != NULL; nv = nv->nv_next) {
219 isfsoption = OPT_FSOPT(nv->nv_name);
220
221 if (nv->nv_flags & NV_OBSOLETE) {
222 if (fprintf(fp, "/* %s `%s' is obsolete */\n",
223 isfsoption ? "file system" : "option",
224 nv->nv_name) < 0)
225 goto bad;
226 if (fprint_global(fp, nv->nv_name, 0xdeadbeef) < 0)
227 goto bad;
228 continue;
229 }
230
231 if (((option = ht_lookup(opttab, nv->nv_name)) == NULL &&
232 (option = ht_lookup(fsopttab, nv->nv_name)) == NULL) &&
233 (nv->nv_str == NULL)) {
234 if (fprintf(fp, "/* %s `%s' not defined */\n",
235 isfsoption ? "file system" : "option",
236 nv->nv_name) < 0)
237 goto bad;
238 if (fprint_global(fp, nv->nv_name, UNDEFINED) < 0)
239 goto bad;
240 continue;
241 }
242
243 opt_value = option != NULL ? option->nv_str : nv->nv_str;
244 if (isfsoption == 1)
245 /* For filesysteme we'd output the lower case name */
246 opt_value = NULL;
247
248 if (fprintf(fp, "#define\t%s", nv->nv_name) < 0)
249 goto bad;
250 if (opt_value != NULL && fprintstr(fp, opt_value) < 0)
251 goto bad;
252 if (fputc('\n', fp) < 0)
253 goto bad;
254 if (fprint_global(fp, nv->nv_name,
255 opt_value == NULL ? 1 : global_hash(opt_value)) < 0)
256 goto bad;
257 }
258
259 if (fclose(fp) == EOF)
260 return (herr("clos", tfname, NULL));
261
262 return (moveifchanged(tfname, name));
263
264 bad:
265 return (herr("writ", tfname, fp));
266 }
267
268 /*
269 * Emit the option header files.
270 */
271 static int
272 emitopts(void)
273 {
274
275 return (ht_enumerate(optfiletab, defopts_print, NULL));
276 }
277
278 /*
279 * A callback function for walking the attribute hash table.
280 * Emit CPP definitions of manifest constants for the locators on the
281 * "name" attribute node (passed as the "value" parameter).
282 */
283 static int
284 locators_print(const char *name, void *value, void *arg)
285 {
286 struct attr *a;
287 struct nvlist *nv;
288 int i;
289 char *locdup, *namedup;
290 char *cp;
291 FILE *fp = arg;
292
293 a = value;
294 if (a->a_locs) {
295 if (strchr(name, ' ') != NULL || strchr(name, '\t') != NULL)
296 /*
297 * name contains a space; we can't generate
298 * usable defines, so ignore it.
299 */
300 return 0;
301 locdup = estrdup(name);
302 for (cp = locdup; *cp; cp++)
303 if (islower((unsigned char)*cp))
304 *cp = toupper((unsigned char)*cp);
305 for (i = 0, nv = a->a_locs; nv; nv = nv->nv_next, i++) {
306 if (strchr(nv->nv_name, ' ') != NULL ||
307 strchr(nv->nv_name, '\t') != NULL)
308 /*
309 * name contains a space; we can't generate
310 * usable defines, so ignore it.
311 */
312 continue;
313 namedup = estrdup(nv->nv_name);
314 for (cp = namedup; *cp; cp++)
315 if (islower((unsigned char)*cp))
316 *cp = toupper((unsigned char)*cp);
317 else if (*cp == ARRCHR)
318 *cp = '_';
319 if (fprintf(fp, "#define %sCF_%s %d\n",
320 locdup, namedup, i) < 0)
321 goto bad;
322 if (nv->nv_str &&
323 fprintf(fp, "#define %sCF_%s_DEFAULT %s\n",
324 locdup, namedup, nv->nv_str) < 0)
325 goto bad;
326 free(namedup);
327 }
328 /* assert(i == a->a_loclen) */
329 if (fprintf(fp, "#define %sCF_NLOCS %d\n",
330 locdup, a->a_loclen) < 0)
331 goto bad1;
332 free(locdup);
333 }
334 return 0;
335 bad:
336 free(namedup);
337 bad1:
338 free(locdup);
339 return 1;
340 }
341
342 /*
343 * Build the "locators.h" file with manifest constants for all potential
344 * locators in the configuration. Do this by enumerating the attribute
345 * hash table and emitting all the locators for each attribute.
346 */
347 static int
348 emitlocs(void)
349 {
350 char *tfname;
351 int rval;
352 FILE *tfp;
353
354 tfname = "tmp_locators.h";
355 if ((tfp = fopen(tfname, "w")) == NULL)
356 return (herr("open", tfname, NULL));
357
358 rval = ht_enumerate(attrtab, locators_print, tfp);
359 if (fclose(tfp) == EOF)
360 return (herr("clos", tfname, NULL));
361 if (rval)
362 return (rval);
363 return (moveifchanged(tfname, "locators.h"));
364 }
365
366 /*
367 * Build the "ioconf.h" file with extern declarations for all configured
368 * cfdrivers.
369 */
370 static int
371 emitioconfh(void)
372 {
373 const char *tfname;
374 FILE *tfp;
375 struct devbase *d;
376
377 tfname = "tmp_ioconf.h";
378 if ((tfp = fopen(tfname, "w")) == NULL)
379 return (herr("open", tfname, NULL));
380
381 TAILQ_FOREACH(d, &allbases, d_next) {
382 if (!devbase_has_instances(d, WILD))
383 continue;
384 if (fprintf(tfp, "extern struct cfdriver %s_cd;\n",
385 d->d_name) < 0) {
386 (void)fclose(tfp);
387 return (1);
388 }
389 }
390
391 if (fclose(tfp) == EOF)
392 return (herr("clos", tfname, NULL));
393
394 return (moveifchanged(tfname, "ioconf.h"));
395 }
396
397 /*
398 * Make a file that config_time.h can use as a source, if required.
399 */
400 static int
401 emittime(void)
402 {
403 FILE *fp;
404 time_t t;
405 struct tm *tm;
406 char buf[128];
407
408 t = time(NULL);
409 tm = gmtime(&t);
410
411 if ((fp = fopen("config_time.src", "w")) == NULL)
412 return (herr("open", "config_time.src", NULL));
413
414 if (strftime(buf, sizeof(buf), "%c %Z", tm) == 0)
415 return (herr("strftime", "config_time.src", fp));
416
417 if (fprintf(fp, "/* %s */\n"
418 "#define CONFIG_TIME\t%2lld\n"
419 "#define CONFIG_YEAR\t%2d\n"
420 "#define CONFIG_MONTH\t%2d\n"
421 "#define CONFIG_DATE\t%2d\n"
422 "#define CONFIG_HOUR\t%2d\n"
423 "#define CONFIG_MINS\t%2d\n"
424 "#define CONFIG_SECS\t%2d\n",
425 buf, (long long)t,
426 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
427 tm->tm_hour, tm->tm_min, tm->tm_sec) < 0)
428 return (herr("fprintf", "config_time.src", fp));
429
430 if (fclose(fp) != 0)
431 return (herr("close", "config_time.src", NULL));
432
433 /*
434 * *Don't* moveifchanged this file. Makefile.kern.inc will
435 * handle that if it determines such a move is necessary.
436 */
437 return (0);
438 }
439
440 /*
441 * Compare two files. If nfname doesn't exist, or is different from
442 * tfname, move tfname to nfname. Otherwise, delete tfname.
443 */
444 int
445 moveifchanged(const char *tfname, const char *nfname)
446 {
447 char tbuf[BUFSIZ], nbuf[BUFSIZ];
448 FILE *tfp, *nfp;
449
450 if ((tfp = fopen(tfname, "r")) == NULL)
451 return (herr("open", tfname, NULL));
452
453 if ((nfp = fopen(nfname, "r")) == NULL)
454 goto moveit;
455
456 while (fgets(tbuf, sizeof(tbuf), tfp) != NULL) {
457 if (fgets(nbuf, sizeof(nbuf), nfp) == NULL) {
458 /*
459 * Old file has fewer lines.
460 */
461 goto moveit;
462 }
463 if (strcmp(tbuf, nbuf) != 0)
464 goto moveit;
465 }
466
467 /*
468 * We've reached the end of the new file. Check to see if new file
469 * has fewer lines than old.
470 */
471 if (fgets(nbuf, sizeof(nbuf), nfp) != NULL) {
472 /*
473 * New file has fewer lines.
474 */
475 goto moveit;
476 }
477
478 (void) fclose(nfp);
479 (void) fclose(tfp);
480 if (remove(tfname) == -1)
481 return(herr("remov", tfname, NULL));
482 return (0);
483
484 moveit:
485 /*
486 * They're different, or the file doesn't exist.
487 */
488 if (nfp)
489 (void) fclose(nfp);
490 if (tfp)
491 (void) fclose(tfp);
492 if (rename(tfname, nfname) == -1)
493 return (herr("renam", tfname, NULL));
494 return (0);
495 }
496
497 static int
498 herr(const char *what, const char *fname, FILE *fp)
499 {
500
501 (void)fprintf(stderr, "%s: error %sing %s: %s\n",
502 getprogname(), what, fname, strerror(errno));
503 if (fp)
504 (void)fclose(fp);
505 return (1);
506 }
507
508 static char *
509 cntname(const char *src)
510 {
511 char *dst;
512 unsigned char c;
513 static char buf[100];
514
515 dst = buf;
516 *dst++ = 'N';
517 while ((c = *src++) != 0)
518 *dst++ = islower(c) ? toupper(c) : c;
519 *dst = 0;
520 return (buf);
521 }
522