File:  [LON-CAPA] / loncom / interface / printout.pl
Revision 1.127: download - view: text, annotated - select for diffs
Fri Sep 21 21:11:20 2007 UTC (16 years, 7 months ago) by albertel
Branches: MAIN
CVS tags: version_2_5_X, HEAD
- make the re match only the types of %%Page: comments we are looking for

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

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