File:  [LON-CAPA] / loncom / xml / run.pm
Revision 1.27: download - view: text, annotated - select for diffs
Fri May 3 22:30:54 2002 UTC (22 years ago) by albertel
Branches: MAIN
CVS tags: HEAD
- better protection of output

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

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