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 "$Id: mkheaders.c,v 1.9 1999/04/17 14:41:40 peter Exp $"; 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 #define ns(s) strdup(s) 54 55 static void do_header __P((char *, char *, int)); 56 static void do_count __P((char *, char *, int)); 57 static char *toheader __P((char *)); 58 static char *tomacro __P((char *)); 59 60 void 61 headers() 62 { 63 register struct file_list *fl; 64 struct device *dp; 65 66 for (fl = ftab; fl != 0; fl = fl->f_next) 67 if (fl->f_needs != 0) 68 do_count(fl->f_needs, fl->f_needs, 1); 69 for (dp = dtab; dp != 0; dp = dp->d_next) { 70 if ((dp->d_type & TYPEMASK) == PSEUDO_DEVICE) { 71 if (!(dp->d_type & DEVDONE)) 72 printf("Warning: pseudo-device \"%s\" is unknown\n", 73 dp->d_name); 74 else 75 dp->d_type &= TYPEMASK; 76 } 77 if ((dp->d_type & TYPEMASK) == DEVICE) { 78 if (!(dp->d_type & DEVDONE)) 79 printf("Warning: device \"%s\" is unknown\n", 80 dp->d_name); 81 else 82 dp->d_type &= TYPEMASK; 83 } 84 } 85 } 86 87 /* 88 * count all the devices of a certain type and recurse to count 89 * whatever the device is connected to 90 */ 91 static void 92 do_count(dev, hname, search) 93 register char *dev, *hname; 94 int search; 95 { 96 register struct device *dp, *mp; 97 register int count, hicount; 98 99 /* 100 * After this loop, "count" will be the actual number of units, 101 * and "hicount" will be the highest unit declared. do_header() 102 * must use this higher of these values. 103 */ 104 for (hicount = count = 0, dp = dtab; dp != 0; dp = dp->d_next) 105 if (dp->d_unit != -1 && eq(dp->d_name, dev)) { 106 if ((dp->d_type & TYPEMASK) == PSEUDO_DEVICE) { 107 count = 108 dp->d_slave != UNKNOWN ? dp->d_slave : 1; 109 dp->d_type |= DEVDONE; 110 break; 111 } 112 if ((dp->d_type & TYPEMASK) == DEVICE) 113 dp->d_type |= DEVDONE; 114 count++; 115 /* 116 * Allow holes in unit numbering, 117 * assumption is unit numbering starts 118 * at zero. 119 */ 120 if (dp->d_unit + 1 > hicount) 121 hicount = dp->d_unit + 1; 122 if (search) { 123 mp = dp->d_conn; 124 if (mp != 0 && mp != TO_NEXUS && 125 mp->d_conn != 0 && mp->d_conn != TO_NEXUS) { 126 do_count(mp->d_name, hname, 0); 127 search = 0; 128 } 129 } 130 } 131 do_header(dev, hname, count > hicount ? count : hicount); 132 } 133 134 static void 135 do_header(dev, hname, count) 136 char *dev, *hname; 137 int count; 138 { 139 char *file, *name, *inw; 140 struct file_list *fl, *fl_head, *tflp; 141 FILE *inf, *outf; 142 int inc, oldcount; 143 144 file = toheader(hname); 145 name = tomacro(dev); 146 inf = fopen(file, "r"); 147 oldcount = -1; 148 if (inf == 0) { 149 outf = fopen(file, "w"); 150 if (outf == 0) 151 err(1, "%s", file); 152 fprintf(outf, "#define %s %d\n", name, count); 153 (void) fclose(outf); 154 return; 155 } 156 fl_head = NULL; 157 for (;;) { 158 char *cp; 159 if ((inw = get_word(inf)) == 0 || inw == (char *)EOF) 160 break; 161 if ((inw = get_word(inf)) == 0 || inw == (char *)EOF) 162 break; 163 inw = ns(inw); 164 cp = get_word(inf); 165 if (cp == 0 || cp == (char *)EOF) 166 break; 167 inc = atoi(cp); 168 if (eq(inw, name)) { 169 oldcount = inc; 170 inc = count; 171 } 172 cp = get_word(inf); 173 if (cp == (char *)EOF) 174 break; 175 fl = (struct file_list *) malloc(sizeof *fl); 176 bzero(fl, sizeof(*fl)); 177 fl->f_fn = inw; /* malloced */ 178 fl->f_type = inc; 179 fl->f_next = fl_head; 180 fl_head = fl; 181 } 182 (void) fclose(inf); 183 if (count == oldcount) { 184 for (fl = fl_head; fl != NULL; fl = tflp) { 185 tflp = fl->f_next; 186 free(fl->f_fn); 187 free(fl); 188 } 189 return; 190 } 191 if (oldcount == -1) { 192 fl = (struct file_list *) malloc(sizeof *fl); 193 bzero(fl, sizeof(*fl)); 194 fl->f_fn = ns(name); 195 fl->f_type = count; 196 fl->f_next = fl_head; 197 fl_head = fl; 198 } 199 outf = fopen(file, "w"); 200 if (outf == 0) 201 err(1, "%s", file); 202 for (fl = fl_head; fl != NULL; fl = tflp) { 203 fprintf(outf, 204 "#define %s %u\n", fl->f_fn, count ? fl->f_type : 0); 205 tflp = fl->f_next; 206 free(fl->f_fn); 207 free(fl); 208 } 209 (void) fclose(outf); 210 } 211 212 /* 213 * convert a dev name to a .h file name 214 */ 215 static char * 216 toheader(dev) 217 char *dev; 218 { 219 static char hbuf[80]; 220 221 (void) strcpy(hbuf, path(dev)); 222 (void) strcat(hbuf, ".h"); 223 return (hbuf); 224 } 225 226 /* 227 * convert a dev name to a macro name 228 */ 229 static char * 230 tomacro(dev) 231 register char *dev; 232 { 233 static char mbuf[20]; 234 register char *cp; 235 236 cp = mbuf; 237 *cp++ = 'N'; 238 while (*dev) 239 *cp++ = islower(*dev) ? toupper(*dev++) : *dev++; 240 *cp++ = 0; 241 return (mbuf); 242 } 243