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 $vmsspec = vmsify('/my/Unix/file/specification');
16 $unixspec = unixify('my:[VMS]file.specification');
17 $path = pathify('my:[VMS.or.Unix.directory]specification.dir');
18 $dirfile = fileify('my:[VMS.or.Unix.directory.specification]');
19 $vmsdir = vmspath('my/VMS/or/Unix/directory/specification.dir');
20 $unixdir = unixpath('my:[VMS.or.Unix.directory]specification.dir');
21 candelete('my:[VMS.or.Unix]file.specification');
25 This package provides routines to simplify conversion between VMS and
26 Unix syntax when processing file specifications. This is useful when
27 porting scripts designed to run under either OS, and also allows you
28 to take advantage of conveniences provided by either syntax (e.g.
29 ability to easily concatenate Unix-style specifications). In
30 addition, it provides an additional file test routine, C<candelete>,
31 which determines whether you have delete access to a file.
33 If you're running under VMS, the routines in this package are special,
34 in that they're automatically made available to any Perl script,
35 whether you're running F<miniperl> or the full F<perl>. The C<use
36 VMS::Filespec> or C<require VMS::Filespec; import VMS::Filespec ...>
37 statement can be used to import the function names into the current
38 package, but they're always available if you use the fully qualified
39 name, whether or not you've mentioned the F<.pm> file in your script.
40 If you're running under another OS and have installed this package, it
41 behaves like a normal Perl extension (in fact, you're using Perl
42 substitutes to emulate the necessary VMS system calls).
44 Each of these routines accepts a file specification in either VMS or
45 Unix syntax, and returns the converted file specification, or C<undef>
46 if an error occurs. The conversions are, for the most part, simply
47 string manipulations; the routines do not check the details of syntax
48 (e.g. that only legal characters are used). There is one exception:
49 when running under VMS, conversions from VMS syntax use the $PARSE
50 service to expand specifications, so illegal syntax, or a relative
51 directory specification which extends above the tope of the current
52 directory path (e.g [---.foo] when in dev:[dir.sub]) will cause
53 errors. In general, any legal file specification will be converted
54 properly, but garbage input tends to produce garbage output.
56 The routines provided are:
60 Converts a file specification to VMS syntax.
64 Converts a file specification to Unix syntax.
68 Converts a directory specification to a path - that is, a string you
69 can prepend to a file name to form a valid file specification. If the
70 input file specification uses VMS syntax, the returned path does, too;
71 likewise for Unix syntax (Unix paths are guaranteed to end with '/').
72 Note that this routine will insist that the input be a legal directory
73 file specification; the file type and version, if specified, must be
74 F<.DIR;1>. For compatibility with Unix usage, the type and version
79 Converts a directory specification to the file specification of the
80 directory file - that is, a string you can pass to functions like
81 C<stat> or C<rmdir> to manipulate the directory file. If the
82 input directory specification uses VMS syntax, the returned file
83 specification does, too; likewise for Unix syntax. As with
84 C<pathify>, the input file specification must have a type and
85 version of F<.DIR;1>, or the type and version must be omitted.
89 Acts like C<pathify>, but insures the returned path uses VMS syntax.
93 Acts like C<pathify>, but insures the returned path uses Unix syntax.
97 Determines whether you have delete access to a file. If you do, C<candelete>
98 returns true. If you don't, or its argument isn't a legal file specification,
99 C<candelete> returns FALSE. Unlike other file tests, the argument to
100 C<candelete> must be a file name (not a FileHandle), and, since it's an XSUB,
101 it's a list operator, so you need to be careful about parentheses. Both of
102 these restrictions may be removed in the future if the functionality of
103 C<candelete> becomes part of the Perl core.
107 This document was last revised 08-Dec-1995, for Perl 5.002.
111 package VMS::Filespec;
113 # If you want to use this package on a non-VMS system,
114 # uncomment the following line.
118 @ISA = qw( Exporter );
119 @EXPORT = qw( &vmsify &unixify &pathify &fileify
120 &vmspath &unixpath &candelete);
122 @EXPORT_OK = qw( &rmsexpand );
129 # The autosplit routines here are provided for use by non-VMS systems
130 # They are not guaranteed to function identically to the XSUBs of the
131 # same name, since they do not have access to the RMS system routine
132 # sys$parse() (in particular, no real provision is made for handling
133 # of complex DECnet node specifications). However, these routines
134 # should be adequate for most purposes.
136 # A sort-of sys$parse() replacement
138 my($fspec,$defaults) = @_;
139 if (!$fspec) { return undef }
140 my($node,$dev,$dir,$name,$type,$ver,$dnode,$ddev,$ddir,$dname,$dtype,$dver);
143 $defaults = [] unless $defaults;
144 $defaults = [ $defaults ] unless ref($defaults) && ref($defaults) eq 'ARRAY';
146 while ($fspec !~ m#[:>\]]# && $ENV{$fspec}) { $fspec = $ENV{$fspec} }
149 my($dev,$devtrn,$base);
150 ($dev,$base) = split(/:/,$fspec);
152 while ($devtrn = $ENV{$devtrn}) {
153 if ($devtrn =~ /(.)([:>\]])$/) {
154 $dev .= ':', last if $1 eq '.';
155 $dev = $devtrn, last;
158 $fspec = $dev . $base;
161 ($node,$dev,$dir,$name,$type,$ver) = $fspec =~
162 /([^:]*::)?([^:]*:)?([^>\]]*[>\]])?([^.;]*)(\.?[^.;]*)([.;]?\d*)/;
163 foreach ((@$defaults,$ENV{'DEFAULT'})) {
164 last if $node && $ver && $type && $dev && $dir && $name;
165 ($dnode,$ddev,$ddir,$dname,$dtype,$dver) =
166 /([^:]*::)?([^:]*:)?([^>\]]*[>\]])?([^.;]*)(\.?[^.;]*)([.;]?\d*)/;
167 $node = $dnode if $dnode && !$node;
168 $dev = $ddev if $ddev && !$dev;
169 $dir = $ddir if $ddir && !$dir;
170 $name = $dname if $dname && !$name;
171 $type = $dtype if $dtype && !$type;
172 $ver = $dver if $dver && !$ver;
174 # do this the long way to keep -w happy
176 $fspec .= $node if $node;
177 $fspec .= $dev if $dev;
178 $fspec .= $dir if $dir;
179 $fspec .= $name if $name;
180 $fspec .= $type if $type;
181 $fspec .= $ver if $ver;
187 my($hasdev,$dev,$defdirs,$dir,$base,@dirs,@realdirs);
189 if ($fspec =~ m#^\.(\.?)/?$#) { return $1 ? '[-]' : '[]'; }
190 return $fspec if $fspec !~ m#/#;
191 ($hasdev,$dir,$base) = $fspec =~ m#(/?)(.*)/(.*)#;
192 @dirs = split(m#/#,$dir);
193 if ($base eq '.') { $base = ''; }
194 elsif ($base eq '..') {
199 next unless $_; # protect against // in input
202 if (@realdirs && $realdirs[$#realdirs] ne '-') { pop @realdirs }
203 else { push @realdirs, '-' }
205 else { push @realdirs, $_; }
208 $dev = shift @realdirs;
209 @realdirs = ('000000') unless @realdirs;
210 $base = '' unless $base; # keep -w happy
211 $dev . ':[' . join('.',@realdirs) . "]$base";
214 '[' . join('',map($_ eq '-' ? $_ : ".$_",@realdirs)) . "]$base";
221 return $fspec if $fspec !~ m#[:>\]]#;
222 return '.' if ($fspec eq '[]' || $fspec eq '<>');
223 if ($fspec =~ m#^[<\[](\.|-+)(.*)# ) {
224 $fspec = ($1 eq '.' ? '' : "$1.") . $2;
225 my($dir,$base) = split(/[\]>]/,$fspec);
226 my(@dirs) = grep($_,split(m#\.#,$dir));
227 if ($dirs[0] =~ /^-/) {
228 my($steps) = shift @dirs;
229 for (1..length($steps)) { unshift @dirs, '..'; }
231 join('/',@dirs) . "/$base";
234 $fspec = rmsexpand($fspec,'_N_O_T_:[_R_E_A_L_]');
235 $fspec =~ s/.*_N_O_T_:(?:\[_R_E_A_L_\])?//;
236 my($dev,$dir,$base) = $fspec =~ m#([^:<\[]*):?[<\[](.*)[>\]](.*)#;
237 my(@dirs) = split(m#\.#,$dir);
238 if ($dirs[0] && $dirs[0] =~ /^-/) {
239 my($steps) = shift @dirs;
240 for (1..length($steps)) { unshift @dirs, '..'; }
242 "/$dev/" . join('/',@dirs) . "/$base";
250 if (!$path) { return undef }
251 if ($path =~ /(.+)\.([^:>\]]*)$/) {
253 if ($2 !~ /^dir(?:;1)?$/i) { return undef }
256 if ($path !~ m#[/>\]]#) {
258 while ($ENV{$path}) {
259 ($path = $ENV{$path}) =~ s/:$//;
260 last if $path =~ m#[/>\]]#;
263 if ($path =~ m#[>\]]#) {
264 my($dir,$sep,$base) = $path =~ /(.*)([>\]])(.*)/;
267 "$dir$sep$base.dir;1";
270 if ($dir !~ /\./) { $dir =~ s/([<\[])/${1}000000./; }
271 $dir =~ s#\.(\w+)$#$sep$1#;
285 if (!$fspec) { return undef }
286 if ($fspec =~ m#[/>\]]$#) { return $fspec; }
287 if ($fspec =~ m#(.+)\.([^/>\]]*)$# && $2 && $2 ne '.') {
289 if ($2 !~ /^dir(?:;1)?$/i) { return undef }
292 if ($fspec !~ m#[/>\]]#) {
294 while ($ENV{$fspec}) {
295 if ($ENV{$fspec} =~ m#[>\]]$#) { return $ENV{$fspec} }
296 else { $fspec = $ENV{$fspec} =~ s/:$// }
300 if ($fspec !~ m#[>\]]#) { "$fspec/"; }
302 if ($fspec =~ /([^>\]]+)([>\]])(.+)/) { "$1.$3$2"; }
308 pathify(vmsify($_[0]));
312 pathify(unixify($_[0]));
319 return '' unless -w $fspec;
321 if ($fspec =~ m#/#) {
322 ($parent = $fspec) =~ s#/[^/]+$#;
325 elsif ($parent = fileify($fspec)) { # fileify() here to expand lnms
326 $parent =~ s/[>\]][^>\]]+//;
327 return (-w fileify($parent));
329 else { return (-w '[-]'); }