lib/Panotools/Makefile/Utils.pm

filter: Statementstmt 100% 41 / 41 Branchbran 100% 14 / 14 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 4 totaltotal 94.4% 68 / 72 CC 9 SCAR 10.6
CC
9
Cov
100%
quoteshell
5.0
platform
3.0
quotetarget
2.0
CRAP 2.9
« blib/lib/Panotools/Makefile/Comment.pm ↑ Summary lib/Panotools/Script/Line.pm »
1 package Panotools::Makefile::Utils;
2  
3 =head1 NAME
4  
5 Panotools::Makefile::Utils - Makefile syntax
6  
7 =head1 SYNOPSIS
8  
9 Simple interface for generating Makefile syntax
10  
11 =head1 DESCRIPTION
12  
13 Writing Makefiles directly from perl scripts with print and "\t" etc... is
14 prone to error, this library provides a simple perl interface for assembling
15 Makefile rules.
16  
17 See L<Panotools::Makefile::Rule> and L<Panotools::Makefile::Variable> for
18 object classes that you can use to contruct makefiles.
19  
20 =cut
21  
22 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 5 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 5 ▶use vars qw /@ISA @EXPORT_OK/;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 5 @ISA = qw /Exporter/;
28 5 @EXPORT_OK = qw /platform quotetarget quoteprerequisite quoteshell/;
29  
30 5 our $PLATFORM;
31  
32 =head1 USAGE
33  
34 Access the current platform name (MSWin32, linux, etc...):
35  
36 print platform;
37  
38 Define a different platform and access the new name:
39  
40 platform ('MSWin32');
41 print platform;
42  
43 Reset platform to default:
44  
45 platform (undef);
46  
47 =cut
48  
49 sub platform
50 595 ▶{
Subroutine platform: called CC=3 SCAR=11.0
Pod undocumented
51 595 ▶ $PLATFORM = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 13 582 595
52 595 ▶ return $PLATFORM if defined $PLATFORM;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Panotools::Makefile::Utils::PLATFORM 236 359 595
53 359 return $^O;
54 }
55  
56 =pod
57  
58 Take a text string (typically a single filename or path) and quote/escape
59 spaces and special characters to make it suitable for use as a Makefile
60 'target' or 'prerequisite':
61  
62 $escaped_target = quotetarget ('My Filename.txt');
63 $escaped_prerequisite = quoteprerequisite ('My Filename.txt');
64  
65 Note that the =;:% characters are not usable as filenames, they may be used as
66 control characters in a target or prerequisite. An exception is the : in
67 Windows paths such as C:\WINDOWS which is understood by gnu make.
68  
69 * and ? are wildcards and will be expanded. You may find that it is
70 possible to use these as actual characters in filenames, but this assumption
71 will lead to subtle errors.
72  
73 $ can be used in a filename, but when used with brackets, ${FOO} or $(BAR),
74 will be substituted as a make variable.
75  
76 Targets starting with . are special make targets and not usable as filenames,
77 the workaround is to supply a full path instead of a relative path. i.e:
78 /foo/bar/.hugin rather than .hugin
79  
80 Additionally the ?<>*|"^\ characters are not portable across filesystems (e.g.
81 USB sticks, CDs, Windows) and should be avoided in filenames.
82  
83 =cut
84  
85 sub quotetarget
86 116 ▶{
Subroutine quotetarget: called CC=2 SCAR=6.9
Pod undocumented
87 116 my $string = shift;
88 # Transform all C:\foo\bar paths to C:/foo/bar
89 116 ▶ $string =~ s/\\/\//g if (platform =~ /^(MSWin|dos)/);
Branch: 1 branch
BranchTrueFalseTotal
if platform() =~ /^(MSWin|dos)/ 22 94 116
90 116 $string =~ s/([ #|\\])/\\$1/g;
91 # escape $ as $$ unless part of a $(VARIABLE)
92 116 $string =~ s/\$([^({]|$)/\$\$$1/g;
93 116 return $string;
94 }
95  
96 sub quoteprerequisite
97 76 ▶{
Subroutine quoteprerequisite: called CC=2 SCAR=6.9
Pod undocumented
98 76 my $string = shift;
99 # Transform all C:\foo\bar paths to C:/foo/bar
100 76 ▶ $string =~ s/\\/\//g if (platform =~ /^(MSWin|dos)/);
Branch: 1 branch
BranchTrueFalseTotal
if platform() =~ /^(MSWin|dos)/ 2 74 76
101 76 $string =~ s/([ #|\\])/\\$1/g;
102 # escape $ as $$ unless part of a $(VARIABLE)
103 76 $string =~ s/\$([^({]|$)/\$\$$1/g;
104 76 return $string;
105 }
106  
107 =pod
108  
109 Take a text string, typically a command-line token, and quote/escape spaces and
110 special characters to make it suitable for use in a Makefile command:
111  
112 $escaped_token = quoteshell ('Hello World');
113  
114 =cut
115  
116 sub quoteshell
117 382 ▶{
Subroutine quoteshell: called CC=5 SCAR=16.1
Pod undocumented
118 382 my $string = shift;
119 382 ▶ if (platform =~ /^(MSWin|dos)/)
Branch: 1 branch
BranchTrueFalseTotal
if (platform() =~ /^(MSWin|dos)/) { } 16 366 382
120 {
121 # Transform all C:\foo\bar paths to C:/foo/bar
122 # Not all tokens are file paths, so \:-) will become /:-)
123 16 $string =~ s/\\/\//g;
124 # hash is parsed by make as a comment, backslash escape
125 16 $string =~ s/#/\\#/g;
126 # caret escape " since we are using it for quoting
127 16 $string =~ s/"/^"/g;
128 # escape $ as $$ unless part of a $(VARIABLE)
129 16 $string =~ s/\$([^({]|$)/\$\$$1/g;
130 # ?<>:*|"^ are unusable in Windows filenames,
131 # other unix shell characters are unspecial in Windows
132 # so the only thing we can quote is a space, ampersand, caret and single quote
133 16 ▶ $string = '"'.$string.'"' if $string =~ /[ &^']/;
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ /[ &^']/ 14 2 16
134 }
135 else
136 {
137 # some shell char sequences are useful shell commands
138 # others are automatic variables $(<D) $(<F) $<
139 366 ▶ unless ($string =~ /^([&<>|]|>>|2>>|2>|\|\||&&|2>&1|`[^`]+`)$/
Branch: 1 branch
BranchTrueFalseTotal
unless ($string =~ /^([&<>|]|>>|2>>|2>|\|\||&&|2>&1|`[^`]+`)$/ or $string =~ /^(\$\(<D\)|\$\(<F\)|\$<|\$@|\$%|\$\?|\$\^|\$\+|\$\||\$\*)$/) 336 30 366
Condition100%3/3
$string =~ /^([&<>|]|>>|2>>|2>|\|\||&&|2>&1|`[^`]+`)$/ or $string =~ /^(\$\(<D\)|\$\(<F\)|\$<|\$@|\$%|\$\?|\$\^|\$\+|\$\||\$\*)$/
l!l&&r!l&&!r
2010336
MC/DC100%2/2
$string =~ /^([&<>|]|>>|2>>|2>|\|\||&&|2>&1|`[^`]+`)$/ or $string =~ /^(\$\(<D\)|\$\(<F\)|\$<|\$@|\$%|\$\?|\$\^|\$\+|\$\||\$\*)$/
$string =~ /^([&<>|]|>>|2>>|2>|\|\||&&|2>&1|`[^`]+`)$/ $string =~ /^(\$\(<D\)|\$\(<F\)|\$<|\$@|\$%|\$\?|\$\^|\$\+|\$\||\$\*)$/
Truth table100%3/3
A or B
A= $string =~ /^([&<>|]|>>|2>>|2>|\|\||&&|2>&1|`[^`]+`)$/B= $string =~ /^(\$\(<D\)|\$\(<F\)|\$<|\$@|\$%|\$\?|\$\^|\$\+|\$\||\$\*)$/
ABresult
1X1
011
000
140 or $string =~ /^(\$\(<D\)|\$\(<F\)|\$<|\$@|\$%|\$\?|\$\^|\$\+|\$\||\$\*)$/)
141 {
142 # backslash escape shell characters
143 336 $string =~ s/([!#'"() `&<>|\\])/\\$1/g;
144 # unquote $(FOO) variables escaped above
145 336 $string =~ s/\$\\\(([^)]+)\\\)/\$($1)/g;
146 # double escape $ as \$$ unless part of a $(VARIABLE)
147 336 $string =~ s/\$([^({]|$)/\\\$\$$1/g;
148 }
149 }
150 382 return $string;
151 }
152  
153 5 1;
« blib/lib/Panotools/Makefile/Comment.pm ↑ Summary lib/Panotools/Script/Line.pm »