mkheaders.c revision 1.4 1 1.4 cube /* $NetBSD: mkheaders.c,v 1.4 2006/03/19 22:27:14 cube Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*
4 1.1 thorpej * Copyright (c) 1992, 1993
5 1.1 thorpej * The Regents of the University of California. All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This software was developed by the Computer Systems Engineering group
8 1.1 thorpej * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 1.1 thorpej * contributed to Berkeley.
10 1.1 thorpej *
11 1.1 thorpej * All advertising materials mentioning features or use of this software
12 1.1 thorpej * must display the following acknowledgement:
13 1.1 thorpej * This product includes software developed by the University of
14 1.1 thorpej * California, Lawrence Berkeley Laboratories.
15 1.1 thorpej *
16 1.1 thorpej * Redistribution and use in source and binary forms, with or without
17 1.1 thorpej * modification, are permitted provided that the following conditions
18 1.1 thorpej * are met:
19 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
20 1.1 thorpej * notice, this list of conditions and the following disclaimer.
21 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
22 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
23 1.1 thorpej * documentation and/or other materials provided with the distribution.
24 1.1 thorpej * 3. Neither the name of the University nor the names of its contributors
25 1.1 thorpej * may be used to endorse or promote products derived from this software
26 1.1 thorpej * without specific prior written permission.
27 1.1 thorpej *
28 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 1.1 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 1.1 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 1.1 thorpej * SUCH DAMAGE.
39 1.1 thorpej *
40 1.1 thorpej * from: @(#)mkheaders.c 8.1 (Berkeley) 6/6/93
41 1.1 thorpej */
42 1.1 thorpej
43 1.1 thorpej #if HAVE_NBTOOL_CONFIG_H
44 1.1 thorpej #include "nbtool_config.h"
45 1.1 thorpej #endif
46 1.1 thorpej
47 1.1 thorpej #include <sys/param.h>
48 1.1 thorpej #include <ctype.h>
49 1.1 thorpej #include <errno.h>
50 1.1 thorpej #include <stdio.h>
51 1.1 thorpej #include <stdlib.h>
52 1.1 thorpej #include <string.h>
53 1.1 thorpej #include <time.h>
54 1.1 thorpej #include "defs.h"
55 1.1 thorpej
56 1.1 thorpej static int emitcnt(struct nvlist *);
57 1.1 thorpej static int emitlocs(void);
58 1.1 thorpej static int emitopts(void);
59 1.1 thorpej static int emitioconfh(void);
60 1.1 thorpej static int emittime(void);
61 1.1 thorpej static int herr(const char *, const char *, FILE *);
62 1.1 thorpej static int locators_print(const char *, void *, void *);
63 1.1 thorpej static int defopts_print(const char *, void *, void *);
64 1.1 thorpej static char *cntname(const char *);
65 1.1 thorpej static int fprintcnt(FILE *, struct nvlist *);
66 1.1 thorpej static int fprintstr(FILE *, const char *);
67 1.1 thorpej
68 1.1 thorpej /*
69 1.1 thorpej * Make the various config-generated header files.
70 1.1 thorpej */
71 1.1 thorpej int
72 1.1 thorpej mkheaders(void)
73 1.1 thorpej {
74 1.1 thorpej struct files *fi;
75 1.1 thorpej
76 1.1 thorpej /*
77 1.1 thorpej * Make headers containing counts, as needed.
78 1.1 thorpej */
79 1.1 thorpej TAILQ_FOREACH(fi, &allfiles, fi_next) {
80 1.1 thorpej if (fi->fi_flags & FI_HIDDEN)
81 1.1 thorpej continue;
82 1.1 thorpej if (fi->fi_flags & (FI_NEEDSCOUNT | FI_NEEDSFLAG) &&
83 1.1 thorpej emitcnt(fi->fi_optf))
84 1.1 thorpej return (1);
85 1.1 thorpej }
86 1.1 thorpej
87 1.1 thorpej if (emitopts() || emitlocs() || emitioconfh() || emittime())
88 1.1 thorpej return (1);
89 1.1 thorpej
90 1.1 thorpej return (0);
91 1.1 thorpej }
92 1.1 thorpej
93 1.1 thorpej
94 1.1 thorpej static int
95 1.1 thorpej fprintcnt(FILE *fp, struct nvlist *nv)
96 1.1 thorpej {
97 1.1 thorpej return (fprintf(fp, "#define\t%s\t%d\n",
98 1.1 thorpej cntname(nv->nv_name), nv->nv_int));
99 1.1 thorpej }
100 1.1 thorpej
101 1.1 thorpej static int
102 1.1 thorpej emitcnt(struct nvlist *head)
103 1.1 thorpej {
104 1.1 thorpej char nfname[BUFSIZ], tfname[BUFSIZ];
105 1.1 thorpej struct nvlist *nv;
106 1.1 thorpej FILE *fp;
107 1.1 thorpej
108 1.1 thorpej (void)snprintf(nfname, sizeof(nfname), "%s.h", head->nv_name);
109 1.1 thorpej (void)snprintf(tfname, sizeof(tfname), "tmp_%s", nfname);
110 1.1 thorpej
111 1.1 thorpej if ((fp = fopen(tfname, "w")) == NULL)
112 1.1 thorpej return (herr("open", tfname, NULL));
113 1.1 thorpej
114 1.1 thorpej for (nv = head; nv != NULL; nv = nv->nv_next)
115 1.1 thorpej if (fprintcnt(fp, nv) < 0)
116 1.1 thorpej return (herr("writ", tfname, fp));
117 1.1 thorpej
118 1.1 thorpej if (fclose(fp) == EOF)
119 1.1 thorpej return (herr("clos", tfname, NULL));
120 1.1 thorpej
121 1.1 thorpej return (moveifchanged(tfname, nfname));
122 1.1 thorpej }
123 1.1 thorpej
124 1.1 thorpej /*
125 1.1 thorpej * Output a string, preceded by a tab and possibly unescaping any quotes.
126 1.1 thorpej * The argument will be output as is if it doesn't start with \".
127 1.1 thorpej * Otherwise the first backslash in a \? sequence will be dropped.
128 1.1 thorpej */
129 1.1 thorpej static int
130 1.1 thorpej fprintstr(FILE *fp, const char *str)
131 1.1 thorpej {
132 1.1 thorpej int n;
133 1.1 thorpej
134 1.1 thorpej if (strncmp(str, "\\\"", 2))
135 1.1 thorpej return fprintf(fp, "\t%s", str);
136 1.1 thorpej
137 1.1 thorpej if (fputc('\t', fp) < 0)
138 1.1 thorpej return -1;
139 1.1 thorpej
140 1.1 thorpej for (n = 1; *str; str++, n++) {
141 1.1 thorpej switch (*str) {
142 1.1 thorpej case '\\':
143 1.1 thorpej if (!*++str) /* XXX */
144 1.1 thorpej str--;
145 1.1 thorpej default:
146 1.1 thorpej if (fputc(*str, fp) < 0)
147 1.1 thorpej return -1;
148 1.1 thorpej break;
149 1.1 thorpej }
150 1.1 thorpej }
151 1.1 thorpej return n;
152 1.1 thorpej }
153 1.1 thorpej
154 1.1 thorpej /*
155 1.1 thorpej * Callback function for walking the option file hash table. We write out
156 1.1 thorpej * the options defined for this file.
157 1.1 thorpej */
158 1.1 thorpej static int
159 1.1 thorpej defopts_print(const char *name, void *value, void *arg)
160 1.1 thorpej {
161 1.1 thorpej char tfname[BUFSIZ];
162 1.1 thorpej struct nvlist *nv, *option;
163 1.1 thorpej int isfsoption;
164 1.1 thorpej FILE *fp;
165 1.1 thorpej
166 1.1 thorpej (void)snprintf(tfname, sizeof(tfname), "tmp_%s", name);
167 1.1 thorpej if ((fp = fopen(tfname, "w")) == NULL)
168 1.1 thorpej return (herr("open", tfname, NULL));
169 1.1 thorpej
170 1.1 thorpej for (nv = value; nv != NULL; nv = nv->nv_next) {
171 1.1 thorpej isfsoption = OPT_FSOPT(nv->nv_name);
172 1.1 thorpej
173 1.1 thorpej if ((option = ht_lookup(opttab, nv->nv_name)) == NULL &&
174 1.1 thorpej (option = ht_lookup(fsopttab, nv->nv_name)) == NULL) {
175 1.1 thorpej if (fprintf(fp, "/* %s `%s' not defined */\n",
176 1.1 thorpej isfsoption ? "file system" : "option",
177 1.1 thorpej nv->nv_name) < 0)
178 1.1 thorpej goto bad;
179 1.1 thorpej } else {
180 1.1 thorpej if (fprintf(fp, "#define\t%s", option->nv_name) < 0)
181 1.1 thorpej goto bad;
182 1.1 thorpej if (option->nv_str != NULL && isfsoption == 0 &&
183 1.1 thorpej fprintstr(fp, option->nv_str) < 0)
184 1.1 thorpej goto bad;
185 1.1 thorpej if (fputc('\n', fp) < 0)
186 1.1 thorpej goto bad;
187 1.1 thorpej }
188 1.1 thorpej }
189 1.1 thorpej
190 1.1 thorpej if (fclose(fp) == EOF)
191 1.1 thorpej return (herr("clos", tfname, NULL));
192 1.1 thorpej
193 1.1 thorpej return (moveifchanged(tfname, name));
194 1.1 thorpej
195 1.1 thorpej bad:
196 1.1 thorpej return (herr("writ", tfname, fp));
197 1.1 thorpej }
198 1.1 thorpej
199 1.1 thorpej /*
200 1.1 thorpej * Emit the option header files.
201 1.1 thorpej */
202 1.1 thorpej static int
203 1.1 thorpej emitopts(void)
204 1.1 thorpej {
205 1.1 thorpej
206 1.1 thorpej return (ht_enumerate(optfiletab, defopts_print, NULL));
207 1.1 thorpej }
208 1.1 thorpej
209 1.1 thorpej /*
210 1.1 thorpej * A callback function for walking the attribute hash table.
211 1.1 thorpej * Emit CPP definitions of manifest constants for the locators on the
212 1.1 thorpej * "name" attribute node (passed as the "value" parameter).
213 1.1 thorpej */
214 1.1 thorpej static int
215 1.1 thorpej locators_print(const char *name, void *value, void *arg)
216 1.1 thorpej {
217 1.1 thorpej struct attr *a;
218 1.1 thorpej struct nvlist *nv;
219 1.1 thorpej int i;
220 1.1 thorpej char *locdup, *namedup;
221 1.1 thorpej char *cp;
222 1.1 thorpej FILE *fp = arg;
223 1.1 thorpej
224 1.1 thorpej a = value;
225 1.1 thorpej if (a->a_locs) {
226 1.1 thorpej if (strchr(name, ' ') != NULL || strchr(name, '\t') != NULL)
227 1.1 thorpej /*
228 1.1 thorpej * name contains a space; we can't generate
229 1.1 thorpej * usable defines, so ignore it.
230 1.1 thorpej */
231 1.1 thorpej return 0;
232 1.1 thorpej locdup = estrdup(name);
233 1.1 thorpej for (cp = locdup; *cp; cp++)
234 1.1 thorpej if (islower((unsigned char)*cp))
235 1.1 thorpej *cp = toupper((unsigned char)*cp);
236 1.1 thorpej for (i = 0, nv = a->a_locs; nv; nv = nv->nv_next, i++) {
237 1.1 thorpej if (strchr(nv->nv_name, ' ') != NULL ||
238 1.1 thorpej strchr(nv->nv_name, '\t') != NULL)
239 1.1 thorpej /*
240 1.1 thorpej * name contains a space; we can't generate
241 1.1 thorpej * usable defines, so ignore it.
242 1.1 thorpej */
243 1.1 thorpej continue;
244 1.1 thorpej namedup = estrdup(nv->nv_name);
245 1.1 thorpej for (cp = namedup; *cp; cp++)
246 1.1 thorpej if (islower((unsigned char)*cp))
247 1.1 thorpej *cp = toupper((unsigned char)*cp);
248 1.1 thorpej else if (*cp == ARRCHR)
249 1.1 thorpej *cp = '_';
250 1.1 thorpej if (fprintf(fp, "#define %sCF_%s %d\n",
251 1.1 thorpej locdup, namedup, i) < 0)
252 1.4 cube goto bad;
253 1.1 thorpej if (nv->nv_str &&
254 1.1 thorpej fprintf(fp, "#define %sCF_%s_DEFAULT %s\n",
255 1.1 thorpej locdup, namedup, nv->nv_str) < 0)
256 1.4 cube goto bad;
257 1.1 thorpej free(namedup);
258 1.1 thorpej }
259 1.3 drochner /* assert(i == a->a_loclen) */
260 1.3 drochner if (fprintf(fp, "#define %sCF_NLOCS %d\n",
261 1.3 drochner locdup, a->a_loclen) < 0)
262 1.4 cube goto bad1;
263 1.1 thorpej free(locdup);
264 1.1 thorpej }
265 1.1 thorpej return 0;
266 1.4 cube bad:
267 1.4 cube free(namedup);
268 1.4 cube bad1:
269 1.4 cube free(locdup);
270 1.4 cube return 1;
271 1.1 thorpej }
272 1.1 thorpej
273 1.1 thorpej /*
274 1.1 thorpej * Build the "locators.h" file with manifest constants for all potential
275 1.1 thorpej * locators in the configuration. Do this by enumerating the attribute
276 1.1 thorpej * hash table and emitting all the locators for each attribute.
277 1.1 thorpej */
278 1.1 thorpej static int
279 1.1 thorpej emitlocs(void)
280 1.1 thorpej {
281 1.1 thorpej char *tfname;
282 1.1 thorpej int rval;
283 1.1 thorpej FILE *tfp;
284 1.1 thorpej
285 1.1 thorpej tfname = "tmp_locators.h";
286 1.1 thorpej if ((tfp = fopen(tfname, "w")) == NULL)
287 1.1 thorpej return (herr("open", tfname, NULL));
288 1.1 thorpej
289 1.1 thorpej rval = ht_enumerate(attrtab, locators_print, tfp);
290 1.1 thorpej if (fclose(tfp) == EOF)
291 1.1 thorpej return (herr("clos", tfname, NULL));
292 1.1 thorpej if (rval)
293 1.1 thorpej return (rval);
294 1.1 thorpej return (moveifchanged(tfname, "locators.h"));
295 1.1 thorpej }
296 1.1 thorpej
297 1.1 thorpej /*
298 1.1 thorpej * Build the "ioconf.h" file with extern declarations for all configured
299 1.1 thorpej * cfdrivers.
300 1.1 thorpej */
301 1.1 thorpej static int
302 1.1 thorpej emitioconfh(void)
303 1.1 thorpej {
304 1.1 thorpej const char *tfname;
305 1.1 thorpej FILE *tfp;
306 1.1 thorpej struct devbase *d;
307 1.1 thorpej
308 1.1 thorpej tfname = "tmp_ioconf.h";
309 1.1 thorpej if ((tfp = fopen(tfname, "w")) == NULL)
310 1.1 thorpej return (herr("open", tfname, NULL));
311 1.1 thorpej
312 1.1 thorpej TAILQ_FOREACH(d, &allbases, d_next) {
313 1.1 thorpej if (!devbase_has_instances(d, WILD))
314 1.1 thorpej continue;
315 1.1 thorpej if (fprintf(tfp, "extern struct cfdriver %s_cd;\n",
316 1.4 cube d->d_name) < 0) {
317 1.4 cube (void)fclose(tfp);
318 1.1 thorpej return (1);
319 1.4 cube }
320 1.1 thorpej }
321 1.1 thorpej
322 1.1 thorpej if (fclose(tfp) == EOF)
323 1.1 thorpej return (herr("clos", tfname, NULL));
324 1.1 thorpej
325 1.1 thorpej return (moveifchanged(tfname, "ioconf.h"));
326 1.1 thorpej }
327 1.1 thorpej
328 1.1 thorpej /*
329 1.1 thorpej * Make a file that config_time.h can use as a source, if required.
330 1.1 thorpej */
331 1.1 thorpej static int
332 1.1 thorpej emittime(void)
333 1.1 thorpej {
334 1.1 thorpej FILE *fp;
335 1.1 thorpej time_t t;
336 1.1 thorpej struct tm *tm;
337 1.1 thorpej char buf[128];
338 1.1 thorpej
339 1.1 thorpej t = time(NULL);
340 1.1 thorpej tm = gmtime(&t);
341 1.1 thorpej
342 1.1 thorpej if ((fp = fopen("config_time.src", "w")) == NULL)
343 1.1 thorpej return (herr("open", "config_time.src", NULL));
344 1.1 thorpej
345 1.1 thorpej if (strftime(buf, sizeof(buf), "%c %Z", tm) == 0)
346 1.4 cube return (herr("strftime", "config_time.src", fp));
347 1.1 thorpej
348 1.1 thorpej if (fprintf(fp, "/* %s */\n"
349 1.1 thorpej "#define CONFIG_TIME\t%2lld\n"
350 1.1 thorpej "#define CONFIG_YEAR\t%2d\n"
351 1.1 thorpej "#define CONFIG_MONTH\t%2d\n"
352 1.1 thorpej "#define CONFIG_DATE\t%2d\n"
353 1.1 thorpej "#define CONFIG_HOUR\t%2d\n"
354 1.1 thorpej "#define CONFIG_MINS\t%2d\n"
355 1.1 thorpej "#define CONFIG_SECS\t%2d\n",
356 1.1 thorpej buf, (long long)t,
357 1.1 thorpej tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
358 1.1 thorpej tm->tm_hour, tm->tm_min, tm->tm_sec) < 0)
359 1.4 cube return (herr("fprintf", "config_time.src", fp));
360 1.1 thorpej
361 1.1 thorpej if (fclose(fp) != 0)
362 1.1 thorpej return (herr("close", "config_time.src", NULL));
363 1.1 thorpej
364 1.1 thorpej /*
365 1.1 thorpej * *Don't* moveifchanged this file. Makefile.kern.inc will
366 1.1 thorpej * handle that if it determines such a move is necessary.
367 1.1 thorpej */
368 1.1 thorpej return (0);
369 1.1 thorpej }
370 1.1 thorpej
371 1.1 thorpej /*
372 1.1 thorpej * Compare two files. If nfname doesn't exist, or is different from
373 1.1 thorpej * tfname, move tfname to nfname. Otherwise, delete tfname.
374 1.1 thorpej */
375 1.1 thorpej int
376 1.1 thorpej moveifchanged(const char *tfname, const char *nfname)
377 1.1 thorpej {
378 1.1 thorpej char tbuf[BUFSIZ], nbuf[BUFSIZ];
379 1.1 thorpej FILE *tfp, *nfp;
380 1.1 thorpej
381 1.1 thorpej if ((tfp = fopen(tfname, "r")) == NULL)
382 1.1 thorpej return (herr("open", tfname, NULL));
383 1.1 thorpej
384 1.1 thorpej if ((nfp = fopen(nfname, "r")) == NULL)
385 1.1 thorpej goto moveit;
386 1.1 thorpej
387 1.1 thorpej while (fgets(tbuf, sizeof(tbuf), tfp) != NULL) {
388 1.1 thorpej if (fgets(nbuf, sizeof(nbuf), nfp) == NULL) {
389 1.1 thorpej /*
390 1.1 thorpej * Old file has fewer lines.
391 1.1 thorpej */
392 1.1 thorpej goto moveit;
393 1.1 thorpej }
394 1.1 thorpej if (strcmp(tbuf, nbuf) != 0)
395 1.1 thorpej goto moveit;
396 1.1 thorpej }
397 1.1 thorpej
398 1.1 thorpej /*
399 1.1 thorpej * We've reached the end of the new file. Check to see if new file
400 1.1 thorpej * has fewer lines than old.
401 1.1 thorpej */
402 1.1 thorpej if (fgets(nbuf, sizeof(nbuf), nfp) != NULL) {
403 1.1 thorpej /*
404 1.1 thorpej * New file has fewer lines.
405 1.1 thorpej */
406 1.1 thorpej goto moveit;
407 1.1 thorpej }
408 1.1 thorpej
409 1.1 thorpej (void) fclose(nfp);
410 1.1 thorpej (void) fclose(tfp);
411 1.1 thorpej if (remove(tfname) == -1)
412 1.1 thorpej return(herr("remov", tfname, NULL));
413 1.1 thorpej return (0);
414 1.1 thorpej
415 1.1 thorpej moveit:
416 1.1 thorpej /*
417 1.1 thorpej * They're different, or the file doesn't exist.
418 1.1 thorpej */
419 1.1 thorpej if (nfp)
420 1.1 thorpej (void) fclose(nfp);
421 1.1 thorpej if (tfp)
422 1.1 thorpej (void) fclose(tfp);
423 1.1 thorpej if (rename(tfname, nfname) == -1)
424 1.1 thorpej return (herr("renam", tfname, NULL));
425 1.1 thorpej return (0);
426 1.1 thorpej }
427 1.1 thorpej
428 1.1 thorpej static int
429 1.1 thorpej herr(const char *what, const char *fname, FILE *fp)
430 1.1 thorpej {
431 1.1 thorpej
432 1.1 thorpej (void)fprintf(stderr, "%s: error %sing %s: %s\n",
433 1.1 thorpej getprogname(), what, fname, strerror(errno));
434 1.1 thorpej if (fp)
435 1.1 thorpej (void)fclose(fp);
436 1.1 thorpej return (1);
437 1.1 thorpej }
438 1.1 thorpej
439 1.1 thorpej static char *
440 1.1 thorpej cntname(const char *src)
441 1.1 thorpej {
442 1.1 thorpej char *dst;
443 1.1 thorpej unsigned char c;
444 1.1 thorpej static char buf[100];
445 1.1 thorpej
446 1.1 thorpej dst = buf;
447 1.1 thorpej *dst++ = 'N';
448 1.1 thorpej while ((c = *src++) != 0)
449 1.1 thorpej *dst++ = islower(c) ? toupper(c) : c;
450 1.1 thorpej *dst = 0;
451 1.1 thorpej return (buf);
452 1.1 thorpej }
453