--- loncom/homework/lonmaxima.pm	2008/06/14 16:34:54	1.23
+++ loncom/homework/lonmaxima.pm	2017/03/07 18:28:02	1.37
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Interface routines to MAXIMA CAS
 #
-# $Id: lonmaxima.pm,v 1.23 2008/06/14 16:34:54 www Exp $
+# $Id: lonmaxima.pm,v 1.37 2017/03/07 18:28:02 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -51,7 +51,7 @@ sub maximareply {
 	print $socket &escape($cmd)."\n";
         my $reply=<$socket>;
         chomp($reply);
-        if ($reply=~/^Incorrect/) { $reply='Error: '.$reply; }
+        if ($reply=~/^Incorrect/i) { $reply='Error: '.$reply; }
         return &unescape($reply);
     } else {
         return 'Error: no connection.';
@@ -60,11 +60,13 @@ sub maximareply {
 
 sub blacklisted {
     my ($cmd)=@_;
-    foreach my $forbidden ('save','load','plot','lisp','includ','compil',
-			   'file','batch','stringout','translat','stout',
-			   'stin','block','system','concat','read','inchar',
-			   'outchar','ttyoff','with_stdout','writefile',
-			   'reset') {
+    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; }
     } 
     return 0;
@@ -115,7 +117,9 @@ sub runscript {
 
 sub maxima_cas_formula_fix {
    my ($expression)=@_;
-   return &Apache::response::implicit_multiplication($expression);
+   $expression=&Apache::response::implicit_multiplication($expression);
+   $expression=~s/([^\w\%])pi/$1\%pi/gs;
+   return $expression;
 }
 
 sub maxima_run {
@@ -151,7 +155,7 @@ sub maxima_eval {
 
 sub compareterms {
     my ($socket,$terma,$termb)=@_;
-    my $difference=$terma.'-('.$termb.')';
+    my $difference='('.$terma.')-('.$termb.')';
     if (&blacklisted($difference)) { return 'Error: blacklisted'; }
     my $reply=&maximareply($socket,'trigsimp(trigreduce('.$difference.'));');
     if ($reply=~/^\s*0\s*$/) { return 'true'; }
@@ -162,7 +166,10 @@ sub compareterms {
 sub maxima_check {
     my ($response,$answer,$reterror) = @_;
     my $socket=&connect();
-    my $reply=&compareterms($socket,$response,$answer);
+    my $reply=&maximareply($socket,$response.';');
+    unless ($reply=~/^Error\:/) {
+        $reply=&compareterms($socket,$response,$answer);
+    }
     &disconnect($socket);
     # integer to string mappings come from capaParser.h
     # 1 maps to 'EXACT_ANS'