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