--- loncom/homework/lonmaxima.pm	2006/03/11 16:28:03	1.1
+++ loncom/homework/lonmaxima.pm	2007/05/29 20:56:39	1.15
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Interface routines to MAXIMA CAS
 #
-# $Id: lonmaxima.pm,v 1.1 2006/03/11 16:28:03 www Exp $
+# $Id: lonmaxima.pm,v 1.15 2007/05/29 20:56:39 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -31,6 +31,8 @@ package Apache::lonmaxima;
 use strict;
 use IO::Socket;
 use Apache::lonnet;
+use Apache::response();
+use LONCAPA;
 
 sub connect {
    return IO::Socket::UNIX->new(Peer    => $Apache::lonnet::perlvar{'lonSockDir'}.'/maximasock',
@@ -46,11 +48,11 @@ sub disconnect {
 sub maximareply {
     my ($socket,$cmd)=@_;
     if ($socket) {
-	print $socket &Apache::lonnet::escape($cmd)."\n";
+	print $socket &escape($cmd)."\n";
         my $reply=<$socket>;
         chomp($reply);
         if ($reply=~/^Incorrect/) { $reply='Error: '.$reply; }
-        return &Apache::lonnet::unescape($reply);
+        return &unescape($reply);
     } else {
         return 'Error: no connection.';
     }
@@ -58,12 +60,65 @@ sub maximareply {
 
 sub blacklisted {
     my ($cmd)=@_;
-    foreach my $forbidden ('save','load','plot','lisp','includ','compil','file','batch','stringout','translat','stout','stin','block','system') {
-	if ($cmd=~/$forbidden/) { return 1; }
+    foreach my $forbidden ('save','load','plot','lisp','includ','compil',
+			   'file','batch','stringout','translat','stout',
+			   'stin','block','system','concat') {
+	if ($cmd=~/$forbidden/s) { return 1; }
     } 
     return 0;
 }
 
+sub runscript {
+    my ($socket,$fullscript)=@_;
+    if (&blacklisted($fullscript)) { return 'Error: blacklisted'; }
+    my $reply;
+    $fullscript=~s/[\n\r\l]//gs;
+    foreach my $line (split(/\;/s,$fullscript)) {
+	if ($line=~/\w/) { $reply=&maximareply($socket,$line.";\n"); }
+	if ($reply=~/^Error\:/) { return $reply; }
+    }
+    $reply=~s/^\s*//gs;
+    $reply=~s/\s*$//gs;
+    &Apache::lonxml::debug("maxima $fullscript \n reply $reply");
+    return $reply;
+}
+
+sub maxima_cas_formula_fix {
+   my ($expression)=@_;
+   return &Apache::response::implicit_multiplication($expression);
+}
+
+sub maxima_run {
+    my ($script,$submission,$argument) = @_;
+    my $socket=&connect();
+    my @submissionarray=split(/\s*\,\s*/,$submission);
+    for (my $i=0;$i<=$#submissionarray;$i++) {
+        my $n=$i+1;
+        my $fixedsubmission=&maxima_cas_formula_fix($submissionarray[$i]);
+        $script=~s/RESPONSE\[$n\]/$fixedsubmission/gs;
+    }
+    my @argumentarray=@{$argument};
+    for (my $i=0;$i<=$#argumentarray;$i++) {
+        my $n=$i+1;
+        my $fixedargument=&maxima_cas_formula_fix($argumentarray[$i]);
+        $script=~s/LONCAPALIST\[$n\]/$fixedargument/gs;
+    }
+    my $reply=&runscript($socket,$script);
+    &disconnect($socket);
+    if ($reply=~/^\s*true\s*$/i) { return 'EXACT_ANS'; }
+    if ($reply=~/^\s*false\s*$/i) { return 'INCORRECT'; } 
+    return 'BAD_FORMULA';
+}
+
+sub maxima_eval {
+    my ($script) = @_;
+    my $socket=&connect();
+    my $reply=&runscript($socket,$script);
+    &disconnect($socket);
+    return $reply;
+}
+
+
 sub compareterms {
     my ($socket,$terma,$termb)=@_;
     my $difference=$terma.'-'.$termb;
@@ -73,6 +128,15 @@ sub compareterms {
     if ($reply=~/^Error\:/) { return $reply; }
     return 'false';
 }
+
+sub maxima_check {
+    my ($response,$answer,$reterror) = @_;
+    my $socket=&connect();
+    my $reply=&compareterms($socket,$response,$answer);
+    &disconnect($socket);
+    if ($reply eq 'true') { return 1; }
+    return 7;
+}
  
 1;
 __END__;