386BSD 0.0 development
[unix-history] / usr / src / usr.sbin / config / mkheaders.c
CommitLineData
23be2999
WJ
1/*
2 * Copyright (c) 1980 Regents of the University of California.
3 * All rights reserved.
4 *
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.
20 *
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.
32 */
33
34#ifndef lint
35static char sccsid[] = "@(#)mkheaders.c 5.7 (Berkeley) 7/1/91";
36#endif /* not lint */
37
38/*
39 * Make all the .h files for the optional entries
40 */
41
42#include <stdio.h>
43#include <ctype.h>
44#include "config.h"
45#include "y.tab.h"
46
47headers()
48{
49 register struct file_list *fl;
50
51 for (fl = ftab; fl != 0; fl = fl->f_next)
52 if (fl->f_needs != 0)
53 do_count(fl->f_needs, fl->f_needs, 1);
54}
55
56/*
57 * count all the devices of a certain type and recurse to count
58 * whatever the device is connected to
59 */
60do_count(dev, hname, search)
61 register char *dev, *hname;
62 int search;
63{
64 register struct device *dp, *mp;
65 register int count;
66
67 for (count = 0,dp = dtab; dp != 0; dp = dp->d_next)
68 if (dp->d_unit != -1 && eq(dp->d_name, dev)) {
69 if (dp->d_type == PSEUDO_DEVICE) {
70 count =
71 dp->d_slave != UNKNOWN ? dp->d_slave : 1;
72 break;
73 }
74 count++;
75 /*
76 * Allow holes in unit numbering,
77 * assumption is unit numbering starts
78 * at zero.
79 */
80 if (dp->d_unit + 1 > count)
81 count = dp->d_unit + 1;
82 if (search) {
83 mp = dp->d_conn;
84 if (mp != 0 && mp != TO_NEXUS &&
85 mp->d_conn != 0 && mp->d_conn != TO_NEXUS) {
86 do_count(mp->d_name, hname, 0);
87 search = 0;
88 }
89 }
90 }
91 do_header(dev, hname, count);
92}
93
94do_header(dev, hname, count)
95 char *dev, *hname;
96 int count;
97{
98 char *file, *name, *inw, *toheader(), *tomacro();
99 struct file_list *fl, *fl_head;
100 FILE *inf, *outf;
101 int inc, oldcount;
102
103 file = toheader(hname);
104 name = tomacro(dev);
105 inf = fopen(file, "r");
106 oldcount = -1;
107 if (inf == 0) {
108 outf = fopen(file, "w");
109 if (outf == 0) {
110 perror(file);
111 exit(1);
112 }
113 fprintf(outf, "#define %s %d\n", name, count);
114 (void) fclose(outf);
115 return;
116 }
117 fl_head = 0;
118 for (;;) {
119 char *cp;
120 if ((inw = get_word(inf)) == 0 || inw == (char *)EOF)
121 break;
122 if ((inw = get_word(inf)) == 0 || inw == (char *)EOF)
123 break;
124 inw = ns(inw);
125 cp = get_word(inf);
126 if (cp == 0 || cp == (char *)EOF)
127 break;
128 inc = atoi(cp);
129 if (eq(inw, name)) {
130 oldcount = inc;
131 inc = count;
132 }
133 cp = get_word(inf);
134 if (cp == (char *)EOF)
135 break;
136 fl = (struct file_list *) malloc(sizeof *fl);
137 bzero(fl, sizeof(*fl));
138 fl->f_fn = inw;
139 fl->f_type = inc;
140 fl->f_next = fl_head;
141 fl_head = fl;
142 }
143 (void) fclose(inf);
144 if (count == oldcount) {
145 for (fl = fl_head; fl != 0; fl = fl->f_next)
146 free((char *)fl);
147 return;
148 }
149 if (oldcount == -1) {
150 fl = (struct file_list *) malloc(sizeof *fl);
151 bzero(fl, sizeof(*fl));
152 fl->f_fn = name;
153 fl->f_type = count;
154 fl->f_next = fl_head;
155 fl_head = fl;
156 }
157 outf = fopen(file, "w");
158 if (outf == 0) {
159 perror(file);
160 exit(1);
161 }
162 for (fl = fl_head; fl != 0; fl = fl->f_next) {
163 fprintf(outf, "#define %s %u\n",
164 fl->f_fn, count ? fl->f_type : 0);
165 free((char *)fl);
166 }
167 (void) fclose(outf);
168}
169
170/*
171 * convert a dev name to a .h file name
172 */
173char *
174toheader(dev)
175 char *dev;
176{
177 static char hbuf[80];
178
179 (void) strcpy(hbuf, path(dev));
180 (void) strcat(hbuf, ".h");
181 return (hbuf);
182}
183
184/*
185 * convert a dev name to a macro name
186 */
187char *tomacro(dev)
188 register char *dev;
189{
190 static char mbuf[20];
191 register char *cp;
192
193 cp = mbuf;
194 *cp++ = 'N';
195 while (*dev)
196 *cp++ = toupper(*dev++);
197 *cp++ = 0;
198 return (mbuf);
199}