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