File:  [LON-CAPA] / loncom / xml / run.pm
Revision 1.50: download - view: text, annotated - select for diffs
Sat Feb 26 06:10:30 2005 UTC (19 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- hide the perl inter variables like $'

    1: package Apache::run;
    2: #
    3: # $Id: run.pm,v 1.50 2005/02/26 06:10:30 albertel 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: 		     { 
   54: 			 use strict;
   55: 			 no strict "vars";
   56: 			 if (eval(defined(eval($__LC__a.$__LC__b)))) {
   57: 			     $__LC__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($__LC__result)) {
   65: 		     $__LC__result=$__LC__prefix.$__LC__a.$__LC__b.$__LC__c;
   66: 		 }
   67: 		 $__LC__result;
   68:                   /sexg;
   69:     if (scalar(values(%_LONCAPA_INTERNAL_oldexpressions))>10) {last;}
   70: }
   71: ENDEVALUATE
   72: 
   73: sub evaluate {
   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;
   82: 	die("timeout");
   83:     };
   84:     my $innererror;
   85:     eval {
   86: 	alarm($Apache::lonnet::perlvar{'lonScriptTimeout'});
   87: 	$safeeval->reval('{'.$decls.';$_=<<\'EXPRESSION\';'."\n".$expression.
   88: 			 "\n".'EXPRESSION'."\n".$EVALUATE_STRING.'}');
   89: 	$innererror=$@;
   90: 	alarm(0);
   91:     };
   92:     my $error=$@;
   93:     if ($error eq '' && $innererror eq '' && !$Apache::run::timeout) {
   94: 	$result = $safeeval->reval('return $_;');
   95: 	chomp $result;
   96:     } else {
   97: 	if ($Apache::run::timeout) {
   98: 	    $error = 'Code ran too long. It ran for more than '.
   99: 		$Apache::lonnet::perlvar{'lonScriptTimeout'}.' seconds';
  100: 	}
  101: 	&Apache::lonxml::error('substitution on <pre>'.
  102: 			       &HTML::Entities::encode($expression,'<>&"').
  103: 			       '</pre> with <pre>'.
  104: 			       &HTML::Entities::encode($decls,'<>&"').
  105: 			       '</pre> caused <pre>'.
  106: 			       &HTML::Entities::encode($error,'<>&"').' '.
  107: 			       &HTML::Entities::encode($innererror,'<>&"').
  108: 			       '</pre>');
  109:     }
  110:     return $result
  111: }
  112: 
  113: sub run {
  114:     my ($code,$safeeval,$hideerrors) = @_;
  115:     my @result;
  116:     $@='';
  117:     $Apache::run::timeout=0;
  118:     $main::SIG{'ALRM'} = sub {
  119: 	$Apache::run::timeout=1;
  120: 	die("timeout");
  121:     };
  122:     my $innererror;
  123:     eval {
  124: 	alarm($Apache::lonnet::perlvar{'lonScriptTimeout'});
  125: 	@result=$safeeval->reval($code);
  126: 	$innererror=$@;
  127: 	alarm(0);
  128:     };
  129:     my $error=$@;
  130:     if (($Apache::run::timeout || $error ne '' || $innererror ne '') && !$hideerrors) {
  131: 	if ($Apache::run::timeout) {
  132: 	    $error = 'Code ran too long. It ran for more than '.
  133: 		$Apache::lonnet::perlvar{'lonScriptTimeout'}.' seconds';
  134: 	}
  135: 	my $errormsg='<pre>'.&HTML::Entities::encode($error,'<>&"').' '.
  136: 	    &HTML::Entities::encode($innererror,'<>&"').
  137: 	    '</pre> occured while running <pre>';
  138: 	$code=&HTML::Entities::encode($code,'<>&"');
  139: 	if ($innererror=~/line (\d+)/) {
  140: 	    my $linenumber=$1;
  141: 	    my @code=split("\n",$code);
  142: 	    $code[$linenumber-1]='<b><font color="red">'.
  143: 		$code[$linenumber-1].'</font></b>';
  144: 	    $code=join("\n",@code);
  145: 	}
  146: 	&Apache::lonxml::error($errormsg.$code.'</pre>');
  147:     }
  148:     if ( $#result < '1') {
  149: 	return $result[0];
  150:     } else {
  151: 	&Apache::lonxml::debug("<b>Got lots results</b>:$#result:");
  152: 	return (@result);
  153:     }
  154: }
  155: 
  156: sub dump {
  157:     my ($target,$safeeval)=@_;
  158:     my $dump='';
  159:     foreach my $symname (sort keys %{$safeeval->varglob('main::')}) {
  160: 	if (($symname!~/^\_/) && ($symname!~/\:$/)) {
  161: 	    my $line;
  162: 	    if ($safeeval->reval('defined($'.$symname.')')) {
  163: 		if ($symname =~ /^\w/) {
  164: 		    $line.='$'.$symname.'='.$safeeval->reval('$'.$symname)."\n";
  165: 		}
  166: 	    }	
  167: 	    if ($safeeval->reval('defined(@'.$symname.')')) {
  168: 		$line.='@'.$symname.'=('.
  169: 		    $safeeval->reval('join(",",@'.$symname.')').")"."\n";
  170: 	    }
  171: 	    if ($safeeval->reval('defined(%'.$symname.')')) {
  172: 		$line.='%'.$symname.'=(';
  173: 		$line.=$safeeval->reval('join(",",map { $_."=>".$'.
  174: 					$symname.'{$_} } sort keys %'.
  175: 					$symname.')').")"."\n"
  176: 				    }
  177: 	    if ($line ne '') {
  178: 		$line=&HTML::Entities::encode($line,'<>&"');
  179: 		$line=~s|\n|<br />|;
  180: 		$dump.=$line;
  181: 	    }
  182: 	}
  183:     }
  184:     $dump.='';
  185:     return $dump;
  186: }
  187: 
  188: 1;
  189: __END__;

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