mkheaders.c revision 71878
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 36static char sccsid[] = "@(#)mkheaders.c 8.1 (Berkeley) 6/6/93"; 37#endif 38static const char rcsid[] = 39 "$FreeBSD: head/usr.sbin/config/mkheaders.c 71878 2001-01-31 10:06:06Z peter $"; 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 <sys/param.h> 51#include "config.h" 52#include "y.tab.h" 53 54static void do_header(char *, int); 55static char *toheader(char *); 56static char *tomacro(char *); 57 58void 59headers(void) 60{ 61 struct file_list *fl; 62 struct device *dp; 63 int match; 64 65 for (fl = ftab; fl != 0; fl = fl->f_next) { 66 if (fl->f_needs != 0) { 67 match = 0; 68 for (dp = dtab; dp != 0; dp = dp->d_next) { 69 if (eq(dp->d_name, fl->f_needs)) { 70 match++; 71 if ((dp->d_type & TYPEMASK) == DEVICE) 72 dp->d_type |= DEVDONE; 73 } 74 } 75 if (fl->f_flags & NEED_COUNT) 76 do_header(fl->f_needs, match); 77 } 78 } 79 for (dp = dtab; dp != 0; dp = dp->d_next) { 80 if ((dp->d_type & TYPEMASK) == DEVICE) { 81 if (!(dp->d_type & DEVDONE)) 82 errx(1, "Error: device \"%s\" is unknown", 83 dp->d_name); 84 } 85 } 86} 87 88static void 89do_header(char *dev, int match) 90{ 91 char *file, *name, *inw; 92 struct file_list *fl, *fl_head, *tflp; 93 struct device *dp; 94 FILE *inf, *outf; 95 int inc, oldcount; 96 int count, hicount; 97 98 /* 99 * After this loop, "count" will be the actual number of units, 100 * and "hicount" will be the highest unit declared. do_header() 101 * must use this higher of these values. 102 */ 103 for (hicount = count = 0, dp = dtab; dp != 0; dp = dp->d_next) { 104 if (eq(dp->d_name, dev)) { 105 count = 106 dp->d_count != UNKNOWN ? dp->d_count : 1; 107 break; 108 } 109 } 110 file = toheader(dev); 111 name = tomacro(dev); 112 if (match) 113 printf("Note: static unit limits for %s are set (%s = %d)\n", dev, name, count); 114 remember(file); 115 inf = fopen(file, "r"); 116 oldcount = -1; 117 if (inf == 0) { 118 outf = fopen(file, "w"); 119 if (outf == 0) 120 err(1, "%s", file); 121 fprintf(outf, "#define %s %d\n", name, count); 122 (void) fclose(outf); 123 return; 124 } 125 fl_head = NULL; 126 for (;;) { 127 char *cp; 128 if ((inw = get_word(inf)) == 0 || inw == (char *)EOF) 129 break; 130 if ((inw = get_word(inf)) == 0 || inw == (char *)EOF) 131 break; 132 inw = ns(inw); 133 cp = get_word(inf); 134 if (cp == 0 || cp == (char *)EOF) 135 break; 136 inc = atoi(cp); 137 if (eq(inw, name)) { 138 oldcount = inc; 139 inc = count; 140 } 141 cp = get_word(inf); 142 if (cp == (char *)EOF) 143 break; 144 fl = (struct file_list *) malloc(sizeof *fl); 145 bzero(fl, sizeof(*fl)); 146 fl->f_fn = inw; /* malloced */ 147 fl->f_type = inc; 148 fl->f_next = fl_head; 149 fl_head = fl; 150 } 151 (void) fclose(inf); 152 if (count == oldcount) { 153 for (fl = fl_head; fl != NULL; fl = tflp) { 154 tflp = fl->f_next; 155 free(fl->f_fn); 156 free(fl); 157 } 158 return; 159 } 160 if (oldcount == -1) { 161 fl = (struct file_list *) malloc(sizeof *fl); 162 bzero(fl, sizeof(*fl)); 163 fl->f_fn = ns(name); 164 fl->f_type = count; 165 fl->f_next = fl_head; 166 fl_head = fl; 167 } 168 outf = fopen(file, "w"); 169 if (outf == 0) 170 err(1, "%s", file); 171 for (fl = fl_head; fl != NULL; fl = tflp) { 172 fprintf(outf, 173 "#define %s %u\n", fl->f_fn, count ? fl->f_type : 0); 174 tflp = fl->f_next; 175 free(fl->f_fn); 176 free(fl); 177 } 178 (void) fclose(outf); 179} 180 181/* 182 * convert a dev name to a .h file name 183 */ 184static char * 185toheader(char *dev) 186{ 187 static char hbuf[MAXPATHLEN]; 188 189 snprintf(hbuf, sizeof(hbuf), "%s.h", path(dev)); 190 return (hbuf); 191} 192 193/* 194 * convert a dev name to a macro name 195 */ 196static char * 197tomacro(char *dev) 198{ 199 static char mbuf[20]; 200 char *cp; 201 202 cp = mbuf; 203 *cp++ = 'N'; 204 while (*dev) 205 *cp++ = islower(*dev) ? toupper(*dev++) : *dev++; 206 *cp++ = 0; 207 return (mbuf); 208} 209