summaryrefslogtreecommitdiff
path: root/contrib/groffer/perl/man.pl
blob: 6193a35905773372dfcea3a8468a84845deae3e3 (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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
#! /usr/bin/env perl

# groffer - display groff files

# Source file position: <groff-source>/contrib/groffer/perl/man.pl
# Installed position: <prefix>/lib/groff/groffer/man.pl

# Copyright (C) 2006, 2009, 2011 Free Software Foundation, Inc.
# Written by Bernd Warken <groff-bernd.warken-72@web.de>.

# Last update: 27 Jan 2011

# This file is part of `groffer', 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 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/>.

########################################################################

use strict;
use warnings;

########################################################################
# functions for apropos, man, whatis
########################################################################

##########
# apropos_filespec()
#
# Compose temporary file for filspec.
#
# Globals:  in: $main::Opt{'APROPOS'}, $main::Special_Setup, $main::Filespec_Arg,
#               $main::Apropos_Prog, $main::Opt{'APROPOS_SECTIONS'}, $main::Opt{'SECTIONS'}
#          out: $main::Special_Filespec
#
sub apropos_filespec {
  return 0 unless $main::Opt{'APROPOS'};
  die 'apropos_filespec(): apropos_setup() must be run first;'
    unless $main::Special_Setup;
  die 'apropos_filespec(): no $main::Filespec_Arg is set;'
    unless defined $main::Filespec_Arg;
  $main::Special_Filespec = 1;

  my $s;
  if ($main::No_Filespecs) {
    &to_tmp_line('.SH no filespec');
    $s = `$main::Apropos_Prog`;
    $s =~ s/^/\\\&/;
    &to_tmp_line($s);
    return 1;
  }

  $s = $main::Filespec_Arg;
  $s =~ s/[^\\]-/\\-/g;
  &to_tmp_line(".SH $s");

  if ( $main::Opt{'APROPOS_SECTIONS'} ) {
    $s = qr/^[^\(]*\([$main::Opt{'APROPOS_SECTIONS'}]/;
  } else {
    if ( $main::Opt{'SECTIONS'} ) {
      $s = $main::Opt{'SECTIONS'};
      $s = qr/^[^\(]*\([$s]/;
    } else {
      $s = qr/^.*\(.+\).*$/;
    }
  }

### apropos_filespec()
  my $filespec = $main::Filespec_Arg;
  $filespec =~ s#/#\\/#g;
  $filespec =~ s#\.#\\./#g;
  my @ap;
  foreach ( `$main::Apropos_Prog $main::Filespec_Arg 2>$main::Dev_Null` ) {
    chomp;
    if (/^$filespec:\s/) {	# for error messages of this script
      my $line = $_;
      $line =~ s/^(.*)$/\\\&$1/s;
      push @ap, $line;
    }
    if (/$s/) {
      push @ap, $_;
    }
  }
  my @res;
  foreach (sort @ap) {
    s/^
      ([^\(]+\(+[$main::Man{'AUTO_SEC_CHARS'}][^\)]*\)+)
      (\s*-*)*\s*
      (.*)
      $/.br\n.TP 15\n.BR "$1"\n\\\&$3\n/sx;
    push @res, $_;
  }
  &to_tmp_line(@res);
  1;
}				# apropos_filespec()


##########
# apropos_setup()
#
# Setup for the --apropos* options, just 2 global variables are set.
#
# Globals:  in: $main::Opt{'APROPOS'}
#          out: $main::Special_Setup, $main::Apropos_Prog
#
sub apropos_setup {
  return 0 unless $main::Opt{'APROPOS'};
  if ( &where_is_prog('apropos') ) {
    $main::Apropos_Prog = 'apropos';
  } elsif ( &where_is_prog('man') ) {
    if (! system("man --apropos man >$main::Dev_Null 2>$main::Dev_Null")) {
      $main::Apropos_Prog = 'man --apropos';
    } elsif (! system("man -k man >$main::Dev_Null 2>$main::Dev_Null")) {
      $main::Apropos_Prog = 'man --k';
    }
  }				# test man
  die 'apropos_setup(): no apropos program available;'
    unless $main::Apropos_Prog;
  &to_tmp_line('.TH GROFFER APROPOS');
  $main::Special_Setup = 1;
  $main::Opt{'TITLE'} = 'apropos' unless $main::Opt{'TITLE'};
  1;
}				# apropos_setup()


##########
# is_man(<man_sec_ext-hash-ref>)
#
# Test whether the argument hash exists as man page (from is_man()).
#
# Globals: in: $main::Man{AUTO_SEC_CHARS},$main::Man{SEC_CHARS},
#              $main::Man{EXT}, $tmpdir
#         out: $main::Manspec
#
# Arguments: 1, a hash refernce with keys 'name', 'sec', 'ext', where
#               'sec' is a string of one or several section characters
#
sub is_man {
  my $n = @_;
  die "is_man(): one argument is needed, you used $n;"
    unless $n == 1;
  die "is_man(): the argument is not a hash reference `$_[0]';"
    if ref($_[0]) ne 'HASH';
  die 'is_man(): temporary directory is not set;' unless $main::tmpdir;
  die 'is_man(): man_setup() must be run first;' unless $main::Man{'IS_SETUP'};

  my $name = $_[0]->{'name'};
  unless ($name) {
    $main::Manspec = '';
    return 0;
  }

  my $sec;
  $sec = $_[0]->{'sec'};
  $sec = $main::Man{'SEC_CHARS'} unless $sec;
  $sec = $main::Man{'AUTO_SEC_CHARS'} unless $sec;
  $sec = '' unless defined $sec;
  # take care, $sec may be several sections

### is_man()
  my $ext;
  $ext = $_[0]->{'ext'};
  $ext = $main::Man{'EXT'} unless $ext;
  $ext = '' unless defined $ext;

  $main::Manspec = "$name.$sec,$ext";
  my @m;
  if (exists $main::Man{'MANSPEC'}{$main::Manspec}) {
    return 1;
  } else {
    foreach my $p ( @{$main::Man{'PATH'}} ) {
      foreach my $s (split //, $sec) {
	my $dir = File::Spec->catdir($p, "man$s");
	next unless -d $dir;
	my $file = File::Spec->catfile($dir, "$name.$s$ext");
	push @m, glob("$file*");
      }
    }
    $main::Man{'MANSPEC'}{$main::Manspec} = \@m;
  }
  return 0 unless (@m);
  return 1;
} # is_man()


##########
# man_get (<man_sec_ext-hash-ref>)
#
# Write a man page to the temporary file.
#
# Globals in: $main::Manspec, $main::Man{MANSPEC}, $main::Man{SEC_CHARS},
#             $main::Man{EXT}, $main::Man{ALL}
#
# Arguments: 1, a hash refernce with keys 'name', 'sec', 'ext', where
#               'sec' is a string of one or several section characters
#
sub man_get {
  my $n = @_;
  die "man_get(): one argument is needed, you used $n;"
    unless $n == 1;
  die "man_get(): the argument is not a hash reference `$_[0]';"
    if ref($_[0]) ne 'HASH';
  die "man_get(): is_man() must be run first on the argument;"
    unless $main::Manspec;
  die "man_get(): wrong hash reference `$_[0]', no 'name' key;"
    unless exists $_[0]->{'name'};

  my ($name, $sec, $ext, $f, $path);
  $name = $_[0]->{'name'};
  die "man_get(): empty 'name' key in the argument;" unless $name;

  $sec = $_[0]->{'sec'};
  $sec = $main::Man{'SEC_CHARS'} if (! $sec) and $main::Man{'SEC_CHARS'};
  $sec = '' unless defined $sec;
  # take care $sec may be several sections

### man_get()
  $ext = $_[0]->{'ext'};
  $ext = $main::Man{'EXT'} unless $ext;
  $ext = '' unless defined $ext;

  die 'man_get(): $main::Manspec does not suit the arguments;'
    if ($main::Manspec ne "$name.$sec,$ext") and
      (! exists $main::Man{'MANSPEC'}{$main::Manspec});

  if ($main::Man{'ALL'}) {
    my $ok;
    my %list;
    foreach ( @{$main::Man{'MANSPEC'}{$main::Manspec}} ) {
      next if exists $list{$_};
      if (-f $_ and -r $_) {
	$list{$_} = 1;
	&to_tmp($_);
	$ok = 1;
      }
    }
    &register_title("man:$name") if $ok;
    return 1;
  }

  # not $main::Man{'ALL'}

  if ($_[0]->{'sec'}) {
    my $path = File::Spec->catfile('', "man$sec", $name);
    if ($ext) {
      foreach $f ( @{$main::Man{'MANSPEC'}{$main::Manspec}} ) {
	if ($f =~ m#$path\.$sec$ext($|\..*$)#) {
	  if (-f $f && -r $f) {
	    &register_file($f);
	    return 1;
	  }
	}			# if $f =~
      }				# foreach $f
### man_get()
      foreach $f ( @{$main::Man{'MANSPEC'}{$main::Manspec}} ) {
	if ($f =~ m#$path\.$sec$ext.*$#) {
	  if (-f $f && -r $f) {
	    &register_file($f);
	    return 1;
	  }
	}			# if $f =~
      }				# foreach $f
    } else {			# $ext is empty
      foreach $f ( @{$main::Man{'MANSPEC'}{$main::Manspec}} ) {
	if ($f =~ m#$path\.$sec($|\..*$)#) {
	  if (-f $f && -r $f) {
	    #	    &to_tmp($f) && &register_file($f);
	    &register_file($f);
	    return 1;
	  }
	}			# if $f =~
      }				# foreach $f
      foreach $f ( @{$main::Man{'MANSPEC'}{$main::Manspec}} ) {
	if ($f =~ m#$path\.$sec.*$#) {
	  if (-f $f && -r $f) {
	    #	    &to_tmp($f) && &register_file($f);
	    &register_file($f);
	    return 1;
	  }
	}			# if $f =~
      }				# foreach $f
    }				# if $ext
### man_get()
  } else {			# sec is empty
    my $m = $main::Man{'SEC_CHARS'};
    $m = $main::Man{'AUTO_SEC_CHARS'} unless $m;
    foreach my $s (split //, $m) {
      my $path = File::Spec->catfile('', "man$s", $name);
      unless ($ext) {
	foreach my $f ( @{$main::Man{'MANSPEC'}{$main::Manspec}} ) {
	  if ( $f =~ m#$path\.$s($|\..*$)# ) {
	    if (-f $f && -r $f) {
	      #	      &to_tmp($f) && &register_file($f);
	      &register_file($f);
	      return 1;
	    }
	  }			# if $f =~
	}			# foreach $f
      }				# unless $ext
      foreach $f ( @{$main::Man{'MANSPEC'}{$main::Manspec}} ) {
	if ($f =~ m#$path\.$s$ext.*$#) {
	  if (-f $f && -r $f) {
	    #	    &to_tmp($f) && &register_file($f);
	    &register_file($f);
	    return 1;
	  }
	}			# if $f =~
      }				# foreach $f
    }				# foreach $s
  }				# if sec

  1;
} # man_get()


##########
# man_setup ()
#
# Setup the variables in %MAN needed for man page searching.
#
# Globals:
#   in:     %OPT, $LANG, $LC_MESSAGES, $LC_ALL,
#           $MANPATH, $MANSEC, $PAGER, $SYSTEM, $MANOPT.
#   out:    $main::Man{PATH}, $main::Man{LANG}, $main::Man{LANG2}, $main::Man{SYS},
#           $main::Man{SEC}, $main::Man{ALL}
#   in/out: $main::Man{ENABLE}
#
# The precedence for the variables related to `man' is that of GNU
# `man', i.e.
#
# $LANG; overridden by
# $LC_MESSAGES; overridden by
# $LC_ALL; this has the same precedence as
# $MANPATH, $MANSEC, $PAGER, $SYSTEM; overridden by
# $MANOPT; overridden by
# the groffer command line options.
#
# $MANROFFSEQ is ignored because grog determines the preprocessors.
#
sub man_setup {
  return 1 if $main::Man{'IS_SETUP'};
  $main::Man{'IS_SETUP'} = 1;
  return 1 unless $main::Man{'ENABLE'};

  # determine basic path for man pages
  my $path;
  if (defined $main::Opt{'MANPATH'}) {
    $path = $main::Opt{'MANPATH'};
  } elsif ($ENV{'MANPATH'}) {
    $path = $ENV{'MANPATH'};
  } elsif ( &where_is_prog('manpath') ) {
    $path = `manpath 2>$main::Dev_Null`;
  }
  if ($path) {
    chomp $path;
    $main::Man{'PATH'} = [split /:/, $path];
  } else {
    $main::Man{'PATH'} = [];
    &manpath_set_from_path();
  }
  unless ( @{$main::Man{'PATH'}} ) {
    $main::Man{'ENABLE'} = 0;
    warn "man_setup(): man path is empty;";
    return 1;
  }

### man_setup()
  # make man path list consisting of unique existing directories
  @{$main::Man{'PATH'}} = &path_uniq( @{$main::Man{'PATH'}} );

  unless ($main::Man{'ALL'}) {
    $main::Man{'ALL'} = $main::Opt{'ALL'} ? 1 : 0;
  }

  # handle man systems
  my $sys = $ENV{'SYSTEM'};
  $sys = $main::Opt{'SYSTEMS'} if $main::Opt{'SYSTEMS'};
  if ($sys) {
    chomp $sys;
    $main::Man{'SYS'} = [split /,/, $sys];
  } else {
    $main::Man{'SYS'} = [];
    $sys = '';
  }

### man_setup()
  # handle language
  my $lang = '';
  $lang = $main::Opt{'LANG'} if $main::Opt{'LANG'};
  unless ($lang) {
    foreach ('LC_ALL', 'LC_MESSAGES', 'LANG') {
      if ($ENV{$_}) {
	$lang = $ENV{$_};
	last;
      }
    }
  }
  if ($lang) {
    if ($lang eq 'C' or $lang eq 'POSIX') {
      $main::Man{'LANG'} = '';
      $main::Man{'LANG2'} = '';
    } elsif ($lang =~ /^.$/) {
      $main::Man{'LANG'} = $lang;
      $main::Man{'LANG2'} = '';
    } elsif ($lang =~ /^..$/) {
      $main::Man{'LANG'} = $lang;
      $main::Man{'LANG2'} = $lang;
    } else {
      $main::Man{'LANG'} = $lang;
      $main::Man{'LANG2'} = $lang;
      $main::Man{'LANG2'} =~ s/^(..).*$/$1/;
    }
  }				# if ($lang)
  # from now on, use only $main::Man{LANG*}, forget about $main::Opt{LANG}, $ENV{LC_*}.

  &manpath_add_lang_sys();

### man_setup()
  # section
  my $sec;
  $sec = $main::Opt{'SECTIONS'} if $main::Opt{'SECTIONS'};
  unless ($sec) {
    $sec = $ENV{'MANSEC'} if $ENV{'MANSEC'};
  }
  $main::Man{'SEC'} = [];
  $main::Man{'SEC_CHARS'} = '';
  if ($sec) {
    foreach (split /:/, $sec) {
      push @{$main::Man{'SEC'}}, $_ if /^[$main::Man{'AUTO_SEC_CHARS'}]$/;
    }
    $main::Man{'SEC_CHARS'} = join '', @{$main::Man{'SEC'}} if @{$main::Man{'SEC'}};
  }				# if ($sec)

  # extension
  my $ext = '';
  $ext = $main::Opt{'EXTENSION'} if $main::Opt{'EXTENSION'};
  unless ($ext) {
    $ext = $ENV{'EXTENSION'} if $ENV{'EXTENSION'};
  }
  $main::Man{'EXT'} = $ext;

  # creation of man temporary is omitted, because of globs in perl
  1;
} # man_setup()


##########
# manpath_add_lang_sys()
#
# Add language and operating system specific directories to man path.
#
# Globals:
#   in:     $main::Man{SYS}: a list of names of operating systems.
#           $main::Man{LANG} and $main::Man{LANG2}: each a single name
#   in/out: @{$main::Man{PATH}}: list of directories which shall have the `man?'
#           subdirectories.
#
sub manpath_add_lang_sys {
  return 1 unless $main::Man{'PATH'};
  return 1 unless @{$main::Man{'PATH'}};

  my @mp;

  if ( @{$main::Man{'SYS'}} ) {
    foreach ( @{$main::Man{'SYS'}} ) {
      if ($_ eq 'man') {
	@mp = (@mp, @{$main::Man{'PATH'}});
      } elsif ($_) {
	my $sys = $_;
	foreach my $p (@{$main::Man{'PATH'}}) {
	  my $dir = File::Spec->catdir($p, $sys);
	  push @mp, $dir;
	}
      }				# if eq 'man'
    }				# foreach SYS
  } else {			# no SYS
    @mp = @{$main::Man{'PATH'}};
  }

  if (@mp && $main::Man{'LANG'}) {
    my @lang_path;
    my $man_lang2 = '';
    $man_lang2 = $main::Man{'LANG2'} if $main::Man{'LANG'} ne $main::Man{'LANG2'};
    foreach my $i ($main::Man{'LANG'}, $man_lang2) {
      next unless $i;
      my $lang = $i;
      foreach my $p (@mp) {
	my $dir = File::Spec->catdir($p, $lang);
	push @lang_path, $dir;
      }				# foreach $p
    }				# foreach $i
    @mp = (@lang_path, @mp);
  }				# if $mp

  $main::Man{PATH} = [&path_uniq(@mp)];
  1;
} # manpath_add_lang_sys()


##########
# manpath_set_from_path()
#
# Determine basic search path for man pages from $PATH.
#
# Return:    `1' if a valid man path was retrieved.
# Output:    none
# Globals:
#   in:  $PATH
#   out: $_MAN_PATH
#
sub manpath_set_from_path {
  my @path =
    qw( /usr/local /usr /usr/X11R6 /usr/openwin /opt /opt/gnome /opt/kde );
  # get a basic man path from $ENV{PATH}
  my @path2 = @main::Path;
  foreach (@path2) {
    s#bin/*$##;
    push @path, $_;
  }
  @path = &path_uniq(@path);
  foreach my $d (@path) {
    foreach my $e ( File::Spec->catdir( qw(share man) ),
		 File::Spec->catdir( qw(share MAN) ),
		 'man',  'MAN' ) {
      my $dir = File::Spec->catdir($d, $e);
      push @{$main::Man{'PATH'}}, $dir if -d $dir;
    }
  }
  1;
} # manpath_set_from_path()


##########
# special_filespec()
#
# Handle special modes like whatis and apropos.  Run their filespec
# functions if suitable.
#
# Globals:  in: $main::Opt{'APROPOS'}, $main::Opt{'WHATIS'}, $main::Special_Setup
#          out: $main::Special_Filespec (internal)
#
sub special_filespec {
  return 0 unless $main::Opt{'APROPOS'} || $main::Opt{'WHATIS'};
  die 'special_filespec(): setup for apropos or whatis must be run first;'
    unless $main::Special_Setup;
  if ( $main::Opt{'APROPOS'} ) {
    die "special_setup(): both apropos and whatis option variables are set;"
      if $main::Opt{'WHATIS'};
    return &apropos_filespec() ? 1 : 0;
  }
  if ( $main::Opt{'WHATIS'} ) {
    return &whatis_filespec() ? 1 : 0;
  }
  0;
} # special_filespec


##########
# special_setup()
#
# Handle special modes like whatis and apropos.  Run their setup
# functions if suitable.
#
sub special_setup {
  if ( $main::Opt{'APROPOS'} ) {
    die "special_setup(): both --apropos and --whatis option variables " .
      "are set;" if $main::Opt{'WHATIS'};
    return &apropos_setup() ? 1 : 0;
  }
  if ( $main::Opt{'WHATIS'} ) {
    return &whatis_setup() ? 1 : 0;
  }
  0;
} # special_setup()


##########
# whatis_filename(<filename>)
#
# Interpret <filename> as a man page and display its `whatis'
# information as a fragment written in the groff language.
#
# Globals:  in: $main::Opt{'WHATIS'}, $main::Special_Setup, $main::Special_Filespec,
#               $main::Filespec_Arg
#
sub whatis_filename {
  my $n = @_;
  die "whatis_filename(): one argument is needed; you used $n;"
    unless $n == 1;
  die "whatis_filename(): whatis is not setup;" unless $main::Opt{'WHATIS'};
  die "whatis_filename(): setup for whatis must be run first;"
    unless $main::Special_Setup;
  die "whatis_filename(): whatis_filespec() must be run first;"
    unless $main::Special_Filespec;
  die 'whatis_filename(): no $main::Filespec_Arg is set;'
    unless defined $main::Filespec_Arg;

  my $arg = $_[0];
  die "whatis_file_name(): $arg is not an existing file" unless -f $arg;
  die "whatis_file_name(): could not read file $arg" unless -r $arg;
  return 1 if -z $arg;
  $arg = 'stdin' if $main::Filespec_Arg eq '-';

  &to_tmp_line('.br', '\\f[CR]' . $arg . '\\f[]:', '.br');

### whatis_filename()
  # get the parts of the file name according to the naming of man pages
  my ($dir, $name) = &split_path( $_[0] );
  my $section = $_[0];
  my $path = File::Spec->catfile('', 'man');
  $section =~ s#^.*$path([$main::Man{'AUTO_SEC_CHARS'}]).*$#$1#;
  $section = '' if $section eq $_[0];
  if ($section) {
    if ($name =~ m#^.*\.$section.*$#) {
      $name =~ s/^(.*)\.$section.*$/$1/;
    } else {
      $section = '';
    }
  }

  # traditional man style; grep the line containing `.TH' macro, if any
  my @catz = &cat_z($_[0]);
  my $res;
  my $test;
  foreach (@catz) {
    if (/^[\.']\s*TH\s/) {		# search .TH, traditional man style
      $test = 1;
      last;
    }
  }				# foreach (@catz)
### whatis_filename()
  if ($test) {			# traditional man style
    # get the first line after the first `.SH' macro before the next `.SH'
    my $test1;
    foreach (@catz) {
      if ($test1) {
	chomp;
	next unless $_;
	next if /^[\.']?\s*$/;
	last if /^[\.']/;		# especially for .SH
	if ($res) {
	  $res = "$res $_";
	} else {
	  $res = $_;
	}
      } else {			# $test1 is empty
	$test1 = 1 if (/^[\.']\s*SH\s/);
      }				# if ($test1)
    }				# foreach (@catz)

    if ($section) {
      if ($res =~ m|^
		    $name
		    \s
		    [^-]*
		    -
		    \s*
		    (.*)
		    $|sx) {
	$res = "$name ($section) \\[em] $1";
      }
    }				# if ($section)
### whatis_filename()
    &to_tmp_line($res, '.br', '', '.br');
    return 1;
  }				# if ($test)

  # mdoc style (BSD doc); grep the line containing `.Nd' macro, if any
  foreach (@catz) {
    if (/^[\.']\s*Nd\s/) {		# BSD doc style
      $res =~ s/^(.*)$/$name ($section) \\[em] $1/;
      &to_tmp_line($res);
      return 1;
    }
  }

  &to_tmp_line('is not a man page', '.br', '', '.br');
  1;
} # whatis_filename()


##########
# whatis_filespec()
#
# Print the filespec name as .SH to the temporary cat file.
#
# Globals: in: $main::Opt{'WHATIS'}, $main::Filespec_Arg
#      in/out: $main::Special_Filespec
#
sub whatis_filespec {
  return 0 unless ($main::Opt{'WHATIS'});
  die 'whatis_filespec(): whatis_setup() must be run first.'
    unless $main::Special_Setup;
  die 'whatis_filespec(): no $main::Filespec_Arg is set;'
    unless defined $main::Filespec_Arg;
  $main::Special_Filespec = 1;
  my $arg = $main::Filespec_Arg;
  $arg =~ s/[^\\]-/\\-/g;
  to_tmp_line(".SH $arg");
} # whatis_filespec()


##########
# whatis_setup ()
#
# Print the whatis header to the temporary cat file; this is the setup
# for whatis.
#
# Globals:  in: $main::Opt{'WHATIS'}
#          out: $main::Special_Setup
sub whatis_setup {
  if ( $main::Opt{'WHATIS'} ) {
    &to_tmp_line('.TH GROFFER WHATIS');
    $main::Special_Setup = 1;
    $main::Opt{'TITLE'} = 'whatis' unless $main::Opt{'TITLE'};
    return 1;
  }
  0;
} # whatis_setup ()

1;