mkdb.c 2.8 KB

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