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

1.2       albertel    1: package Apache::run;
1.21      www         2: #
1.33    ! albertel    3: # $Id: run.pm,v 1.32 2003/03/24 22:43:31 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:     };
                     50:     eval {
                     51: 	alarm(Apache->request->server->timeout);
                     52: 	$safeeval->reval('{'.$decls.';$_=<<\'EXPRESSION\';'."\n".$expression.
                     53: 			 "\n".'EXPRESSION'."\n".$EVALUATE_STRING.'}');
                     54: 	alarm(0);
                     55:     };
                     56:     my $error=$@;
                     57:     if ($error eq '' && !$Apache::run::timeout) {
                     58: 	$result = $safeeval->reval('return $_;');
                     59: 	chomp $result;
                     60:     } else {
                     61: 	if ($Apache::run::timeout) {
                     62: 	    $error = 'Code ran too long. It ran for more than '.
                     63: 		Apache->request->server->timeout.' seconds';
                     64: 	}
                     65: 	&Apache::lonxml::error('substitution on <pre>'.$expression.
                     66: 			       '</pre> with <pre>'.$decls.
                     67: 			       '</pre> caused <pre>'.$error);
                     68:     }
                     69:     return $result
1.2       albertel   70: }
                     71: 
                     72: sub run {
1.32      albertel   73:     my ($code,$safeeval,$hideerrors) = @_;
                     74:     my @result;
                     75:     $@='';
                     76:     $Apache::run::timeout=0;
                     77:     $main::SIG{'ALRM'} = sub {
                     78: 	$Apache::run::timeout=1;
1.33    ! albertel   79: 	die("timeout");
1.32      albertel   80:     };
                     81:     eval {
                     82: 	alarm(Apache->request->server->timeout);
                     83: 	@result=$safeeval->reval($code);
                     84: 	alarm(0);
                     85:     };
                     86:     my $error=$@;
                     87:     if (($Apache::run::timeout || $error ne '') && !$hideerrors) {
                     88: 	if ($Apache::run::timeout) {
                     89: 	    $error = 'Code ran too long. It ran for more than '.
                     90: 		Apache->request->server->timeout.' seconds';
                     91: 	}
                     92: 	&Apache::lonxml::error('<pre>'.&HTML::Entities::encode($error).
                     93: 			       '</pre> occured while running <pre>'.
                     94: 			       &HTML::Entities::encode($code).'</pre>');
                     95:     }
                     96:     if ( $#result < '1') {
                     97: 	return $result[0];
                     98:     } else {
                     99: 	&Apache::lonxml::debug("<b>Got lots results</b>:$#result:");
                    100: 	return (@result);
                    101:     }
1.2       albertel  102: }
                    103: 
1.19      albertel  104: sub dump {
                    105:   my ($target,$safeeval)=@_;
                    106:   my $dump='';
                    107:   foreach my $symname (sort keys %{$safeeval->varglob('main::')}) {
                    108:     if (($symname!~/^\_/) && ($symname!~/\:$/)) {
1.27      albertel  109:       my $line;
1.20      albertel  110:       if ($safeeval->reval('defined($'.$symname.')')) {
1.27      albertel  111: 	$line='$'.$symname.'='.$safeeval->reval('$'.$symname);
1.19      albertel  112:       }	
1.22      albertel  113:       if ($safeeval->reval('defined(@'.$symname.')')) {
1.27      albertel  114: 	$line='@'.$symname.'=('.
                    115: 	  $safeeval->reval('join(",",@'.$symname.')').")";
1.19      albertel  116:       }
1.22      albertel  117:       if ($safeeval->reval('defined(%'.$symname.')')) {
1.27      albertel  118: 	$line='%'.$symname.'=(';
                    119: 	$line.=$safeeval->reval('join(",",map { $_."=>".$'.
1.19      albertel  120: 				$symname.'{$_} } sort keys %'.
1.27      albertel  121: 				$symname.')').")"
1.19      albertel  122:       }
1.27      albertel  123:       if ($line ne '') { $dump.=&HTML::Entities::encode($line)."<br />\n"; }
1.19      albertel  124:     }
                    125:   }
                    126:   $dump.='';
                    127:   return $dump;
                    128: }
                    129: 
1.2       albertel  130: 1;
                    131: __END__;

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