Commit | Line | Data |
270d1e39 |
1 | package File::Spec::VMS; |
2 | |
cbc7acb0 |
3 | use strict; |
4 | use vars qw(@ISA); |
5 | require File::Spec::Unix; |
270d1e39 |
6 | @ISA = qw(File::Spec::Unix); |
7 | |
cbc7acb0 |
8 | use File::Basename; |
9 | use VMS::Filespec; |
270d1e39 |
10 | |
11 | =head1 NAME |
12 | |
13 | File::Spec::VMS - methods for VMS file specs |
14 | |
15 | =head1 SYNOPSIS |
16 | |
cbc7acb0 |
17 | require File::Spec::VMS; # Done internally by File::Spec if needed |
270d1e39 |
18 | |
19 | =head1 DESCRIPTION |
20 | |
21 | See File::Spec::Unix for a documentation of the methods provided |
22 | there. This package overrides the implementation of these methods, not |
23 | the semantics. |
24 | |
377875b9 |
25 | =item eliminate_macros |
26 | |
27 | Expands MM[KS]/Make macros in a text string, using the contents of |
28 | identically named elements of C<%$self>, and returns the result |
29 | as a file specification in Unix syntax. |
30 | |
1f47e8e2 |
31 | =cut |
32 | |
33 | sub eliminate_macros { |
34 | my($self,$path) = @_; |
35 | return '' unless $path; |
36 | $self = {} unless ref $self; |
37 | my($npath) = unixify($path); |
38 | my($complex) = 0; |
39 | my($head,$macro,$tail); |
40 | |
41 | # perform m##g in scalar context so it acts as an iterator |
42 | while ($npath =~ m#(.*?)\$\((\S+?)\)(.*)#g) { |
43 | if ($self->{$2}) { |
44 | ($head,$macro,$tail) = ($1,$2,$3); |
45 | if (ref $self->{$macro}) { |
46 | if (ref $self->{$macro} eq 'ARRAY') { |
47 | $macro = join ' ', @{$self->{$macro}}; |
48 | } |
49 | else { |
50 | print "Note: can't expand macro \$($macro) containing ",ref($self->{$macro}), |
51 | "\n\t(using MMK-specific deferred substitutuon; MMS will break)\n"; |
52 | $macro = "\cB$macro\cB"; |
53 | $complex = 1; |
54 | } |
55 | } |
56 | else { ($macro = unixify($self->{$macro})) =~ s#/$##; } |
57 | $npath = "$head$macro$tail"; |
58 | } |
59 | } |
60 | if ($complex) { $npath =~ s#\cB(.*?)\cB#\${$1}#g; } |
61 | $npath; |
62 | } |
63 | |
377875b9 |
64 | =item fixpath |
65 | |
66 | Catchall routine to clean up problem MM[SK]/Make macros. Expands macros |
67 | in any directory specification, in order to avoid juxtaposing two |
68 | VMS-syntax directories when MM[SK] is run. Also expands expressions which |
69 | are all macro, so that we can tell how long the expansion is, and avoid |
70 | overrunning DCL's command buffer when MM[KS] is running. |
71 | |
72 | If optional second argument has a TRUE value, then the return string is |
73 | a VMS-syntax directory specification, if it is FALSE, the return string |
74 | is a VMS-syntax file specification, and if it is not specified, fixpath() |
75 | checks to see whether it matches the name of a directory in the current |
76 | default directory, and returns a directory or file specification accordingly. |
77 | |
78 | =cut |
79 | |
1f47e8e2 |
80 | sub fixpath { |
81 | my($self,$path,$force_path) = @_; |
82 | return '' unless $path; |
83 | $self = bless {} unless ref $self; |
84 | my($fixedpath,$prefix,$name); |
85 | |
86 | if ($path =~ m#^\$\([^\)]+\)$# || $path =~ m#[/:>\]]#) { |
87 | if ($force_path or $path =~ /(?:DIR\)|\])$/) { |
88 | $fixedpath = vmspath($self->eliminate_macros($path)); |
89 | } |
90 | else { |
91 | $fixedpath = vmsify($self->eliminate_macros($path)); |
92 | } |
93 | } |
94 | elsif ((($prefix,$name) = ($path =~ m#^\$\(([^\)]+)\)(.+)#)) && $self->{$prefix}) { |
95 | my($vmspre) = $self->eliminate_macros("\$($prefix)"); |
96 | # is it a dir or just a name? |
97 | $vmspre = ($vmspre =~ m|/| or $prefix =~ /DIR$/) ? vmspath($vmspre) : ''; |
98 | $fixedpath = ($vmspre ? $vmspre : $self->{$prefix}) . $name; |
99 | $fixedpath = vmspath($fixedpath) if $force_path; |
100 | } |
101 | else { |
102 | $fixedpath = $path; |
103 | $fixedpath = vmspath($fixedpath) if $force_path; |
104 | } |
105 | # No hints, so we try to guess |
106 | if (!defined($force_path) and $fixedpath !~ /[:>(.\]]/) { |
107 | $fixedpath = vmspath($fixedpath) if -d $fixedpath; |
108 | } |
109 | # Trim off root dirname if it's had other dirs inserted in front of it. |
110 | $fixedpath =~ s/\.000000([\]>])/$1/; |
111 | $fixedpath; |
112 | } |
113 | |
114 | |
270d1e39 |
115 | =head2 Methods always loaded |
116 | |
117 | =over |
118 | |
119 | =item catdir |
120 | |
121 | Concatenates a list of file specifications, and returns the result as a |
122 | VMS-syntax directory specification. |
123 | |
124 | =cut |
125 | |
126 | sub catdir { |
cbc7acb0 |
127 | my ($self,@dirs) = @_; |
128 | my $dir = pop @dirs; |
270d1e39 |
129 | @dirs = grep($_,@dirs); |
cbc7acb0 |
130 | my $rslt; |
270d1e39 |
131 | if (@dirs) { |
cbc7acb0 |
132 | my $path = (@dirs == 1 ? $dirs[0] : $self->catdir(@dirs)); |
133 | my ($spath,$sdir) = ($path,$dir); |
134 | $spath =~ s/.dir$//; $sdir =~ s/.dir$//; |
135 | $sdir = $self->eliminate_macros($sdir) unless $sdir =~ /^[\w\-]+$/; |
136 | $rslt = $self->fixpath($self->eliminate_macros($spath)."/$sdir",1); |
270d1e39 |
137 | } |
cbc7acb0 |
138 | else { |
139 | if ($dir =~ /^\$\([^\)]+\)$/) { $rslt = $dir; } |
140 | else { $rslt = vmspath($dir); } |
270d1e39 |
141 | } |
cbc7acb0 |
142 | return $rslt; |
270d1e39 |
143 | } |
144 | |
145 | =item catfile |
146 | |
147 | Concatenates a list of file specifications, and returns the result as a |
148 | VMS-syntax directory specification. |
149 | |
150 | =cut |
151 | |
152 | sub catfile { |
cbc7acb0 |
153 | my ($self,@files) = @_; |
154 | my $file = pop @files; |
270d1e39 |
155 | @files = grep($_,@files); |
cbc7acb0 |
156 | my $rslt; |
270d1e39 |
157 | if (@files) { |
cbc7acb0 |
158 | my $path = (@files == 1 ? $files[0] : $self->catdir(@files)); |
159 | my $spath = $path; |
160 | $spath =~ s/.dir$//; |
161 | if ($spath =~ /^[^\)\]\/:>]+\)$/ && basename($file) eq $file) { |
162 | $rslt = "$spath$file"; |
163 | } |
164 | else { |
165 | $rslt = $self->eliminate_macros($spath); |
166 | $rslt = vmsify($rslt.($rslt ? '/' : '').unixify($file)); |
167 | } |
270d1e39 |
168 | } |
169 | else { $rslt = vmsify($file); } |
cbc7acb0 |
170 | return $rslt; |
270d1e39 |
171 | } |
172 | |
173 | =item curdir (override) |
174 | |
cbc7acb0 |
175 | Returns a string representation of the current directory: '[]' |
270d1e39 |
176 | |
177 | =cut |
178 | |
179 | sub curdir { |
180 | return '[]'; |
181 | } |
182 | |
99804bbb |
183 | =item devnull (override) |
184 | |
cbc7acb0 |
185 | Returns a string representation of the null device: '_NLA0:' |
99804bbb |
186 | |
187 | =cut |
188 | |
189 | sub devnull { |
cbc7acb0 |
190 | return "_NLA0:"; |
99804bbb |
191 | } |
192 | |
270d1e39 |
193 | =item rootdir (override) |
194 | |
cbc7acb0 |
195 | Returns a string representation of the root directory: 'SYS$DISK:[000000]' |
270d1e39 |
196 | |
197 | =cut |
198 | |
199 | sub rootdir { |
cbc7acb0 |
200 | return 'SYS$DISK:[000000]'; |
201 | } |
202 | |
203 | =item tmpdir (override) |
204 | |
205 | Returns a string representation of the first writable directory |
206 | from the following list or '' if none are writable: |
207 | |
208 | /sys$scratch |
209 | $ENV{TMPDIR} |
210 | |
211 | =cut |
212 | |
213 | my $tmpdir; |
214 | sub tmpdir { |
215 | return $tmpdir if defined $tmpdir; |
216 | foreach ('/sys$scratch', $ENV{TMPDIR}) { |
217 | next unless defined && -d && -w _; |
218 | $tmpdir = $_; |
219 | last; |
220 | } |
221 | $tmpdir = '' unless defined $tmpdir; |
222 | return $tmpdir; |
270d1e39 |
223 | } |
224 | |
225 | =item updir (override) |
226 | |
cbc7acb0 |
227 | Returns a string representation of the parent directory: '[-]' |
270d1e39 |
228 | |
229 | =cut |
230 | |
231 | sub updir { |
232 | return '[-]'; |
233 | } |
234 | |
235 | =item path (override) |
236 | |
237 | Translate logical name DCL$PATH as a searchlist, rather than trying |
238 | to C<split> string value of C<$ENV{'PATH'}>. |
239 | |
240 | =cut |
241 | |
242 | sub path { |
cbc7acb0 |
243 | my (@dirs,$dir,$i); |
270d1e39 |
244 | while ($dir = $ENV{'DCL$PATH;' . $i++}) { push(@dirs,$dir); } |
cbc7acb0 |
245 | return @dirs; |
270d1e39 |
246 | } |
247 | |
248 | =item file_name_is_absolute (override) |
249 | |
250 | Checks for VMS directory spec as well as Unix separators. |
251 | |
252 | =cut |
253 | |
254 | sub file_name_is_absolute { |
cbc7acb0 |
255 | my ($self,$file) = @_; |
270d1e39 |
256 | # If it's a logical name, expand it. |
cbc7acb0 |
257 | $file = $ENV{$file} while $file =~ /^[\w\$\-]+$/ && $ENV{$file}; |
258 | return scalar($file =~ m!^/! || |
259 | $file =~ m![<\[][^.\-\]>]! || |
260 | $file =~ /:[^<\[]/); |
270d1e39 |
261 | } |
262 | |
cbc7acb0 |
263 | =back |
270d1e39 |
264 | |
cbc7acb0 |
265 | =head1 SEE ALSO |
266 | |
267 | L<File::Spec> |
268 | |
269 | =cut |
270 | |
271 | 1; |