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, 1 month ago) by www
Branches: MAIN
CVS tags: HEAD
Playing around with Computer Algebra System

#!/usr/bin/perl
#
# The LearningOnline Network with CAPA
# Connect to MAXIMA CAS
#
# $Id: lonmaxima,v 1.1 2006/03/03 16:07:34 www Exp $
#
# Copyright Michigan State University Board of Trustees
#
# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
#
# LON-CAPA is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# LON-CAPA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LON-CAPA; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# /home/httpd/html/adm/gpl.txt
#

# 
# http://www.lon-capa.org/
#
 
use IPC::Open3;
use IO::Select;
# Scary: cannot use strict!!!
##### use strict;

sub maximareply {
    my $cmd=shift;
    my $reply='';
    my $error='';
    my $exitstatus='';

    unless ($cmd=~/\;\n$/) { $cmd.=";\n"; }
    my $pid = open3($cmd_in, $cmd_out, $cmd_err, 'maxima');
    
    $SIG{CHLD} = sub {
	$exitstatus="$? on $pid\n" if waitpid($pid, 0) > 0;
    };

    print $cmd_in $cmd;
    close $cmd_in;

    my $selector = IO::Select->new( );
    $selector->add($cmd_err, $cmd_out);
    
    while (my @ready = $selector->can_read) {
	foreach my $fh (@ready) {
	    if (fileno($fh) == fileno($cmd_err)) {
		$error.=<$cmd_err>;
	    } else {
		my $line = scalar <$cmd_out>;
                if ($line=~/^(\(\%o|\s)/) {
                   $line=~s/^\(.*\)/     /; 
                   $reply.=$line; 
	       }
	    }
	    $selector->remove($fh) if eof($fh);
	}
    }
    close $cmd_out;
    close $cmd_err;
    return ($reply,$error,$exitstatus);
}

print join("\n----\n",&maximareply('1234'));
print join("\n----\n",&maximareply('x0: 5;x1: 7;integrate (x^2, x, x0, x1);'));

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