File:  [LON-CAPA] / loncom / interface / printout.pl
Revision 1.142.6.4: download - view: text, annotated - select for diffs
Thu Aug 26 08:45:08 2010 UTC (13 years, 8 months ago) by raeburn
Branches: version_2_9_X
CVS tags: version_2_9_1
Diff to branchpoint 1.142: preferred, unified
- Exclude fixps from 2.9.
- Correct 1.142.6.3 (backport of 1.146).

    1: #!/usr/bin/perl
    2: # CGI-script to run LaTeX, dvips, ps2ps, ps2pdf etc.
    3: #
    4: # $Id: printout.pl,v 1.142.6.4 2010/08/26 08:45:08 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: 
   29: use lib '/home/httpd/lib/perl';
   30: use LONCAPA::loncgi;
   31: use File::Path;
   32: use File::Basename;
   33: use File::Copy;
   34: use IO::File;
   35: use Image::Magick;
   36: use Apache::lonhtmlcommon();
   37: use Apache::lonnet;
   38: use Apache::loncommon();
   39: use Apache::lonlocal;
   40: use Apache::lonmsg();
   41: use LONCAPA::Enrollment;
   42: use LONCAPA::Configuration;
   43: 
   44: use strict;
   45: 
   46: my $busy_wait_timeout = 30; 
   47: 
   48: 
   49: my $debugging = 0;
   50: 
   51: sub debug {
   52:     if ($debugging) {
   53: 	my ($text) = @_;
   54: 	print "$text <br />\n";
   55:     }
   56: }
   57: 
   58: #   Determine if a user is operating as a student for this course/domain.
   59: #Parameters:
   60: #    none
   61: #Implicit:
   62: #    $env{request.role} contains the role under which this user operated this
   63: #                       this request.
   64: sub is_student {
   65:     return ($env{'request.role'}=~/^st\./);
   66: }
   67: 
   68: #
   69: #   Debugging:  Dump the environment for debugging.
   70: #
   71: sub dumpenv  {
   72:     print "<br />-------------------<br />";
   73:     foreach my $key (sort (keys %env)) {
   74: 	print "<br />$key -> $env{$key}";
   75:     }
   76:     print "<br />-------------------<br />";
   77: }
   78: 
   79: #
   80: #   This sub sends a message to the appropriate person if there was an error
   81: #   rendering the latex  At present, there's only one case to consider:
   82: #   a student printing inside a course results in messages to the course coordinator.
   83: #Parmaeters:
   84: #    identifier -  The unique identifier of this cgi request.
   85: #    badresource-  Filepath to most likely failing 
   86: #    logfile    -  The contents of the log file (included in the message).
   87: #    texfile    -  reference to an array containing the LaTeX input file
   88: #                  (included in the message).
   89: #Implicit inputs:
   90: #   From the environment:
   91: #       cgi.$identifier.user     - User doing the printing.
   92: #       cgi.$identifier.domain   - Domain the user is logged in on with printing.
   93: #       cgi.$identifier.courseid - Id of the course (if this is a course).
   94: #       cgi.$identifier.coursedom- Domain in which course was constituted.
   95: #       cgi.$identifier.resources - List of resource URL's for which the print
   96: #                                  was attempted.
   97: # 
   98: sub send_error_mail {
   99:     my ($identifier, $badresource, $logfile, $texfile) = @_;
  100:     my $user     = $env{"cgi.$identifier.user"};
  101:     my $domain   = $env{"cgi.$identifier.domain"};
  102:     my $courseid = $env{"cgi.$identifier.courseid"};
  103:     my $coursedom= $env{"cgi.$identifier.coursedom"};
  104:     my $resources= $env{"cgi.$identifier.resources"};
  105: 
  106:     #  resource file->URL
  107:     #
  108:     my $badurl = &Apache::lonnet::declutter($badresource);
  109: 
  110:     # &dumpenv();
  111: 
  112: 
  113: 
  114:     #  Only continue if there is a user, domain, courseid, course domain
  115:     #  and resources:
  116: 
  117:     if(defined($user)       && defined($domain) && defined($courseid) &&
  118:        defined($coursedom)  && defined($resources) ){
  119: 	   
  120: 	#  Only mail if the conditions are ripe for it:
  121: 	#  The user is a student in the course:
  122: 	#
  123: 	
  124: 	if (&is_student()) {
  125: 	    # build the subject and message body:
  126: 	    &debug("sending message to course coordinators.");
  127: 
  128: 	    # Todo: Convert badurl into a url from file path:
  129: 
  130: 	    my $subject  = "Error [$badurl] Print failed for $user".'@'.$domain;
  131: 	    my $message .= "Print failed to render LaTeX for $user".'@'."$domain\n";
  132: 	    $message    .= "  User was attempting to print: \n";
  133: 	    foreach my $resource (split(/:/,$resources)) {
  134: 		$message    .= "       $resource\n";
  135: 	    }
  136: 	    $message    .= "--------------------LaTeX logfile:------------ \n";
  137: 	    $message    .= $logfile;
  138: 	    $message    .= "-----------------LaTeX source file: ------------\n";
  139: 	    
  140: 	    foreach my $line (@$texfile) {
  141: 		$message .= "$line\n";
  142: 	    }
  143: 	    my (undef, %receivers) = &Apache::lonmsg::decide_receiver(undef, 0,
  144: 								      1,1,1);
  145: 	    &debug("sending...section:  $env{'request.course.sec'}");
  146: 	    foreach my $dest (keys %receivers) {
  147: 		&debug("dest is $dest");
  148: 		my @destinfo = split(/:/,$dest);
  149: 		my $user = $destinfo[0];
  150: 		my $dom  = $destinfo[1];
  151: 
  152: 		&Apache::lonmsg::user_normal_msg($user, $dom,
  153: 						 $subject, $message);
  154: 		
  155: 		# No point in looking at the return status as there's no good
  156: 		# error action I can think of right now (log maybe?).
  157: 	    }
  158: 	}
  159:     }
  160: }
  161: 
  162: $|=1;
  163: if (! &LONCAPA::loncgi::check_cookie_and_load_env()) {
  164:     print <<END;
  165: Content-type: text/html
  166: 
  167: <html>
  168: <head><title>Bad Cookie</title></head>
  169: <body>
  170: Your cookie information is incorrect.
  171: </body>
  172: </html>
  173: END
  174:     exit;
  175: }
  176: 
  177: my %perlvar=%{&LONCAPA::Configuration::read_conf('loncapa.conf')};
  178: &Apache::lonlocal::get_language_handle();
  179: &Apache::loncommon::content_type(undef,'text/html');
  180: $env{'request.noversionuri'} = '/cgi-bin/printout.pl';
  181: print(&Apache::loncommon::start_page('Creating PDF'));
  182: 
  183: my $identifier = $ENV{'QUERY_STRING'};
  184: my $texfile = $env{'cgi.'.$identifier.'.file'};
  185: my $laystyle = $env{'cgi.'.$identifier.'.layout'};
  186: my $numberofcolumns = $env{'cgi.'.$identifier.'.numcol'};
  187: my $paper = $env{'cgi.'.$identifier.'.paper'};
  188: my $selectionmade = $env{'cgi.'.$identifier.'.selection'};
  189: my $tableofcontents = $env{'cgi.'.$identifier.'.tableofcontents'};
  190: my $tableofindex = $env{'cgi.'.$identifier.'.tableofindex'};
  191: my $advanced_role = $env{'cgi.'.$identifier.'.role'};
  192: my $number_of_files = $env{'cgi.'.$identifier.'.numberoffiles'}+1;
  193: my $student_names = $env{'cgi.'.$identifier.'.studentnames'};
  194: my $backref = &Apache::lonnet::unescape($env{'cgi.'.$identifier.'.backref'});
  195: 
  196: 
  197: my @names_pack=();
  198: if ($student_names=~/_END_/) {  
  199:     @names_pack=split(/_ENDPERSON_/,$student_names);
  200: }
  201: if ($backref) {
  202:     print('<p>'.&mt("[_1]Return[_2] to editing resource.",
  203: 		    "<a href=\"$backref\"><b>","</b></a>").'</p>');
  204: }
  205: my $figfile = $texfile;
  206: $figfile =~ s/^(.*_printout)_\d+_\d+_\d+\.tex/$1\.dat/;
  207: my $duefile = $texfile;
  208: $duefile =~ s/^(.*_printout)_\d+_\d+_\d+\.tex/$1\.due/;
  209: 
  210: 
  211: #-------------------------------------------------------------------------------------
  212: #
  213: #   Each print may have associated with it a file that contains a set of figures
  214: #   that need to be converted to .eps from whatever form they were in when included
  215: #   in the resource.  The name of the figure file is in $figfile.  If it exists,
  216: #   it contains the names of the files that need to be converted, one per line.
  217: #
  218: 
  219: &debug("Figure file is $figfile");
  220: 
  221: if (-e $figfile) {
  222:     &debug( "Figure file exists");
  223:     &debug("$figfile exists");
  224:     my %done_conversion;
  225:     my $temporary_file=IO::File->new($figfile) || die "Couldn't open fig file $figfile for reading: $!\n";
  226:     my @content_of_file = <$temporary_file>;
  227:     close $temporary_file;  
  228:     my $noteps;
  229:     my %prog_state;
  230:     if ($advanced_role) { %prog_state=&Apache::lonhtmlcommon::Create_PrgWin('','Converting Images to EPS','Picture Conversion Status',$#content_of_file,'inline','80');  }
  231:     print('<br />');
  232:     foreach my $not_eps (@content_of_file) {
  233: 	chomp($not_eps);
  234: 	&debug( "Being asked to convert $not_eps");
  235: 	if ($not_eps ne '') {
  236: 	    $not_eps=~s|\/\.\/|\/|g;
  237: 	    if (!$done_conversion{$not_eps}) { #  Only convert multiple includes once.
  238: 		&convert_figure($not_eps);
  239: 		$done_conversion{$not_eps} = 1;
  240: 	    }
  241: 	}
  242:     }
  243:     if ($advanced_role) { 
  244: 	&Apache::lonhtmlcommon::Close_PrgWin('',\%prog_state); 
  245:     }
  246:     unlink($figfile);
  247: }
  248: #     End of figure conversion section:
  249: #
  250: #--------------------------------------------------------------------------------------------
  251: #
  252: #  Figure out which Tex files we need to process.  If this is a large class e.g.
  253: #  the instructor may have asked that the printout be by section, one section per file
  254: #  in that case, the output tex fiels will be the base filename with 3 digit serial numbers
  255: #  just prior to the .tex file type.
  256: #  By the time this loop exits, @texfile is an array of the files to process.
  257: #
  258: 
  259: my @texfile=($texfile);
  260: if ($number_of_files>1) {
  261:     @texfile=();
  262:     for (my $i=1;$i<=$number_of_files;$i++) {
  263: 	my $new_texfile=$texfile;
  264: 	$new_texfile=~s/\.tex//;
  265: 	$new_texfile = sprintf("%s_%03d.tex", $new_texfile,$i);
  266: 	push @texfile,$new_texfile;
  267:     } 
  268: }
  269: 
  270: #--------------------------------------------------------------------------------------------
  271: 
  272: my $ind=-1;
  273: 
  274: my %prog_state;
  275: if ($advanced_role) { 
  276:     %prog_state=&Apache::lonhtmlcommon::Create_PrgWin('','Print Status',&mt('Class Print Status'),$number_of_files,'inline','80'); 
  277: }
  278: print "<br />";
  279: my $num_files = @texfile;	# How does this differ from $number_of_files , can that be 0?
  280: 
  281: 
  282: 
  283: 
  284: foreach $texfile (@texfile) {
  285:   my $status_statement='';
  286:   my $link_text='download PDF';
  287:   $ind++;
  288: 
  289:   #---------------------------------------------------------------------------------------
  290:   #  This chunk of code 
  291:   #  determines the status message for the printout, and the download link text.
  292:   #  If the print is for one or more students, both will contain the range of students
  293:   #  covered by this file.
  294:   #  
  295: 
  296:   my @stud_info=split(/_END_/,$names_pack[$ind]);
  297:   my @tempo_array=split(/:/,$stud_info[0]);       # username:domain:section:full name:...
  298:   my $name;
  299:   my $name_range='';
  300: 
  301:   # $name       -> Either user's full name or username@domain
  302:   # $name_range -> Either user's last name or usrname.
  303: 
  304:   if ($tempo_array[3]) {
  305:       $name=$tempo_array[3];
  306:       ($name_range) = split(/,/,$name, 2);
  307:   } else {
  308:       $name=$tempo_array[0].'@'.$tempo_array[1];
  309:       $name_range = $tempo_array[0];
  310:   }
  311: 
  312:   # If there truly is a name add it to the status text so we know which
  313:   # user is getting printed.
  314:   #
  315: 
  316:   if (($name ne "") && ($name ne '@') ) { # Could be printing codes...
  317:       $link_text='<b>'.$name.'</b>';
  318:       $status_statement.=$name;
  319:   }
  320: 
  321:   # Group of students being printed...
  322:   # $name_range -> first student's name - last student's name
  323:   #
  324: 
  325:   if ($#stud_info>0) {
  326:       @tempo_array=split(/:/,$stud_info[-1]);
  327:       if ($tempo_array[3]) {
  328: 	  $name=$tempo_array[3];
  329: 	  my ($lastname) = split(/,/, $name,2);
  330: 	  $name_range .= "-".$lastname;
  331:       } else {
  332: 	  $name=$tempo_array[0].'@'.$tempo_array[1];
  333: 	  $name_range .= '-'.$tempo_array[0];
  334:       }
  335:       if (($name ne "") && ($name ne '@')) {
  336: 	  $link_text.=' - <b>'.$name.'</b>';
  337: 	  $status_statement.=' -  '.$name;
  338:   
  339:       }
  340:   }
  341: 
  342:   # $name_range is the range of names in this file.
  343:   # $name is the first of the names in this file.
  344:   #
  345:   #-----------------------------------------------------------------------
  346: 
  347:   #  If the number of files is > 1, but we don't have multiple
  348:   #  student names, we must be printing an exam from codes.
  349:   #  The download link becomes the filename (basename.tex
  350:   #  The status info has the basename appended to it.
  351:   #
  352:   if(($num_files > 1) && ($link_text eq 'download PDF')) {
  353:       $link_text = '<b>'.basename($texfile,'.tex').'.pdf</b>';
  354:       $status_statement .= basename($texfile);
  355:   }
  356: 
  357: 
  358:   #------------------------------------------------------------------------
  359: 
  360:   $name_range =~ s/'//g;	# O'Neil -> ONeil e.g.
  361: 
  362: 
  363:   print "<br/>";
  364:   if ($advanced_role) { 
  365:       &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state,'Creating PDF for '.$status_statement); 
  366:   }
  367: 
  368:   if (-e $texfile) {		# Ensure the tex file exists:
  369: 
  370:       #---------------------------------------------------------------------
  371:       #
  372:       #  Put username ranges into the original tex
  373:       #  Tex filename from which they'll propagate into the other filenames as well.
  374:       #
  375: 
  376:       if (($name_range ne '') && ($num_files > 1)) {
  377: 	  my $newtexfile = $texfile;
  378: 	  $newtexfile    =~ s/\.tex/$name_range\.tex/;
  379: 	  rename($texfile, $newtexfile);
  380: 	  $texfile       = $newtexfile;
  381:       }
  382: 
  383:       #---------------------------------------------------------------------
  384: 
  385:       $texfile =~ m/^(.*)\/([^\/]+)$/; 
  386:       my $name_file = $2;
  387:       my $path_file = $1.'/';
  388:       chdir $path_file;
  389:       my $dvi_file= $name_file; $dvi_file =~ s/\.tex$/\.dvi/;
  390:       &make_dvi_file($name_file,
  391: 		     $dvi_file,
  392: 		     $tableofcontents,
  393: 		     $tableofindex,
  394: 		     $status_statement,
  395: 		     \%prog_state,
  396: 		     $busy_wait_timeout);
  397: 
  398: 
  399:       #Do we have a latex error in the log file?
  400: 
  401: 
  402:       my $logfilename = $texfile; $logfilename =~ s/\.tex$/\.log/;
  403: 
  404: 
  405:       if (&analyze_logfile($logfilename, $texfile, $advanced_role)) {
  406: 	  
  407: 
  408: 	  #LaTeX successfully parsed tex file 
  409: 	  $name_file =~ s/\.tex/\.dvi/;
  410: 	  my $new_name_file = $name_file;
  411: 	  $new_name_file =~ s/\.dvi/\.ps/;
  412: 	  my $papera=$paper;
  413: 	  if ($papera eq 'letter') {$papera='';}
  414: 	  if ($papera ne '') {$papera='-t'.$papera;}
  415: 	  my $extra_ps_header = $perlvar{'lonLib'} .'/includepsheader.ps';
  416: 	  my $comma = "dvips $papera -h $extra_ps_header -Ppdf -G0 -o  $new_name_file";
  417: 	  &busy_wait_command("$comma $name_file 1>/dev/null 2>/dev/null",
  418: 			     "for $status_statement now Converting to PS",
  419: 			     \%prog_state,$new_name_file);
  420: 	  #
  421: 	  #  One last little hinky kinky thing.
  422: 	  #  It's just possible that some fonts could not be maded
  423: 	  #  at the resolution of the pdf print driver.
  424: 	  #  In that case a file called missfont.log will have been
  425: 	  #  created that will contain the commands that were attempted
  426: 	  # to create the missing fonts.  If we basically
  427: 	  # take all the 8000 strings in that file, and
  428: 	  # replace them with 600 (the ljfour resolution)
  429: 	  # run the commands in that file and redvips,
  430: 	  # we'll be able to print the missing glyphs at 600dpi.
  431: 	  #
  432: 	  # Supposedly it is possible to tune TeX/Metafont to do this
  433: 	  # right but I failed to get that to work when following the
  434: 	  # docs at the tug site, hence this rather kludgey fix.
  435: 	  #
  436: 
  437: 	  my $print_directory = dirname($name_file);
  438: 	  my $missfonts_file  = $print_directory."/missfont.log";
  439: 	  #print("<br /> Missing fonts file is: $missfonts_file");
  440: 	  if (-e $missfonts_file) {
  441: 	      #print("<br />Missing fonts file exists\n");
  442: 	      &create_missing_fonts($missfonts_file,\%prog_state);
  443: 	      &busy_wait_command("$comma $name_file 1>/dev/null 2>/dev/null",
  444: 				 "for $status_statement dvips generated missing fonts",
  445: 				 \%prog_state, $new_name_file);
  446: 	  }
  447: 	  if (-e $new_name_file) {
  448: 	      my $latex_file = $name_file;
  449: 	      $latex_file =~ s/\.dvi/\.tex/;
  450: 	      &repaginate($new_name_file, $latex_file,  $numberofcolumns);
  451: 
  452: 	      &make_dvi_file($latex_file,
  453: 			     $name_file,
  454: 			     $tableofcontents,
  455: 			     $tableofindex,
  456: 			     $status_statement,
  457: 			     \%prog_state,
  458: 			     $busy_wait_timeout);
  459: 
  460: 
  461: 	      &busy_wait_command("$comma $name_file 1>/dev/null 2>/dev/null",
  462: 				 "for $status_statement dvips to repaginate",
  463: 				 \%prog_state, $new_name_file);
  464: 
  465: 	      print "<br />";
  466: 	      $new_name_file =~ m/^(.*)\./;
  467: 	      my $ps_file = my $tempo_file = $1.'temporar.ps';
  468: 	      my $pdf_file = $1.'.pdf';
  469: 	      $papera=~s/t/p/;
  470: 	      if ($laystyle eq 'album' and $numberofcolumns eq '2') {
  471: 		  $comma = "psnup $papera -2 -s1.0 $new_name_file";
  472: 		  &debug("PSNUP command: $comma");
  473:                   &busy_wait_command("$comma $tempo_file 1>/dev/null 2>/dev/null",
  474: 				     "for $status_statement now Modifying PS layout",
  475: 				     \%prog_state,$tempo_file);
  476: 	      } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
  477: 		  $comma = 'pstops '.$papera.' "2:0+1(0.48w,0)" '.$new_name_file;
  478: 		  &debug("PSTOPS command: $comma ");
  479: 		  &busy_wait_command("$comma $tempo_file 1>/dev/null 2>/dev/null",
  480: 				     "for $status_statement now Modifying PS layout",
  481: 				     \%prog_state,$tempo_file); 
  482: 	      } else {
  483: 		  $ps_file=$new_name_file;
  484: 	      }
  485: 	      my $addtoPSfile={'legal'=>'<< /PageSize [612 1008] >> setpagedevice',
  486:                                'tabloid'=>'<< /PageSize [792 1224] >> setpagedevice',
  487:                                'executive'=>,'<< /PageSize [540 720] >> setpagedevice',
  488:                                'a2'=>'<< /PageSize [1195.02 1690.09] >> setpagedevice',
  489:                                'a3'=>'<< /PageSize [842 1195.02] >> setpagedevice',
  490:                                'a4'=>'<< /PageSize [595.2 842] >> setpagedevice',
  491:                                'a5'=>'<< /PageSize [421.1 595.2] >> setpagedevice',
  492:                                'a6'=>'<< /PageSize [298.75 421.1] >> setpagedevice',
  493: 			   };
  494: 	      if ($paper ne 'letter') {
  495: 		  open(FFH,'<',$ps_file) || die "Couldn't open ps file $ps_file for reading: $!\n";
  496: 		  my $new_ps_file='new'.$ps_file;
  497: 		  open(FFHS,'>',$new_ps_file) || die "Couldn't open new ps file $new_ps_file for reading: $!\n";
  498: 		  print FFHS $addtoPSfile->{$paper}."\n";
  499: 		  while (<FFH>) {
  500: 		      print FFHS $_;
  501: 		  }
  502: 		  close(FFH);
  503: 		  close(FFHS);
  504: 		  $ps_file=$new_ps_file;	  
  505: 	      }
  506: 	      &busy_wait_command("ps2pdf13 $ps_file $pdf_file 1>/dev/null 2>/dev/null",
  507: 				 "for $status_statement now Converting PS to PDF",
  508: 				 \%prog_state,$pdf_file);
  509: 	    
  510: 	      my $texlog = $texfile;
  511: 	      my $texaux = $texfile;
  512: 	      my $texdvi = $texfile;
  513: 	      my $texps = $texfile;
  514: 	      $texlog =~ s/\.tex/\.log/;
  515: 	      $texaux =~ s/\.tex/\.aux/;
  516: 	      $texdvi =~ s/\.tex/\.dvi/;
  517: 	      $texps =~ s/\.tex/\.ps/;
  518: 	      my @garb = ($texlog,$texaux,$texdvi,$texps);
  519: #	  unlink @garb;
  520: 	      unlink($duefile);
  521: 	      print "<a href=\"/prtspool/$pdf_file\">$link_text - click here to download pdf</a>";
  522: 	      print "\n";
  523: 	  }
  524: 	  unlink($missfonts_file);
  525: 
  526:       }  
  527:   } else {
  528:       print "LaTeX file $texfile was not created successfully";
  529:   }
  530: }
  531: print "<br />";
  532: if ($number_of_files>1) {
  533:     my $zipfile=$texfile[0];
  534:     $zipfile=~s/\.tex/\.zip/;
  535:     my $statement="zip $zipfile";
  536:     foreach my $file (@texfile) {
  537: 	$file=~s/\.tex/.\pdf/;
  538: 	$statement.=' '.$file; 
  539:     }
  540:     print("<pre>Zip Output:\n");
  541:     system($statement);
  542:     print("</pre>");
  543:     $zipfile=~s{^\Q$perlvar{'lonPrtDir'}\E}{/prtspool};
  544:     print "<br /> A <a href=\"$zipfile\">ZIP file</a> of all the PDFs.";
  545: }
  546: if ($advanced_role) { &Apache::lonhtmlcommon::Close_PrgWin('',\%prog_state); }
  547: print(&Apache::loncommon::end_page());
  548: my $done;
  549: 
  550: sub REAPER {
  551:     $done=1;
  552: }
  553: #
  554: #  Execute a command updating the status window as the command's
  555: #  output file builds up (at intervals of a second).
  556: #
  557: #   If the timeout argument defined, then if that many seconds
  558: #   elapses without an increase in the size of the output file,
  559: #   the command will be killed (this deals with the case when
  560: #   latex crawls into an infinite loop).
  561: #
  562: sub busy_wait_command {
  563:     my ($command,$message,$progress_win,$output_file, $timeout)=@_;
  564:     
  565:     $SIG{CHLD} = \&REAPER;
  566:     $done=0;
  567:     my $pid=open(CMD,"$command |");
  568:     if ($advanced_role) {
  569: 	&Apache::lonhtmlcommon::Update_PrgWin('',$progress_win,$message);
  570:     }
  571:     my $last_size      = 0;
  572:     my $unchanged_time = 0;
  573:     while(!$done) {
  574: 	sleep 1;
  575: 	my $extra_msg;
  576: 	if ($output_file) {
  577: 	    my $size=(stat($output_file))[7];
  578: 	    $extra_msg=", $size bytes generated";
  579: 	    if ($size == $last_size) {
  580: 		$unchanged_time++;
  581: 		if ($timeout && ($unchanged_time > $timeout)) {
  582: 		    print "<h1>Operation timed out!</h1>\n";
  583: 		    print "<p>Executing $command, the output file $output_file did not grow\n";
  584: 		    print "after $timeout seconds.  This <em>may</em> indicate $command\n";
  585: 		    print "is in an infinite loop.\n";
  586: 		    print "See if printing fewer copies helps.  Please contact LON-CAPA\n";
  587: 		    print "support about this in any event.";
  588: 		    print "</p>";
  589: 		    kill(9, $pid); # Reaper will do the rest...I hope there's errors in the log.
  590: 		}
  591: 	    } else {
  592: 		$last_size      = $size;
  593: 		$unchanged_time = 0;
  594: 	    }
  595: 	}
  596: 	if ($advanced_role) {
  597: 	    &Apache::lonhtmlcommon::Update_PrgWin('',$progress_win,
  598: 						  $message.$extra_msg);
  599: 	}
  600:     }
  601:     $SIG{CHLD}='IGNORE';
  602:     close(CMD);
  603: }
  604: 
  605: # Make the dvi file (or rather try to), from the latex file and the
  606: # various bits and pieces that control how the latex file is processed:
  607: # LaTeX is run as many times a needed to make this all happen... this may
  608: # result in several runs of LaTeX that just are errors if the LaTeX is
  609: # bad, but the printing subsystem is _supposed_ to not do that.
  610: #
  611: # Parameters:
  612: #   name_file        - Name of the LaTeX file to process.
  613: #   dvi_file         - Name of resulting dvi file.
  614: #   tableofcontents  - "yes" if we are supposed to make a table of contents.
  615: #   tableofindex     - "yes" if we are suposed to make an index.
  616: #   status_statement - Part of the status statement for ths status window.
  617: #   prog_state       - Reference to the program state hash.
  618: #   busy_wait_timeout- Seconds without any progress that imply a problem.
  619: #
  620: #
  621: sub make_dvi_file {
  622:     my ($name_file,
  623: 	$dvi_file,
  624: 	$tableofcontents,
  625: 	$tableofindex,
  626: 	$status_statement,
  627: 	$prog_state,
  628: 	$busy_wait_timeout) = @_;
  629:     
  630:     
  631:     &busy_wait_command("latex $name_file 1>/dev/null 2>/dev/null",
  632: 		       "for $status_statement now LaTeXing file",
  633: 		       $prog_state,$dvi_file, $busy_wait_timeout);
  634: 
  635:     # If the tableof contents was requested, we need to run 
  636:     # LaTex a couple more times to get all the references sorted out.
  637: 
  638:     if ($tableofcontents eq 'yes') {
  639: 	&busy_wait_command("latex $name_file 1>/dev/null 2>/dev/null",
  640: 			   "for $status_statement First LaTeX of file for table of contents",
  641: 			   $prog_state,$dvi_file, $busy_wait_timeout);
  642: 	&busy_wait_command("latex $name_file 1>/dev/null 2>/dev/null",
  643: 			   "for $status_statement Second LaTeX of file for table of contents",
  644: 			   $prog_state,$dvi_file,$busy_wait_timeout);
  645:     } 
  646: 
  647:     # And makeindex and another run of LaTeX to incorporate it if the index
  648:     # is enabled.
  649: 
  650: 
  651:     if ($tableofindex eq 'yes') {
  652: 	my $idxname=$name_file;
  653: 	$idxname=~s/\.tex$/\.idx/;
  654: 	&busy_wait_command("makeindex $idxname",
  655: 			   "making index file",
  656: 			   $prog_state,$idxname);
  657: 	&busy_wait_command("latex $name_file 1>/dev/null 2>/dev/null",
  658: 			   "for $status_statement now LaTeXing file for index section",
  659: 			   $prog_state,$dvi_file, $busy_wait_timeout);
  660:     } 
  661:     
  662: }    
  663: 
  664: 
  665: #  Repagninate
  666: #  What we need to do:
  667: #   - Count the number of pages in each student.
  668: #   - Rewrite the latex file replacing the \specials that
  669: #     mark the end of student with an appropriate number of newlines.
  670: #   parameters:
  671: #     psfile     - Postscript filename
  672: #     latexfile  - LaTeX filename
  673: #     columns    - number of columns.
  674: sub repaginate {
  675: 
  676:     # We will try to do this in 2 passes through the postscript since
  677:     # the postscript is potentially large, to do 2 passes, the first pass
  678:     # must be able to calculate the total number of document pages so that
  679:     # at the beginning of the second pass we already know how to replace
  680:     #  %%Pages:
  681: 
  682:     #  Figure out
  683:     #    1. Number of pages in the document
  684:     #    2. Maximum number of pages in a student
  685:     #    3. Number of pages in each student.
  686: 
  687:     my ($postscript_filename, $latex_filename, $num_columns) = @_;
  688:     open(PSFILE, "<$postscript_filename");
  689:     my $line;
  690:     my $total_pages;		# Total pages in document.
  691:     my $seen_pages        = 0;	# There are several %%Pages only the first is useful
  692:     my @pages_in_student;	# For each student his/her initial page count.
  693:     my $max_pages = 0;		# Pages in 'longest' student.
  694:     my $page_number = 0;
  695:     &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state, 
  696: 					  &mt("Counting pages for student: [_1]",1));
  697: 
  698:     while ($line = <PSFILE>) {
  699: 	
  700: 	# Check for total pages (%%Pages:)
  701: 
  702: 	if (($line =~ /^%%Pages:/) && (!$seen_pages)) {
  703: 	    my @pageinfo = split(/ /,$line);
  704: 	    $total_pages = $pageinfo[1];
  705: 	    $seen_pages  = 1;
  706: 	}
  707: 	#  Check for %%Page: n m  $page_number will be the
  708: 	#  biggest of these until we see an endofstudent.
  709: 	#  Note that minipages generate spurious %Page: 1 1's so
  710: 	#  we only are looking for the largest n (n is page number at the
  711: 	#  bottom of the page, m the page number within the document.
  712: 	#
  713: 
  714: 	if ($line =~ /^%%Page:\s+\d+\s+\d+/) {
  715: 	    my @pageinfo = split(/\s+/, $line);
  716: 	    if ($page_number < $pageinfo[1]) {
  717: 		$page_number = $pageinfo[1];
  718: 	    } elsif ($pageinfo[2] ne 1) {
  719: 		#  current page count reset, and it's not because of a 
  720: 		#    minipage 
  721: 		# - save the page_number, reset and, if necessary
  722: 		#    update max_pages.
  723: 		push(@pages_in_student, $page_number);
  724: 		&Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state, 
  725: 						      &mt("Counting pages for student: [_1]", scalar(@pages_in_student)));
  726: 		if ($page_number > $max_pages) {
  727: 		    $max_pages = $page_number;
  728: 		}
  729: 		$page_number = $pageinfo[1];
  730: 	    }
  731: 	}
  732: 
  733: 	
  734:     }
  735:     # file ended so one more student
  736:     push(@pages_in_student, $page_number);
  737:     &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state, 
  738: 					  &mt("Counting pages for student: [_1]",scalar(@pages_in_student)));
  739:     if ($page_number > $max_pages) {
  740: 	$max_pages = $page_number;
  741:     }
  742:     $page_number = 0;
  743:     
  744:     close(PSFILE);
  745: 
  746:     #  If 2 columns, max_pages must go to an even number of columns:
  747: 
  748:    
  749:     if ($num_columns == 2) {
  750: 	if ($max_pages % 2) {
  751: 	    $max_pages++;
  752: 	}
  753:     }
  754:     
  755:     #  Now rewrite the LaTex file, substituting our \special
  756:     #  with an appropriate number of \newpage directives.
  757: 
  758:     my $outfilename = $latex_filename."temp";
  759: 
  760:     open(LATEXIN, "<$latex_filename");
  761:     open(LATEXOUT, ">$outfilename");
  762: 
  763: 
  764:     my $student_number    = 0;	# Index of student we're working on.
  765:     &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state, 
  766: 					  "Repaginating student ".$student_number+1);
  767: 
  768:     while (my $line = <LATEXIN>) {
  769: 	if ($line eq "\\special{ps:ENDOFSTUDENTSTAMP}\n") {
  770: 	    # only end of student stamp if next line is ENDOFSTUDENTSTAMP:
  771: 
  772: 
  773: 	    # End of student replace with 0 or more newpages.
  774: 	    
  775: 	    my $addlines = $max_pages - $pages_in_student[$student_number];
  776: 	    while($addlines)  {
  777: 		print LATEXOUT '\clearpage \strut \clearpage';
  778: 
  779: 		$addlines--;
  780: 	    }
  781: 	    
  782: 	    $student_number++;
  783: 	    &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state, 
  784: 						  "Repaginating student ".$student_number+1);
  785: 	    
  786: 	} else {
  787: 	    print LATEXOUT $line;
  788: 	}
  789:     }
  790: 
  791:     close(LATEXIN);
  792:     close(LATEXOUT);
  793:     rename($outfilename, $latex_filename);
  794: 
  795: }
  796: 
  797: #
  798: #   Create missing fonts given a latex missfonts.log file.
  799: #   This file will have lines like:
  800: #
  801: #   mktexpk --mfmode ljfour --bdpi 8000 --mag 1+0/8000 --dpi 8000 tcrm0500
  802: #
  803: #  We want to execute those lines with the 8000's changed to 600's
  804: #  in order to match the resolution of the ljfour printer.
  805: #  Of course if some wiseguy has changed the default printer from ljfour
  806: #  in the dvips's config.ps file that will break so we'll also
  807: #  ensure that --mfmode is ljfour.
  808: #
  809: sub create_missing_fonts {
  810:     my ($fontfile, $state) = @_;
  811: 
  812:     # Open and read in the font file..we'll read it into the array
  813:     #  font_commands.
  814:     #
  815:     open(my $font_handle, $fontfile);
  816:     my @font_commands = <$font_handle>;
  817: 
  818:     # make the list contain each command only once
  819:     my %uniq;
  820:     @font_commands = map { $uniq{$_}++ == 0 ? $_ : () } @font_commands;
  821: 
  822:     #  Now process each command replacing the appropriate 8000's with
  823:     #  600's ensuring that font names with 8000's in them are not corrupted.
  824:     #  and if the --mfmode is not ljfour we turn it into ljfour.
  825:     #   Then we execute the command.
  826:     #
  827:     
  828:     foreach my $command (@font_commands) {
  829: 	#print("<br />Raw command: $command");
  830: 	$command =~ s/ 8000/ 600/g;    # dpi directives.
  831: 	$command =~ s/\/8000/\/600/g;  # mag directives.
  832: 	#print("<br />After dpi replacements: $command");
  833: 
  834: 	my @cmdarray = split(/ /,$command);
  835: 	for (my $i =0; $i < scalar(@cmdarray); $i++) {
  836: 	    if ($cmdarray[$i] eq '--mfmode') {
  837: 		$cmdarray[$i+1] = "ljfour";
  838: 	    }
  839: 	}
  840: 	#print("<br /> before reassembly : (@cmdarray)");
  841: 	$command = join(" ", (@cmdarray));
  842: 
  843: 	#print("<br />Creating fonts via command: $command");
  844: 	&busy_wait_command("$command 1>/dev/null 2>/dev/null",
  845: 			   "Creating missing font",
  846: 			   $state);
  847: 			   
  848:     }
  849: 
  850: }
  851: #
  852: #  Convert a figure file to encapsulated postscript:
  853: #  At present, this is using a lot of file scoped globals to pass data around. 
  854: # Parameters:
  855: #    not_eps  - The name of the file to convert which, presumably, is not
  856: #               already an eps file.
  857: #
  858: sub convert_figure {
  859:     my ($not_eps) = @_;
  860:     &debug("in convert_figure");
  861: 
  862:     my $status_statement='EPS picture for '.$not_eps;
  863:     my $eps_f = $not_eps;
  864: 
  865:     if ($eps_f=~/\/home\/([^\/]+)\/public_html\//) {
  866: 	$eps_f=~s/\/home\/([^\/]+)\/public_html/$1/;
  867:     } elsif ($eps_f=~/$perlvar{'lonDocRoot'}\/res\//) {
  868: 	$eps_f=~ s/$perlvar{'lonDocRoot'}\/res\/(.+)/$1/;
  869:     } elsif ($eps_f=~/$perlvar{'lonUsersDir'}\//) {
  870: 	$eps_f=~ s/$perlvar{'lonUsersDir'}\/([^\/]+)\/\w\/\w\/\w\/(.+)/$1\/$2/;
  871:     }
  872: 
  873:     $eps_f = $perlvar{'lonPrtDir'}.'/'.$eps_f;
  874: 
  875:     # Spaces are problematic for system commands and LaTeX, replace with _
  876: 
  877:     $eps_f  =~ s/ /\_/g; 
  878: 
  879:     # 
  880:     # If the file is already an .eps or .ps file (eps_f still has the original
  881:     # file type),
  882:     # We really just need to copy it from where it was to prtspool
  883:     # but with the spaces substituted to _'s.
  884:     #
  885:     my ($nsname,$path, $sext) = &fileparse($eps_f, qr/\.(ps|eps)/i);
  886:     if ($sext =~/ps$/i) {
  887: 	&File::Path::mkpath($path,0,0777);
  888: 	copy("$not_eps", "$eps_f"); 
  889:     } else {
  890: 	
  891: 	$eps_f .= '.eps';	# Just append the eps ext.
  892: 	my $path= &dirname($eps_f);
  893: 	&File::Path::mkpath($path,0,0777);
  894: 	$not_eps =~ s/^\s+//;
  895: 	$not_eps =~ s/\s+$//;
  896: 	$not_eps =~ s/ /\\ /g;
  897: 	if ($advanced_role) {
  898: 	    my $prettyname=$not_eps;
  899: 	    $prettyname=~s|/home/([^/]+)/public_html|/priv/$1|;
  900: 	    $prettyname=~s|$perlvar{'lonDocRoot'}/|/|;
  901: 	    &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state,
  902: 						  'Converting to EPS '.$prettyname);
  903: 	}
  904: 	#
  905: 	#  If the file is a PDF, need to use pdftops to convert it to a ps file.
  906: 	#  otherwise use imagemagik:
  907: 	#
  908: 	if($not_eps =~/\.(pdf|PDF)$/) {
  909: 
  910: 	    #
  911: 	    # For whatever reason, pure postscript conversions have to be
  912: 	    # in the same dir as the base file:
  913: 	    #
  914: 	    $eps_f = &basename($eps_f);
  915: 	    $eps_f = $perlvar{'lonPrtDir'}.'/'.$eps_f;
  916: 
  917: 	    &debug("Converting pdf $not_eps to postscript: $eps_f");
  918: 	    system("pdftops $not_eps $eps_f");
  919: 	} else {
  920: 	    system("convert $not_eps $eps_f");
  921: 	}
  922: 
  923: 	if (not -e $eps_f) {
  924: 	    # converting an animated gif creates either:
  925: 	    # anim.gif.eps.0
  926: 	    # or
  927: 	    # anim.gif-0.eps
  928: 	    for (my $i=0;$i<10000;$i++) {
  929: 		if (-e $eps_f.'.'.$i) {
  930: 		    rename($eps_f.'.'.$i, $eps_f);
  931: 		    last;
  932: 		}
  933: 		my $anim_eps = $eps_f;
  934: 		$anim_eps =~ s/(\.[^.]*)\.eps$/$1-$i\.eps/i;
  935: 		if (-e $anim_eps) {
  936: 		    rename($anim_eps, $eps_f);
  937: 		    last;
  938: 		}
  939: 	    }
  940: 	}
  941: 	
  942: 	# imagemagick 6.2.0-6.2.7 fails to properly handle
  943: 	# convert anim.gif anim.gif.eps
  944: 	# it creates anim.eps instead. 
  945: 	if (not -e $eps_f) {
  946: 	    my $eps_f2 = $eps_f;
  947: 	    $eps_f2 =~ s/\.[^.]*\.eps$/\.eps/i;
  948: 	    if(-e $eps_f2) {
  949: 		rename($eps_f2,$eps_f);
  950: 	    }
  951: 	}
  952:     }
  953:     
  954: }
  955: #
  956: #   Analyze a LaTeX logfile producing appropriate  output on error and 
  957: #   returning a boolean to let the caller know if, in our opinion, it's
  958: #   worth continuing on to produce the PDF file.
  959: #
  960: # Parameters:
  961: #   logfilename   - Name of the logfile.
  962: #   texfile       - Name of the LaTeX file that was being processed.
  963: #   advanced_role - True if the user is privileged with respect to the printout
  964: #                   (e.g. is the course coordinator or some such thing).
  965: # Returns:
  966: #    1            - Caller is advised to continue to create the PDF.
  967: #    0            - Caller need not bother creating the PDF.
  968: # Side Effects:
  969: #   Messages are printed to describe any errors that have been encountered.
  970: # NOTE:
  971: #    The current policy is to assume that if LaTeX decided to insert some text
  972: #    it has salvaged the resource and the resource can be printed.. in that case
  973: #    a message is emitted from this sub.
  974: #
  975: sub analyze_logfile {
  976:     my ($logfilename, $texfile, $advanced_role) = @_;
  977: 
  978:     my $temporary_file=IO::File->new($logfilename) || die "Couldn't open log file $logfilename for reading: $!\n";
  979:     my @content_of_file = <$temporary_file>;
  980:     close $temporary_file; 
  981:     my $body_log_file = join(' ',@content_of_file);
  982:     $logfilename =~ s/\.log$/\.html/;
  983:     $temporary_file = IO::File->new('>'.$logfilename); 
  984:     print $temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_log_file.'</pre></body></html>'."\n";
  985:     if ($body_log_file=~m/!\s+Emergency stop/) {
  986: 	my $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART';
  987: 	my $whereitends = rindex $body_log_file,'STAMPOFPASSEDRESOURCEEND';
  988: 	my $badresource;
  989: 	my $badtext;
  990: 	if ($whereitbegins!=-1 and $whereitends!=-1) {
  991: 	    $badtext = substr($body_log_file,$whereitbegins+26, $whereitends-$whereitbegins-26);
  992: 	    $whereitbegins  = rindex $badtext,'located in';
  993: 	    if ($whereitbegins != -1) {
  994: 		
  995: 		$badresource = substr($badtext, $whereitbegins+27, 
  996: 				      length($badtext) - $whereitbegins - 48);
  997: 		# print "<br />failing resourcename: $badresource<br />";
  998: 	    }
  999:         }
 1000: 
 1001: 	# Guys with privileged roles get a more detailed error output:
 1002: 
 1003: 	if ($advanced_role) {  
 1004: 	    #LaTeX failed to parse tex file 
 1005: 	    print "<h2>".&mt('LaTeX could not successfully parse your TeX file.')."</h2>";
 1006: 	    print &mt('It probably has errors in it.')."<br />";
 1007: 	    if ($badtext) {
 1008:                 print &mt('With very high probability this error occured in [_1].',$badtext)
 1009:                      ."<br /><br />";
 1010:             }
 1011: 	    print &mt('Here are the error messages in the LaTeX log file:')
 1012:                  ."<br /><pre>";
 1013: 	    
 1014: 	    my $sygnal = 0;
 1015: 	    for (my $i=0;$i<=$#content_of_file;$i++) {
 1016: 		if ($content_of_file[$i]=~m/^Runaway argument?/ or $content_of_file[$i]=~m/^!/) {
 1017: 		    $sygnal = 1;
 1018: 		} 
 1019: 		if ($content_of_file[$i]=~m/Here is how much of/) {
 1020: 		    $sygnal = 0;
 1021: 		} 
 1022: 		if ($sygnal) {
 1023: 		    print "$content_of_file[$i]";
 1024: 		}  
 1025: 	    }
 1026: 	    print "</pre>\n";
 1027: 	    # print "<br /> Advanced role <br />";
 1028: 	    $logfilename=~s{^\Q$perlvar{'lonPrtDir'}\E}{/prtspool};
 1029: 	    print "<b><big>"
 1030:                  .&mt('The link to [_1]Your log file[_2]','<a href="'.$logfilename.'">','</a>')
 1031: 	         ."</big></b>\n";
 1032: 	    #link to original LaTeX file
 1033: 	    my $tex_temporary_file=IO::File->new($texfile) || die "Couldn't open tex file $texfile for reading: $!\n";
 1034: 	    my @tex_content_of_file = <$tex_temporary_file>;
 1035: 	    close $tex_temporary_file; 
 1036: 	    my $body_tex_file = join(' ',@tex_content_of_file);
 1037: 	    $texfile =~ s/\.tex$/aaaaa\.html/;
 1038: 	    $tex_temporary_file = IO::File->new('>'.$texfile); 
 1039: 	    print $tex_temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_tex_file.'</pre></body></html>'."\n";
 1040: 	    print "<br /><br />";
 1041: 	    $texfile=~s{^\Q$perlvar{'lonPrtDir'}\E}{/prtspool};
 1042: 	    print "<b><big>"
 1043:                  .&mt('The link to [_1]Your original LaTeX file[_2]','<a href="'.$texfile.'">','</a>')
 1044: 	         ."</big></b><br /><br />\n";
 1045: 	    my $help_text = &Apache::loncommon::help_open_topic("Print_Resource", &mt('Help on printing'));
 1046: 	    print ("$help_text");
 1047: 
 1048: 	    # Students on the other hand get a minimal error message, since they won't
 1049: 	    # be able to correct the error message. A message is sent to the 
 1050: 	    # instructor:
 1051: 
 1052: 	} else {		# Student role...
 1053: 	    #  at this point:
 1054: 	    #    $body_log_file - contains the log file.
 1055: 	    #    $name_file     - is the name of the LaTeX file.
 1056: 	    #    $identifier    - is the unique LaTeX identifier.l
 1057: 	    
 1058:             print '<p class="LC_error">';
 1059: 	    if ($badtext) {
 1060:                 print &mt('There are errors in [_1].',$badtext);
 1061:             } else {
 1062:                 print &mt('There are errors.');
 1063:             }
 1064:             print '</p>'
 1065:                  .&mt('These errors prevent this resource from printing correctly.');
 1066: 
 1067: 	    my $tex_handle = IO::File->new($texfile);
 1068: 	    my @tex_contents = <$tex_handle>;
 1069: 	    &send_error_mail($identifier, $badresource, $body_log_file, \@tex_contents);
 1070: 	    print "<p>"
 1071:                  .&mt('A message has been sent to the instructor describing this failure.')
 1072:                  ."</p>";
 1073: 	    my $help_text = &Apache::loncommon::help_open_topic("Print_Resource", &mt('Help on printing'));
 1074: 	    print  ("$help_text");
 1075: 	    
 1076: 	  }
 1077: 
 1078: 	# Either way, an emergency stop does not allow us to continue so:
 1079: 
 1080: 	return 0;
 1081: 	
 1082: 	# The branch of code below is taken if it appears that 
 1083: 	# there was no emergency stop but LaTeX had to correct the
 1084: 	# input file to run.
 1085: 	# In that case we need to provide error feedback, as the correction >may< not be
 1086: 	# sufficient, we can let the game continue as there's a dvi file to process.
 1087: 
 1088:     } elsif ($body_log_file=~m/<inserted text>/) {
 1089: 	my $whereitbegins = index $body_log_file,'<inserted text>';
 1090: 	print &mt('You are running LaTeX in [_1]batch mode[_2].','<b>','</b>');
 1091: 	while ($whereitbegins != -1) {
 1092: 	    my $tempobegin=$whereitbegins;
 1093: 	    $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART',$whereitbegins;
 1094: 	    my $whereitends = index $body_log_file,'STAMPOFPASSEDRESOURCEEND',$whereitbegins;
 1095: 	    print "<br />"
 1096:                  .&mt('It has found an error in [_1][_2]and corrected it.',substr($body_log_file,$whereitbegins+26,$whereitends-$whereitbegins-26),"<br />")."\n";
 1097: 	    print &mt('Usually this correction is valid but you probably need to check the indicated resource one more time and implement neccessary corrections by yourself.')."\n";
 1098: 	    $whereitbegins = index $body_log_file,'<inserted text>',$tempobegin+10;
 1099: 	}
 1100: 
 1101: 	if ($advanced_role) {  
 1102: 	    print "<br /><br />";
 1103: 	    $logfilename=~s{^\Q$perlvar{'lonPrtDir'}\E}{/prtspool};
 1104: 	    print "<b><big>"
 1105:                  .&mt('The link to [_1]Your log file[_2]'
 1106:                      ,'<a href="'.$logfilename.'">','</a>')
 1107: 	         ."</big></b>\n";
 1108: 	    #link to original LaTeX file
 1109: 	    my $tex_temporary_file=IO::File->new($texfile) || die "Couldn't open tex file $texfile for reading: $!\n";
 1110: 	    my @tex_content_of_file = <$tex_temporary_file>;
 1111: 	    close $tex_temporary_file; 
 1112: 	    my $body_tex_file = join(' ',@tex_content_of_file);
 1113: 	    $texfile =~ s/\.tex$/aaaaa\.html/;
 1114: 	    $tex_temporary_file = IO::File->new('>'.$texfile); 
 1115: 	    print $tex_temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_tex_file.'</pre></body></html>'."\n";
 1116: 	    print "<br /><br />";
 1117: 	    $texfile=~s{^\Q$perlvar{'lonPrtDir'}\E}{/prtspool};
 1118: 	    print "<b><big>"
 1119:                   .&mt('The link to [_1]Your original LaTeX file[_2]','<a href="'.$texfile.'">','</a>');
 1120: 	    print "</big></b>\n";
 1121: 	}
 1122: 	return 1;
 1123:     }
 1124:     return 1;			# NO log file issues at all.
 1125: }

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