1 # Perl hooks into the routines in vms.c for interconversion
2 # of VMS and Unix file specification syntax.
5 # Author: Charles Bailey bailey@genetics.upenn.edu
10 VMS::Filespec - convert between VMS and Unix file specification syntax
15 $fullspec = rmsexpand('[.VMS]file.specification');
16 $vmsspec = vmsify('/my/Unix/file/specification');
17 $unixspec = unixify('my:[VMS]file.specification');
18 $path = pathify('my:[VMS.or.Unix.directory]specification.dir');
19 $dirfile = fileify('my:[VMS.or.Unix.directory.specification]');
20 $vmsdir = vmspath('my/VMS/or/Unix/directory/specification.dir');
21 $unixdir = unixpath('my:[VMS.or.Unix.directory]specification.dir');
22 candelete('my:[VMS.or.Unix]file.specification');
26 This package provides routines to simplify conversion between VMS and
27 Unix syntax when processing file specifications. This is useful when
28 porting scripts designed to run under either OS, and also allows you
29 to take advantage of conveniences provided by either syntax (I<e.g.>
30 ability to easily concatenate Unix-style specifications). In
31 addition, it provides an additional file test routine, C<candelete>,
32 which determines whether you have delete access to a file.
34 If you're running under VMS, the routines in this package are special,
35 in that they're automatically made available to any Perl script,
36 whether you're running F<miniperl> or the full F<perl>. The C<use
37 VMS::Filespec> or C<require VMS::Filespec; import VMS::Filespec ...>
38 statement can be used to import the function names into the current
39 package, but they're always available if you use the fully qualified
40 name, whether or not you've mentioned the F<.pm> file in your script.
41 If you're running under another OS and have installed this package, it
42 behaves like a normal Perl extension (in fact, you're using Perl
43 substitutes to emulate the necessary VMS system calls).
45 Each of these routines accepts a file specification in either VMS or
46 Unix syntax, and returns the converted file specification, or C<undef>
47 if an error occurs. The conversions are, for the most part, simply
48 string manipulations; the routines do not check the details of syntax
49 (e.g. that only legal characters are used). There is one exception:
50 when running under VMS, conversions from VMS syntax use the $PARSE
51 service to expand specifications, so illegal syntax, or a relative
52 directory specification which extends above the tope of the current
53 directory path (e.g [---.foo] when in dev:[dir.sub]) will cause
54 errors. In general, any legal file specification will be converted
55 properly, but garbage input tends to produce garbage output.
57 Each of these routines is prototyped as taking a single scalar
58 argument, so you can use them as unary operators in complex
59 expressions (as long as you don't use the C<&> form of
60 subroutine call, which bypasses prototype checking).
63 The routines provided are:
67 Uses the RMS $PARSE and $SEARCH services to expand the input
68 specification to its fully qualified form. (If the file does
69 not exist, the input specification is expanded as much as
70 possible.) If an error occurs, returns C<undef> and sets C<$!>
75 Converts a file specification to VMS syntax.
79 Converts a file specification to Unix syntax.
83 Converts a directory specification to a path - that is, a string you
84 can prepend to a file name to form a valid file specification. If the
85 input file specification uses VMS syntax, the returned path does, too;
86 likewise for Unix syntax (Unix paths are guaranteed to end with '/').
87 Note that this routine will insist that the input be a legal directory
88 file specification; the file type and version, if specified, must be
89 F<.DIR;1>. For compatibility with Unix usage, the type and version
94 Converts a directory specification to the file specification of the
95 directory file - that is, a string you can pass to functions like
96 C<stat> or C<rmdir> to manipulate the directory file. If the
97 input directory specification uses VMS syntax, the returned file
98 specification does, too; likewise for Unix syntax. As with
99 C<pathify>, the input file specification must have a type and
100 version of F<.DIR;1>, or the type and version must be omitted.
104 Acts like C<pathify>, but insures the returned path uses VMS syntax.
108 Acts like C<pathify>, but insures the returned path uses Unix syntax.
112 Determines whether you have delete access to a file. If you do, C<candelete>
113 returns true. If you don't, or its argument isn't a legal file specification,
114 C<candelete> returns FALSE. Unlike other file tests, the argument to
115 C<candelete> must be a file name (not a FileHandle), and, since it's an XSUB,
116 it's a list operator, so you need to be careful about parentheses. Both of
117 these restrictions may be removed in the future if the functionality of
118 C<candelete> becomes part of the Perl core.
122 This document was last revised 22-Feb-1996, for Perl 5.002.
126 package VMS::Filespec;
130 # If you want to use this package on a non-VMS system,
131 # uncomment the following line.
135 @ISA = qw( Exporter );
136 @EXPORT = qw( &vmsify &unixify &pathify &fileify
137 &vmspath &unixpath &candelete &rmsexpand );
145 # The autosplit routines here are provided for use by non-VMS systems
146 # They are not guaranteed to function identically to the XSUBs of the
147 # same name, since they do not have access to the RMS system routine
148 # sys$parse() (in particular, no real provision is made for handling
149 # of complex DECnet node specifications). However, these routines
150 # should be adequate for most purposes.
152 # A sort-of sys$parse() replacement
153 sub rmsexpand ($;$) {
154 my($fspec,$defaults) = @_;
155 if (!$fspec) { return undef }
156 my($node,$dev,$dir,$name,$type,$ver,$dnode,$ddev,$ddir,$dname,$dtype,$dver);
159 $defaults = [] unless $defaults;
160 $defaults = [ $defaults ] unless ref($defaults) && ref($defaults) eq 'ARRAY';
162 while ($fspec !~ m#[:>\]]# && $ENV{$fspec}) { $fspec = $ENV{$fspec} }
165 my($dev,$devtrn,$base);
166 ($dev,$base) = split(/:/,$fspec);
168 while ($devtrn = $ENV{$devtrn}) {
169 if ($devtrn =~ /(.)([:>\]])$/) {
170 $dev .= ':', last if $1 eq '.';
171 $dev = $devtrn, last;
174 $fspec = $dev . $base;
177 ($node,$dev,$dir,$name,$type,$ver) = $fspec =~
178 /([^:]*::)?([^:]*:)?([^>\]]*[>\]])?([^.;]*)(\.?[^.;]*)([.;]?\d*)/;
179 foreach ((@$defaults,$ENV{'DEFAULT'})) {
180 last if $node && $ver && $type && $dev && $dir && $name;
181 ($dnode,$ddev,$ddir,$dname,$dtype,$dver) =
182 /([^:]*::)?([^:]*:)?([^>\]]*[>\]])?([^.;]*)(\.?[^.;]*)([.;]?\d*)/;
183 $node = $dnode if $dnode && !$node;
184 $dev = $ddev if $ddev && !$dev;
185 $dir = $ddir if $ddir && !$dir;
186 $name = $dname if $dname && !$name;
187 $type = $dtype if $dtype && !$type;
188 $ver = $dver if $dver && !$ver;
190 # do this the long way to keep -w happy
192 $fspec .= $node if $node;
193 $fspec .= $dev if $dev;
194 $fspec .= $dir if $dir;
195 $fspec .= $name if $name;
196 $fspec .= $type if $type;
197 $fspec .= $ver if $ver;
203 my($hasdev,$dev,$defdirs,$dir,$base,@dirs,@realdirs);
205 if ($fspec =~ m#^\.(\.?)/?$#) { return $1 ? '[-]' : '[]'; }
206 return $fspec if $fspec !~ m#/#;
207 ($hasdev,$dir,$base) = $fspec =~ m#(/?)(.*)/(.*)#;
208 @dirs = split(m#/#,$dir);
209 if ($base eq '.') { $base = ''; }
210 elsif ($base eq '..') {
215 next unless $_; # protect against // in input
218 if (@realdirs && $realdirs[$#realdirs] ne '-') { pop @realdirs }
219 else { push @realdirs, '-' }
221 else { push @realdirs, $_; }
224 $dev = shift @realdirs;
225 @realdirs = ('000000') unless @realdirs;
226 $base = '' unless $base; # keep -w happy
227 $dev . ':[' . join('.',@realdirs) . "]$base";
230 '[' . join('',map($_ eq '-' ? $_ : ".$_",@realdirs)) . "]$base";
237 return $fspec if $fspec !~ m#[:>\]]#;
238 return '.' if ($fspec eq '[]' || $fspec eq '<>');
239 if ($fspec =~ m#^[<\[](\.|-+)(.*)# ) {
240 $fspec = ($1 eq '.' ? '' : "$1.") . $2;
241 my($dir,$base) = split(/[\]>]/,$fspec);
242 my(@dirs) = grep($_,split(m#\.#,$dir));
243 if ($dirs[0] =~ /^-/) {
244 my($steps) = shift @dirs;
245 for (1..length($steps)) { unshift @dirs, '..'; }
247 join('/',@dirs) . "/$base";
250 $fspec = rmsexpand($fspec,'_N_O_T_:[_R_E_A_L_]');
251 $fspec =~ s/.*_N_O_T_:(?:\[_R_E_A_L_\])?//;
252 my($dev,$dir,$base) = $fspec =~ m#([^:<\[]*):?[<\[](.*)[>\]](.*)#;
253 my(@dirs) = split(m#\.#,$dir);
254 if ($dirs[0] && $dirs[0] =~ /^-/) {
255 my($steps) = shift @dirs;
256 for (1..length($steps)) { unshift @dirs, '..'; }
258 "/$dev/" . join('/',@dirs) . "/$base";
266 if (!$path) { return undef }
267 if ($path =~ /(.+)\.([^:>\]]*)$/) {
269 if ($2 !~ /^dir(?:;1)?$/i) { return undef }
272 if ($path !~ m#[/>\]]#) {
274 while ($ENV{$path}) {
275 ($path = $ENV{$path}) =~ s/:$//;
276 last if $path =~ m#[/>\]]#;
279 if ($path =~ m#[>\]]#) {
280 my($dir,$sep,$base) = $path =~ /(.*)([>\]])(.*)/;
283 "$dir$sep$base.dir;1";
286 if ($dir !~ /\./) { $dir =~ s/([<\[])/${1}000000./; }
287 $dir =~ s#\.(\w+)$#$sep$1#;
301 if (!$fspec) { return undef }
302 if ($fspec =~ m#[/>\]]$#) { return $fspec; }
303 if ($fspec =~ m#(.+)\.([^/>\]]*)$# && $2 && $2 ne '.') {
305 if ($2 !~ /^dir(?:;1)?$/i) { return undef }
308 if ($fspec !~ m#[/>\]]#) {
310 while ($ENV{$fspec}) {
311 if ($ENV{$fspec} =~ m#[>\]]$#) { return $ENV{$fspec} }
312 else { $fspec = $ENV{$fspec} =~ s/:$// }
316 if ($fspec !~ m#[>\]]#) { "$fspec/"; }
318 if ($fspec =~ /([^>\]]+)([>\]])(.+)/) { "$1.$3$2"; }
324 pathify(vmsify($_[0]));
328 pathify(unixify($_[0]));
335 return '' unless -w $fspec;
337 if ($fspec =~ m#/#) {
338 ($parent = $fspec) =~ s#/[^/]+$#;
341 elsif ($parent = fileify($fspec)) { # fileify() here to expand lnms
342 $parent =~ s/[>\]][^>\]]+//;
343 return (-w fileify($parent));
345 else { return (-w '[-]'); }