xref: /original-bsd/usr.sbin/config/mkheaders.c (revision 91abda3c)
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.6 (Berkeley) 06/01/90";
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 		fl->f_fn = inw;
112 		fl->f_type = inc;
113 		fl->f_next = fl_head;
114 		fl_head = fl;
115 	}
116 	(void) fclose(inf);
117 	if (count == oldcount) {
118 		for (fl = fl_head; fl != 0; fl = fl->f_next)
119 			free((char *)fl);
120 		return;
121 	}
122 	if (oldcount == -1) {
123 		fl = (struct file_list *) malloc(sizeof *fl);
124 		fl->f_fn = name;
125 		fl->f_type = count;
126 		fl->f_next = fl_head;
127 		fl_head = fl;
128 	}
129 	outf = fopen(file, "w");
130 	if (outf == 0) {
131 		perror(file);
132 		exit(1);
133 	}
134 	for (fl = fl_head; fl != 0; fl = fl->f_next) {
135 		fprintf(outf, "#define %s %u\n",
136 		    fl->f_fn, count ? fl->f_type : 0);
137 		free((char *)fl);
138 	}
139 	(void) fclose(outf);
140 }
141 
142 /*
143  * convert a dev name to a .h file name
144  */
145 char *
146 toheader(dev)
147 	char *dev;
148 {
149 	static char hbuf[80];
150 
151 	(void) strcpy(hbuf, path(dev));
152 	(void) strcat(hbuf, ".h");
153 	return (hbuf);
154 }
155 
156 /*
157  * convert a dev name to a macro name
158  */
159 char *tomacro(dev)
160 	register char *dev;
161 {
162 	static char mbuf[20];
163 	register char *cp;
164 
165 	cp = mbuf;
166 	*cp++ = 'N';
167 	while (*dev)
168 		*cp++ = toupper(*dev++);
169 	*cp++ = 0;
170 	return (mbuf);
171 }
172