home *** CD-ROM | disk | FTP | other *** search
/ Mega Top 1 / os2_top1.zip / os2_top1 / APPS / TEKST / GROFFEXE / BIN / GROG.CMD < prev    next >
OS/2 REXX Batch file  |  1994-01-02  |  3KB  |  152 lines

  1. extproc perl -Sx
  2.  
  3. #!/usr/bin/perl
  4. # grog -- guess options for groff command
  5. # Inspired by doctype script in Kernighan & Pike, Unix Programming
  6. # Environment, pp 306-8.
  7.  
  8. $prog = $0;
  9. $prog =~ s@.*/@@;
  10.  
  11. push(@command, "groff");
  12.  
  13. while ($ARGV[0] =~ /^-./) {
  14.     $arg = shift(@ARGV);
  15.     last if $arg eq "--";
  16.     push(@command, $arg);
  17. }
  18.  
  19. if (@ARGV) {
  20.     foreach $arg (@ARGV) {
  21.     &process($arg, 0);
  22.     }
  23. }
  24. else {
  25.     &process("-", 0);
  26. }
  27.  
  28. sub process {
  29.     local($filename, $level) = @_;
  30.     local(*FILE);
  31.  
  32.     if (!open(FILE, $filename eq "-" ? $filename : "< $filename")) {
  33.     print STDERR "$prog: can't open \`$filename': $!\n";
  34.     exit 1 unless $level;
  35.     return;
  36.     }
  37.     while (<FILE>) {
  38.     if (/^\.TS/) {
  39.         $_ = <FILE>;
  40.         if (!/^\./) {
  41.         $tbl++;
  42.         $soelim++ if $level;
  43.         }
  44.     }
  45.     elsif (/^\.EQ/) {
  46.         $_ = <FILE>;
  47.         if (!/^\./ || /^\.[0-9]/) {
  48.         $eqn++;
  49.         $soelim++ if $level;
  50.         }
  51.     }
  52.     elsif (/^\.PS([ 0-9.<].*)?$/) {
  53.         if (/^\.PS\s*<\s*(\S+)/) {
  54.         $pic++;
  55.         $soelim++ if $level;
  56.         &process($1, $level);
  57.         }
  58.         else {
  59.             $_ = <FILE>;
  60.             if (!/^\./ || /^\.ps/) {
  61.             $pic++;
  62.             $soelim++ if $level;
  63.         }
  64.         }
  65.     }
  66.     elsif (/^\.R1/ || /^\.\[/) {
  67.         $refer++;
  68.         $soelim++ if $level;
  69.     }
  70.     elsif (/^\.[PLI]P/) {
  71.         $PP++;
  72.     }
  73.     elsif (/^\.P$/) {
  74.         $P++;
  75.     }
  76.         elsif (/^\.(PH|SA)/) {
  77.             $mm++;
  78.     }
  79.     elsif (/^\.TH/) {
  80.         $TH++;
  81.     }
  82.     elsif (/^\.SH/) {
  83.         $SH++;
  84.     }
  85.     elsif (/^\.([pnil]p|sh)/) {
  86.         $me++;
  87.     }
  88.     elsif (/^\.Dd/) {
  89.         $mdoc++;
  90.     }
  91.     elsif (/^\.(Tp|Dp|De|Cx|Cl)/) {
  92.         $mdoc_old = 1;
  93.     }
  94.         # In the old version of -mdoc `Oo' is a toggle, in the new it's
  95.     # closed by `Oc'.
  96.     elsif (/^\.Oo/) {
  97.         $Oo++;
  98.     }
  99.     elsif (/^\.Oc/) {
  100.         $Oo--;
  101.     }
  102.     if (/^\.so/) {
  103.         chop;
  104.         s/^.so *//;
  105.         s/\\\".*//;
  106.         s/ .*$//;
  107.         &process($_, $level + 1) unless /\\/ || $_ eq "";
  108.     }
  109.     }
  110.     close(FILE);
  111. }
  112.  
  113. if ($pic || $tbl || $eqn || $refer) {
  114.     $s = "-";
  115.     $s .= "s" if $soelim;
  116.     $s .= "R" if $refer;
  117.     $s .= "p" if $pic;
  118.     $s .= "t" if $tbl;
  119.     $s .= "e" if $eqn;
  120.     push(@command, $s);
  121. }
  122.  
  123. if ($me > 0) {
  124.     push(@command, "-me");
  125. }
  126. elsif ($SH > 0 && $TH > 0) {
  127.     push(@command, "-man");
  128. }
  129. elsif ($PP > 0) {
  130.     push(@command, "-ms");
  131. }
  132. elsif ($P > 0 || $mm > 0) {
  133.     push(@command, "-mm");
  134. }
  135. elsif ($mdoc > 0) {
  136.     push(@command, ($mdoc_old || $Oo > 0) ? "-mdoc.old" : "-mdoc");
  137. }
  138.  
  139. push(@command, "--") if @ARGV && $ARGV[0] =~ /^-./;
  140.  
  141. push(@command, @ARGV);
  142.  
  143. # We could implement an option to execute the command here.
  144.  
  145. foreach (@command) {
  146.     next unless /[\$\\\"\';&()|<> \t\n]/;
  147.     s/\'/\'\\\'\'/;
  148.     $_ = "'" . $_ . "'";
  149. }
  150.  
  151. print join(' ', @command), "\n";
  152.