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