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