2 eval 'exec perl -S $0 ${1+"$@"}'
3 if $running_under_some_shell;
13 B<tesh> [I<options>] I<tesh_file>
17 use Pod::Usage qw(pod2usage);
18 use Getopt::Long qw(GetOptions);
24 # make sure we received a tesh file
25 scalar @ARGV > 0 || pod2usage(-exitval => 1);
27 #Add current directory to path
28 $ENV{PATH} = "$ENV{PATH}:.";
32 ## Command line option handling
35 # option handling helper subs
40 print "[Tesh/INFO] change directory to $directory\n";
42 die "[Tesh/CRITICAL] Directory not found : \"$directory\"\n";
47 if ($_[1] =~ /^(.*)=(.*)$/) {
48 my($var,$ctn)=($1,$2);
50 print "[Tesh/INFO] setenv $var=$ctn\n";
52 die "[Tesh/CRITICAL] Malformed argument to setenv: expected 'name=value' but got '$_[1]'\n";
56 # Main option parsing sub
59 # remove the tesh file from the ARGV used
61 $tesh_file = pop @ARGV;
63 # temporary arrays for GetOption
73 Getopt::Long::config('bundling', 'no_getopt_compat', 'no_auto_abbrev');
76 'help|h' => \$opt{'help'},
78 'verbose|v' => \@verbose,
79 'debug|d' => \$opt{"debug"},
82 'setenv=s' => \&setenv_cmd,
86 $opt{'verbose'} = scalar @verbose;
88 $opt{'cfg'} .= " --cfg=$_";
93 my %opts = get_options(@ARGV);
98 my($line2,$execline,$command,$command_tesh);
99 my($command_executed)=0;
100 my($expected_result_line)=0;
103 my(@list1,@list2,@list3,@list_of_commands)=();
110 my($no_output_ignore)=1;
124 open SH_LIGNE, $tesh_file or die "[Tesh/CRITICAL] Unable to open $tesh_file $!\n";
126 while (defined(my $line1=<SH_LIGNE>)) {
127 if($line1 =~ /^\< \$ /){ # arg command line
128 $line1 =~ s/\${EXEEXT:=}//g;
129 $line1 =~ s/^\< \$\ *//g;
130 $line1 =~ s/^.\/lua/lua/g;
131 $line1 =~ s/^.\/ruby/ruby/g;
133 $line1 =~ s/^tesh/.\/tesh/g;
134 $line1 =~ s/\(%i:%P@%h\)/\\\(%i:%P@%h\\\)/g;
136 $command_tesh = $line1;
137 print "[Tesh/INFO] arg_exec_line : $command_tesh\n";
139 elsif($line1 =~ /^\< \< /){ # arg buffer line
140 $line1 =~ s/^\< \<\ *//g;
142 print "[Tesh/INFO] arg_buffer_line : $line1\n";
143 push @buffer_tesh, "$line1\n";
145 elsif($line1 =~ /^\< \> /){ # arg output line
146 $line1 =~ s/^\< \>\ *//g;
150 print "[Tesh/INFO] arg_output_line : $line1\n";
151 $expected_result_line = 1;
153 elsif($line1 =~ /^\$ mkfile/){ # "mkfile" command line
155 $line1 =~ s/mkfile//g;
159 print "[Tesh/INFO] exec_line : mkfile $fich_name\n";
161 open(FILE,">$fich_name") or die "[Tesh/CRITICAL] Unable to make file : $fich_name. $!\n";
166 elsif($line1 =~ /^\$ cd/){ # "cd" command line
169 print "[Tesh/INFO] exec_line : $line1\n";
171 chdir("$line1") or die "[Tesh/CRITICAL] Unable to open $line1. $!\n";
173 elsif($line1 =~ /^\$ /){ #command line
174 if($line1 =~ /^\$ .\/tesh/){ # tesh command line
176 @buffer = @buffer_tesh;
179 $line1 =~ s/\${EXEEXT:=}//g;
180 $line1 =~ s/^\$\ *//g;
181 $line1 =~ s/^.\/lua/lua/g;
182 $line1 =~ s/^.\/ruby/ruby/g;
184 $line1 =~ s/^tesh/.\/tesh/g;
185 $line1 =~ s/\(%i:%P@%h\)/\\\(%i:%P@%h\\\)/g;
187 $line1 = "$line1 $config";
191 print "[Tesh/CRITICAL] -\n";
192 print "[Tesh/CRITICAL] + @list1";
193 print color("reset"), "\n";
195 if(@list_of_commands){ # need parallel execution
196 push @list_of_commands, $line1;
197 print "[Tesh/INFO] exec_line : $line1\n";
200 print "[Tesh/INFO] exec_line : $line1\n";
202 { $execline = $command_tesh;
205 { $execline = $line1;}
206 $pid = open3(\*IN, \*OUT, \*OUT, $execline );
208 $forked = fork();die "fork() failed: $!" unless defined $forked;
219 $line1 = shift (@buffer);
225 if($timeout){kill(9, $forked);$timeout=0;}
228 while(defined($linebis=<OUT>))
231 $linebis =~ s/^( )*//g;
233 push @list1,"$linebis";
236 $command_executed = 1;
239 elsif($line1 =~ /^\& /){ # parallel command line
240 $command_executed = 0;
241 $expected_result_line = 0;
242 $line1 =~ s/\${EXEEXT:=}//g;
244 $line1 =~ s/^.\/lua/lua/g;
245 $line1 =~ s/^.\/ruby/ruby/g;
247 $line1 =~ s/\(%i:%P@%h\)/\\\(%i:%P@%h\\\)/g;
249 $line1 = "$line1 $config";
251 $execline = "$line1";
252 print "[Tesh/INFO] exec_line : $execline\n";
253 push @list_of_commands, $execline;
255 elsif($line1 =~ /^\>/){ #expected result line
256 $line1 =~ s/^\>( )*//g;
260 $expected_result_line = 1;
262 elsif($line1 =~ /^\</ or $encore==1){ #need to buffer
263 $line1 =~ s/^\<( )*//g; #delete < with space or tab after
266 if($line1 =~ /\\$/) # need to store this line into old_buff
270 $old_buffer = "$old_buffer$line1";
274 if($encore == 1){push @buffer, "$old_buffer$line1";}
275 else{push @buffer, "$line1\n";}
280 elsif($line1 =~ /^p/){ #comment
284 print "[Tesh/INFO] comment_line :$line1\n";
286 elsif($line1 =~ /^! output sort/){ #output sort
289 elsif($line1 =~ /^! output ignore/){ #output ignore
292 elsif($line1 =~ /^! expect signal SIGABRT$/) #expect signal SIGABRT
296 elsif($line1 =~ /^! expect return/){ #expect return
297 $line1 =~ s/^! expect return //g;
301 print color("red"), "[Tesh/INFO] expect return $return";
302 print color("reset"), "\n";
305 elsif($line1 =~ /^! setenv/){ #setenv
306 $line1 =~ s/^! setenv //g;
309 $line1 =~ /^(.*)=(.*)$/;
311 print "[Tesh/INFO] setenv: $1=$2\n";
313 elsif($line1 =~ /^! include/){ #output sort
314 print color("red"), "[Tesh/CRITICAL] need include";
315 print color("reset"), "\n";
318 elsif($line1 =~ /^! timeout/){ #timeout
319 $line1 =~ s/^! timeout //g;
322 if($timeout != $line1){
324 print "[Tesh/INFO] timeout : $timeout\n";}
326 elsif($line1 =~ /^! need execute/){ #need execute // commands
328 $sort = 1; # need sort output
329 while(@list_of_commands)
331 $command = shift (@list_of_commands);
332 if($execline){$execline = "$command & $execline";}
333 else{$execline = "$command";}
335 $pid = open3(\*IN, \*OUT, \*OUT, $execline);
337 $forked = fork();die "fork() failed: $!" unless defined $forked;
348 $line1 = shift (@buffer);
353 if($timeout){kill(9, $forked);$timeout=0;}
357 while(defined($linebis=<OUT>))
360 $linebis =~ s/^( )*//g;
362 push @list1,"$linebis";
365 $command_executed = 1;
367 elsif($command_executed and $expected_result_line)
369 if($no_output_ignore){
373 @list3 = sort @list1;
378 @list3 = sort @list2;
387 push @list2,"Aborted";
391 while(@list1 or @list2)
393 if(@list1){$line1 = shift (@list1);$expected_result_line = "x$line1";}
394 if(@list2){$line2 = shift (@list2);$command_executed = "x$line2";}
395 if($command_executed and $expected_result_line)
398 if($line1 eq $line2){
399 if($verbose == 1){print color("green"),"[Tesh/VERB] $line1\n",color("reset");}
400 else{push @buffer, "[Tesh/CRITICAL] $line1\n";}
404 { if($verbose == 0){print color("green"),@buffer,color("reset");}
405 if($line2) {print color("red"), "[Tesh/CRITICAL] - $line2",color("reset"),"\n";}
406 if($line1) {print color("red"), "[Tesh/CRITICAL] + $line1",color("reset"),"\n";}
411 { if($verbose == 0){print color("green"),@buffer,color("reset");}
412 if($line2) {print color("red"), "[Tesh/CRITICAL] - $line2",color("reset"),"\n";}
413 if($line1) {print color("red"), "[Tesh/CRITICAL] + $line1",color("reset"),"\n";}
417 }else{$no_output_ignore = 1;}
418 $command_executed = 0;
419 $expected_result_line = 0;
429 if(@list_of_commands){ # need parallel execution
431 $sort = 1; # need sort output
432 while(@list_of_commands)
434 $command = shift (@list_of_commands);
435 if($execline){$execline = "$command & $execline";}
436 else{$execline = "$command";}
438 print "[Tesh/INFO] exec_line : $execline\n";
439 $pid = open3(\*IN, \*OUT, \*OUT,"$execline");
442 $forked = fork();die "fork() failed: $!" unless defined $forked;
453 $line1 = shift (@buffer);
458 if($timeout){kill(9, $forked);$timeout=0;}
462 while(defined($linebis=<OUT>))
465 $linebis =~ s/^( )*//g;
467 push @list1,"$linebis";
470 $command_executed = 1;
473 if($command_executed and $expected_result_line ){
474 if($no_output_ignore){
478 @list3 = sort @list1;
483 @list3 = sort @list2;
492 push @list2,"Aborted";
496 while(@list1 or @list2)
498 if(@list1){$line1 = shift (@list1);$expected_result_line = "x$line1";}
499 if(@list2){$line2 = shift (@list2);$command_executed = "x$line2";}
500 if($command_executed and $expected_result_line)
502 if($line1 eq $line2){
503 if($verbose == 1){print color("green"),"[Tesh/VERB] $line1\n",color("reset");}
504 else{push @buffer, "[Tesh/CRITICAL] $line1\n";}
508 { if($verbose == 0){print color("green"),@buffer,color("reset");}
509 if($line2) {print color("red"), "[Tesh/CRITICAL] - $line2",color("reset"),"\n";}
510 if($line1) {print color("red"), "[Tesh/CRITICAL] + $line1",color("reset"),"\n";}
515 { if($verbose == 0){print color("green"),@buffer,color("reset");}
516 if($line2) {print color("red"), "[Tesh/CRITICAL] - $line2",color("reset"),"\n";}
517 if($line1) {print color("red"), "[Tesh/CRITICAL] + $line1",color("reset"),"\n";}
521 }else{$no_output_ignore = 1;}
522 $command_executed = 0;
523 $expected_result_line= 0;