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