Annotation of loncom/xml/run.pm, revision 1.45
1.2 albertel 1: package Apache::run;
1.21 www 2: #
1.45 ! albertel 3: # $Id: run.pm,v 1.44 2004/02/26 22:08:54 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: (?=[^\[\{\(]|$)/
! 49: &__LC_INTERNAL_EVALUATE__($1,$2,$3)/sexg;
1.30 albertel 50: if (scalar(values(%_LONCAPA_INTERNAL_oldexpressions))>10) {last;}
1.45 ! albertel 51: }
1.11 albertel 52: ENDEVALUATE
53:
1.10 albertel 54: sub evaluate {
1.32 albertel 55: my ($expression,$safeeval,$decls) = @_;
56: unless (defined($expression)) { return ''; }
57: if ($Apache::lonxml::evaluate < 1) { return $expression; }
58: my $result = '';
59: $@='';
60: $Apache::run::timeout=0;
61: $main::SIG{'ALRM'} = sub {
62: $Apache::run::timeout=1;
1.33 albertel 63: die("timeout");
1.32 albertel 64: };
1.34 albertel 65: my $innererror;
1.32 albertel 66: eval {
1.35 albertel 67: alarm($Apache::lonnet::perlvar{'lonScriptTimeout'});
1.32 albertel 68: $safeeval->reval('{'.$decls.';$_=<<\'EXPRESSION\';'."\n".$expression.
69: "\n".'EXPRESSION'."\n".$EVALUATE_STRING.'}');
1.34 albertel 70: $innererror=$@;
1.32 albertel 71: alarm(0);
72: };
73: my $error=$@;
1.34 albertel 74: if ($error eq '' && $innererror eq '' && !$Apache::run::timeout) {
1.32 albertel 75: $result = $safeeval->reval('return $_;');
76: chomp $result;
77: } else {
78: if ($Apache::run::timeout) {
79: $error = 'Code ran too long. It ran for more than '.
1.43 albertel 80: $Apache::lonnet::perlvar{'lonScriptTimeout'}.' seconds';
1.32 albertel 81: }
1.34 albertel 82: &Apache::lonxml::error('substitution on <pre>'.
83: &HTML::Entities::encode($expression).
84: '</pre> with <pre>'.
85: &HTML::Entities::encode($decls).
86: '</pre> caused <pre>'.
87: &HTML::Entities::encode($error).' '.
88: &HTML::Entities::encode($innererror).
89: '</pre>');
1.32 albertel 90: }
91: return $result
1.2 albertel 92: }
93:
94: sub run {
1.32 albertel 95: my ($code,$safeeval,$hideerrors) = @_;
96: my @result;
97: $@='';
98: $Apache::run::timeout=0;
99: $main::SIG{'ALRM'} = sub {
100: $Apache::run::timeout=1;
1.33 albertel 101: die("timeout");
1.32 albertel 102: };
1.34 albertel 103: my $innererror;
1.32 albertel 104: eval {
1.35 albertel 105: alarm($Apache::lonnet::perlvar{'lonScriptTimeout'});
1.32 albertel 106: @result=$safeeval->reval($code);
1.34 albertel 107: $innererror=$@;
1.32 albertel 108: alarm(0);
109: };
110: my $error=$@;
1.34 albertel 111: if (($Apache::run::timeout || $error ne '' || $innererror ne '') && !$hideerrors) {
1.32 albertel 112: if ($Apache::run::timeout) {
113: $error = 'Code ran too long. It ran for more than '.
1.43 albertel 114: $Apache::lonnet::perlvar{'lonScriptTimeout'}.' seconds';
1.32 albertel 115: }
1.40 albertel 116: my $errormsg='<pre>'.&HTML::Entities::encode($error).' '.
117: &HTML::Entities::encode($innererror).
118: '</pre> occured while running <pre>';
119: $code=&HTML::Entities::encode($code);
120: if ($innererror=~/line (\d+)/) {
121: my $linenumber=$1;
122: my @code=split("\n",$code);
123: $code[$linenumber-1]='<b><font color="red">'.
124: $code[$linenumber-1].'</font></b>';
125: $code=join("\n",@code);
126: }
127: &Apache::lonxml::error($errormsg.$code.'</pre>');
1.32 albertel 128: }
129: if ( $#result < '1') {
130: return $result[0];
131: } else {
132: &Apache::lonxml::debug("<b>Got lots results</b>:$#result:");
133: return (@result);
134: }
1.2 albertel 135: }
136:
1.19 albertel 137: sub dump {
1.42 albertel 138: my ($target,$safeeval)=@_;
139: my $dump='';
140: foreach my $symname (sort keys %{$safeeval->varglob('main::')}) {
141: if (($symname!~/^\_/) && ($symname!~/\:$/)) {
142: my $line;
143: if ($safeeval->reval('defined($'.$symname.')')) {
144: $line='$'.$symname.'='.$safeeval->reval('$'.$symname);
145: }
146: if ($safeeval->reval('defined(@'.$symname.')')) {
147: $line='@'.$symname.'=('.
148: $safeeval->reval('join(",",@'.$symname.')').")";
149: }
150: if ($safeeval->reval('defined(%'.$symname.')')) {
151: $line='%'.$symname.'=(';
152: $line.=$safeeval->reval('join(",",map { $_."=>".$'.
153: $symname.'{$_} } sort keys %'.
154: $symname.')').")"
155: }
156: if ($line ne '') {$dump.=&HTML::Entities::encode($line)."<br />";}
157: }
1.19 albertel 158: }
1.42 albertel 159: $dump.='';
160: return $dump;
1.19 albertel 161: }
162:
1.2 albertel 163: 1;
164: __END__;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>