kernel: Fix buildkernel without INVARIANTS.
[dragonfly.git] / usr.sbin / config / mkheaders.c
blobcddb981eaae0aa02f62a51b4a9892c359b23f6bd
1 /*
2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
33 * @(#)mkheaders.c 8.1 (Berkeley) 6/6/93
34 * $FreeBSD: src/usr.sbin/config/mkheaders.c,v 1.14.2.2 2001/01/23 00:09:32 peter Exp $
38 * Make all the .h files for the optional entries
41 #include <ctype.h>
42 #include <err.h>
43 #include <stdio.h>
44 #include <string.h>
45 #include <sys/param.h>
46 #include "config.h"
47 #include "y.tab.h"
49 static void do_header(const char *, char *, int);
50 static void do_count(const char *, char *, int);
51 static char *toheader(const char *);
52 static char *tomacro(const char *);
54 void
55 headers(void)
57 struct file_list *fl;
58 struct device *dp;
60 for (fl = ftab; fl != NULL; fl = fl->f_next)
61 if (fl->f_needs != NULL)
62 do_count(fl->f_needs, fl->f_needs, 1);
63 for (dp = dtab; dp != NULL; dp = dp->d_next) {
64 if ((dp->d_type & TYPEMASK) == PSEUDO_DEVICE) {
65 if (!(dp->d_type & DEVDONE)) {
66 printf("Warning: pseudo-device \"%s\" is unknown\n",
67 dp->d_name);
68 exit(1);
71 if ((dp->d_type & TYPEMASK) == DEVICE) {
72 if (!(dp->d_type & DEVDONE)) {
73 printf("Warning: device \"%s\" is unknown\n",
74 dp->d_name);
75 exit(1);
82 * count all the devices of a certain type and recurse to count
83 * whatever the device is connected to
85 static void
86 do_count(const char *dev, char *hname, int search)
88 struct device *dp;
89 int count, hicount;
90 const char *mp;
93 * After this loop, "count" will be the actual number of units,
94 * and "hicount" will be the highest unit declared. do_header()
95 * must use the higher of these values.
97 for (dp = dtab; dp != NULL; dp = dp->d_next) {
98 if (strcmp(dp->d_name, dev) == 0) {
99 if ((dp->d_type & TYPEMASK) == PSEUDO_DEVICE)
100 dp->d_type |= DEVDONE;
101 else if ((dp->d_type & TYPEMASK) == DEVICE)
102 dp->d_type |= DEVDONE;
105 for (hicount = count = 0, dp = dtab; dp != NULL; dp = dp->d_next) {
106 if (dp->d_unit != -1 && strcmp(dp->d_name, dev) == 0) {
107 if ((dp->d_type & TYPEMASK) == PSEUDO_DEVICE) {
108 count =
109 dp->d_count != UNKNOWN ? dp->d_count : 1;
110 break;
112 count++;
114 * Allow holes in unit numbering,
115 * assumption is unit numbering starts
116 * at zero.
118 if (dp->d_unit + 1 > hicount)
119 hicount = dp->d_unit + 1;
120 if (search) {
121 mp = dp->d_conn;
122 if (mp != NULL && dp->d_connunit < 0)
123 mp = NULL;
124 if (mp != NULL && strcmp(mp, "nexus") == 0)
125 mp = NULL;
126 if (mp != NULL) {
127 do_count(mp, hname, 0);
128 search = 0;
133 do_header(dev, hname, count > hicount ? count : hicount);
136 static void
137 do_header(const char *dev, char *hname, int count)
139 char *file, *name, *inw;
140 struct file_list *fl, *fl_head, *tflp;
141 FILE *inf, *outf;
142 int inc, oldcount;
144 file = toheader(hname);
145 name = tomacro(dev);
146 inf = fopen(file, "r");
147 oldcount = -1;
148 if (inf == NULL) {
149 outf = fopen(file, "w");
150 if (outf == NULL)
151 err(1, "%s", file);
152 fprintf(outf, "#define %s %d\n", name, count);
153 fclose(outf);
154 return;
156 fl_head = NULL;
157 for (;;) {
158 char *cp;
159 if ((inw = get_word(inf)) == NULL || inw == (char *)EOF)
160 break;
161 if ((inw = get_word(inf)) == NULL || inw == (char *)EOF)
162 break;
163 inw = strdup(inw);
164 cp = get_word(inf);
165 if (cp == NULL || cp == (char *)EOF)
166 break;
167 inc = atoi(cp);
168 if (strcmp(inw, name) == 0) {
169 oldcount = inc;
170 inc = count;
172 cp = get_word(inf);
173 if (cp == (char *)EOF)
174 break;
175 fl = malloc(sizeof(*fl));
176 bzero(fl, sizeof(*fl));
177 fl->f_fn = inw; /* malloced */
178 fl->f_type = inc;
179 fl->f_next = fl_head;
180 fl_head = fl;
182 fclose(inf);
183 if (count == oldcount) {
184 for (fl = fl_head; fl != NULL; fl = tflp) {
185 tflp = fl->f_next;
186 free(fl->f_fn);
187 free(fl);
189 return;
191 if (oldcount == -1) {
192 fl = malloc(sizeof(*fl));
193 bzero(fl, sizeof(*fl));
194 fl->f_fn = strdup(name);
195 fl->f_type = count;
196 fl->f_next = fl_head;
197 fl_head = fl;
199 outf = fopen(file, "w");
200 if (outf == NULL)
201 err(1, "%s", file);
202 for (fl = fl_head; fl != NULL; fl = tflp) {
203 fprintf(outf,
204 "#define %s %u\n", fl->f_fn, count ? fl->f_type : 0);
205 tflp = fl->f_next;
206 free(fl->f_fn);
207 free(fl);
209 fclose(outf);
213 * convert a dev name to a .h file name
215 static char *
216 toheader(const char *dev)
218 static char hbuf[MAXPATHLEN];
219 static char udev[MAXPATHLEN];
221 snprintf(udev, sizeof(udev), "use_%s", dev);
223 snprintf(hbuf, sizeof(hbuf), "%s.h", path(udev));
224 return(hbuf);
228 * convert a dev name to a macro name
230 static char *
231 tomacro(const char *dev)
233 static char mbuf[20];
234 char *cp;
236 cp = mbuf;
237 *cp++ = 'N';
238 while (*dev != 0)
239 *cp++ = islower(*dev) ? toupper(*dev++) : *dev++;
240 *cp++ = 0;
241 return(mbuf);