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