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

1.2       albertel    1: package Apache::run;
1.21      www         2: #
1.43    ! albertel    3: # $Id: run.pm,v 1.42 2003/11/09 01:17:35 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.11      albertel   30: $Apache::run::EVALUATE_STRING=<<'ENDEVALUATE';  
1.24      albertel   31:   my %_LONCAPA_INTERNAL_oldexpressions=();
                     32:   while (!$_LONCAPA_INTERNAL_oldexpressions{$_}) {
                     33:     $_LONCAPA_INTERNAL_oldexpressions{$_}=1;
1.13      www        34:     $_ =~s/((?:\$|\&)(?:[\#|\$]*[A-Za-z][\w]*|\{[A-Za-z][\w]*\}))([\[\{][^\$\&\]\}]+[\]\}])*?(\([^\$\&\)]+\))*?(?=[^\[\{\(]|$)/eval(defined(eval($1.$2))?eval('$1.$2.$3'):'$1.$2.$3')/seg;
1.30      albertel   35:     if (scalar(values(%_LONCAPA_INTERNAL_oldexpressions))>10) {last;}
1.11      albertel   36:   }
                     37: ENDEVALUATE
                     38: 
1.10      albertel   39: sub evaluate {
1.32      albertel   40:     my ($expression,$safeeval,$decls) = @_;
                     41:     unless (defined($expression)) { return ''; }
                     42:     if ($Apache::lonxml::evaluate < 1) { return $expression; }
                     43:     my $result = '';
                     44:     $@='';
                     45:     $Apache::run::timeout=0;
                     46:     $main::SIG{'ALRM'} = sub {
                     47: 	$Apache::run::timeout=1;
1.33      albertel   48: 	die("timeout");
1.32      albertel   49:     };
1.34      albertel   50:     my $innererror;
1.32      albertel   51:     eval {
1.35      albertel   52: 	alarm($Apache::lonnet::perlvar{'lonScriptTimeout'});
1.32      albertel   53: 	$safeeval->reval('{'.$decls.';$_=<<\'EXPRESSION\';'."\n".$expression.
                     54: 			 "\n".'EXPRESSION'."\n".$EVALUATE_STRING.'}');
1.34      albertel   55: 	$innererror=$@;
1.32      albertel   56: 	alarm(0);
                     57:     };
                     58:     my $error=$@;
1.34      albertel   59:     if ($error eq '' && $innererror eq '' && !$Apache::run::timeout) {
1.32      albertel   60: 	$result = $safeeval->reval('return $_;');
                     61: 	chomp $result;
                     62:     } else {
                     63: 	if ($Apache::run::timeout) {
                     64: 	    $error = 'Code ran too long. It ran for more than '.
1.43    ! albertel   65: 		$Apache::lonnet::perlvar{'lonScriptTimeout'}.' seconds';
1.32      albertel   66: 	}
1.34      albertel   67: 	&Apache::lonxml::error('substitution on <pre>'.
                     68: 			       &HTML::Entities::encode($expression).
                     69: 			       '</pre> with <pre>'.
                     70: 			       &HTML::Entities::encode($decls).
                     71: 			       '</pre> caused <pre>'.
                     72: 			       &HTML::Entities::encode($error).' '.
                     73: 			       &HTML::Entities::encode($innererror).
                     74: 			       '</pre>');
1.32      albertel   75:     }
                     76:     return $result
1.2       albertel   77: }
                     78: 
                     79: sub run {
1.32      albertel   80:     my ($code,$safeeval,$hideerrors) = @_;
                     81:     my @result;
                     82:     $@='';
                     83:     $Apache::run::timeout=0;
                     84:     $main::SIG{'ALRM'} = sub {
                     85: 	$Apache::run::timeout=1;
1.33      albertel   86: 	die("timeout");
1.32      albertel   87:     };
1.34      albertel   88:     my $innererror;
1.32      albertel   89:     eval {
1.35      albertel   90: 	alarm($Apache::lonnet::perlvar{'lonScriptTimeout'});
1.32      albertel   91: 	@result=$safeeval->reval($code);
1.34      albertel   92: 	$innererror=$@;
1.32      albertel   93: 	alarm(0);
                     94:     };
                     95:     my $error=$@;
1.34      albertel   96:     if (($Apache::run::timeout || $error ne '' || $innererror ne '') && !$hideerrors) {
1.32      albertel   97: 	if ($Apache::run::timeout) {
                     98: 	    $error = 'Code ran too long. It ran for more than '.
1.43    ! albertel   99: 		$Apache::lonnet::perlvar{'lonScriptTimeout'}.' seconds';
1.32      albertel  100: 	}
1.40      albertel  101: 	my $errormsg='<pre>'.&HTML::Entities::encode($error).' '.
                    102: 	    &HTML::Entities::encode($innererror).
                    103: 	    '</pre> occured while running <pre>';
                    104: 	$code=&HTML::Entities::encode($code);
                    105: 	if ($innererror=~/line (\d+)/) {
                    106: 	    my $linenumber=$1;
                    107: 	    my @code=split("\n",$code);
                    108: 	    $code[$linenumber-1]='<b><font color="red">'.
                    109: 		$code[$linenumber-1].'</font></b>';
                    110: 	    $code=join("\n",@code);
                    111: 	}
                    112: 	&Apache::lonxml::error($errormsg.$code.'</pre>');
1.32      albertel  113:     }
                    114:     if ( $#result < '1') {
                    115: 	return $result[0];
                    116:     } else {
                    117: 	&Apache::lonxml::debug("<b>Got lots results</b>:$#result:");
                    118: 	return (@result);
                    119:     }
1.2       albertel  120: }
                    121: 
1.19      albertel  122: sub dump {
1.42      albertel  123:     my ($target,$safeeval)=@_;
                    124:     my $dump='';
                    125:     foreach my $symname (sort keys %{$safeeval->varglob('main::')}) {
                    126: 	if (($symname!~/^\_/) && ($symname!~/\:$/)) {
                    127: 	    my $line;
                    128: 	    if ($safeeval->reval('defined($'.$symname.')')) {
                    129: 		$line='$'.$symname.'='.$safeeval->reval('$'.$symname);
                    130: 	    }	
                    131: 	    if ($safeeval->reval('defined(@'.$symname.')')) {
                    132: 		$line='@'.$symname.'=('.
                    133: 		    $safeeval->reval('join(",",@'.$symname.')').")";
                    134: 	    }
                    135: 	    if ($safeeval->reval('defined(%'.$symname.')')) {
                    136: 		$line='%'.$symname.'=(';
                    137: 		$line.=$safeeval->reval('join(",",map { $_."=>".$'.
                    138: 					$symname.'{$_} } sort keys %'.
                    139: 					$symname.')').")"
                    140: 				    }
                    141: 	    if ($line ne '') {$dump.=&HTML::Entities::encode($line)."<br />";}
                    142: 	}
1.19      albertel  143:     }
1.42      albertel  144:     $dump.='';
                    145:     return $dump;
1.19      albertel  146: }
                    147: 
1.2       albertel  148: 1;
                    149: __END__;

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