[system] / trunk / webwork / system / courseScripts / dangerousMacros.pl Repository:
ViewVC logotype

View of /trunk/webwork/system/courseScripts/dangerousMacros.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 28 - (download) (as text) (annotate)
Tue Jun 19 20:56:08 2001 UTC (11 years, 11 months ago) by gage
File size: 39537 byte(s)
Fixed problem in dangerousMacros.pl defining i so that it can be used
as a complex number.

Fixed htmlLink so that it gives a friendly error message about broken links
when fed an undefined url.

    1 #!/usr/local/bin/webwork-perl
    2 
    3 
    4 ####################################################################
    5 # Copyright @ 1995-1999 University of Rochester
    6 # All Rights Reserved
    7 ####################################################################
    8 
    9 ####################################################################
   10 #
   11 #  dangerousMacros.pl contains macros with potentially dangerous commands
   12 #  such as require and eval.  They can reference disk files for reading and
   13 #  writing and can create links.  It may be necessary to modify certain addresses
   14 #  in this file to make the scripts run in different environments.
   15 #
   16 #
   17 
   18 =head1 NAME
   19 
   20    dangerousMacros.pl --- located in the courseScripts directory
   21 
   22 =head1 SYNPOSIS
   23 
   24   loadMacros(macrofile1,macrofile2,...)
   25 
   26   insertGraph(graphObject);
   27     returns a path to the file containing the graph image.
   28 
   29   tth(texString)
   30     returns an HTML version of the tex code passed to it.
   31 
   32   alias(pathToFile);
   33     returns URL which links to that file
   34 
   35 
   36 =head1 DESCRIPTION
   37 
   38 
   39 C<dangerousMacros.pl> contains macros with potentially dangerous commands
   40 such as require and eval.  They can reference disk files for reading and
   41 writing and can create links.  It may be necessary to modify certain addresses
   42 in this file to make the scripts run properly in different environments.
   43 
   44 C<dangerousMacros.pl> is loaded and reinitialized
   45 every time a new problem is rendered.
   46 
   47 =cut
   48 
   49 
   50 ######## Dangerous macros#########
   51 ## The macros in this file are defined while the safe compartment is wide open.
   52 ## Be careful!
   53 #########################################
   54 
   55 
   56 =head2 Sharing modules:
   57 
   58 Most modules are loaded by dangerousMacros.pl
   59 
   60 The modules must be loaded using require (not use) since the courseScriptsDirectory is
   61 defined at run time.
   62 
   63 
   64  The following considerations come into play.
   65 
   66   * One needs to limit the access to modules for safety -- hence only
   67    modules in the F<courseScriptsDirectory> can be loaded.
   68 
   69   * Loading them in dangerousMacros.pl is wasteful, since the modules
   70    would need to be reloaded everytime a new safe compartment is created.
   71      (I believe that using require takes care of this.)
   72 
   73   * Loading GD within a safeCompartment creates infinite recurrsion in AUTOLOAD (probably a bug)
   74    hence this module is loaded by translate.pl and then shared with
   75    the safe compartment.
   76 
   77   * Other modules loaded by translate.pl are C<Exporter> and C<DynaLoader.
   78 
   79   * PGrandom is loaded by F<PG.pl> , since it is needed there.
   80 
   81 
   82 
   83 The module name spaces loaded in dangerousMacros are:
   84 
   85   PGrandom (if not previously loaded)
   86   WWPlot
   87   Fun
   88   Label
   89   Circle
   90 
   91 in addition the  subroutine &evaluate_units is shared from the module Units.
   92 
   93 =cut
   94 
   95 
   96 
   97 
   98 
   99 BEGIN {
  100   be_strict(); # an alias for use strict.  This means that all global variable must contain main:: as a prefix.
  101 }
  102 sub _dangerousMacros_init {
  103 }
  104 
  105 sub _dangerousMacros_export {
  106   my @EXPORT= (
  107       '&_dangerousMacros_init',
  108     '&alias',
  109     '&compile_file',
  110     '&insertGraph',
  111     '&loadMacros',
  112     '&HEADER_TEXT',
  113     '&sourceAlias',
  114     '&tth',
  115   );
  116   @EXPORT;
  117 }
  118 
  119 
  120 =head2 loadMacros
  121 
  122 C<loadMacros(macrofile1,macrofile2,...)>
  123 
  124 loadMacros takes a list of file names and evaluates the contents of each file.  This is used to load macros
  125 which define and augment the PG language.  The macro files are first searched for in the macro
  126 directory of the course C<($macroDirectory)> and then, if not found, in the WeBWorK courseScripts
  127 directory C<($courseScriptsDirectory)> where the default behavior of the PG language is defined.
  128 
  129 An individual course can modify the PG language, B<for that course only>, by
  130 duplicating one of the macro files in the courseScripts directory and placing this
  131 file in the macro directory for the course. The new file in the course
  132 macro directory will now be used instead of the file in the courseScripts directory.
  133 
  134 The new file in the course macro directory can by modified by adding macros or modifying existing macros.
  135 
  136 I< Modifying macros is for users with some experience.>
  137 
  138 Modifying existing macros might break other standard macros or problems which depend on the
  139 unmodified behavior of these macors so do this with great caution.
  140 In addition problems which use new macros defined in these files or which depend on the
  141 modified behavior of existing macros will not work in other courses unless the macros are also
  142 transferred to the new course.  It helps to document the  problems by indicating any special macros
  143 which the problems require.
  144 
  145 There is no facility for modifying or overloading a single macro.  The entire file containing the macro
  146 must be overloaded.
  147 
  148 Modifications to files in the course macros directory affect only that course,
  149 they will not interfere with the normal behavior of B<WeBWorK> in other courses.
  150 
  151 
  152 
  153 =cut
  154 
  155 # Global variables used
  156 #   ${main::macroDirectory}
  157 # ${main::courseScriptsDirectory}
  158 # Global macros used
  159 # None
  160 
  161 # Because of the need to use the directory variables it is tricky to define this
  162 # in translate.pl since, as currently written, the directories are not available
  163 # at that time.  Perhaps if I rewrite translate as an object that method will work.
  164 
  165 # The only difficulty with defining loadMacros inside the Safe compartment is that
  166 # the error reporting does not work with syntax errors.
  167 # A kludge using require works around this problem
  168 
  169 
  170 my ($macroDirectory,
  171   $courseScriptsDirectory,
  172   $templateDirectory,
  173   $scriptDirectory,
  174   );
  175 
  176 sub loadMacros {
  177     my @files = @_;
  178     my $fileName;
  179     ###############################################################################
  180   # At this point the directories have been defined from %envir and we can define
  181   # the directories for this file
  182   ###############################################################################
  183 
  184     $macroDirectory = eval('$main::macroDirectory') unless defined($macroDirectory);
  185     $courseScriptsDirectory = eval('$main::courseScriptsDirectory') unless defined($courseScriptsDirectory);
  186     $templateDirectory = eval('$main::courseScriptsDirectory') unless defined($templateDirectory);
  187     $scriptDirectory = eval('$main::scriptDirectory') unless defined($scriptDirectory);
  188 
  189     # Hack to handle those problems where DOCUMENT() comes after loadMacros.
  190     unless (defined( $macroDirectory)) {
  191       warn "The macroDirectory |$macroDirectory| variable was not initialized.\n" ,
  192            "  Please make sure that the DOCUMENT() statement comes before\n" ,
  193            " the loadMacros() statement in the problem template."
  194     }
  195     while (@files) {
  196         $fileName = shift @files;
  197         next  if ($fileName =~ /^PG.pl$/) ;    # the PG.pl macro package is already loaded.
  198 
  199         my $macro_file_name = $fileName;
  200     $macro_file_name =~s/\.pl//;  # trim off the extenstion
  201     my $init_subroutine_name = "_${macro_file_name}_init";
  202       my $macro_file_loaded;
  203     #no strict;
  204     ###############################################################################
  205     # For some reason the "no stict" which works on webwork-db doesn't work on
  206     # webwork.  For this reason the constuction &{$init_subroutine_name}
  207     # was abandoned and replaced by eval.  This is considerably more dangerous
  208     # since one could hide something nasty in a file name.
  209     #  Keep an eye on this ???
  210     # webwork-db used perl 5.6.1 and webwork used perl 5.6.0  It seems
  211     # unlikely that this was the problem. Otherwise all files seemed to
  212     # be the same.
  213     ###############################################################################
  214 
  215     local($temp::rf_init_subroutine);
  216     eval qq{ \$temp::rf_init_subroutine = \\&main::$init_subroutine_name;};
  217 
  218 
  219 
  220 
  221 
  222     $macro_file_loaded  = defined($temp::rf_init_subroutine) && defined( &{$temp::rf_init_subroutine} );
  223 
  224         # macros are searched for first in the $macroDirectory of the course
  225         # and then in the webwork  $courseScripts directory.
  226         unless ($macro_file_loaded) {
  227           #print "loading macro file $fileName\n";
  228       if (-r "${main::macroDirectory}$fileName") {
  229         compile_file("${main::macroDirectory}$fileName");
  230 
  231       } elsif (-r  "${main::courseScriptsDirectory}$fileName" ) {
  232          compile_file("${main::courseScriptsDirectory}$fileName");
  233       } else {
  234         die "Can't locate macro file via path: |${main::macroDirectory}$fileName| or |${main::courseScriptsDirectory}$fileName|";
  235       }
  236     }
  237     if ( defined($temp::rf_init_subroutine) and defined( &{$temp::rf_init_subroutine} ) ) {
  238         #print " &$init_subroutine_name defined = ", $macro_file_loaded,"\n";
  239       &{$temp::rf_init_subroutine}();  #initialize file
  240       #print "initializing $init_subroutine_name\n";
  241     }
  242 
  243   }
  244 }
  245 
  246 # errors in compiling macros is not always being reported.
  247 sub compile_file {
  248   my $filePath = shift;
  249   local(*MACROFILE);
  250   local($/);
  251   $/ = undef;   # allows us to treat the file as a single line
  252   open(MACROFILE, "<$filePath") || die "Cannot open file: $filePath";
  253   my $string = <MACROFILE>;
  254   my ($result,$error,$fullerror) = PG_restricted_eval($string);
  255   if ($error) {    # the $fullerror report has formatting and is never empty
  256     $fullerror =~ s/\(eval \d+\)/ $filePath\n/;   # attempt to insert file name instead of eval number
  257     die "Error detected while loading $filePath:\n$fullerror";
  258 
  259   }
  260 
  261   close(MACROFILE);
  262 
  263 }
  264 
  265 # This creates on the fly graphs
  266 
  267 =head2 insertGraph
  268 
  269   $filePath = insertGraph(graphObject);
  270       returns a path to the file containing the graph image.
  271 
  272 insertGraph(graphObject) writes a gif file to the C<html/tmp/gif> directory of the current course.
  273 The file name
  274 is obtained from the graphObject.  Warnings are issued if errors occur while writing to
  275 the file.
  276 
  277 The permissions and ownership of the file are controlled by C<$main::tmp_file_permission>
  278 and C<$main::numericalGroupID>.
  279 
  280 B<Returns:>   A string containing the full path to the temporary file containing the GIF image.
  281 
  282 
  283 
  284 InsertGraph draws the object $graph, stores it in "${tempDirectory}gif/$gifName.gif (or .png)" where
  285 the $imageName is obtained from the graph object.  ConvertPath and surePathToTmpFile are used to insure
  286 that the correct directory separators are used for the platform and that the necessary directories
  287 are created if they are not already present.
  288 
  289 The directory address to the file is the result.  This is most often used in the construct
  290 
  291   TEXT(alias(insertGraph($graph)) );
  292 
  293 where alias converts the directory address to a URL when serving HTML pages and insures that
  294 an eps file is generated when creating TeX code for downloading.
  295 
  296 =cut
  297 
  298 # Global variables used:
  299 # $main::tmp_file_permission,
  300 # $main::numericalGroupID
  301 
  302 #Global macros used:
  303 #   &convertPath
  304 #   &surePathToTmpFile
  305 
  306 sub insertGraph {
  307         # Convert the image to GIF and print it on standard output
  308   my $graph = shift;
  309   my $extension = ($WWPlot::use_png) ? '.png' : '.gif';
  310   my $fileName = $graph->imageName  . $extension;
  311   my $filePath = convertPath("gif/$fileName");
  312   $filePath = &surePathToTmpFile( $filePath );
  313   #createFile($filePath, $main::tmp_file_permission, $main::numericalGroupID);
  314   local(*OUTPUT);  # create local file handle so it won't overwrite other open files.
  315   open(OUTPUT, ">$filePath")||warn ("$0","Can't open $filePath<BR>","");
  316   chmod( 0777, $filePath);
  317   print OUTPUT $graph->draw|| warn("$0","Can't print graph to $filePath<BR>","");
  318   close(OUTPUT)||warn("$0","Can't close $filePath<BR>","");
  319   $filePath;
  320 }
  321 
  322 
  323 
  324 =head2 tth
  325 
  326   tth(texString)
  327       returns an HTML version of the tex code passed to it.
  328 
  329 This macro sends the texString to the filter program C<tth> created by Ian Hutchinson.
  330 The tth program was created by Ian Hutchinson and is freely available
  331 for B<non-commerical purposes> at the C<tth> main site: C<http://hutchinson.belmont.ma.us/tth/>.
  332 
  333 The purpose of C<tth> is to translate text in the TeX or Latex markup language into
  334 HTML markup as best as possible.  Some symbols, such as square root symbols are not
  335 translated completely.  Macintosh users must use the "MacRoman" encoding (available in 4.0 and
  336 higher browsers) in order to view the symbols correctly.  WeBWorK attempts to force Macintosh
  337 browsers to use this encoding when such a browser is detected.
  338 
  339 The contents of the file C<tthPreamble.tex> in the courses template directory are prepended
  340 to each string.  This allows one to define TeX macros which can be used in every problem.
  341 Currently there is no default C<tthPreamble.tex> file, so if the file is not present in the
  342 course template directory no TeX macro definitions are prepended.  C<tth> already understands most
  343 Latex commands, but will not, in general know I<AMS-Latex> commands.  Additional information
  344 on C<tth> is available at the C<tth> main site.
  345 
  346 This macro contains code which is system dependent and may need to be modified
  347 to run on different systems.
  348 
  349 =for html
  350 The link to <CODE>tth</CODE> for <STRONG>non-commerical</STRONG> is
  351 <A HREF="http://hutchinson.belmont.ma.us/tth/">http://hutchinson.belmont.ma.us/tth/</A>.
  352 Binaries for many operating systems are available as well as the source code.  Links
  353 describing how to obtain <CODE>tth</CODE> for commerical use are also available on this page.
  354 
  355 =cut
  356 
  357 
  358 
  359 # This file allows the tth display.
  360 # Global variables:
  361 # ${main::templateDirectory}tthPreamble.tex   # location of any preamble TeX commands for tth
  362 #   ${main::templateDirectory}
  363 #   ${main::scriptDirectory}tth        # path to tth application
  364 # Global macros:
  365 #   None
  366 
  367 my ($tthPreambleFile, $tthPreambleContents); # the contents of this file will not change during problem compilation
  368                                              # it only needs to be read once
  369 sub tth {
  370   my $inputString = shift;
  371 
  372   # read the contents of the tthPreamble.tex file, unless it has already been read
  373   unless ( defined( $tthPreambleContents) ) {
  374     $tthPreambleFile = "${main::templateDirectory}tthPreamble.tex" if ( -r "${main::templateDirectory}tthPreamble.tex" );
  375     if ( defined($tthPreambleFile) )   {
  376       local(*TTHIN);
  377       open (TTHIN, "${main::templateDirectory}tthPreamble.tex") || die "Can't open file ${main::templateDirectory}tthPreamble.tex";
  378       #my @tthPreambleArray = <TTHIN>;
  379       local($/);
  380       $/ = undef;
  381       $tthPreambleContents = <TTHIN>;#join("",@tthPreambleArray);
  382       close(TTHIN);
  383 
  384       $tthPreambleContents =~ s/(.)\n/$1%\n/g;  # thanks to Jim Martino
  385                                                 # each line in the definition file
  386                                                 # should end with a % to prevent
  387                                                 # adding supurious paragraphs to output.
  388 
  389       $tthPreambleContents .="%\n";             # solves the problem if the file doesn't end with a return.
  390 
  391     } else {
  392       $tthPreambleContents = "";
  393     }
  394   }
  395 
  396     $inputString = $tthPreambleContents . $inputString;
  397     $inputString    = "<<END_OF_TTH_INPUT_STRING;\n\n\n" . $inputString . "\nEND_OF_TTH_INPUT_STRING\necho \"\" >/dev/null"; #it's not clear why another command is needed.
  398     ##WARNING -- how should we get the value of $scriptDirectory to this macro?
  399     ## perhaps tth belongs in displayMacros???
  400     my $tthpath     = "/usr/local/bin/tth";
  401     my $out;
  402 
  403     if (-x $tthpath ) {
  404       my $tthcmd      = "$tthpath -L -f5 -r 2>/dev/null " . $inputString;
  405       if (open(TTH, "$tthcmd   |")) {
  406           local($/);
  407       $/ = undef;
  408       $out = <TTH>;
  409       $/ = "\n";
  410       close(TTH);
  411       }else {
  412           $out = "<BR>there has been an error in executing $tthcmd<BR>";
  413       }
  414   } else {
  415     $out = "<BR> Can't execute the program tth at |$tthpath|<BR>";
  416     }
  417 
  418     $out;
  419 }
  420 
  421 
  422 
  423 =head2  alias
  424 
  425   alias(pathToFile);
  426     returns A string describing the URL which links to GIF or html file
  427             (in HTML and Latex2HTML modes).
  428             or a path to the appropriate eps version of a GIF file
  429              (TeX Mode)
  430 
  431 
  432 
  433 C<alias> allows you to refer to auxiliary files which are in a directory along with
  434 the problem definition.  In addition alias creates an eps copy of GIF files when
  435 downloading hard copy (TeX mode).
  436 
  437 As a rule auxiliary files that are used by
  438 a number of problems in a course should be placed in C<html/gif> or C<html>
  439 or in a subdirectory of the C<html> directory,
  440 while auxiliary files which are used in only one problem should be placed in
  441 the same directory as the problem in order to make the problem more portable.
  442 
  443 
  444 
  445 =over 4
  446 
  447 =item  Files in the html subdirectory
  448 
  449 B<When not in TeX mode:>
  450 
  451 If the file lies under the C<html> subdirectory, then the approriate URL for the file is created.
  452 Since the C<html> subdirectory is already accessible to the webserver no other changes need to be made.
  453 The file path for this type of file should be the complete file path. The path should
  454 start with the prefix defined in $Global:htmlDirectory.
  455 
  456 B<When in TeX mode:>
  457 
  458 
  459 GIF files will be translated into an eps file (using system dependent code)
  460 and placed in the directory C<tmp/eps>.  The full path to this file is returned
  461 for use by TeX in producing the hard copy. (This should work even in a chrooted
  462 environment.) in producing the hard copy.   (This should work even in a chrooted
  463 environment.)
  464 
  465 The conversion is done by a system dependent script
  466 called C<gif2eps> which should be in the scripts directory
  467 
  468 The URL's for the other files are produced as in non-tex mode
  469 but will of course not be active.
  470 
  471 =item  Files in the tmp subdirectory
  472 
  473 B<When not in TeX mode:>
  474 
  475 If the file lies under the C<tmp> subdirectory, then the approriate URL for the file is created.
  476 Since the C<tmp> subdirectory is already accessible to the webserver no other changes need to be made.
  477 The file path for this type of file should be the complete file path. The path should
  478 start with the prefix defined in $Global:tempDirectory.
  479 
  480 B<When in TeX mode:>
  481 
  482 
  483 GIF files will be translated into an eps file (using system dependent code)
  484 and placed in the directory C<tmp/eps>.  The full path to this file is returned
  485 for use by TeX in producing the hard copy.  (This should work even in a chrooted
  486 environment.)
  487 
  488 The conversion is done by a system dependent script
  489 called C<gif2eps> which should be in the scripts directory
  490 
  491 The URL's for the other files are produced as in non-tex mode
  492 but will of course not be active.
  493 
  494 =item Files in the course template subdirectory:
  495 
  496 B<When not in TeX mode:>
  497 
  498 If the file lies under the course templates subdirectory,
  499 it is assumed to lie in subdirectory rooted in the directory
  500 containing the problem template file.
  501 An alias is created under the C<html/tmp/gif> or
  502 C<html/tmp/html> directory and linked to the original file.
  503 The file path for this type of file is a relative
  504 path rooted at the directory containing the problem template file.
  505 
  506 B<When in TeX mode:>
  507 
  508 GIF files will be translated into an eps file (using system dependent code)
  509 and placed in the directory C<html/tmp/eps>.  The full path to this file is returned
  510 for use by TeX in producing the hard copy.   (This should work even in a chrooted
  511 environment.)
  512 
  513 The conversion is done by a system dependent script
  514 called C<gif2eps> which should be in the scripts directory
  515 
  516 The URL's for the other files are produced as in non-tex mode
  517 but will of course not be active.
  518 
  519 =back
  520 
  521 =cut
  522 
  523 
  524 
  525 # Currently gif, html and types are supported.
  526 #
  527 # If the auxiliary file path has not extension then the extension .gif isassumed.
  528 #
  529 # If the auxiliary file path leads to a file in the ${Global::htmlDirectory}
  530 # no changes are made to the file path.
  531 #
  532 # If the auxiliary file path is not complete, than it is assumed that it refers
  533 # to a subdirectoy of the directory containing the problem..
  534 #
  535 # The output is either the correct URL for the file
  536 # or (in TeX mode) the complete path to the eps version of the file
  537 # and can be used as input into the image macro.
  538 #
  539 # surePathToTmpFile takes a path and outputs the complete path:
  540 # ${main::htmlDirectory}/tmp/path
  541 # It insures that all of the directories in the path have been created,
  542 # but does not create the
  543 # final file.
  544 
  545 # For postscript printing, alias generates an eps version of the gif image and places
  546 # it in the directory eps.  This slows down downloading postscript versions somewhat,
  547 # but not excessivevly.
  548 # Alias does not do any garbage collection, so files and alias may accumulate and
  549 # need to be removed manually or by a reaper daemon.
  550 
  551 
  552 # Global variables used:
  553 #  $main::fileName  # the full path to the current problem template file
  554 #  $main::htmlDirectory
  555 #  $main::htmlURL
  556 #  $main::tempDirectory
  557 #  $main::tempURL
  558 #  $main::studentLogin
  559 #  $main::psvnNumber
  560 #  $main::setNumber
  561 #  $main::probNum
  562 #  $main::displayMode
  563 
  564 # Global macros used
  565 # gif2eps   An external file called by system
  566 # surePathToTmpFile
  567 # convertPath
  568 # directoryFromPath
  569 
  570 
  571 # This subroutine  has commands which will not work on non-UNIX environments.
  572 # system("cat $gifSourceFile  | /usr/math/bin/giftopnm | /usr/math/bin/pnmdepth 1 | /usr/math/bin/pnmtops -noturn>$adr_output") &&
  573 
  574 
  575 # local constants $User, $psvn $setNumber $probNum $displayMode
  576 
  577 sub sourceAlias {
  578   my $path_to_file = shift;
  579   my ($user,$err,$errmsg) = PG_restricted_eval('$main::inputs_ref->{user}');
  580   $user = " " unless defined($user);
  581     my ($out,$err2,$errmsg2) = PG_restricted_eval(q{"source.pl?probSetKey=$main::psvn".
  582         "&amp;probNum=$main::probNum" .
  583         "&amp;Mode=$main::displayMode" .
  584         "&amp;course=". $main::courseName .
  585         "&amp;user=" . $user .
  586       "&amp;displayPath=$path_to_file" .
  587         "&amp;key=". $main::sessionKey;}
  588    );
  589    $out;
  590 
  591 }
  592 
  593 
  594 sub alias {
  595     # input is a path to the original auxiliary file
  596     unless ( defined($main::tempURL) ) {
  597     PG_restricted_eval( q{
  598     $fileName = $main::fileName;
  599     $htmlDirectory = $main::htmlDirectory;
  600     $htmlURL = $main::htmlURL;
  601     $tempDirectory = $main::tempDirectory;
  602     $tempURL =  $main::tempURL;
  603     $studentLogin =  $main::studentLogin;
  604     $psvnNumber =  $main::psvnNumber;
  605     $setNumber =  $main::setNumber;
  606     $probNum =  $main::probNum;
  607     $displayMode =  $main::displayMode;
  608     });
  609   }
  610   my $aux_file_path = shift @_;
  611 warn "Empty string used as input into the function alias" unless $aux_file_path;
  612 # problem specific data
  613 warn "The path to the current problem file template is not defined." unless $main::fileName;
  614 warn "The current studentLogin is not defined " unless $main::studentLogin;
  615 warn "The current problem set number is not defined" if $main::setNumber eq ""; # allow for sets equal to 0
  616 warn "The current problem number is not defined"  if $main::probNum eq "";
  617 warn "The current problem set version number (psvn) is not defined" unless $main::psvnNumber;
  618 warn "The displayMode is not defined" unless $main::displayMode;
  619 # required macros
  620 warn "The macro &surePathToTmpFile can't be found" unless defined(&surePathToTmpFile);
  621 warn "The macro &convertPath can't be found" unless defined(&convertPath);
  622 warn "The macro &directoryFromPath can't be found" unless defined(&directoryFromPath);
  623 warn "Can't execute the gif2eps script at ${main::scriptDirectory}gif2eps" unless ( -x "${main::scriptDirectory}gif2eps" );
  624 warn "Can't execute the png2eps script at ${main::scriptDirectory}png2eps" unless ( -x "${main::scriptDirectory}png2eps" );
  625 # required directory addresses (and URL address)
  626 warn "htmlDirectory is not defined in $main::htmlDirectory" unless $main::htmlDirectory;
  627 warn "htmlURL is not defined in \$main::htmlURL" unless $main::htmlURL;
  628 warn "tempURL is not defined in \$main::tempURL" unless $main::tempURL;
  629 warn "The scripts directory is not defined in \$main::scriptDirectory" unless $main::scriptDirectory;
  630 
  631 # determine extension, if there is one
  632 #if extension exists, strip and use the value for $ext
  633 # files without extensions are considered to be picture files:
  634 
  635   my $ext;
  636   if ($aux_file_path =~ s/\.([^\.]*)$// ) {
  637     $ext = $1;
  638   } else {
  639       warn "This file name $aux_file_path did not have an extension.<BR> " .
  640             "Every file name used as an argument to alias must have an extension.<BR> " .
  641              "The permissable extensions are .gif, .png, and .html .<BR>";
  642     $ext  = "gif";
  643 
  644 
  645   }
  646 
  647 # $adr_output is a url in HTML and Latex2HTML modes
  648 # and a complete path in TEX mode.
  649   my $adr_output;
  650 
  651 # in order to facilitate maintenance of this macro the routines for handling
  652 # different file types are defined separately.  This involves some redundancy
  653 # in the code but it makes it easier to define special handling for a new file
  654 # type, (but harder to change the behavior for all of the file types at once
  655 # (sigh)  ).
  656 #####################################################
  657 #   .html FILES in HTML, HTML_tth and Latex2HTML mode
  658 #####################################################
  659 
  660   if ($ext eq 'html') {
  661 #   No changes are made for auxiliary files in the
  662 #       ${Global::htmlDirectory} subtree.
  663       if ( $aux_file_path =~ m|^$main::tempDirectory| ) {
  664         $adr_output = $aux_file_path;
  665         $adr_output =~ s|$main::tempDirectory|$main::tempURL/|;
  666         $adr_output .= ".$ext";
  667       } elsif ($aux_file_path =~ m|^$main::htmlDirectory| ) {
  668         $adr_output = $aux_file_path;
  669         $adr_output =~ s|$main::htmlDirectory|$main::htmlURL|;
  670         $adr_output .= ".$ext";
  671     } else {
  672 
  673 #     HTML files not in the htmlDirectory are assumed under live under the
  674 #       templateDirectory in the same directory as the problem.
  675 #       Create an alias file (link) in the directory html/tmp/html which
  676 #   points to the original file and return the URL of this alias.
  677 #       Create all of the subdirectories of html/tmp/html which are needed
  678 #       using sure file to path.
  679           # $fileName is obtained from environment for PGeval
  680           # it gives the  full path to the current problem
  681           my $filePath = directoryFromPath($main::fileName);
  682           my $htmlFileSource = convertPath("$main::templateDirectory${filePath}$aux_file_path.html");
  683                 my $link = "html/$main::studentLogin-$main::psvnNumber-set$main::setNumber-prob$main::probNum-$aux_file_path.$ext";
  684           my $linkPath = surePathToTmpFile($link);
  685           $adr_output = "${main::tempURL}$link";
  686                 if (-e $htmlFileSource) {
  687               if (-e $linkPath) {
  688                   unlink($linkPath) || warn "Unable to unlink alias file at |$linkPath|";
  689                   # destroy the old link.
  690               }
  691             symlink( $htmlFileSource, $linkPath)  ||
  692             warn "The macro alias cannot create a link from |$linkPath|  to |$htmlFileSource| <BR>" ;
  693             } else {
  694             warn("The macro alias cannot find an HTML file at: |$htmlFileSource|");
  695         }
  696     }
  697 
  698 #####################################################
  699 #   .gif FILES in HTML, HTML_tth, Latex2HTML and TeX modes
  700 #####################################################
  701 
  702   } elsif ($ext eq 'gif') {
  703     if ( $main::displayMode eq 'HTML' ||
  704          $main::displayMode eq 'HTML_tth'||
  705          $main::displayMode eq 'Latex2HTML')  {
  706 #     warn "tempDirectory is $main::tempDirectory";
  707 #     warn "file Path for auxiliary file is $aux_file_path";
  708 #       No changes are made for auxiliary files in the htmlDirectory or in the tempDirectory subtree.
  709       if ( $aux_file_path =~ m|^$main::tempDirectory| ) {
  710         $adr_output = $aux_file_path;
  711         $adr_output =~ s|$main::tempDirectory|$main::tempURL|;
  712         $adr_output .= ".$ext";
  713 #       warn "adress out is $adr_output";
  714       } elsif ($aux_file_path =~ m|^$main::htmlDirectory| ) {
  715         $adr_output = $aux_file_path;
  716         $adr_output =~ s|$main::htmlDirectory|$main::htmlURL|;
  717         $adr_output .= ".$ext";
  718       } else {
  719 #              files not in the htmlDirectory sub tree are assumed to live under the templateDirectory
  720 #              subtree in the same directory as the problem.
  721         # For a gif file the alias macro creates an alias under the html/images directory
  722         # which points to the gif file in the problem directory.
  723         # All of the subdirectories of html/tmp/gif which are needed are also created.
  724           my $filePath = directoryFromPath($main::fileName);
  725 
  726           # $fileName is obtained from environment for PGeval
  727           # it gives the full path to the current problem
  728         my $gifSourceFile = convertPath("$main::templateDirectory${filePath}$aux_file_path.gif");
  729                 my $link = "gif/$main::studentLogin-$main::psvnNumber-set$main::setNumber-prob$main::probNum-$aux_file_path.$ext";
  730           my $linkPath = surePathToTmpFile($link);
  731           $adr_output = "${main::tempURL}$link";
  732           #warn "linkPath is $linkPath";
  733           #warn "adr_output is $adr_output";
  734           if (-e $gifSourceFile) {
  735               if (-e $linkPath) {
  736                   unlink($linkPath) || warn "Unable to unlink old alias file at $linkPath";
  737               }
  738 
  739             symlink($gifSourceFile, $linkPath) ||
  740             warn "The macro alias cannot create a link from |$linkPath|  to |$gifSourceFile| <BR>" ;
  741             } else {
  742             warn("The macro alias cannot find a GIF file at: |$gifSourceFile|");
  743         }
  744       }
  745 #####################################################
  746 #   .gif FILES in TeX mode
  747 #####################################################
  748 
  749     } elsif ($main::displayMode eq 'TeX') {
  750 
  751     ################################################################################
  752     ####
  753     ## This is statement used below is system dependent.
  754     # Notice that the range of colors is restricted when converting to postscript to keep the files small
  755     # "cat $gifSourceFile  | /usr/math/bin/giftopnm |/usr/math/bin/pnmtops -noturn>$adr_output"
  756     #   "cat $gifSourceFile  | /usr/math/bin/giftopnm | /usr/math/bin/pnmdepth 1 | /usr/math/bin/pnmtops -noturn>$adr_output"
  757     ####
  758     ################################################################################
  759 
  760         if ($aux_file_path =~  m|^$main::htmlDirectory|  or $aux_file_path =~  m|^$main::tempDirectory|)  {
  761 
  762         # To serve an eps file copy an eps version of the gif file to the subdirectory of eps/
  763             my $linkPath = directoryFromPath($main::fileName);
  764 
  765           my $gifSourceFile = "$aux_file_path.gif";
  766           my $gifFileName = fileFromPath($gifSourceFile);
  767           $adr_output = surePathToTmpFile("$main::tempDirectory/eps/$main::studentLogin-$main::psvnNumber-$gifFileName.eps") ;
  768 
  769           if (-e $gifSourceFile) {
  770              #system("cat $gifSourceFile  | /usr/math/bin/giftopnm | /usr/math/bin/pnmdepth 1 | /usr/math/bin/pnmtops -noturn>$adr_output") &&
  771              system("${main::scriptDirectory}gif2eps $gifSourceFile $adr_output" ) &&
  772                die "Unable to create eps file:\n |$adr_output| from file\n |$gifSourceFile|\n in problem $main::probNum " .
  773                    "using the system dependent script\n |${main::scriptDirectory}gif2eps| \n";
  774 
  775           } else {
  776               die "|$gifSourceFile| cannot be found.  Problem number: |$main::probNum|";
  777             }
  778 
  779         } else  {
  780         # To serve an eps file copy an eps version of the gif file to  a subdirectory of eps/
  781             my $filePath = directoryFromPath($main::fileName);
  782           my $gifSourceFile = "${main::templateDirectory}${filePath}$aux_file_path.gif";
  783           #print "content-type: text/plain \n\nfileName = $fileName and aux_file_path =$aux_file_path<BR>";
  784           $adr_output = surePathToTmpFile("eps/$main::studentLogin-$main::psvnNumber-set$main::setNumber-prob$main::probNum-$aux_file_path.eps") ;
  785           if (-e $gifSourceFile) {
  786             #system("cat $gifSourceFile  | /usr/math/bin/giftopnm | /usr/math/bin/pnmdepth 1 | /usr/math/bin/pnmtops -noturn>$adr_output") &&
  787             #warn "Unable to create eps file: |$adr_output|\n from file\n |$gifSourceFile|\n in problem $main::probNum";
  788             #warn "Help ${main::scriptDirectory}gif2eps" unless -x "${main::scriptDirectory}gif2eps";
  789                       system("${main::scriptDirectory}gif2eps $gifSourceFile $adr_output" ) &&
  790               die "Unable to create eps file:\n |$adr_output| from file\n |$gifSourceFile|\n in problem $main::probNum " .
  791                    "using the system dependent script\n |${main::scriptDirectory}gif2eps| \n ";
  792 
  793 
  794           }  else {
  795             die "|$gifSourceFile| cannot be found.  Problem number: |$main::probNum|";            }
  796         }
  797 
  798     } else {
  799       wwerror("Error in alias: dangerousMacros.pl","unrecognizable displayMode = $main::displayMode","");
  800     }
  801 #####################################################
  802 #   .png FILES in HTML, HTML_tth, Latex2HTML and TeX modes
  803 #####################################################
  804 
  805   } elsif ($ext eq 'png') {
  806     if ( $main::displayMode eq 'HTML' ||
  807          $main::displayMode eq 'HTML_tth'||
  808          $main::displayMode eq 'Latex2HTML')  {
  809 #     warn "tempDirectory is $main::tempDirectory";
  810 #     warn "file Path for auxiliary file is $aux_file_path";
  811 #       No changes are made for auxiliary files in the htmlDirectory or in the tempDirectory subtree.
  812       if ( $aux_file_path =~ m|^$main::tempDirectory| ) {
  813         $adr_output = $aux_file_path;
  814         $adr_output =~ s|$main::tempDirectory|$main::tempURL|;
  815         $adr_output .= ".$ext";
  816 #       warn "adress out is $adr_output";
  817       } elsif ($aux_file_path =~ m|^$main::htmlDirectory| ) {
  818         $adr_output = $aux_file_path;
  819         $adr_output =~ s|$main::htmlDirectory|$main::htmlURL|;
  820         $adr_output .= ".$ext";
  821       } else {
  822 #              files not in the htmlDirectory sub tree are assumed to live under the templateDirectory
  823 #              subtree in the same directory as the problem.
  824         # For a png file the alias macro creates an alias under the html/images directory
  825         # which points to the png file in the problem directory.
  826         # All of the subdirectories of html/tmp/gif which are needed are also created.
  827           my $filePath = directoryFromPath($main::fileName);
  828 
  829           # $fileName is obtained from environment for PGeval
  830           # it gives the full path to the current problem
  831         my $pngSourceFile = convertPath("$main::templateDirectory${filePath}$aux_file_path.png");
  832                 my $link = "gif/$main::studentLogin-$main::psvnNumber-set$main::setNumber-prob$main::probNum-$aux_file_path.$ext";
  833           my $linkPath = surePathToTmpFile($link);
  834           $adr_output = "${main::tempURL}$link";
  835           #warn "linkPath is $linkPath";
  836           #warn "adr_output is $adr_output";
  837           if (-e $pngSourceFile) {
  838               if (-e $linkPath) {
  839                   unlink($linkPath) || warn "Unable to unlink old alias file at $linkPath";
  840               }
  841 
  842             symlink($pngSourceFile, $linkPath) ||
  843             warn "The macro alias cannot create a link from |$linkPath|  to |$pngSourceFile| <BR>" ;
  844             } else {
  845             warn("The macro alias cannot find a PNG file at: |$pngSourceFile|");
  846         }
  847       }
  848 #####################################################
  849 #   .png FILES in TeX mode
  850 #####################################################
  851 
  852     } elsif ($main::displayMode eq 'TeX') {
  853 
  854     ################################################################################
  855     ####
  856     ## This is statement used below is system dependent.
  857     # Notice that the range of colors is restricted when converting to postscript to keep the files small
  858     # "cat $pngSourceFile  | /usr/math/bin/pngtopnm |/usr/math/bin/pnmtops -noturn>$adr_output"
  859     #   "cat $pngSourceFile  | /usr/math/bin/pngtopnm | /usr/math/bin/pnmdepth 1 | /usr/math/bin/pnmtops -noturn>$adr_output"
  860     ####
  861     ################################################################################
  862 
  863         if ($aux_file_path =~  m|^$main::htmlDirectory|  or $aux_file_path =~  m|^$main::tempDirectory|)  {
  864 
  865         # To serve an eps file copy an eps version of the png file to the subdirectory of eps/
  866             my $linkPath = directoryFromPath($main::fileName);
  867 
  868           my $pngSourceFile = "$aux_file_path.png";
  869           my $pngFileName = fileFromPath($pngSourceFile);
  870           $adr_output = surePathToTmpFile("$main::tempDirectory/eps/$main::studentLogin-$main::psvnNumber-$pngFileName.eps") ;
  871 
  872           if (-e $pngSourceFile) {
  873              #system("cat $pngSourceFile  | /usr/math/bin/pngtopnm | /usr/math/bin/pnmdepth 1 | /usr/math/bin/pnmtops -noturn>$adr_output") &&
  874              system("${main::scriptDirectory}png2eps $pngSourceFile $adr_output" ) &&
  875                die "Unable to create eps file:\n |$adr_output| from file\n |$pngSourceFile|\n in problem $main::probNum " .
  876                    "using the system dependent script\n |${main::scriptDirectory}png2eps| \n";
  877 
  878           } else {
  879               die "|$pngSourceFile| cannot be found.  Problem number: |$main::probNum|";
  880             }
  881 
  882         } else  {
  883         # To serve an eps file copy an eps version of the png file to  a subdirectory of eps/
  884             my $filePath = directoryFromPath($main::fileName);
  885           my $pngSourceFile = "${main::templateDirectory}${filePath}$aux_file_path.png";
  886           #print "content-type: text/plain \n\nfileName = $fileName and aux_file_path =$aux_file_path<BR>";
  887           $adr_output = surePathToTmpFile("eps/$main::studentLogin-$main::psvnNumber-set$main::setNumber-prob$main::probNum-$aux_file_path.eps") ;
  888           if (-e $pngSourceFile) {
  889             #system("cat $pngSourceFile  | /usr/math/bin/pngtopnm | /usr/math/bin/pnmdepth 1 | /usr/math/bin/pnmtops -noturn>$adr_output") &&
  890             #warn "Unable to create eps file: |$adr_output|\n from file\n |$pngSourceFile|\n in problem $main::probNum";
  891             #warn "Help ${main::scriptDirectory}png2eps" unless -x "${main::scriptDirectory}png2eps";
  892                       system("${main::scriptDirectory}png2eps $pngSourceFile $adr_output" ) &&
  893               die "Unable to create eps file:\n |$adr_output| from file\n |$pngSourceFile|\n in problem $main::probNum " .
  894                    "using the system dependent script\n |${main::scriptDirectory}png2eps| \n ";
  895 
  896 
  897           }  else {
  898             die "|$pngSourceFile| cannot be found.  Problem number: |$main::probNum|";            }
  899         }
  900 
  901     } else {
  902       wwerror("Error in alias: dangerousMacros.pl","unrecognizable displayMode = $main::displayMode","");
  903     }
  904 #####################################################
  905 #    FILES  with unrecognized file extensions in all display modes
  906 #####################################################
  907 
  908   } else {  # $ext is not recognized
  909     warn "Error in the macro alias. Alias does not understand how to process
  910           files with extension $ext.  (Path ot problem file is  $main::fileName) ";
  911 
  912   }
  913   warn "The macro alias was unable to form a URL for some auxiliary file used in this problem." unless $adr_output;
  914   $adr_output;
  915   ;
  916 
  917 }
  918 
  919 
  920 
  921 
  922 
  923 # Experiments
  924 
  925 # It is important that these subroutines using sort are evaluated before
  926 # the problem template is evaluated.
  927 # Once the problem template has a "my $a;" susequent sort routines will not work.
  928 #
  929 # PGsort can be used as a slightly slower but safer sort within problems.
  930 
  931 
  932 
  933 =head2 PGsort
  934 
  935 Because of the way sort is optimized in Perl, the symbols $a and $b
  936 have special significance.
  937 
  938 C<sort {$a<=>$b} @list>
  939 C<sort {$a cmp $b} @list>
  940 
  941 sorts the list numerically and lexically respectively.
  942 
  943 If C<my $a;> is used in a problem, before the sort routine is defined in a macro, then
  944 things get badly confused.  To correct this, the following macros are defined in
  945 dangerougMacros.pl which is evaluated before the problem template is read.
  946 
  947   PGsort sub { $_[0] <=> $_[1] }, @list;
  948   PGsort sub { $_[0] cmp $_[1] }, @list;
  949 
  950 provide slightly slower, but safer, routines for the PG language. (The subroutines
  951 for ordering are B<required>. Note the commas!)
  952 
  953 =cut
  954 
  955 
  956 
  957 # sub PGsort {
  958 #   my $sort_order = shift;
  959 #   die "Must supply an ordering function with PGsort: PGsort sub {\$a cmp \$b }, \@list\n" unless ref($sort_order) eq 'CODE';
  960 #   sort {&$sort_order($a,$b)} @_;
  961 # }
  962 # Moved to translate.pl
  963 # For some reason it still caused
  964 # trouble here when there was
  965 # more than one ans_eval in ANS()
  966 # No-one knows why?
  967 
  968 # This allows the use of i for  imaginary numbers
  969 #  one can write   3 +2i rather than 3+2i()
  970 #
  971 
  972 sub i;
  973 
  974 1;  # required to load properly

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9