version 1.13, 2007/04/19 17:40:43
|
version 1.38, 2017/06/12 00:22:35
|
Line 51 sub maximareply {
|
Line 51 sub maximareply {
|
print $socket &escape($cmd)."\n"; |
print $socket &escape($cmd)."\n"; |
my $reply=<$socket>; |
my $reply=<$socket>; |
chomp($reply); |
chomp($reply); |
if ($reply=~/^Incorrect/) { $reply='Error: '.$reply; } |
if ($reply=~/^Incorrect/i) { $reply='Error: '.$reply; } |
return &unescape($reply); |
return &unescape($reply); |
} else { |
} else { |
return 'Error: no connection.'; |
return 'Error: no connection.'; |
Line 60 sub maximareply {
|
Line 60 sub maximareply {
|
|
|
sub blacklisted { |
sub blacklisted { |
my ($cmd)=@_; |
my ($cmd)=@_; |
foreach my $forbidden ('save','load','plot','lisp','includ','compil','file','batch','stringout','translat','stout','stin','block','system') { |
foreach my $forbidden ( |
|
'\? ','\?','%i\d+','%o','batch','block' |
|
,'compil','concat','describe','display2d','file','inchar' |
|
,'includ','lisp','load','outchar','plot','quit' |
|
,'read','reset','save','stin','stout','stringout' |
|
,'system','translat','ttyoff','with_stdout','writefile' |
|
) { |
if ($cmd=~/$forbidden/s) { return 1; } |
if ($cmd=~/$forbidden/s) { return 1; } |
} |
} |
return 0; |
return 0; |
} |
} |
|
|
|
sub maxima_allowed_libraries { |
|
return ( |
|
"absimp","affine","atensor","atrig1","augmented_lagrangian","contrib_ode","ctensor","descriptive","diag", |
|
"eigen","facexp","fft","fourie","functs","ggf","grobner","impdiff","ineq","interpol","itensor","lapack", |
|
"lbfgs","lindstedt","linearalgebra","lsquares","makeOrders","mnewton","mchrpl","ntrig","orthopoly", |
|
"quadpack","rducon","romberg","scifac","simplex","solve_rec","sqdnst","stats","sterling","sym","units", |
|
"vect","zeilberger"); |
|
} |
|
|
|
sub maxima_is_allowed_library { |
|
my ($library)=@_; |
|
foreach my $allowed_library (&maxima_allowed_libraries()) { |
|
if ($library eq $allowed_library) { return 1; } |
|
} |
|
return 0; |
|
} |
|
|
sub runscript { |
sub runscript { |
my ($socket,$fullscript)=@_; |
my ($socket,$fullscript,$libraries)=@_; |
if (&blacklisted($fullscript)) { return 'Error: blacklisted'; } |
if (&blacklisted($fullscript)) { return 'Error: blacklisted'; } |
my $reply; |
my $reply; |
$fullscript=~s/[\n\r\l]//gs; |
$fullscript=~s/[\n\r\l]//gs; |
|
if ($libraries) { |
|
foreach my $library (split(/\s*\,\s*/,$libraries)) { |
|
unless ($library=~/\w/) { next; } |
|
if (&maxima_is_allowed_library($library)) { |
|
$reply=&maximareply($socket,'load('.$library.')$'."\n"); |
|
if ($reply=~/^Error\:/) { return $reply; } |
|
} else { |
|
return 'Error: blacklisted'; |
|
} |
|
} |
|
} |
foreach my $line (split(/\;/s,$fullscript)) { |
foreach my $line (split(/\;/s,$fullscript)) { |
if ($line=~/\w/) { $reply=&maximareply($socket,$line.";\n"); } |
if ($line=~/\w/) { $reply=&maximareply($socket,$line.";\n"); } |
if ($reply=~/^Error\:/) { return $reply; } |
if ($reply=~/^Error\:/) { return $reply; } |
} |
} |
$reply=~s/\W//gs; |
$reply=~s/^\s*//gs; |
|
$reply=~s/\s*$//gs; |
|
&Apache::lonxml::debug("maxima $fullscript \n reply $reply"); |
return $reply; |
return $reply; |
} |
} |
|
|
sub maxima_cas_formula_fix { |
sub maxima_cas_formula_fix { |
my ($expression)=@_; |
my ($expression)=@_; |
return &Apache::response::implicit_multiplication($expression); |
$expression=&Apache::response::implicit_multiplication($expression); |
|
$expression=~s/(^|[^\w\%])pi(\W|$)/$1\%pi$2/gs; |
|
return $expression; |
} |
} |
|
|
sub maxima_run { |
sub maxima_run { |
my ($script,$submission,$argument) = @_; |
my ($script,$submission,$argument,$libraries) = @_; |
my $socket=&connect(); |
my $socket=&connect(); |
my @submissionarray=split(/\s*\,\s*/,$submission); |
my @submissionarray=split(/\s*\,\s*/,$submission); |
for (my $i=0;$i<=$#submissionarray;$i++) { |
for (my $i=0;$i<=$#submissionarray;$i++) { |
Line 99 sub maxima_run {
|
Line 137 sub maxima_run {
|
my $fixedargument=&maxima_cas_formula_fix($argumentarray[$i]); |
my $fixedargument=&maxima_cas_formula_fix($argumentarray[$i]); |
$script=~s/LONCAPALIST\[$n\]/$fixedargument/gs; |
$script=~s/LONCAPALIST\[$n\]/$fixedargument/gs; |
} |
} |
my $reply=&runscript($socket,$script); |
my $reply=&runscript($socket,$script,$libraries); |
&disconnect($socket); |
&disconnect($socket); |
if ($reply=~/^\s*true\s*$/i) { return 'EXACT_ANS'; } |
if ($reply=~/^\s*true\s*$/i) { return 'EXACT_ANS'; } |
if ($reply=~/^\s*false\s*$/i) { return 'INCORRECT'; } |
if ($reply=~/^\s*false\s*$/i) { return 'INCORRECT'; } |
Line 107 sub maxima_run {
|
Line 145 sub maxima_run {
|
} |
} |
|
|
sub maxima_eval { |
sub maxima_eval { |
my ($script) = @_; |
my ($script,$libraries) = @_; |
my $socket=&connect(); |
my $socket=&connect(); |
my $reply=&runscript($socket,$script); |
my $reply=&runscript($socket,$script,$libraries); |
&disconnect($socket); |
&disconnect($socket); |
return $reply; |
return $reply; |
} |
} |
Line 117 sub maxima_eval {
|
Line 155 sub maxima_eval {
|
|
|
sub compareterms { |
sub compareterms { |
my ($socket,$terma,$termb)=@_; |
my ($socket,$terma,$termb)=@_; |
my $difference=$terma.'-'.$termb; |
my $difference='('.$terma.')-('.$termb.')'; |
if (&blacklisted($difference)) { return 'Error: blacklisted'; } |
if (&blacklisted($difference)) { return 'Error: blacklisted'; } |
my $reply=&maximareply($socket,'trigsimp(trigreduce('.$difference.'));'); |
my $reply=&maximareply($socket,'trigsimp(trigreduce('.$difference.'));'); |
if ($reply=~/^\s*0\s*$/) { return 'true'; } |
if ($reply=~/^\s*0\s*$/) { return 'true'; } |
Line 128 sub compareterms {
|
Line 166 sub compareterms {
|
sub maxima_check { |
sub maxima_check { |
my ($response,$answer,$reterror) = @_; |
my ($response,$answer,$reterror) = @_; |
my $socket=&connect(); |
my $socket=&connect(); |
my $reply=&compareterms($socket,$response,$answer); |
my $reply=&maximareply($socket,$response.';'); |
|
unless ($reply=~/^Error\:/) { |
|
$reply=&compareterms($socket,$response,$answer); |
|
} |
&disconnect($socket); |
&disconnect($socket); |
|
# integer to string mappings come from capaParser.h |
|
# 1 maps to 'EXACT_ANS' |
if ($reply eq 'true') { return 1; } |
if ($reply eq 'true') { return 1; } |
|
# 11 maps to 'BAD_FORMULA' |
|
if ($reply=~/^Error\:/) { return 11; } |
|
# 7 maps to 'INCORRECT' |
return 7; |
return 7; |
} |
} |
|
|