Annotation of loncom/xml/run.pm, revision 1.63

1.2       albertel    1: package Apache::run;
1.21      www         2: #
1.63    ! raeburn     3: # $Id: run.pm,v 1.62 2013/12/04 15:57:15 bisitz Exp $
1.21      www         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: #
1.1       sakharuk   27: 
1.26      albertel   28: use HTML::Entities;
1.62      bisitz     29: use Apache::lonlocal;
1.26      albertel   30: 
1.45      albertel   31: $Apache::run::EVALUATE_STRING=<<'ENDEVALUATE';
                     32: my %_LONCAPA_INTERNAL_oldexpressions=();
                     33: while (!$_LONCAPA_INTERNAL_oldexpressions{$_}) {
1.24      albertel   34:     $_LONCAPA_INTERNAL_oldexpressions{$_}=1;
1.45      albertel   35:     $_ =~s/  # $1 will be the variable reference or subroutine name
                     36:             ((?:\$|\&) #look for a starting $ or &
                     37:              (?:[\#|\$]* #support $$ or $#$ etc.
                     38:               [A-Za-z][\w]*| # get variable name
                     39:               \{[A-Za-z][\w]*\})) # for ${a}
                     40:              # $2 is 0 or more array dereferences []
                     41:              #             or  hash dereferences {}
                     42:              # the ^$ and ^& is because we do this iteratively
                     43:              #    $a[$c] becomes $a[3] which then evaluates
                     44:              ([\[\{][^\$\&\]\}]+[\]\}])*?
                     45:              # $3 is the list of arguments
                     46:              (\([^\$\&\)]+\))*?
                     47:                 # only match the above if there is not { [ ( coming up
                     48:                 # Why? (I.e. this fails &a(1)[2]
                     49:                 (?=[^\[\{\(]|$)/
1.47      albertel   50: 		 my ($__LC__a,$__LC__b,$__LC__c)=($1,$2,$3);
                     51: 		 my $__LC__prefix;
1.49      albertel   52: 		 my $__LC__result;
1.47      albertel   53: 		 while (1) {
1.56      albertel   54: 		     if ($__LC__a =~ m-^&(theta|pi|rho)$-) { last; }
1.47      albertel   55: 		     { 
                     56: 			 use strict;
                     57: 			 no strict "vars";
                     58: 			 if (eval(defined(eval($__LC__a.$__LC__b)))) {
1.49      albertel   59: 			     $__LC__result= $__LC__prefix.eval($__LC__a.$__LC__b.$__LC__c);
1.47      albertel   60: 			     last;
                     61: 			 }
                     62: 		     }
                     63: 		     $__LC__prefix.=substr($__LC__a,0,1,"");
                     64: 		     if ($__LC__a!~m-^(\$|&|\#)-) { last; }
                     65: 		 }
1.49      albertel   66: 		 if (!defined($__LC__result)) {
                     67: 		     $__LC__result=$__LC__prefix.$__LC__a.$__LC__b.$__LC__c;
1.47      albertel   68: 		 }
1.49      albertel   69: 		 $__LC__result;
1.47      albertel   70:                   /sexg;
1.30      albertel   71:     if (scalar(values(%_LONCAPA_INTERNAL_oldexpressions))>10) {last;}
1.45      albertel   72: }
1.11      albertel   73: ENDEVALUATE
                     74: 
1.10      albertel   75: sub evaluate {
1.32      albertel   76:     my ($expression,$safeeval,$decls) = @_;
                     77:     unless (defined($expression)) { return ''; }
                     78:     if ($Apache::lonxml::evaluate < 1) { return $expression; }
                     79:     my $result = '';
                     80:     $@='';
                     81:     $Apache::run::timeout=0;
1.58      albertel   82:     local $main::SIG{'ALRM'} = sub {
1.32      albertel   83: 	$Apache::run::timeout=1;
1.33      albertel   84: 	die("timeout");
1.32      albertel   85:     };
1.34      albertel   86:     my $innererror;
1.32      albertel   87:     eval {
1.51      albertel   88: 	&Apache::lonxml::start_alarm();
1.32      albertel   89: 	$safeeval->reval('{'.$decls.';$_=<<\'EXPRESSION\';'."\n".$expression.
                     90: 			 "\n".'EXPRESSION'."\n".$EVALUATE_STRING.'}');
1.34      albertel   91: 	$innererror=$@;
1.51      albertel   92: 	&Apache::lonxml::end_alarm();
1.32      albertel   93:     };
1.51      albertel   94:     my $error=$@; 
1.34      albertel   95:     if ($error eq '' && $innererror eq '' && !$Apache::run::timeout) {
1.32      albertel   96: 	$result = $safeeval->reval('return $_;');
                     97: 	chomp $result;
                     98:     } else {
1.62      bisitz     99:         if ($Apache::run::timeout) {
                    100:             $error =
                    101:                 &mt('Code ran too long. It ran for more than [_1] seconds.',
                    102:                         $Apache::lonnet::perlvar{'lonScriptTimeout'});
                    103:         }
                    104:         &Apache::lonxml::error(
                    105:             &mt('Substitution on [_1] with [_2] caused:',
                    106:                     '<pre>'. &HTML::Entities::encode($expression,'<>&"').'</pre>',
                    107:                     '<pre>'.&HTML::Entities::encode($decls,'<>&"').'</pre>')
                    108:            .'<br />'
                    109:            .'<pre>'.&HTML::Entities::encode($error,'<>&"').' '
                    110:            .&HTML::Entities::encode($innererror,'<>&"').'</pre>');
1.32      albertel  111:     }
1.62      bisitz    112:     return $result;
1.2       albertel  113: }
                    114: 
                    115: sub run {
1.32      albertel  116:     my ($code,$safeeval,$hideerrors) = @_;
                    117:     my @result;
                    118:     $@='';
                    119:     $Apache::run::timeout=0;
1.58      albertel  120:     local $main::SIG{'ALRM'} = sub {
1.32      albertel  121: 	$Apache::run::timeout=1;
1.33      albertel  122: 	die("timeout");
1.32      albertel  123:     };
1.34      albertel  124:     my $innererror;
1.32      albertel  125:     eval {
1.51      albertel  126: 	&Apache::lonxml::start_alarm();
1.32      albertel  127: 	@result=$safeeval->reval($code);
1.34      albertel  128: 	$innererror=$@;
1.51      albertel  129: 	&Apache::lonxml::end_alarm();
1.32      albertel  130:     };
                    131:     my $error=$@;
1.34      albertel  132:     if (($Apache::run::timeout || $error ne '' || $innererror ne '') && !$hideerrors) {
1.62      bisitz    133:         if ($Apache::run::timeout) {
                    134:             $error = &mt('Code ran too long. It ran for more than [_1] seconds.',
                    135:                              $Apache::lonnet::perlvar{'lonScriptTimeout'});
                    136:         }
                    137:         $code=&HTML::Entities::encode($code,'<>&"');
                    138:         if ($innererror=~/line (\d+)/) {
                    139:             my $linenumber=$1;
                    140:             my @code=split("\n",$code);
                    141:             if ($linenumber < scalar(@code)) {
                    142:                 $code[$linenumber-1]=
                    143:                     '<span style="color:red;font-weight:bold;">'.
                    144:                     $code[$linenumber-1].'</span>';
                    145:             }
                    146:             $code=join("\n",@code);
                    147:         }
                    148:         &Apache::lonxml::error(
                    149:             '<pre>'.&HTML::Entities::encode($error,'<>&"').' '.
                    150:             &HTML::Entities::encode($innererror,'<>&"').
                    151:             '</pre>'
                    152:            .&mt('This occurred while running: [_1]',
                    153:                    '<pre>'.$code.'</pre>'));
1.32      albertel  154:     }
                    155:     if ( $#result < '1') {
                    156: 	return $result[0];
                    157:     } else {
                    158: 	&Apache::lonxml::debug("<b>Got lots results</b>:$#result:");
                    159: 	return (@result);
                    160:     }
1.2       albertel  161: }
                    162: 
1.19      albertel  163: sub dump {
1.42      albertel  164:     my ($target,$safeeval)=@_;
                    165:     my $dump='';
1.63    ! raeburn   166:     foreach my $symname (sort(keys(%{$safeeval->varglob('main::')}))) {
1.54      albertel  167: 	if (($symname!~ /^(INC|SIG)/) && ($symname!~/\027/) &&
                    168: 	    ($symname!~/^\_/) && ($symname!~/\:$/)) {
1.42      albertel  169: 	    my $line;
                    170: 	    if ($safeeval->reval('defined($'.$symname.')')) {
1.50      albertel  171: 		if ($symname =~ /^\w/) {
                    172: 		    $line.='$'.$symname.'='.$safeeval->reval('$'.$symname)."\n";
                    173: 		}
1.42      albertel  174: 	    }	
1.61      raeburn   175: 	    if ($safeeval->reval('@'.$symname)) {
1.49      albertel  176: 		$line.='@'.$symname.'=('.
                    177: 		    $safeeval->reval('join(",",@'.$symname.')').")"."\n";
1.42      albertel  178: 	    }
1.61      raeburn   179: 	    if ($safeeval->reval('%'.$symname)) {
1.49      albertel  180: 		$line.='%'.$symname.'=(';
1.42      albertel  181: 		$line.=$safeeval->reval('join(",",map { $_."=>".$'.
1.63    ! raeburn   182: 					$symname.'{$_} } sort(keys(%'.
        !           183: 					$symname.')))').")"."\n";
1.53      albertel  184: 	    }
1.49      albertel  185: 	    if ($line ne '') {
                    186: 		$line=&HTML::Entities::encode($line,'<>&"');
1.52      albertel  187: 		$line=~s|\n|<br />|g;
1.49      albertel  188: 		$dump.=$line;
                    189: 	    }
1.42      albertel  190: 	}
1.19      albertel  191:     }
1.42      albertel  192:     $dump.='';
                    193:     return $dump;
1.19      albertel  194: }
                    195: 
1.2       albertel  196: 1;
                    197: __END__;
1.60      jms       198: 
                    199: =pod
                    200: 
                    201: =head1 NAME
                    202: 
                    203: Apache::run.pm
                    204: 
                    205: =head1 SYNOPSIS
                    206: 
                    207: Used to prevent poorly written problems from
                    208: causing lingering after effects
                    209: 
                    210: This is part of the LearningOnline Network with CAPA project
                    211: described at http://www.lon-capa.org.
                    212: 
                    213: 
                    214: =head1 NOTABLE SUBROUTINES
                    215: 
                    216: =over
                    217: 
                    218: =item run(), dump(), evaluate()
                    219: 
                    220: =back
                    221: 
                    222: =cut
                    223: 

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