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