1 /* $NetBSD: mkheaders.c,v 1.4 2006/03/19 22:27:14 cube 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 for (i = 0, nv = a->a_locs; nv; nv = nv->nv_next, i++) { 237 if (strchr(nv->nv_name, ' ') != NULL || 238 strchr(nv->nv_name, '\t') != NULL) 239 /* 240 * name contains a space; we can't generate 241 * usable defines, so ignore it. 242 */ 243 continue; 244 namedup = estrdup(nv->nv_name); 245 for (cp = namedup; *cp; cp++) 246 if (islower((unsigned char)*cp)) 247 *cp = toupper((unsigned char)*cp); 248 else if (*cp == ARRCHR) 249 *cp = '_'; 250 if (fprintf(fp, "#define %sCF_%s %d\n", 251 locdup, namedup, i) < 0) 252 goto bad; 253 if (nv->nv_str && 254 fprintf(fp, "#define %sCF_%s_DEFAULT %s\n", 255 locdup, namedup, nv->nv_str) < 0) 256 goto bad; 257 free(namedup); 258 } 259 /* assert(i == a->a_loclen) */ 260 if (fprintf(fp, "#define %sCF_NLOCS %d\n", 261 locdup, a->a_loclen) < 0) 262 goto bad1; 263 free(locdup); 264 } 265 return 0; 266 bad: 267 free(namedup); 268 bad1: 269 free(locdup); 270 return 1; 271 } 272 273 /* 274 * Build the "locators.h" file with manifest constants for all potential 275 * locators in the configuration. Do this by enumerating the attribute 276 * hash table and emitting all the locators for each attribute. 277 */ 278 static int 279 emitlocs(void) 280 { 281 char *tfname; 282 int rval; 283 FILE *tfp; 284 285 tfname = "tmp_locators.h"; 286 if ((tfp = fopen(tfname, "w")) == NULL) 287 return (herr("open", tfname, NULL)); 288 289 rval = ht_enumerate(attrtab, locators_print, tfp); 290 if (fclose(tfp) == EOF) 291 return (herr("clos", tfname, NULL)); 292 if (rval) 293 return (rval); 294 return (moveifchanged(tfname, "locators.h")); 295 } 296 297 /* 298 * Build the "ioconf.h" file with extern declarations for all configured 299 * cfdrivers. 300 */ 301 static int 302 emitioconfh(void) 303 { 304 const char *tfname; 305 FILE *tfp; 306 struct devbase *d; 307 308 tfname = "tmp_ioconf.h"; 309 if ((tfp = fopen(tfname, "w")) == NULL) 310 return (herr("open", tfname, NULL)); 311 312 TAILQ_FOREACH(d, &allbases, d_next) { 313 if (!devbase_has_instances(d, WILD)) 314 continue; 315 if (fprintf(tfp, "extern struct cfdriver %s_cd;\n", 316 d->d_name) < 0) { 317 (void)fclose(tfp); 318 return (1); 319 } 320 } 321 322 if (fclose(tfp) == EOF) 323 return (herr("clos", tfname, NULL)); 324 325 return (moveifchanged(tfname, "ioconf.h")); 326 } 327 328 /* 329 * Make a file that config_time.h can use as a source, if required. 330 */ 331 static int 332 emittime(void) 333 { 334 FILE *fp; 335 time_t t; 336 struct tm *tm; 337 char buf[128]; 338 339 t = time(NULL); 340 tm = gmtime(&t); 341 342 if ((fp = fopen("config_time.src", "w")) == NULL) 343 return (herr("open", "config_time.src", NULL)); 344 345 if (strftime(buf, sizeof(buf), "%c %Z", tm) == 0) 346 return (herr("strftime", "config_time.src", fp)); 347 348 if (fprintf(fp, "/* %s */\n" 349 "#define CONFIG_TIME\t%2lld\n" 350 "#define CONFIG_YEAR\t%2d\n" 351 "#define CONFIG_MONTH\t%2d\n" 352 "#define CONFIG_DATE\t%2d\n" 353 "#define CONFIG_HOUR\t%2d\n" 354 "#define CONFIG_MINS\t%2d\n" 355 "#define CONFIG_SECS\t%2d\n", 356 buf, (long long)t, 357 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, 358 tm->tm_hour, tm->tm_min, tm->tm_sec) < 0) 359 return (herr("fprintf", "config_time.src", fp)); 360 361 if (fclose(fp) != 0) 362 return (herr("close", "config_time.src", NULL)); 363 364 /* 365 * *Don't* moveifchanged this file. Makefile.kern.inc will 366 * handle that if it determines such a move is necessary. 367 */ 368 return (0); 369 } 370 371 /* 372 * Compare two files. If nfname doesn't exist, or is different from 373 * tfname, move tfname to nfname. Otherwise, delete tfname. 374 */ 375 int 376 moveifchanged(const char *tfname, const char *nfname) 377 { 378 char tbuf[BUFSIZ], nbuf[BUFSIZ]; 379 FILE *tfp, *nfp; 380 381 if ((tfp = fopen(tfname, "r")) == NULL) 382 return (herr("open", tfname, NULL)); 383 384 if ((nfp = fopen(nfname, "r")) == NULL) 385 goto moveit; 386 387 while (fgets(tbuf, sizeof(tbuf), tfp) != NULL) { 388 if (fgets(nbuf, sizeof(nbuf), nfp) == NULL) { 389 /* 390 * Old file has fewer lines. 391 */ 392 goto moveit; 393 } 394 if (strcmp(tbuf, nbuf) != 0) 395 goto moveit; 396 } 397 398 /* 399 * We've reached the end of the new file. Check to see if new file 400 * has fewer lines than old. 401 */ 402 if (fgets(nbuf, sizeof(nbuf), nfp) != NULL) { 403 /* 404 * New file has fewer lines. 405 */ 406 goto moveit; 407 } 408 409 (void) fclose(nfp); 410 (void) fclose(tfp); 411 if (remove(tfname) == -1) 412 return(herr("remov", tfname, NULL)); 413 return (0); 414 415 moveit: 416 /* 417 * They're different, or the file doesn't exist. 418 */ 419 if (nfp) 420 (void) fclose(nfp); 421 if (tfp) 422 (void) fclose(tfp); 423 if (rename(tfname, nfname) == -1) 424 return (herr("renam", tfname, NULL)); 425 return (0); 426 } 427 428 static int 429 herr(const char *what, const char *fname, FILE *fp) 430 { 431 432 (void)fprintf(stderr, "%s: error %sing %s: %s\n", 433 getprogname(), what, fname, strerror(errno)); 434 if (fp) 435 (void)fclose(fp); 436 return (1); 437 } 438 439 static char * 440 cntname(const char *src) 441 { 442 char *dst; 443 unsigned char c; 444 static char buf[100]; 445 446 dst = buf; 447 *dst++ = 'N'; 448 while ((c = *src++) != 0) 449 *dst++ = islower(c) ? toupper(c) : c; 450 *dst = 0; 451 return (buf); 452 } 453