Home | History | Annotate | Line # | Download | only in config
mkheaders.c revision 1.12
      1 /*	$NetBSD: mkheaders.c,v 1.12 2007/05/12 10:15:31 dsl 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, unsigned int 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%x\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%x\\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%d\n", name, nv->nv_int);
    155 	fprint_global(fp, name, nv->nv_int);
    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 		fputc('\n', fp);
    261 		fprint_global(fp, nv->nv_name,
    262 		    opt_value == NULL ? 1 : global_hash(opt_value));
    263 	}
    264 
    265 	fflush(fp);
    266 	if (ferror(fp))
    267 		return herr("writ", tfname, fp);
    268 
    269 	if (fclose(fp) == EOF)
    270 		return (herr("clos", tfname, NULL));
    271 
    272 	return (moveifchanged(tfname, name));
    273 }
    274 
    275 /*
    276  * Emit the option header files.
    277  */
    278 static int
    279 emitopts(void)
    280 {
    281 
    282 	return (ht_enumerate(optfiletab, defopts_print, NULL));
    283 }
    284 
    285 /*
    286  * A callback function for walking the attribute hash table.
    287  * Emit CPP definitions of manifest constants for the locators on the
    288  * "name" attribute node (passed as the "value" parameter).
    289  */
    290 static int
    291 locators_print(const char *name, void *value, void *arg)
    292 {
    293 	struct attr *a;
    294 	struct nvlist *nv;
    295 	int i;
    296 	char *locdup, *namedup;
    297 	char *cp;
    298 	FILE *fp = arg;
    299 
    300 	a = value;
    301 	if (a->a_locs) {
    302 		if (strchr(name, ' ') != NULL || strchr(name, '\t') != NULL)
    303 			/*
    304 			 * name contains a space; we can't generate
    305 			 * usable defines, so ignore it.
    306 			 */
    307 			return 0;
    308 		locdup = estrdup(name);
    309 		for (cp = locdup; *cp; cp++)
    310 			if (islower((unsigned char)*cp))
    311 				*cp = toupper((unsigned char)*cp);
    312 		for (i = 0, nv = a->a_locs; nv; nv = nv->nv_next, i++) {
    313 			if (strchr(nv->nv_name, ' ') != NULL ||
    314 			    strchr(nv->nv_name, '\t') != NULL)
    315 				/*
    316 				 * name contains a space; we can't generate
    317 				 * usable defines, so ignore it.
    318 				 */
    319 				continue;
    320 			namedup = estrdup(nv->nv_name);
    321 			for (cp = namedup; *cp; cp++)
    322 				if (islower((unsigned char)*cp))
    323 					*cp = toupper((unsigned char)*cp);
    324 				else if (*cp == ARRCHR)
    325 					*cp = '_';
    326 			fprintf(fp, "#define %sCF_%s %d\n", locdup, namedup, i);
    327 			if (nv->nv_str != NULL)
    328 				fprintf(fp, "#define %sCF_%s_DEFAULT %s\n",
    329 				    locdup, namedup, nv->nv_str);
    330 			free(namedup);
    331 		}
    332 		/* assert(i == a->a_loclen) */
    333 		fprintf(fp, "#define %sCF_NLOCS %d\n", locdup, a->a_loclen);
    334 		free(locdup);
    335 	}
    336 	return 0;
    337 }
    338 
    339 /*
    340  * Build the "locators.h" file with manifest constants for all potential
    341  * locators in the configuration.  Do this by enumerating the attribute
    342  * hash table and emitting all the locators for each attribute.
    343  */
    344 static int
    345 emitlocs(void)
    346 {
    347 	char *tfname;
    348 	int rval;
    349 	FILE *tfp;
    350 
    351 	tfname = "tmp_locators.h";
    352 	if ((tfp = fopen(tfname, "w")) == NULL)
    353 		return (herr("open", tfname, NULL));
    354 
    355 	rval = ht_enumerate(attrtab, locators_print, tfp);
    356 
    357 	fflush(tfp);
    358 	if (ferror(tfp))
    359 		return (herr("writ", tfname, NULL));
    360 	if (fclose(tfp) == EOF)
    361 		return (herr("clos", tfname, NULL));
    362 	if (rval)
    363 		return (rval);
    364 	return (moveifchanged(tfname, "locators.h"));
    365 }
    366 
    367 /*
    368  * Build the "ioconf.h" file with extern declarations for all configured
    369  * cfdrivers.
    370  */
    371 static int
    372 emitioconfh(void)
    373 {
    374 	const char *tfname;
    375 	FILE *tfp;
    376 	struct devbase *d;
    377 
    378 	tfname = "tmp_ioconf.h";
    379 	if ((tfp = fopen(tfname, "w")) == NULL)
    380 		return (herr("open", tfname, NULL));
    381 
    382 	TAILQ_FOREACH(d, &allbases, d_next) {
    383 		if (!devbase_has_instances(d, WILD))
    384 			continue;
    385 		fprintf(tfp, "extern struct cfdriver %s_cd;\n", d->d_name);
    386 	}
    387 
    388 	fflush(tfp);
    389 	if (ferror(tfp))
    390 		return herr("writ", tfname, tfp);
    391 
    392 	if (fclose(tfp) == EOF)
    393 		return (herr("clos", tfname, NULL));
    394 
    395 	return (moveifchanged(tfname, "ioconf.h"));
    396 }
    397 
    398 /*
    399  * Make a file that config_time.h can use as a source, if required.
    400  */
    401 static int
    402 emittime(void)
    403 {
    404 	FILE *fp;
    405 	time_t t;
    406 	struct tm *tm;
    407 	char buf[128];
    408 
    409 	t = time(NULL);
    410 	tm = gmtime(&t);
    411 
    412 	if ((fp = fopen("config_time.src", "w")) == NULL)
    413 		return (herr("open", "config_time.src", NULL));
    414 
    415 	if (strftime(buf, sizeof(buf), "%c %Z", tm) == 0)
    416 		return (herr("strftime", "config_time.src", fp));
    417 
    418 	fprintf(fp, "/* %s */\n"
    419 	    "#define CONFIG_TIME\t%2lld\n"
    420 	    "#define CONFIG_YEAR\t%2d\n"
    421 	    "#define CONFIG_MONTH\t%2d\n"
    422 	    "#define CONFIG_DATE\t%2d\n"
    423 	    "#define CONFIG_HOUR\t%2d\n"
    424 	    "#define CONFIG_MINS\t%2d\n"
    425 	    "#define CONFIG_SECS\t%2d\n",
    426 	    buf, (long long)t,
    427 	    tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
    428 	    tm->tm_hour, tm->tm_min, tm->tm_sec);
    429 
    430 	fflush(fp);
    431 	if (ferror(fp))
    432 		return (herr("fprintf", "config_time.src", fp));
    433 
    434 	if (fclose(fp) != 0)
    435 		return (herr("clos", "config_time.src", NULL));
    436 
    437 	/*
    438 	 * *Don't* moveifchanged this file.  Makefile.kern.inc will
    439 	 * handle that if it determines such a move is necessary.
    440 	 */
    441 	return (0);
    442 }
    443 
    444 /*
    445  * Compare two files.  If nfname doesn't exist, or is different from
    446  * tfname, move tfname to nfname.  Otherwise, delete tfname.
    447  */
    448 int
    449 moveifchanged(const char *tfname, const char *nfname)
    450 {
    451 	char tbuf[BUFSIZ], nbuf[BUFSIZ];
    452 	FILE *tfp, *nfp;
    453 
    454 	if ((tfp = fopen(tfname, "r")) == NULL)
    455 		return (herr("open", tfname, NULL));
    456 
    457 	if ((nfp = fopen(nfname, "r")) == NULL)
    458 		goto moveit;
    459 
    460 	while (fgets(tbuf, sizeof(tbuf), tfp) != NULL) {
    461 		if (fgets(nbuf, sizeof(nbuf), nfp) == NULL) {
    462 			/*
    463 			 * Old file has fewer lines.
    464 			 */
    465 			goto moveit;
    466 		}
    467 		if (strcmp(tbuf, nbuf) != 0)
    468 			goto moveit;
    469 	}
    470 
    471 	/*
    472 	 * We've reached the end of the new file.  Check to see if new file
    473 	 * has fewer lines than old.
    474 	 */
    475 	if (fgets(nbuf, sizeof(nbuf), nfp) != NULL) {
    476 		/*
    477 		 * New file has fewer lines.
    478 		 */
    479 		goto moveit;
    480 	}
    481 
    482 	(void) fclose(nfp);
    483 	(void) fclose(tfp);
    484 	if (remove(tfname) == -1)
    485 		return(herr("remov", tfname, NULL));
    486 	return (0);
    487 
    488  moveit:
    489 	/*
    490 	 * They're different, or the file doesn't exist.
    491 	 */
    492 	if (nfp)
    493 		(void) fclose(nfp);
    494 	if (tfp)
    495 		(void) fclose(tfp);
    496 	if (rename(tfname, nfname) == -1)
    497 		return (herr("renam", tfname, NULL));
    498 	return (0);
    499 }
    500 
    501 static int
    502 herr(const char *what, const char *fname, FILE *fp)
    503 {
    504 
    505 	warn("error %sing %s", what, fname);
    506 	if (fp)
    507 		(void)fclose(fp);
    508 	return (1);
    509 }
    510 
    511 static char *
    512 cntname(const char *src)
    513 {
    514 	char *dst;
    515 	unsigned char c;
    516 	static char buf[100];
    517 
    518 	dst = buf;
    519 	*dst++ = 'N';
    520 	while ((c = *src++) != 0)
    521 		*dst++ = islower(c) ? toupper(c) : c;
    522 	*dst = 0;
    523 	return (buf);
    524 }
    525