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