Git init
[external/ifupdown.git] / defn2man.pl
1 #!/usr/bin/perl -w
2
3 use strict;
4
5 # declarations
6 my $line;
7 my $match;
8
9 # subroutines
10 sub nextline {
11         $line = <>;
12         while($line and ($line =~ /^#/ or $line =~ /^\s*$/)) {
13                 $line = <>;
14         }
15         if (!$line) { return 0; }
16         chomp $line;
17         while ($line =~ m/^(.*)\\$/) {
18                 my $addon = <>;
19                 chomp $addon;
20                 $line = $1 . $addon;
21         }
22         return 1;
23 }
24 sub match {
25         my $line = $_[0];
26         my $cmd = "$_[1]" ? "$_[1]\\b\\s*" : "";;
27         my $indentexp = (@_ == 3) ? "$_[2]\\s+" : "";
28
29         if ($line =~ /^${indentexp}${cmd}(([^\s](.*[^\s])?)?)\s*$/) {
30                 $match = $1;
31                 return 1;
32         } else {
33                 return 0;
34         } 
35 }
36 sub skip_section {
37         my $struct = $_[0];
38         my $indent = ($line =~ /(\s*)[^\s]/) ? $1 : "";
39
40         1 while (nextline && match($line, "", $indent));
41 }
42 sub get_address_family {
43         print ".SH " . uc($match) . " ADDRESS FAMILY\n";
44         print "This section documents the methods available in the\n";
45         print "$match address family.\n";
46         nextline;
47 }
48 sub get_architecture {
49         # no op
50         nextline;
51 }
52 sub get_method {
53         my $method = shift;
54         my $indent = ($line =~ /(\s*)\S/) ? $1 : "";
55         my $description = "";
56         my @options = ();
57
58         nextline;
59         while ($line and match($line, "", $indent)) {
60                 if (match($line, "description", $indent)) {
61                         $description = get_description();
62                 } elsif (match($line, "options", $indent)) {
63                         @options = get_options();
64                 } else {
65                         skip_section;
66                 }
67         }
68
69         print ".SS The $method Method\n";
70         if ($description ne "") {
71                 print usenet2man($description) . "\n";
72         } else {
73                 print "(No description)\n";
74         }
75         print ".PP\n";
76         print ".B Options\n";
77         print ".RS\n";
78         if (@options) {
79                 foreach my $o (@options) {
80                         if ($o =~ m/^\s*(\S*)\s*(.*)\s+--\s+(\S.*)$/) {
81                                 my $opt = $1;
82                                 my $optargs = $2;
83                                 my $dsc = $3;
84                                 print ".TP\n";
85                                 print ".BI $opt";
86                                 print " \" $optargs\"" unless($optargs =~ m/^\s*$/);
87                                 print "\n";
88                                 print usenet2man($dsc) . "\n";
89                         } else {
90                                 print ".TP\n";
91                                 print ".B $o\n";
92                         }
93                 }
94         } else {
95                 print ".TP\n";
96                 print "(No options)\n";
97         }
98         print ".RE\n";
99 }
100 sub get_description {
101         my $desc = "";
102         my $indent = ($line =~ /(\s*)\S/) ? $1 : "";
103         while(nextline && match($line, "", $indent)) {
104                 $desc .= "$match\n";
105         }
106         return $desc;
107 }
108 sub usenet2man {
109         my $in = shift;
110         my $out = "";
111
112         $in =~ s/\s+/ /g;
113         while ($in =~ m%^([^*/]*)([*/])([^*/]*)([*/])(.*)$%s) {
114                 my ($pre, $l, $mid, $r, $post) = ($1, $2, $3, $4, $5);
115                 if ($l eq $r && " $pre"  =~ m/[[:punct:][:space:]]$/ 
116                              && "$post " =~ m/^[[:punct:][:space:]]/) {
117                         $out .= $pre;
118                         $out .= ($l eq "*" ? '\fB' : '\fI') . $mid . '\fP';
119                         ($in = $post) =~ s/^\s+/ /;
120                 } else {
121                         $out .= $pre . $l;
122                         $in = $mid . $r . $post;
123                 }
124         } 
125         return $out . $in;
126 }
127 sub get_options {
128         my @opts = ();
129         my $indent = ($line =~ /(\s*)\S/) ? $1 : "";
130         while(nextline && match($line, "", $indent)) {
131                 push @opts, $match;
132         }
133         return @opts;
134 }
135
136 # main code
137 nextline;
138 if ($line and match($line, "address_family")) {
139         get_address_family $match;
140 } else {
141         die "address_family must be listed first\n";
142 }
143 if ($line and match($line, "architecture")) {
144         get_architecture $match;
145 }
146 while ($line and match($line, "method")) {
147         get_method $match;
148 }