xref: /original-bsd/usr.sbin/config/mkheaders.c (revision 46bf0326)
1 /*
2  * Copyright (c) 1980 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)mkheaders.c	5.7 (Berkeley) 07/01/91";
10 #endif /* not lint */
11 
12 /*
13  * Make all the .h files for the optional entries
14  */
15 
16 #include <stdio.h>
17 #include <ctype.h>
18 #include "config.h"
19 #include "y.tab.h"
20 
21 headers()
22 {
23 	register struct file_list *fl;
24 
25 	for (fl = ftab; fl != 0; fl = fl->f_next)
26 		if (fl->f_needs != 0)
27 			do_count(fl->f_needs, fl->f_needs, 1);
28 }
29 
30 /*
31  * count all the devices of a certain type and recurse to count
32  * whatever the device is connected to
33  */
34 do_count(dev, hname, search)
35 	register char *dev, *hname;
36 	int search;
37 {
38 	register struct device *dp, *mp;
39 	register int count;
40 
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) {
44 				count =
45 				    dp->d_slave != UNKNOWN ? dp->d_slave : 1;
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;
58 				if (mp != 0 && mp != TO_NEXUS &&
59 				    mp->d_conn != 0 && mp->d_conn != TO_NEXUS) {
60 					do_count(mp->d_name, hname, 0);
61 					search = 0;
62 				}
63 			}
64 		}
65 	do_header(dev, hname, count);
66 }
67 
68 do_header(dev, hname, count)
69 	char *dev, *hname;
70 	int count;
71 {
72 	char *file, *name, *inw, *toheader(), *tomacro();
73 	struct file_list *fl, *fl_head;
74 	FILE *inf, *outf;
75 	int inc, oldcount;
76 
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);
88 		(void) fclose(outf);
89 		return;
90 	}
91 	fl_head = 0;
92 	for (;;) {
93 		char *cp;
94 		if ((inw = get_word(inf)) == 0 || inw == (char *)EOF)
95 			break;
96 		if ((inw = get_word(inf)) == 0 || inw == (char *)EOF)
97 			break;
98 		inw = ns(inw);
99 		cp = get_word(inf);
100 		if (cp == 0 || cp == (char *)EOF)
101 			break;
102 		inc = atoi(cp);
103 		if (eq(inw, name)) {
104 			oldcount = inc;
105 			inc = count;
106 		}
107 		cp = get_word(inf);
108 		if (cp == (char *)EOF)
109 			break;
110 		fl = (struct file_list *) malloc(sizeof *fl);
111 		bzero(fl, sizeof(*fl));
112 		fl->f_fn = inw;
113 		fl->f_type = inc;
114 		fl->f_next = fl_head;
115 		fl_head = fl;
116 	}
117 	(void) fclose(inf);
118 	if (count == oldcount) {
119 		for (fl = fl_head; fl != 0; fl = fl->f_next)
120 			free((char *)fl);
121 		return;
122 	}
123 	if (oldcount == -1) {
124 		fl = (struct file_list *) malloc(sizeof *fl);
125 		bzero(fl, sizeof(*fl));
126 		fl->f_fn = name;
127 		fl->f_type = count;
128 		fl->f_next = fl_head;
129 		fl_head = fl;
130 	}
131 	outf = fopen(file, "w");
132 	if (outf == 0) {
133 		perror(file);
134 		exit(1);
135 	}
136 	for (fl = fl_head; fl != 0; fl = fl->f_next) {
137 		fprintf(outf, "#define %s %u\n",
138 		    fl->f_fn, count ? fl->f_type : 0);
139 		free((char *)fl);
140 	}
141 	(void) fclose(outf);
142 }
143 
144 /*
145  * convert a dev name to a .h file name
146  */
147 char *
148 toheader(dev)
149 	char *dev;
150 {
151 	static char hbuf[80];
152 
153 	(void) strcpy(hbuf, path(dev));
154 	(void) strcat(hbuf, ".h");
155 	return (hbuf);
156 }
157 
158 /*
159  * convert a dev name to a macro name
160  */
161 char *tomacro(dev)
162 	register char *dev;
163 {
164 	static char mbuf[20];
165 	register char *cp;
166 
167 	cp = mbuf;
168 	*cp++ = 'N';
169 	while (*dev)
170 		*cp++ = toupper(*dev++);
171 	*cp++ = 0;
172 	return (mbuf);
173 }
174