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

1.2       albertel    1: package Apache::run;
1.21      www         2: #
1.47    ! albertel    3: # $Id: run.pm,v 1.46 2004/03/31 05:24:00 albertel 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;
                     29: 
1.45      albertel   30: $Apache::run::EVALUATE_STRING=<<'ENDEVALUATE';
                     31: my %_LONCAPA_INTERNAL_oldexpressions=();
                     32: while (!$_LONCAPA_INTERNAL_oldexpressions{$_}) {
1.24      albertel   33:     $_LONCAPA_INTERNAL_oldexpressions{$_}=1;
1.45      albertel   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:                 (?=[^\[\{\(]|$)/
1.47    ! albertel   49: 		 my ($__LC__a,$__LC__b,$__LC__c)=($1,$2,$3);
        !            50: 		 my $__LC__prefix;
        !            51: 		 my $result;
        !            52: 		 while (1) {
        !            53: 		     { 
        !            54: 			 use strict;
        !            55: 			 no strict "vars";
        !            56: 			 if (eval(defined(eval($__LC__a.$__LC__b)))) {
        !            57: 			     $result= $__LC__prefix.eval($__LC__a.$__LC__b.$__LC__c);
        !            58: 			     last;
        !            59: 			 }
        !            60: 		     }
        !            61: 		     $__LC__prefix.=substr($__LC__a,0,1,"");
        !            62: 		     if ($__LC__a!~m-^(\$|&|\#)-) { last; }
        !            63: 		 }
        !            64: 		 if (!defined($result)) {
        !            65: 		     $result=$__LC__prefix.$__LC__a.$__LC__b.$__LC__c;
        !            66: 		 }
        !            67: 		 $result;
        !            68:                   /sexg;
1.30      albertel   69:     if (scalar(values(%_LONCAPA_INTERNAL_oldexpressions))>10) {last;}
1.45      albertel   70: }
1.11      albertel   71: ENDEVALUATE
                     72: 
1.10      albertel   73: sub evaluate {
1.32      albertel   74:     my ($expression,$safeeval,$decls) = @_;
                     75:     unless (defined($expression)) { return ''; }
                     76:     if ($Apache::lonxml::evaluate < 1) { return $expression; }
                     77:     my $result = '';
                     78:     $@='';
                     79:     $Apache::run::timeout=0;
                     80:     $main::SIG{'ALRM'} = sub {
                     81: 	$Apache::run::timeout=1;
1.33      albertel   82: 	die("timeout");
1.32      albertel   83:     };
1.34      albertel   84:     my $innererror;
1.47    ! albertel   85:     $safeeval->permit("require");
1.32      albertel   86:     eval {
1.35      albertel   87: 	alarm($Apache::lonnet::perlvar{'lonScriptTimeout'});
1.32      albertel   88: 	$safeeval->reval('{'.$decls.';$_=<<\'EXPRESSION\';'."\n".$expression.
                     89: 			 "\n".'EXPRESSION'."\n".$EVALUATE_STRING.'}');
1.34      albertel   90: 	$innererror=$@;
1.32      albertel   91: 	alarm(0);
                     92:     };
1.47    ! albertel   93:     $safeeval->deny("require");
1.32      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 {
                     99: 	if ($Apache::run::timeout) {
                    100: 	    $error = 'Code ran too long. It ran for more than '.
1.43      albertel  101: 		$Apache::lonnet::perlvar{'lonScriptTimeout'}.' seconds';
1.32      albertel  102: 	}
1.34      albertel  103: 	&Apache::lonxml::error('substitution on <pre>'.
1.46      albertel  104: 			       &HTML::Entities::encode($expression,'<>&"').
1.34      albertel  105: 			       '</pre> with <pre>'.
1.46      albertel  106: 			       &HTML::Entities::encode($decls,'<>&"').
1.34      albertel  107: 			       '</pre> caused <pre>'.
1.46      albertel  108: 			       &HTML::Entities::encode($error,'<>&"').' '.
                    109: 			       &HTML::Entities::encode($innererror,'<>&"').
1.34      albertel  110: 			       '</pre>');
1.32      albertel  111:     }
                    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;
                    120:     $main::SIG{'ALRM'} = sub {
                    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.35      albertel  126: 	alarm($Apache::lonnet::perlvar{'lonScriptTimeout'});
1.32      albertel  127: 	@result=$safeeval->reval($code);
1.34      albertel  128: 	$innererror=$@;
1.32      albertel  129: 	alarm(0);
                    130:     };
                    131:     my $error=$@;
1.34      albertel  132:     if (($Apache::run::timeout || $error ne '' || $innererror ne '') && !$hideerrors) {
1.32      albertel  133: 	if ($Apache::run::timeout) {
                    134: 	    $error = 'Code ran too long. It ran for more than '.
1.43      albertel  135: 		$Apache::lonnet::perlvar{'lonScriptTimeout'}.' seconds';
1.32      albertel  136: 	}
1.46      albertel  137: 	my $errormsg='<pre>'.&HTML::Entities::encode($error,'<>&"').' '.
                    138: 	    &HTML::Entities::encode($innererror,'<>&"').
1.40      albertel  139: 	    '</pre> occured while running <pre>';
1.46      albertel  140: 	$code=&HTML::Entities::encode($code,'<>&"');
1.40      albertel  141: 	if ($innererror=~/line (\d+)/) {
                    142: 	    my $linenumber=$1;
                    143: 	    my @code=split("\n",$code);
                    144: 	    $code[$linenumber-1]='<b><font color="red">'.
                    145: 		$code[$linenumber-1].'</font></b>';
                    146: 	    $code=join("\n",@code);
                    147: 	}
                    148: 	&Apache::lonxml::error($errormsg.$code.'</pre>');
1.32      albertel  149:     }
                    150:     if ( $#result < '1') {
                    151: 	return $result[0];
                    152:     } else {
                    153: 	&Apache::lonxml::debug("<b>Got lots results</b>:$#result:");
                    154: 	return (@result);
                    155:     }
1.2       albertel  156: }
                    157: 
1.19      albertel  158: sub dump {
1.42      albertel  159:     my ($target,$safeeval)=@_;
                    160:     my $dump='';
                    161:     foreach my $symname (sort keys %{$safeeval->varglob('main::')}) {
                    162: 	if (($symname!~/^\_/) && ($symname!~/\:$/)) {
                    163: 	    my $line;
                    164: 	    if ($safeeval->reval('defined($'.$symname.')')) {
                    165: 		$line='$'.$symname.'='.$safeeval->reval('$'.$symname);
                    166: 	    }	
                    167: 	    if ($safeeval->reval('defined(@'.$symname.')')) {
                    168: 		$line='@'.$symname.'=('.
                    169: 		    $safeeval->reval('join(",",@'.$symname.')').")";
                    170: 	    }
                    171: 	    if ($safeeval->reval('defined(%'.$symname.')')) {
                    172: 		$line='%'.$symname.'=(';
                    173: 		$line.=$safeeval->reval('join(",",map { $_."=>".$'.
                    174: 					$symname.'{$_} } sort keys %'.
                    175: 					$symname.')').")"
                    176: 				    }
1.46      albertel  177: 	    if ($line ne '') {$dump.=&HTML::Entities::encode($line,'<>&"')."<br />";}
1.42      albertel  178: 	}
1.19      albertel  179:     }
1.42      albertel  180:     $dump.='';
                    181:     return $dump;
1.19      albertel  182: }
                    183: 
1.2       albertel  184: 1;
                    185: __END__;

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