xref: /xnu-8019.80.24/SETUP/config/mkheaders.c (revision a325d9c4a84054e40bbe985afedcb50ab80993ea)
1 /*
2  * Copyright (c) 1999-2006 Apple Computer, Inc. All rights reserved.
3  *
4  * @APPLE_LICENSE_HEADER_START@
5  *
6  * "Portions Copyright (c) 1999 Apple Computer, Inc.  All Rights
7  * Reserved.  This file contains Original Code and/or Modifications of
8  * Original Code as defined in and that are subject to the Apple Public
9  * Source License Version 1.0 (the 'License').  You may not use this file
10  * except in compliance with the License.  Please obtain a copy of the
11  * License at http://www.apple.com/publicsource and read it before using
12  * this file.
13  *
14  * The Original Code and all software distributed under the License are
15  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
19  * License for the specific language governing rights and limitations
20  * under the License."
21  *
22  * @APPLE_LICENSE_HEADER_END@
23  */
24 /*
25  * Mach Operating System
26  * Copyright (c) 1990 Carnegie-Mellon University
27  * Copyright (c) 1989 Carnegie-Mellon University
28  * Copyright (c) 1988 Carnegie-Mellon University
29  * Copyright (c) 1987 Carnegie-Mellon University
30  * All rights reserved.  The CMU software License Agreement specifies
31  * the terms and conditions for use and redistribution.
32  */
33 
34 /*
35  * Copyright (c) 1980 Regents of the University of California.
36  * All rights reserved.
37  *
38  * Redistribution and use in source and binary forms are permitted
39  * provided that the above copyright notice and this paragraph are
40  * duplicated in all such forms and that any documentation,
41  * advertising materials, and other materials related to such
42  * distribution and use acknowledge that the software was developed
43  * by the University of California, Berkeley.  The name of the
44  * University may not be used to endorse or promote products derived
45  * from this software without specific prior written permission.
46  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
47  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
48  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
49  */
50 
51 #ifndef lint
52 static char sccsid[] __attribute__((used)) = "@(#)mkheaders.c	5.5 (Berkeley) 6/18/88";
53 #endif /* not lint */
54 
55 /*
56  * Make all the .h files for the optional entries
57  */
58 
59 #include <stdio.h>
60 #include <unistd.h>     /* unlink */
61 #include <ctype.h>
62 #include "config.h"
63 #include "parser.h"
64 
65 static void     do_count(const char *dev, const char *hname, int search);
66 static void     do_header(const char *dev, const char *hname, int count);
67 static char     *toheader(const char *dev);
68 static char     *tomacro(const char *dev);
69 
70 void
headers(void)71 headers(void)
72 {
73 	struct file_list *fl;
74 
75 	for (fl = ftab; fl != 0; fl = fl->f_next) {
76 		if (fl->f_needs != 0) {
77 			do_count(fl->f_needs, fl->f_needs, 1);
78 		}
79 	}
80 }
81 
82 /*
83  * count all the devices of a certain type and recurse to count
84  * whatever the device is connected to
85  */
86 void
do_count(const char * dev,const char * hname,int search)87 do_count(const char *dev, const char *hname, int search)
88 {
89 	struct device *dp;
90 	int count;
91 
92 	for (count = 0, dp = dtab; dp != 0; dp = dp->d_next) {
93 		if (eq(dp->d_name, dev)) {
94 			if (dp->d_type == PSEUDO_DEVICE) {
95 				count =
96 				    dp->d_slave != UNKNOWN ? dp->d_slave : 1;
97 				if (dp->d_flags) {
98 					dev = NULL;
99 				}
100 				break;
101 			}
102 		}
103 	}
104 	do_header(dev, hname, count);
105 }
106 
107 static void
do_header(const char * dev,const char * hname,int count)108 do_header(const char *dev, const char *hname, int count)
109 {
110 	char *file, *name;
111 	const char *inw;
112 	char *inwcopy;
113 	struct file_list *fl = NULL;    /* may exit for(;;) uninitted */
114 	struct file_list *fl_head, *fl_prev;
115 	FILE *inf, *outf;
116 	int inc, oldcount;
117 
118 	file = toheader(hname);
119 	name = tomacro(dev?dev:hname) + (dev == NULL);
120 	inf = fopen(file, "r");
121 	oldcount = -1;
122 	if (inf == 0) {
123 		(void) unlink(file);
124 		outf = fopen(file, "w");
125 		if (outf == 0) {
126 			perror(file);
127 			exit(1);
128 		}
129 		fprintf(outf, "#define %s %d\n", name, count);
130 		(void) fclose(outf);
131 		file = path("meta_features.h");
132 		outf = fopen(file, "a");
133 		if (outf == 0) {
134 			perror(file);
135 			exit(1);
136 		}
137 		fprintf(outf, "#include <%s.h>\n", hname);
138 		(void) fclose(outf);
139 		return;
140 	}
141 	fl_head = 0;
142 	for (;;) {
143 		const char *cp;
144 		if ((inw = get_word(inf)) == 0 || inw == (char *)EOF) {
145 			break;
146 		}
147 		if ((inw = get_word(inf)) == 0 || inw == (char *)EOF) {
148 			break;
149 		}
150 		inwcopy = ns(inw);
151 		cp = get_word(inf);
152 		if (cp == 0 || cp == (char *)EOF) {
153 			break;
154 		}
155 		inc = atoi(cp);
156 		if (eq(inwcopy, name)) {
157 			oldcount = inc;
158 			inc = count;
159 		}
160 		cp = get_word(inf);
161 		if (cp == (char *)EOF) {
162 			break;
163 		}
164 		fl = (struct file_list *) malloc(sizeof *fl);
165 		fl->f_fn = inwcopy;
166 		fl->f_type = inc;
167 		fl->f_next = fl_head;
168 		fl_head = fl;
169 	}
170 	(void) fclose(inf);
171 	if (count == oldcount) {
172 		while (fl != 0) {
173 			fl_prev = fl;
174 			fl = fl->f_next;
175 			free((char *)fl_prev);
176 		}
177 		return;
178 	}
179 	if (oldcount == -1) {
180 		fl = (struct file_list *) malloc(sizeof *fl);
181 		fl->f_fn = name;
182 		fl->f_type = count;
183 		fl->f_next = fl_head;
184 		fl_head = fl;
185 	}
186 	unlink(file);
187 	outf = fopen(file, "w");
188 	if (outf == 0) {
189 		perror(file);
190 		exit(1);
191 	}
192 	for (fl = fl_head; fl != 0; fl = fl->f_next) {
193 		fprintf(outf, "#define %s %d\n",
194 		    fl->f_fn, count ? fl->f_type : 0);
195 		free((char *)fl);
196 	}
197 	(void) fclose(outf);
198 }
199 
200 /*
201  * convert a dev name to a .h file name
202  */
203 static char *
toheader(const char * dev)204 toheader(const char *dev)
205 {
206 	static char hbuf[MAXPATHLEN];
207 	(void) snprintf(hbuf, sizeof hbuf, "%s.h", path(dev));
208 	hbuf[MAXPATHLEN - 1] = '\0';
209 	return hbuf;
210 }
211 
212 /*
213  * convert a dev name to a macro name
214  */
215 static char *
tomacro(const char * dev)216 tomacro(const char *dev)
217 {
218 	static char mbuf[FILENAME_MAX];
219 	char *cp;
220 
221 	cp = mbuf;
222 	*cp++ = 'N';
223 	while (*dev) {
224 		if (!islower(*dev)) {
225 			*cp++ = *dev++;
226 		} else {
227 			*cp++ = toupper(*dev++);
228 		}
229 	}
230 	*cp++ = 0;
231 	return mbuf;
232 }
233