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