File:  [LON-CAPA] / loncom / lonmaxima
Revision 1.1: download - view: text, annotated - select for diffs
Fri Mar 3 16:07:34 2006 UTC (18 years, 2 months ago) by www
Branches: MAIN
CVS tags: HEAD
Playing around with Computer Algebra System

    1: #!/usr/bin/perl
    2: #
    3: # The LearningOnline Network with CAPA
    4: # Connect to MAXIMA CAS
    5: #
    6: # $Id: lonmaxima,v 1.1 2006/03/03 16:07:34 www Exp $
    7: #
    8: # Copyright Michigan State University Board of Trustees
    9: #
   10: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   11: #
   12: # LON-CAPA is free software; you can redistribute it and/or modify
   13: # it under the terms of the GNU General Public License as published by
   14: # the Free Software Foundation; either version 2 of the License, or
   15: # (at your option) any later version.
   16: #
   17: # LON-CAPA is distributed in the hope that it will be useful,
   18: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   19: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   20: # GNU General Public License for more details.
   21: #
   22: # You should have received a copy of the GNU General Public License
   23: # along with LON-CAPA; if not, write to the Free Software
   24: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   25: #
   26: # /home/httpd/html/adm/gpl.txt
   27: #
   28: 
   29: # 
   30: # http://www.lon-capa.org/
   31: #
   32:  
   33: use IPC::Open3;
   34: use IO::Select;
   35: # Scary: cannot use strict!!!
   36: ##### use strict;
   37: 
   38: sub maximareply {
   39:     my $cmd=shift;
   40:     my $reply='';
   41:     my $error='';
   42:     my $exitstatus='';
   43: 
   44:     unless ($cmd=~/\;\n$/) { $cmd.=";\n"; }
   45:     my $pid = open3($cmd_in, $cmd_out, $cmd_err, 'maxima');
   46:     
   47:     $SIG{CHLD} = sub {
   48: 	$exitstatus="$? on $pid\n" if waitpid($pid, 0) > 0;
   49:     };
   50: 
   51:     print $cmd_in $cmd;
   52:     close $cmd_in;
   53: 
   54:     my $selector = IO::Select->new( );
   55:     $selector->add($cmd_err, $cmd_out);
   56:     
   57:     while (my @ready = $selector->can_read) {
   58: 	foreach my $fh (@ready) {
   59: 	    if (fileno($fh) == fileno($cmd_err)) {
   60: 		$error.=<$cmd_err>;
   61: 	    } else {
   62: 		my $line = scalar <$cmd_out>;
   63:                 if ($line=~/^(\(\%o|\s)/) {
   64:                    $line=~s/^\(.*\)/     /; 
   65:                    $reply.=$line; 
   66: 	       }
   67: 	    }
   68: 	    $selector->remove($fh) if eof($fh);
   69: 	}
   70:     }
   71:     close $cmd_out;
   72:     close $cmd_err;
   73:     return ($reply,$error,$exitstatus);
   74: }
   75: 
   76: print join("\n----\n",&maximareply('1234'));
   77: print join("\n----\n",&maximareply('x0: 5;x1: 7;integrate (x^2, x, x0, x1);'));

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>