home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1996 May / PCOnline_05_1996.bin / linux / source / n / bind / bind-4.001 / bind-4~ / bind-4.9.3-BETA9 / contrib / dnsparse / simphosts < prev    next >
Text File  |  1990-09-11  |  3KB  |  105 lines

  1. #!/usr/bin/perl
  2. #
  3. # $Id: simphosts,v 2.0 90/09/11 11:07:41 hakanson Rel $
  4. #
  5. # Simplify hosts(5) format file.
  6. #   Marion Hakanson (hakanson@cse.ogi.edu)
  7. #   Oregon Graduate Institute of Science and Technology
  8. #
  9. # Copyright (c) 1990, Marion Hakanson.
  10. #
  11. # You may distribute under the terms of the GNU General Public License
  12. # as specified in the README file that comes with the dnsparse kit.
  13. #
  14. # The input file is read, and the mappings are stored up in the order
  15. # encountered (earlier ones will override any later ones which may
  16. # overlap or otherwise be redundant), in the same manner as that used
  17. # by the bsd host-table-based resolution code.  When the entire file
  18. # has been processed, each address is printed (in the order that they
  19. # were encountered) as follows:
  20. #    addr            first-canon-for-addr
  21. #
  22. # Next, the remaining (not yet printed) names are sorted, and each
  23. # one is printed, one-per-line, as follows:
  24. #    first-addr-for-name    first-canon-for-name name
  25. # (if the two names are identical, only one is printed).
  26. #
  27. # Thus, if we've done the job right, the resulting table (also in legal
  28. # hosts(5) format) should be functionally equivalent to the original,
  29. # yet in a canonical form which can be compared textually without
  30. # having semantically meaningless differences cloud the issue.
  31. #
  32. # Note that if one runs the result through this script another time,
  33. # one should end up with the same result yet again.
  34.  
  35. if ( $#ARGV >= $[ ) {
  36.   open(HOSTS, $ARGV[$[]) || die "Cannot open $ARGV[$[]: $!, aborted";
  37. } else {
  38.   open(HOSTS, "<&STDIN") || die 'Cannot dup STDIN, aborted';
  39. }
  40.  
  41. # Print header comments as encountered
  42. while ( <HOSTS> ) {
  43.   last unless ( /^\s*#/ );
  44.   print;
  45. }
  46.  
  47. exit if ( eof(HOSTS) );
  48.  
  49. # Note that $_ was set from above
  50. do {
  51.  # All other comments are removed
  52.  unless ( /^\s*#/ ) {
  53.   chop;
  54.   s/#.*//;
  55.   ($addr, @names) = split;
  56.   $canon = $names[$[];
  57.   unless ( defined($addr_to_name{$addr}) ) {
  58.     $addr_to_name{$addr} = $canon;
  59.     push(@addrlist, $addr);
  60.   }
  61.   foreach $name ( @names ) {
  62.     unless ( defined($name_to_addr{$name}) ) {
  63.       $name_to_addr{$name} = $addr;
  64.       push(@namelist, $name);
  65.     }
  66.     unless ( defined($name_to_canon{$name}) ) {
  67.       $name_to_canon{$name} = $canon;
  68.     }
  69.   }
  70.  }
  71. } while (<HOSTS>);
  72.  
  73.  
  74. # First print the addr-to-name mappings
  75. foreach $addr (@addrlist) {
  76.   $realcanon = $addr_to_name{$addr};
  77.   print "$addr\t$realcanon\n";
  78.   $printed{$realcanon} = 1;
  79. }
  80.  
  81. # A nice separator
  82. print "#\n";
  83.  
  84. # Now sort the names and print out their data
  85. @namelist = sort(@namelist);
  86.  
  87. foreach $name (@namelist) {
  88.   # Skip those we printed above
  89.   next if ( $printed{$name} );
  90.  
  91.   $addr = $name_to_addr{$name};
  92.   $canon = $name_to_canon{$name};
  93.   $realcanon = $addr_to_name{$addr};
  94.  
  95.   print "$addr\t$canon";
  96.   print " $name" if ( $name ne $canon );
  97.  
  98.   # Flag an anomaly (but preserve it)
  99.   if ( $realcanon ne $canon ) {
  100.     print "\t# HEY! $realcanon should be canonical here";
  101.   }
  102.  
  103.   print "\n";
  104. }
  105.