1 /*
2  * Copyright (c) 1992 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This software was developed by the Computer Systems Engineering group
6  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7  * contributed to Berkeley.
8  *
9  * All advertising materials mentioning features or use of this software
10  * must display the following acknowledgement:
11  *	This product includes software developed by the University of
12  *	California, Lawrence Berkeley Laboratories.
13  *
14  * %sccs.include.redist.c%
15  *
16  *	@(#)mkheaders.c	5.1 (Berkeley) 01/12/93
17  *
18  * from: $Header: mkheaders.c,v 1.2 93/01/12 03:58:42 torek Exp $
19  */
20 
21 #include <sys/param.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include "config.h"
28 
29 static int emitcnt __P((struct nvlist *));
30 static int err __P((const char *, char *, FILE *));
31 static char *cntname __P((const char *));
32 
33 /*
34  * Make headers containing counts, as needed.
35  */
36 int
37 mkheaders()
38 {
39 	register struct files *fi;
40 
41 	for (fi = allfiles; fi != NULL; fi = fi->fi_next) {
42 		if (fi->fi_flags & FI_HIDDEN)
43 			continue;
44 		if (fi->fi_flags & (FI_NEEDSCOUNT | FI_NEEDSFLAG) &&
45 		    emitcnt(fi->fi_opt))
46 			return (1);
47 	}
48 	return (0);
49 }
50 
51 static int
52 emitcnt(head)
53 	register struct nvlist *head;
54 {
55 	register struct nvlist *nv;
56 	register FILE *fp;
57 	register char *fname;
58 	int cnt;
59 	char nam[100];
60 	char buf[BUFSIZ];
61 
62 	(void)sprintf(buf, "%s.h", head->nv_name);
63 	fname = path(buf);
64 	if ((fp = fopen(fname, "r")) == NULL)
65 		goto writeit;
66 	nv = head;
67 	while (fgets(buf, sizeof(buf), fp) != NULL) {
68 		if (nv == NULL)
69 			goto writeit;
70 		if (sscanf(buf, "#define %s %d", nam, &cnt) != 2 ||
71 		    strcmp(nam, cntname(nv->nv_name)) != 0 ||
72 		    cnt != nv->nv_int)
73 			goto writeit;
74 		nv = nv->nv_next;
75 	}
76 	if (ferror(fp))
77 		return (err("read", fname, fp));
78 	(void)fclose(fp);
79 	if (nv == NULL)
80 		return (0);
81 writeit:
82 	if ((fp = fopen(fname, "w")) == NULL) {
83 		(void)fprintf(stderr, "config: cannot write %s: %s\n",
84 		    fname, strerror(errno));
85 		return (1);
86 	}
87 	for (nv = head; nv != NULL; nv = nv->nv_next)
88 		if (fprintf(fp, "#define\t%s\t%d\n",
89 		    cntname(nv->nv_name), nv->nv_int) < 0)
90 			return (err("writ", fname, fp));
91 	if (fclose(fp))
92 		return (err("writ", fname, NULL));
93 	return (0);
94 }
95 
96 static int
97 err(what, fname, fp)
98 	const char *what;
99 	char *fname;
100 	FILE *fp;
101 {
102 
103 	(void)fprintf(stderr, "config: error %sing %s: %s\n",
104 	    what, fname, strerror(errno));
105 	if (fp)
106 		(void)fclose(fp);
107 	free(fname);
108 	return (1);
109 }
110 
111 static char *
112 cntname(src)
113 	register const char *src;
114 {
115 	register char *dst, c;
116 	static char buf[100];
117 
118 	dst = buf;
119 	*dst++ = 'N';
120 	while ((c = *src++) != 0)
121 		*dst++ = islower(c) ? toupper(c) : c;
122 	*dst = 0;
123 	return (buf);
124 }
125