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

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

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