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, ir undef if
46 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 '/').
75 Converts a directory specification to the file specification of the
76 directory file - that is, a string you can pass to functions like
77 C<stat> or C<rmdir> to manipulate the directory file. If the
78 input directory specification uses VMS syntax, the returned file
79 specification does, too; likewise for Unix syntax.
83 Acts like C<pathify>, but insures the returned path uses VMS syntax.
87 Acts like C<pathify>, but insures the returned path uses Unix syntax.
91 Determines whether you have delete access to a file. If you do, C<candelete>
92 returns true. If you don't, or its argument isn't a legal file specification,
93 C<candelete> returns FALSE. Unlike other file tests, the argument to
94 C<candelete> must be a file name (not a FileHandle), and, since it's an XSUB,
95 it's a list operator, so you need to be careful about parentheses. Both of
96 these restrictions may be removed in the future if the functionality of
97 C<candelete> becomes part of the Perl core.
101 This document was last revised 08-Mar-1995, for Perl 5.001.
105 package VMS::Filespec;
107 # If you want to use this package on a non-VMS system, uncomment
108 # the following line, and add AutoLoader to @ISA.
109 # require AutoLoader;
112 @ISA = qw( Exporter );
113 @EXPORT = qw( &rmsexpand &vmsify &unixify &pathify
114 &fileify &vmspath &unixpath &candelete);
122 # The autosplit routines here are provided for use by non-VMS systems
123 # They are not guaranteed to function identically to the XSUBs of the
124 # same name, since they do not have access to the RMS system routine
125 # sys$parse() (in particular, no real provision is made for handling
126 # of complex DECnet node specifications). However, these routines
127 # should be adequate for most purposes.
129 # A sort-of sys$parse() replacement
131 my($fspec,$defaults) = @_;
132 if (!$fspec) { return undef }
133 my($node,$dev,$dir,$name,$type,$ver,$dnode,$ddev,$ddir,$dname,$dtype,$dver);
136 $defaults = [] unless $defaults;
137 $defaults = [ $defaults ] unless ref($defaults) && ref($defaults) eq 'ARRAY';
139 while ($fspec !~ m#[:>\]]# && $ENV{$fspec}) { $fspec = $ENV{$fspec} }
142 my($dev,$devtrn,$base);
143 ($dev,$base) = split(/:/,$fspec);
145 while ($devtrn = $ENV{$devtrn}) {
146 if ($devtrn =~ /(.)([:>\]])$/) {
147 $dev .= ':', last if $1 eq '.';
148 $dev = $devtrn, last;
151 $fspec = $dev . $base;
154 ($node,$dev,$dir,$name,$type,$ver) = $fspec =~
155 /([^:]*::)?([^:]*:)?([^>\]]*[>\]])?([^.;]*)(\.?[^.;]*)([.;]?\d*)/;
156 foreach ((@$defaults,$ENV{'DEFAULT'})) {
157 last if $node && $ver && $type && $dev && $dir && $name;
158 ($dnode,$ddev,$ddir,$dname,$dtype,$dver) =
159 /([^:]*::)?([^:]*:)?([^>\]]*[>\]])?([^.;]*)(\.?[^.;]*)([.;]?\d*)/;
160 $node = $dnode if $dnode && !$node;
161 $dev = $ddev if $ddev && !$dev;
162 $dir = $ddir if $ddir && !$dir;
163 $name = $dname if $dname && !$name;
164 $type = $dtype if $dtype && !$type;
165 $ver = $dver if $dver && !$ver;
167 # do this the long way to keep -w happy
169 $fspec .= $node if $node;
170 $fspec .= $dev if $dev;
171 $fspec .= $dir if $dir;
172 $fspec .= $name if $name;
173 $fspec .= $type if $type;
174 $fspec .= $ver if $ver;
180 my($hasdev,$dev,$defdirs,$dir,$base,@dirs,@realdirs);
182 if ($fspec =~ m#^\.(\.?)/?$#) { return $1 ? '[-]' : '[]'; }
183 return $fspec if $fspec !~ m#/#;
184 ($hasdev,$dir,$base) = $fspec =~ m#(/?)(.*)/(.*)#;
185 @dirs = split(m#/#,$dir);
186 if ($base eq '.') { $base = ''; }
187 elsif ($base eq '..') {
192 next unless $_; # protect against // in input
195 if (@realdirs && $realdirs[$#realdirs] ne '-') { pop @realdirs }
196 else { push @realdirs, '-' }
198 else { push @realdirs, $_; }
201 $dev = shift @realdirs;
202 @realdirs = ('000000') unless @realdirs;
203 $base = '' unless $base; # keep -w happy
204 $dev . ':[' . join('.',@realdirs) . "]$base";
207 '[' . join('',map($_ eq '-' ? $_ : ".$_",@realdirs)) . "]$base";
214 return $fspec if $fspec !~ m#[:>\]]#;
215 return '.' if ($fspec eq '[]' || $fspec eq '<>');
216 if ($fspec =~ m#^[<\[](\.|-+)(.*)# ) {
217 $fspec = ($1 eq '.' ? '' : "$1.") . $2;
218 my($dir,$base) = split(/[\]>]/,$fspec);
219 my(@dirs) = grep($_,split(m#\.#,$dir));
220 if ($dirs[0] =~ /^-/) {
221 my($steps) = shift @dirs;
222 for (1..length($steps)) { unshift @dirs, '..'; }
224 join('/',@dirs) . "/$base";
227 $fspec = rmsexpand($fspec,'_N_O_T_:[_R_E_A_L_]');
228 $fspec =~ s/.*_N_O_T_:(?:\[_R_E_A_L_\])?//;
229 my($dev,$dir,$base) = $fspec =~ m#([^:<\[]*):?[<\[](.*)[>\]](.*)#;
230 my(@dirs) = split(m#\.#,$dir);
231 if ($dirs[0] && $dirs[0] =~ /^-/) {
232 my($steps) = shift @dirs;
233 for (1..length($steps)) { unshift @dirs, '..'; }
235 "/$dev/" . join('/',@dirs) . "/$base";
243 if (!$path) { return undef }
244 if ($path =~ /(.+)\.([^:>\]]*)$/) {
246 if ($2 !~ /^dir(?:;1)?$/i) { return undef }
249 if ($path !~ m#[/>\]]#) {
251 while ($ENV{$path}) {
252 ($path = $ENV{$path}) =~ s/:$//;
253 last if $path =~ m#[/>\]]#;
256 if ($path =~ m#[>\]]#) {
257 my($dir,$sep,$base) = $path =~ /(.*)([>\]])(.*)/;
260 "$dir$sep$base.dir;1";
263 if ($dir !~ /\./) { $dir =~ s/([<\[])/${1}000000./; }
264 $dir =~ s#\.(\w+)$#$sep$1#;
278 if (!$fspec) { return undef }
279 if ($fspec =~ m#[/>\]]$#) { return $fspec; }
280 if ($fspec =~ m#(.+)\.([^/>\]]*)$# && $2 && $2 ne '.') {
282 if ($2 !~ /^dir(?:;1)?$/i) { return undef }
285 if ($fspec !~ m#[/>\]]#) {
287 while ($ENV{$fspec}) {
288 if ($ENV{$fspec} =~ m#[>\]]$#) { return $ENV{$fspec} }
289 else { $fspec = $ENV{$fspec} =~ s/:$// }
293 if ($fspec !~ m#[>\]]#) { "$fspec/"; }
295 if ($fspec =~ /([^>\]]+)([>\]])(.+)/) { "$1.$3$2"; }
301 pathify(vmsify($_[0]));
305 pathify(unixify($_[0]));
312 return '' unless -w $fspec;
314 if ($fspec =~ m#/#) {
315 ($parent = $fspec) =~ s#/[^/]+$#;
318 elsif ($parent = fileify($fspec)) { # fileify() here to expand lnms
319 $parent =~ s/[>\]][^>\]]+//;
320 return (-w fileify($parent));
322 else { return (-w '[-]'); }