xref: /original-bsd/usr.sbin/config/mkheaders.c (revision f0fd5f8a)
1 /*	mkheaders.c	1.13	82/12/09	*/
2 
3 /*
4  * Make all the .h files for the optional entries
5  */
6 
7 #include <stdio.h>
8 #include <ctype.h>
9 #include "config.h"
10 #include "y.tab.h"
11 
12 headers()
13 {
14 	register struct file_list *fl;
15 
16 	for (fl = ftab; fl != 0; fl = fl->f_next)
17 		if (fl->f_needs != 0)
18 			do_count(fl->f_needs, fl->f_needs, 1);
19 }
20 
21 /*
22  * count all the devices of a certain type and recurse to count
23  * whatever the device is connected to
24  */
25 do_count(dev, hname, search)
26 	register char *dev, *hname;
27 	int search;
28 {
29 	register struct device *dp, *mp;
30 	register int count;
31 
32 	for (count = 0,dp = dtab; dp != 0; dp = dp->d_next)
33 		if (dp->d_unit != -1 && eq(dp->d_name, dev)) {
34 			if (dp->d_type == PSEUDO_DEVICE) {
35 				count =
36 				    dp->d_slave != UNKNOWN ? dp->d_slave : 1;
37 				break;
38 			}
39 			count++;
40 			/*
41 			 * Allow holes in unit numbering,
42 			 * assumption is unit numbering starts
43 			 * at zero.
44 			 */
45 			if (dp->d_unit + 1 > count)
46 				count = dp->d_unit + 1;
47 			if (search) {
48 				mp = dp->d_conn;
49 				if (mp != 0 && mp != (struct device *)-1 &&
50 				    mp->d_conn != (struct device *)-1) {
51 					do_count(mp->d_name, hname, 0);
52 					search = 0;
53 				}
54 			}
55 		}
56 	do_header(dev, hname, count);
57 }
58 
59 do_header(dev, hname, count)
60 	char *dev, *hname;
61 	int count;
62 {
63 	char *file, *name, *inw, *toheader(), *tomacro();
64 	struct file_list *fl, *fl_head;
65 	FILE *inf, *outf;
66 	int inc, oldcount;
67 
68 	file = toheader(hname);
69 	name = tomacro(dev);
70 	inf = fopen(file, "r");
71 	oldcount = -1;
72 	if (inf == 0) {
73 		outf = fopen(file, "w");
74 		if (outf == 0) {
75 			perror(file);
76 			exit(1);
77 		}
78 		fprintf(outf, "#define %s %d\n", name, count);
79 		(void) fclose(outf);
80 		return;
81 	}
82 	fl_head = 0;
83 	for (;;) {
84 		char *cp;
85 		if ((inw = get_word(inf)) == 0 || inw == (char *)EOF)
86 			break;
87 		if ((inw = get_word(inf)) == 0 || inw == (char *)EOF)
88 			break;
89 		inw = ns(inw);
90 		cp = get_word(inf);
91 		if (cp == 0 || cp == (char *)EOF)
92 			break;
93 		inc = atoi(cp);
94 		if (eq(inw, name)) {
95 			oldcount = inc;
96 			inc = count;
97 		}
98 		cp = get_word(inf);
99 		if (cp == (char *)EOF)
100 			break;
101 		fl = (struct file_list *) malloc(sizeof *fl);
102 		fl->f_fn = inw;
103 		fl->f_type = inc;
104 		fl->f_next = fl_head;
105 		fl_head = fl;
106 	}
107 	(void) fclose(inf);
108 	if (count == oldcount) {
109 		for (fl = fl_head; fl != 0; fl = fl->f_next)
110 			free((char *)fl);
111 		return;
112 	}
113 	if (oldcount == -1) {
114 		fl = (struct file_list *) malloc(sizeof *fl);
115 		fl->f_fn = name;
116 		fl->f_type = count;
117 		fl->f_next = fl_head;
118 		fl_head = fl;
119 	}
120 	outf = fopen(file, "w");
121 	if (outf == 0) {
122 		perror(file);
123 		exit(1);
124 	}
125 	for (fl = fl_head; fl != 0; fl = fl->f_next) {
126 		fprintf(outf, "#define %s %d\n",
127 		    fl->f_fn, count ? fl->f_type : 0);
128 		free((char *)fl);
129 	}
130 	(void) fclose(outf);
131 }
132 
133 /*
134  * convert a dev name to a .h file name
135  */
136 char *
137 toheader(dev)
138 	char *dev;
139 {
140 	static char hbuf[80];
141 
142 	(void) strcpy(hbuf, path(dev));
143 	(void) strcat(hbuf, ".h");
144 	return (hbuf);
145 }
146 
147 /*
148  * convert a dev name to a macro name
149  */
150 char *tomacro(dev)
151 	register char *dev;
152 {
153 	static char mbuf[20];
154 	register char *cp;
155 
156 	cp = mbuf;
157 	*cp++ = 'N';
158 	while (*dev)
159 		*cp++ = toupper(*dev++);
160 	*cp++ = 0;
161 	return (mbuf);
162 }
163