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