Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2dd685b5935744c0b159031c8a4d2e3faa3670bb
[simgrid.git] / tools / tesh / tesh.pl
1 #! /usr/bin/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($bindir)=".";
23 my($srcdir)=".";
24 my($timeout)=0;
25 my($time_to_wait)=0;
26 my $path = $0;
27 my $OS;
28 my $enable_coverage=0;
29 my $diff_tool=0;
30 my $diff_tool_tmp_fh=0;
31 my $diff_tool_tmp_filename=0;
32 my $sort_prefix = -1;
33 my $tesh_file;
34 my $tesh_name;
35 my $error=0;
36 my $exitcode=0;
37 my @bg_cmds;
38 my (%environ);
39 $SIG{'PIPE'} = 'IGNORE';
40 $path =~ s|[^/]*$||;
41 push @INC,$path;
42
43 use Getopt::Long qw(GetOptions);
44 use strict;
45 use Term::ANSIColor;
46 use Text::ParseWords;
47 use IPC::Open3;
48 use IO::File;
49
50 if($^O eq "linux"){
51     $OS = "UNIX";
52 }
53 else{
54     $OS = "WIN";
55     $ENV{"PRINTF_EXPONENT_DIGITS"} = "2";
56 }
57
58 ##
59 ## Command line option handling
60 ##
61
62 sub var_subst {
63     my ($text, $name, $value) = @_;
64     if ($value) {
65         $text =~ s/\${$name(?::[=-][^}]*)?}/$value/g;
66         $text =~ s/\$$name(\W|$)/$value$1/g;
67     }
68     else {
69         $text =~ s/\${$name:=([^}]*)}/$1/g;
70         $text =~ s/\${$name}//g;
71         $text =~ s/\$$name(\W|$)/$1/g;
72     }
73     return $text;
74 }
75
76 # option handling helper subs
77 sub cd_cmd {
78   my $directory=$_[1];
79   my $failure=1;
80   if (-e $directory && -d $directory) {
81     chdir("$directory");
82     print "[Tesh/INFO] change directory to $directory\n";
83   $failure=0;
84   } elsif (-e $directory) {
85     print "Cannot change directory to '$directory': it is not a directory\n";
86   } else {
87     print "Chdir to $directory failed: No such file or directory\n";
88   }
89   if($failure==1){
90   $error=1;
91   $exitcode=4;
92   print "Test suite `$tesh_file': NOK (system error)\n";
93   exit 4;
94   }
95 }
96
97 sub setenv_cmd {
98   my($var,$ctn);
99   if ($_[0] =~ /^(.*)=(.*)$/) {
100     ($var,$ctn)=($1,$2);
101   }elsif ($_[1] =~ /^(.*)=(.*)$/) {
102     ($var,$ctn)=($1,$2);
103   } else {
104       die "[Tesh/CRITICAL] Malformed argument to setenv: expected 'name=value' but got '$_[1]'\n";
105   }
106
107   print "[Tesh/INFO] setenv $var=$ctn\n";
108   $environ{$var} = $ctn;
109 }
110
111 # Main option parsing sub
112
113 sub get_options {
114   # remove the tesh file from the ARGV used
115   my @ARGV = @_;
116   $tesh_file = pop @ARGV;
117
118   # temporary arrays for GetOption
119   my @verbose = ();
120   my @cfg;
121   my $log; # ignored
122
123
124   my %opt = (
125     "help"  => 0,
126     "debug"   => 0,
127     "verbose" => 0
128     );
129
130   Getopt::Long::config('bundling', 'no_getopt_compat', 'no_auto_abbrev');
131
132   GetOptions(
133     'help|h'   => \$opt{'help'},
134
135     'verbose|v'  => \@verbose,
136     'debug|d'  => \$opt{"debug"},
137
138     'difftool=s' => \$diff_tool,
139
140     'cd=s'     => \&cd_cmd,
141     'timeout=s'  => \$opt{'timeout'},
142     'setenv=s'   => \&setenv_cmd,
143     'cfg=s'    => \@cfg,
144     'log=s'    => \$log,
145     'enable-coverage+'  => \$enable_coverage,
146     );
147
148   if($enable_coverage){
149     print "Enable coverage\n";
150   }
151
152   if($diff_tool){
153     use File::Temp qw/ tempfile /;
154     ($diff_tool_tmp_fh, $diff_tool_tmp_filename) = tempfile();
155     print "New tesh: $diff_tool_tmp_filename\n";
156   }
157
158   unless($tesh_file=~/(.*)\.tesh/){
159     $tesh_file="(stdin)";
160     $tesh_name="(stdin)";
161     print "Test suite from stdin\n";
162   }else{
163     $tesh_name=$1;
164     print "Test suite `$tesh_name'\n";
165   }
166
167   $opt{'verbose'} = scalar @verbose;
168   foreach (@cfg) {
169     $opt{'cfg'} .= " --cfg=$_";
170   }
171   return %opt;
172 }
173
174 my %opts = get_options(@ARGV);
175
176 ##
177 ## File parsing
178 ##
179 my($nb_arg)=0;
180 my($old_buffer);
181 my($linebis);
182 my($SIGABRT)=0;
183 my($verbose)=0;
184 my($return)=-1;
185 my($pid);
186 my($result);
187 my($result_err);
188 my($forked);
189 my($config)="";
190 my($tesh_command)=0;
191 my(@buffer_tesh)=();
192
193 #eval {
194   use POSIX;
195
196   sub exit_status {
197     my $status = shift;
198     if (POSIX::WIFEXITED($status)) {
199       $exitcode=POSIX::WEXITSTATUS($status)+40;
200       return "returned code ".POSIX::WEXITSTATUS($status);
201     } elsif (POSIX::WIFSIGNALED($status)) {
202       my $code;
203       if (POSIX::WTERMSIG($status) == SIGINT){$code="SIGINT"; }
204       elsif  (POSIX::WTERMSIG($status) == SIGTERM) {$code="SIGTERM"; }
205       elsif  (POSIX::WTERMSIG($status) == SIGKILL) {$code= "SIGKILL"; }
206       elsif  (POSIX::WTERMSIG($status) == SIGABRT) {$code="SIGABRT"; }
207       elsif  (POSIX::WTERMSIG($status) == SIGSEGV) {$code="SIGSEGV" ;}
208       $exitcode=POSIX::WTERMSIG($status)+4;
209       return "got signal $code";
210     }
211     return "Unparsable status. Is the process stopped?";
212   }
213 #};
214 #if ($@) { # no POSIX available?
215 #  warn "POSIX not usable to parse the return value of forked child: $@\n";
216 #  sub exit_status {
217 #    return "returned code -1 $@ ";
218 #  }
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($OS eq "WIN") {
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 (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       sleep $time_to_wait;
283       kill(SIGTERM, $cmd{'pid'});
284       sleep 1;
285       kill(SIGKILL, $cmd{'pid'});
286       exit $time_to_wait;
287     }
288   }
289
290
291   # Cleanup the executing child, and kill the timeouter brother on need
292   $cmd{'return'} = 0 unless defined($cmd{'return'});
293   if($cmd{'background'} != 1){
294     waitpid ($cmd{'pid'}, 0);
295     $cmd{'gotret'} = exit_status($?);
296     parse_out(\%cmd);
297   }else{
298     # & commands, which will be handled at the end
299     push @bg_cmds, \%cmd;
300     # no timeout for background commands
301     if($forked){
302        kill(SIGKILL, $forked);
303        $timeout=0;
304        $forked=0;
305     }
306   }
307 }
308
309
310 sub parse_out {
311   my %cmd = %{$_[0]};
312   my $gotret=$cmd{'gotret'};
313
314   my $wantret;
315
316   if(defined($cmd{'expect'}) and ($cmd{'expect'} ne "")){
317     $wantret = "got signal $cmd{'expect'}";
318   }else{
319     $wantret = "returned code ".(defined($cmd{'return'})? $cmd{'return'} : 0);
320   }
321
322   local *got = $cmd{'got'};
323   seek(got,0,0);
324   # pop all output from executing child
325   my @got;
326   while(defined(my $got=<got>)) {
327     $got =~ s/\r//g;
328     chomp $got;
329     print $diff_tool_tmp_fh "> $got\n" if ($diff_tool);
330
331     if (!($enable_coverage and $got=~ /^profiling:/)){
332       push @got, $got;
333     }
334   }
335
336   if ($cmd{'sort'}){
337     # Save the unsorted observed output to report it on error.
338     map { push @{$cmd{'unsorted got'}}, $_ } @got;
339
340     sub mysort{
341         substr($a, 0, $sort_prefix) cmp substr($b, 0, $sort_prefix)
342     }
343     use sort 'stable';
344     if ($sort_prefix>0) {
345         @got = sort mysort @got;
346     } else {
347         @got = sort @got;
348     }       
349     while (@got and $got[0] eq "") {
350       shift @got;
351     }
352
353     # Sort the expected output to make it easier to write for humans
354     if(defined($cmd{'out'})){
355       if ($sort_prefix>0) {
356           @{$cmd{'out'}} = sort mysort @{$cmd{'out'}};
357       } else {
358           @{$cmd{'out'}} = sort @{$cmd{'out'}};
359       }
360       while (@{$cmd{'out'}} and ${$cmd{'out'}}[0] eq "") {
361         shift @{$cmd{'out'}};
362       }
363     }
364   }
365
366   # Did we timeout ? If yes, handle it. If not, kill the forked process.
367
368   if($timeout==-1 and ($gotret eq "got signal SIGTERM" or $gotret eq "got signal SIGKILL")){
369     $gotret="return code 0";
370     $timeout=1;
371     $gotret= "timeout after $time_to_wait sec";
372     $error=1;
373     $exitcode=3;
374     print STDERR "<$cmd{'file'}:$cmd{'line'}> timeouted. Kill the process.\n";
375   }else{
376     $timeout=0;
377   }
378   if($gotret ne $wantret) {
379     $error=1;
380     my $msg = "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> $gotret)\n";
381     if ($timeout!=1) {
382         $msg=$msg."Output of <$cmd{'file'}:$cmd{'line'}> so far:\n";
383     }
384     map {$msg .=  "|| $_\n"} @got;
385     if(!@got) {
386         if($timeout==1){
387         print STDERR "<$cmd{'file'}:$cmd{'line'}> No output before timeout\n";
388         }else{
389         $msg .= "||\n";
390         }
391     }
392     $timeout = 0;
393     print STDERR "$msg";
394   }
395
396
397   ###
398   # Check the result of execution
399   ###
400   my $diff;
401   if (defined($cmd{'output display'})){
402     print "[Tesh/INFO] Here is the (ignored) command output:\n";
403     map { print "||$_\n" } @got;
404   }
405   elsif (!defined($cmd{'output ignore'})){
406     $diff = build_diff(\@{$cmd{'out'}}, \@got);
407   }else{
408     print "(ignoring the output of <$cmd{'file'}:$cmd{'line'}> as requested)\n"
409   }
410   if (length $diff) {
411     print "Output of <$cmd{'file'}:$cmd{'line'}> mismatch".($cmd{'sort'}?" (even after sorting)":"").":\n";
412     map { print "$_\n" } split(/\n/,$diff);
413     if ($cmd{'sort'}) {
414         print "WARNING: Both the observed output and expected output were sorted as requested.\n";
415         print "WARNING: Output were only sorted using the $sort_prefix first chars.\n"
416           if ($sort_prefix>0);
417         print "WARNING: Use <! output sort 19> to sort by simulated date and process ID only.\n";
418         # print "----8<---------------  Begin of unprocessed observed output (as it should appear in file):\n";
419         # map {print "> $_\n"} @{$cmd{'unsorted got'}};
420         # print "--------------->8----  End of the unprocessed observed output.\n";
421     }
422
423     print "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> output mismatch)\n";
424     $error=1;
425     $exitcode=2;
426   }
427 }
428
429 sub mkfile_cmd {
430   my %cmd = %{$_[0]};
431   my $file = $cmd{'arg'};
432   print "[Tesh/INFO] mkfile $file\n";
433
434   unlink($file);
435   open(FILE,">$file") or die "[Tesh/CRITICAL] Unable to create file $file: $!\n";
436   print FILE join("\n", @{$cmd{'in'}});
437   print FILE "\n" if (scalar @{$cmd{'in'}} > 0);
438   close(FILE);
439 }
440
441 # parse tesh file
442 #my $teshfile=$tesh_file;
443 #$teshfile=~ s{\.[^.]+$}{};
444
445 unless($tesh_file eq "(stdin)"){
446   open TESH_FILE, $tesh_file or die "[Tesh/CRITICAL] Unable to open $tesh_file $!\n";
447 }
448
449 my %cmd; # everything about the next command to run
450 my $line_num=0;
451 my $finished =0;
452 LINE: while (not $finished and not $error) {
453   my $line;
454
455
456   if ($tesh_file ne "(stdin)" and !defined($line=<TESH_FILE>)){
457     $finished=1;
458     next LINE;
459   }elsif ($tesh_file eq "(stdin)" and !defined($line=<>)){
460     $finished=1;
461     next LINE;
462   }
463
464   $line_num++;
465   chomp $line;
466   $line =~ s/\r//g;
467   print "[TESH/debug] $line_num: $line\n" if $opts{'debug'};
468   my $next;
469   # deal with line continuations
470   while ($line =~ /^(.*?)\\$/) {
471     $next=<TESH_FILE>;
472     die "[TESH/CRITICAL] Continued line at end of file\n"
473       unless defined($next);
474     $line_num++;
475     chomp $next;
476     print "[TESH/debug] $line_num: $next\n" if $opts{'debug'};
477     $line = $1.$next;
478   }
479
480   # Push delayed commands on empty lines
481   unless ($line =~ m/^(.)(.*)$/) {
482     if (defined($cmd{'cmd'}))  {
483       exec_cmd(\%cmd);
484       %cmd = ();
485     }
486     print $diff_tool_tmp_fh "$line\n" if ($diff_tool);
487     next LINE;
488   }
489
490   my ($cmd,$arg) = ($1,$2);
491   print $diff_tool_tmp_fh "$line\n" if ($diff_tool and $cmd ne '>');
492   $arg =~ s/^ //g;
493   $arg =~ s/\r//g;
494   $arg =~ s/\\\\/\\/g;
495   # handle the commands
496   if ($cmd =~ /^#/) {    #comment
497   } elsif ($cmd eq '>'){    #expected result line
498     print "[TESH/debug] push expected result\n" if $opts{'debug'};
499     push @{$cmd{'out'}}, $arg;
500
501   } elsif ($cmd eq '<') {    # provided input
502     print "[TESH/debug] push provided input\n" if $opts{'debug'};
503     push @{$cmd{'in'}}, $arg;
504
505   } elsif ($cmd eq 'p') {    # comment
506     print "[$tesh_name:$line_num] $arg\n";
507
508   } elsif ($cmd eq '$') {  # Command
509     # if we have something buffered, run it now
510     if (defined($cmd{'cmd'})) {
511       exec_cmd(\%cmd);
512       %cmd = ();
513     }
514     if ($arg =~ /^\s*mkfile /){      # "mkfile" command line
515       die "[TESH/CRITICAL] Output expected from mkfile command!\n" if scalar @{cmd{'out'}};
516
517       $cmd{'arg'} = $arg;
518       $cmd{'arg'} =~ s/\s*mkfile //;
519       mkfile_cmd(\%cmd);
520       %cmd = ();
521
522     } elsif ($arg =~ /^\s*cd /) {
523       die "[TESH/CRITICAL] Input provided to cd command!\n" if scalar @{cmd{'in'}};
524       die "[TESH/CRITICAL] Output expected from cd command!\n" if scalar @{cmd{'out'}};
525
526       $arg =~ s/^ *cd //;
527       cd_cmd("",$arg);
528       %cmd = ();
529
530     } else { # regular command
531       $cmd{'cmd'} = $arg;
532       $cmd{'file'} = $tesh_file;
533       $cmd{'line'} = $line_num;
534     }
535   }
536   elsif($cmd eq '&'){      # parallel command line
537
538     if (defined($cmd{'cmd'})) {
539       exec_cmd(\%cmd);
540       %cmd = ();
541     }
542     $cmd{'background'} = 1;
543     $cmd{'cmd'} = $arg;
544     $cmd{'file'} = $tesh_file;
545     $cmd{'line'} = $line_num;
546   }
547   elsif($line =~ /^!\s*output sort/){    #output sort
548     if (defined($cmd{'cmd'})) {
549       exec_cmd(\%cmd);
550       %cmd = ();
551     }
552     $cmd{'sort'} = 1;
553     if ($line =~ /^!\s*output sort\s+(\d+)/) {
554         $sort_prefix = $1;
555     }
556   }
557   elsif($line =~ /^!\s*output ignore/){    #output ignore
558     if (defined($cmd{'cmd'})) {
559       exec_cmd(\%cmd);
560       %cmd = ();
561     }
562     $cmd{'output ignore'} = 1;
563   }
564   elsif($line =~ /^!\s*output display/){    #output display
565     if (defined($cmd{'cmd'})) {
566       exec_cmd(\%cmd);
567       %cmd = ();
568     }
569     $cmd{'output display'} = 1;
570   }
571   elsif($line =~ /^!\s*expect signal (\w*)/) {#expect signal SIGABRT
572     if (defined($cmd{'cmd'})) {
573       exec_cmd(\%cmd);
574       %cmd = ();
575     }
576 print "hey\n";
577     $cmd{'expect'} = "$1";
578   }
579   elsif($line =~ /^!\s*expect return/){    #expect return
580     if (defined($cmd{'cmd'})) {
581       exec_cmd(\%cmd);
582       %cmd = ();
583     }
584     $line =~ s/^! expect return //g;
585     $line =~ s/\r//g;
586     $cmd{'return'} = $line;
587   }
588   elsif($line =~ /^!\s*setenv/){    #setenv
589     if (defined($cmd{'cmd'})) {
590       exec_cmd(\%cmd);
591       %cmd = ();
592     }
593     $line =~ s/^! setenv //g;
594     $line =~ s/\r//g;
595     setenv_cmd($line);
596   }
597   elsif($line =~ /^!\s*include/){    #include
598     if (defined($cmd{'cmd'})) {
599       exec_cmd(\%cmd);
600       %cmd = ();
601     }
602     print color("red"), "[Tesh/CRITICAL] need include";
603     print color("reset"), "\n";
604     die;
605   }
606   elsif($line =~ /^!\s*timeout/){    #timeout
607     if (defined($cmd{'cmd'})) {
608       exec_cmd(\%cmd);
609       %cmd = ();
610     }
611     $line =~ s/^! timeout //;
612     $line =~ s/\r//g;
613     $cmd{'timeout'} = $line;
614   } else {
615     die "[TESH/CRITICAL] parse error: $line\n";
616   }
617   if($forked){
618    kill(SIGKILL, $forked);
619    $timeout=0;
620   }
621
622 }
623
624
625
626 # Deal with last command
627 if (defined($cmd{'cmd'})) {
628   exec_cmd(\%cmd);
629   %cmd = ();
630 }
631
632
633 if($forked){
634    kill(SIGKILL, $forked);
635    $timeout=0;
636 }
637
638 foreach(@bg_cmds){
639   my %test=%{$_};
640   waitpid ($test{'pid'}, 0);
641   $test{'gotret'} = exit_status($?);
642   parse_out(\%test);
643 }
644
645 @bg_cmds=();
646
647 if ($diff_tool) {
648   close $diff_tool_tmp_fh;
649   system("$diff_tool $diff_tool_tmp_filename $tesh_file");
650   unlink $diff_tool_tmp_filename;
651 }
652
653 if($error !=0){
654     exit $exitcode;
655 }elsif($tesh_file eq "(stdin)"){
656     print "Test suite from stdin OK\n";
657 }else{
658     print "Test suite `$tesh_name' OK\n";
659 }
660
661 #my (@a,@b);
662 #push @a,"bl1";   push @b,"bl1";
663 #push @a,"bl2";   push @b,"bl2";
664 #push @a,"bl3";   push @b,"bl3";
665 #push @a,"bl4";   push @b,"bl4";
666 #push @a,"bl5";   push @b,"bl5";
667 #push @a,"bl6";   push @b,"bl6";
668 #push @a,"bl7";   push @b,"bl7";
669 ##push @a,"Perl";  push @b,"ruby";
670 #push @a,"END1";   push @b,"END1";
671 #push @a,"END2";   push @b,"END2";
672 #push @a,"END3";   push @b,"END3";
673 #push @a,"END4";   push @b,"END4";
674 #push @a,"END5";   push @b,"END5";
675 #push @a,"END6";   push @b,"END6";
676 #push @a,"END7";   push @b,"END7";
677 #print "Identical:\n". build_diff(\@a,\@b);
678
679 #@a = (); @b =();
680 #push @a,"AZE"; push @b,"EZA";
681 #print "Different:\n".build_diff(\@a,\@b);
682
683 use lib "@CMAKE_BINARY_DIR@/bin" ;
684
685 use Diff qw(diff); # postpone a bit to have time to change INC
686
687 sub build_diff {
688   my $res;
689   my $diff = Diff->new(@_);
690
691   $diff->Base( 1 );   # Return line numbers, not indices
692   my $chunk_count = $diff->Next(-1); # Compute the amount of chuncks
693   return ""   if ($chunk_count == 1 && $diff->Same());
694   $diff->Reset();
695   while(  $diff->Next()  ) {
696     my @same = $diff->Same();
697     if ($diff->Same() ) {
698       if ($diff->Next(0) > 1) { # not first chunk: print 2 first lines
699         $res .= '  '.$same[0]."\n" ;
700         $res .= '  '.$same[1]."\n" if (scalar @same>1);
701       }
702       $res .= "...\n"  if (scalar @same>2);
703 #    $res .= $diff->Next(0)."/$chunk_count\n";
704       if ($diff->Next(0) < $chunk_count) { # not last chunk: print 2 last lines
705         $res .= '  '.$same[scalar @same -2]."\n" if (scalar @same>1);
706         $res .= '  '.$same[scalar @same -1]."\n";
707       }
708     }
709     next if  $diff->Same();
710     map { $res .= "- $_\n" } $diff->Items(1);
711     map { $res .= "+ $_\n" } $diff->Items(2);
712   }
713   return $res;
714 }
715
716