Blob


1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include <ctype.h>
6 Biobuf in;
7 Biobuf out;
9 enum
10 {
11 Empty,
12 Sys,
13 Dk,
14 Ip,
15 Domain,
16 };
18 int
19 iscomment(char *name)
20 {
21 return *name == '#';
22 }
24 /*
25 * is this a fully specified datakit name?
26 */
27 int
28 isdk(char *name)
29 {
30 int slash;
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 }
45 /*
46 * Is this an internet domain name?
47 */
48 int
49 isdomain(char *name)
50 {
51 int dot = 0;
52 int alpha = 0;
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 }
70 /*
71 * is this an ip address?
72 */
73 int
74 isip(char *name)
75 {
76 int dot = 0;
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 }
90 char tup[64][64];
91 int ttype[64];
92 int ntup;
94 void
95 tprint(void)
96 {
97 int i, tab;
98 char *p;
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;
109 for(i = 0; i < ntup; i++){
110 if(ttype[i] == Empty)
111 continue;
112 if(tab)
113 Bprint(&out, "\t");
114 tab = 1;
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 ");
131 Bprint(&out, "dk=%s\n", tup[i]);
132 break;
133 case Sys:
134 Bprint(&out, "sys=%s\n", tup[i]);
135 break;
140 #define NFIELDS 64
142 /*
143 * make a database file from a merged uucp/inet database
144 */
145 void
146 main(void)
148 int n, i, j;
149 char *l;
150 char *fields[NFIELDS];
151 int ftype[NFIELDS];
152 int same, match;
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;
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;
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;
181 if(!same && ntup){
182 tprint();
183 ntup = 0;
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;
193 if(!match){
194 ttype[ntup] = ftype[i];
195 strcpy(tup[ntup], fields[i]);
196 ntup++;
200 if(ntup)
201 tprint();
202 exits(0);