xref: /xnu-12377.81.4/SETUP/config/mkheaders.c (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
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
free_file_list(struct file_list * fl)108 free_file_list(struct file_list *fl)
109 {
110 	struct file_list *fl_prev;
111 	while (fl != 0) {
112 		fl_prev = fl;
113 		fl = fl->f_next;
114 		free((char *)fl_prev);
115 	}
116 }
117 
118 static void
do_header(const char * dev,const char * hname,int count)119 do_header(const char *dev, const char *hname, int count)
120 {
121 	char *file, *name;
122 	const char *inw;
123 	char *inwcopy;
124 	struct file_list *fl = NULL;    /* may exit for(;;) uninitted */
125 	struct file_list *fl_head;
126 	FILE *inf, *outf;
127 	int inc, oldcount;
128 
129 	file = toheader(hname);
130 	name = tomacro(dev?dev:hname) + (dev == NULL);
131 	inf = fopen(file, "r");
132 	oldcount = -1;
133 	if (inf == 0) {
134 		(void) unlink(file);
135 		outf = fopen(file, "w");
136 		if (outf == 0) {
137 			perror(file);
138 			exit(1);
139 		}
140 		fprintf(outf, "#define %s %d\n", name, count);
141 		(void) fclose(outf);
142 		file = path("meta_features.h");
143 		outf = fopen(file, "a");
144 		if (outf == 0) {
145 			perror(file);
146 			exit(1);
147 		}
148 		fprintf(outf, "#include <%s.h>\n", hname);
149 		(void) fclose(outf);
150 		return;
151 	}
152 	fl_head = 0;
153 	for (;;) {
154 		const char *cp;
155 		if ((inw = get_word(inf)) == 0 || inw == (char *)EOF) {
156 			break;
157 		}
158 		if ((inw = get_word(inf)) == 0 || inw == (char *)EOF) {
159 			break;
160 		}
161 		inwcopy = ns(inw);
162 		cp = get_word(inf);
163 		if (cp == 0 || cp == (char *)EOF) {
164 			break;
165 		}
166 		inc = atoi(cp);
167 		if (eq(inwcopy, name)) {
168 			oldcount = inc;
169 			inc = count;
170 		}
171 		cp = get_word(inf);
172 		if (cp == (char *)EOF) {
173 			break;
174 		}
175 		fl = (struct file_list *) malloc(sizeof *fl);
176 		fl->f_fn = inwcopy;
177 		fl->f_type = inc;
178 		fl->f_next = fl_head;
179 		fl_head = fl;
180 	}
181 	(void) fclose(inf);
182 	if (count == oldcount) {
183 		free_file_list(fl_head);
184 		return;
185 	}
186 	if (oldcount == -1) {
187 		fl = (struct file_list *) malloc(sizeof *fl);
188 		fl->f_fn = name;
189 		fl->f_type = count;
190 		fl->f_next = fl_head;
191 		fl_head = fl;
192 	}
193 	unlink(file);
194 	outf = fopen(file, "w");
195 	if (outf == 0) {
196 		perror(file);
197 		exit(1);
198 	}
199 	for (fl = fl_head; fl != 0; fl = fl->f_next) {
200 		fprintf(outf, "#define %s %d\n",
201 		    fl->f_fn, count ? fl->f_type : 0);
202 	}
203 	free_file_list(fl_head);
204 	(void) fclose(outf);
205 }
206 
207 /*
208  * convert a dev name to a .h file name
209  */
210 static char *
toheader(const char * dev)211 toheader(const char *dev)
212 {
213 	static char hbuf[MAXPATHLEN];
214 	(void) snprintf(hbuf, sizeof hbuf, "%s.h", path(dev));
215 	hbuf[MAXPATHLEN - 1] = '\0';
216 	return hbuf;
217 }
218 
219 /*
220  * convert a dev name to a macro name
221  */
222 static char *
tomacro(const char * dev)223 tomacro(const char *dev)
224 {
225 	static char mbuf[FILENAME_MAX];
226 	char *cp;
227 
228 	cp = mbuf;
229 	*cp++ = 'N';
230 	while (*dev) {
231 		if (!islower(*dev)) {
232 			*cp++ = *dev++;
233 		} else {
234 			*cp++ = toupper(*dev++);
235 		}
236 	}
237 	*cp++ = 0;
238 	return mbuf;
239 }
240