Logo AND Algorithmique Numérique Distribuée

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