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
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)mkheaders.c 8.1 (Berkeley) 6/6/93
30 * $FreeBSD: src/usr.sbin/config/mkheaders.c,v 1.14.2.2 2001/01/23 00:09:32 peter Exp $
34 * Make all the .h files for the optional entries
41 #include <sys/param.h>
45 static void do_header(const char *, char *, int);
46 static void do_count(const char *, char *, int);
47 static char *toheader(const char *);
48 static char *tomacro(const char *);
56 for (fl
= ftab
; fl
!= NULL
; fl
= fl
->f_next
)
57 if (fl
->f_needs
!= NULL
)
58 do_count(fl
->f_needs
, fl
->f_needs
, 1);
59 for (dp
= dtab
; dp
!= NULL
; dp
= dp
->d_next
) {
60 if ((dp
->d_type
& TYPEMASK
) == PSEUDO_DEVICE
) {
61 if (!(dp
->d_type
& DEVDONE
)) {
62 printf("Warning: pseudo-device \"%s\" is unknown\n",
67 if ((dp
->d_type
& TYPEMASK
) == DEVICE
) {
68 if (!(dp
->d_type
& DEVDONE
)) {
69 printf("Warning: device \"%s\" is unknown\n",
78 * count all the devices of a certain type and recurse to count
79 * whatever the device is connected to
82 do_count(const char *dev
, char *hname
, int search
)
89 * After this loop, "count" will be the actual number of units,
90 * and "hicount" will be the highest unit declared. do_header()
91 * must use the higher of these values.
93 for (dp
= dtab
; dp
!= NULL
; dp
= dp
->d_next
) {
94 if (strcmp(dp
->d_name
, dev
) == 0) {
95 if ((dp
->d_type
& TYPEMASK
) == PSEUDO_DEVICE
)
96 dp
->d_type
|= DEVDONE
;
97 else if ((dp
->d_type
& TYPEMASK
) == DEVICE
)
98 dp
->d_type
|= DEVDONE
;
101 for (hicount
= count
= 0, dp
= dtab
; dp
!= NULL
; dp
= dp
->d_next
) {
102 if (dp
->d_unit
!= -1 && strcmp(dp
->d_name
, dev
) == 0) {
103 if ((dp
->d_type
& TYPEMASK
) == PSEUDO_DEVICE
) {
105 dp
->d_count
!= UNKNOWN
? dp
->d_count
: 1;
110 * Allow holes in unit numbering,
111 * assumption is unit numbering starts
114 if (dp
->d_unit
+ 1 > hicount
)
115 hicount
= dp
->d_unit
+ 1;
118 if (mp
!= NULL
&& dp
->d_connunit
< 0)
120 if (mp
!= NULL
&& strcmp(mp
, "nexus") == 0)
123 do_count(mp
, hname
, 0);
129 do_header(dev
, hname
, count
> hicount
? count
: hicount
);
133 do_header(const char *dev
, char *hname
, int count
)
135 char *file
, *name
, *inw
;
136 struct file_list
*fl
, *fl_head
, *tflp
;
140 file
= toheader(hname
);
142 inf
= fopen(file
, "r");
145 outf
= fopen(file
, "w");
148 fprintf(outf
, "#define %s %d\n", name
, count
);
155 if ((inw
= get_word(inf
)) == NULL
|| inw
== (char *)EOF
)
157 if ((inw
= get_word(inf
)) == NULL
|| inw
== (char *)EOF
)
161 if (cp
== NULL
|| cp
== (char *)EOF
)
164 if (strcmp(inw
, name
) == 0) {
169 if (cp
== (char *)EOF
)
171 fl
= malloc(sizeof(*fl
));
172 bzero(fl
, sizeof(*fl
));
173 fl
->f_fn
= inw
; /* malloced */
175 fl
->f_next
= fl_head
;
179 if (count
== oldcount
) {
180 for (fl
= fl_head
; fl
!= NULL
; fl
= tflp
) {
187 if (oldcount
== -1) {
188 fl
= malloc(sizeof(*fl
));
189 bzero(fl
, sizeof(*fl
));
190 fl
->f_fn
= strdup(name
);
192 fl
->f_next
= fl_head
;
195 outf
= fopen(file
, "w");
198 for (fl
= fl_head
; fl
!= NULL
; fl
= tflp
) {
200 "#define %s %u\n", fl
->f_fn
, count
? fl
->f_type
: 0);
209 * convert a dev name to a .h file name
212 toheader(const char *dev
)
214 static char hbuf
[MAXPATHLEN
];
215 static char udev
[MAXPATHLEN
];
217 snprintf(udev
, sizeof(udev
), "use_%s", dev
);
219 snprintf(hbuf
, sizeof(hbuf
), "%s.h", path(udev
));
224 * convert a dev name to a macro name
227 tomacro(const char *dev
)
229 static char mbuf
[20];
235 *cp
++ = islower(*dev
) ? toupper(*dev
++) : *dev
++;