mkheaders.c revision 16023
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 35static char sccsid[] = "@(#)mkheaders.c 8.1 (Berkeley) 6/6/93"; 36#endif /* not lint */ 37 38/* 39 * Make all the .h files for the optional entries 40 */ 41 42#include <stdio.h> 43#include <ctype.h> 44#include "config.h" 45#include "config.tab.h" 46 47headers() 48{ 49 register struct file_list *fl; 50 51 for (fl = ftab; fl != 0; fl = fl->f_next) 52 if (fl->f_needs != 0) 53 do_count(fl->f_needs, fl->f_needs, 1); 54} 55 56/* 57 * count all the devices of a certain type and recurse to count 58 * whatever the device is connected to 59 */ 60do_count(dev, hname, search) 61 register char *dev, *hname; 62 int search; 63{ 64 register struct device *dp, *mp; 65 register int count, hicount; 66 67 /* 68 * After this loop, "count" will be the actual number of units, 69 * and "hicount" will be the highest unit declared. do_header() 70 * must use this higher of these values. 71 */ 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