summaryrefslogtreecommitdiff
path: root/src/roff/grog/grog.pl
blob: 3f33d8676f60985e007be2507b78dbbf33e216ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#! /usr/bin/env perl
# grog - guess options for groff command
# Inspired by doctype script in Kernighan & Pike, Unix Programming
# Environment, pp 306-8.

# Source file position: <groff-source>/src/roff/grog/grog.pl
# Installed position: <prefix>/bin/grog

# Copyright (C) 1993, 2006, 2009, 2011-2012 Free Software Foundation, Inc.
# Written by James Clark, maintained by Werner Lemberg.
# Rewritten and put under GPL by Bernd Warken <groff-bernd.warken-72@web.de>.

# This file is part of `grog', which is part of `groff'.

# `groff' is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License (GPL) as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# `groff' is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

########################################################################
my $Last_Update = '24 May 2012';
########################################################################

require v5.6;

use warnings;
use strict;
use File::Spec;

my $Prog = $0;
{
  my ($v, $d, $f) = File::Spec->splitpath($Prog);
  $Prog = $f;
}

#my $Sp = "[\\s\\n]";
my $Sp = qr([\s\n]);

my @Command;			# stores the final output
my @Mparams;			# stores the options -m*
my %Groff;

{
  my @filespec = ();
  my $double_minus = 0;
  my $was_minus = 0;
  my $had_filespec = 0;

  foreach my $arg (@ARGV) {
    next unless $arg;
    if ($double_minus) {
      $had_filespec = 1;
      if (-f $arg && -r $arg) {
	push @filespec, $arg;
      } else {
	print STDERR "grog: $arg is not a readable file.\n";
      }
      next;
    }

    if ($arg eq '--') {
      $double_minus = 1;
      push(@Command, $arg);
      next;
    }
    if ($arg eq '-') {
      unless ($was_minus) {
	push @filespec, $arg;
	$was_minus = 1;
      }
      next;
    }

    &version(0) if $arg eq '-v' || '--version' =~ /^$arg/;
    &help() if $arg eq '-h' || '--help' =~ /^$arg/;
    print STDERR "grog: wrong option $arg.\n" if $arg =~ /^--/;

    if ($arg =~ /^-m/) {
      push @Mparams, $arg;
      next;
    }
    $Sp = '' if $arg eq '-C';

    if ($arg =~ /^-/) {
      push(@Command, $arg);
      next;
    } else {
      $had_filespec = 1;
      if (-f $arg && -r $arg) {
	push @filespec, $arg;
      } else {
	print STDERR "grog: $arg is not a readable file.\n";
      }
      next;
    }
  }
  @filespec = ('-') if ! @filespec && ! $had_filespec;
  exit 1 unless @filespec;
  @ARGV = @filespec;
}

foreach my $arg (@ARGV) {
  &process($arg, 0);
}

sub process {
  my ($filename, $level) = @_;
  local(*FILE);

  if (!open(FILE, $filename eq "-" ? $filename : "< $filename")) {
    print STDERR "$Prog: can't open \`$filename': $!\n";
    exit 1 unless $level;
    return;
  }
  while (<FILE>) {
    chomp;
    s/^[.']\s*/./;
    s/^\s+|\s+$//g;
    s/$/\n/;

    if (/^(.cstart)|(begin\s+chem)$/) {
      $Groff{'chem'}++;
      $Groff{'soelim'}++ if $level;
    } elsif (/^\.TS$Sp/) {
      $_ = <FILE>;
      if (!/^\./ || /^\.so/) {
	$Groff{'tbl'}++;
	$Groff{'soelim'}++ if $level;
      }
    } elsif (/^\.EQ$Sp/) {
      $_ = <FILE>;
      if (!/^\./ || /^\.[0-9]/ || /^\.so/) {
	$Groff{'eqn'}++;
	$Groff{'soelim'}++ if $level;
      }
    } elsif (/^\.GS$Sp/) {
      $_ = <FILE>;
      if (!/^\./ || /^\.so/) {
	$Groff{'grn'}++;
	$Groff{'soelim'}++ if $level;
      }
    } elsif (/^\.G1$Sp/) {
      $_ = <FILE>;
      if (!/^\./ || /^\.so/) {
	$Groff{'grap'}++;
	$Groff{'soelim'}++ if $level;
      }
#    } elsif (/^\.PS\Sp([ 0-9.<].*)?$/) {
#      if (/^\.PS\s*<\s*(\S+)/) {
#	$Groff{'pic'}++;
#	$Groff{'soelim'}++ if $level;
#	&process($1, $level);
#      } else {
#	$_ = <FILE>;
#	if (!/^\./ || /^\.ps/ || /^\.so/) {
#	  $Groff{'pic'}++;
#	  $Groff{'soelim'}++ if $level;
#	}
#      }
    } elsif (/^\.PS[\s\n<]/) {
      $Groff{'pic'}++;
      $Groff{'soelim'}++ if $level;
      if (/^\.PS\s*<\s*(\S+)/) {
	&process($1, $level);
      }
    } elsif (/^\.R1$Sp/) {
      $Groff{'refer'}++;
      $Groff{'soelim'}++ if $level;
    } elsif (/^\.\[/) {
      $Groff{'refer_open'}++;
      $Groff{'soelim'}++ if $level;
    } elsif (/^\.\]/) {
      $Groff{'refer_close'}++;
      $Groff{'soelim'}++ if $level;
    } elsif (/^\.NH$Sp/) {
      $Groff{'NH'}++;		# for ms
    } elsif (/^\.TL$Sp/) {
      $Groff{'TL'}++;		# for mm and ms
    } elsif (/^\.PP$Sp/) {
      $Groff{'PP'}++;		# for mom and ms
    } elsif (/^\.[IL]P$Sp/) {
      $Groff{'ILP'}++;		# for man and ms
    } elsif (/^\.P$/) {
      $Groff{'P'}++;
    } elsif (/^\.(PH|SA)$Sp/) {
      $Groff{'mm'}++;
    } elsif (/^\.TH$Sp/) {
      $Groff{'TH'}++;
    } elsif (/^\.SH$Sp/) {
      $Groff{'SH'}++;
    } elsif (/^\.([pnil]p|sh)$Sp/) {
      $Groff{'me'}++;
    } elsif (/^\.Dd$Sp/) {
      $Groff{'mdoc'}++;
    } elsif (/^\.(Tp|Dp|De|Cx|Cl)$Sp/) {
      $Groff{'mdoc_old'} = 1;
    }
    # In the old version of -mdoc `Oo' is a toggle, in the new it's
    # closed by `Oc'.
    elsif (/^\.Oo$Sp/) {
      $Groff{'Oo'}++;
      s/^\.Oo/\. /;
      redo;
    }
    # The test for `Oo' and `Oc' not starting a line (as allowed by the
    # new implementation of -mdoc) is not complete; it assumes that
    # macro arguments are well behaved, i.e., "" is used within "..." to
    # indicate a doublequote as a string element, and weird features
    # like `.foo a"b' are not used.
    elsif (/^\..* Oo( |$)/) {
      s/\\\".*//;
      s/\"[^\"]*\"//g;
      s/\".*//;
      if (s/ Oo( |$)/ /) {
	$Groff{'Oo'}++;
      }
      redo;
    } elsif (/^\.Oc$Sp/) {
      $Groff{'Oo'}--;
      s/^\.Oc/\. /;
      redo;
    } elsif (/^\..* Oc( |$)/) {
      s/\\\".*//;
      s/\"[^\"]*\"//g;
      s/\".*//;
      if (s/ Oc( |$)/ /) {
	$Groff{'Oo'}--;
      }
      redo;
    } elsif (/^\.(PRINTSTYLE|START)$Sp/) {
      $Groff{'mom'}++;
    }
    if (/^\.so$Sp/) {
      chop;
      s/^.so *//;
      s/\\\".*//;
      s/ .*$//;
      # The next if-clause catches e.g.
      #
      #   .EQ
      #   .so foo
      #   .EN
      #
      # However, the code is not fully correct since it is too generous.
      # Theoretically, we should check for .so only within preprocessor
      # blocks like .EQ/.EN or .TS/.TE; but it doesn't harm if we call
      # soelim even if we don't need to.
      if ( $Groff{'pic'} || $Groff{'tbl'} || $Groff{'eqn'} ||
	   $Groff{'grn'} || $Groff{'grap'} || $Groff{'refer'} ||
	   $Groff{'refer_open'} || $Groff{'refer_close'} ||
	   $Groff{'chem'} ) {
	$Groff{'soelim'}++;
      }
      &process($_, $level + 1) unless /\\/ || $_ eq "";
    }
  }
  close(FILE);
}

sub help {
  print <<EOF;
usage: grog [option]... [--] [filespec]...

"filespec" is either the name of an existing, readable file or "-" for
standard input.  If no "filespec" is specified, standard input is
assumed automatically.

"option" is either a "groff" option or one of these:

-C            compatibility mode
-h --help     print this uasge message
-v --version  print version information

"groff" options are appended to the output, "-m" options are checked.

EOF
  exit 0;
}

sub version {
  my ($exit_status) = @_;
  print "Perl version of GNU $Prog of $Last_Update " .
    "in groff version @VERSION@\n";
  exit $exit_status;
}

{
  my @m = ();
  my $is_man = 0;
  my $is_mm = 0;
  my $is_mom = 0;

  $Groff{'refer'} ||= $Groff{'refer_open'} && $Groff{'refer_close'};

  if ( $Groff{'pic'} || $Groff{'tbl'} || $Groff{'eqn'} ||
       $Groff{'grn'} || $Groff{'grap'} || $Groff{'refer'} ||
       $Groff{'chem'} ) {
    my $s = "-";
    $s .= "s" if $Groff{'soelim'};
    $s .= "R" if $Groff{'refer'};
    $s .= "G" if $Groff{'grap'};
    $s .= "j" if $Groff{'chem'};
    $s .= "p" if $Groff{'pic'};
    $s .= "g" if $Groff{'grn'};
    $s .= "t" if $Groff{'tbl'};
    $s .= "e" if $Groff{'eqn'};
    push(@Command, $s);
  }

  if ( $Groff{'me'} ) {
    push(@m, '-me');
    push(@Command, '-me');
  }
  if ( $Groff{'SH'} && $Groff{'TH'} ) {
    push(@m, '-man');
    push(@Command, '-man');
    $is_man = 1;
  }
  if ( $Groff{'mom'} ) {
    push(@m, '-mom');
    push(@Command, '-mom');
    $is_mom = 1;
  }
  if ( $Groff{'mm'} || ($Groff{'P'} && ! $is_man) ) {
    push(@m, '-mm');
    push(@Command, '-mm');
    $is_mm = 1;
  }
  if ( $Groff{'NH'} || ($Groff{'TL'} && ! $is_mm) ||
       ($Groff{'ILP'} && ! $is_man) ||
       ($Groff{'PP'} && ! $is_mom && ! $is_man) ) {
    # .PP occurs in -mom, -man and -ms, .IP and .LP occur in -man and -ms
    push(@m, '-ms');
    push(@Command, '-ms');
  }
  if ( $Groff{'mdoc'} ) {
    my $s = ( $Groff{'mdoc_old'} || $Groff{'Oo'} ) ? '-mdoc-old' : '-mdoc';
    push(@m, $s);
    push(@Command, $s);
  }

  unshift @Command, 'groff';
  push(@Command, @ARGV);

  foreach (@Command) {
    next unless /\s/;
    $_ = "'" . $_ . "'";
  }

  # We could implement an option to execute the command here.

#  foreach (@Command) {
#    next unless /[\$\\\"\';&()|<> \t\n]/;
#    s/\'/\'\\\'\'/;
#    $_ = "'" . $_ . "'";
#  }

  my $n = scalar @m;
  my $np = scalar @Mparams;
  print STDERR "$Prog: more than 1 `-m' argument: @Mparams" if $np > 1;
  if ($n == 0) {
    unshift @Command, $Mparams[0] if $np == 1;
  } elsif ($n == 1) {
    if ($np == 1) {
      print STDERR "$Prog: wrong `-m' argument: $Mparams[0]\n"
	if $m[0] ne $Mparams[0];
    }
  } else {
    print STDERR "$Prog: error: there are several macro packages: @m\n";
  }

  print "@Command\n";

  exit $n if $n > 1;
  exit 0;
}

########################################################################
### Emacs settings
# Local Variables:
# mode: CPerl
# End: