1 /* 2 * Copyright (c) 1980, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef lint 35 #if 0 36 static char sccsid[] = "@(#)mkheaders.c 8.1 (Berkeley) 6/6/93"; 37 #endif 38 static const char rcsid[] = 39 "$FreeBSD$"; 40 #endif /* not lint */ 41 42 /* 43 * Make all the .h files for the optional entries 44 */ 45 46 #include <ctype.h> 47 #include <err.h> 48 #include <stdio.h> 49 #include <string.h> 50 #include "config.h" 51 #include "y.tab.h" 52 53 static void do_header __P((char *, char *, int)); 54 static void do_count __P((char *, char *, int)); 55 static char *toheader __P((char *)); 56 static char *tomacro __P((char *)); 57 58 void 59 headers() 60 { 61 register struct file_list *fl; 62 struct device *dp; 63 64 for (fl = ftab; fl != 0; fl = fl->f_next) 65 if (fl->f_needs != 0) 66 do_count(fl->f_needs, fl->f_needs, 1); 67 for (dp = dtab; dp != 0; dp = dp->d_next) { 68 if ((dp->d_type & TYPEMASK) == PSEUDO_DEVICE) { 69 if (!(dp->d_type & DEVDONE)) 70 printf("Warning: pseudo-device \"%s\" is unknown\n", 71 dp->d_name); 72 else 73 dp->d_type &= TYPEMASK; 74 } 75 if ((dp->d_type & TYPEMASK) == DEVICE) { 76 if (!(dp->d_type & DEVDONE)) 77 printf("Warning: device \"%s\" is unknown\n", 78 dp->d_name); 79 else 80 dp->d_type &= TYPEMASK; 81 } 82 if ((dp->d_type & TYPEMASK) == CONTROLLER) { 83 if (!(dp->d_type & DEVDONE)) 84 printf("Warning: controller \"%s\" is unknown\n", 85 dp->d_name); 86 else 87 dp->d_type &= TYPEMASK; 88 } 89 } 90 } 91 92 /* 93 * count all the devices of a certain type and recurse to count 94 * whatever the device is connected to 95 */ 96 static void 97 do_count(dev, hname, search) 98 register char *dev, *hname; 99 int search; 100 { 101 register struct device *dp, *mp; 102 register int count, hicount; 103 104 /* 105 * After this loop, "count" will be the actual number of units, 106 * and "hicount" will be the highest unit declared. do_header() 107 * must use this higher of these values. 108 */ 109 for (hicount = count = 0, dp = dtab; dp != 0; dp = dp->d_next) { 110 if (eq(dp->d_name, dev)) { 111 if ((dp->d_type & TYPEMASK) == PSEUDO_DEVICE) 112 dp->d_type |= DEVDONE; 113 else if ((dp->d_type & TYPEMASK) == DEVICE) 114 dp->d_type |= DEVDONE; 115 else if ((dp->d_type & TYPEMASK) == CONTROLLER) 116 dp->d_type |= DEVDONE; 117 } 118 if (dp->d_unit != -1 && eq(dp->d_name, dev)) { 119 if ((dp->d_type & TYPEMASK) == PSEUDO_DEVICE) { 120 count = 121 dp->d_count != UNKNOWN ? dp->d_count : 1; 122 break; 123 } 124 count++; 125 /* 126 * Allow holes in unit numbering, 127 * assumption is unit numbering starts 128 * at zero. 129 */ 130 if (dp->d_unit + 1 > hicount) 131 hicount = dp->d_unit + 1; 132 if (search) { 133 mp = dp->d_conn; 134 if (mp != 0 && mp != TO_NEXUS && 135 mp->d_conn != 0 && mp->d_conn != TO_NEXUS) { 136 do_count(mp->d_name, hname, 0); 137 search = 0; 138 } 139 } 140 } 141 } 142 do_header(dev, hname, count > hicount ? count : hicount); 143 } 144 145 static void 146 do_header(dev, hname, count) 147 char *dev, *hname; 148 int count; 149 { 150 char *file, *name, *inw; 151 struct file_list *fl, *fl_head, *tflp; 152 FILE *inf, *outf; 153 int inc, oldcount; 154 155 file = toheader(hname); 156 name = tomacro(dev); 157 inf = fopen(file, "r"); 158 oldcount = -1; 159 if (inf == 0) { 160 outf = fopen(file, "w"); 161 if (outf == 0) 162 err(1, "%s", file); 163 fprintf(outf, "#define %s %d\n", name, count); 164 (void) fclose(outf); 165 return; 166 } 167 fl_head = NULL; 168 for (;;) { 169 char *cp; 170 if ((inw = get_word(inf)) == 0 || inw == (char *)EOF) 171 break; 172 if ((inw = get_word(inf)) == 0 || inw == (char *)EOF) 173 break; 174 inw = ns(inw); 175 cp = get_word(inf); 176 if (cp == 0 || cp == (char *)EOF) 177 break; 178 inc = atoi(cp); 179 if (eq(inw, name)) { 180 oldcount = inc; 181 inc = count; 182 } 183 cp = get_word(inf); 184 if (cp == (char *)EOF) 185 break; 186 fl = (struct file_list *) malloc(sizeof *fl); 187 bzero(fl, sizeof(*fl)); 188 fl->f_fn = inw; /* malloced */ 189 fl->f_type = inc; 190 fl->f_next = fl_head; 191 fl_head = fl; 192 } 193 (void) fclose(inf); 194 if (count == oldcount) { 195 for (fl = fl_head; fl != NULL; fl = tflp) { 196 tflp = fl->f_next; 197 free(fl->f_fn); 198 free(fl); 199 } 200 return; 201 } 202 if (oldcount == -1) { 203 fl = (struct file_list *) malloc(sizeof *fl); 204 bzero(fl, sizeof(*fl)); 205 fl->f_fn = ns(name); 206 fl->f_type = count; 207 fl->f_next = fl_head; 208 fl_head = fl; 209 } 210 outf = fopen(file, "w"); 211 if (outf == 0) 212 err(1, "%s", file); 213 for (fl = fl_head; fl != NULL; fl = tflp) { 214 fprintf(outf, 215 "#define %s %u\n", fl->f_fn, count ? fl->f_type : 0); 216 tflp = fl->f_next; 217 free(fl->f_fn); 218 free(fl); 219 } 220 (void) fclose(outf); 221 } 222 223 /* 224 * convert a dev name to a .h file name 225 */ 226 static char * 227 toheader(dev) 228 char *dev; 229 { 230 static char hbuf[80]; 231 232 (void) strcpy(hbuf, path(dev)); 233 (void) strcat(hbuf, ".h"); 234 return (hbuf); 235 } 236 237 /* 238 * convert a dev name to a macro name 239 */ 240 static char * 241 tomacro(dev) 242 register char *dev; 243 { 244 static char mbuf[20]; 245 register char *cp; 246 247 cp = mbuf; 248 *cp++ = 'N'; 249 while (*dev) 250 *cp++ = islower(*dev) ? toupper(*dev++) : *dev++; 251 *cp++ = 0; 252 return (mbuf); 253 } 254