home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2006 December / PCpro_2006_12.ISO / ossdvd / server / Perl2 / lib / File / Spec / vms.pm < prev    next >
Encoding:
Perl POD Document  |  2002-06-19  |  14.2 KB  |  521 lines

  1. package File::Spec::VMS;
  2.  
  3. use strict;
  4. use vars qw(@ISA $VERSION);
  5. require File::Spec::Unix;
  6.  
  7. $VERSION = '1.2';
  8.  
  9. @ISA = qw(File::Spec::Unix);
  10.  
  11. use Cwd;
  12. use File::Basename;
  13. use VMS::Filespec;
  14.  
  15. =head1 NAME
  16.  
  17. File::Spec::VMS - methods for VMS file specs
  18.  
  19. =head1 SYNOPSIS
  20.  
  21.  require File::Spec::VMS; # Done internally by File::Spec if needed
  22.  
  23. =head1 DESCRIPTION
  24.  
  25. See File::Spec::Unix for a documentation of the methods provided
  26. there. This package overrides the implementation of these methods, not
  27. the semantics.
  28.  
  29. =over 4
  30.  
  31. =item eliminate_macros
  32.  
  33. Expands MM[KS]/Make macros in a text string, using the contents of
  34. identically named elements of C<%$self>, and returns the result
  35. as a file specification in Unix syntax.
  36.  
  37. =cut
  38.  
  39. sub eliminate_macros {
  40.     my($self,$path) = @_;
  41.     return '' unless $path;
  42.     $self = {} unless ref $self;
  43.  
  44.     if ($path =~ /\s/) {
  45.       return join ' ', map { $self->eliminate_macros($_) } split /\s+/, $path;
  46.     }
  47.  
  48.     my($npath) = unixify($path);
  49.     my($complex) = 0;
  50.     my($head,$macro,$tail);
  51.  
  52.     # perform m##g in scalar context so it acts as an iterator
  53.     while ($npath =~ m#(.*?)\$\((\S+?)\)(.*)#gs) { 
  54.         if ($self->{$2}) {
  55.             ($head,$macro,$tail) = ($1,$2,$3);
  56.             if (ref $self->{$macro}) {
  57.                 if (ref $self->{$macro} eq 'ARRAY') {
  58.                     $macro = join ' ', @{$self->{$macro}};
  59.                 }
  60.                 else {
  61.                     print "Note: can't expand macro \$($macro) containing ",ref($self->{$macro}),
  62.                           "\n\t(using MMK-specific deferred substitutuon; MMS will break)\n";
  63.                     $macro = "\cB$macro\cB";
  64.                     $complex = 1;
  65.                 }
  66.             }
  67.             else { ($macro = unixify($self->{$macro})) =~ s#/\Z(?!\n)##; }
  68.             $npath = "$head$macro$tail";
  69.         }
  70.     }
  71.     if ($complex) { $npath =~ s#\cB(.*?)\cB#\${$1}#gs; }
  72.     $npath;
  73. }
  74.  
  75. =item fixpath
  76.  
  77. Catchall routine to clean up problem MM[SK]/Make macros.  Expands macros
  78. in any directory specification, in order to avoid juxtaposing two
  79. VMS-syntax directories when MM[SK] is run.  Also expands expressions which
  80. are all macro, so that we can tell how long the expansion is, and avoid
  81. overrunning DCL's command buffer when MM[KS] is running.
  82.  
  83. If optional second argument has a TRUE value, then the return string is
  84. a VMS-syntax directory specification, if it is FALSE, the return string
  85. is a VMS-syntax file specification, and if it is not specified, fixpath()
  86. checks to see whether it matches the name of a directory in the current
  87. default directory, and returns a directory or file specification accordingly.
  88.  
  89. =cut
  90.  
  91. sub fixpath {
  92.     my($self,$path,$force_path) = @_;
  93.     return '' unless $path;
  94.     $self = bless {} unless ref $self;
  95.     my($fixedpath,$prefix,$name);
  96.  
  97.     if ($path =~ /\s/) {
  98.       return join ' ',
  99.              map { $self->fixpath($_,$force_path) }
  100.          split /\s+/, $path;
  101.     }
  102.  
  103.     if ($path =~ m#^\$\([^\)]+\)\Z(?!\n)#s || $path =~ m#[/:>\]]#) { 
  104.         if ($force_path or $path =~ /(?:DIR\)|\])\Z(?!\n)/) {
  105.             $fixedpath = vmspath($self->eliminate_macros($path));
  106.         }
  107.         else {
  108.             $fixedpath = vmsify($self->eliminate_macros($path));
  109.         }
  110.     }
  111.     elsif ((($prefix,$name) = ($path =~ m#^\$\(([^\)]+)\)(.+)#s)) && $self->{$prefix}) {
  112.         my($vmspre) = $self->eliminate_macros("\$($prefix)");
  113.         # is it a dir or just a name?
  114.         $vmspre = ($vmspre =~ m|/| or $prefix =~ /DIR\Z(?!\n)/) ? vmspath($vmspre) : '';
  115.         $fixedpath = ($vmspre ? $vmspre : $self->{$prefix}) . $name;
  116.         $fixedpath = vmspath($fixedpath) if $force_path;
  117.     }
  118.     else {
  119.         $fixedpath = $path;
  120.         $fixedpath = vmspath($fixedpath) if $force_path;
  121.     }
  122.     # No hints, so we try to guess
  123.     if (!defined($force_path) and $fixedpath !~ /[:>(.\]]/) {
  124.         $fixedpath = vmspath($fixedpath) if -d $fixedpath;
  125.     }
  126.  
  127.     # Trim off root dirname if it's had other dirs inserted in front of it.
  128.     $fixedpath =~ s/\.000000([\]>])/$1/;
  129.     # Special case for VMS absolute directory specs: these will have had device
  130.     # prepended during trip through Unix syntax in eliminate_macros(), since
  131.     # Unix syntax has no way to express "absolute from the top of this device's
  132.     # directory tree".
  133.     if ($path =~ /^[\[>][^.\-]/) { $fixedpath =~ s/^[^\[<]+//; }
  134.     $fixedpath;
  135. }
  136.  
  137. =back
  138.  
  139. =head2 Methods always loaded
  140.  
  141. =over 4
  142.  
  143. =item canonpath (override)
  144.  
  145. Removes redundant portions of file specifications according to VMS syntax.
  146.  
  147. =cut
  148.  
  149. sub canonpath {
  150.     my($self,$path) = @_;
  151.  
  152.     if ($path =~ m|/|) { # Fake Unix
  153.       my $pathify = $path =~ m|/\Z(?!\n)|;
  154.       $path = $self->SUPER::canonpath($path);
  155.       if ($pathify) { return vmspath($path); }
  156.       else          { return vmsify($path);  }
  157.     }
  158.     else {
  159.       $path =~ s/([\[<])000000\./$1/g;                  # [000000.foo     ==> [foo
  160.       $path =~ s/([^-]+)\.(\]\[|><)?000000([\]\>])/$1$3/g;  # foo.000000] ==> foo]
  161.       $path =~ s-\]\[--g;  $path =~ s/><//g;            # foo.][bar       ==> foo.bar
  162.       1 while $path =~ s{([\[<-])\.-}{$1-};             # [.-.-           ==> [--
  163.       $path =~ s/\.[^\[<\.]+\.-([\]\>])/$1/;            # bar.foo.-]      ==> bar]
  164.       $path =~ s/([\[<])(-+)/$1 . "\cx" x length($2)/e; # encode leading '-'s
  165.       $path =~ s/([\[<\.])([^\[<\.\cx]+)\.-\.?/$1/g;    # bar.-.foo       ==> foo
  166.       $path =~ s/([\[<])(\cx+)/$1 . '-' x length($2)/e; # then decode
  167.       return $path;
  168.     }
  169. }
  170.  
  171. =item catdir
  172.  
  173. Concatenates a list of file specifications, and returns the result as a
  174. VMS-syntax directory specification.  No check is made for "impossible"
  175. cases (e.g. elements other than the first being absolute filespecs).
  176.  
  177. =cut
  178.  
  179. sub catdir {
  180.     my ($self,@dirs) = @_;
  181.     my $dir = pop @dirs;
  182.     @dirs = grep($_,@dirs);
  183.     my $rslt;
  184.     if (@dirs) {
  185.     my $path = (@dirs == 1 ? $dirs[0] : $self->catdir(@dirs));
  186.     my ($spath,$sdir) = ($path,$dir);
  187.     $spath =~ s/\.dir\Z(?!\n)//; $sdir =~ s/\.dir\Z(?!\n)//; 
  188.     $sdir = $self->eliminate_macros($sdir) unless $sdir =~ /^[\w\-]+\Z(?!\n)/s;
  189.     $rslt = $self->fixpath($self->eliminate_macros($spath)."/$sdir",1);
  190.  
  191.     # Special case for VMS absolute directory specs: these will have had device
  192.     # prepended during trip through Unix syntax in eliminate_macros(), since
  193.     # Unix syntax has no way to express "absolute from the top of this device's
  194.     # directory tree".
  195.     if ($spath =~ /^[\[<][^.\-]/s) { $rslt =~ s/^[^\[<]+//s; }
  196.     }
  197.     else {
  198.     if    (not defined $dir or not length $dir) { $rslt = ''; }
  199.     elsif ($dir =~ /^\$\([^\)]+\)\Z(?!\n)/s)          { $rslt = $dir; }
  200.     else                                        { $rslt = vmspath($dir); }
  201.     }
  202.     return $self->canonpath($rslt);
  203. }
  204.  
  205. =item catfile
  206.  
  207. Concatenates a list of file specifications, and returns the result as a
  208. VMS-syntax file specification.
  209.  
  210. =cut
  211.  
  212. sub catfile {
  213.     my ($self,@files) = @_;
  214.     my $file = pop @files;
  215.     @files = grep($_,@files);
  216.     my $rslt;
  217.     if (@files) {
  218.     my $path = (@files == 1 ? $files[0] : $self->catdir(@files));
  219.     my $spath = $path;
  220.     $spath =~ s/\.dir\Z(?!\n)//;
  221.     if ($spath =~ /^[^\)\]\/:>]+\)\Z(?!\n)/s && basename($file) eq $file) {
  222.         $rslt = "$spath$file";
  223.     }
  224.     else {
  225.         $rslt = $self->eliminate_macros($spath);
  226.         $rslt = vmsify($rslt.($rslt ? '/' : '').unixify($file));
  227.     }
  228.     }
  229.     else { $rslt = (defined($file) && length($file)) ? vmsify($file) : ''; }
  230.     return $self->canonpath($rslt);
  231. }
  232.  
  233.  
  234. =item curdir (override)
  235.  
  236. Returns a string representation of the current directory: '[]'
  237.  
  238. =cut
  239.  
  240. sub curdir {
  241.     return '[]';
  242. }
  243.  
  244. =item devnull (override)
  245.  
  246. Returns a string representation of the null device: '_NLA0:'
  247.  
  248. =cut
  249.  
  250. sub devnull {
  251.     return "_NLA0:";
  252. }
  253.  
  254. =item rootdir (override)
  255.  
  256. Returns a string representation of the root directory: 'SYS$DISK:[000000]'
  257.  
  258. =cut
  259.  
  260. sub rootdir {
  261.     return 'SYS$DISK:[000000]';
  262. }
  263.  
  264. =item tmpdir (override)
  265.  
  266. Returns a string representation of the first writable directory
  267. from the following list or '' if none are writable:
  268.  
  269.     sys$scratch:
  270.     $ENV{TMPDIR}
  271.  
  272. Since perl 5.8.0, if running under taint mode, and if $ENV{TMPDIR}
  273. is tainted, it is not used.
  274.  
  275. =cut
  276.  
  277. my $tmpdir;
  278. sub tmpdir {
  279.     return $tmpdir if defined $tmpdir;
  280.     my @dirlist = ('sys$scratch:', $ENV{TMPDIR});
  281.     {
  282.     no strict 'refs';
  283.     if (${"\cTAINT"}) { # Check for taint mode on perl >= 5.8.0
  284.             require Scalar::Util;
  285.         pop @dirlist if Scalar::Util::tainted($ENV{TMPDIR});
  286.     }
  287.     }
  288.     foreach (@dirlist) {
  289.     next unless defined && -d && -w _;
  290.     $tmpdir = $_;
  291.     last;
  292.     }
  293.     $tmpdir = '' unless defined $tmpdir;
  294.     return $tmpdir;
  295. }
  296.  
  297. =item updir (override)
  298.  
  299. Returns a string representation of the parent directory: '[-]'
  300.  
  301. =cut
  302.  
  303. sub updir {
  304.     return '[-]';
  305. }
  306.  
  307. =item case_tolerant (override)
  308.  
  309. VMS file specification syntax is case-tolerant.
  310.  
  311. =cut
  312.  
  313. sub case_tolerant {
  314.     return 1;
  315. }
  316.  
  317. =item path (override)
  318.  
  319. Translate logical name DCL$PATH as a searchlist, rather than trying
  320. to C<split> string value of C<$ENV{'PATH'}>.
  321.  
  322. =cut
  323.  
  324. sub path {
  325.     my (@dirs,$dir,$i);
  326.     while ($dir = $ENV{'DCL$PATH;' . $i++}) { push(@dirs,$dir); }
  327.     return @dirs;
  328. }
  329.  
  330. =item file_name_is_absolute (override)
  331.  
  332. Checks for VMS directory spec as well as Unix separators.
  333.  
  334. =cut
  335.  
  336. sub file_name_is_absolute {
  337.     my ($self,$file) = @_;
  338.     # If it's a logical name, expand it.
  339.     $file = $ENV{$file} while $file =~ /^[\w\$\-]+\Z(?!\n)/s && $ENV{$file};
  340.     return scalar($file =~ m!^/!s             ||
  341.           $file =~ m![<\[][^.\-\]>]!  ||
  342.           $file =~ /:[^<\[]/);
  343. }
  344.  
  345. =item splitpath (override)
  346.  
  347. Splits using VMS syntax.
  348.  
  349. =cut
  350.  
  351. sub splitpath {
  352.     my($self,$path) = @_;
  353.     my($dev,$dir,$file) = ('','','');
  354.  
  355.     vmsify($path) =~ /(.+:)?([\[<].*[\]>])?(.*)/s;
  356.     return ($1 || '',$2 || '',$3);
  357. }
  358.  
  359. =item splitdir (override)
  360.  
  361. Split dirspec using VMS syntax.
  362.  
  363. =cut
  364.  
  365. sub splitdir {
  366.     my($self,$dirspec) = @_;
  367.     $dirspec =~ s/\]\[//g;  $dirspec =~ s/\-\-/-.-/g;
  368.     $dirspec = "[$dirspec]" unless $dirspec =~ /[\[<]/; # make legal
  369.     my(@dirs) = split('\.', vmspath($dirspec));
  370.     $dirs[0] =~ s/^[\[<]//s;  $dirs[-1] =~ s/[\]>]\Z(?!\n)//s;
  371.     @dirs;
  372. }
  373.  
  374.  
  375. =item catpath (override)
  376.  
  377. Construct a complete filespec using VMS syntax
  378.  
  379. =cut
  380.  
  381. sub catpath {
  382.     my($self,$dev,$dir,$file) = @_;
  383.     if ($dev =~ m|^/+([^/]+)|) { $dev = "$1:"; }
  384.     else { $dev .= ':' unless $dev eq '' or $dev =~ /:\Z(?!\n)/; }
  385.     if (length($dev) or length($dir)) {
  386.       $dir = "[$dir]" unless $dir =~ /[\[<\/]/;
  387.       $dir = vmspath($dir);
  388.     }
  389.     "$dev$dir$file";
  390. }
  391.  
  392. =item abs2rel (override)
  393.  
  394. Use VMS syntax when converting filespecs.
  395.  
  396. =cut
  397.  
  398. sub abs2rel {
  399.     my $self = shift;
  400.  
  401.     return vmspath(File::Spec::Unix::abs2rel( $self, @_ ))
  402.         if ( join( '', @_ ) =~ m{/} ) ;
  403.  
  404.     my($path,$base) = @_;
  405.  
  406.     # Note: we use '/' to glue things together here, then let canonpath()
  407.     # clean them up at the end.
  408.  
  409.     # Clean up $path
  410.     if ( ! $self->file_name_is_absolute( $path ) ) {
  411.         $path = $self->rel2abs( $path ) ;
  412.     }
  413.     else {
  414.         $path = $self->canonpath( $path ) ;
  415.     }
  416.  
  417.     # Figure out the effective $base and clean it up.
  418.     if ( !defined( $base ) || $base eq '' ) {
  419.         $base = cwd() ;
  420.         $base = $self->canonpath( $base ) ;
  421.     }
  422.     elsif ( ! $self->file_name_is_absolute( $base ) ) {
  423.         $base = $self->rel2abs( $base ) ;
  424.     }
  425.     else {
  426.         $base = $self->canonpath( $base ) ;
  427.     }
  428.  
  429.     # Split up paths
  430.     my ( $path_directories, $path_file ) =
  431.         ($self->splitpath( $path, 1 ))[1,2] ;
  432.  
  433.     $path_directories = $1
  434.         if $path_directories =~ /^\[(.*)\]\Z(?!\n)/s ;
  435.  
  436.     my $base_directories = ($self->splitpath( $base, 1 ))[1] ;
  437.  
  438.     $base_directories = $1
  439.         if $base_directories =~ /^\[(.*)\]\Z(?!\n)/s ;
  440.  
  441.     # Now, remove all leading components that are the same
  442.     my @pathchunks = $self->splitdir( $path_directories );
  443.     unshift(@pathchunks,'000000') unless $pathchunks[0] eq '000000';
  444.     my @basechunks = $self->splitdir( $base_directories );
  445.     unshift(@basechunks,'000000') unless $basechunks[0] eq '000000';
  446.  
  447.     while ( @pathchunks && 
  448.             @basechunks && 
  449.             lc( $pathchunks[0] ) eq lc( $basechunks[0] ) 
  450.           ) {
  451.         shift @pathchunks ;
  452.         shift @basechunks ;
  453.     }
  454.  
  455.     # @basechunks now contains the directories to climb out of,
  456.     # @pathchunks now has the directories to descend in to.
  457.     $path_directories = '-.' x @basechunks . join( '.', @pathchunks ) ;
  458.     $path_directories =~ s{\.\Z(?!\n)}{} ;
  459.     return $self->canonpath( $self->catpath( '', $path_directories, $path_file ) ) ;
  460. }
  461.  
  462.  
  463. =item rel2abs (override)
  464.  
  465. Use VMS syntax when converting filespecs.
  466.  
  467. =cut
  468.  
  469. sub rel2abs {
  470.     my $self = shift ;
  471.     return vmspath(File::Spec::Unix::rel2abs( $self, @_ ))
  472.         if ( join( '', @_ ) =~ m{/} ) ;
  473.  
  474.     my ($path,$base ) = @_;
  475.     # Clean up and split up $path
  476.     if ( ! $self->file_name_is_absolute( $path ) ) {
  477.         # Figure out the effective $base and clean it up.
  478.         if ( !defined( $base ) || $base eq '' ) {
  479.             $base = cwd() ;
  480.         }
  481.         elsif ( ! $self->file_name_is_absolute( $base ) ) {
  482.             $base = $self->rel2abs( $base ) ;
  483.         }
  484.         else {
  485.             $base = $self->canonpath( $base ) ;
  486.         }
  487.  
  488.         # Split up paths
  489.         my ( $path_directories, $path_file ) =
  490.             ($self->splitpath( $path ))[1,2] ;
  491.  
  492.         my ( $base_volume, $base_directories ) =
  493.             $self->splitpath( $base ) ;
  494.  
  495.         $path_directories = '' if $path_directories eq '[]' ||
  496.                                   $path_directories eq '<>';
  497.         my $sep = '' ;
  498.         $sep = '.'
  499.             if ( $base_directories =~ m{[^.\]>]\Z(?!\n)} &&
  500.                  $path_directories =~ m{^[^.\[<]}s
  501.             ) ;
  502.         $base_directories = "$base_directories$sep$path_directories";
  503.         $base_directories =~ s{\.?[\]>][\[<]\.?}{.};
  504.  
  505.         $path = $self->catpath( $base_volume, $base_directories, $path_file );
  506.    }
  507.  
  508.     return $self->canonpath( $path ) ;
  509. }
  510.  
  511.  
  512. =back
  513.  
  514. =head1 SEE ALSO
  515.  
  516. L<File::Spec>
  517.  
  518. =cut
  519.  
  520. 1;
  521.