ndbmkdb.c (2913B)
1 #include <u.h> 2 #include <libc.h> 3 #include <bio.h> 4 #include <ctype.h> 5 6 Biobuf in; 7 Biobuf out; 8 9 enum 10 { 11 Empty, 12 Sys, 13 Dk, 14 Ip, 15 Domain 16 }; 17 18 int 19 iscomment(char *name) 20 { 21 return *name == '#'; 22 } 23 24 /* 25 * is this a fully specified datakit name? 26 */ 27 int 28 isdk(char *name) 29 { 30 int slash; 31 32 slash = 0; 33 for(; *name; name++){ 34 if(isalnum((uchar)*name)) 35 continue; 36 if(*name == '/'){ 37 slash = 1; 38 continue; 39 } 40 return 0; 41 } 42 return slash; 43 } 44 45 /* 46 * Is this an internet domain name? 47 */ 48 int 49 isdomain(char *name) 50 { 51 int dot = 0; 52 int alpha = 0; 53 54 for(; *name; name++){ 55 if(isalpha((uchar)*name) || *name == '-'){ 56 alpha = 1; 57 continue; 58 } 59 if(*name == '.'){ 60 dot = 1; 61 continue; 62 } 63 if(isdigit((uchar)*name)) 64 continue; 65 return 0; 66 } 67 return dot && alpha; 68 } 69 70 /* 71 * is this an ip address? 72 */ 73 int 74 isip(char *name) 75 { 76 int dot = 0; 77 78 for(; *name; name++){ 79 if(*name == '.'){ 80 dot = 1; 81 continue; 82 } 83 if(isdigit((uchar)*name)) 84 continue; 85 return 0; 86 } 87 return dot; 88 } 89 90 char tup[64][64]; 91 int ttype[64]; 92 int ntup; 93 94 void 95 tprint(void) 96 { 97 int i, tab; 98 char *p; 99 100 tab = 0; 101 for(i = 0; i < ntup; i++){ 102 if(ttype[i] == Sys){ 103 Bprint(&out, "sys = %s\n", tup[i]); 104 tab = 1; 105 ttype[i] = Empty; 106 break; 107 } 108 } 109 for(i = 0; i < ntup; i++){ 110 if(ttype[i] == Empty) 111 continue; 112 if(tab) 113 Bprint(&out, "\t"); 114 tab = 1; 115 116 switch(ttype[i]){ 117 case Domain: 118 Bprint(&out, "dom=%s\n", tup[i]); 119 break; 120 case Ip: 121 Bprint(&out, "ip=%s\n", tup[i]); 122 break; 123 case Dk: 124 p = strrchr(tup[i], '/'); 125 if(p){ 126 p++; 127 if((*p == 'C' || *p == 'R') 128 && strncmp(tup[i], "nj/astro/", p-tup[i]) == 0) 129 Bprint(&out, "flavor=console "); 130 } 131 Bprint(&out, "dk=%s\n", tup[i]); 132 break; 133 case Sys: 134 Bprint(&out, "sys=%s\n", tup[i]); 135 break; 136 } 137 } 138 } 139 140 #define NFIELDS 64 141 142 /* 143 * make a database file from a merged uucp/inet database 144 */ 145 void 146 main(void) 147 { 148 int n, i, j; 149 char *l; 150 char *fields[NFIELDS]; 151 int ftype[NFIELDS]; 152 int same, match; 153 154 Binit(&in, 0, OREAD); 155 Binit(&out, 1, OWRITE); 156 ntup = 0; 157 while(l = Brdline(&in, '\n')){ 158 l[Blinelen(&in)-1] = 0; 159 n = getfields(l, fields, NFIELDS, 1, " \t"); 160 same = 0; 161 for(i = 0; i < n; i++){ 162 if(iscomment(fields[i])){ 163 n = i; 164 break; 165 } 166 if(isdomain(fields[i])){ 167 ftype[i] = Domain; 168 for(j = 0; j < ntup; j++) 169 if(ttype[j] == Domain && strcmp(fields[i], tup[j]) == 0){ 170 same = 1; 171 ftype[i] = Empty; 172 break; 173 } 174 } else if(isip(fields[i])) 175 ftype[i] = Ip; 176 else if(isdk(fields[i])) 177 ftype[i] = Dk; 178 else 179 ftype[i] = Sys; 180 } 181 if(!same && ntup){ 182 tprint(); 183 ntup = 0; 184 } 185 for(i = 0; i < n; i++){ 186 match = 0; 187 for(j = 0; j < ntup; j++){ 188 if(ftype[i] == ttype[j] && strcmp(fields[i], tup[j]) == 0){ 189 match = 1; 190 break; 191 } 192 } 193 if(!match){ 194 ttype[ntup] = ftype[i]; 195 strcpy(tup[ntup], fields[i]); 196 ntup++; 197 } 198 } 199 } 200 if(ntup) 201 tprint(); 202 exits(0); 203 }