-p option to create profiling kernel
[unix-history] / usr / src / usr.sbin / config / mkheaders.c
CommitLineData
900ae8f5 1/*
576264df 2 * mkheaders.c 1.8 82/07/21
1614283b 3 * Make all the .h files for the optional entries
900ae8f5
MT
4 */
5
6#include <stdio.h>
7#include <ctype.h>
8#include "config.h"
576264df 9#include "y.tab.h"
900ae8f5 10
1614283b
MT
11/*
12 * This macro reads a line of the form
13 * #define STRING <number>
14 * and assigns STRING to wd and <number> to count
15 */
16#define rdln(f, wd, count) {\
17 register char *iwd;\
18 if ((wd = get_word(f)) != NULL && wd != EOF)\
19 if ((wd = get_word(f)) != NULL && wd != EOF) {\
20 iwd = ns(wd);\
21 if ((wd = get_word(f)) != NULL && wd != EOF) {\
22 count = atoi(wd);\
23 wd = get_word(f);\
24 wd = iwd;\
25 }\
26 }\
27 }
900ae8f5
MT
28
29headers()
30{
1614283b
MT
31 register struct file_list *fl;
32
33 for (fl = ftab; fl != NULL; fl = fl->f_next)
34 if (fl->f_needs != NULL)
35 do_count(fl->f_needs, fl->f_needs, TRUE);
36}
37
38/*
39 * do_count:
40 * Count all the devices of a certain type and recurse to count
41 * whatever the device is connected to
42 */
43
44do_count(dev, hname, search)
45register char *dev, *hname;
46bool search;
47{
48 register struct device *dp, *mp;
900ae8f5
MT
49 register int count;
50
1614283b
MT
51 for (count = 0,dp = dtab; dp != NULL; dp = dp->d_next)
52 if (dp->d_unit != -1 && eq(dp->d_name, dev))
53 {
576264df
RE
54 if (dp->d_type == PSEUDO_DEVICE) {
55 count = dp->d_slave != UNKNOWN ? dp->d_slave : 1;
56 break;
57 }
1614283b 58 count++;
ec6a4064
SL
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;
1614283b
MT
66 if (search)
67 {
68 mp = dp->d_conn;
69 if (mp != NULL && mp != -1 && mp->d_conn != -1)
70 {
71 do_count(mp->d_name, hname, FALSE);
72 search = FALSE;
73 }
74 }
75 }
76 do_header(dev, hname, count);
77}
78
79do_header(dev, hname, count)
80char *dev, *hname;
81int count;
82{
83 char *file, *name, *inw, *toheader(), *tomacro();
84 struct file_list *fl, *fl_head;
85 FILE *inf, *outf;
86 int inc, oldcount;
87
88 file = toheader(hname);
89 name = tomacro(dev);
90 inf = fopen(file, "r");
91 oldcount = -1;
92 if (inf == NULL)
93 {
94 outf = fopen(file, "w");
cc03da8f
BJ
95 if (outf == NULL) {
96 perror(file);
97 exit(1);
98 }
1614283b
MT
99 fprintf(outf, "#define %s %d\n", name, count);
100 fclose(outf);
101 return;
102 }
103 fl_head = NULL;
104 while(1)
900ae8f5 105 {
1614283b
MT
106 rdln(inf, inw, inc);
107 if (inw == EOF)
108 break;
109 if (eq(inw, name))
900ae8f5 110 {
1614283b
MT
111 oldcount = inc;
112 inc = count;
900ae8f5 113 }
1614283b
MT
114 fl = (struct file_list *) malloc(sizeof *fl);
115 fl->f_fn = inw;
116 fl->f_type = inc;
117 fl->f_next = fl_head;
118 fl_head = fl;
900ae8f5 119 }
1614283b
MT
120 fclose(inf);
121 if (count == oldcount)
122 {
123 for (fl = fl_head; fl != NULL; fl = fl->f_next)
124 free(fl);
125 return;
126 }
127 if (oldcount == -1)
128 {
129 fl = (struct file_list *) malloc(sizeof *fl);
130 fl->f_fn = name;
131 fl->f_type = count;
132 fl->f_next = fl_head;
133 fl_head = fl;
134 }
135 outf = fopen(file, "w");
cc03da8f
BJ
136 if (outf == NULL) {
137 perror(file);
138 exit(1);
139 }
1614283b
MT
140 for (fl = fl_head; fl != NULL; fl = fl->f_next)
141 {
bccb6adf 142 fprintf(outf, "#define %s %d\n", fl->f_fn, count ? fl->f_type : 0);
1614283b
MT
143 free(fl);
144 }
145 fclose(outf);
900ae8f5
MT
146}
147
148/*
1614283b
MT
149 * toheader:
150 * Convert a dev name to a .h file nae
900ae8f5
MT
151 */
152
1614283b
MT
153char *toheader(dev)
154char *dev;
900ae8f5 155{
1614283b 156 static char hbuf[80];
900ae8f5 157
d5429061 158 strcpy(hbuf, path(dev));
1614283b
MT
159 strcat(hbuf, ".h");
160 return hbuf;
900ae8f5
MT
161}
162
163/*
1614283b
MT
164 * tomacro:
165 * Convert a dev name to a macro name
900ae8f5
MT
166 */
167
1614283b
MT
168char *tomacro(dev)
169register char *dev;
900ae8f5 170{
1614283b
MT
171 static char mbuf[20];
172 register char *cp;
900ae8f5 173
1614283b
MT
174 cp = mbuf;
175 *cp++ = 'N';
176 while(*dev)
177 *cp++ = toupper(*dev++);
178 *cp++ = '\0';
179 return mbuf;
900ae8f5 180}