mkheaders.c revision 16073
1148771Scperciva/* 2148771Scperciva * Copyright (c) 1980, 1993 3148771Scperciva * The Regents of the University of California. All rights reserved. 4148771Scperciva * 5148771Scperciva * Redistribution and use in source and binary forms, with or without 6148771Scperciva * modification, are permitted provided that the following conditions 7148771Scperciva * are met: 8148771Scperciva * 1. Redistributions of source code must retain the above copyright 9148771Scperciva * notice, this list of conditions and the following disclaimer. 10148771Scperciva * 2. Redistributions in binary form must reproduce the above copyright 11148771Scperciva * notice, this list of conditions and the following disclaimer in the 12148771Scperciva * documentation and/or other materials provided with the distribution. 13148771Scperciva * 3. All advertising materials mentioning features or use of this software 14148771Scperciva * must display the following acknowledgement: 15148771Scperciva * This product includes software developed by the University of 16148771Scperciva * California, Berkeley and its contributors. 17148771Scperciva * 4. Neither the name of the University nor the names of its contributors 18148771Scperciva * may be used to endorse or promote products derived from this software 19148771Scperciva * without specific prior written permission. 20148771Scperciva * 21148771Scperciva * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22148771Scperciva * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23148771Scperciva * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24148771Scperciva * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25148771Scperciva * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26148771Scperciva * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27148771Scperciva * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28148771Scperciva * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29148771Scperciva * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30152573Sru * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31148771Scperciva * SUCH DAMAGE. 32148771Scperciva */ 33152573Sru 34152573Sru#ifndef lint 35148771Scpercivastatic char sccsid[] = "@(#)mkheaders.c 8.1 (Berkeley) 6/6/93"; 36148771Scperciva#endif /* not lint */ 37152573Sru 38148771Scperciva/* 39152573Sru * Make all the .h files for the optional entries 40148771Scperciva */ 41152573Sru 42148771Scperciva#include <stdio.h> 43152573Sru#include <ctype.h> 44148771Scperciva#include "config.h" 45152573Sru#include "y.tab.h" 46148771Scperciva 47152573Sruheaders() 48148771Scperciva{ 49152573Sru register struct file_list *fl; 50152573Sru 51152573Sru for (fl = ftab; fl != 0; fl = fl->f_next) 52148771Scperciva if (fl->f_needs != 0) 53152573Sru do_count(fl->f_needs, fl->f_needs, 1); 54148771Scperciva} 55152573Sru 56152573Sru/* 57152573Sru * count all the devices of a certain type and recurse to count 58152573Sru * whatever the device is connected to 59152573Sru */ 60148771Scpercivado_count(dev, hname, search) 61148771Scperciva register char *dev, *hname; 62148771Scperciva int search; 63148771Scperciva{ 64208027Suqs register struct device *dp, *mp; 65208027Suqs register int count, hicount; 66198671Scperciva 67198671Scperciva /* 68198671Scperciva * After this loop, "count" will be the actual number of units, 69198671Scperciva * and "hicount" will be the highest unit declared. do_header() 70198671Scperciva * must use this higher of these values. 71198671Scperciva */ 72198671Scperciva for (hicount = count = 0, dp = dtab; dp != 0; dp = dp->d_next) 73198671Scperciva if (dp->d_unit != -1 && eq(dp->d_name, dev)) { 74198671Scperciva if (dp->d_type == PSEUDO_DEVICE) { 75198671Scperciva count = 76198671Scperciva dp->d_slave != UNKNOWN ? dp->d_slave : 1; 77198671Scperciva break; 78198671Scperciva } 79198671Scperciva count++; 80198671Scperciva /* 81198671Scperciva * Allow holes in unit numbering, 82198671Scperciva * assumption is unit numbering starts 83198671Scperciva * at zero. 84198671Scperciva */ 85198671Scperciva if (dp->d_unit + 1 > hicount) 86198671Scperciva 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