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