1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
# Perl hooks into the routines in vms.c for interconversion
# of VMS and Unix file specification syntax.
#
# Version: 1.1
# Author: Charles Bailey bailey@genetics.upenn.edu
# Revised: 08-Mar-1995
=head1 NAME
VMS::Filespec - convert between VMS and Unix file specification syntax
=head1 SYNOPSIS
use VMS::Filespec;
$fullspec = rmsexpand('[.VMS]file.specification'[, 'default:[file.spec]']);
$vmsspec = vmsify('/my/Unix/file/specification');
$unixspec = unixify('my:[VMS]file.specification');
$path = pathify('my:[VMS.or.Unix.directory]specification.dir');
$dirfile = fileify('my:[VMS.or.Unix.directory.specification]');
$vmsdir = vmspath('my/VMS/or/Unix/directory/specification.dir');
$unixdir = unixpath('my:[VMS.or.Unix.directory]specification.dir');
candelete('my:[VMS.or.Unix]file.specification');
=head1 DESCRIPTION
This package provides routines to simplify conversion between VMS and
Unix syntax when processing file specifications. This is useful when
porting scripts designed to run under either OS, and also allows you
to take advantage of conveniences provided by either syntax (I<e.g.>
ability to easily concatenate Unix-style specifications). In
addition, it provides an additional file test routine, C<candelete>,
which determines whether you have delete access to a file.
If you're running under VMS, the routines in this package are special,
in that they're automatically made available to any Perl script,
whether you're running F<miniperl> or the full F<perl>. The C<use
VMS::Filespec> or C<require VMS::Filespec; import VMS::Filespec ...>
statement can be used to import the function names into the current
package, but they're always available if you use the fully qualified
name, whether or not you've mentioned the F<.pm> file in your script.
If you're running under another OS and have installed this package, it
behaves like a normal Perl extension (in fact, you're using Perl
substitutes to emulate the necessary VMS system calls).
Each of these routines accepts a file specification in either VMS or
Unix syntax, and returns the converted file specification, or C<undef>
if an error occurs. The conversions are, for the most part, simply
string manipulations; the routines do not check the details of syntax
(e.g. that only legal characters are used). There is one exception:
when running under VMS, conversions from VMS syntax use the $PARSE
service to expand specifications, so illegal syntax, or a relative
directory specification which extends above the tope of the current
directory path (e.g [---.foo] when in dev:[dir.sub]) will cause
errors. In general, any legal file specification will be converted
properly, but garbage input tends to produce garbage output.
Each of these routines is prototyped as taking a single scalar
argument, so you can use them as unary operators in complex
expressions (as long as you don't use the C<&> form of
subroutine call, which bypasses prototype checking).
The routines provided are:
=head2 rmsexpand
Uses the RMS $PARSE and $SEARCH services to expand the input
specification to its fully qualified form, except that a null type
or version is not added unless it was present in either the original
file specification or the default specification passed to C<rmsexpand>.
(If the file does not exist, the input specification is expanded as much
as possible.) If an error occurs, returns C<undef> and sets C<$!>
and C<$^E>.
=head2 vmsify
Converts a file specification to VMS syntax.
=head2 unixify
Converts a file specification to Unix syntax.
=head2 pathify
Converts a directory specification to a path - that is, a string you
can prepend to a file name to form a valid file specification. If the
input file specification uses VMS syntax, the returned path does, too;
likewise for Unix syntax (Unix paths are guaranteed to end with '/').
Note that this routine will insist that the input be a legal directory
file specification; the file type and version, if specified, must be
F<.DIR;1>. For compatibility with Unix usage, the type and version
may also be omitted.
=head2 fileify
Converts a directory specification to the file specification of the
directory file - that is, a string you can pass to functions like
C<stat> or C<rmdir> to manipulate the directory file. If the
input directory specification uses VMS syntax, the returned file
specification does, too; likewise for Unix syntax. As with
C<pathify>, the input file specification must have a type and
version of F<.DIR;1>, or the type and version must be omitted.
=head2 vmspath
Acts like C<pathify>, but insures the returned path uses VMS syntax.
=head2 unixpath
Acts like C<pathify>, but insures the returned path uses Unix syntax.
=head2 candelete
Determines whether you have delete access to a file. If you do, C<candelete>
returns true. If you don't, or its argument isn't a legal file specification,
C<candelete> returns FALSE. Unlike other file tests, the argument to
C<candelete> must be a file name (not a FileHandle), and, since it's an XSUB,
it's a list operator, so you need to be careful about parentheses. Both of
these restrictions may be removed in the future if the functionality of
C<candelete> becomes part of the Perl core.
=head1 REVISION
This document was last revised 22-Feb-1996, for Perl 5.002.
=cut
package VMS::Filespec;
require 5.002;
# If you want to use this package on a non-VMS system,
# uncomment the following line.
# use AutoLoader;
require Exporter;
@ISA = qw( Exporter );
@EXPORT = qw( &vmsify &unixify &pathify &fileify
&vmspath &unixpath &candelete &rmsexpand );
1;
__END__
# The autosplit routines here are provided for use by non-VMS systems
# They are not guaranteed to function identically to the XSUBs of the
# same name, since they do not have access to the RMS system routine
# sys$parse() (in particular, no real provision is made for handling
# of complex DECnet node specifications). However, these routines
# should be adequate for most purposes.
# A sort-of sys$parse() replacement
sub rmsexpand ($;$) {
my($fspec,$defaults) = @_;
if (!$fspec) { return undef }
my($node,$dev,$dir,$name,$type,$ver,$dnode,$ddev,$ddir,$dname,$dtype,$dver);
$fspec =~ s/:$//;
$defaults = [] unless $defaults;
$defaults = [ $defaults ] unless ref($defaults) && ref($defaults) eq 'ARRAY';
while ($fspec !~ m#[:>\]]# && $ENV{$fspec}) { $fspec = $ENV{$fspec} }
if ($fspec =~ /:/) {
my($dev,$devtrn,$base);
($dev,$base) = split(/:/,$fspec);
$devtrn = $dev;
while ($devtrn = $ENV{$devtrn}) {
if ($devtrn =~ /(.)([:>\]])$/) {
$dev .= ':', last if $1 eq '.';
$dev = $devtrn, last;
}
}
$fspec = $dev . $base;
}
($node,$dev,$dir,$name,$type,$ver) = $fspec =~
/([^:]*::)?([^:]*:)?([^>\]]*[>\]])?([^.;]*)(\.?[^.;]*)([.;]?\d*)/;
foreach ((@$defaults,$ENV{'DEFAULT'})) {
last if $node && $ver && $type && $dev && $dir && $name;
($dnode,$ddev,$ddir,$dname,$dtype,$dver) =
/([^:]*::)?([^:]*:)?([^>\]]*[>\]])?([^.;]*)(\.?[^.;]*)([.;]?\d*)/;
$node = $dnode if $dnode && !$node;
$dev = $ddev if $ddev && !$dev;
$dir = $ddir if $ddir && !$dir;
$name = $dname if $dname && !$name;
$type = $dtype if $dtype && !$type;
$ver = $dver if $dver && !$ver;
}
# do this the long way to keep -w happy
$fspec = '';
$fspec .= $node if $node;
$fspec .= $dev if $dev;
$fspec .= $dir if $dir;
$fspec .= $name if $name;
$fspec .= $type if $type;
$fspec .= $ver if $ver;
$fspec;
}
sub vmsify ($) {
my($fspec) = @_;
my($hasdev,$dev,$defdirs,$dir,$base,@dirs,@realdirs);
if ($fspec =~ m#^\.(\.?)/?$#) { return $1 ? '[-]' : '[]'; }
return $fspec if $fspec !~ m#/#;
($hasdev,$dir,$base) = $fspec =~ m#(/?)(.*)/(.*)#;
@dirs = split(m#/#,$dir);
if ($base eq '.') { $base = ''; }
elsif ($base eq '..') {
push @dirs,$base;
$base = '';
}
foreach (@dirs) {
next unless $_; # protect against // in input
next if $_ eq '.';
if ($_ eq '..') {
if (@realdirs && $realdirs[$#realdirs] ne '-') { pop @realdirs }
else { push @realdirs, '-' }
}
else { push @realdirs, $_; }
}
if ($hasdev) {
$dev = shift @realdirs;
@realdirs = ('000000') unless @realdirs;
$base = '' unless $base; # keep -w happy
$dev . ':[' . join('.',@realdirs) . "]$base";
}
else {
'[' . join('',map($_ eq '-' ? $_ : ".$_",@realdirs)) . "]$base";
}
}
sub unixify ($) {
my($fspec) = @_;
return $fspec if $fspec !~ m#[:>\]]#;
return '.' if ($fspec eq '[]' || $fspec eq '<>');
if ($fspec =~ m#^[<\[](\.|-+)(.*)# ) {
$fspec = ($1 eq '.' ? '' : "$1.") . $2;
my($dir,$base) = split(/[\]>]/,$fspec);
my(@dirs) = grep($_,split(m#\.#,$dir));
if ($dirs[0] =~ /^-/) {
my($steps) = shift @dirs;
for (1..length($steps)) { unshift @dirs, '..'; }
}
join('/',@dirs) . "/$base";
}
else {
$fspec = rmsexpand($fspec,'_N_O_T_:[_R_E_A_L_]');
$fspec =~ s/.*_N_O_T_:(?:\[_R_E_A_L_\])?//;
my($dev,$dir,$base) = $fspec =~ m#([^:<\[]*):?[<\[](.*)[>\]](.*)#;
my(@dirs) = split(m#\.#,$dir);
if ($dirs[0] && $dirs[0] =~ /^-/) {
my($steps) = shift @dirs;
for (1..length($steps)) { unshift @dirs, '..'; }
}
"/$dev/" . join('/',@dirs) . "/$base";
}
}
sub fileify ($) {
my($path) = @_;
if (!$path) { return undef }
if ($path =~ /(.+)\.([^:>\]]*)$/) {
$path = $1;
if ($2 !~ /^dir(?:;1)?$/i) { return undef }
}
if ($path !~ m#[/>\]]#) {
$path =~ s/:$//;
while ($ENV{$path}) {
($path = $ENV{$path}) =~ s/:$//;
last if $path =~ m#[/>\]]#;
}
}
if ($path =~ m#[>\]]#) {
my($dir,$sep,$base) = $path =~ /(.*)([>\]])(.*)/;
$sep =~ tr/<[/>]/;
if ($base) {
"$dir$sep$base.dir;1";
}
else {
if ($dir !~ /\./) { $dir =~ s/([<\[])/${1}000000./; }
$dir =~ s#\.(\w+)$#$sep$1#;
$dir =~ s/^.$sep//;
"$dir.dir;1";
}
}
else {
$path =~ s#/$##;
"$path.dir;1";
}
}
sub pathify ($) {
my($fspec) = @_;
if (!$fspec) { return undef }
if ($fspec =~ m#[/>\]]$#) { return $fspec; }
if ($fspec =~ m#(.+)\.([^/>\]]*)$# && $2 && $2 ne '.') {
$fspec = $1;
if ($2 !~ /^dir(?:;1)?$/i) { return undef }
}
if ($fspec !~ m#[/>\]]#) {
$fspec =~ s/:$//;
while ($ENV{$fspec}) {
if ($ENV{$fspec} =~ m#[>\]]$#) { return $ENV{$fspec} }
else { $fspec = $ENV{$fspec} =~ s/:$// }
}
}
if ($fspec !~ m#[>\]]#) { "$fspec/"; }
else {
if ($fspec =~ /([^>\]]+)([>\]])(.+)/) { "$1.$3$2"; }
else { $fspec; }
}
}
sub vmspath ($) {
pathify(vmsify($_[0]));
}
sub unixpath ($) {
pathify(unixify($_[0]));
}
sub candelete ($) {
my($fspec) = @_;
my($parent);
return '' unless -w $fspec;
$fspec =~ s#/$##;
if ($fspec =~ m#/#) {
($parent = $fspec) =~ s#/[^/]+$#;
return (-w $parent);
}
elsif ($parent = fileify($fspec)) { # fileify() here to expand lnms
$parent =~ s/[>\]][^>\]]+//;
return (-w fileify($parent));
}
else { return (-w '[-]'); }
}
|