Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / ibm-public / postfix / dist / mantools / fixman
blob635729e64efab91c6160050ff3f69b2e89b57ba4
1 #!/usr/bin/perl
3 use Getopt::Std;
5 # Usage: fixman [-f] postconf.proto filename.c >filename.c.new
7 # fixman - fix parameter text in embedded man pages
9 # Basic operation:
11 # - Read definitions fron postconf.proto like file
13 # - Read source file with embedded manual page
15 # - Write to stdout the updated source file.
18 #use Getopt::Std;
20 #$opt_h = undef;
21 #$opt_v = undef;
22 #getopts("hv");
24 #push @ARGV, "/dev/null"; # XXX
26 $opt_f = undef;
27 $opt_v = undef;
28 getopts("fv");
30 die "Usage: $0 [-fv] protofile [sourcefile...]
31 -f: include full parameter description instead of one-line summary
32 -v: verbose mode\n"
33 unless $protofile = shift(@ARGV);
35 # Save one definition.
37 sub save_text
39 if ($category eq "PARAM") {
40 $text =~ s/\.\s.*/.\n/s unless $opt_f;
41 $param_text{$name} = $text;
42 $defval = "empty" unless $defval ne "";
43 $defval_text{$name} = $defval;
44 if ($opt_v) {
45 printf "saving entry %s %.20s..\n", $name, $text;
47 } elsif ($category eq "CLASS") {
48 $class_text{$name} = $text;
49 if ($opt_v) {
50 printf "saving class %s %.20s..\n", $name, $text;
52 } else {
53 die "Unknown category: $category. Need PARAM or CLASS.\n";
57 # Emit one parameter name and text
59 sub emit_text
61 my ($delim) = @_;
62 if ($block = $param_text{$name}) {
63 print "$delim .IP \"\\fB$name ($defval_text{$name})\\fR\"\n";
64 $wantpp = 0;
65 $block =~ s/<a [^>]*>//g;
66 $block =~ s/<\/a>//g;
67 $block =~ s/<b>/\\fB/g;
68 $block =~ s/<i>/\\fI/g;
69 $block =~ s/<\/b>/\\fR/g;
70 $block =~ s/<\/i>/\\fR/g;
71 $block =~ s/\n(<p(re)?>)/\n.sp\n\1/g ; # if ($wantpp);
72 $block =~ s/^(<p(re)?>)/.sp\n\1/ ; # if ($wantpp);
73 $block =~ s/<p> */\n/g;
74 $block =~ s/<\/p>/\n/g;
75 $block =~ s/<pre>/\n.nf\n.na\n.ft C\n/g;
76 $block =~ s/<\/pre>/\n.fi\n.ad\n.ft R\n/g;
77 $block =~ s/<dl[^>]*>/\n.RS\n/g;
78 $block =~ s/<ul>/\n.RS\n/g;
79 #$block =~ s/<\/dl>/\n.PP\n/g;
80 #$block =~ s/<\/ul>/\n.PP\n/g;
81 $block =~ s/<\/dl>/\n.RE\n.IP ""\n/g;
82 $block =~ s/<\/ul>/\n.RE\n.IP ""\n/g;
83 $block =~ s/<dd>/\n/g;
84 $block =~ s/<\/dd>/\n/g;
85 $block =~ s/<li>\s*/\n.IP \\(bu\n/g;
86 $block =~ s/<dt>\s*/\n.IP "/g;
87 $block =~ s/\s*<\/dt>/"/g;
88 $block =~ s/<blockquote>/\n.na\n.nf\n.in +4\n/g;
89 $block =~ s/<\/blockquote>/\n.in -4\n.fi\n.ad\n/g;
90 $block =~ s/\n<br>/\n.br\n/g;
91 $block =~ s/<br>\s*/\n.br\n/g;
92 $block =~ s/&lt;/</g;
93 $block =~ s/&gt;/>/g;
95 # Peep-hole optimizer.
96 $block =~ s/^\s+//g;
97 $block =~ s/\s+\n/\n/g;
98 $block =~ s/^\n//g;
99 $block =~ s/\.IP ""\n(\.sp\n)+/.IP ""\n/g;
100 $block =~ s/\.IP ""\n(\.[A-Z][A-Z])/\1/g;
101 $block =~ s/(.IP ""\n)+$//;
102 $block =~ s/^(\.(PP|sp)\n)+//;
103 #$wantpp = !($block =~ /^\.(SH|IP)/);
105 # Boldify man page references.
106 $block =~ s/([_a-zA-Z0-9-]+)(\([0-9]\))/\\fB\1\\fR\2/g;
108 # Encapsulate as C code comment.
109 $block =~ s/^([^.])/$delim\t\1/;
110 $block =~ s/^\./$delim ./;
111 $block =~ s/\n([^.])/\n$delim\t\1/g;
112 $block =~ s/\n\./\n$delim ./g;
114 print $block;
115 } else {
116 print "$delim .IP \"\\fB$name ($defval)\\fR\"\n";
117 print $text;
119 $name = "";
122 # Read the whole file even if we want to print only one parameter.
124 open(POSTCONF, $protofile) || die " cannot open $protofile: $!\n";
126 while(<POSTCONF>) {
128 next if /^#/;
129 next unless ($name || /\S/);
131 if (/^%(PARAM|CLASS)/) {
133 # Save the accumulated text.
135 if ($name && $text) {
136 save_text();
139 # Reset the parameter name and accumulated text.
141 $name = $text = "";
142 $category = $1;
144 # Accumulate the parameter name and default value.
146 do {
147 $text .= $_;
148 } while(($_ = <POSTCONF>) && /\S/);
149 ($junk, $name, $defval) = split(/\s+/, $text, 3);
151 $defval =~ s/\s+/ /g;
152 $defval =~ s/\s+$//;
153 $defval =~ s/&lt;/</g;
154 $defval =~ s/&gt;/>/g;
155 $defval =~ s/"/'/g;
156 $text = "";
157 next;
160 # Accumulate the text in the class or parameter definition.
162 $text .= $_;
166 # Save the last definition.
168 if ($name && $text) {
169 save_text();
172 # Process source file with embedded text. For now, hard-coded for C & sh.
174 while(<>) {
176 if (/^(\/\*|#)\+\+/) {
177 $incomment = 1;
178 $name = "";
179 print;
180 next;
183 if (/^(\/\*|#)--/) {
184 emit_text($1) if ($name ne "");
185 $incomment = 0;
186 print;
187 next;
190 if (!$incomment) {
191 print;
192 next;
195 if (/(\/\*|#) +CONFIGURATION +PARAM/) {
196 $incomment = 2;
199 # Delete text after nested itemized list.
200 if ($incomment == 2 && /^(\/\*|#) +\.IP ""/) {
201 $text .= $_;
202 while (<>) {
203 last if /^(\/\*|#) +([A-Z][A-Z][A-Z]+|\.[A-Z][A-Z])/;
204 $text .= $_;
208 # Delete nested itemized list.
209 if ($incomment == 2 && /^(\/\*|#) +\.RS/) {
210 $text .= $_;
211 $rsnest++;
212 while (<>) {
213 $text .= $_;
214 $rsnest++ if /^(\/\*|#) +\.RS/;
215 $rsnest-- if /(\/\*|#) +\.RE/;
216 last if $rsnest == 0;
218 next;
221 if ($incomment == 2 && /^(\/\*|#) +\.IP +"?\\fB([a-zA-Z0-9_]+)( +\((.*)\))?/) {
222 emit_text($1) if ($name ne "");
223 $name = $2;
224 $defval = $4;
225 $text = "";
226 next;
229 if ($incomment == 2 && /^(\/\*|#) +([A-Z][A-Z][A-Z]+|\.[A-Z][A-Z])/) {
230 emit_text($1) if ($name ne "");
231 $incomment = 0 if /^(\/\*|#) +(SEE +ALSO|README +FILES|LICENSE|AUTHOR)/;
232 print;
233 next;
236 if ($name ne "") {
237 $text .= $_;
238 next;
241 print;
242 next;
245 die "Unterminated comment\n" if $incomment;