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