]>
Commit | Line | Data |
---|---|---|
6251188c ZW |
1 | #! /usr/bin/perl -w |
2 | ||
6e26f4aa JM |
3 | # Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc. |
4 | ||
5 | # This file is part of GNU CC. | |
6 | ||
7 | # GNU CC is free software; you can redistribute it and/or modify | |
8 | # it under the terms of the GNU General Public License as published by | |
9 | # the Free Software Foundation; either version 2, or (at your option) | |
10 | # any later version. | |
11 | ||
12 | # GNU CC is distributed in the hope that it will be useful, | |
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | # GNU General Public License for more details. | |
16 | ||
17 | # You should have received a copy of the GNU General Public License | |
18 | # along with GNU CC; see the file COPYING. If not, write to | |
19 | # the Free Software Foundation, 59 Temple Place - Suite 330, | |
20 | # Boston MA 02111-1307, USA. | |
21 | ||
6251188c ZW |
22 | # This does trivial (and I mean _trivial_) conversion of Texinfo |
23 | # markup to Perl POD format. It's intended to be used to extract | |
24 | # something suitable for a manpage from a Texinfo document. | |
25 | ||
6251188c | 26 | $output = 0; |
3d6f4d76 | 27 | $skipping = 0; |
6251188c ZW |
28 | %sects = (); |
29 | $section = ""; | |
30 | @icstack = (); | |
31 | @endwstack = (); | |
3d6f4d76 | 32 | @skstack = (); |
6251188c | 33 | $shift = ""; |
3d6f4d76 | 34 | %defs = (); |
b75d998f | 35 | $fnno = 1; |
6251188c | 36 | |
b75d998f | 37 | while ($_ = shift) { |
3d6f4d76 ZW |
38 | if (/^-D(.*)$/) { |
39 | if ($1 ne "") { | |
40 | $flag = $1; | |
41 | } else { | |
42 | $flag = shift; | |
43 | } | |
44 | die "no flag specified for -D\n" | |
45 | unless $flag ne ""; | |
46 | die "flags may only contain letters, digits, hyphens, and underscores\n" | |
47 | unless $flag =~ /^[a-zA-Z0-9_-]+$/; | |
48 | $defs{$flag} = ""; | |
49 | } elsif (/^-/) { | |
50 | usage(); | |
51 | } else { | |
52 | $in = $_, next unless defined $in; | |
53 | $out = $_, next unless defined $out; | |
54 | usage(); | |
55 | } | |
56 | } | |
57 | ||
58 | if (defined $in) { | |
59 | open(STDIN, $in) or die "opening \"$in\": $!\n"; | |
60 | } | |
61 | if (defined $out) { | |
62 | open(STDOUT, ">$out") or die "opening \"$out\": $!\n"; | |
63 | } | |
64 | ||
65 | while(<STDIN>) | |
6251188c | 66 | { |
b75d998f ZW |
67 | # Certain commands are discarded without further processing. |
68 | /^\@(?: | |
69 | [a-z]+index # @*index: useful only in complete manual | |
70 | |need # @need: useful only in printed manual | |
71 | |(?:end\s+)?group # @group .. @end group: ditto | |
72 | |page # @page: ditto | |
73 | |node # @node: useful only in .info file | |
74 | )\b/x and next; | |
75 | ||
6251188c | 76 | chomp; |
b75d998f ZW |
77 | |
78 | # Look for filename and title markers. | |
79 | /^\@setfilename\s+([^.]+)/ and $fn = $1, next; | |
80 | /^\@settitle\s+([^.]+)/ and $tl = $1, next; | |
81 | ||
82 | # Look for blocks surrounded by @c man begin SECTION ... @c man end. | |
83 | # This really oughta be @ifman ... @end ifman and the like, but such | |
84 | # would require rev'ing all other Texinfo translators. | |
6251188c ZW |
85 | /^\@c man begin ([A-Z]+)/ and $sect = $1, $output = 1, next; |
86 | /^\@c man end/ and do { | |
f4e8dec6 ZW |
87 | $sects{$sect} = "" unless exists $sects{$sect}; |
88 | $sects{$sect} .= postprocess($section); | |
6251188c ZW |
89 | $section = ""; |
90 | $output = 0; | |
91 | next; | |
92 | }; | |
b75d998f | 93 | next unless $output; |
3d6f4d76 | 94 | |
b75d998f ZW |
95 | # Discard comments. (Can't do it above, because then we'd never see |
96 | # @c man lines.) | |
97 | /^\@c\b/ and next; | |
6251188c | 98 | |
b75d998f ZW |
99 | # End-block handler goes up here because it needs to operate even |
100 | # if we are skipping. | |
101 | /^\@end\s+([a-z]+)/ and do { | |
f4e8dec6 ZW |
102 | # Ignore @end foo, where foo is not an operation which may |
103 | # cause us to skip, if we are presently skipping. | |
104 | my $ended = $1; | |
105 | next if $skipping && $ended !~ /^(?:ifset|ifclear|ignore|menu)$/; | |
106 | ||
107 | die "\@end $ended without \@$ended at line $.\n" unless defined $endw; | |
108 | die "\@$endw ended by \@end $ended at line $.\n" unless $ended eq $endw; | |
6251188c | 109 | |
b75d998f | 110 | $endw = pop @endwstack; |
6251188c | 111 | |
f4e8dec6 | 112 | if ($ended =~ /^(?:ifset|ifclear|ignore|menu)$/) { |
b75d998f ZW |
113 | $skipping = pop @skstack; |
114 | next; | |
f4e8dec6 ZW |
115 | } elsif ($ended =~ /^(?:example|smallexample)$/) { |
116 | $shift = ""; | |
117 | $_ = ""; # need a paragraph break | |
118 | } elsif ($ended =~ /^(?:itemize|enumerate|table)$/) { | |
b75d998f ZW |
119 | $_ = "\n=back\n"; |
120 | $ic = pop @icstack; | |
f4e8dec6 ZW |
121 | } else { |
122 | die "unknown command \@end $ended at line $.\n"; | |
3d6f4d76 ZW |
123 | } |
124 | }; | |
f4e8dec6 ZW |
125 | |
126 | # We must handle commands which can cause skipping even while we | |
127 | # are skipping, otherwise we will not process nested conditionals | |
128 | # correctly. | |
129 | /^\@ifset\s+([a-zA-Z0-9_-]+)/ and do { | |
130 | push @endwstack, $endw; | |
131 | push @skstack, $skipping; | |
132 | $endw = "ifset"; | |
133 | $skipping = 1 unless exists $defs{$1}; | |
134 | next; | |
135 | }; | |
136 | ||
137 | /^\@ifclear\s+([a-zA-Z0-9_-]+)/ and do { | |
138 | push @endwstack, $endw; | |
139 | push @skstack, $skipping; | |
140 | $endw = "ifclear"; | |
141 | $skipping = 1 if exists $defs{$1}; | |
142 | next; | |
143 | }; | |
144 | ||
145 | /^\@(ignore|menu)\b/ and do { | |
146 | push @endwstack, $endw; | |
147 | push @skstack, $skipping; | |
148 | $endw = $1; | |
149 | $skipping = 1; | |
150 | next; | |
151 | }; | |
152 | ||
3d6f4d76 ZW |
153 | next if $skipping; |
154 | ||
b75d998f ZW |
155 | # Character entities. First the ones that can be replaced by raw text |
156 | # or discarded outright: | |
157 | s/\@copyright\{\}/(c)/g; | |
158 | s/\@dots\{\}/.../g; | |
159 | s/\@enddots\{\}/..../g; | |
160 | s/\@([.!? ])/$1/g; | |
161 | s/\@[:-]//g; | |
162 | s/\@bullet(?:\{\})?/*/g; | |
163 | s/\@TeX\{\}/TeX/g; | |
164 | s/\@pounds\{\}/\#/g; | |
165 | s/\@minus(?:\{\})?/-/g; | |
445c435a | 166 | s/\\,/,/g; |
6251188c | 167 | |
b75d998f ZW |
168 | # Now the ones that have to be replaced by special escapes |
169 | # (which will be turned back into text by unmunge()) | |
170 | s/&/&/g; | |
171 | s/\@\{/{/g; | |
172 | s/\@\}/}/g; | |
173 | s/\@\@/&at;/g; | |
174 | # POD doesn't interpret E<> inside a verbatim block. | |
175 | if ($shift eq "") { | |
176 | s/</</g; | |
177 | s/>/>/g; | |
178 | } else { | |
179 | s/</</g; | |
180 | s/>/>/g; | |
181 | } | |
182 | ||
183 | # Single line command handlers. | |
3d6f4d76 ZW |
184 | /^\@set\s+([a-zA-Z0-9_-]+)\s*(.*)$/ and $defs{$1} = $2, next; |
185 | /^\@clear\s+([a-zA-Z0-9_-]+)/ and delete $defs{$1}, next; | |
186 | ||
b75d998f ZW |
187 | /^\@section\s+(.+)$/ and $_ = "\n=head2 $1\n"; |
188 | /^\@subsection\s+(.+)$/ and $_ = "\n=head3 $1\n"; | |
189 | ||
190 | # Block command handlers: | |
b75d998f | 191 | /^\@itemize\s+(\@[a-z]+|\*|-)/ and do { |
6251188c ZW |
192 | push @endwstack, $endw; |
193 | push @icstack, $ic; | |
194 | $ic = $1; | |
6251188c ZW |
195 | $_ = "\n=over 4\n"; |
196 | $endw = "itemize"; | |
197 | }; | |
198 | ||
b75d998f | 199 | /^\@enumerate(?:\s+([A-Z0-9]+))?/ and do { |
6251188c ZW |
200 | push @endwstack, $endw; |
201 | push @icstack, $ic; | |
b75d998f ZW |
202 | if (defined $1) { |
203 | $ic = $1 . "."; | |
204 | } else { | |
205 | $ic = "1."; | |
206 | } | |
6251188c ZW |
207 | $_ = "\n=over 4\n"; |
208 | $endw = "enumerate"; | |
209 | }; | |
210 | ||
211 | /^\@table\s+(\@[a-z]+)/ and do { | |
212 | push @endwstack, $endw; | |
213 | push @icstack, $ic; | |
214 | $ic = $1; | |
2642624b | 215 | $ic =~ s/\@(?:samp|strong|key|gcctabopt|env)/B/; |
6251188c | 216 | $ic =~ s/\@(?:code|kbd)/C/; |
b75d998f | 217 | $ic =~ s/\@(?:dfn|var|emph|cite|i)/I/; |
6251188c ZW |
218 | $ic =~ s/\@(?:file)/F/; |
219 | $_ = "\n=over 4\n"; | |
220 | $endw = "table"; | |
221 | }; | |
222 | ||
223 | /^\@((?:small)?example)/ and do { | |
224 | push @endwstack, $endw; | |
225 | $endw = $1; | |
226 | $shift = "\t"; | |
f4e8dec6 | 227 | $_ = ""; # need a paragraph break |
6251188c ZW |
228 | }; |
229 | ||
6251188c | 230 | /^\@itemx?\s*(.+)?$/ and do { |
b75d998f ZW |
231 | if (defined $1) { |
232 | # Entity escapes prevent munging by the <> processing below. | |
233 | $_ = "\n=item $ic\<$1\>\n"; | |
234 | } else { | |
235 | $_ = "\n=item $ic\n"; | |
6251188c ZW |
236 | $ic =~ y/A-Ya-y1-8/B-Zb-z2-9/; |
237 | } | |
238 | }; | |
3d6f4d76 | 239 | |
6251188c ZW |
240 | $section .= $shift.$_."\n"; |
241 | } | |
242 | ||
b75d998f ZW |
243 | die "No filename or title\n" unless defined $fn && defined $tl; |
244 | ||
6251188c | 245 | $sects{NAME} = "$fn \- $tl\n"; |
b75d998f | 246 | $sects{FOOTNOTES} .= "=back\n" if exists $sects{FOOTNOTES}; |
6251188c ZW |
247 | |
248 | for $sect (qw(NAME SYNOPSIS DESCRIPTION OPTIONS ENVIRONMENT FILES | |
b75d998f ZW |
249 | BUGS NOTES FOOTNOTES SEEALSO AUTHOR COPYRIGHT)) { |
250 | if(exists $sects{$sect}) { | |
6251188c ZW |
251 | $head = $sect; |
252 | $head =~ s/SEEALSO/SEE ALSO/; | |
253 | print "=head1 $head\n\n"; | |
b75d998f | 254 | print scalar unmunge ($sects{$sect}); |
6251188c ZW |
255 | print "\n"; |
256 | } | |
257 | } | |
3d6f4d76 ZW |
258 | |
259 | sub usage | |
260 | { | |
261 | die "usage: $0 [-D toggle...] [infile [outfile]]\n"; | |
262 | } | |
b75d998f ZW |
263 | |
264 | sub postprocess | |
265 | { | |
266 | local $_ = $_[0]; | |
267 | ||
268 | # @value{foo} is replaced by whatever 'foo' is defined as. | |
269 | s/\@value\{([a-zA-Z0-9_-]+)\}/$defs{$1}/g; | |
270 | ||
271 | # Formatting commands. | |
4bc1997b JM |
272 | # Temporary escape for @r. |
273 | s/\@r\{([^\}]*)\}/R<$1>/g; | |
b75d998f ZW |
274 | s/\@(?:dfn|var|emph|cite|i)\{([^\}]*)\}/I<$1>/g; |
275 | s/\@(?:code|kbd)\{([^\}]*)\}/C<$1>/g; | |
4bc1997b | 276 | s/\@(?:gccoptlist|samp|strong|key|option|env|command|b)\{([^\}]*)\}/B<$1>/g; |
b75d998f ZW |
277 | s/\@sc\{([^\}]*)\}/\U$1/g; |
278 | s/\@file\{([^\}]*)\}/F<$1>/g; | |
279 | s/\@w\{([^\}]*)\}/S<$1>/g; | |
280 | s/\@(?:dmn|math)\{([^\}]*)\}/$1/g; | |
281 | ||
282 | # Cross references are thrown away, as are @noindent and @refill. | |
283 | # (@noindent is impossible in .pod, and @refill is unnecessary.) | |
284 | # @* is also impossible in .pod; we discard it and any newline that | |
4bc1997b | 285 | # follows it. Similarly, our macro @gol must be discarded. |
b75d998f | 286 | |
4bc1997b | 287 | s/\(?\@xref\{(?:[^\}]*)\}(?:[^.<]|(?:<[^<>]*>))*\.\)?//g; |
b75d998f ZW |
288 | s/\s+\(\@pxref\{(?:[^\}]*)\}\)//g; |
289 | s/;\s+\@pxref\{(?:[^\}]*)\}//g; | |
290 | s/\@noindent\s*//g; | |
291 | s/\@refill//g; | |
4bc1997b | 292 | s/\@gol//g; |
b75d998f ZW |
293 | s/\@\*\s*\n?//g; |
294 | ||
295 | # @uref can take one, two, or three arguments, with different | |
296 | # semantics each time. @url and @email are just like @uref with | |
297 | # one argument, for our purposes. | |
2642624b | 298 | s/\@(?:uref|url|email)\{([^\},]*)\}/<B<$1>>/g; |
b75d998f ZW |
299 | s/\@uref\{([^\},]*),([^\},]*)\}/$2 (C<$1>)/g; |
300 | s/\@uref\{([^\},]*),([^\},]*),([^\},]*)\}/$3/g; | |
301 | ||
302 | # Turn B<blah I<blah> blah> into B<blah> I<blah> B<blah> to | |
aee96fe9 JM |
303 | # match Texinfo semantics of @emph inside @samp. Also handle @r |
304 | # inside bold. | |
b75d998f ZW |
305 | s/</</g; |
306 | s/>/>/g; | |
aee96fe9 | 307 | 1 while s/B<((?:[^<>]|I<[^<>]*>)*)R<([^>]*)>/B<$1>${2}B</g; |
b75d998f ZW |
308 | 1 while (s/B<([^<>]*)I<([^>]+)>/B<$1>I<$2>B</g); |
309 | 1 while (s/I<([^<>]*)B<([^>]+)>/I<$1>B<$2>I</g); | |
310 | s/[BI]<>//g; | |
311 | s/([BI])<(\s+)([^>]+)>/$2$1<$3>/g; | |
312 | s/([BI])<([^>]+?)(\s+)>/$1<$2>$3/g; | |
313 | ||
314 | # Extract footnotes. This has to be done after all other | |
315 | # processing because otherwise the regexp will choke on formatting | |
316 | # inside @footnote. | |
317 | while (/\@footnote/g) { | |
318 | s/\@footnote\{([^\}]+)\}/[$fnno]/; | |
319 | add_footnote($1, $fnno); | |
320 | $fnno++; | |
321 | } | |
322 | ||
323 | return $_; | |
324 | } | |
325 | ||
326 | sub unmunge | |
327 | { | |
328 | # Replace escaped symbols with their equivalents. | |
329 | local $_ = $_[0]; | |
330 | ||
331 | s/</E<lt>/g; | |
332 | s/>/E<gt>/g; | |
333 | s/{/\{/g; | |
334 | s/}/\}/g; | |
335 | s/&at;/\@/g; | |
336 | s/&/&/g; | |
337 | return $_; | |
338 | } | |
339 | ||
340 | sub add_footnote | |
341 | { | |
342 | unless (exists $sects{FOOTNOTES}) { | |
343 | $sects{FOOTNOTES} = "\n=over 4\n\n"; | |
344 | } | |
345 | ||
346 | $sects{FOOTNOTES} .= "=item $fnno.\n\n"; $fnno++; | |
347 | $sects{FOOTNOTES} .= $_[0]; | |
348 | $sects{FOOTNOTES} .= "\n\n"; | |
349 | } | |
350 |