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