5 # Usage: fixman [-f] postconf.proto filename.c >filename.c.new
7 # fixman - fix parameter text in embedded man pages
11 # - Read definitions fron postconf.proto like file
13 # - Read source file with embedded manual page
15 # - Write to stdout the updated source file.
24 #push @ARGV, "/dev/null"; # XXX
30 die "Usage: $0 [-fv] protofile [sourcefile...]
31 -f: include full parameter description instead of one-line summary
33 unless $protofile = shift(@ARGV);
35 # Save one definition.
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;
45 printf "saving entry %s %.20s..\n", $name, $text;
47 } elsif ($category eq "CLASS") {
48 $class_text{$name} = $text;
50 printf "saving class %s %.20s..\n", $name, $text;
53 die "Unknown category: $category. Need PARAM or CLASS.\n";
57 # Emit one parameter name and text
62 if ($block = $param_text{$name}) {
63 print "$delim .IP \"\\fB$name ($defval_text{$name})\\fR\"\n";
65 $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;
95 # Peep-hole optimizer.
97 $block =~ s/\s+\n/\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;
116 print "$delim .IP
\"\\fB
$name ($defval)\\fR
\"\n";
122 # Read the whole file even if we want to print only one parameter.
124 open(POSTCONF, $protofile) || die " cannot
open $protofile: $!\n";
129 next unless ($name || /\S/);
131 if (/^%(PARAM|CLASS)/) {
133 # Save the accumulated text.
135 if ($name && $text) {
139 # Reset the parameter name and accumulated text.
144 # Accumulate the parameter name and default value.
148 } while(($_ = <POSTCONF>) && /\S/);
149 ($junk, $name, $defval) = split(/\s+/, $text, 3);
151 $defval =~ s/\s+/ /g;
153 $defval =~ s/</</g;
154 $defval =~ s/>/>/g;
160 # Accumulate the text in the class or parameter definition.
166 # Save the last definition.
168 if ($name && $text) {
172 # Process source file with embedded text. For now, hard-coded for C & sh.
176 if (/^(\/\*|#)\+\+/) {
184 emit_text($1) if ($name ne "");
195 if (/(\/\*|#) +CONFIGURATION +PARAM/) {
199 # Delete text after nested itemized list.
200 if ($incomment == 2 && /^(\/\*|#) +\.IP ""/) {
203 last if /^(\/\*|#) +([A-Z][A-Z][A-Z]+|\.[A-Z][A-Z])/;
208 # Delete nested itemized list.
209 if ($incomment == 2 && /^(\/\*|#) +\.RS/) {
214 $rsnest++ if /^(\/\*|#) +\.RS/;
215 $rsnest-- if /(\/\*|#) +\.RE/;
216 last if $rsnest == 0;
221 if ($incomment == 2 && /^(\/\*|#) +\.IP +"?
\\fB
([a
-zA
-Z0
-9_
]+)( +\
((.*)\
))?
/) {
222 emit_text
($1) if ($name ne "");
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)/;
245 die "Unterminated comment\n" if $incomment;