xref: /original-bsd/usr.sbin/config/mkheaders.c (revision 6c57d260)
1 /*
2  *	mkheaders.c	1.6	81/04/08
3  * Make all the .h files for the optional entries
4  */
5 
6 #include <stdio.h>
7 #include <ctype.h>
8 #include "config.h"
9 
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 	}
27 
28 headers()
29 {
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 
43 do_count(dev, hname, search)
44 register char *dev, *hname;
45 bool search;
46 {
47     register struct device *dp, *mp;
48     register int count;
49 
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 
67 do_header(dev, hname, count)
68 char *dev, *hname;
69 int 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");
83 	if (outf == NULL) {
84 	    perror(file);
85 	    exit(1);
86 	}
87 	fprintf(outf, "#define %s %d\n", name, count);
88 	fclose(outf);
89 	return;
90     }
91     fl_head = NULL;
92     while(1)
93     {
94 	rdln(inf, inw, inc);
95 	if (inw == EOF)
96 	    break;
97 	if (eq(inw, name))
98 	{
99 	    oldcount = inc;
100 	    inc = count;
101 	}
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;
107     }
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");
124     if (outf == NULL) {
125 	perror(file);
126 	exit(1);
127     }
128     for (fl = fl_head; fl != NULL; fl = fl->f_next)
129     {
130 	fprintf(outf, "#define %s %d\n", fl->f_fn, count ? fl->f_type : 0);
131 	free(fl);
132     }
133     fclose(outf);
134 }
135 
136 /*
137  * toheader:
138  *	Convert a dev name to a .h file nae
139  */
140 
141 char *toheader(dev)
142 char *dev;
143 {
144     static char hbuf[80];
145 
146     strcpy(hbuf, path(dev));
147     strcat(hbuf, ".h");
148     return hbuf;
149 }
150 
151 /*
152  * tomacro:
153  *	Convert a dev name to a macro name
154  */
155 
156 char *tomacro(dev)
157 register char *dev;
158 {
159     static char mbuf[20];
160     register char *cp;
161 
162     cp = mbuf;
163     *cp++ = 'N';
164     while(*dev)
165 	*cp++ = toupper(*dev++);
166     *cp++ = '\0';
167     return mbuf;
168 }
169