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