mkdb.c 3.2 KB

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