mkheaders.c revision 12772
1158787Sume/* 2158787Sume * Copyright (c) 1980, 1993 3158787Sume * The Regents of the University of California. All rights reserved. 4157308Sume * 5158787Sume * Redistribution and use in source and binary forms, with or without 6158787Sume * modification, are permitted provided that the following conditions 7158787Sume * are met: 8157308Sume * 1. Redistributions of source code must retain the above copyright 9158787Sume * notice, this list of conditions and the following disclaimer. 10158787Sume * 2. Redistributions in binary form must reproduce the above copyright 11158787Sume * notice, this list of conditions and the following disclaimer in the 12158787Sume * documentation and/or other materials provided with the distribution. 13158787Sume * 3. All advertising materials mentioning features or use of this software 14158787Sume * must display the following acknowledgement: 15158787Sume * This product includes software developed by the University of 16157308Sume * California, Berkeley and its contributors. 17157308Sume * 4. Neither the name of the University nor the names of its contributors 18158787Sume * may be used to endorse or promote products derived from this software 19158787Sume * without specific prior written permission. 20158787Sume * 21158787Sume * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22157308Sume * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23158787Sume * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24158787Sume * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25157308Sume * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26158787Sume * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27158787Sume * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28158787Sume * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29158787Sume * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30157308Sume * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31157308Sume * SUCH DAMAGE. 32157308Sume */ 33157308Sume 34158787Sume#ifndef lint 35158787Sumestatic char sccsid[] = "@(#)mkheaders.c 8.1 (Berkeley) 6/6/93"; 36158787Sume#endif /* not lint */ 37158787Sume 38158787Sume/* 39157308Sume * Make all the .h files for the optional entries 40158787Sume */ 41158787Sume 42157308Sume#include <stdio.h> 43157308Sume#include <ctype.h> 44158787Sume#include "config.h" 45157308Sume#include "y.tab.h" 46157308Sume 47157308Sumeheaders() 48157308Sume{ 49158787Sume register struct file_list *fl; 50157308Sume 51157308Sume for (fl = ftab; fl != 0; fl = fl->f_next) 52158787Sume if (fl->f_needs != 0) 53158787Sume do_count(fl->f_needs, fl->f_needs, 1); 54158787Sume} 55158787Sume 56157308Sume/* 57158787Sume * count all the devices of a certain type and recurse to count 58158787Sume * whatever the device is connected to 59158787Sume */ 60158787Sumedo_count(dev, hname, search) 61158787Sume register char *dev, *hname; 62158787Sume int search; 63157308Sume{ 64157308Sume register struct device *dp, *mp; 65158787Sume register int count, hicount; 66157308Sume 67158787Sume /* 68157308Sume * After this loop, "count" will be the actual number of units, 69157308Sume * and "hicount" will be the highest unit declared. do_header() 70157308Sume * must use this higher of these values. 71158787Sume */ 72 for (hicount = count = 0, dp = dtab; dp != 0; dp = dp->d_next) 73 if (dp->d_unit != -1 && eq(dp->d_name, dev)) { 74 if (dp->d_type == PSEUDO_DEVICE) { 75 count = 76 dp->d_slave != UNKNOWN ? dp->d_slave : 1; 77 break; 78 } 79 count++; 80 /* 81 * Allow holes in unit numbering, 82 * assumption is unit numbering starts 83 * at zero. 84 */ 85 if (dp->d_unit + 1 > hicount) 86 hicount = dp->d_unit + 1; 87 if (search) { 88 mp = dp->d_conn; 89 if (mp != 0 && mp != TO_NEXUS && 90 mp->d_conn != 0 && mp->d_conn != TO_NEXUS) { 91 do_count(mp->d_name, hname, 0); 92 search = 0; 93 } 94 } 95 } 96 do_header(dev, hname, count > hicount ? count : hicount); 97} 98 99do_header(dev, hname, count) 100 char *dev, *hname; 101 int count; 102{ 103 char *file, *name, *inw, *toheader(), *tomacro(); 104 struct file_list *fl, *fl_head, *tflp; 105 FILE *inf, *outf; 106 int inc, oldcount; 107 108 file = toheader(hname); 109 name = tomacro(dev); 110 inf = fopen(file, "r"); 111 oldcount = -1; 112 if (inf == 0) { 113 outf = fopen(file, "w"); 114 if (outf == 0) { 115 perror(file); 116 exit(1); 117 } 118 fprintf(outf, "#define %s %d\n", name, count); 119 (void) fclose(outf); 120 return; 121 } 122 fl_head = NULL; 123 for (;;) { 124 char *cp; 125 if ((inw = get_word(inf)) == 0 || inw == (char *)EOF) 126 break; 127 if ((inw = get_word(inf)) == 0 || inw == (char *)EOF) 128 break; 129 inw = ns(inw); 130 cp = get_word(inf); 131 if (cp == 0 || cp == (char *)EOF) 132 break; 133 inc = atoi(cp); 134 if (eq(inw, name)) { 135 oldcount = inc; 136 inc = count; 137 } 138 cp = get_word(inf); 139 if (cp == (char *)EOF) 140 break; 141 fl = (struct file_list *) malloc(sizeof *fl); 142 bzero(fl, sizeof(*fl)); 143 fl->f_fn = inw; /* malloced */ 144 fl->f_type = inc; 145 fl->f_next = fl_head; 146 fl_head = fl; 147 } 148 (void) fclose(inf); 149 if (count == oldcount) { 150 for (fl = fl_head; fl != NULL; fl = tflp) { 151 tflp = fl->f_next; 152 free(fl->f_fn); 153 free(fl); 154 } 155 return; 156 } 157 if (oldcount == -1) { 158 fl = (struct file_list *) malloc(sizeof *fl); 159 bzero(fl, sizeof(*fl)); 160 fl->f_fn = ns(name); /* malloced */ 161 fl->f_type = count; 162 fl->f_next = fl_head; 163 fl_head = fl; 164 } 165 outf = fopen(file, "w"); 166 if (outf == 0) { 167 perror(file); 168 exit(1); 169 } 170 for (fl = fl_head; fl != NULL; fl = tflp) { 171 fprintf(outf, 172 "#define %s %u\n", fl->f_fn, count ? fl->f_type : 0); 173 tflp = fl->f_next; 174 free(fl->f_fn); 175 free(fl); 176 } 177 (void) fclose(outf); 178} 179 180/* 181 * convert a dev name to a .h file name 182 */ 183char * 184toheader(dev) 185 char *dev; 186{ 187 static char hbuf[80]; 188 189 (void) strcpy(hbuf, path(dev)); 190 (void) strcat(hbuf, ".h"); 191 return (hbuf); 192} 193 194/* 195 * convert a dev name to a macro name 196 */ 197char *tomacro(dev) 198 register char *dev; 199{ 200 static char mbuf[20]; 201 register char *cp; 202 203 cp = mbuf; 204 *cp++ = 'N'; 205 while (*dev) 206 *cp++ = islower(*dev) ? toupper(*dev++) : *dev++; 207 *cp++ = 0; 208 return (mbuf); 209} 210