Annotation of loncom/interface/printout.pl, revision 1.135

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

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