summary refs log tree commit diff
path: root/linuxthreads/man/troffprepro
blob: ba564fefbe4c11ced1a1c841855b16d92c5e3cbc (plain) (blame)
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
#!/usr/local/bin/perl

$insynopsis = 0;

open(INPUT, $ARGV[0]) || die("cannot open $ARGV[0]");
open(OUTPUT, "> $ARGV[1]") || die("cannot create $ARGV[1]");

select(OUTPUT);

line:
while(<INPUT>) {
  if (/^\.XREF (.*)$/) {
    $xref = $1;
    $_ = $ARGV[1];
    m/^.*\.(([1-8]).*)$/;
    $suffix = $1;
    $extension = $2;
    open(XREF, "> $xref.$suffix");
    print XREF ".so man$extension/$ARGV[1]\n";
    close(XREF);
    next line;
  }
  if (/^\.SH/) {
    $insynopsis = /SYNOPSIS/;
    print $_;
    next;
  }
  if ($insynopsis) {
    if (/^#/) {
      print ".B ", $_;
    }
    elsif (/^[a-z]/) {    
      chop;
#      if (m/^([a-zA-Z][a-zA-Z0-9_]*\s+[a-zA-Z][a-zA-Z0-9_]*)\(/) {
#          print ".B \"", $1, "\"\n";
#          $_ = '(' . $';
#      }
#      s/([a-zA-Z][a-zA-Z0-9_]*)(\s*[,()=])/" \1 "\2/g;
      s/([ *])([a-zA-Z][a-zA-Z0-9_]*)(\s*[,)=])/\1" \2 "\3/g;
      print ".BI \"", $_, "\"\n";
    }
    else {
      print $_;
    }
    next;
  }
  chop;
  s/!([^!]+)!\|([^|]+)\|([^\s]*)\s*/\n.BI "\1" "\2\3"\n/g;
  s/([!|])([^!|]+)\1([^\s]*)\s*/do subst($1,$2,$3)/eg;
  s/^\n+//;
  s/\n+$//;
  s/\n\n+/\n/g;
  print $_, "\n";
}

close(INPUT);
close(OUTPUT);

sub subst {
  local ($a, $b, $c) = @_;
  if ($c) {
    "\n" . ($a eq "!" ? ".BR " : ".IR ") . "\"$b\" $c\n"
  } else {
    "\n" . ($a eq "!" ? ".B " : ".I ") . "\"$b\"\n"
  }
}