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.8 (Berkeley) 05/04/92"; 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, *tflp; 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 = NULL; 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 != NULL; fl = tflp) { 120 tflp = fl->f_next; 121 free(fl); 122 } 123 return; 124 } 125 if (oldcount == -1) { 126 fl = (struct file_list *) malloc(sizeof *fl); 127 bzero(fl, sizeof(*fl)); 128 fl->f_fn = name; 129 fl->f_type = count; 130 fl->f_next = fl_head; 131 fl_head = fl; 132 } 133 outf = fopen(file, "w"); 134 if (outf == 0) { 135 perror(file); 136 exit(1); 137 } 138 for (fl = fl_head; fl != NULL; fl = tflp) { 139 fprintf(outf, 140 "#define %s %u\n", fl->f_fn, count ? fl->f_type : 0); 141 tflp = fl->f_next; 142 free(fl); 143 } 144 (void) fclose(outf); 145 } 146 147 /* 148 * convert a dev name to a .h file name 149 */ 150 char * 151 toheader(dev) 152 char *dev; 153 { 154 static char hbuf[80]; 155 156 (void) strcpy(hbuf, path(dev)); 157 (void) strcat(hbuf, ".h"); 158 return (hbuf); 159 } 160 161 /* 162 * convert a dev name to a macro name 163 */ 164 char *tomacro(dev) 165 register char *dev; 166 { 167 static char mbuf[20]; 168 register char *cp; 169 170 cp = mbuf; 171 *cp++ = 'N'; 172 while (*dev) 173 *cp++ = toupper(*dev++); 174 *cp++ = 0; 175 return (mbuf); 176 } 177