SafeName.pm

filter: Statementstmt 88.5% 85 / 96 Branchbran 91.4% 75 / 82 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 85.7% 6 / 7 Podpod 100% 5 / 5 totaltotal 90.2% 176 / 195 CC 44 SCAR 28.4
CC
44
Cov
90%
safename
21.0
unsafename
19.0
safe_archive
12.0
CRAP 17.2
« Root.pm ↑ Summary
1  
2 #
3 # UNIX/VMS Filename "special character" and upper/lower case mapping
4 # as used by Multinet and UCX NFS
5 #
6 # Version: 0.021
7 # Author: C. Lane lane@duphy4.physics.drexel.edu
8 # Revised: 4 Jan 2001
9 #
10  
11 =head1 NAME
12  
13 VMS::FileUtils::SafeName -- convert special chars and case of filenames
14  
15 =head1 Synopsis
16  
17 use VMS::FileUtils::SafeName qw(:all) ;
18  
19 $vmsusablename = safename('unix:..name &tc',$do_all_dots);
20 $unixname = unsafename($vmsusablename);
21 $path = safepath('x.y/version:1.2.3',$do_all_dots_filename);
22 $unixpath = unsafepath($safepathoutput);
23 $archivename = safe_archive('HTML-Parser-1.07.tar.gz');
24  
25 =head1 DESCRIPTION
26  
27 This package provides conversion between Unix filenames and VMS filenames
28 where the Unix filenames may have characters that are illegal under VMS.
29  
30 Spaces, punctuation, control characters, etc. get mapped to a sequence of
31 the form '$6C'. Also the case of the original unix filename is preserved
32 using '$' to shift from lower to uppercase:
33  
34 Unix filename safename output
35 ------------- ---------------
36 abc.DEF.ghi -> $ABC.$DEF$5NGHI
37 ^ ^ ^^^ extra periods get converted
38 +----+-------change case
39  
40 The conversion may be reversed with unsafename
41  
42 The conversion provided here is the same as is contained in the
43 Multinet and UCX NFS software.
44  
45 Routines:
46  
47 =head2 safename
48  
49 Converts upper/lower case, punctuation etc. to chars valid for VMS filename.
50 The first '.' in the filename is left intact (unless the second parameter
51 to vmsify_name is true, in which case all .s are converted).
52  
53 Lowercase is taken as the default for input filenames.
54  
55 =head2 unsafename
56  
57 Converts filenames produced by safename back to their original form.
58  
59 =head2 safepath
60  
61 Converts path elements into VMS-safe form. The trailing element
62 (if the path doesn't end in a '/') can optionally have all periods
63 converted, or (default) all but the first period converted. Path
64 elements that correspond to directory names have all periods converted
65  
66 =head2 unsafepath
67  
68 Reverses the conversion done by safepath
69  
70 =head2 safe_archive
71  
72 Converts filename of the type typically used for CPAN archives into
73 a VMS-compatible format:
74  
75 HTML-Parser-1.07.tar.gz -> HTML-Parser-1_07.tar-gz
76  
77  
78 =head1 REVISION
79  
80 This document was last revised on 10 Mar 1998, for Perl 5.004.59
81  
82 =cut
83  
84 package VMS::FileUtils::SafeName;
85 1 require 5.002;
86 1 ▶use vars qw($VERSION @ISA @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
87 1 ▶use Exporter();
Subroutine BEGIN: called CC=1 SCAR=0.0
88  
89  
90 1 $VERSION = '0.02';
91 1 @ISA = qw( Exporter );
92 1 @EXPORT_OK = qw(safename unsafename safepath unsafepath safe_archive);
93 1 %EXPORT_TAGS = (
94 all => [qw(safename unsafename safepath unsafepath safe_archive)]
95 );
96  
97  
98 274 ▶sub safename ($;$) {
Subroutine safename: called CC=21 SCAR=30.4
Pod documented
99 274 my (@fn) = split(//,$_[0]);
100 274 my ($doalldots) = $_[1];
101 274 my ($out) = '';
102 274 my ($shift) = 0;
103 274 ▶ my ($dots) = defined($doalldots) && $doalldots;
Condition100%3/3
defined $doalldots && $doalldots
!ll&&!rl&&r
258412
MC/DC100%2/2
defined $doalldots && $doalldots
defined $doalldots $doalldots
Truth table100%3/3
A && B
A= defined $doalldotsB= $doalldots
ABresult
0X0
100
111
104  
105 274 foreach (@fn) {
106 1884 ▶ if (/[a-z]/) {
Branch: 15 branches
BranchTrueFalseTotal
if (/[a-z]/) { } 867 1017 1884
elsif (/[A-Z]/) { } 797 220 1017
elsif (/[0-9\-_]/) { } 12 208 220
elsif ($_ eq '$') { } 1 207 208
elsif ($_ eq '.') { } 17 190 207
elsif (ord $_ == 0) { } 1 189 190
elsif ($_ eq ' ') { } 1 188 189
elsif (ord $_ == 64) { } 1 187 188
elsif (ord $_ == 96) { } 1 186 187
elsif (ord $_ <= 26) { } 26 160 186
elsif (ord $_ <= 31) { } 5 155 160
elsif (ord $_ <= 58) { } 13 142 155
elsif (ord $_ <= 63) { } 5 137 142
elsif (ord $_ <= 94) { } 4 133 137
elsif (ord $_ <= 127) { } 5 128 133
107 867 ▶ $out .= ($shift==0 ? uc($_) : '$'.uc($_)) ;
Branch: 1 branch
BranchTrueFalseTotal
$shift == 0 ? : 866 1 867
108 867 $shift = 0;
109 } elsif (/[A-Z]/) {
110 797 ▶ $out .= ($shift==1 ? uc($_) : '$'.uc($_)) ;
Branch: 1 branch
BranchTrueFalseTotal
$shift == 1 ? : 540 257 797
111 797 $shift = 1;
112 } elsif (/[0-9\-_]/) {
113 12 $out .= $_;
114 } elsif ($_ eq '$') {
115 1 $out .= '$$';
116 } elsif ($_ eq '.') {
117 17 ▶ $out .= $dots ? '$5N': '.';
Branch: 1 branch
BranchTrueFalseTotal
$dots ? : 11 6 17
118 17 $dots++;
119 } elsif (ord($_) == 0) {
120 1 $out .= '$6A';
121 } elsif ($_ eq ' ') {
122 1 $out .= '$7A';
123 } elsif (ord($_) == 0x40) {
124 1 $out .= '$8A';
125 } elsif (ord($_) == 0x60) {
126 1 $out .= '$9A';
127 } elsif (ord($_) <= 0x1A) {
128 26 $out .= '$4'.chr(ord($_)-0x01+ord('A'));
129 } elsif (ord($_) <= 0x1F) {
130 5 $out .= '$6'.chr(ord($_)-0x1B+ord('B'));
131 } elsif (ord($_) <= 0x3A) {
132 13 $out .= '$5'.chr(ord($_)-0x21+ord('A'));
133 } elsif (ord($_) <= 0x3F) {
134 5 $out .= '$7'.chr(ord($_)-0x3B+ord('B'));
135 } elsif (ord($_) <= 0x5E) {
136 4 $out .= '$8'.chr(ord($_)-0x5B+ord('B'));
137 } elsif (ord($_) <= 0x7F) {
138 5 $out .= '$9'.chr(ord($_)-0x7B+ord('B'));
139 } else {
140 128 $out .= sprintf('$%03.3o',ord($_));
141 }
142 }
143 274 return($out);
144 }
145  
146  
147 257 ▶sub unsafename ($) {
Subroutine unsafename: called CC=19 SCAR=29.5
Pod documented
148 257 my ($in) = uc($_[0]);
149 257 my ($shift, $i, $mod, $out);
150  
151 257 $mod =1 ;
152 257 ▶ while ($mod) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod) 449 257 706
153 449 $mod = 0;
154 449 ▶ if ($in =~ /\$([0-7]{3,3})/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($in =~ /\$([0-7]{3,3})/) { } 128 321 449
elsif ($in =~ /\$([4-9])([A-Z])/) { } 64 257 321
155 128 $in = $`.chr(oct($1)).$';
156 128 $mod = 1;
157 } elsif ($in =~ /\$([4-9])([A-Z])/) {
158 64 $i = ord($2) - ord('A');
159 64 ▶ if ($1 == 4) {
Branch: 6 branches
BranchTrueFalseTotal
if ($1 == 4) { } 26 38 64
elsif ($1 == 5) { } 15 23 38
elsif ($1 == 6) { } 6 17 23
elsif ($1 == 7) { } 6 11 17
elsif ($1 == 8) { } 5 6 11
elsif ($1 == 9) { } 6 0 6
160 26 $in = $`.chr($i+0x01).$';
161 } elsif ($1 == 5) {
162 15 $in = $`.chr($i+0x21).$';
163 } elsif ($1 == 6) {
164 6 ▶ $in = $`.($i ? chr($i+0x1A) : chr(0)).$';
Branch: 1 branch
BranchTrueFalseTotal
$i ? : 5 1 6
165 } elsif ($1 == 7) {
166 6 ▶ $in = $`.($i ? chr($i+0x3A) : ' ').$';
Branch: 1 branch
BranchTrueFalseTotal
$i ? : 5 1 6
167 } elsif ($1 == 8) {
168 5 ▶ $in = $`.($i ? chr($i+0x5A) : chr(0x40)).$';
Branch: 1 branch
BranchTrueFalseTotal
$i ? : 4 1 5
169 } elsif ($1 == 9) {
170 6 ▶ $in = $`.($i ? chr($i+0x7A) : chr(0x60)).$';
Branch: 1 branch
BranchTrueFalseTotal
$i ? : 5 1 6
171 }
172 64 $mod = 1;
173 }
174 }
175  
176 257 $mod = 1;
177 257 $shift = 0;
178 257 $in = lc($in);
179 257 $out = '';
180 257 ▶ while ($mod) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod) 514 257 771
181 514 $mod = 0;
182 514 ▶ if ($in =~ /\$([A-Z\$])/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in =~ /\$([A-Z\$])/i) 257 257 514
183 257 ▶ if ($1 eq '$') {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq '$') { } 1 256 257
184 1 $out .= $`.'$';
185 1 $in = $';
186 1 $mod = 1;
187 } else {
188 256 $shift = !$shift;
189 256 ▶ $out .= $`.($shift? uc($1) : lc($1));
Branch: 1 branch
BranchTrueFalseTotal
$shift ? : 256 0 256
190 256 ▶ $in = $shift ? uc($') : lc($');
Branch: 1 branch
BranchTrueFalseTotal
$shift ? : 256 0 256
191 256 $mod = 1;
192 }
193 }
194 }
195 257 return ($out.$in);
196 }
197  
198 5 ▶sub safepath ($;$) {
Subroutine safepath: called CC=4 SCAR=13.9
Pod documented
199 5 my ($path, $dolast) = @_;
200 5 my (@e) = split('/',$path);
201 5 my ($j, $isadir);
202  
203 5 ▶ if (!defined($dolast)) {$dolast = 0;}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dolast) 3 2 5
204 5 $isadir = ($path =~ /\/$/);
205 5 $dolast ^= $isadir;
206  
207 5 ▶ for ($j = 0; $j < $#e ; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $#e) 9 5 14
208 9 $e[$j] = safename($e[$j],1);
209 }
210 5 $e[$#e] = safename($e[$#e],$dolast);
211 5 ▶ $path = join('/',@e).($isadir ? '/' : '');
Branch: 1 branch
BranchTrueFalseTotal
$isadir ? : 1 4 5
212 5 return $path;
213 }
214  
215 1 ▶sub unsafepath ($) {
Subroutine unsafepath: called CC=1 SCAR=0.0
Pod documented
216 1 return unsafename($_[0]);
217 }
218  
219  
220 0 ▶sub safe_archive ($) {
Subroutine safe_archive: not called CC=3 SCAR=24.8
Pod documented
221 0 my $name = shift;
222 0 my $suff = '';
223 0 ▶ if ($name =~ /\.([\w\-\$]+)\.(gz|Z|zip|gzip)\Z/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($name =~ /\.([\w\-\$]+)\.(gz|Z|zip|gzip)\Z/i) { } 0 0 0
elsif ($name =~ /\.([\w\-\$]+)\Z/i) { } 0 0 0
224 0 $suff = $1.'-'.$2;
225 0 $name = $`;
226 } elsif ($name =~ /\.([\w\-\$]+)\Z/i) {
227 0 $suff = $1;
228 0 $name = $`;
229 }
230 0 $name =~ s#\.$##;
231 0 $name =~ s#\.#_#g;
232 0 $name .= '.'.$suff;
233 0 return $name;
234 }
235  
236  
237  
238 1 1;
239  
240  
« Root.pm ↑ Summary