]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/config.new/mkheaders.c
Restore back -i for adjkerntz
[FreeBSD/FreeBSD.git] / usr.sbin / config.new / mkheaders.c
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  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. All advertising materials mentioning features or use of this software
23  *    must display the following acknowledgement:
24  *      This product includes software developed by the University of
25  *      California, Berkeley and its contributors.
26  * 4. Neither the name of the University nor the names of its contributors
27  *    may be used to endorse or promote products derived from this software
28  *    without specific prior written permission.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40  * SUCH DAMAGE.
41  *
42  *      @(#)mkheaders.c 8.1 (Berkeley) 6/6/93
43  */
44
45 #include <sys/param.h>
46 #include <ctype.h>
47 #include <errno.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include "config.h"
52
53 static int emitcnt __P((struct nvlist *));
54 static int err __P((const char *, char *, FILE *));
55 static char *cntname __P((const char *));
56
57 /*
58  * Make headers containing counts, as needed.
59  */
60 int
61 mkheaders()
62 {
63         register struct files *fi;
64
65         for (fi = allfiles; fi != NULL; fi = fi->fi_next) {
66                 if (fi->fi_flags & FI_HIDDEN)
67                         continue;
68                 if (fi->fi_flags & (FI_NEEDSCOUNT | FI_NEEDSFLAG) &&
69                     emitcnt(fi->fi_opt))
70                         return (1);
71         }
72         return (0);
73 }
74
75 static int
76 emitcnt(head)
77         register struct nvlist *head;
78 {
79         register struct nvlist *nv;
80         register FILE *fp;
81         register char *fname;
82         int cnt;
83         char nam[100];
84         char buf[BUFSIZ];
85
86         (void)sprintf(buf, "%s.h", head->nv_name);
87         fname = path(buf);
88         if ((fp = fopen(fname, "r")) == NULL)
89                 goto writeit;
90         nv = head;
91         while (fgets(buf, sizeof(buf), fp) != NULL) {
92                 if (nv == NULL)
93                         goto writeit;
94                 if (sscanf(buf, "#define %s %d", nam, &cnt) != 2 ||
95                     strcmp(nam, cntname(nv->nv_name)) != 0 ||
96                     cnt != nv->nv_int)
97                         goto writeit;
98                 nv = nv->nv_next;
99         }
100         if (ferror(fp))
101                 return (err("read", fname, fp));
102         (void)fclose(fp);
103         if (nv == NULL)
104                 return (0);
105 writeit:
106         if ((fp = fopen(fname, "w")) == NULL) {
107                 (void)fprintf(stderr, "config: cannot write %s: %s\n",
108                     fname, strerror(errno));
109                 return (1);
110         }
111         for (nv = head; nv != NULL; nv = nv->nv_next)
112                 if (fprintf(fp, "#define\t%s\t%d\n",
113                     cntname(nv->nv_name), nv->nv_int) < 0)
114                         return (err("writ", fname, fp));
115         if (fclose(fp))
116                 return (err("writ", fname, NULL));
117         return (0);
118 }
119
120 static int
121 err(what, fname, fp)
122         const char *what;
123         char *fname;
124         FILE *fp;
125 {
126
127         (void)fprintf(stderr, "config: error %sing %s: %s\n",
128             what, fname, strerror(errno));
129         if (fp)
130                 (void)fclose(fp);
131         free(fname);
132         return (1);
133 }
134
135 static char *
136 cntname(src)
137         register const char *src;
138 {
139         register char *dst, c;
140         static char buf[100];
141
142         dst = buf;
143         *dst++ = 'N';
144         while ((c = *src++) != 0)
145                 *dst++ = islower(c) ? toupper(c) : c;
146         *dst = 0;
147         return (buf);
148 }