drm/radeon: Reduce differences with Linux 3.18
[dragonfly.git] / usr.sbin / config / mkheaders.c
blob8dfe74f7c2f3257a654927f9470056465ec36ac2
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. 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
27 * SUCH DAMAGE.
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
37 #include <ctype.h>
38 #include <err.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <sys/param.h>
42 #include "config.h"
43 #include "y.tab.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 *);
50 void
51 headers(void)
53 struct file_list *fl;
54 struct device *dp;
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",
63 dp->d_name);
64 exit(1);
67 if ((dp->d_type & TYPEMASK) == DEVICE) {
68 if (!(dp->d_type & DEVDONE)) {
69 printf("Warning: device \"%s\" is unknown\n",
70 dp->d_name);
71 exit(1);
78 * count all the devices of a certain type and recurse to count
79 * whatever the device is connected to
81 static void
82 do_count(const char *dev, char *hname, int search)
84 struct device *dp;
85 int count, hicount;
86 const char *mp;
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) {
104 count =
105 dp->d_count != UNKNOWN ? dp->d_count : 1;
106 break;
108 count++;
110 * Allow holes in unit numbering,
111 * assumption is unit numbering starts
112 * at zero.
114 if (dp->d_unit + 1 > hicount)
115 hicount = dp->d_unit + 1;
116 if (search) {
117 mp = dp->d_conn;
118 if (mp != NULL && dp->d_connunit < 0)
119 mp = NULL;
120 if (mp != NULL && strcmp(mp, "nexus") == 0)
121 mp = NULL;
122 if (mp != NULL) {
123 do_count(mp, hname, 0);
124 search = 0;
129 do_header(dev, hname, count > hicount ? count : hicount);
132 static void
133 do_header(const char *dev, char *hname, int count)
135 char *file, *name, *inw;
136 struct file_list *fl, *fl_head, *tflp;
137 FILE *inf, *outf;
138 int inc, oldcount;
140 file = toheader(hname);
141 name = tomacro(dev);
142 inf = fopen(file, "r");
143 oldcount = -1;
144 if (inf == NULL) {
145 outf = fopen(file, "w");
146 if (outf == NULL)
147 err(1, "%s", file);
148 fprintf(outf, "#define %s %d\n", name, count);
149 fclose(outf);
150 return;
152 fl_head = NULL;
153 for (;;) {
154 char *cp;
155 if ((inw = get_word(inf)) == NULL || inw == (char *)EOF)
156 break;
157 if ((inw = get_word(inf)) == NULL || inw == (char *)EOF)
158 break;
159 inw = strdup(inw);
160 cp = get_word(inf);
161 if (cp == NULL || cp == (char *)EOF)
162 break;
163 inc = atoi(cp);
164 if (strcmp(inw, name) == 0) {
165 oldcount = inc;
166 inc = count;
168 cp = get_word(inf);
169 if (cp == (char *)EOF)
170 break;
171 fl = malloc(sizeof(*fl));
172 bzero(fl, sizeof(*fl));
173 fl->f_fn = inw; /* malloced */
174 fl->f_type = inc;
175 fl->f_next = fl_head;
176 fl_head = fl;
178 fclose(inf);
179 if (count == oldcount) {
180 for (fl = fl_head; fl != NULL; fl = tflp) {
181 tflp = fl->f_next;
182 free(fl->f_fn);
183 free(fl);
185 return;
187 if (oldcount == -1) {
188 fl = malloc(sizeof(*fl));
189 bzero(fl, sizeof(*fl));
190 fl->f_fn = strdup(name);
191 fl->f_type = count;
192 fl->f_next = fl_head;
193 fl_head = fl;
195 outf = fopen(file, "w");
196 if (outf == NULL)
197 err(1, "%s", file);
198 for (fl = fl_head; fl != NULL; fl = tflp) {
199 fprintf(outf,
200 "#define %s %u\n", fl->f_fn, count ? fl->f_type : 0);
201 tflp = fl->f_next;
202 free(fl->f_fn);
203 free(fl);
205 fclose(outf);
209 * convert a dev name to a .h file name
211 static char *
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));
220 return(hbuf);
224 * convert a dev name to a macro name
226 static char *
227 tomacro(const char *dev)
229 static char mbuf[20];
230 char *cp;
232 cp = mbuf;
233 *cp++ = 'N';
234 while (*dev != 0)
235 *cp++ = islower(*dev) ? toupper(*dev++) : *dev++;
236 *cp++ = 0;
237 return(mbuf);