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