home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2001 May / SGI IRIX 6.5 Applications 2001 May.iso / dev / insight_dev.idb / usr / share / Insight / bin / xref.z / xref
Encoding:
Text File  |  2001-04-05  |  3.0 KB  |  116 lines

  1. #!/usr/bin/perl
  2.  
  3. # This script is designed to create a report about the validity of
  4. # cross references within an SGIDOC or SGIDOCBK SGML instance
  5.  
  6. # test command line syntax
  7. $SGMLFILE=$ARGV[0];
  8. if ($SGMLFILE eq "") {
  9.   print "Usage : xref <input-file-name>\n";
  10.   exit;
  11. }
  12.  
  13. if (! -r $SGMLFILE) {
  14.     print "xref: ERROR unable to read file '$SGMLFILE'\n";
  15.     exit;
  16. }
  17.  
  18. # make an array of targets (with text as content of each entry);
  19. # push any repeated targets into a list for later use
  20. open(F,$SGMLFILE) || die "xref: Unable to open file '$SGMLFILE'\n";
  21.  
  22. while (<F>) {
  23.   while (s/<[^>]+\sID\s*=\s*\"([^\"]+)\"[^>]*>/$1/i) {
  24.     $SAVED_PATTERNSPACE = $_;
  25.     $TARGET = $1;
  26.     s/<[^>]+>([^>]*)$TARGET([^>]*)<[^>]+>/$1 $2/i;
  27.     if (! $TARGETS{$TARGET}) { 
  28.       $TARGETS{$TARGET} = "$1 $2"; }
  29.     else {
  30.       $TARGETS{$TARGET} .= ": " . "$1 $2";
  31.       if (! grep(/$TARGET/, @REPEAT_TARGETS)) {
  32.         push(@REPEAT_TARGETS,$TARGET);
  33.       }
  34.     }
  35.     $_ = $SAVED_PATTERNSPACE;
  36.   }
  37. }
  38.  
  39. # print out number and content of repeated targets, using list from above
  40. if ($#REPEAT_TARGETS == -1) {
  41.   print "\n Link targets appearing multiple times in book: none\n";
  42. }
  43. else {
  44.   print "\n Link targets appearing multiple times in book:\n";
  45.   print " ----------------------------------------------\n";
  46.   foreach $REPEAT_TARGET (@REPEAT_TARGETS) {
  47.     foreach $REPEAT (split(/:/,$TARGETS{$REPEAT_TARGET})) {
  48.       print " '$REPEAT_TARGET' : $REPEAT\n";
  49.     }
  50.   }
  51. }
  52.  
  53. # find xrefs and compare them to target array; print dangling pointers
  54. # SGIDOC <XREF> tags use IDREF attributes
  55. # SGIDOCBK <XREF> tags use LINKEND attributes
  56. seek(F,0,0);
  57. while (<F>) {
  58.    while (s/<XREF[^>]+(IDREF|LINKEND)\s*=\s*\"([^\"]+)\"[^>]*>/$1/i) {
  59.       $POINTER = $2;
  60.     if (! $TARGETS{$POINTER}) {
  61.       if ($POINTERS{$POINTER}) {
  62.         $POINTERS{$POINTER} += 1;
  63.       } else { 
  64.     $POINTERS{$POINTER} = 1;
  65.       }
  66.     }
  67.   }
  68. }
  69. if (%POINTERS == "") {
  70.   print "\n Unresolved References present in this book: none\n";
  71. }
  72. else {
  73.   print "\n Unresolved References present in this book:\n";
  74.   print " -------------------------------------------\n";
  75.   foreach $POINTER (keys(%POINTERS)) {
  76.     print " '$POINTER' appears $POINTERS{$POINTER} time(s)\n";
  77.   }
  78. }
  79.  
  80. # find and print any extrefs
  81. # SGIDOC uses <EXTREF IDREF="" BOOK="">
  82. # SGIDOCBK uses <LINK EXTREF="" BOOK="">
  83. seek(F,0,0);
  84. while (<F>) {
  85.   while (s/<(EXTREF|LINK)([^>]+)>//i) {
  86.     $LINK_INFO = $2;
  87.     if($LINK_INFO =~ /\sBOOK\s*=\s*"([^\"]+)"/i) {
  88.         $BOOK = $1;
  89.         if($LINK_INFO =~ /\s(IDREF|EXTREF)\s*=\s*"([^\"]+)"/i) {
  90.             $ID = $2;
  91.         } else {
  92.             $ID = "Unknown id destination\n";
  93.         }
  94.         $RESULT = "$BOOK => $ID";
  95.         if ($EXTREFS{$RESULT}) {
  96.             $EXTREFS{$RESULT} += 1; 
  97.         } else {
  98.             $EXTREFS{$RESULT} = 1;
  99.         }
  100.     }
  101.   }
  102. }
  103.  
  104. if (%EXTREFS == "") {
  105.   print "\n External Book References present in this book: none \n";
  106. }
  107. else {
  108.   print "\n External Book References present in this book:\n";
  109.   print " ----------------------------------------------\n";
  110.   foreach $EXREF (keys(%EXTREFS)) {
  111.     print " $EXREF appears $EXTREFS{$EXREF} time(s)\n";
  112.   }
  113. }
  114.  
  115. close(F);
  116.