| 1 | /* |
| 2 | * Copyright (c) 1980 Regents of the University of California. |
| 3 | * 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 | static char sccsid[] = "@(#)mkheaders.c 5.7 (Berkeley) 7/1/91"; |
| 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 "y.tab.h" |
| 46 | |
| 47 | headers() |
| 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 | */ |
| 60 | do_count(dev, hname, search) |
| 61 | register char *dev, *hname; |
| 62 | int search; |
| 63 | { |
| 64 | register struct device *dp, *mp; |
| 65 | register int count; |
| 66 | |
| 67 | for (count = 0,dp = dtab; dp != 0; dp = dp->d_next) |
| 68 | if (dp->d_unit != -1 && eq(dp->d_name, dev)) { |
| 69 | if (dp->d_type == PSEUDO_DEVICE) { |
| 70 | count = |
| 71 | dp->d_slave != UNKNOWN ? dp->d_slave : 1; |
| 72 | break; |
| 73 | } |
| 74 | count++; |
| 75 | /* |
| 76 | * Allow holes in unit numbering, |
| 77 | * assumption is unit numbering starts |
| 78 | * at zero. |
| 79 | */ |
| 80 | if (dp->d_unit + 1 > count) |
| 81 | count = dp->d_unit + 1; |
| 82 | if (search) { |
| 83 | mp = dp->d_conn; |
| 84 | if (mp != 0 && mp != TO_NEXUS && |
| 85 | mp->d_conn != 0 && mp->d_conn != TO_NEXUS) { |
| 86 | do_count(mp->d_name, hname, 0); |
| 87 | search = 0; |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | do_header(dev, hname, count); |
| 92 | } |
| 93 | |
| 94 | do_header(dev, hname, count) |
| 95 | char *dev, *hname; |
| 96 | int count; |
| 97 | { |
| 98 | char *file, *name, *inw, *toheader(), *tomacro(); |
| 99 | struct file_list *fl, *fl_head; |
| 100 | FILE *inf, *outf; |
| 101 | int inc, oldcount; |
| 102 | |
| 103 | file = toheader(hname); |
| 104 | name = tomacro(dev); |
| 105 | inf = fopen(file, "r"); |
| 106 | oldcount = -1; |
| 107 | if (inf == 0) { |
| 108 | outf = fopen(file, "w"); |
| 109 | if (outf == 0) { |
| 110 | perror(file); |
| 111 | exit(1); |
| 112 | } |
| 113 | fprintf(outf, "#define %s %d\n", name, count); |
| 114 | (void) fclose(outf); |
| 115 | return; |
| 116 | } |
| 117 | fl_head = 0; |
| 118 | for (;;) { |
| 119 | char *cp; |
| 120 | if ((inw = get_word(inf)) == 0 || inw == (char *)EOF) |
| 121 | break; |
| 122 | if ((inw = get_word(inf)) == 0 || inw == (char *)EOF) |
| 123 | break; |
| 124 | inw = ns(inw); |
| 125 | cp = get_word(inf); |
| 126 | if (cp == 0 || cp == (char *)EOF) |
| 127 | break; |
| 128 | inc = atoi(cp); |
| 129 | if (eq(inw, name)) { |
| 130 | oldcount = inc; |
| 131 | inc = count; |
| 132 | } |
| 133 | cp = get_word(inf); |
| 134 | if (cp == (char *)EOF) |
| 135 | break; |
| 136 | fl = (struct file_list *) malloc(sizeof *fl); |
| 137 | bzero(fl, sizeof(*fl)); |
| 138 | fl->f_fn = inw; |
| 139 | fl->f_type = inc; |
| 140 | fl->f_next = fl_head; |
| 141 | fl_head = fl; |
| 142 | } |
| 143 | (void) fclose(inf); |
| 144 | if (count == oldcount) { |
| 145 | for (fl = fl_head; fl;) { |
| 146 | struct file_list *fln = fl->f_next; |
| 147 | |
| 148 | free((char *)fl); |
| 149 | fl = fln; |
| 150 | } |
| 151 | return; |
| 152 | } |
| 153 | if (oldcount == -1) { |
| 154 | fl = (struct file_list *) malloc(sizeof *fl); |
| 155 | bzero(fl, sizeof(*fl)); |
| 156 | fl->f_fn = name; |
| 157 | fl->f_type = count; |
| 158 | fl->f_next = fl_head; |
| 159 | fl_head = fl; |
| 160 | } |
| 161 | outf = fopen(file, "w"); |
| 162 | if (outf == 0) { |
| 163 | perror(file); |
| 164 | exit(1); |
| 165 | } |
| 166 | for (fl = fl_head; fl;) { |
| 167 | struct file_list *fln = fl->f_next; |
| 168 | |
| 169 | fprintf(outf, "#define %s %u\n", |
| 170 | fl->f_fn, count ? fl->f_type : 0); |
| 171 | free((char *)fl); |
| 172 | fl = fln; |
| 173 | } |
| 174 | (void) fclose(outf); |
| 175 | } |
| 176 | |
| 177 | /* |
| 178 | * convert a dev name to a .h file name |
| 179 | */ |
| 180 | char * |
| 181 | toheader(dev) |
| 182 | char *dev; |
| 183 | { |
| 184 | static char hbuf[80]; |
| 185 | |
| 186 | (void) strcpy(hbuf, path(dev)); |
| 187 | (void) strcat(hbuf, ".h"); |
| 188 | return (hbuf); |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * convert a dev name to a macro name |
| 193 | */ |
| 194 | char *tomacro(dev) |
| 195 | register char *dev; |
| 196 | { |
| 197 | static char mbuf[20]; |
| 198 | register char *cp; |
| 199 | |
| 200 | cp = mbuf; |
| 201 | *cp++ = 'N'; |
| 202 | while (*dev) |
| 203 | *cp++ = toupper(*dev++); |
| 204 | *cp++ = 0; |
| 205 | return (mbuf); |
| 206 | } |