lib/LaTeXML/Util/Pathname.pm

filter: Statementstmt 18.9% 52 / 274 Branchbran 6.3% 11 / 174 Conditioncond 6.1% 3 / 49 MC/DCmcdc 3.4% 1 / 29 Subroutinesub 27.7% 10 / 36 Podpod 81.4% 22 / 27 totaltotal 16.8% 99 / 589 CC 111 SCAR 55.5
CC
111
Cov
13%
candidate_pathnames
992.0
build_kpse_cache
210.0
pathname_kpsewhich
132.0
CRAP 257.0
« lib/LaTeXML/Post/CrossRef.pm ↑ Summary lib/LaTeXML/Core/Token.pm »
1 # /=====================================================================\ #
2 # | LaTeXML::Util::Pathname | #
3 # | Pathname Utilities for LaTeXML | #
4 # |=====================================================================| #
5 # | Part of LaTeXML: | #
6 # | Public domain software, produced as part of work done by the | #
7 # | United States Government & not subject to copyright in the US. | #
8 # |---------------------------------------------------------------------| #
9 # | Bruce Miller <bruce.miller@nist.gov> #_# | #
10 # | http://dlmf.nist.gov/LaTeXML/ (o o) | #
11 # \=========================================================ooo==U==ooo=/ #
12 #======================================================================
13 # Sanely combine features of File::Spec and File::Basename
14 # Somehow, both modules tend to bite me at random times.
15 # eg. sometimes Basename's fileparse doesn't extract extension.
16 # sometimes File::Spec seems to do too many filesystem checks (gets slow!)
17 # File::Spec->splitpath "may or may not ... trailing '/'" ... Huh?
18 #======================================================================
19 # My first instinct is that this should bless the pathnames,
20 # but strings as pathnames come so naturally in perl;
21 # But I may still do it...
22 #======================================================================
23 # Some portability changes for Windows, thanks to Ioan Sucan.
24 #======================================================================
25 # Packages in the LaTeXML::Util package set have no dependence on LaTeXML
26 # objects or context.
27 #======================================================================
28 package LaTeXML::Util::Pathname;
29 34 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 34 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 34 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 34 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 34 ▶use File::Which;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 34 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 34 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
36 34 our @EXPORT = qw( &pathname_find &pathname_findall &pathname_kpsewhich
37 &pathname_make &pathname_canonical
38 &pathname_split &pathname_directory &pathname_name &pathname_type
39 &pathname_timestamp
40 &pathname_concat
41 &pathname_relative &pathname_absolute &pathname_to_url
42 &pathname_is_absolute &pathname_is_contained
43 &pathname_is_url &pathname_is_literaldata
44 &pathname_is_raw
45 &pathname_protocol
46 &pathname_cwd &pathname_chdir &pathname_mkdir &pathname_copy
47 &pathname_installation);
48  
49 34 my $ISWINDOWS;
50  
51 34 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.2
52 34 $ISWINDOWS = $^O =~ /^(MSWin|NetWare|cygwin)/i;
53 34 ▶ require Win32::ShellQuote if $ISWINDOWS;
Branch: 1 branch
BranchTrueFalseTotal
if $ISWINDOWS 0 34 34
54 }
55  
56 # NOTE: For absolute pathnames, the directory component starts with
57 # whatever File::Spec considers to be the volume, or "/".
58 #======================================================================
59 # Ioan Sucan suggests switching this to '\\' for windows, but notes
60 # that it works as it is, so we'll leave it (for now).
61 ### my $SEP = '/'; # [CONSTANT]
62 # Some indicators that this is not sufficient? (calls to libraries/externals???)
63 # PRELIMINARY test, probably need to be even more careful
64 34 ▶my $SEP = ($ISWINDOWS ? '\\' : '/'); # [CONSTANT]
Branch: 1 branch
BranchTrueFalseTotal
$ISWINDOWS ? : 0 34 34
65 34 ▶my $KPATHSEP = ($ISWINDOWS ? ';' : ':'); # [CONSTANT]
Branch: 1 branch
BranchTrueFalseTotal
$ISWINDOWS ? : 0 34 34
66 34 my $LITERAL_RE = '(?:literal)(?=:)'; # [CONSTANT]
67 34 my $PROTOCOL_RE = '(?:https|http|ftp)(?=:)'; # [CONSTANT]
68  
69 #======================================================================
70 # pathname_make(dir=>dir, name=>name, type=>type);
71 # Returns a pathname. This will be an absolute path if
72 # dir (or the first, if dir is an array), is absolute.
73 0 ▶sub pathname_make {
Subroutine pathname_make: not called CC=9 SCAR=45.0
Pod documented
74 0 my (%pieces) = @_;
75 0 my $pathname = '';
76 0 ▶ if (my $dir = $pieces{dir}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $dir = $pieces{"dir"}) 0 0 0
77 0 ▶ my @dirs = (ref $dir eq 'ARRAY' ? @$dir : ($dir));
Branch: 1 branch
BranchTrueFalseTotal
ref $dir eq 'ARRAY' ? : 0 0 0
78 0 $pathname = shift(@dirs);
79 0 foreach my $d (@dirs) {
80 0 $pathname =~ s|\Q$SEP\E$||; $dir =~ s|^\Q$SEP\E||;
81 0 $pathname .= $SEP . $dir; } }
82 0 ▶ $pathname .= $SEP if $pathname && $pieces{name} && $pathname !~ m|\Q$SEP\E$|;
Branch: 1 branch
BranchTrueFalseTotal
if $pathname and $pieces{"name"} and not $pathname =~ /\Q$SEP\E$/ 0 0 0
Condition0%0/6
$pathname and $pieces{"name"} and not $pathname =~ /\Q$SEP\E$/
!ll&&!rl&&r
000
$pathname and $pieces{"name"}
!ll&&!rl&&r
000
MC/DC0%0/3
$pathname and $pieces{"name"} and not $pathname =~ /\Q$SEP\E$/
$pathname $pieces{"name"} not $pathname =~ /\Q$SEP\E$/
Truth table0%0/4
A and B and not(C)
A= $pathnameB= $pieces{"name"}C= not $pathname =~ /\Q$SEP\E$/
ABCresult
0XX0
10X0
1100
1111
83 0 ▶ $pathname .= $pieces{name} if $pieces{name};
Branch: 1 branch
BranchTrueFalseTotal
if $pieces{"name"} 0 0 0
84 0 ▶ $pathname .= '.' . $pieces{type} if $pieces{type};
Branch: 1 branch
BranchTrueFalseTotal
if $pieces{"type"} 0 0 0
85 0 return pathname_canonical($pathname); }
86  
87 # Split the pathname into components (dir,name,type).
88 # If pathname is absolute, dir starts with volume or '/'
89 0 ▶sub pathname_split {
Subroutine pathname_split: not called CC=7 SCAR=40.3
Pod documented
90 0 my ($pathname) = @_;
91 0 ▶ return unless defined $pathname;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pathname 0 0 0
92 0 $pathname = pathname_canonical($pathname);
93 0 my ($vol, $dir, $name) = File::Spec->splitpath($pathname);
94 # Hmm, for /, we get $dir = / but we want $vol='/' ?????
95 0 ▶ if ($vol) { $dir = $vol . $dir; }
Branch: 2 branches
BranchTrueFalseTotal
if ($vol) { } 0 0 0
elsif ('File::Spec'->file_name_is_absolute($pathname) and not 'File::Spec'->file_name_is_absolute($dir)) { } 0 0 0
96 0 ▶ elsif (File::Spec->file_name_is_absolute($pathname) && !File::Spec->file_name_is_absolute($dir)) { $dir = $SEP . $dir; }
Condition0%0/3
"File::Spec"->file_name_is_absolute($pathname) and not "File::Spec"->file_name_is_absolute($dir)
!ll&&!rl&&r
000
MC/DC0%0/2
"File::Spec"->file_name_is_absolute($pathname) and not "File::Spec"->file_name_is_absolute($dir)
"File::Spec"->file_name_is_absolute($pathname) not "File::Spec"->file_name_is_absolute($dir)
Truth table0%0/3
A and not(B)
A= "File::Spec"->file_name_is_absolute($pathname)B= not "File::Spec"->file_name_is_absolute($dir)
ABresult
0X0
100
111
97 # $dir shouldn't end with separator, unless it is root.
98 0 ▶ $dir =~ s/\Q$SEP\E$// unless $dir eq $SEP;
Branch: 1 branch
BranchTrueFalseTotal
unless $dir eq $SEP 0 0 0
99 0 my $type = '';
100 0 ▶ if ($name =~ s/\.([^\.]+)$//) { $type = $1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ s/\.([^\.]+)$//) 0 0 0
101 0 return ($dir, $name, $type); }
102  
103 34 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
104  
105 # This likely needs portability work!!! (particularly regarding urls, separators, ...)
106 # AND, care about symbolic links and collapsing ../ !!!
107 272 ▶sub pathname_canonical {
Subroutine pathname_canonical: called CC=8 SCAR=22.9
Pod documented
108 272 my ($pathname) = @_;
109 272 ▶ return unless defined $pathname;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pathname 0 272 272
110 272 ▶ if ($pathname =~ /^($LITERAL_RE)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pathname =~ /^($LITERAL_RE)/) 0 272 272
111 0 return $pathname; }
112 # Don't call pathname_is_absolute, etc, here, cause THEY call US!
113 272 ▶ confess "Undefined pathname!" unless defined $pathname;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pathname 0 272 272
114 # File::Spec->canonpath($pathname); }
115 272 $pathname =~ s|^~|$ENV{HOME}|;
116 # We CAN canonicalize urls, but we need to be careful about the // before host!
117 # OHHH, but we DON'T want \ for separator!
118 272 my $urlprefix = undef;
119 272 ▶ if ($pathname =~ s|^($PROTOCOL_RE//[^/]*)/|/|) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pathname =~ s[^($PROTOCOL_RE//[^/]*)/][/]) 0 272 272
120 0 $urlprefix = $1; }
121  
122 272 ▶ if ($pathname =~ m|//+/|) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pathname =~ m[//+/]) 0 272 272
123 0 Carp::cluck "Recursive pathname? : $pathname\n"; }
124 ## $pathname =~ s|//+|/|g;
125 272 $pathname =~ s|/\./|/|g;
126 # Collapse any foo/.. patterns, but not ../..
127 272 ▶ while ($pathname =~ s|/(?!\.\./)[^/]+/\.\.(/\|$)|$1|) { }
Branch: 1 branch
BranchTrueFalseTotal
if ($pathname =~ s[/(?!\.\./)[^/]+/\.\.(/|$)][$1]) 1 272 273
128 # Reduce ./foo to foo, but preserve ./
129 272 $pathname =~ s|^\./(.)|$1|;
130 272 ▶ return (defined $urlprefix ? $urlprefix . $pathname : $pathname); }
Branch: 1 branch
BranchTrueFalseTotal
defined $urlprefix ? : 0 272 272
131  
132 # Convenient extractors;
133 0 ▶sub pathname_directory {
Subroutine pathname_directory: not called CC=1 SCAR=6.9
Pod documented
134 0 my ($pathname) = @_;
135 0 my ($dir, $name, $type) = pathname_split($pathname);
136 0 return $dir; }
137  
138 0 ▶sub pathname_name {
Subroutine pathname_name: not called CC=1 SCAR=6.9
Pod documented
139 0 my ($pathname) = @_;
140 0 my ($dir, $name, $type) = pathname_split($pathname);
141 0 return $name; }
142  
143 0 ▶sub pathname_type {
Subroutine pathname_type: not called CC=1 SCAR=6.9
Pod documented
144 0 my ($pathname) = @_;
145 0 my ($dir, $name, $type) = pathname_split($pathname);
146 0 return $type; }
147  
148 # Note that this returns ONLY recognized protocols!
149 0 ▶sub pathname_protocol {
Subroutine pathname_protocol: not called CC=3 SCAR=24.8
Pod undocumented
150 0 my ($pathname) = @_;
151 0 ▶ return unless defined $pathname;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pathname 0 0 0
152 0 ▶ return ($pathname =~ /^($PROTOCOL_RE|$LITERAL_RE)/ ? $1 : 'file'); }
Branch: 1 branch
BranchTrueFalseTotal
$pathname =~ /^($PROTOCOL_RE|$LITERAL_RE)/ ? : 0 0 0
153  
154 #======================================================================
155 0 ▶sub pathname_concat {
Subroutine pathname_concat: not called CC=5 SCAR=34.0
Pod documented
156 0 my ($dir, $file) = @_;
157 0 ▶ return $file unless $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless $dir 0 0 0
158 0 ▶ return $dir if !defined $file || ($file eq '.');
Branch: 1 branch
BranchTrueFalseTotal
if not defined $file or $file eq "." 0 0 0
Condition0%0/3
not defined $file or $file eq "."
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $file or $file eq "."
not defined $file $file eq "."
Truth table0%0/3
not(A) or B
A= not defined $fileB= $file eq "."
ABresult
1X1
011
000
159 0 ▶ return pathname_canonical(File::Spec->catpath('', $dir || '', $file)); }
Condition0%0/2
$dir || ''
l!l
00
MC/DC0%0/1
$dir || ''
$dir
Truth table0%0/2
A
A= $dir
Aresult
11
00
160  
161 #======================================================================
162 # Is $pathname an absolute pathname ?
163 # pathname_is_absolute($pathname) => (0|1)
164 0 ▶sub pathname_is_absolute {
Subroutine pathname_is_absolute: not called CC=2 SCAR=17.9
Pod documented
165 0 my ($pathname) = @_;
166 0 ▶ return $pathname && File::Spec->file_name_is_absolute(pathname_canonical($pathname)); }
Condition0%0/3
$pathname && 'File::Spec'->file_name_is_absolute(pathname_canonical($pathname))
!ll&&!rl&&r
000
MC/DC0%0/2
$pathname && 'File::Spec'->file_name_is_absolute(pathname_canonical($pathname))
$pathname 'File::Spec'->file_name_is_absolute(pathname_canonical($pathname))
Truth table0%0/3
A && B
A= $pathnameB= 'File::Spec'->file_name_is_absolute(pathname_canonical($pathname))
ABresult
0X0
100
111
167  
168 0 ▶sub pathname_is_url {
Subroutine pathname_is_url: not called CC=3 SCAR=24.8
Pod documented
169 0 my ($pathname) = @_;
170 0 ▶ return $pathname && $pathname =~ /^($PROTOCOL_RE)/ && $1; } # Other protocols?
Condition0%0/6
$pathname && $pathname =~ /^($PROTOCOL_RE)/ && $1
!ll&&!rl&&r
000
$pathname and $pathname =~ /^($PROTOCOL_RE)/
!ll&&!rl&&r
000
MC/DC0%0/3
$pathname && $pathname =~ /^($PROTOCOL_RE)/ && $1
$pathname $pathname =~ /^($PROTOCOL_RE)/ $1
Truth table0%0/4
A and B && C
A= $pathnameB= $pathname =~ /^($PROTOCOL_RE)/C= $1
ABCresult
0XX0
10X0
1100
1111
171  
172 0 ▶sub pathname_is_literaldata {
Subroutine pathname_is_literaldata: not called CC=2 SCAR=17.9
Pod documented
173 0 my ($pathname) = @_;
174 0 ▶ if ($pathname =~ /^($LITERAL_RE)/) { return $1; } else { return; } }
Branch: 1 branch
BranchTrueFalseTotal
if ($pathname =~ /^($LITERAL_RE)/) { } 0 0 0
175  
176 # Check whether $pathname is contained in (ie. underneath) $base
177 # Returns the relative pathname if it is underneath; undef otherwise.
178 0 ▶sub pathname_is_contained {
Subroutine pathname_is_contained: not called CC=2 SCAR=17.9
Pod documented
179 0 my ($pathname, $base) = @_;
180 # after assuring that both paths are absolute,
181 # get $pathname relative to $base
182 0 my $rel = pathname_canonical(pathname_relative(pathname_absolute($pathname),
183 pathname_absolute($base)));
184 # If the relative pathname starts with "../" that it apparently is NOT underneath base!
185 0 ▶ return ($rel =~ m|^\.\.(?:/\|\Q$SEP\E)| ? undef : $rel); }
Branch: 1 branch
BranchTrueFalseTotal
$rel =~ m[^\.\.(?:/|\Q$SEP\E)] ? : 0 0 0
186  
187 # Check whether a pathname is a raw TeX source or definition
188 0 ▶sub pathname_is_raw {
Subroutine pathname_is_raw: not called CC=1 SCAR=6.9
Pod documented
189 0 my ($pathname) = @_;
190 0 return ($pathname =~ /\.(tex|pool|sty|cls|clo|cnf|cfg|ldf|def|dfu)$/); }
191  
192 # pathname_relative($pathname,$base) => $relativepathname
193 # If $pathname is an absolute, non-URL pathname,
194 # return the pathname relative to $base,
195 # else just return its canonical form.
196 # Actually, if it's a url and $base is also url, to SAME host! & protocol...
197 # we _could_ make relative...
198 0 ▶sub pathname_relative {
Subroutine pathname_relative: not called CC=5 SCAR=34.0
Pod documented
199 0 my ($pathname, $base) = @_;
200 0 ▶ return unless defined $pathname;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pathname 0 0 0
201 0 $pathname = pathname_canonical($pathname);
202 0 ▶ return ($base && pathname_is_absolute($pathname) && !pathname_is_url($pathname)
Branch: 1 branch
BranchTrueFalseTotal
$base && pathname_is_absolute($pathname) && !pathname_is_url($pathname) ? : 0 0 0
Condition0%0/6
$base and pathname_is_absolute($pathname) and not pathname_is_url($pathname)
!ll&&!rl&&r
000
$base and pathname_is_absolute($pathname)
!ll&&!rl&&r
000
MC/DC0%0/3
$base and pathname_is_absolute($pathname) and not pathname_is_url($pathname)
$base pathname_is_absolute($pathname) not pathname_is_url($pathname)
Truth table0%0/4
A and B and not(C)
A= $baseB= pathname_is_absolute($pathname)C= not pathname_is_url($pathname)
ABCresult
0XX0
10X0
1100
1111
203 ? File::Spec->abs2rel($pathname, pathname_canonical($base))
204 : $pathname); }
205  
206 0 ▶sub pathname_absolute {
Subroutine pathname_absolute: not called CC=5 SCAR=34.0
Pod documented
207 0 my ($pathname, $base) = @_;
208 0 ▶ return unless defined $pathname;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pathname 0 0 0
209 0 $pathname = pathname_canonical($pathname);
210 0 ▶ return (!pathname_is_absolute($pathname) && !pathname_is_url($pathname)
Branch: 2 branches
BranchTrueFalseTotal
!(pathname_is_absolute($pathname) || pathname_is_url($pathname)) ? : 0 0 0
$base ? : 0 0 0
Condition0%0/3
pathname_is_absolute($pathname) || pathname_is_url($pathname)
l!l&&r!l&&!r
000
MC/DC0%0/2
pathname_is_absolute($pathname) || pathname_is_url($pathname)
pathname_is_absolute($pathname) pathname_is_url($pathname)
Truth table0%0/3
A || B
A= pathname_is_absolute($pathname)B= pathname_is_url($pathname)
ABresult
1X1
011
000
211 ? File::Spec->rel2abs($pathname, ($base ? pathname_canonical($base) : pathname_cwd()))
212 : $pathname); }
213  
214 0 ▶sub pathname_to_url {
Subroutine pathname_to_url: not called CC=3 SCAR=24.8
Pod documented
215 0 my ($pathname) = @_;
216 0 ▶ return unless defined $pathname;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pathname 0 0 0
217 0 my $relative_pathname = pathname_relative($pathname);
218 0 ▶ if ($SEP ne '/') {
Branch: 1 branch
BranchTrueFalseTotal
if ($SEP ne "/") 0 0 0
219 0 $relative_pathname = join('/', split(/\Q$SEP\E/, $relative_pathname)); }
220 0 return $relative_pathname; }
221  
222 #======================================================================
223 # Actual file system operations.
224 0 ▶sub pathname_timestamp {
Subroutine pathname_timestamp: not called CC=2 SCAR=17.9
Pod documented
225 0 my ($pathname) = @_;
226 0 ▶ return -f $pathname ? (stat($pathname))[9] : 0; }
Branch: 1 branch
BranchTrueFalseTotal
-f $pathname ? : 0 0 0
227  
228 34 our $CWD = undef;
229 # DO NOT use pathname_cwd, unless you also use pathname_chdir to change dirs!!!
230 0 ▶sub pathname_cwd {
Subroutine pathname_cwd: not called CC=3 SCAR=24.8
Pod documented
231 0 ▶ if (!defined $CWD) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $LaTeXML::Util::Pathname::CWD) 0 0 0
232 0 ▶ if (my $cwd = cwd()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cwd = cwd()) { } 0 0 0
233 0 $CWD = pathname_canonical($cwd); }
234 else {
235 # Fatal not imported
236 0 die "INTERNAL: Could not determine current working directory (cwd)"
237 . "Perhaps a problem with Perl's locale settings?"; } }
238 0 return $CWD; }
239  
240 0 ▶sub pathname_chdir {
Subroutine pathname_chdir: not called CC=1 SCAR=6.9
Pod undocumented
241 0 my ($directory) = @_;
242 0 chdir($directory);
243 0 pathname_cwd(); # RE-cache $CWD!
244 0 return; }
245  
246 0 ▶sub pathname_mkdir {
Subroutine pathname_mkdir: not called CC=5 SCAR=34.0
Pod documented
247 0 my ($directory) = @_;
248 0 ▶ return unless $directory;
Branch: 1 branch
BranchTrueFalseTotal
unless $directory 0 0 0
249 0 $directory = pathname_canonical($directory);
250 0 my ($volume, $dirs, $last) = File::Spec->splitpath($directory);
251 0 my (@dirs) = (File::Spec->splitdir($dirs), $last);
252 0 ▶ for (my $i = 0 ; $i <= $#dirs ; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#dirs) 0 0 0
253 0 my $dir = File::Spec->catpath($volume, File::Spec->catdir(@dirs[0 .. $i]), '');
254 0 ▶ if (!-d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 0 0 0
255 0 ▶ mkdir($dir) or return; } }
Branch: 1 branch
BranchTrueFalseTotal
mkdir $dir or return 0 0 0
256 0 return $directory; }
257  
258 # copy a file, preserving attributes, if possible.
259 # Why doesn't File::Copy preserve attributes on Unix !?!?!?
260 0 ▶sub pathname_copy {
Subroutine pathname_copy: not called CC=6 SCAR=37.4
Pod documented
261 0 my ($source, $destination) = @_;
262 # If it _needs_ to be copied:
263 0 $source = pathname_canonical($source);
264 0 $destination = pathname_canonical($destination);
265 0 ▶ if ((!-f $destination) || (pathname_timestamp($source) > pathname_timestamp($destination))) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -f $destination or pathname_timestamp($source) > pathname_timestamp($destination)) 0 0 0
Condition0%0/3
not -f $destination or pathname_timestamp($source) > pathname_timestamp($destination)
l!l&&r!l&&!r
000
MC/DC0%0/2
not -f $destination or pathname_timestamp($source) > pathname_timestamp($destination)
not -f $destination pathname_timestamp($source) > pathname_timestamp($destination)
Truth table0%0/3
not(A) or B
A= not -f $destinationB= pathname_timestamp($source) > pathname_timestamp($destination)
ABresult
1X1
011
000
266 0 ▶ if (my $destdir = pathname_directory($destination)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $destdir = pathname_directory($destination)) 0 0 0
267 0 ▶ pathname_mkdir($destdir) or return; }
Branch: 1 branch
BranchTrueFalseTotal
pathname_mkdir($destdir) or return 0 0 0
268 ### if($^O =~ /^(MSWin32|NetWare)$/){ # Windows
269 ### # According to Ioan, this should work:
270 ### system("xcopy /P $source $destination")==0 or return; }
271 ### else { # Unix
272 ### system("cp --preserve=timestamps $source $destination")==0 or return; }
273 # Hopefully this portably copies, preserving timestamp.
274 0 ▶ copy($source, $destination) or return;
Branch: 1 branch
BranchTrueFalseTotal
copy($source, $destination) or return 0 0 0
275 0 my ($atime, $mtime) = (stat($source))[8, 9];
276 0 utime $atime, $mtime, $destination; # And set the modification time
277 }
278 0 return $destination; }
279  
280 #======================================================================
281 # pathname_find($pathname, paths=>[...], types=>[...]) => $absolute_pathname;
282 # Find a file corresponding to $pathname returning the absolute,
283 # completed pathname if found, else undef
284 # * If $pathname is a not an absolute pathname
285 # (although it may still have directory components)
286 # then if search $paths are given, search for it relative to
287 # each of the directories in $paths,
288 # else search for it relative to the current working directory.
289 # * If types is given, then search (in each searched directory)
290 # for the first file with the given extension.
291 # The extension "" (empty string) means to search for the exact name.
292 # * If types is not given, search for the exact named file
293 # without additional extension.
294 # * If installation_subdir is given, look in that subdirectory of where LaTeXML
295 # was installed, by appending it to the paths.
296  
297 # This is presumably daemon safe...
298 272 ▶my @INSTALLDIRS = grep { (-f "$_.pm") && (-d $_) }
Condition66%2/3
-f "$_.pm" && -d $_
!ll&&!rl&&r
1700102
MC/DC50%1/2
-f "$_.pm" && -d $_
-f "$_.pm" -d $_
Truth table66%2/3
A && B
A= -f "$_.pm"B= -d $_
ABresult
0X0
100
111
299 34 map { pathname_canonical($_ . $SEP . 'LaTeXML') } @INC; # [CONSTANT]
300  
301 0 ▶sub pathname_installation {
Subroutine pathname_installation: not called CC=1 SCAR=6.9
Pod undocumented
302 0 return $INSTALLDIRS[0]; }
303  
304 0 ▶sub pathname_find {
Subroutine pathname_find: not called CC=2 SCAR=17.9
Pod documented
305 0 my ($pathname, %options) = @_;
306 0 ▶ return unless $pathname;
Branch: 1 branch
BranchTrueFalseTotal
unless $pathname 0 0 0
307 0 $options{findfirst} = 1;
308 0 my @paths = candidate_pathnames($pathname, %options);
309 0 return $paths[0]; }
310  
311 0 ▶sub pathname_findall {
Subroutine pathname_findall: not called CC=2 SCAR=17.9
Pod documented
312 0 my ($pathname, %options) = @_;
313 0 ▶ return unless $pathname;
Branch: 1 branch
BranchTrueFalseTotal
unless $pathname 0 0 0
314 0 $options{findfirst} = undef;
315 0 return candidate_pathnames($pathname, %options); }
316  
317 # It's presumably cheap to concatinate all the pathnames,
318 # relative to the cost of testing for files,
319 # and this simplifies overall.
320 0 ▶sub candidate_pathnames {
Subroutine candidate_pathnames: not called CC=31 SCAR=69.0
Pod undocumented
321 0 my ($pathname, %options) = @_;
322 0 my @dirs = ();
323 0 ▶ $pathname = pathname_canonical($pathname) unless $pathname eq '*';
Branch: 1 branch
BranchTrueFalseTotal
unless $pathname eq "*" 0 0 0
324 0 ▶ my ($pathdir, $name, $type) = ($pathname eq '*' ? (undef, '*', undef) : pathname_split($pathname));
Branch: 1 branch
BranchTrueFalseTotal
$pathname eq '*' ? : 0 0 0
325 0 ▶ $name .= '.' . $type if (defined $type) && ($type ne '');
Branch: 1 branch
BranchTrueFalseTotal
if defined $type and $type ne "" 0 0 0
Condition0%0/3
defined $type and $type ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $type and $type ne ""
defined $type $type ne ""
Truth table0%0/3
A and B
A= defined $typeB= $type ne ""
ABresult
0X0
100
111
326 # generate the set of search paths we'll use.
327 0 ▶ if (pathname_is_absolute($pathname)) {
Branch: 1 branch
BranchTrueFalseTotal
if (pathname_is_absolute($pathname)) { } 0 0 0
328 0 push(@dirs, $pathdir); }
329 else {
330 0 my $cwd = pathname_cwd();
331 0 ▶ if ($options{paths}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"paths"}) 0 0 0
332 0 foreach my $p (@{ $options{paths} }) {
333 # Complete the search paths by prepending current dir to relative paths,
334 0 ▶ my $pp = pathname_concat((pathname_is_absolute($p) ? pathname_canonical($p) : pathname_concat($cwd, $p)),
Branch: 1 branch
BranchTrueFalseTotal
pathname_is_absolute($p) ? : 0 0 0
335 $pathdir);
336 0 ▶ push(@dirs, $pp) unless grep { $pp eq $_ } @dirs; } } # but only include each dir ONCE
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$pp eq $_;} @dirs 0 0 0
337 0 ▶ push(@dirs, pathname_concat($cwd, $pathdir)) unless @dirs; # At least have the current directory!
Branch: 1 branch
BranchTrueFalseTotal
unless @dirs 0 0 0
338 # And, if installation dir specified, append it.
339 0 ▶ if (my $subdir = $options{installation_subdir}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $subdir = $options{"installation_subdir"}) 0 0 0
340 0 push(@dirs, map { pathname_concat($_, $subdir) } @INSTALLDIRS); } }
341  
342 # extract the desired extensions.
343 0 my @exts = ();
344 0 ▶ if ($options{type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"type"}) 0 0 0
345 0 push(@exts, '.' . $options{type}); }
346 0 ▶ if ($options{types}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"types"}) 0 0 0
347 0 foreach my $ext (@{ $options{types} }) {
348 0 ▶ if ($ext eq '') { push(@exts, ''); }
Branch: 2 branches
BranchTrueFalseTotal
if ($ext eq '') { } 0 0 0
elsif ($ext eq '*') { } 0 0 0
349 elsif ($ext eq '*') {
350 0 push(@exts, '.*', ''); }
351 else {
352 0 ▶ if ($pathname =~ /\.\Q$ext\E$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pathname =~ /\.\Q$ext\E$/i) 0 0 0
353 0 push(@exts, ''); }
354 0 push(@exts, '.' . $ext); } } }
355 0 ▶ push(@exts, '') unless @exts;
Branch: 1 branch
BranchTrueFalseTotal
unless @exts 0 0 0
356  
357 0 my @paths = ();
358 0 my @nocase_paths = ();
359 # Precompute the intended regexes to apply
360 0 my @regexes = ();
361 0 foreach my $ext (@exts) {
362 0 ▶ if ($name eq '*') { # Unfortunately, we've got to test the file system NOW...
Branch: 2 branches
BranchTrueFalseTotal
if ($name eq '*') { } 0 0 0
elsif ($ext eq '.*') { } 0 0 0
363 0 ▶ if ($ext eq '.*') { # everything, except hidden files
Branch: 1 branch
BranchTrueFalseTotal
if ($ext eq '.*') { } 0 0 0
364 0 push(@regexes, [qr/^[^.]/, qr/^[^.]/]); }
365 else {
366 0 push(@regexes, [qr/\Q$ext\E$/i,
367 qr/\Q$ext\E$/]); } }
368 elsif ($ext eq '.*') {
369 0 push(@regexes, [qr/^\Q$name\E\.\w+$/i,
370 qr/^\Q$name\E\.\w+$/]); }
371 else {
372 0 push(@regexes, [qr/^\Q$name\E\Q$ext\E$/i,
373 qr/^\Q$name\E\Q$ext\E$/]); } }
374 # Now, combine; precedence to leading directories.
375 0 foreach my $dir (@dirs) {
376 0 ▶ opendir(DIR, $dir) or next;
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, $dir or next 0 0 0
377 0 my @dir_files = readdir(DIR);
378 0 closedir(DIR);
379 0 for my $local_file (@dir_files) {
380 0 for my $regex_pair (@regexes) {
381 0 my ($i_regex, $regex) = @$regex_pair;
382 0 ▶ if ($local_file =~ m/$i_regex/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($local_file =~ /$i_regex/) 0 0 0
383 0 my $full_file = pathname_concat($dir, $local_file);
384 0 push(@nocase_paths, $full_file);
385 0 ▶ if ($local_file =~ m/$regex/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($local_file =~ /$regex/) 0 0 0
386 # if we are only interested in the first match, return it:
387 0 ▶ return ($full_file) if $options{findfirst};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"findfirst"} 0 0 0
388 0 push(@paths, $full_file); } } } } }
389 # Fallback: if no strict matches were found, return any existing case-insensitive matches
390 # Defer the -f check until we are sure we need it, to keep the usual cases fast.
391 0 ▶ return @paths ? @paths : @nocase_paths; }
Branch: 1 branch
BranchTrueFalseTotal
@paths ? : 0 0 0
392  
393 #======================================================================
394 34 ▶our $kpsewhich = which($ENV{LATEXML_KPSEWHICH} || 'kpsewhich');
Condition50%1/2
$ENV{'LATEXML_KPSEWHICH'} || 'kpsewhich'
l!l
034
MC/DC0%0/1
$ENV{'LATEXML_KPSEWHICH'} || 'kpsewhich'
$ENV{'LATEXML_KPSEWHICH'}
Truth table50%1/2
A
A= $ENV{'LATEXML_KPSEWHICH'}
Aresult
11
00
395 34 our $kpse_cache = undef;
396 34 our $kpse_toolchain = "";
397  
398 0 ▶sub pathname_kpsewhich {
Subroutine pathname_kpsewhich: not called CC=11 SCAR=48.8
Pod documented
399 0 my (@candidates) = @_;
400 # ($kpsewhich,@candidates) MUST NOT be empty to guarantee that Perl runs $kpsewhich directly
401 # rather than through a shell or cmd.exe
402 0 ▶ return unless $kpsewhich && @candidates;
Branch: 1 branch
BranchTrueFalseTotal
unless $LaTeXML::Util::Pathname::kpsewhich and @candidates 0 0 0
Condition0%0/3
$LaTeXML::Util::Pathname::kpsewhich and @candidates
!ll&&!rl&&r
000
MC/DC0%0/2
$LaTeXML::Util::Pathname::kpsewhich and @candidates
$LaTeXML::Util::Pathname::kpsewhich @candidates
Truth table0%0/3
A and B
A= $LaTeXML::Util::Pathname::kpsewhichB= @candidates
ABresult
0X0
100
111
403 0 ▶ build_kpse_cache() unless $kpse_cache;
Branch: 1 branch
BranchTrueFalseTotal
unless $LaTeXML::Util::Pathname::kpse_cache 0 0 0
404 0 foreach my $file (@candidates) {
405 0 ▶ if (my $result = $$kpse_cache{$file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $result = $LaTeXML::Util::Pathname::kpse_cache->{$file}) 0 0 0
406 0 return $result; } }
407 # If we've failed to read the cache, try directly calling kpsewhich
408 # For multiple calls, this is slower in general. But MiKTeX, eg., doesn't use texmf ls-R files!
409 0 ▶ if ($kpse_toolchain) {
Branch: 1 branch
BranchTrueFalseTotal
if ($LaTeXML::Util::Pathname::kpse_toolchain) 0 0 0
410 0 push(@candidates, $kpse_toolchain); }
411 0 ▶ if ($kpsewhich && open(my $resfh, '-|',
Branch: 2 branches
BranchTrueFalseTotal
if ($LaTeXML::Util::Pathname::kpsewhich and open my $resfh, "-|", $ISWINDOWS ? Win32::ShellQuote::quote_system_list($LaTeXML::Util::Pathname::kpsewhich, @candidates) : ($LaTeXML::Util::Pathname::kpsewhich, @candidates)) 0 0 0
$ISWINDOWS ? : 0 0 0
Condition0%0/3
$LaTeXML::Util::Pathname::kpsewhich and open my $resfh, "-|", $ISWINDOWS ? Win32::ShellQuote::quote_system_list($LaTeXML::Util::Pathname::kpsewhich, @candidates) : ($LaTeXML::Util::Pathname::kpsewhich, @candidates)
!ll&&!rl&&r
000
MC/DC0%0/2
$LaTeXML::Util::Pathname::kpsewhich and open my $resfh, "-|", $ISWINDOWS ? Win32::ShellQuote::quote_system_list($LaTeXML::Util::Pathname::kpsewhich, @candidates) : ($LaTeXML::Util::Pathname::kpsewhich, @candidates)
$LaTeXML::Util::Pathname::kpsewhich open my $resfh, "-|", $ISWINDOWS ? Win32::ShellQuote::quote_system_list($LaTeXML::Util::Pathname::kpsewhich, @candidates) : ($LaTeXML::Util::Pathname::kpsewhich, @candidates)
Truth table0%0/3
A and B
A= $LaTeXML::Util::Pathname::kpsewhichB= open my $resfh, "-|", $ISWINDOWS ? Win32::ShellQuote::quote_system_list($LaTeXML::Util::Pathname::kpsewhich, @candidates) : ($LaTeXML::Util::Pathname::kpsewhich, @candidates)
ABresult
0X0
100
111
412 # on Windows, ($kpsewhich, @candidates) is joined into a single string, which most binaries
413 # parse with CommandLineToArgvW, so the arguments must be escaped accordingly
414 $ISWINDOWS ? Win32::ShellQuote::quote_system_list($kpsewhich, @candidates) : ($kpsewhich, @candidates))) {
415 0 my $result = <$resfh>; # we only need the first line
416 0 { local $/; <$resfh>; } # discard the rest of the output
417 0 close($resfh); # ignore exit status (only one of @candidates exists, usually)
418 0 ▶ if ($result) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result) 0 0 0
419 0 chomp $result;
420 0 return $result; } }
421 0 return; }
422  
423 0 ▶sub build_kpse_cache {
Subroutine build_kpse_cache: not called CC=14 SCAR=53.5
Pod undocumented
424 0 $kpse_cache = {}; # At least we've tried.
425 0 ▶ return unless $kpsewhich;
Branch: 1 branch
BranchTrueFalseTotal
unless $LaTeXML::Util::Pathname::kpsewhich 0 0 0
426 # This finds ALL the directories looked for for any purposes, including docs, fonts, etc
427 0 ▶ $kpse_toolchain = "--miktex-admin" if ($ENV{"LATEXML_KPSEWHICH_MIKTEX_ADMIN"});
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"LATEXML_KPSEWHICH_MIKTEX_ADMIN"} 0 0 0
428 0 my $texmf = `"$kpsewhich" --expand-var \'\\\$TEXMF\' $kpse_toolchain`; chomp($texmf);
429 # These are directories which contain the tex related files we're interested in.
430 # (but they're typically below where the ls-R indexes are!)
431 0 my $texpaths = `"$kpsewhich" --show-path tex $kpse_toolchain`; chomp($texpaths);
432 0 my @filters = ();
433 0 foreach my $path (split(/$KPATHSEP/, $texpaths)) {
434 0 $path =~ s/^!!//; $path =~ s|//+$|/|;
435 0 ▶ push(@filters, $path) if -d $path; }
Branch: 1 branch
BranchTrueFalseTotal
if -d $path 0 0 0
436 0 $texmf =~ s/^["']//; $texmf =~ s/["']$//;
437 0 $texmf =~ s/^\s*\\\{(.+?)}\s*$/$1/s;
438 0 $texmf =~ s/\{\}//g;
439 0 my @dirs = split(/,/, $texmf);
440 0 foreach my $dir (@dirs) {
441 0 $dir =~ s/^!!//;
442 # Presumably if no ls-R, we can ignore the directory?
443 0 ▶ if (-f "$dir/ls-R") {
Branch: 1 branch
BranchTrueFalseTotal
if (-f "$dir/ls-R") 0 0 0
444 0 my $LSR;
445 my $subdir;
446 0 my $skip = 0; # whether to skip entries in the current subdirectory.
447 0 ▶ open($LSR, '<', "$dir/ls-R") or die "Cannot read $dir/ls-R: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $LSR, "<", "$dir/ls-R" or die "Cannot read $dir/ls-R: $!" 0 0 0
448 0 ▶ while (<$LSR>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $LSR)) 0 0 0
449 0 chop;
450 0 ▶ next unless $_;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ 0 0 0
451 0 ▶ if (/^%/) { }
Branch: 3 branches
BranchTrueFalseTotal
if (/^%/) { } 0 0 0
elsif (/^(.*?):$/) { } 0 0 0
elsif (not $skip) { } 0 0 0
452 elsif (/^(.*?):$/) { # Move to a new subdirectory
453 0 $subdir = $1;
454 0 $subdir =~ s|^\./||; # remove prefix
455 0 my $d = $dir . '/' . $subdir; # Hopefully OS safe, for comparison?
456 0 $skip = !grep { $d =~ /^\Q$_\E/ } @filters; # check if one of the TeX paths
457 0 ▶ $skip |= ($d =~ m|-dev[$//]|) unless $LaTeXML::DEBUG{'latex-dev'};
Branch: 1 branch
BranchTrueFalseTotal
unless $LaTeXML::DEBUG{"latex-dev"} 0 0 0
458 }
459 elsif (!$skip) {
460 # Is it safe to use '/' here?
461 0 my $sep = '/';
462 0 $$kpse_cache{$_} = join($sep, $dir, $subdir, $_); } }
463 0 close($LSR); } }
464 0 return; }
465  
466 #======================================================================
467 34 1;
468  
469 __END__
« lib/LaTeXML/Post/CrossRef.pm ↑ Summary lib/LaTeXML/Core/Token.pm »