home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / sgml-data / sgml-catalog-check.pl
Encoding:
Perl Script  |  2004-01-11  |  4.2 KB  |  168 lines

  1. #!/usr/bin/perl
  2. # sgml-catalog-check.pl -- check sgml catalog file
  3. #
  4. #Author: apharris@onshore.com (A. P. Harris)
  5. #$Date: 2004/01/11 05:53:25 $
  6. #$Revision: 1.20 $
  7. #
  8. #Todo:
  9. #    cross check the links/dtds and make sure they all appear in the
  10. #      SGML catalog
  11. #    convert to use perl sgml stuff instead of hand-rolling?
  12. #    make a nice lintian script from this
  13. #    deal with declation and notation files
  14.  
  15. use Getopt::Std;
  16.  
  17. $Verbose = 1;            # verboseness, 1 == chatty, 2 == loud
  18. $SGMLdir = "debian/tmp/usr/share/sgml"; # default dir for link making etc
  19. $Catalog = "sgml.catalog";    # default SGML catalog file
  20. $ChopEN = 1;            # whether to chop off //EN[//.*] language specifiers
  21.  
  22. $Usage = "Usage: $0 [-d <SGML dir>] [-v #] [-e] [<SGML catalog file>]
  23. Check SGML catalog file, create the links as documented in the SGML
  24. sub-policy, and also ensure that the files referenced from the catalog
  25. file actually exists.
  26.    -d <SGML dir>        base dir, default is $SGMLdir
  27.    -v <number>        verbosity amount, 0=silent, 1=default, 2=debug
  28.    -e                   don't omit the trailing EN language specifier (//EN)
  29.    -l                   legacy argument, ignored
  30.    <SGML catalog file>  default is $Catalog
  31. ";
  32.  
  33. $warnings = $errors = 0;    # error and warning count
  34.  
  35. &getopts('ehlv:d:') || die $Usage;
  36.  
  37. if ( $opt_h ) 
  38. {
  39.     print $Usage;
  40.     $opt_h && exit;        # shut up -w
  41. }
  42. elsif ( $opt_d == 1 ) {
  43.     die "option '-d' must have an argument\n$Usage";
  44. elsif ( $opt_d ) {
  45.     $SGMLdir = $opt_d;
  46. }
  47.  
  48. if ( defined($opt_v) ) {
  49.     $Verbose = $opt_v;
  50. }
  51.  
  52. if ( $opt_l ) {
  53.     $opt_l = $opt_l;            # shut up, -w
  54.     warn("symlinks under /usr/share/sgml no longer desired or created, ignoring -l\n");
  55. }
  56.  
  57. if ( $opt_e ) {
  58.     $opt_e = $opt_e;            # shut up, -w
  59.     $ChopEN = 0;
  60. }
  61.  
  62. if ( $#ARGV > 0 ) {
  63.     die "too many arguments\n$Usage";
  64. } elsif ( $#ARGV == 0 ) {
  65.     $Catalog = $ARGV[0];
  66. }
  67.  
  68. ( -f $Catalog ) or
  69.     die "catalog file $Catalog does not exist\n$Usage";
  70. ( -d $SGMLdir ) or
  71.     die "SGML directory $SGMLdir does not exist\n$Usage";
  72.  
  73. open(CAT, "<$Catalog") or
  74.     die "cannot read $Catalog: $!\n";
  75.  
  76. ## when checking for system ids, we need to check relative to the
  77. ## catalog file location, so figure out the relative dir of the
  78. ## catalog file, possibly removing a prepended SGMLdir
  79.  
  80. $CatDir = `dirname $Catalog`;
  81. chomp($CatDir);
  82. $CatDir =~ s/^$SGMLdir\/?//;
  83.  
  84. while (<CAT>) {
  85.     chomp;
  86.     # FIXME: add another line if next line starts with whitespace
  87.     # D: skipped catalog line:
  88.     #  PUBLIC "-//OASIS//DTD DocBook V4.2//EN"
  89.     # D: skipped catalog line:
  90.     #    "docbook.dtd"
  91.  
  92.     if ( m/^PUBLIC\s+\"([^\"]+)\"\s+\"?([^\s\"]+)\"?/ ) {
  93.     ( $id, $file ) = ( $1, $2 );
  94.     debug("found public identifier \"$id\"");
  95.     debug("system identifier is $file");
  96.     if ( -f "$SGMLdir/$CatDir/$file" ) {
  97.             $file = "$CatDir/$file";
  98.         } elsif ( ! -f "$SGMLdir/$file" ) {
  99.         error("referenced-file-does-not-exist $SGMLdir/$CatDir/$file of $SGMLdir/$file");
  100.         next;
  101.     }
  102.     
  103.     if ( $id =~ m!^(.+)//(?:([^/]+)//)?(ELEMENTS|DOCUMENT|ENTITIES|DTD)\s+([^/]+)//(.+)$! ) {
  104.         ( $reg, $vendor, $type, $name, $misc ) = ( $1, $2, $3, $4, $5 );
  105.  
  106.         if ( $type eq "ENTITIES" ) {
  107.                                 # AOK, no checking for location
  108.         } 
  109.         elsif ( $type eq "DTD" || $type eq "ELEMENTS" ) {
  110.                                 # AOK, no checking for location
  111.         }
  112.         elsif ( $type eq "DOCUMENT" ) {
  113.         ( $file =~ m!^dtd/! || $file =~ m!^entities! ) &&
  114.             error("DOCUMENT-in-dtd-or-entities-dir $file");
  115.         }
  116.         else {
  117.         error("identifier-type-not-recognized $type on FPI $id");
  118.         }
  119.         
  120.         # would be nice to check that the DTD file is reasonable
  121.         # oh well...
  122.  
  123.             # quieten warnings
  124.             $name = $name;
  125.             $misc = $misc;
  126.             $reg = $reg;
  127.             $vendor = $vendor;
  128.     }
  129.     else {
  130.         error("SGML-identifier-not-in-recognized-form $id");
  131.         next;
  132.     }
  133.     }
  134.     else {
  135.     debug("skipped catalog line:\n   $_");
  136.     next;
  137.     }
  138. }
  139.  
  140. if ( $errors ) {
  141.     exit(1);
  142. }
  143. exit(0);
  144.  
  145. sub debug {
  146.     local($msg) = @_;
  147.     ( $Verbose > 1 ) && warn("D: $msg\n");
  148. }
  149.  
  150. sub inform {
  151.     local($msg) = @_;
  152.     ( $Verbose ) && warn("N: $msg\n");
  153. }
  154.  
  155. sub warning {
  156.     local($msg) = @_;
  157.     $warnings++;
  158.     warn("W: $msg\n");
  159. }
  160.  
  161. sub error {
  162.     local($msg) = @_;
  163.     $errors++;
  164.     warn("E: $msg\n");
  165. }
  166.  
  167.