--- loncom/lonr 2009/04/18 23:43:47 1.5 +++ loncom/lonr 2014/11/16 15:35:27 1.10 @@ -3,7 +3,7 @@ # The LearningOnline Network with CAPA # Connect to R CAS # -# $Id: lonr,v 1.5 2009/04/18 23:43:47 www Exp $ +# $Id: lonr,v 1.10 2014/11/16 15:35:27 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -275,9 +275,9 @@ sub make_new_child { &status('Accepting connections'); my $client = $server->accept() or last; &sync($command); -# print $command ("display2d:false;simp:true;kill(all);\n"); -# &getroutput($command,2); -# &sync($command); + print $command ("library(phpSerialize);\n"); + &getroutput($command); + &sync($command); my $syntaxerr = 0; while (my $cmd=<$client>) { &status('Processing command'); @@ -316,11 +316,16 @@ sub make_new_child { sub sync { my ($command)=@_; $counter++; + my $digits = length($counter); + if ($digits > 10) { + $counter = 1; + } my $expect=$counter; - print $command "$expect;\n"; + print $command "print($expect,digits=$digits);\n"; while (1) { my $output=&getroutput($command); - if (($output=~/\Q$expect\E/) || ($output=~/^Error\:/)) { + chomp($output); + if (($output=~/^\Q$expect\E/) || ($output=~/^Error\:/)) { return; } } @@ -329,7 +334,7 @@ sub make_new_child { sub getroutput { my ($command)=@_; - my $regexp = '>'; + my $regexp = '>\s+$'; my $syntaxerr=0; my $timeout = 20; my (undef,$error,$matched,$output) = @@ -346,13 +351,12 @@ sub getroutput { return 'Error: '.$error; } - my $foundoutput=0; - my $found_label=0; my $realoutput=''; foreach my $line (split(/\n/,$output)) { - $line=~s/\s$//gs; - if ($line=~/^Error\:/) { $syntaxerr=1; next; } - if (my ($result)=($line=~/^\[?\d+\,*\]?\s*(.*)/)) { $realoutput.=$result."\n"; } + $line=~s/\s$//gs; + if ($line=~/^Error\:/) { $syntaxerr=1; next; } + if ($line=~ /\;$/) { next; } + if (my ($result)=($line=~/^\s*\[?\d+\,?\]?\s*(.*)/)) { $realoutput.=$result."\n"; } } if (wantarray) { return ($realoutput,$syntaxerr);