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