Commit | Line | Data |
---|---|---|
cd68466f DF |
1 | /* |
2 | * Copyright (c) 1980 Regents of the University of California. | |
86f9c1e9 KB |
3 | * All rights reserved. |
4 | * | |
5 | * Redistribution and use in source and binary forms are permitted | |
b8c620d6 KB |
6 | * provided that the above copyright notice and this paragraph are |
7 | * duplicated in all such forms and that any documentation, | |
8 | * advertising materials, and other materials related to such | |
9 | * distribution and use acknowledge that the software was developed | |
10 | * by the University of California, Berkeley. The name of the | |
11 | * University may not be used to endorse or promote products derived | |
12 | * from this software without specific prior written permission. | |
13 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
14 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
15 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
cd68466f DF |
16 | */ |
17 | ||
524aa063 | 18 | #ifndef lint |
b8c620d6 | 19 | static char sccsid[] = "@(#)mkheaders.c 5.5 (Berkeley) %G%"; |
86f9c1e9 | 20 | #endif /* not lint */ |
e0a482d6 | 21 | |
900ae8f5 | 22 | /* |
1614283b | 23 | * Make all the .h files for the optional entries |
900ae8f5 MT |
24 | */ |
25 | ||
26 | #include <stdio.h> | |
27 | #include <ctype.h> | |
28 | #include "config.h" | |
576264df | 29 | #include "y.tab.h" |
900ae8f5 | 30 | |
900ae8f5 MT |
31 | headers() |
32 | { | |
e0a482d6 | 33 | register struct file_list *fl; |
1614283b | 34 | |
e0a482d6 BJ |
35 | for (fl = ftab; fl != 0; fl = fl->f_next) |
36 | if (fl->f_needs != 0) | |
821eeaae | 37 | do_count(fl->f_needs, fl->f_needs, 1); |
1614283b MT |
38 | } |
39 | ||
40 | /* | |
e0a482d6 BJ |
41 | * count all the devices of a certain type and recurse to count |
42 | * whatever the device is connected to | |
1614283b | 43 | */ |
1614283b | 44 | do_count(dev, hname, search) |
e0a482d6 | 45 | register char *dev, *hname; |
f025f13d | 46 | int search; |
1614283b | 47 | { |
e0a482d6 BJ |
48 | register struct device *dp, *mp; |
49 | register int count; | |
900ae8f5 | 50 | |
e0a482d6 BJ |
51 | for (count = 0,dp = dtab; dp != 0; dp = dp->d_next) |
52 | if (dp->d_unit != -1 && eq(dp->d_name, dev)) { | |
53 | if (dp->d_type == PSEUDO_DEVICE) { | |
73845e07 SL |
54 | count = |
55 | dp->d_slave != UNKNOWN ? dp->d_slave : 1; | |
e0a482d6 BJ |
56 | break; |
57 | } | |
58 | count++; | |
59 | /* | |
60 | * Allow holes in unit numbering, | |
61 | * assumption is unit numbering starts | |
62 | * at zero. | |
63 | */ | |
64 | if (dp->d_unit + 1 > count) | |
65 | count = dp->d_unit + 1; | |
66 | if (search) { | |
67 | mp = dp->d_conn; | |
8a659ddd MK |
68 | if (mp != 0 && mp != TO_NEXUS && |
69 | mp->d_conn != 0 && mp->d_conn != TO_NEXUS) { | |
821eeaae BJ |
70 | do_count(mp->d_name, hname, 0); |
71 | search = 0; | |
e0a482d6 BJ |
72 | } |
73 | } | |
1614283b | 74 | } |
e0a482d6 | 75 | do_header(dev, hname, count); |
1614283b MT |
76 | } |
77 | ||
78 | do_header(dev, hname, count) | |
e0a482d6 BJ |
79 | char *dev, *hname; |
80 | int count; | |
1614283b | 81 | { |
e0a482d6 BJ |
82 | char *file, *name, *inw, *toheader(), *tomacro(); |
83 | struct file_list *fl, *fl_head; | |
84 | FILE *inf, *outf; | |
85 | int inc, oldcount; | |
1614283b | 86 | |
e0a482d6 BJ |
87 | file = toheader(hname); |
88 | name = tomacro(dev); | |
89 | inf = fopen(file, "r"); | |
90 | oldcount = -1; | |
91 | if (inf == 0) { | |
92 | outf = fopen(file, "w"); | |
93 | if (outf == 0) { | |
94 | perror(file); | |
95 | exit(1); | |
96 | } | |
97 | fprintf(outf, "#define %s %d\n", name, count); | |
22d68ad0 | 98 | (void) fclose(outf); |
e0a482d6 BJ |
99 | return; |
100 | } | |
101 | fl_head = 0; | |
102 | for (;;) { | |
103 | char *cp; | |
22d68ad0 | 104 | if ((inw = get_word(inf)) == 0 || inw == (char *)EOF) |
e0a482d6 | 105 | break; |
22d68ad0 | 106 | if ((inw = get_word(inf)) == 0 || inw == (char *)EOF) |
e0a482d6 BJ |
107 | break; |
108 | inw = ns(inw); | |
109 | cp = get_word(inf); | |
22d68ad0 | 110 | if (cp == 0 || cp == (char *)EOF) |
e0a482d6 BJ |
111 | break; |
112 | inc = atoi(cp); | |
113 | if (eq(inw, name)) { | |
114 | oldcount = inc; | |
115 | inc = count; | |
116 | } | |
117 | cp = get_word(inf); | |
73845e07 | 118 | if (cp == (char *)EOF) |
e0a482d6 BJ |
119 | break; |
120 | fl = (struct file_list *) malloc(sizeof *fl); | |
121 | fl->f_fn = inw; | |
122 | fl->f_type = inc; | |
123 | fl->f_next = fl_head; | |
124 | fl_head = fl; | |
125 | } | |
22d68ad0 | 126 | (void) fclose(inf); |
e0a482d6 BJ |
127 | if (count == oldcount) { |
128 | for (fl = fl_head; fl != 0; fl = fl->f_next) | |
22d68ad0 | 129 | free((char *)fl); |
e0a482d6 BJ |
130 | return; |
131 | } | |
132 | if (oldcount == -1) { | |
133 | fl = (struct file_list *) malloc(sizeof *fl); | |
134 | fl->f_fn = name; | |
135 | fl->f_type = count; | |
136 | fl->f_next = fl_head; | |
137 | fl_head = fl; | |
138 | } | |
1614283b | 139 | outf = fopen(file, "w"); |
e0a482d6 BJ |
140 | if (outf == 0) { |
141 | perror(file); | |
142 | exit(1); | |
cc03da8f | 143 | } |
e0a482d6 | 144 | for (fl = fl_head; fl != 0; fl = fl->f_next) { |
63e21440 | 145 | fprintf(outf, "#define %s %u\n", |
e0a482d6 | 146 | fl->f_fn, count ? fl->f_type : 0); |
22d68ad0 | 147 | free((char *)fl); |
900ae8f5 | 148 | } |
22d68ad0 | 149 | (void) fclose(outf); |
900ae8f5 MT |
150 | } |
151 | ||
152 | /* | |
e0a482d6 | 153 | * convert a dev name to a .h file name |
900ae8f5 | 154 | */ |
e0a482d6 BJ |
155 | char * |
156 | toheader(dev) | |
157 | char *dev; | |
900ae8f5 | 158 | { |
e0a482d6 | 159 | static char hbuf[80]; |
900ae8f5 | 160 | |
22d68ad0 BJ |
161 | (void) strcpy(hbuf, path(dev)); |
162 | (void) strcat(hbuf, ".h"); | |
f025f13d | 163 | return (hbuf); |
900ae8f5 MT |
164 | } |
165 | ||
166 | /* | |
e0a482d6 | 167 | * convert a dev name to a macro name |
900ae8f5 | 168 | */ |
1614283b | 169 | char *tomacro(dev) |
e0a482d6 | 170 | register char *dev; |
900ae8f5 | 171 | { |
e0a482d6 BJ |
172 | static char mbuf[20]; |
173 | register char *cp; | |
900ae8f5 | 174 | |
e0a482d6 BJ |
175 | cp = mbuf; |
176 | *cp++ = 'N'; | |
177 | while (*dev) | |
178 | *cp++ = toupper(*dev++); | |
179 | *cp++ = 0; | |
f025f13d | 180 | return (mbuf); |
900ae8f5 | 181 | } |