summaryrefslogtreecommitdiffstats
path: root/mantools/fixman
blob: 6c2c6ea912f7328055e31cec0364e039da14a5b3 (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
#!/usr/bin/perl

use Getopt::Std;

# Usage: fixman [-f] postconf.proto filename.c >filename.c.new

# fixman - fix parameter text in embedded man pages

# Basic operation:
#
# - Read definitions fron postconf.proto like file
#
# - Read source file with embedded manual page
#
# - Write to stdout the updated source file.
#

#use Getopt::Std;

#$opt_h = undef;
#$opt_v = undef;
#getopts("hv");

#push @ARGV, "/dev/null"; # XXX

$opt_f = undef;
$opt_v = undef;
getopts("fv");

die "Usage: $0 [-fv] protofile [sourcefile...]
-f: include full parameter description instead of one-line summary
-v: verbose mode\n"
        unless $protofile = shift(@ARGV);

# Save one definition.

sub save_text 
{
    if ($category eq "PARAM") {
	$text =~ s/\.\s.*/.\n/s unless $opt_f;
        $param_text{$name} = $text;
	$defval = "empty" unless $defval ne "";
	$defval_text{$name} = $defval;
        if ($opt_v) {
            printf "saving entry %s %.20s..\n", $name, $text;
        } 
    } elsif ($category eq "CLASS") {
        $class_text{$name} = $text;
        if ($opt_v) {
            printf "saving class %s %.20s..\n", $name, $text;
        } 
    } else {
        die "Unknown category: $category. Need PARAM or CLASS.\n";
    }
}

# Emit one parameter name and text

sub emit_text
{
    my ($delim) = @_;
    if ($block = $param_text{$name}) {
	print "$delim .IP \"\\fB$name ($defval_text{$name})\\fR\"\n";
	$wantpp = 0;
	$block =~ s/<a [^>]*>//g;
	$block =~ s/<\/a>//g;
	$block =~ s/<b>/\\fB/g;
	$block =~ s/<i>/\\fI/g;
	$block =~ s/<\/b>/\\fR/g;
	$block =~ s/<\/i>/\\fR/g;
	$block =~ s/\n(<p(re)?>)/\n.sp\n\1/g ; # if ($wantpp);
	$block =~ s/^(<p(re)?>)/.sp\n\1/ ; # if ($wantpp);
	$block =~ s/<p> */\n/g;
	$block =~ s/<\/p>/\n/g;
	$block =~ s/<pre>/\n.nf\n.na\n.ft C\n/g;
	$block =~ s/<\/pre>/\n.fi\n.ad\n.ft R\n/g;
	$block =~ s/<dl[^>]*>/\n.RS\n/g;
	$block =~ s/<ul>/\n.RS\n/g;
	#$block =~ s/<\/dl>/\n.PP\n/g;
	#$block =~ s/<\/ul>/\n.PP\n/g;
	$block =~ s/<\/dl>/\n.RE\n.IP ""\n/g;
	$block =~ s/<\/ul>/\n.RE\n.IP ""\n/g;
	$block =~ s/<dd>/\n/g;
	$block =~ s/<\/dd>/\n/g;
	$block =~ s/<li>\s*/\n.IP \\(bu\n/g;
	$block =~ s/<dt>\s*/\n.IP "/g;
	$block =~ s/\s*<\/dt>/"/g;
	$block =~ s/<blockquote>/\n.na\n.nf\n.in +4\n/g;
	$block =~ s/<\/blockquote>/\n.in -4\n.fi\n.ad\n/g;
	$block =~ s/\n<br>/\n.br\n/g;
	$block =~ s/<br>\s*/\n.br\n/g;
	$block =~ s/&le;/<=/g;
	$block =~ s/&ge;/>=/g;
	$block =~ s/&lt;/</g;
	$block =~ s/&gt;/>/g;

	# Peep-hole optimizer.
	$block =~ s/^\s+//g;
	$block =~ s/\s+\n/\n/g;
	$block =~ s/^\n//g;
	$block =~ s/\.IP ""\n(\.sp\n)+/.IP ""\n/g;
	$block =~ s/\.IP ""\n(\.[A-Z][A-Z])/\1/g;
	$block =~ s/(.IP ""\n)+$//;
	$block =~ s/^(\.(PP|sp)\n)+//;
	#$wantpp = !($block =~ /^\.(SH|IP)/);

	# Boldify man page references.
	$block =~ s/([_a-zA-Z0-9-]+)(\([0-9]\))/\\fB\1\\fR\2/g;

	# Encapsulate as C code comment.
	$block =~ s/^([^.])/$delim\t\1/;
	$block =~ s/^\./$delim ./;
	$block =~ s/\n([^.])/\n$delim\t\1/g;
	$block =~ s/\n\./\n$delim ./g;

	print $block;
    } else {
	print "$delim .IP \"\\fB$name ($defval)\\fR\"\n";
	print $text;
    }
    $name = "";
}

# Read the whole file even if we want to print only one parameter.

open(POSTCONF, $protofile) || die " cannot open $protofile: $!\n";

while(<POSTCONF>) {

    next if /^#/;
    next unless ($name || /\S/);

    if (/^%(PARAM|CLASS)/) {

        # Save the accumulated text.

        if ($name && $text) {
            save_text();
        }

        # Reset the parameter name and accumulated text.

        $name = $text = "";
        $category = $1;

        # Accumulate the parameter name and default value.

        do {
            $text .= $_;
        } while(($_ = <POSTCONF>) && /\S/);
        ($junk, $name, $defval) = split(/\s+/, $text, 3);

	$defval =~ s/\s+/ /g;
	$defval =~ s/\s+$//;
	$defval =~ s/&le;/<=/g;
	$defval =~ s/&ge;/>=/g;
	$defval =~ s/&lt;/</g;
	$defval =~ s/&gt;/>/g;
	$defval =~ s/"/'/g;
	$text = "";
	next;
    } 

    # Accumulate the text in the class or parameter definition.

    $text .= $_;

}

# Save the last definition.

if ($name && $text) {
    save_text();
}

# Process source file with embedded text. For now, hard-coded for C & sh.

while(<>) {

    if (/^(\/\*|#)\+\+/) {
	$incomment = 1;
	$name = "";
	print;
	next;
    }

    if (/^(\/\*|#)--/) {
	emit_text($1) if ($name ne "");
	$incomment = 0;
	print;
	next;
    }

    if (!$incomment) {
	print;
	next;
    }

    if (/(\/\*|#) +CONFIGURATION +PARAM/) {
	$incomment = 2;
    }

    # Delete text after nested itemized list.
    if ($incomment == 2 && /^(\/\*|#) +\.IP ""/) {
	$text .= $_;
	while (<>) {
	    last if /^(\/\*|#) +([A-Z][A-Z][A-Z]+|\.[A-Z][A-Z])/;
	    $text .= $_;
	}
    }

    # Delete nested itemized list.
    if ($incomment == 2 && /^(\/\*|#) +\.RS/) {
	$text .= $_;
	$rsnest++;
	while (<>) {
	    $text .= $_;
	    $rsnest++ if /^(\/\*|#) +\.RS/;
	    $rsnest-- if /(\/\*|#) +\.RE/;
	    last if $rsnest == 0;
	}
	next;
    }

    if ($incomment == 2 && /^(\/\*|#) +\.IP +"?\\fB([a-zA-Z0-9_]+)( +\((.*)\))?/) {
	emit_text($1) if ($name ne "");
	$name = $2;
	$defval = $4;
	$text = "";
	next;
    }

    if ($incomment == 2 && /^(\/\*|#) +\.IP +"?\\fI([a-zA-Z0-9_]+)\\fB([a-zA-Z0-9_]+)( +\((.*)\))?/) {
	emit_text($1) if ($name ne "");
	$name = "$2$3";
	$defval = $4;
	$text = "";
	next;
    }

    if ($incomment == 2 && /^(\/\*|#) +([A-Z][A-Z][A-Z]+|\.[A-Z][A-Z])/) {
	emit_text($1) if ($name ne "");
	$incomment = 0 if /^(\/\*|#) +(SEE +ALSO|README +FILES|LICENSE|AUTHOR)/;
	print;
	next;
    }

    if ($name ne "") {
	$text .= $_;
	next;
    }

    print;
    next;
}

die "Unterminated comment\n" if $incomment;