bin/sort

filter: Statementstmt 65.7% 196 / 298 Branchbran 44.8% 105 / 234 Conditioncond 30.6% 19 / 62 MC/DCmcdc 2.9% 1 / 34 Subroutinesub 66.6% 14 / 21 Podpod n/a totaltotal 51.6% 335 / 649 CC 137 SCAR 49.4
CC
137
Cov
54%
_make_sort_sub
220.3
_sort_file
168.8
_merge_files
79.0
CRAP 139.1
« bin/ls ↑ Summary bin/cp »
1 #!/usr/bin/perl
2  
3 =begin metadata
4  
5 Name: sort
6 Description: sort or merge text files
7 Author: Chris Nandor, pudge@pobox.com
8 License: perl
9  
10 =end metadata
11  
12 =cut
13  
14  
15 # perl implementation of sort(1), by pudge@pobox.com
16 # see POD below for more information
17  
18 6 ▶use Getopt::Long qw(GetOptions);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 6 ▶use Fcntl qw(O_RDONLY O_WRONLY O_CREAT O_TRUNC);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 6 ▶use Symbol qw(gensym);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 6 ▶use locale;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 6 ▶use vars qw($VERSION *sortsub *sort1 *sort2 *map1 *map2 %fh);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 6 $VERSION = '1.01';
26 6 Getopt::Long::config('bundling'); # -cmu == -c -m -u
27  
28 {
29 6 my(%o, @argv, @pos);
30  
31 # take care of +pos1 -pos2 now instead of in getopts
32 6 foreach my $argv (0..$#ARGV) {
33 13 $_ = $ARGV[$argv];
34 13 my $n;
35 13 ▶ if (/^\+(\d+)(?:\.(\d+))?([bdfinr]+)?/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\+(\d+)(?:\.(\d+))?([bdfinr]+)?/l) { } 0 13 13
36 0 $n = $1 + 1;
37 0 ▶ $n .= '.' . ($2 + 1) if defined $2;
Branch: 1 branch
BranchTrueFalseTotal
if defined $2 0 0 0
38 0 ▶ $n .= $3 if $3;
Branch: 1 branch
BranchTrueFalseTotal
if $3 0 0 0
39 0 push @argv, $argv;
40 } else {
41 13 next;
42 }
43  
44 0 $_ = $ARGV[$argv + 1];
45 0 ▶ if (/^\-(\d+)(?:\.(\d+))?([bdfinr]+)?/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\-(\d+)(?:\.(\d+))?([bdfinr]+)?/l) 0 0 0
46 0 ▶ $n .= "," . (defined $2 ? ($1 + 1) . ".$2" : $1);
Branch: 1 branch
BranchTrueFalseTotal
defined $2 ? : 0 0 0
47 0 ▶ $n .= $3 if $3;
Branch: 1 branch
BranchTrueFalseTotal
if $3 0 0 0
48 0 push @argv, $argv;
49 }
50  
51 0 push @pos, $n;
52 }
53  
54 # delete used elements from @ARGV
55 6 for (reverse @argv) {
56 0 splice(@ARGV, $_, 1);
57 }
58  
59 usage() unless GetOptions(\%o, 'k=s@', qw(c m u d f i n r b D),
60 6 ▶ map {"$_=s"} qw(X t y F o R));
Branch: 1 branch
BranchTrueFalseTotal
unless &GetOptions(\%o, "k=s\@", ("c", "m", "u", "d", "f", "i", "n", "r", "b", "D"), map({"$_=s";} "X", "t", "y", "F", "o", "R")) 0 6 6
61 0 ▶ push @{$o{'k'}}, @pos if @pos; # add pos stuff back
Branch: 1 branch
BranchTrueFalseTotal
if @pos 0 6 6
62 6 ▶ @ARGV = '-' unless @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
unless @ARGV 0 6 6
63 6 $o{I} = [@ARGV]; # input files
64  
65 6 my $exit = _sort_file(\%o);
66 6 ▶ warn "Exit status is $exit\n" if $exit != 1;
Branch: 1 branch
BranchTrueFalseTotal
if $exit != 1 1 5 6
67 6 ▶ exit($exit == 1 ? $exit ? 0 : $exit : 1);
Branch: 2 branches
BranchTrueFalseTotal
$exit == 1 ? : 5 1 6
$exit ? : 5 0 5
68 }
69  
70 6 ▶sub _sort_file {
Subroutine _sort_file: called CC=48 SCAR=51.3
71 6 local $\; # don't mess up our prints
72 6 my($opts, @fh, @recs) = shift;
73  
74 # record separator, default to \n
75 6 ▶ local $/ = $opts->{R} ? $opts->{R} : "\n";
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'R'} ? : 0 6 6
76  
77 # get input files into anon array if not already
78 6 ▶ $opts->{I} = [$opts->{I}] unless ref $opts->{I};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $opts->{"I"} 0 6 6
79  
80 6 ▶ usage() unless @{$opts->{I}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$opts->{"I"};} 0 6 6
81  
82 # "K" == "no k", for later
83 6 ▶ $opts->{K} = $opts->{k} ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'k'} ? : 1 5 6
84 6 ▶ $opts->{k} = $opts->{k} ? [$opts->{k}] : [] if !ref $opts->{k};
Branch: 2 branches
BranchTrueFalseTotal
unless ref $opts->{"k"} 5 1 6
$opts->{'k'} ? : 0 5 5
85  
86 6 ▶ $opts->{'y'} ||= $ENV{'MAX_SORT_RECORDS'} || 200000; # default max records
Condition40%2/5
$opts->{"y"} ||= $ENV{"MAX_SORT_RECORDS"} || 200000
l!l&&r!l&&!r
060
$ENV{'MAX_SORT_RECORDS'} || 200000
l!l
06
MC/DC0%0/2
$opts->{"y"} ||= $ENV{"MAX_SORT_RECORDS"} || 200000
$opts->{"y"} $ENV{'MAX_SORT_RECORDS'}
Truth table0%0/3
A ||= B
A= $opts->{"y"}B= $ENV{'MAX_SORT_RECORDS'}
ABresult
1X1
011
000
87 6 ▶ $opts->{'F'} ||= $ENV{'MAX_SORT_FILES'} || 40; # default max files
Condition40%2/5
$opts->{"F"} ||= $ENV{"MAX_SORT_FILES"} || 40
l!l&&r!l&&!r
060
$ENV{'MAX_SORT_FILES'} || 40
l!l
06
MC/DC0%0/2
$opts->{"F"} ||= $ENV{"MAX_SORT_FILES"} || 40
$opts->{"F"} $ENV{'MAX_SORT_FILES'}
Truth table0%0/3
A ||= B
A= $opts->{"F"}B= $ENV{'MAX_SORT_FILES'}
ABresult
1X1
011
000
88 6 ▶ if (defined $opts->{'F'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts->{"F"}) 6 0 6
89 6 ▶ die "option -F expects a positive number\n" if (int($opts->{'F'}) < 1);
Branch: 1 branch
BranchTrueFalseTotal
if int $opts->{"F"} < 1 0 6 6
90 }
91 6 ▶ if (defined $opts->{'y'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts->{"y"}) 6 0 6
92 6 ▶ die "option -y expects a positive number\n" if (int($opts->{'y'}) < 1);
Branch: 1 branch
BranchTrueFalseTotal
if int $opts->{"y"} < 1 0 6 6
93 }
94  
95 # see big ol' mess below
96 6 _make_sort_sub($opts);
97  
98 # only check to see if file is sorted
99 6 ▶ if ($opts->{c}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{'c'}) { } 2 4 6
elsif ($opts->{'m'}) { } 0 4 4
100 2 local *F;
101 2 my $last;
102  
103 2 my $filein = $opts->{I}[0];
104 2 ▶ if ($filein eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($filein eq '-') { } 2 0 2
105 2 open F, '<-';
106 } else {
107 0 ▶ die "$0: '$filein' is a directory\n" if -d $filein;
Branch: 1 branch
BranchTrueFalseTotal
if -d $filein 0 0 0
108 0 ▶ sysopen(F, $filein, O_RDONLY)
Branch: 1 branch
BranchTrueFalseTotal
sysopen F, $filein, 0 or die "Can't open `${filein}' for reading: $!\n" 0 0 0
109 or die "Can't open `$filein' for reading: $!\n";
110 }
111  
112 2 ▶ while (defined(my $rec = <F>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $rec = readline F)) 6 1 7
113 # fail if -u and keys are not unique (assume sorted)
114 6 ▶ if ($opts->{u} && $last) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"u"} and $last) 0 6 6
Condition33%1/3
$opts->{"u"} and $last
!ll&&!rl&&r
600
MC/DC0%0/2
$opts->{"u"} and $last
$opts->{"u"} $last
Truth table33%1/3
A and B
A= $opts->{"u"}B= $last
ABresult
0X0
100
111
115 0 ▶ return 0 unless _are_uniq($opts->{K}, $last, $rec);
Branch: 1 branch
BranchTrueFalseTotal
unless _are_uniq($opts->{"K"}, $last, $rec) 0 0 0
116 }
117  
118 # fail if records not in proper sort order
119 6 ▶ if ($last) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last) 4 2 6
120 4 my @foo;
121 4 ▶ if ($opts->{K}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'K'}) { } 4 0 4
122 4 local $^W;
123 4 @foo = sort sort1 ($rec, $last);
124 } else {
125 0 local $^W;
126 0 @foo = map {$_->[0]} sort sortsub
127 map &map1, ($rec, $last);
128 }
129 4 ▶ return 0 if $foo[0] ne $last || $foo[1] ne $rec;
Branch: 1 branch
BranchTrueFalseTotal
if $foo[0] ne $last or $foo[1] ne $rec 1 3 4
Condition66%2/3
$foo[0] ne $last or $foo[1] ne $rec
l!l&&r!l&&!r
103
MC/DC50%1/2
$foo[0] ne $last or $foo[1] ne $rec
$foo[0] ne $last $foo[1] ne $rec
Truth table66%2/3
A or B
A= $foo[0] ne $lastB= $foo[1] ne $rec
ABresult
1X1
011
000
130 }
131  
132 # save value of last record
133 5 $last = $rec;
134 }
135  
136 # success, yay
137 1 return 1;
138  
139 # if merging sorted files
140 } elsif ($opts->{'m'}) {
141  
142 0 foreach my $filein (@{$opts->{I}}) {
143  
144 # just open files and get array of handles
145 0 ▶ die "$0: '$filein' is a directory\n" if -d $filein;
Branch: 1 branch
BranchTrueFalseTotal
if -d $filein 0 0 0
146 0 my $sym = gensym();
147  
148 0 ▶ sysopen($sym, $filein, O_RDONLY)
Branch: 1 branch
BranchTrueFalseTotal
sysopen $sym, $filein, 0 or die "Can't open `${filein}' for reading: $!" 0 0 0
149 or die "Can't open `$filein' for reading: $!";
150  
151 0 push @fh, $sym;
152 }
153  
154 # ooo, get ready, get ready
155 } else {
156  
157 # once for each input file
158 4 foreach my $filein (@{$opts->{I}}) {
159 4 local *F;
160 4 my $count = 0;
161  
162 4 ▶ _debug("Sorting file $filein ...\n") if $opts->{D};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"D"} 0 4 4
163  
164 4 ▶ if ($filein eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($filein eq '-') { } 1 3 4
165 1 ▶ open F, '<-' or die "Could not open '-': $!/$^E";
Branch: 1 branch
BranchTrueFalseTotal
open F, "<-" or die "Could not open '-': $!/$^E" 1 0 1
166 } else {
167 3 ▶ die "$0: '$filein' is a directory\n" if -d $filein;
Branch: 1 branch
BranchTrueFalseTotal
if -d $filein 0 3 3
168 3 ▶ sysopen(F, $filein, O_RDONLY)
Branch: 1 branch
BranchTrueFalseTotal
sysopen F, $filein, 0 or die "Can't open `${filein}' for reading: $!" 3 0 3
169 or die "Can't open `$filein' for reading: $!";
170 }
171  
172 4 ▶ while (defined(my $rec = <F>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $rec = readline F)) 118 4 122
173 118 push @recs, $rec;
174 118 $count++; # keep track of number of records
175  
176 118 ▶ if ($count >= $opts->{'y'}) { # don't go over record limit
Branch: 1 branch
BranchTrueFalseTotal
if ($count >= $opts->{"y"}) 0 118 118
177  
178 _debug("$count records reached in `$filein'\n")
179 0 ▶ if $opts->{D};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"D"} 0 0 0
180  
181 # save to temp file, add new fh to array
182 0 push @fh, _write_temp(\@recs, $opts);
183  
184 # reset record count and record array
185 0 ($count, @recs) = (0);
186  
187 # do a merge now if at file limit
188 0 ▶ if (@fh >= $opts->{F}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fh >= $opts->{"F"}) 0 0 0
189  
190 # get filehandle and restart array with it
191 0 @fh = (_merge_files($opts, \@fh, [], _get_temp()));
192  
193 0 ▶ _debug("\nCreating temp files ...\n") if $opts->{D};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"D"} 0 0 0
194 }
195 }
196 } continue {
197 118 ▶ close F if eof;
Branch: 1 branch
BranchTrueFalseTotal
if eof 4 114 118
198 }
199  
200 4 close F;
201 }
202  
203 # records leftover, didn't reach record limit
204 4 ▶ if (@recs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@recs) 4 0 4
205 4 ▶ _debug("\nSorting leftover records ...\n") if $opts->{D};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"D"} 0 4 4
206 4 _check_last(\@recs);
207 4 ▶ if ($opts->{K}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'K'}) { } 3 1 4
208 3 local $^W;
209 3 @recs = sort sort1 @recs;
210 } else {
211 1 local $^W;
212 1 @recs = map {$_->[0]} sort sortsub map &map1, @recs;
213 }
214 }
215 }
216  
217 # do the merge thang, uh huh, do the merge thang
218 4 my $close = _merge_files($opts, \@fh, \@recs, $opts->{o});
219 4 ▶ close $close unless fileno($close) == fileno('STDOUT'); # don't close STDOUT
Branch: 1 branch
BranchTrueFalseTotal
unless fileno $close == fileno STDOUT 0 4 4
220  
221 4 ▶ _debug("\nDone!\n\n") if $opts->{D};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"D"} 0 4 4
222 4 return 1; # yay
223 }
224  
225 # take optional arrayref of handles of sorted files,
226 # plus optional arrayref of sorted scalars
227 4 ▶sub _merge_files {
Subroutine _merge_files: called CC=16 SCAR=43.7
228 # we need the options, filehandles, and output file
229 4 my($opts, $fh, $recs, $file) = @_;
230 4 my($uniq, $first, $o, %oth);
231  
232 # arbitrarily named keys, store handles as values
233 0 %oth = map {($o++ => $_)} @$fh;
234  
235 # match handle key in %oth to next record of the handle
236 %fh = map {
237 0 my $fh = $oth{$_};
238 0 ($_ => scalar <$fh>);
239 } keys %oth;
240  
241 # extra records, special X "handle"
242 4 ▶ $fh{X} = shift @$recs if @$recs;
Branch: 1 branch
BranchTrueFalseTotal
if @$recs 4 0 4
243  
244 4 ▶ _debug("\nCreating sorted $file ...\n") if $opts->{D};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"D"} 0 4 4
245  
246 # output to STDOUT if no output file provided
247 4 ▶ if ($file eq '') {
Branch: 2 branches
BranchTrueFalseTotal
if ($file eq '') { } 4 0 4
elsif (not ref $file) { } 0 0 0
248 4 $file = \*STDOUT;
249  
250 # if output file is a path, not a reference to a file, open
251 # file and get a reference to it
252 } elsif (!ref $file) {
253 0 my $tfh = gensym();
254 0 ▶ sysopen($tfh, $file, O_WRONLY|O_CREAT|O_TRUNC)
Branch: 1 branch
BranchTrueFalseTotal
sysopen $tfh, $file, 577 or die "Can't open `${file}' for writing: $!" 0 0 0
255 or die "Can't open `$file' for writing: $!";
256 0 $file = $tfh;
257 }
258  
259 4 my $oldfh = select $file;
260 4 $| = 0; # just in case, use the buffer, you knob
261  
262 4 ▶ while (keys %fh) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %main::fh) 4 0 4
263 # don't bother sorting keys if only one key remains!
264 4 ▶ if (!$opts->{u} && keys %fh == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $opts->{"u"} and keys %main::fh == 1) 4 0 4
Condition33%1/3
not $opts->{"u"} and keys %main::fh == 1
!ll&&!rl&&r
004
MC/DC0%0/2
not $opts->{"u"} and keys %main::fh == 1
not $opts->{"u"} keys %main::fh == 1
Truth table33%1/3
not(A) and B
A= not $opts->{"u"}B= keys %main::fh == 1
ABresult
0X0
100
111
265 4 ($first) = keys %fh;
266 4 my $curr = $oth{$first};
267 4 ▶ my @left = $first eq 'X' ? @$recs : <$curr>;
Branch: 1 branch
BranchTrueFalseTotal
$first eq 'X' ? : 4 0 4
268 4 print $fh{$first}, @left;
269 4 delete $fh{$first};
270 4 last;
271 }
272  
273 {
274 # $first is arbitrary number assigned to first fh in sort
275 0 ▶ if ($opts->{K}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'K'}) { } 0 0 0
276 0 local $^W;
277 0 ($first) = (sort sort2 keys %fh);
278 } else {
279 0 local $^W;
280 0 ($first) = (map {$_->[0]} sort sortsub
281 map &map2, keys %fh);
282 }
283 }
284  
285 # don't print if -u and not unique
286 0 ▶ if ($opts->{u}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'u'}) { } 0 0 0
287 print $fh{$first} if
288 0 ▶ (!$uniq || _are_uniq($opts->{K}, $uniq, $fh{$first}));
Branch: 1 branch
BranchTrueFalseTotal
if not $uniq or _are_uniq($opts->{"K"}, $uniq, $main::fh{$first}) 0 0 0
Condition0%0/3
not $uniq or _are_uniq($opts->{"K"}, $uniq, $main::fh{$first})
l!l&&r!l&&!r
000
MC/DC0%0/2
not $uniq or _are_uniq($opts->{"K"}, $uniq, $main::fh{$first})
not $uniq _are_uniq($opts->{"K"}, $uniq, $main::fh{$first})
Truth table0%0/3
not(A) or B
A= not $uniqB= _are_uniq($opts->{"K"}, $uniq, $main::fh{$first})
ABresult
1X1
011
000
289 0 $uniq = $fh{$first};
290 } else {
291 0 print $fh{$first};
292 }
293  
294 # get current filehandle
295 0 my $curr = $oth{$first};
296  
297 # use @$recs, not filehandles, if key is X
298 0 ▶ my $rec = $first eq 'X' ? shift @$recs : scalar <$curr>;
Branch: 1 branch
BranchTrueFalseTotal
$first eq 'X' ? : 0 0 0
299  
300 0 ▶ if (defined $rec) { # bring up next record for this filehandle
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rec) { } 0 0 0
301 0 $fh{$first} = $rec;
302  
303 } else { # we don't need you anymore
304 0 delete $fh{$first};
305 }
306 }
307  
308 4 seek $file, 0, 0; # might need to read back from it
309 4 select $oldfh;
310 4 return $file;
311 }
312  
313 4 ▶sub _check_last {
Subroutine _check_last: called CC=2 SCAR=6.9
314 # add new record separator if not one there
315 4 ▶ ${$_[0]}[-1] .= $/ if (${$_[0]}[-1] !~ m|$/$|);
Branch: 1 branch
BranchTrueFalseTotal
if not do { $_[0] }->[-1] =~ m[$/$]l 1 3 4
316 }
317  
318 0 ▶sub _write_temp {
Subroutine _write_temp: not called CC=4 SCAR=30.0
319 0 my($recs, $opts) = @_;
320 0 ▶ my $temp = _get_temp() or die "Can't get temp file: $!";
Branch: 1 branch
BranchTrueFalseTotal
my $temp = _get_temp() or die "Can't get temp file: $!" 0 0 0
321  
322 0 _check_last($recs);
323  
324 0 ▶ _debug("New tempfile: $temp\n") if $opts->{D};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"D"} 0 0 0
325  
326 0 ▶ if ($opts->{K}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'K'}) { } 0 0 0
327 0 local $^W;
328 0 print $temp sort sort1 @{$recs};
329 } else {
330 0 local $^W;
331 0 print $temp map {$_->[0]} sort sortsub map &map1, @{$recs};
332 }
333  
334 0 seek $temp, 0, 0; # might need to read back from it
335 0 return $temp;
336 }
337  
338 2 ▶sub _parse_keydef {
Subroutine _parse_keydef: called CC=19 SCAR=42.5
339 2 my($k, $topts) = @_;
340  
341 # gurgle
342 2 $k =~ /^(\d+)(?:\.(\d+))?([bdfinr]+)?
343 (?:,(\d+)(?:\.(\d+))?([bdfinr]+)?)?$/x;
344  
345 # set defaults at zero or undef
346 2 ▶ my %opts = (
Branch: 1 branch
BranchTrueFalseTotal
defined $4 ? : 0 2 2
Condition50%5/10
$1 || 0
l!l
20
$2 || 0
l!l
02
$3 || ''
l!l
02
$5 || 0
l!l
02
$6 || ''
l!l
02
MC/DC0%0/1
$1 || 0
$1
MC/DC0%0/1
$2 || 0
$2
MC/DC0%0/1
$3 || ''
$3
MC/DC0%0/1
$5 || 0
$5
MC/DC0%0/1
$6 || ''
$6
Truth table50%1/2
A
A= $1
Aresult
11
00
Truth table50%1/2
A
A= $2
Aresult
11
00
Truth table50%1/2
A
A= $3
Aresult
11
00
Truth table50%1/2
A
A= $5
Aresult
11
00
Truth table50%1/2
A
A= $6
Aresult
11
00
347 %$topts, # get other options
348 ksf => $1 || 0, # start field
349 ksc => $2 || 0, # start field char start
350 kst => $3 || '', # start field type
351 kff => (defined $4 ? $4 : undef), # end field
352 kfc => $5 || 0, # end field char end
353 kft => $6 || '', # end field type
354 );
355  
356 # their idea of 1 is not ours
357 2 for (qw(ksf ksc kff)) { # kfc stays same
358 6 ▶ $opts{$_}-- if $opts{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{$_} 2 4 6
359 }
360  
361 # if nothing in kst or kft, use other flags possibly passed
362 2 ▶ if (!$opts{kst} && !$opts{kft}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $opts{'kst'} || $opts{'kft'}) { } 2 0 2
Condition33%1/3
$opts{'kst'} || $opts{'kft'}
l!l&&r!l&&!r
002
MC/DC0%0/2
$opts{'kst'} || $opts{'kft'}
$opts{'kst'} $opts{'kft'}
Truth table33%1/3
A || B
A= $opts{'kst'}B= $opts{'kft'}
ABresult
1X1
011
000
363 2 foreach (qw(b d f i n r)) {
364 12 ▶ $opts{kst} .= $_ if $topts->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $topts->{$_} 0 12 12
365 12 ▶ $opts{kft} .= $_ if $topts->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $topts->{$_} 0 12 12
366 }
367  
368 # except for b, flags on one apply to the other
369 } else {
370 0 foreach (qw(d f i n r)) {
371 0 ▶ $opts{kst} .= $_ if ($opts{kst} =~ /$_/ || $opts{kft} =~ /$_/);
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"kst"} =~ /$_/l or $opts{"kft"} =~ /$_/l 0 0 0
Condition0%0/3
$opts{"kst"} =~ /$_/l or $opts{"kft"} =~ /$_/l
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"kst"} =~ /$_/l or $opts{"kft"} =~ /$_/l
$opts{"kst"} =~ /$_/l $opts{"kft"} =~ /$_/l
Truth table0%0/3
A or B
A= $opts{"kst"} =~ /$_/lB= $opts{"kft"} =~ /$_/l
ABresult
1X1
011
000
372 0 ▶ $opts{kft} .= $_ if ($opts{kst} =~ /$_/ || $opts{kft} =~ /$_/);
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"kst"} =~ /$_/l or $opts{"kft"} =~ /$_/l 0 0 0
Condition0%0/3
$opts{"kst"} =~ /$_/l or $opts{"kft"} =~ /$_/l
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"kst"} =~ /$_/l or $opts{"kft"} =~ /$_/l
$opts{"kst"} =~ /$_/l $opts{"kft"} =~ /$_/l
Truth table0%0/3
A or B
A= $opts{"kst"} =~ /$_/lB= $opts{"kft"} =~ /$_/l
ABresult
1X1
011
000
373 }
374 }
375  
376 2 return \%opts;
377 }
378  
379 6 ▶sub _make_sort_sub {
Subroutine _make_sort_sub: called CC=50 SCAR=53.9
380 6 my($topts, @sortsub, @mapsub, @sort1, @sort2) = shift;
381  
382 # if no keydefs set
383 6 ▶ if ($topts->{K}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($topts->{"K"}) 5 1 6
384 5 $topts->{kst} = '';
385 5 foreach (qw(b d f i n r)) {
386 30 ▶ $topts->{kst} .= $_ if $topts->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $topts->{$_} 1 29 30
387 }
388  
389 # more complex stuff, act like we had -k defined
390 5 ▶ if ($topts->{kst} =~ /[bdfi]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($topts->{"kst"} =~ /[bdfi]/l) 0 5 5
391 0 $topts->{K} = 0;
392 0 $topts->{k} = ['K']; # special K ;-)
393 }
394 }
395  
396 # if no keydefs set
397 6 ▶ if ($topts->{K}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($topts->{'K'}) { } 5 1 6
398 5 ▶ _debug("No keydef set\n") if $topts->{D};
Branch: 1 branch
BranchTrueFalseTotal
if $topts->{"D"} 0 5 5
399  
400 # defaults for main sort sub components
401 5 my($cmp, $aa, $bb, $fa, $fb) = qw(cmp $a $b $fh{$a} $fh{$b});
402  
403 # reverse sense
404 5 ▶ ($bb, $aa, $fb, $fa) = ($aa, $bb, $fa, $fb) if $topts->{r};
Branch: 1 branch
BranchTrueFalseTotal
if $topts->{"r"} 0 5 5
405  
406 # do numeric sort
407 5 ▶ $cmp = '<=>' if $topts->{n};
Branch: 1 branch
BranchTrueFalseTotal
if $topts->{"n"} 1 4 5
408  
409 # add finished expression to array
410 5 my $sort1 = "sub { $aa $cmp $bb }\n";
411 5 my $sort2 = "sub { $fa $cmp $fb }\n";
412  
413 5 ▶ _debug("$sort1\n$sort2\n") if $topts->{D};
Branch: 1 branch
BranchTrueFalseTotal
if $topts->{"D"} 0 5 5
414  
415 {
416 5 local $^W;
417 5 ▶ *sort1 = eval $sort1;
Subroutine __ANON__: called CC=1 SCAR=0.0
418 5 ▶ die "Can't create sort sub: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 5 5
419 0 ▶ *sort2 = eval $sort2;
Subroutine __ANON__: not called CC=1 SCAR=1.2
420 5 ▶ die "Can't create sort sub: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 5 5
421 }
422  
423 } else {
424  
425 # get text separator or use whitespace
426 $topts->{t} =
427 defined $topts->{X} ? $topts->{X} :
428 1 ▶ defined $topts->{t} ? quotemeta($topts->{t}) :
Branch: 2 branches
BranchTrueFalseTotal
defined $topts->{'X'} ? : 0 1 1
defined $topts->{'t'} ? : 0 1 1
429 '\s+';
430 1 ▶ $topts->{t} =~ s|/|\\/|g if defined $topts->{X};
Branch: 1 branch
BranchTrueFalseTotal
if defined $topts->{"X"} 0 1 1
431  
432 1 foreach my $k (@{$topts->{k}}) {
433 2 my($opts, @fil) = ($topts);
434  
435 # defaults for main sort sub components
436 2 my($cmp, $ab_, $fab_, $aa, $bb) = qw(cmp $_ $fh{$_} $a $b);
437  
438 # skip stuff if special K
439 2 ▶ $opts = $k eq 'K' ? $topts : _parse_keydef($k, $topts);
Branch: 1 branch
BranchTrueFalseTotal
$k eq 'K' ? : 0 2 2
440  
441 2 ▶ if ($k ne 'K') {
Branch: 1 branch
BranchTrueFalseTotal
if ($k ne 'K') { } 2 0 2
442 my($tmp1, $tmp2) = ("\$tmp[$opts->{ksf}]",
443 2 ▶ ($opts->{kff} ? "\$tmp[$opts->{kff}]" : ''));
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'kff'} ? : 0 2 2
444  
445 # skip leading spaces
446 2 ▶ if ($opts->{kst} =~ /b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"kst"} =~ /b/l) 0 2 2
447 0 $tmp1 = "($tmp1 =~ /(\\S.*)/)[0]";
448 }
449  
450 2 ▶ if ($opts->{kft} =~ /b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"kft"} =~ /b/l) 0 2 2
451 0 $tmp2 = "($tmp2 =~ /(\\S.*)/)[0]";
452 }
453  
454 # simpler if one field, goody for us
455 2 ▶ if (! defined $opts->{kff} || $opts->{ksf} == $opts->{kff}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $opts->{'kff'} or $opts->{'ksf'} == $opts->{'kff'}) { } 2 0 2
Condition33%1/3
not defined $opts->{"kff"} or $opts->{"ksf"} == $opts->{"kff"}
l!l&&r!l&&!r
200
MC/DC0%0/2
not defined $opts->{"kff"} or $opts->{"ksf"} == $opts->{"kff"}
not defined $opts->{"kff"} $opts->{"ksf"} == $opts->{"kff"}
Truth table33%1/3
not(A) or B
A= not defined $opts->{"kff"}B= $opts->{"ksf"} == $opts->{"kff"}
ABresult
1X1
011
000
456  
457 # simpler if chars are both 0, wicked pissah
458 2 ▶ if ($opts->{ksc} == 0 &&
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{'ksc'} == 0 and !$opts->{'kfc'} || $opts->{'kfc'} == 0) { } 2 0 2
elsif (not $opts->{'kfc'}) { } 0 0 0
Condition33%2/6
$opts->{"ksc"} == 0 and !$opts->{"kfc"} || $opts->{"kfc"} == 0
!ll&&!rl&&r
002
not $opts->{"kfc"} or $opts->{"kfc"} == 0
l!l&&r!l&&!r
200
MC/DC0%0/3
$opts->{"ksc"} == 0 and !$opts->{"kfc"} || $opts->{"kfc"} == 0
$opts->{"ksc"} == 0 not $opts->{"kfc"} $opts->{"kfc"} == 0
Truth table25%1/4
A and not(B) or C
A= $opts->{"ksc"} == 0B= not $opts->{"kfc"}C= $opts->{"kfc"} == 0
ABCresult
0XX0
1000
11X1
1011
459 (!$opts->{kfc} || $opts->{kfc} == 0)) {
460 2 @fil = "\$tmp[$opts->{ksf}]";
461  
462 # hmmmmm
463 } elsif (!$opts->{kfc}) {
464 0 @fil = "substr($tmp1, $opts->{ksc})";
465  
466 # getting out of hand now
467 } else {
468 @fil = "substr($tmp1, $opts->{ksc}, ".
469 0 ($opts->{kfc} - $opts->{ksc}) . ')';
470 }
471  
472 # try again, shall we?
473 } else {
474  
475 # if spans two fields, but chars are both 0
476 # and neither has -b, alrighty
477 0 ▶ if ($opts->{kfc} == 0 && $opts->{ksc} == 0 &&
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{'kfc'} == 0 and $opts->{'ksc'} == 0 and not $opts->{'kst'} =~ /b/l and not $opts->{'kft'} =~ /b/l) { } 0 0 0
elsif ($opts->{'kff'} - $opts->{'ksf'} == 1) { } 0 0 0
Condition0%0/9
$opts->{"kfc"} == 0 and $opts->{"ksc"} == 0 and not $opts->{"kst"} =~ /b/l and not $opts->{"kft"} =~ /b/l
!ll&&!rl&&r
000
$opts->{"kfc"} == 0 and $opts->{"ksc"} == 0 and not $opts->{"kst"} =~ /b/l
!ll&&!rl&&r
000
$opts->{"kfc"} == 0 and $opts->{"ksc"} == 0
!ll&&!rl&&r
000
MC/DC0%0/4
$opts->{"kfc"} == 0 and $opts->{"ksc"} == 0 and not $opts->{"kst"} =~ /b/l and not $opts->{"kft"} =~ /b/l
$opts->{"kfc"} == 0 $opts->{"ksc"} == 0 not $opts->{"kst"} =~ /b/l not $opts->{"kft"} =~ /b/l
Truth table0%0/5
A and B and not(C) and not(D)
A= $opts->{"kfc"} == 0B= $opts->{"ksc"} == 0C= not $opts->{"kst"} =~ /b/lD= not $opts->{"kft"} =~ /b/l
ABCDresult
0XXX0
10XX0
110X0
11100
11111
478 $opts->{kst} !~ /b/ && $opts->{kft} !~ /b/) {
479 0 @fil = "join(''," .
480 "\@tmp[$opts->{ksf} .. $opts->{kff}])";
481  
482 # if only one field away
483 } elsif (($opts->{kff} - $opts->{ksf}) == 1) {
484 0 @fil = "join('', substr($tmp1, $opts->{ksc}), " .
485 "substr($tmp2, 0, $opts->{kfc}))";
486  
487 # fine, have it your way! hurt me! love me!
488 } else {
489 @fil = "join('', substr($tmp1, $opts->{ksc}), " .
490 "\@tmp[" . ($opts->{ksf} + 1) . " .. " .
491 0 ($opts->{kff} - 1) . "], " .
492 "substr($tmp2, 0, $opts->{kfc}))";
493 }
494 }
495 } else {
496 0 ▶ @fil = $opts->{kst} =~ /b/ ?
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'kst'} =~ /b/l ? : 0 0 0
497 "(\$tmp[0] =~ /(\\S.*)/)[0]" : "\$tmp[0]";
498 }
499  
500 # fold to upper case
501 2 ▶ if ($opts->{kst} =~ /f/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"kst"} =~ /f/l) 0 2 2
502 0 $fil[0] = "uc($fil[0])";
503 }
504  
505 # only alphanumerics and whitespace, override -i
506 2 ▶ if ($opts->{kst} =~ /d/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{'kst'} =~ /d/l) { } 0 2 2
elsif ($opts->{'kst'} =~ /i/l) { } 0 2 2
507 0 $topts->{DD}++;
508 0 push @fil, "\$tmp =~ s/[^\\w\\s]+//g", '"$tmp"';
509  
510 # only printable characters
511 } elsif ($opts->{kst} =~ /i/) {
512 0 $fil[0] = "join '', grep { / ^ [[:print:]]+ $ /x } " .
513 "split //,\n$fil[0]";
514 }
515  
516 2 ▶ $fil[0] = "\$tmp = $fil[0]" if $opts->{kst} =~ /d/;
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"kst"} =~ /d/l 0 2 2
517  
518  
519 # reverse sense
520 2 ▶ ($bb, $aa) = ($aa, $bb) if ($opts->{kst} =~ /r/);
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"kst"} =~ /r/l 0 2 2
521  
522 # do numeric sort
523 2 ▶ $cmp = '<=>' if ($opts->{kst} =~ /n/);
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"kst"} =~ /n/l 0 2 2
524  
525 # add finished expressions to arrays
526 2 my $n = @sortsub + 2;
527 2 push @sortsub, sprintf "%s->[$n] %s %s->[$n]",
528 $aa, $cmp, $bb;
529  
530 2 ▶ if (@fil > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fil > 1) { } 0 2 2
531 push @mapsub, " (\n" .
532 0 join(",\n", map {s/^/ /mg; $_} @fil),
533 "\n )[-1],\n ";
534 } else {
535 2 push @mapsub, " " . $fil[0] . ",\n ";
536 }
537 }
538  
539 # if not -u
540 1 ▶ if (! $topts->{u} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($topts->{"u"}) 1 0 1
541 # do straight compare if all else is equal
542 push @sortsub, sprintf "%s->[1] %s %s->[1]",
543 1 ▶ $topts->{r} ? qw($b cmp $a) : qw($a cmp $b);
Branch: 1 branch
BranchTrueFalseTotal
$topts->{'r'} ? : 0 1 1
544 }
545  
546 1 my(%maps, $sortsub, $mapsub) = (map1 => '$_', map2 => '$fh{$_}');
547  
548 1 $sortsub = "sub {\n " . join(" || \n ", @sortsub) . "\n}\n";
549  
550 1 for my $m (keys %maps) {
551 2 my $k = $maps{$m};
552 $maps{$m} = sprintf "sub {\n my \@tmp = %s;\n",
553 2 ▶ $topts->{k}[0] eq 'K' ? $k : "split(/$topts->{t}/, $k)";
Branch: 1 branch
BranchTrueFalseTotal
$topts->{'k'}[0] eq 'K' ? : 0 2 2
554  
555 2 ▶ $maps{$m} .= " my \$tmp;\n" if $topts->{DD};
Branch: 1 branch
BranchTrueFalseTotal
if $topts->{"DD"} 0 2 2
556 2 $maps{$m} .= "\n [\$_, $k";
557 2 ▶ $maps{$m} .= ",\n " . join('', @mapsub) if @mapsub;
Branch: 1 branch
BranchTrueFalseTotal
if @mapsub 2 0 2
558 2 $maps{$m} .= "]\n}\n";
559 }
560  
561 1 ▶ _debug("$sortsub\n$maps{map1}\n$maps{map2}\n") if $topts->{D};
Branch: 1 branch
BranchTrueFalseTotal
if $topts->{"D"} 0 1 1
562  
563 {
564 1 local $^W;
565 1 ▶ *sortsub = eval $sortsub;
Subroutine __ANON__: called CC=3 SCAR=11.7
Branch: 1 branch
BranchTrueFalseTotal
$main::a->[2] cmp $main::b->[2] or $main::a->[3] cmp $main::b->[3] or $main::a->[1] cmp $main::b->[1] 16 0 16
Condition66%2/3
$main::a->[2] cmp $main::b->[2] or $main::a->[3] cmp $main::b->[3]
l!l&&r!l&&!r
1420
MC/DC0%0/2
$main::a->[2] cmp $main::b->[2] or $main::a->[3] cmp $main::b->[3]
$main::a->[2] cmp $main::b->[2] $main::a->[3] cmp $main::b->[3]
Truth table66%2/3
A or B
A= $main::a->[2] cmp $main::b->[2]B= $main::a->[3] cmp $main::b->[3]
ABresult
1X1
011
000
566 1 ▶ die "Can't create sort sub: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
567 1 ▶ *map1 = eval $maps{map1};
Subroutine __ANON__: called CC=1 SCAR=0.0
568 1 ▶ die "Can't create sort sub: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
569 0 ▶ *map2 = eval $maps{map2};
Subroutine __ANON__: not called CC=1 SCAR=2.6
570 1 ▶ die "Can't create sort sub: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
571 }
572 }
573 }
574  
575  
576 0 ▶sub _get_temp { # nice and simple
Subroutine _get_temp: not called CC=1 SCAR=6.9
577 0 require IO::File;
578 0 IO::File->new_tmpfile;
579 }
580  
581 0 ▶sub _are_uniq {
Subroutine _are_uniq: not called CC=2 SCAR=17.9
582 0 my $nok = shift;
583 0 local $^W;
584  
585 0 ▶ if ($nok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nok) { } 0 0 0
586 0 ($a, $b) = @_;
587 0 return &sort1;
588 } else {
589 0 ($a, $b) = map &map1, @_;
590 0 return &sortsub;
591 }
592 }
593  
594 0 ▶sub _debug {
Subroutine _debug: not called CC=1 SCAR=6.9
595 0 print STDERR @_;
596 }
597  
598 0 ▶sub usage {
Subroutine usage: not called CC=1 SCAR=6.9
599 0 require Pod::Usage;
600 0 Pod::Usage::pod2usage({ -exitval => 1, -verbose => 0 });
601 }
602  
603 __END__
« bin/ls ↑ Summary bin/cp »