1 /*
2  * Copyright (c) 1992, 1993
3  *	The Regents of the University of California.  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	8.1 (Berkeley) 06/06/93
17  */
18 
19 #include <sys/param.h>
20 #include <ctype.h>
21 #include <errno.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include "config.h"
26 
27 static int emitcnt __P((struct nvlist *));
28 static int err __P((const char *, char *, FILE *));
29 static char *cntname __P((const char *));
30 
31 /*
32  * Make headers containing counts, as needed.
33  */
34 int
mkheaders()35 mkheaders()
36 {
37 	register struct files *fi;
38 
39 	for (fi = allfiles; fi != NULL; fi = fi->fi_next) {
40 		if (fi->fi_flags & FI_HIDDEN)
41 			continue;
42 		if (fi->fi_flags & (FI_NEEDSCOUNT | FI_NEEDSFLAG) &&
43 		    emitcnt(fi->fi_opt))
44 			return (1);
45 	}
46 	return (0);
47 }
48 
49 static int
emitcnt(head)50 emitcnt(head)
51 	register struct nvlist *head;
52 {
53 	register struct nvlist *nv;
54 	register FILE *fp;
55 	register char *fname;
56 	int cnt;
57 	char nam[100];
58 	char buf[BUFSIZ];
59 
60 	(void)sprintf(buf, "%s.h", head->nv_name);
61 	fname = path(buf);
62 	if ((fp = fopen(fname, "r")) == NULL)
63 		goto writeit;
64 	nv = head;
65 	while (fgets(buf, sizeof(buf), fp) != NULL) {
66 		if (nv == NULL)
67 			goto writeit;
68 		if (sscanf(buf, "#define %s %d", nam, &cnt) != 2 ||
69 		    strcmp(nam, cntname(nv->nv_name)) != 0 ||
70 		    cnt != nv->nv_int)
71 			goto writeit;
72 		nv = nv->nv_next;
73 	}
74 	if (ferror(fp))
75 		return (err("read", fname, fp));
76 	(void)fclose(fp);
77 	if (nv == NULL)
78 		return (0);
79 writeit:
80 	if ((fp = fopen(fname, "w")) == NULL) {
81 		(void)fprintf(stderr, "config: cannot write %s: %s\n",
82 		    fname, strerror(errno));
83 		return (1);
84 	}
85 	for (nv = head; nv != NULL; nv = nv->nv_next)
86 		if (fprintf(fp, "#define\t%s\t%d\n",
87 		    cntname(nv->nv_name), nv->nv_int) < 0)
88 			return (err("writ", fname, fp));
89 	if (fclose(fp))
90 		return (err("writ", fname, NULL));
91 	return (0);
92 }
93 
94 static int
err(what,fname,fp)95 err(what, fname, fp)
96 	const char *what;
97 	char *fname;
98 	FILE *fp;
99 {
100 
101 	(void)fprintf(stderr, "config: error %sing %s: %s\n",
102 	    what, fname, strerror(errno));
103 	if (fp)
104 		(void)fclose(fp);
105 	free(fname);
106 	return (1);
107 }
108 
109 static char *
cntname(src)110 cntname(src)
111 	register const char *src;
112 {
113 	register char *dst, c;
114 	static char buf[100];
115 
116 	dst = buf;
117 	*dst++ = 'N';
118 	while ((c = *src++) != 0)
119 		*dst++ = islower(c) ? toupper(c) : c;
120 	*dst = 0;
121 	return (buf);
122 }
123