File:  [LON-CAPA] / loncom / xml / run.pm
Revision 1.60: download - view: text, annotated - select for diffs
Mon Nov 24 18:55:01 2008 UTC (15 years, 5 months ago) by jms
Branches: MAIN
CVS tags: version_2_9_X, version_2_9_99_0, version_2_9_1, version_2_9_0, version_2_8_99_1, version_2_8_99_0, version_2_10_X, version_2_10_1, version_2_10_0_RC2, version_2_10_0_RC1, version_2_10_0, loncapaMITrelate_1, language_hyphenation_merge, language_hyphenation, bz6209-base, bz6209, bz5969, bz2851, PRINT_INCOMPLETE_base, PRINT_INCOMPLETE, HEAD, GCI_3, GCI_2, BZ5971-printing-apage, BZ5434-fox, BZ4492-merge, BZ4492-feature_horizontal_radioresponse
Added/modified POD comments

    1: package Apache::run;
    2: #
    3: # $Id: run.pm,v 1.60 2008/11/24 18:55:01 jms Exp $
    4: #
    5: # Copyright Michigan State University Board of Trustees
    6: #
    7: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    8: #
    9: # LON-CAPA is free software; you can redistribute it and/or modify
   10: # it under the terms of the GNU General Public License as published by
   11: # the Free Software Foundation; either version 2 of the License, or
   12: # (at your option) any later version.
   13: #
   14: # LON-CAPA is distributed in the hope that it will be useful,
   15: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   16: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   17: # GNU General Public License for more details.
   18: #
   19: # You should have received a copy of the GNU General Public License
   20: # along with LON-CAPA; if not, write to the Free Software
   21: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   22: #
   23: # /home/httpd/html/adm/gpl.txt
   24: #
   25: # http://www.lon-capa.org/
   26: #
   27: 
   28: use HTML::Entities;
   29: 
   30: $Apache::run::EVALUATE_STRING=<<'ENDEVALUATE';
   31: my %_LONCAPA_INTERNAL_oldexpressions=();
   32: while (!$_LONCAPA_INTERNAL_oldexpressions{$_}) {
   33:     $_LONCAPA_INTERNAL_oldexpressions{$_}=1;
   34:     $_ =~s/  # $1 will be the variable reference or subroutine name
   35:             ((?:\$|\&) #look for a starting $ or &
   36:              (?:[\#|\$]* #support $$ or $#$ etc.
   37:               [A-Za-z][\w]*| # get variable name
   38:               \{[A-Za-z][\w]*\})) # for ${a}
   39:              # $2 is 0 or more array dereferences []
   40:              #             or  hash dereferences {}
   41:              # the ^$ and ^& is because we do this iteratively
   42:              #    $a[$c] becomes $a[3] which then evaluates
   43:              ([\[\{][^\$\&\]\}]+[\]\}])*?
   44:              # $3 is the list of arguments
   45:              (\([^\$\&\)]+\))*?
   46:                 # only match the above if there is not { [ ( coming up
   47:                 # Why? (I.e. this fails &a(1)[2]
   48:                 (?=[^\[\{\(]|$)/
   49: 		 my ($__LC__a,$__LC__b,$__LC__c)=($1,$2,$3);
   50: 		 my $__LC__prefix;
   51: 		 my $__LC__result;
   52: 		 while (1) {
   53: 		     if ($__LC__a =~ m-^&(theta|pi|rho)$-) { last; }
   54: 		     { 
   55: 			 use strict;
   56: 			 no strict "vars";
   57: 			 if (eval(defined(eval($__LC__a.$__LC__b)))) {
   58: 			     $__LC__result= $__LC__prefix.eval($__LC__a.$__LC__b.$__LC__c);
   59: 			     last;
   60: 			 }
   61: 		     }
   62: 		     $__LC__prefix.=substr($__LC__a,0,1,"");
   63: 		     if ($__LC__a!~m-^(\$|&|\#)-) { last; }
   64: 		 }
   65: 		 if (!defined($__LC__result)) {
   66: 		     $__LC__result=$__LC__prefix.$__LC__a.$__LC__b.$__LC__c;
   67: 		 }
   68: 		 $__LC__result;
   69:                   /sexg;
   70:     if (scalar(values(%_LONCAPA_INTERNAL_oldexpressions))>10) {last;}
   71: }
   72: ENDEVALUATE
   73: 
   74: sub evaluate {
   75:     my ($expression,$safeeval,$decls) = @_;
   76:     unless (defined($expression)) { return ''; }
   77:     if ($Apache::lonxml::evaluate < 1) { return $expression; }
   78:     my $result = '';
   79:     $@='';
   80:     $Apache::run::timeout=0;
   81:     local $main::SIG{'ALRM'} = sub {
   82: 	$Apache::run::timeout=1;
   83: 	die("timeout");
   84:     };
   85:     my $innererror;
   86:     eval {
   87: 	&Apache::lonxml::start_alarm();
   88: 	$safeeval->reval('{'.$decls.';$_=<<\'EXPRESSION\';'."\n".$expression.
   89: 			 "\n".'EXPRESSION'."\n".$EVALUATE_STRING.'}');
   90: 	$innererror=$@;
   91: 	&Apache::lonxml::end_alarm();
   92:     };
   93:     my $error=$@; 
   94:     if ($error eq '' && $innererror eq '' && !$Apache::run::timeout) {
   95: 	$result = $safeeval->reval('return $_;');
   96: 	chomp $result;
   97:     } else {
   98: 	if ($Apache::run::timeout) {
   99: 	    $error = 'Code ran too long. It ran for more than '.
  100: 		$Apache::lonnet::perlvar{'lonScriptTimeout'}.' seconds';
  101: 	}
  102: 	&Apache::lonxml::error('substitution on <pre>'.
  103: 			       &HTML::Entities::encode($expression,'<>&"').
  104: 			       '</pre> with <pre>'.
  105: 			       &HTML::Entities::encode($decls,'<>&"').
  106: 			       '</pre> caused <pre>'.
  107: 			       &HTML::Entities::encode($error,'<>&"').' '.
  108: 			       &HTML::Entities::encode($innererror,'<>&"').
  109: 			       '</pre>');
  110:     }
  111:     return $result
  112: }
  113: 
  114: sub run {
  115:     my ($code,$safeeval,$hideerrors) = @_;
  116:     my @result;
  117:     $@='';
  118:     $Apache::run::timeout=0;
  119:     local $main::SIG{'ALRM'} = sub {
  120: 	$Apache::run::timeout=1;
  121: 	die("timeout");
  122:     };
  123:     my $innererror;
  124:     eval {
  125: 	&Apache::lonxml::start_alarm();
  126: 	@result=$safeeval->reval($code);
  127: 	$innererror=$@;
  128: 	&Apache::lonxml::end_alarm();
  129:     };
  130:     my $error=$@;
  131:     if (($Apache::run::timeout || $error ne '' || $innererror ne '') && !$hideerrors) {
  132: 	if ($Apache::run::timeout) {
  133: 	    $error = 'Code ran too long. It ran for more than '.
  134: 		$Apache::lonnet::perlvar{'lonScriptTimeout'}.' seconds';
  135: 	}
  136: 	my $errormsg='<pre>'.&HTML::Entities::encode($error,'<>&"').' '.
  137: 	    &HTML::Entities::encode($innererror,'<>&"').
  138: 	    '</pre> occurred while running <pre>';
  139: 	$code=&HTML::Entities::encode($code,'<>&"');
  140: 	if ($innererror=~/line (\d+)/) {
  141: 	    my $linenumber=$1;
  142: 	    my @code=split("\n",$code);
  143: 	    if ($linenumber < scalar(@code)) {
  144: 		$code[$linenumber-1]='<b><font color="red">'.
  145: 		    $code[$linenumber-1].'</font></b>';
  146: 	    }
  147: 	    $code=join("\n",@code);
  148: 	}
  149: 	&Apache::lonxml::error($errormsg.$code.'</pre>');
  150:     }
  151:     if ( $#result < '1') {
  152: 	return $result[0];
  153:     } else {
  154: 	&Apache::lonxml::debug("<b>Got lots results</b>:$#result:");
  155: 	return (@result);
  156:     }
  157: }
  158: 
  159: sub dump {
  160:     my ($target,$safeeval)=@_;
  161:     my $dump='';
  162:     foreach my $symname (sort keys %{$safeeval->varglob('main::')}) {
  163: 	if (($symname!~ /^(INC|SIG)/) && ($symname!~/\027/) &&
  164: 	    ($symname!~/^\_/) && ($symname!~/\:$/)) {
  165: 	    my $line;
  166: 	    if ($safeeval->reval('defined($'.$symname.')')) {
  167: 		if ($symname =~ /^\w/) {
  168: 		    $line.='$'.$symname.'='.$safeeval->reval('$'.$symname)."\n";
  169: 		}
  170: 	    }	
  171: 	    if ($safeeval->reval('defined(@'.$symname.')')) {
  172: 		$line.='@'.$symname.'=('.
  173: 		    $safeeval->reval('join(",",@'.$symname.')').")"."\n";
  174: 	    }
  175: 	    if ($safeeval->reval('defined(%'.$symname.')')) {
  176: 		$line.='%'.$symname.'=(';
  177: 		$line.=$safeeval->reval('join(",",map { $_."=>".$'.
  178: 					$symname.'{$_} } sort keys %'.
  179: 					$symname.')').")"."\n";
  180: 	    }
  181: 	    if ($line ne '') {
  182: 		$line=&HTML::Entities::encode($line,'<>&"');
  183: 		$line=~s|\n|<br />|g;
  184: 		$dump.=$line;
  185: 	    }
  186: 	}
  187:     }
  188:     $dump.='';
  189:     return $dump;
  190: }
  191: 
  192: 1;
  193: __END__;
  194: 
  195: =pod
  196: 
  197: =head1 NAME
  198: 
  199: Apache::run.pm
  200: 
  201: =head1 SYNOPSIS
  202: 
  203: Used to prevent poorly written problems from
  204: causing lingering after effects
  205: 
  206: This is part of the LearningOnline Network with CAPA project
  207: described at http://www.lon-capa.org.
  208: 
  209: 
  210: =head1 NOTABLE SUBROUTINES
  211: 
  212: =over
  213: 
  214: =item run(), dump(), evaluate()
  215: 
  216: =back
  217: 
  218: =cut
  219: 

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