* doc/meref.me: Fix description of .GS request.
[s-roff.git] / src / roff / grog / grog.pl
blob2c539f69c9f31f054615886f10b8d09e25318cf3
1 #!/usr/bin/perl
2 # grog -- guess options for groff command
3 # Inspired by doctype script in Kernighan & Pike, Unix Programming
4 # Environment, pp 306-8.
6 $prog = $0;
7 $prog =~ s@.*/@@;
9 push(@command, "groff");
11 while ($ARGV[0] =~ /^-./) {
12 $arg = shift(@ARGV);
13 last if $arg eq "--";
14 push(@command, $arg);
17 if (@ARGV) {
18 foreach $arg (@ARGV) {
19 &process($arg, 0);
22 else {
23 &process("-", 0);
26 sub process {
27 local($filename, $level) = @_;
28 local(*FILE);
30 if (!open(FILE, $filename eq "-" ? $filename : "< $filename")) {
31 print STDERR "$prog: can't open \`$filename': $!\n";
32 exit 1 unless $level;
33 return;
35 while (<FILE>) {
36 if (/^\.TS/) {
37 $_ = <FILE>;
38 if (!/^\./) {
39 $tbl++;
40 $soelim++ if $level;
43 elsif (/^\.EQ/) {
44 $_ = <FILE>;
45 if (!/^\./ || /^\.[0-9]/) {
46 $eqn++;
47 $soelim++ if $level;
50 elsif (/^\.GS/) {
51 $_ = <FILE>;
52 if (!/^\./) {
53 $grn++;
54 $soelim++ if $level;
56 elsif (/^\.PS([ 0-9.<].*)?$/) {
57 if (/^\.PS\s*<\s*(\S+)/) {
58 $pic++;
59 $soelim++ if $level;
60 &process($1, $level);
62 else {
63 $_ = <FILE>;
64 if (!/^\./ || /^\.ps/) {
65 $pic++;
66 $soelim++ if $level;
70 elsif (/^\.R1/ || /^\.\[/) {
71 $refer++;
72 $soelim++ if $level;
74 elsif (/^\.[PLI]P/) {
75 $PP++;
77 elsif (/^\.P$/) {
78 $P++;
80 elsif (/^\.(PH|SA)/) {
81 $mm++;
83 elsif (/^\.TH/) {
84 $TH++;
86 elsif (/^\.SH/) {
87 $SH++;
89 elsif (/^\.([pnil]p|sh)/) {
90 $me++;
92 elsif (/^\.Dd/) {
93 $mdoc++;
95 elsif (/^\.(Tp|Dp|De|Cx|Cl)/) {
96 $mdoc_old = 1;
98 # In the old version of -mdoc `Oo' is a toggle, in the new it's
99 # closed by `Oc'.
100 elsif (/^\.Oo/) {
101 $Oo++;
103 elsif (/^\.Oc/) {
104 $Oo--;
106 if (/^\.so/) {
107 chop;
108 s/^.so *//;
109 s/\\\".*//;
110 s/ .*$//;
111 &process($_, $level + 1) unless /\\/ || $_ eq "";
114 close(FILE);
117 if ($pic || $tbl || $eqn || $grn || $refer) {
118 $s = "-";
119 $s .= "s" if $soelim;
120 $s .= "R" if $refer;
121 $s .= "p" if $pic;
122 $s .= "h" if $grn;
123 $s .= "t" if $tbl;
124 $s .= "e" if $eqn;
125 push(@command, $s);
128 if ($me > 0) {
129 push(@command, "-me");
131 elsif ($SH > 0 && $TH > 0) {
132 push(@command, "-man");
134 elsif ($PP > 0) {
135 push(@command, "-ms");
137 elsif ($P > 0 || $mm > 0) {
138 push(@command, "-mm");
140 elsif ($mdoc > 0) {
141 push(@command, ($mdoc_old || $Oo > 0) ? "-mdoc.old" : "-mdoc");
144 push(@command, "--") if @ARGV && $ARGV[0] =~ /^-./;
146 push(@command, @ARGV);
148 # We could implement an option to execute the command here.
150 foreach (@command) {
151 next unless /[\$\\\"\';&()|<> \t\n]/;
152 s/\'/\'\\\'\'/;
153 $_ = "'" . $_ . "'";
156 print join(' ', @command), "\n";