Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[tesh] reduce the uglyness of that code a bit
[simgrid.git] / tools / tesh / tesh.pl
1 #! /usr/bin/env perl
2
3 # Copyright (c) 2012-2014. The SimGrid Team.
4 # All rights reserved.
5
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of the license (GNU LGPL) which comes with this package.
8 eval 'exec perl -S $0 ${1+"$@"}'
9   if $running_under_some_shell;
10
11 =encoding UTF-8
12
13 =head1 NAME
14
15 tesh -- testing shell
16
17 =head1 SYNOPSIS
18
19 B<tesh> [I<options>] I<tesh_file>
20
21 =cut
22 my($timeout)=0;
23 my($time_to_wait)=0;
24 my $path = $0;
25 my $enable_coverage=0;
26 my $diff_tool=0;
27 my $diff_tool_tmp_fh=0;
28 my $diff_tool_tmp_filename=0;
29 my $sort_prefix = -1;
30 my $tesh_file;
31 my $tesh_name;
32 my $error=0;
33 my $exitcode=0;
34 my @bg_cmds;
35 my (%environ);
36 $SIG{'PIPE'} = 'IGNORE';
37 $path =~ s|[^/]*$||;
38 push @INC,$path;
39
40 use Getopt::Long qw(GetOptions);
41 use strict;
42 use Term::ANSIColor;
43 use Text::ParseWords;
44 use IPC::Open3;
45 use IO::File;
46 use English;
47
48 ## 
49 ## Portability bits for windows
50 ##
51
52 use constant RUNNING_ON_WINDOWS => ($OSNAME =~ /^(?:mswin|dos|os2)/oi);
53 use POSIX qw(:sys_wait_h WIFEXITED WIFSIGNALED WIFSTOPPED WEXITSTATUS WTERMSIG WSTOPSIG
54              :signal_h SIGINT SIGTERM SIGKILL SIGABRT SIGSEGV);
55 # These are not implemented on windows
56 BEGIN {
57     if (RUNNING_ON_WINDOWS) {
58         *WIFEXITED   = sub { not $_[0] & 127 };
59         *WEXITSTATUS = sub { $_[0] >> 8 };
60         *WIFSIGNALED = sub { ($_[0] & 127) && ($_[0] & 127 != 127) };
61         *WTERMSIG    = sub { $_[0] & 127 };
62     }
63 }
64
65
66 ##
67 ## Command line option handling
68 ##
69
70 if ($ARGV[0] eq "--internal-killer-process") {
71     # We fork+exec a waiter process in charge of killing the command after timeout
72     # If the command stops earlier, that's fine: the killer sends a signal to an already stopped process, fails, and quits. 
73     #    Nobody cares about the killer issues. 
74     #    The only problem could arise if another process is given the same PID than cmd. We bet it won't happen :)
75     my $time_to_wait = $ARGV[1];
76     my $pid = $ARGV[2];
77     sleep $time_to_wait;
78     kill('TERM', $pid);
79     sleep 1;
80     kill('KILL', $pid);
81     exit $time_to_wait;
82 }
83
84
85 sub var_subst {
86     my ($text, $name, $value) = @_;
87     if ($value) {
88         $text =~ s/\${$name(?::[=-][^}]*)?}/$value/g;
89         $text =~ s/\$$name(\W|$)/$value$1/g;
90     }
91     else {
92         $text =~ s/\${$name:=([^}]*)}/$1/g;
93         $text =~ s/\${$name}//g;
94         $text =~ s/\$$name(\W|$)/$1/g;
95     }
96     return $text;
97 }
98
99 # option handling helper subs
100 sub cd_cmd {
101     my $directory=$_[1];
102     my $failure=1;
103     if (-e $directory && -d $directory) {
104         chdir("$directory");
105         print "[Tesh/INFO] change directory to $directory\n";
106         $failure=0;
107     } elsif (-e $directory) {
108         print "Cannot change directory to '$directory': it is not a directory\n";
109     } else {
110         print "Chdir to $directory failed: No such file or directory\n";
111     }
112     if($failure==1){
113         print "Test suite `$tesh_file': NOK (system error)\n";
114         exit 4;
115     }
116 }
117
118 sub setenv_cmd {
119   my($var,$ctn);
120   if ($_[0] =~ /^(.*)=(.*)$/) {
121     ($var,$ctn)=($1,$2);
122   }elsif ($_[1] =~ /^(.*)=(.*)$/) {
123     ($var,$ctn)=($1,$2);
124   } else {
125       die "[Tesh/CRITICAL] Malformed argument to setenv: expected 'name=value' but got '$_[1]'\n";
126   }
127
128   print "[Tesh/INFO] setenv $var=$ctn\n";
129   $environ{$var} = $ctn;
130 }
131
132 # Main option parsing sub
133
134 sub get_options {
135   # remove the tesh file from the ARGV used
136   my @ARGV = @_;
137   $tesh_file = pop @ARGV;
138
139   # temporary arrays for GetOption
140   my @cfg;
141   my $log; # ignored
142
143
144   my %opt = (
145     "help"  => 0,
146     "debug"   => 0,
147     );
148
149   Getopt::Long::config('bundling', 'no_getopt_compat', 'no_auto_abbrev');
150
151   GetOptions(
152     'help|h'   => \$opt{'help'},
153
154     'debug|d'  => \$opt{"debug"},
155
156     'difftool=s' => \$diff_tool,
157
158     'cd=s'     => \&cd_cmd,
159     'timeout=s'  => \$opt{'timeout'},
160     'setenv=s'   => \&setenv_cmd,
161     'cfg=s'    => \@cfg,
162     'log=s'    => \$log,
163     'enable-coverage+'  => \$enable_coverage,
164     );
165
166   if($enable_coverage){
167     print "Enable coverage\n";
168   }
169
170   if($diff_tool){
171     use File::Temp qw/ tempfile /;
172     ($diff_tool_tmp_fh, $diff_tool_tmp_filename) = tempfile();
173     print "New tesh: $diff_tool_tmp_filename\n";
174   }
175
176   if ($tesh_file =~ m/(.*)\.tesh/) {
177     $tesh_name=$1;
178     print "Test suite `$tesh_name'\n";
179   } else {
180     $tesh_file="(stdin)";
181     $tesh_name="(stdin)";
182     print "Test suite from stdin\n";
183   }
184
185   foreach (@cfg) {
186     $opt{'cfg'} .= " --cfg=$_";
187   }
188   return %opt;
189 }
190
191 my %opts = get_options(@ARGV);
192
193 ##
194 ## File parsing
195 ##
196 my($return)=-1;
197 my($forked);
198 my($config)="";
199 my(@buffer_tesh)=();
200
201 ###########################################################################
202
203 sub exit_status {
204     my $status = shift;
205     if (WIFEXITED($status)) {
206         $exitcode=WEXITSTATUS($status)+40;
207         return "returned code ".WEXITSTATUS($status);
208     } elsif (WIFSIGNALED($status)) {
209         my $code;
210         if (WTERMSIG($status) == SIGINT){$code="SIGINT"; }
211         elsif  (WTERMSIG($status) == SIGTERM) {$code="SIGTERM"; }
212         elsif  (WTERMSIG($status) == SIGKILL) {$code= "SIGKILL"; }
213         elsif  (WTERMSIG($status) == SIGABRT) {$code="SIGABRT"; }
214         elsif  (WTERMSIG($status) == SIGSEGV) {$code="SIGSEGV" ;}
215         $exitcode=WTERMSIG($status)+4;
216         return "got signal $code";
217     }
218     return "Unparsable status. Is the process stopped?";
219 }
220
221 sub exec_cmd {
222   my %cmd = %{$_[0]};
223   if ($opts{'debug'}) {
224     print "IN BEGIN\n";
225     map {print "  $_"} @{$cmd{'in'}};
226     print "IN END\n";
227     print "OUT BEGIN\n";
228     map {print "  $_"} @{$cmd{'out'}};
229     print "OUT END\n";
230     print "CMD: $cmd{'cmd'}\n";
231   }
232
233   # cleanup the command line
234   if(RUNNING_ON_WINDOWS) {
235       var_subst($cmd{'cmd'}, "EXEEXT", ".exe");
236   } else {
237       var_subst($cmd{'cmd'}, "EXEEXT", "");
238   }
239
240   # substitute environ variables
241   foreach my $key (keys %environ) {
242       $cmd{'cmd'} = var_subst($cmd{'cmd'}, $key, $environ{$key});
243   }
244   # substitute remaining variables, if any
245   while ($cmd{'cmd'} =~ /\${(\w+)(?::[=-][^}]*)?}/) {
246       $cmd{'cmd'} = var_subst($cmd{'cmd'}, $1, "");
247   }
248   while ($cmd{'cmd'} =~ /\$(\w+)/) {
249       $cmd{'cmd'} = var_subst($cmd{'cmd'}, $1, "");
250   }
251
252   # add cfg options
253   $cmd{'cmd'} .= " $opts{'cfg'}" if (defined($opts{'cfg'}) && length($opts{'cfg'}));
254
255   # final cleanup
256   $cmd{'cmd'} =~ s/^\s+//;
257   $cmd{'cmd'} =~ s/\s+$//;
258
259   print "[$tesh_name:$cmd{'line'}] $cmd{'cmd'}\n" ;
260
261   ###
262   # exec the command line
263   ###  $line =~ s/\r//g;
264
265   $cmd{'got'} = IO::File->new_tmpfile;
266   $cmd{'got'}->autoflush(1);
267   local *E = $cmd{'got'};
268   $cmd{'pid'} = open3(\*CHILD_IN,  ">&E",  ">&E",
269                       quotewords('\s+', 0, $cmd{'cmd'}));
270
271   # push all provided input to executing child
272   map { print CHILD_IN "$_\n"; }  @{$cmd{'in'}};
273   close CHILD_IN;
274
275   # if timeout specified, fork and kill executing child at the end of timeout
276   if (not $cmd{'background'} and (defined($cmd{'timeout'}) or defined($opts{'timeout'}))){
277     $time_to_wait= defined($cmd{'timeout'}) ? $cmd{'timeout'} : $opts{'timeout'};
278     $forked = fork();
279     $timeout=-1;
280     die "fork() failed: $!" unless defined $forked;
281     if ( $forked == 0 ) { # child
282         exec("$PROGRAM_NAME --internal-killer-process $time_to_wait $cmd{'pid'}");
283     }
284   }
285
286   # Cleanup the executing child, and kill the timeouter brother on need
287   $cmd{'return'} = 0 unless defined($cmd{'return'});
288   if ($cmd{'background'} != 1) {
289     waitpid ($cmd{'pid'}, 0);
290     $cmd{'gotret'} = exit_status($?);
291     parse_out(\%cmd);
292   } else {
293     # & commands, which will be handled at the end
294     push @bg_cmds, \%cmd;
295   }
296 }
297
298
299 sub parse_out {
300   my %cmd = %{$_[0]};
301   my $gotret=$cmd{'gotret'};
302
303   my $wantret;
304
305   if(defined($cmd{'expect'}) and ($cmd{'expect'} ne "")){
306     $wantret = "got signal $cmd{'expect'}";
307   }else{
308     $wantret = "returned code ".(defined($cmd{'return'})? $cmd{'return'} : 0);
309   }
310
311   local *got = $cmd{'got'};
312   seek(got,0,0);
313   # pop all output from executing child
314   my @got;
315   while(defined(my $got=<got>)) {
316     $got =~ s/\r//g;
317     chomp $got;
318     print $diff_tool_tmp_fh "> $got\n" if ($diff_tool);
319
320     if (!($enable_coverage and $got=~ /^profiling:/)){
321       push @got, $got;
322     }
323   }
324
325   if ($cmd{'sort'}){
326     # Save the unsorted observed output to report it on error.
327     map { push @{$cmd{'unsorted got'}}, $_ } @got;
328
329     sub mysort{
330         substr($a, 0, $sort_prefix) cmp substr($b, 0, $sort_prefix)
331     }
332     use sort 'stable';
333     if ($sort_prefix>0) {
334         @got = sort mysort @got;
335     } else {
336         @got = sort @got;
337     }       
338     while (@got and $got[0] eq "") {
339       shift @got;
340     }
341
342     # Sort the expected output to make it easier to write for humans
343     if(defined($cmd{'out'})){
344       if ($sort_prefix>0) {
345           @{$cmd{'out'}} = sort mysort @{$cmd{'out'}};
346       } else {
347           @{$cmd{'out'}} = sort @{$cmd{'out'}};
348       }
349       while (@{$cmd{'out'}} and ${$cmd{'out'}}[0] eq "") {
350         shift @{$cmd{'out'}};
351       }
352     }
353   }
354
355   # Did we timeout ? If yes, handle it. If not, kill the forked process.
356
357   if($timeout==-1 and ($gotret eq "got signal SIGTERM" or $gotret eq "got signal SIGKILL")){
358     $gotret="return code 0";
359     $timeout=1;
360     $gotret= "timeout after $time_to_wait sec";
361     $error=1;
362     $exitcode=3;
363     print STDERR "<$cmd{'file'}:$cmd{'line'}> timeouted. Kill the process.\n";
364   }else{
365     $timeout=0;
366   }
367   if($gotret ne $wantret) {
368     $error=1;
369     my $msg = "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> $gotret)\n";
370     if ($timeout!=1) {
371         $msg=$msg."Output of <$cmd{'file'}:$cmd{'line'}> so far:\n";
372     }
373     map {$msg .=  "|| $_\n"} @got;
374     if(!@got) {
375         if($timeout==1){
376         print STDERR "<$cmd{'file'}:$cmd{'line'}> No output before timeout\n";
377         }else{
378         $msg .= "||\n";
379         }
380     }
381     $timeout = 0;
382     print STDERR "$msg";
383   }
384
385
386   ###
387   # Check the result of execution
388   ###
389   my $diff;
390   if (defined($cmd{'output display'})) {
391     print "[Tesh/INFO] Here is the (ignored) command output:\n";
392     map { print "||$_\n" } @got;      
393   } elsif (defined($cmd{'output ignore'})) {
394     print "(ignoring the output of <$cmd{'file'}:$cmd{'line'}> as requested)\n"
395   } else {
396     $diff = build_diff(\@{$cmd{'out'}}, \@got);
397   }
398   if (length $diff) {
399     print "Output of <$cmd{'file'}:$cmd{'line'}> mismatch".($cmd{'sort'}?" (even after sorting)":"").":\n";
400     map { print "$_\n" } split(/\n/,$diff);
401     if ($cmd{'sort'}) {
402         print "WARNING: Both the observed output and expected output were sorted as requested.\n";
403         print "WARNING: Output were only sorted using the $sort_prefix first chars.\n"
404           if ($sort_prefix>0);
405         print "WARNING: Use <! output sort 19> to sort by simulated date and process ID only.\n";
406         # print "----8<---------------  Begin of unprocessed observed output (as it should appear in file):\n";
407         # map {print "> $_\n"} @{$cmd{'unsorted got'}};
408         # print "--------------->8----  End of the unprocessed observed output.\n";
409     }
410
411     print "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> output mismatch)\n";
412     exit 2;
413   }
414 }
415
416 sub mkfile_cmd {
417   my %cmd = %{$_[0]};
418   my $file = $cmd{'arg'};
419   print "[Tesh/INFO] mkfile $file\n";
420
421   unlink($file);
422   open(FILE,">$file") or die "[Tesh/CRITICAL] Unable to create file $file: $!\n";
423   print FILE join("\n", @{$cmd{'in'}});
424   print FILE "\n" if (scalar @{$cmd{'in'}} > 0);
425   close(FILE);
426 }
427
428 # parse tesh file
429 my $infh; # The file descriptor from which we should read the teshfile
430 if($tesh_file eq "(stdin)"){
431   $infh = *STDIN;
432 } else {
433   open $infh, $tesh_file 
434       or die "[Tesh/CRITICAL] Unable to open $tesh_file: $!\n";
435 }
436
437 my %cmd; # everything about the next command to run
438 my $line_num=0;
439 LINE: while (defined(my $line=<$infh>) and not $error) {
440   chomp $line;
441   $line =~ s/\r//g;
442
443   $line_num++;
444   print "[TESH/debug] $line_num: $line\n" if $opts{'debug'};
445   my $next;
446   # deal with line continuations
447   while ($line =~ /^(.*?)\\$/) {
448     $next=<$infh>;
449     die "[TESH/CRITICAL] Continued line at end of file\n"
450       unless defined($next);
451     $line_num++;
452     chomp $next;
453     print "[TESH/debug] $line_num: $next\n" if $opts{'debug'};
454     $line = $1.$next;
455   }
456
457   # Push delayed commands on empty lines
458   unless ($line =~ m/^(.)(.*)$/) {
459     if (defined($cmd{'cmd'}))  {
460       exec_cmd(\%cmd);
461       %cmd = ();
462     }
463     print $diff_tool_tmp_fh "$line\n" if ($diff_tool);
464     next LINE;
465   }
466
467   my ($cmd,$arg) = ($1,$2);
468   print $diff_tool_tmp_fh "$line\n" if ($diff_tool and $cmd ne '>');
469   $arg =~ s/^ //g;
470   $arg =~ s/\r//g;
471   $arg =~ s/\\\\/\\/g;
472   # handle the commands
473   if ($cmd =~ /^#/) {    #comment
474   } elsif ($cmd eq '>'){    #expected result line
475     print "[TESH/debug] push expected result\n" if $opts{'debug'};
476     push @{$cmd{'out'}}, $arg;
477
478   } elsif ($cmd eq '<') {    # provided input
479     print "[TESH/debug] push provided input\n" if $opts{'debug'};
480     push @{$cmd{'in'}}, $arg;
481
482   } elsif ($cmd eq 'p') {    # comment
483     print "[$tesh_name:$line_num] $arg\n";
484
485   } elsif ($cmd eq '$') {  # Command
486     # if we have something buffered, run it now
487     if (defined($cmd{'cmd'})) {
488       exec_cmd(\%cmd);
489       %cmd = ();
490     }
491     if ($arg =~ /^\s*mkfile /){      # "mkfile" command line
492       die "[TESH/CRITICAL] Output expected from mkfile command!\n" if scalar @{cmd{'out'}};
493
494       $cmd{'arg'} = $arg;
495       $cmd{'arg'} =~ s/\s*mkfile //;
496       mkfile_cmd(\%cmd);
497       %cmd = ();
498
499     } elsif ($arg =~ /^\s*cd /) {
500       die "[TESH/CRITICAL] Input provided to cd command!\n" if scalar @{cmd{'in'}};
501       die "[TESH/CRITICAL] Output expected from cd command!\n" if scalar @{cmd{'out'}};
502
503       $arg =~ s/^ *cd //;
504       cd_cmd("",$arg);
505       %cmd = ();
506
507     } else { # regular command
508       $cmd{'cmd'} = $arg;
509       $cmd{'file'} = $tesh_file;
510       $cmd{'line'} = $line_num;
511     }
512   }
513   elsif($cmd eq '&'){      # parallel command line
514
515     if (defined($cmd{'cmd'})) {
516       exec_cmd(\%cmd);
517       %cmd = ();
518     }
519     $cmd{'background'} = 1;
520     $cmd{'cmd'} = $arg;
521     $cmd{'file'} = $tesh_file;
522     $cmd{'line'} = $line_num;
523   }
524   elsif($line =~ /^!\s*output sort/){    #output sort
525     if (defined($cmd{'cmd'})) {
526       exec_cmd(\%cmd);
527       %cmd = ();
528     }
529     $cmd{'sort'} = 1;
530     if ($line =~ /^!\s*output sort\s+(\d+)/) {
531         $sort_prefix = $1;
532     }
533   }
534   elsif($line =~ /^!\s*output ignore/){    #output ignore
535     if (defined($cmd{'cmd'})) {
536       exec_cmd(\%cmd);
537       %cmd = ();
538     }
539     $cmd{'output ignore'} = 1;
540   }
541   elsif($line =~ /^!\s*output display/){    #output display
542     if (defined($cmd{'cmd'})) {
543       exec_cmd(\%cmd);
544       %cmd = ();
545     }
546     $cmd{'output display'} = 1;
547   }
548   elsif($line =~ /^!\s*expect signal (\w*)/) {#expect signal SIGABRT
549     if (defined($cmd{'cmd'})) {
550       exec_cmd(\%cmd);
551       %cmd = ();
552     }
553 print "hey\n";
554     $cmd{'expect'} = "$1";
555   }
556   elsif($line =~ /^!\s*expect return/){    #expect return
557     if (defined($cmd{'cmd'})) {
558       exec_cmd(\%cmd);
559       %cmd = ();
560     }
561     $line =~ s/^! expect return //g;
562     $line =~ s/\r//g;
563     $cmd{'return'} = $line;
564   }
565   elsif($line =~ /^!\s*setenv/){    #setenv
566     if (defined($cmd{'cmd'})) {
567       exec_cmd(\%cmd);
568       %cmd = ();
569     }
570     $line =~ s/^! setenv //g;
571     $line =~ s/\r//g;
572     setenv_cmd($line);
573   }
574   elsif($line =~ /^!\s*include/){    #include
575     if (defined($cmd{'cmd'})) {
576       exec_cmd(\%cmd);
577       %cmd = ();
578     }
579     print color("red"), "[Tesh/CRITICAL] need include";
580     print color("reset"), "\n";
581     die;
582   }
583   elsif($line =~ /^!\s*timeout/){    #timeout
584     if (defined($cmd{'cmd'})) {
585       exec_cmd(\%cmd);
586       %cmd = ();
587     }
588     $line =~ s/^! timeout //;
589     $line =~ s/\r//g;
590     $cmd{'timeout'} = $line;
591   } else {
592     die "[TESH/CRITICAL] parse error: $line\n";
593   }
594   if($forked){
595    kill('KILL', $forked);
596    $timeout=0;
597   }
598 }
599
600 # We're done reading the input file
601 close $infh unless ($tesh_file eq "(stdin)");
602
603 # Deal with last command
604 if (defined($cmd{'cmd'})) {
605   exec_cmd(\%cmd);
606   %cmd = ();
607 }
608
609
610 if($forked){
611    kill('KILL', $forked);
612    $timeout=0;
613 }
614
615 foreach(@bg_cmds){
616   my %test=%{$_};
617   waitpid ($test{'pid'}, 0);
618   $test{'gotret'} = exit_status($?);
619   parse_out(\%test);
620 }
621
622 @bg_cmds=();
623
624 if ($diff_tool) {
625   close $diff_tool_tmp_fh;
626   system("$diff_tool $diff_tool_tmp_filename $tesh_file");
627   unlink $diff_tool_tmp_filename;
628 }
629
630 if ($error !=0){
631     exit $exitcode;
632 } elsif($tesh_file eq "(stdin)") {
633     print "Test suite from stdin OK\n";
634 } else {
635     print "Test suite `$tesh_name' OK\n";
636 }
637
638 #my (@a,@b);
639 #push @a,"bl1";   push @b,"bl1";
640 #push @a,"bl2";   push @b,"bl2";
641 #push @a,"bl3";   push @b,"bl3";
642 #push @a,"bl4";   push @b,"bl4";
643 #push @a,"bl5";   push @b,"bl5";
644 #push @a,"bl6";   push @b,"bl6";
645 #push @a,"bl7";   push @b,"bl7";
646 ##push @a,"Perl";  push @b,"ruby";
647 #push @a,"END1";   push @b,"END1";
648 #push @a,"END2";   push @b,"END2";
649 #push @a,"END3";   push @b,"END3";
650 #push @a,"END4";   push @b,"END4";
651 #push @a,"END5";   push @b,"END5";
652 #push @a,"END6";   push @b,"END6";
653 #push @a,"END7";   push @b,"END7";
654 #print "Identical:\n". build_diff(\@a,\@b);
655
656 #@a = (); @b =();
657 #push @a,"AZE"; push @b,"EZA";
658 #print "Different:\n".build_diff(\@a,\@b);
659
660 use lib "@CMAKE_BINARY_DIR@/bin" ;
661
662 use Diff qw(diff); # postpone a bit to have time to change INC
663
664 sub build_diff {
665   my $res;
666   my $diff = Diff->new(@_);
667
668   $diff->Base( 1 );   # Return line numbers, not indices
669   my $chunk_count = $diff->Next(-1); # Compute the amount of chuncks
670   return ""   if ($chunk_count == 1 && $diff->Same());
671   $diff->Reset();
672   while(  $diff->Next()  ) {
673     my @same = $diff->Same();
674     if ($diff->Same() ) {
675       if ($diff->Next(0) > 1) { # not first chunk: print 2 first lines
676         $res .= '  '.$same[0]."\n" ;
677         $res .= '  '.$same[1]."\n" if (scalar @same>1);
678       }
679       $res .= "...\n"  if (scalar @same>2);
680 #    $res .= $diff->Next(0)."/$chunk_count\n";
681       if ($diff->Next(0) < $chunk_count) { # not last chunk: print 2 last lines
682         $res .= '  '.$same[scalar @same -2]."\n" if (scalar @same>1);
683         $res .= '  '.$same[scalar @same -1]."\n";
684       }
685     }
686     next if  $diff->Same();
687     map { $res .= "- $_\n" } $diff->Items(1);
688     map { $res .= "+ $_\n" } $diff->Items(2);
689   }
690   return $res;
691 }
692
693