1: package Apache::run;
2: #
3: # $Id: run.pm,v 1.31 2002/06/24 21:23:26 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: unless (defined($expression)) { return ''; }
42: if ($Apache::lonxml::evaluate < 1) { return $expression; }
43: my $result = '';
44: $@='';
45: if ($Apache::lonxml::request) {
46: $Apache::lonxml::request->hard_timeout("Apache::run::run, evaluation $code");
47: }
48: $safeeval->reval('{'.$decls.';$_=<<\'EXPRESSION\';'."\n".$expression.
49: "\n".'EXPRESSION'."\n".$EVALUATE_STRING.'}');
50: if ($Apache::lonxml::request) { $Apache::lonxml::request->kill_timeout; }
51: # $safeeval->reval('{'.$decls.';<< &evaluate(q|'.$expression.'|);}');
52: my $error=$@;
53: if ($@ eq '') {
54: $result = $safeeval->reval('return $_;');
55: chomp $result;
56: } else {
57: &Apache::lonxml::error('substitution on <pre>'.$expression.
58: '</pre> with <pre>'.$decls.
59: '</pre> caused <pre>'.$error);
60: }
61: return $result
62: }
63:
64: sub run {
65: my ($code,$safeeval,$hideerrors) = @_;
66: # print "inside run\n";
67: $@='';
68: if ($Apache::lonxml::request) {
69: $Apache::lonxml::request->hard_timeout("Apache::run::run, evaluation $code");
70: }
71: my (@result)=$safeeval->reval($code);
72: if ($Apache::lonxml::request) { $Apache::lonxml::request->kill_timeout; }
73: my $error=$@;
74: if ($error ne '' && !$hideerrors) {
75: &Apache::lonxml::error('<pre>'.&HTML::Entities::encode($error).
76: '</pre> occured while running <pre>'.
77: &HTML::Entities::encode($code).'</pre>');
78: }
79: if ( $#result < '1') {
80: return $result[0];
81: } else {
82: &Apache::lonxml::debug("<b>Got lots results</b>:$#result:");
83: return (@result);
84: }
85: }
86:
87: sub dump {
88: my ($target,$safeeval)=@_;
89: my $dump='';
90: foreach my $symname (sort keys %{$safeeval->varglob('main::')}) {
91: if (($symname!~/^\_/) && ($symname!~/\:$/)) {
92: my $line;
93: if ($safeeval->reval('defined($'.$symname.')')) {
94: $line='$'.$symname.'='.$safeeval->reval('$'.$symname);
95: }
96: if ($safeeval->reval('defined(@'.$symname.')')) {
97: $line='@'.$symname.'=('.
98: $safeeval->reval('join(",",@'.$symname.')').")";
99: }
100: if ($safeeval->reval('defined(%'.$symname.')')) {
101: $line='%'.$symname.'=(';
102: $line.=$safeeval->reval('join(",",map { $_."=>".$'.
103: $symname.'{$_} } sort keys %'.
104: $symname.')').")"
105: }
106: if ($line ne '') { $dump.=&HTML::Entities::encode($line)."<br />\n"; }
107: }
108: }
109: $dump.='';
110: return $dump;
111: }
112:
113: 1;
114: __END__;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>