2 # grog - guess options for groff command
3 # Inspired by doctype script in Kernighan & Pike, Unix Programming
4 # Environment, pp 306-8.
6 # Source file position: <groff-source>/src/roff/grog/grog.pl
7 # Installed position: <prefix>/bin/grog
9 # Copyright (C) 1993, 2006, 2009 Free Software Foundation, Inc.
10 # Written by James Clark, maintained by Werner Lemberg.
11 # Rewritten and put under GPL by Bernd Warken.
13 # This file is part of `grog', which is part of `groff'.
15 # `groff' is free software; you can redistribute it and/or modify it
16 # under the terms of the GNU General Public License (GPL) as published
17 # by the Free Software Foundation, either version 3 of the License, or
18 # (at your option) any later version.
20 # `groff' is distributed in the hope that it will be useful, but
21 # WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 # General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program. If not, see <http://www.gnu.org/licenses/>.
28 ########################################################################
29 my $Last_Update = '5 Jan 2009';
30 ########################################################################
40 my ($v, $d, $f) = File::Spec->splitpath($Prog);
47 my @Command; # stores the final output
48 my @Mparams; # stores the options -m*
57 foreach my $arg (@ARGV) {
61 if (-f $arg && -r $arg) {
64 print STDERR "grog: $arg is not a readable file.\n";
82 &version(0) if $arg eq '-v' || '--version' =~ /^$arg/;
83 &help() if $arg eq '-h' || '--help' =~ /^$arg/;
84 print STDERR "grog: wrong option $arg.\n" if $arg =~ /^--/;
90 $Sp = '' if $arg eq '-C';
97 if (-f $arg && -r $arg) {
100 print STDERR "grog: $arg is not a readable file.\n";
105 @filespec = ('-') if ! @filespec && ! $had_filespec;
106 exit 1 unless @filespec;
110 foreach my $arg (@ARGV) {
115 my ($filename, $level) = @_;
118 if (!open(FILE, $filename eq "-" ? $filename : "< $filename")) {
119 print STDERR "$Prog: can't open \`$filename': $!\n";
120 exit 1 unless $level;
129 if (/^(.cstart)|(begin\s+chem)$/) {
132 } elsif (/^\.TS$Sp/) {
136 $Groff{'soelim'}++ if $level;
138 } elsif (/^\.EQ$Sp/) {
140 if (!/^\./ || /^\.[0-9]/) {
142 $Groff{'soelim'}++ if $level;
144 } elsif (/^\.GS$Sp/) {
148 $Groff{'soelim'}++ if $level;
150 } elsif (/^\.G1$Sp/) {
155 $Groff{'soelim'}++ if $level;
157 # } elsif (/^\.PS\Sp([ 0-9.<].*)?$/) {
158 # if (/^\.PS\s*<\s*(\S+)/) {
160 # $Groff{'soelim'}++ if $level;
161 # &process($1, $level);
164 # if (!/^\./ || /^\.ps/) {
166 # $Groff{'soelim'}++ if $level;
169 } elsif (/^\.PS[\s\n<]/) {
171 $Groff{'soelim'}++ if $level;
172 if (/^\.PS\s*<\s*(\S+)/) {
173 &process($1, $level);
175 } elsif (/^\.R1$Sp/) {
177 $Groff{'soelim'}++ if $level;
179 $Groff{'refer_open'}++;
180 $Groff{'soelim'}++ if $level;
182 $Groff{'refer_close'}++;
183 $Groff{'soelim'}++ if $level;
184 } elsif (/^\.NH$Sp/) {
185 $Groff{'NH'}++; # for ms
186 } elsif (/^\.TL$Sp/) {
187 $Groff{'TL'}++; # for mm and ms
188 } elsif (/^\.PP$Sp/) {
189 $Groff{'PP'}++; # for mom and ms
190 } elsif (/^\.[IL]P$Sp/) {
191 $Groff{'ILP'}++; # for man and ms
194 } elsif (/^\.(PH|SA)$Sp/) {
196 } elsif (/^\.TH$Sp/) {
198 } elsif (/^\.SH$Sp/) {
200 } elsif (/^\.([pnil]p|sh)$Sp/) {
202 } elsif (/^\.Dd$Sp/) {
204 } elsif (/^\.(Tp|Dp|De|Cx|Cl)$Sp/) {
205 $Groff{'mdoc_old'} = 1;
207 # In the old version of -mdoc `Oo' is a toggle, in the new it's
214 # The test for `Oo' and `Oc' not starting a line (as allowed by the
215 # new implementation of -mdoc) is not complete; it assumes that
216 # macro arguments are well behaved, i.e., "" is used within "..." to
217 # indicate a doublequote as a string element, and weird features
218 # like `.foo a"b' are not used.
219 elsif (/^\..* Oo( |$)/) {
227 } elsif (/^\.Oc$Sp/) {
231 } elsif (/^\..* Oc( |$)/) {
239 } elsif (/^\.(PRINTSTYLE|START)$Sp/) {
247 &process($_, $level + 1) unless /\\/ || $_ eq "";
255 usage: grog [option]... [--] [filespec]...
257 "filespec" is either the name of an existing, readable file or "-" for
258 standard input. If no "filespec" is specified, standard input is
259 assumed automatically.
261 "option" is either a "groff" option or one of these:
263 -C compatibility mode
264 -h --help print this uasge message
265 -v --version print version information
267 "groff" options are appended to the output, "-m" options are checked.
274 my ($exit_status) = @_;
275 print "Perl version of GNU $Prog of $Last_Update " .
276 "in groff version @VERSION@\n";
286 $Groff{'refer'} ||= $Groff{'refer_open'} && $Groff{'refer_close'};
288 if ( $Groff{'pic'} || $Groff{'tbl'} || $Groff{'eqn'} ||
289 $Groff{'grn'} || $Groff{'grap'} || $Groff{'refer'} ) {
291 $s .= "s" if $Groff{'soelim'};
292 $s .= "R" if $Groff{'refer'};
293 # grap must be run before pic
294 $s .= "G" if $Groff{'grap'};
295 $s .= "p" if $Groff{'pic'};
296 $s .= "g" if $Groff{'grn'};
297 $s .= "t" if $Groff{'tbl'};
298 $s .= "e" if $Groff{'eqn'};
302 if ( $Groff{'me'} ) {
304 push(@Command, '-me');
306 if ( $Groff{'SH'} && $Groff{'TH'} ) {
308 push(@Command, '-man');
311 if ( $Groff{'mom'} ) {
313 push(@Command, '-mom');
316 if ( $Groff{'mm'} || ($Groff{'P'} && ! $is_man) ) {
318 push(@Command, '-mm');
321 if ( $Groff{'NH'} || ($Groff{'TL'} && ! $is_mm) ||
322 ($Groff{'ILP'} && ! $is_man) ||
323 ($Groff{'PP'} && ! $is_mom && ! $is_man) ) {
324 # .PP occurs in -mom, -man and -ms, .IP and .LP occur in -man and -ms
326 push(@Command, '-ms');
328 if ( $Groff{'mdoc'} ) {
329 my $s = ( $Groff{'mdoc_old'} || $Groff{'Oo'} ) ? '-mdoc-old' : '-mdoc';
334 if ($Groff{'chem'}) {
335 my @chem = ('chem', @ARGV, '|', 'groff');
336 unshift(@Command, @chem);
338 unshift @Command, 'groff';
339 push(@Command, @ARGV);
347 # We could implement an option to execute the command here.
349 # foreach (@Command) {
350 # next unless /[\$\\\"\';&()|<> \t\n]/;
352 # $_ = "'" . $_ . "'";
356 my $np = scalar @Mparams;
357 print STDERR "$Prog: more than 1 `-m' argument: @Mparams" if $np > 1;
359 unshift @Command, $Mparams[0] if $np == 1;
362 print STDERR "$Prog: wrong `-m' argument: $Mparams[0]\n"
363 if $m[0] ne $Mparams[0];
366 print STDERR "$Prog: error: there are several macro packages: @m\n";
375 ########################################################################