1 /* 2 * mkheaders.c 1.8 82/07/21 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 #include "y.tab.h" 10 11 /* 12 * This macro reads a line of the form 13 * #define STRING <number> 14 * and assigns STRING to wd and <number> to count 15 */ 16 #define rdln(f, wd, count) {\ 17 register char *iwd;\ 18 if ((wd = get_word(f)) != NULL && wd != EOF)\ 19 if ((wd = get_word(f)) != NULL && wd != EOF) {\ 20 iwd = ns(wd);\ 21 if ((wd = get_word(f)) != NULL && wd != EOF) {\ 22 count = atoi(wd);\ 23 wd = get_word(f);\ 24 wd = iwd;\ 25 }\ 26 }\ 27 } 28 29 headers() 30 { 31 register struct file_list *fl; 32 33 for (fl = ftab; fl != NULL; fl = fl->f_next) 34 if (fl->f_needs != NULL) 35 do_count(fl->f_needs, fl->f_needs, TRUE); 36 } 37 38 /* 39 * do_count: 40 * Count all the devices of a certain type and recurse to count 41 * whatever the device is connected to 42 */ 43 44 do_count(dev, hname, search) 45 register char *dev, *hname; 46 bool search; 47 { 48 register struct device *dp, *mp; 49 register int count; 50 51 for (count = 0,dp = dtab; dp != NULL; dp = dp->d_next) 52 if (dp->d_unit != -1 && eq(dp->d_name, dev)) 53 { 54 if (dp->d_type == PSEUDO_DEVICE) { 55 count = dp->d_slave != UNKNOWN ? dp->d_slave : 1; 56 break; 57 } 58 count++; 59 /* 60 * Allow holes in unit numbering, 61 * assumption is unit numbering starts 62 * at zero. 63 */ 64 if (dp->d_unit + 1 > count) 65 count = dp->d_unit + 1; 66 if (search) 67 { 68 mp = dp->d_conn; 69 if (mp != NULL && mp != -1 && mp->d_conn != -1) 70 { 71 do_count(mp->d_name, hname, FALSE); 72 search = FALSE; 73 } 74 } 75 } 76 do_header(dev, hname, count); 77 } 78 79 do_header(dev, hname, count) 80 char *dev, *hname; 81 int count; 82 { 83 char *file, *name, *inw, *toheader(), *tomacro(); 84 struct file_list *fl, *fl_head; 85 FILE *inf, *outf; 86 int inc, oldcount; 87 88 file = toheader(hname); 89 name = tomacro(dev); 90 inf = fopen(file, "r"); 91 oldcount = -1; 92 if (inf == NULL) 93 { 94 outf = fopen(file, "w"); 95 if (outf == NULL) { 96 perror(file); 97 exit(1); 98 } 99 fprintf(outf, "#define %s %d\n", name, count); 100 fclose(outf); 101 return; 102 } 103 fl_head = NULL; 104 while(1) 105 { 106 rdln(inf, inw, inc); 107 if (inw == EOF) 108 break; 109 if (eq(inw, name)) 110 { 111 oldcount = inc; 112 inc = count; 113 } 114 fl = (struct file_list *) malloc(sizeof *fl); 115 fl->f_fn = inw; 116 fl->f_type = inc; 117 fl->f_next = fl_head; 118 fl_head = fl; 119 } 120 fclose(inf); 121 if (count == oldcount) 122 { 123 for (fl = fl_head; fl != NULL; fl = fl->f_next) 124 free(fl); 125 return; 126 } 127 if (oldcount == -1) 128 { 129 fl = (struct file_list *) malloc(sizeof *fl); 130 fl->f_fn = name; 131 fl->f_type = count; 132 fl->f_next = fl_head; 133 fl_head = fl; 134 } 135 outf = fopen(file, "w"); 136 if (outf == NULL) { 137 perror(file); 138 exit(1); 139 } 140 for (fl = fl_head; fl != NULL; fl = fl->f_next) 141 { 142 fprintf(outf, "#define %s %d\n", fl->f_fn, count ? fl->f_type : 0); 143 free(fl); 144 } 145 fclose(outf); 146 } 147 148 /* 149 * toheader: 150 * Convert a dev name to a .h file nae 151 */ 152 153 char *toheader(dev) 154 char *dev; 155 { 156 static char hbuf[80]; 157 158 strcpy(hbuf, path(dev)); 159 strcat(hbuf, ".h"); 160 return hbuf; 161 } 162 163 /* 164 * tomacro: 165 * Convert a dev name to a macro name 166 */ 167 168 char *tomacro(dev) 169 register char *dev; 170 { 171 static char mbuf[20]; 172 register char *cp; 173 174 cp = mbuf; 175 *cp++ = 'N'; 176 while(*dev) 177 *cp++ = toupper(*dev++); 178 *cp++ = '\0'; 179 return mbuf; 180 } 181