File:  [LON-CAPA] / loncom / xml / run.pm
Revision 1.37: download - view: text, annotated - select for diffs
Mon Jun 9 21:37:54 2003 UTC (21 years ago) by albertel
Branches: MAIN
CVS tags: HEAD
- how about this instead then.

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

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