1 /* 2 * mkheaders.c 1.7 82/03/28 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 /* 55 * Allow holes in unit numbering, 56 * assumption is unit numbering starts 57 * at zero. 58 */ 59 if (dp->d_unit + 1 > count) 60 count = dp->d_unit + 1; 61 if (search) 62 { 63 mp = dp->d_conn; 64 if (mp != NULL && mp != -1 && mp->d_conn != -1) 65 { 66 do_count(mp->d_name, hname, FALSE); 67 search = FALSE; 68 } 69 } 70 } 71 do_header(dev, hname, count); 72 } 73 74 do_header(dev, hname, count) 75 char *dev, *hname; 76 int count; 77 { 78 char *file, *name, *inw, *toheader(), *tomacro(); 79 struct file_list *fl, *fl_head; 80 FILE *inf, *outf; 81 int inc, oldcount; 82 83 file = toheader(hname); 84 name = tomacro(dev); 85 inf = fopen(file, "r"); 86 oldcount = -1; 87 if (inf == NULL) 88 { 89 outf = fopen(file, "w"); 90 if (outf == NULL) { 91 perror(file); 92 exit(1); 93 } 94 fprintf(outf, "#define %s %d\n", name, count); 95 fclose(outf); 96 return; 97 } 98 fl_head = NULL; 99 while(1) 100 { 101 rdln(inf, inw, inc); 102 if (inw == EOF) 103 break; 104 if (eq(inw, name)) 105 { 106 oldcount = inc; 107 inc = count; 108 } 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 } 115 fclose(inf); 116 if (count == oldcount) 117 { 118 for (fl = fl_head; fl != NULL; fl = fl->f_next) 119 free(fl); 120 return; 121 } 122 if (oldcount == -1) 123 { 124 fl = (struct file_list *) malloc(sizeof *fl); 125 fl->f_fn = name; 126 fl->f_type = count; 127 fl->f_next = fl_head; 128 fl_head = fl; 129 } 130 outf = fopen(file, "w"); 131 if (outf == NULL) { 132 perror(file); 133 exit(1); 134 } 135 for (fl = fl_head; fl != NULL; fl = fl->f_next) 136 { 137 fprintf(outf, "#define %s %d\n", fl->f_fn, count ? fl->f_type : 0); 138 free(fl); 139 } 140 fclose(outf); 141 } 142 143 /* 144 * toheader: 145 * Convert a dev name to a .h file nae 146 */ 147 148 char *toheader(dev) 149 char *dev; 150 { 151 static char hbuf[80]; 152 153 strcpy(hbuf, path(dev)); 154 strcat(hbuf, ".h"); 155 return hbuf; 156 } 157 158 /* 159 * tomacro: 160 * Convert a dev name to a macro name 161 */ 162 163 char *tomacro(dev) 164 register char *dev; 165 { 166 static char mbuf[20]; 167 register char *cp; 168 169 cp = mbuf; 170 *cp++ = 'N'; 171 while(*dev) 172 *cp++ = toupper(*dev++); 173 *cp++ = '\0'; 174 return mbuf; 175 } 176