This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
In autodoc.pl, remove the unused parameters and functionality from walk_table().
[perl5.git] / autodoc.pl
CommitLineData
94bdecf9
JH
1#!/usr/bin/perl -w
2
3require 5.003; # keep this compatible, an old perl is all we may have before
4 # we build the new one
5
36bb303b
NC
6BEGIN {
7 push @INC, 'lib';
9ad884cb 8 require 'regen_lib.pl';
69e39a9a 9}
a64c954a 10
56a0c332 11use strict;
a64c954a 12
94bdecf9 13#
346f75ff 14# See database of global and static function prototypes in embed.fnc
94bdecf9
JH
15# This is used to generate prototype headers under various configurations,
16# export symbols lists for different platforms, and macros to provide an
17# implicit interpreter context argument.
18#
19
20open IN, "embed.fnc" or die $!;
21
22# walk table providing an array of components in each line to
23# subroutine, printing the result
24sub walk_table (&@) {
25 my $function = shift;
94bdecf9
JH
26 seek IN, 0, 0; # so we may restart
27 while (<IN>) {
28 chomp;
29 next if /^:/;
78c9d763 30 while (s|\\\s*$||) {
94bdecf9
JH
31 $_ .= <IN>;
32 chomp;
33 }
23f1b5c3 34 s/\s+$//;
94bdecf9
JH
35 my @args;
36 if (/^\s*(#|$)/) {
37 @args = $_;
38 }
39 else {
40 @args = split /\s*\|\s*/, $_;
41 }
1b6737cc 42 s/\b(NN|NULLOK)\b\s+//g for @args;
848ad75c 43 $function->(@args);
36bb303b 44 }
94bdecf9
JH
45}
46
47my %apidocs;
48my %gutsdocs;
49my %docfuncs;
7eb550cf 50my %seenfuncs;
94bdecf9
JH
51
52my $curheader = "Unknown section";
53
54sub autodoc ($$) { # parse a file and extract documentation info
55 my($fh,$file) = @_;
56 my($in, $doc, $line);
57FUNC:
58 while (defined($in = <$fh>)) {
59 if ($in=~ /^=head1 (.*)/) {
60 $curheader = $1;
61 next FUNC;
62 }
63 $line++;
78c9d763 64 if ($in =~ /^=for\s+apidoc\s+(.*?)\s*\n/) {
94bdecf9
JH
65 my $proto = $1;
66 $proto = "||$proto" unless $proto =~ /\|/;
67 my($flags, $ret, $name, @args) = split /\|/, $proto;
68 my $docs = "";
69DOC:
70 while (defined($doc = <$fh>)) {
94bdecf9
JH
71 $line++;
72 last DOC if $doc =~ /^=\w+/;
73 if ($doc =~ m:^\*/$:) {
74 warn "=cut missing? $file:$line:$doc";;
75 last DOC;
76 }
77 $docs .= $doc;
78 }
79 $docs = "\n$docs" if $docs and $docs !~ /^\n/;
80 if ($flags =~ /m/) {
81 if ($flags =~ /A/) {
82 $apidocs{$curheader}{$name} = [$flags, $docs, $ret, $file, @args];
83 }
84 else {
85 $gutsdocs{$curheader}{$name} = [$flags, $docs, $ret, $file, @args];
86 }
87 }
88 else {
89 $docfuncs{$name} = [$flags, $docs, $ret, $file, $curheader, @args];
90 }
91 if (defined $doc) {
e509e693 92 if ($doc =~ /^=(?:for|head)/) {
94bdecf9
JH
93 $in = $doc;
94 redo FUNC;
95 }
96 } else {
97 warn "$file:$line:$in";
98 }
99 }
100 }
101}
102
103sub docout ($$$) { # output the docs for one function
104 my($fh, $name, $docref) = @_;
105 my($flags, $docs, $ret, $file, @args) = @$docref;
d8c40edc 106 $name =~ s/\s*$//;
94bdecf9
JH
107
108 $docs .= "NOTE: this function is experimental and may change or be
109removed without notice.\n\n" if $flags =~ /x/;
110 $docs .= "NOTE: the perl_ form of this function is deprecated.\n\n"
111 if $flags =~ /p/;
112
d8c40edc 113 print $fh "=item $name\nX<$name>\n$docs";
94bdecf9
JH
114
115 if ($flags =~ /U/) { # no usage
116 # nothing
117 } elsif ($flags =~ /s/) { # semicolon ("dTHR;")
118 print $fh "\t\t$name;\n\n";
119 } elsif ($flags =~ /n/) { # no args
120 print $fh "\t$ret\t$name\n\n";
121 } else { # full usage
122 print $fh "\t$ret\t$name";
123 print $fh "(" . join(", ", @args) . ")";
124 print $fh "\n\n";
125 }
126 print $fh "=for hackers\nFound in file $file\n\n";
127}
128
e0492643
NC
129sub readonly_header (*) {
130 my $fh = shift;
131 print $fh <<"_EOH_";
132-*- buffer-read-only: t -*-
133
134!!!!!!! DO NOT EDIT THIS FILE !!!!!!!
135This file is built by $0 extracting documentation from the C source
136files.
137
138_EOH_
139}
140
141sub readonly_footer (*) {
142 my $fh = shift;
143 print $fh <<'_EOF_';
144=cut
145
3f98fbb3 146 ex: set ro:
e0492643
NC
147_EOF_
148}
149
94bdecf9 150my $file;
69e39a9a
NC
151# glob() picks up docs from extra .c or .h files that may be in unclean
152# development trees.
153my $MANIFEST = do {
154 local ($/, *FH);
155 open FH, "MANIFEST" or die "Can't open MANIFEST: $!";
156 <FH>;
157};
158
159for $file (($MANIFEST =~ /^(\S+\.c)\t/gm), ($MANIFEST =~ /^(\S+\.h)\t/gm)) {
94bdecf9
JH
160 open F, "< $file" or die "Cannot open $file for docs: $!\n";
161 $curheader = "Functions in file $file\n";
162 autodoc(\*F,$file);
163 close F or die "Error closing $file: $!\n";
164}
165
36bb303b 166safer_unlink "pod/perlapi.pod";
08858ed2 167my $doc = safer_open("pod/perlapi.pod");
94bdecf9 168
7eb550cf 169walk_table { # load documented functions into appropriate hash
94bdecf9
JH
170 if (@_ > 1) {
171 my($flags, $retval, $func, @args) = @_;
172 return "" unless $flags =~ /d/;
173 $func =~ s/\t//g; $flags =~ s/p//; # clean up fields from embed.pl
174 $retval =~ s/\t//;
78c9d763 175 my $docref = delete $docfuncs{$func};
7eb550cf 176 $seenfuncs{$func} = 1;
78c9d763
DM
177 if ($docref and @$docref) {
178 if ($flags =~ /A/) {
179 $docref->[0].="x" if $flags =~ /M/;
7eb550cf
RGS
180 $apidocs{$docref->[4]}{$func} =
181 [$docref->[0] . 'A', $docref->[1], $retval, $docref->[3],
182 @args];
78c9d763 183 } else {
7eb550cf 184 $gutsdocs{$docref->[4]}{$func} =
78c9d763
DM
185 [$docref->[0], $docref->[1], $retval, $docref->[3], @args];
186 }
187 }
188 else {
7eb550cf 189 warn "no docs for $func\n" unless $seenfuncs{$func};
94bdecf9
JH
190 }
191 }
848ad75c 192};
94bdecf9
JH
193
194for (sort keys %docfuncs) {
195 # Have you used a full for apidoc or just a func name?
196 # Have you used Ap instead of Am in the for apidoc?
197 warn "Unable to place $_!\n";
198}
199
08858ed2 200readonly_header($doc);
e0492643 201
08858ed2 202print $doc <<'_EOB_';
94bdecf9
JH
203=head1 NAME
204
205perlapi - autogenerated documentation for the perl public API
206
207=head1 DESCRIPTION
d8c40edc 208X<Perl API> X<API> X<api>
94bdecf9
JH
209
210This file contains the documentation of the perl public API generated by
211embed.pl, specifically a listing of functions, macros, flags, and variables
212that may be used by extension writers. The interfaces of any functions that
213are not listed here are subject to change without notice. For this reason,
214blindly using functions listed in proto.h is to be avoided when writing
215extensions.
216
217Note that all Perl API global variables must be referenced with the C<PL_>
218prefix. Some macros are provided for compatibility with the older,
219unadorned names, but this support may be disabled in a future release.
220
2bbc8d55
SP
221Perl was originally written to handle US-ASCII only (that is characters
222whose ordinal numbers are in the range 0 - 127).
223And documentation and comments may still use the term ASCII, when
224sometimes in fact the entire range from 0 - 255 is meant.
225
226Note that Perl can be compiled and run under EBCDIC (See L<perlebcdic>)
227or ASCII. Most of the documentation (and even comments in the code)
228ignore the EBCDIC possibility.
229For almost all purposes the differences are transparent.
230As an example, under EBCDIC,
231instead of UTF-8, UTF-EBCDIC is used to encode Unicode strings, and so
232whenever this documentation refers to C<utf8>
233(and variants of that name, including in function names),
234it also (essentially transparently) means C<UTF-EBCDIC>.
235But the ordinals of characters differ between ASCII, EBCDIC, and
236the UTF- encodings, and a string encoded in UTF-EBCDIC may occupy more bytes
237than in UTF-8.
238
239Also, on some EBCDIC machines, functions that are documented as operating on
240US-ASCII (or Basic Latin in Unicode terminology) may in fact operate on all
241256 characters in the EBCDIC range, not just the subset corresponding to
242US-ASCII.
243
244The listing below is alphabetical, case insensitive.
94bdecf9
JH
245
246_EOB_
247
248my $key;
6a477168
HS
249# case insensitive sort, with fallback for determinacy
250for $key (sort { uc($a) cmp uc($b) || $a cmp $b } keys %apidocs) {
94bdecf9 251 my $section = $apidocs{$key};
08858ed2 252 print $doc "\n=head1 $key\n\n=over 8\n\n";
22469dce
NC
253 # Again, fallback for determinacy
254 for my $key (sort { uc($a) cmp uc($b) || $a cmp $b } keys %$section) {
08858ed2 255 docout($doc, $key, $section->{$key});
94bdecf9 256 }
08858ed2 257 print $doc "\n=back\n";
94bdecf9
JH
258}
259
08858ed2 260print $doc <<'_EOE_';
94bdecf9
JH
261
262=head1 AUTHORS
263
264Until May 1997, this document was maintained by Jeff Okamoto
265<okamoto@corp.hp.com>. It is now maintained as part of Perl itself.
266
267With lots of help and suggestions from Dean Roehrich, Malcolm Beattie,
268Andreas Koenig, Paul Hudson, Ilya Zakharevich, Paul Marquess, Neil
269Bowers, Matthew Green, Tim Bunce, Spider Boardman, Ulrich Pfeifer,
270Stephen McCamant, and Gurusamy Sarathy.
271
272API Listing originally by Dean Roehrich <roehrich@cray.com>.
273
274Updated to be autogenerated from comments in the source by Benjamin Stuhl.
275
276=head1 SEE ALSO
277
278perlguts(1), perlxs(1), perlxstut(1), perlintern(1)
279
280_EOE_
281
08858ed2 282readonly_footer($doc);
94bdecf9 283
08858ed2 284safer_close($doc);
94bdecf9 285
36bb303b 286safer_unlink "pod/perlintern.pod";
08858ed2
NC
287my $guts = safer_open("pod/perlintern.pod");
288readonly_header($guts);
289print $guts <<'END';
94bdecf9
JH
290=head1 NAME
291
292perlintern - autogenerated documentation of purely B<internal>
293 Perl functions
294
295=head1 DESCRIPTION
d8c40edc 296X<internal Perl functions> X<interpreter functions>
94bdecf9
JH
297
298This file is the autogenerated documentation of functions in the
299Perl interpreter that are documented using Perl's internal documentation
300format but are not marked as part of the Perl API. In other words,
301B<they are not for use in extensions>!
302
303END
304
305for $key (sort { uc($a) cmp uc($b); } keys %gutsdocs) {
306 my $section = $gutsdocs{$key};
08858ed2 307 print $guts "\n=head1 $key\n\n=over 8\n\n";
94bdecf9 308 for my $key (sort { uc($a) cmp uc($b); } keys %$section) {
08858ed2 309 docout($guts, $key, $section->{$key});
94bdecf9 310 }
08858ed2 311 print $guts "\n=back\n";
94bdecf9
JH
312}
313
08858ed2 314print $guts <<'END';
94bdecf9
JH
315
316=head1 AUTHORS
317
318The autodocumentation system was originally added to the Perl core by
319Benjamin Stuhl. Documentation is by whoever was kind enough to
320document their functions.
321
322=head1 SEE ALSO
323
324perlguts(1), perlapi(1)
325
326END
08858ed2 327readonly_footer($guts);
94bdecf9 328
08858ed2 329safer_close($guts);