This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Provide .gitignore for PathTools distribution.
[perl5.git] / autodoc.pl
CommitLineData
94bdecf9 1#!/usr/bin/perl -w
6294c161
DM
2#
3# Unconditionally regenerate:
4#
5# pod/perlintern.pod
6# pod/perlapi.pod
7#
8# from information stored in
9#
10# embed.fnc
11# plus all the .c and .h files listed in MANIFEST
12#
13# Has an optional arg, which is the directory to chdir to before reading
14# MANIFEST and *.[ch].
15#
52a9d53b 16# This script is invoked as part of 'make all'
151c3fe5 17#
f554dfc5
MH
18# '=head1' are the only headings looked for. If the first non-blank line after
19# the heading begins with a word character, it is considered to be the first
20# line of documentation that applies to the heading itself. That is, it is
21# output immediately after the heading, before the first function, and not
22# indented. The next input line that is a pod directive terminates this
23# heading-level documentation.
94bdecf9 24
56a0c332 25use strict;
a64c954a 26
7882b24a
NC
27if (@ARGV) {
28 my $workdir = shift;
29 chdir $workdir
30 or die "Couldn't chdir to '$workdir': $!";
31}
3d7c117d
MB
32require './regen/regen_lib.pl';
33require './regen/embed_lib.pl';
7882b24a 34
94bdecf9 35#
346f75ff 36# See database of global and static function prototypes in embed.fnc
94bdecf9
JH
37# This is used to generate prototype headers under various configurations,
38# export symbols lists for different platforms, and macros to provide an
39# implicit interpreter context argument.
40#
41
6a235718 42my %docs;
5ce57792
NC
43my %funcflags;
44my %macro = (
45 ax => 1,
46 items => 1,
47 ix => 1,
48 svtype => 1,
49 );
50my %missing;
94bdecf9
JH
51
52my $curheader = "Unknown section";
53
54sub autodoc ($$) { # parse a file and extract documentation info
55 my($fh,$file) = @_;
151c3fe5 56 my($in, $doc, $line, $header_doc);
f554dfc5
MH
57
58 # Count lines easier
59 my $get_next_line = sub { $line++; return <$fh> };
60
94bdecf9 61FUNC:
f554dfc5 62 while (defined($in = $get_next_line->())) {
5ce57792
NC
63 if ($in =~ /^#\s*define\s+([A-Za-z_][A-Za-z_0-9]+)\(/ &&
64 ($file ne 'embed.h' || $file ne 'proto.h')) {
65 $macro{$1} = $file;
66 next FUNC;
67 }
94bdecf9
JH
68 if ($in=~ /^=head1 (.*)/) {
69 $curheader = $1;
151c3fe5 70
f554dfc5
MH
71 # If the next non-space line begins with a word char, then it is
72 # the start of heading-ldevel documentation.
73 if (defined($doc = $get_next_line->())) {
74 # Skip over empty lines
75 while ($doc =~ /^\s+$/) {
76 if (! defined($doc = $get_next_line->())) {
77 next FUNC;
78 }
79 }
80
151c3fe5
KW
81 if ($doc !~ /^\w/) {
82 $in = $doc;
83 redo FUNC;
84 }
85 $header_doc = $doc;
151c3fe5
KW
86
87 # Continue getting the heading-level documentation until read
88 # in any pod directive (or as a fail-safe, find a closing
89 # comment to this pod in a C language file
90HDR_DOC:
f554dfc5 91 while (defined($doc = $get_next_line->())) {
151c3fe5
KW
92 if ($doc =~ /^=\w/) {
93 $in = $doc;
94 redo FUNC;
95 }
151c3fe5
KW
96
97 if ($doc =~ m:^\s*\*/$:) {
98 warn "=cut missing? $file:$line:$doc";;
99 last HDR_DOC;
100 }
101 $header_doc .= $doc;
102 }
103 }
94bdecf9
JH
104 next FUNC;
105 }
78c9d763 106 if ($in =~ /^=for\s+apidoc\s+(.*?)\s*\n/) {
94bdecf9
JH
107 my $proto = $1;
108 $proto = "||$proto" unless $proto =~ /\|/;
109 my($flags, $ret, $name, @args) = split /\|/, $proto;
110 my $docs = "";
111DOC:
f554dfc5 112 while (defined($doc = $get_next_line->())) {
72d4186d
KW
113
114 # Other pod commands are considered part of the current
115 # function's docs, so can have lists, etc.
116 last DOC if $doc =~ /^=(cut|for\s+apidoc|head)/;
94bdecf9
JH
117 if ($doc =~ m:^\*/$:) {
118 warn "=cut missing? $file:$line:$doc";;
119 last DOC;
72d4186d 120 }
94bdecf9
JH
121 $docs .= $doc;
122 }
123 $docs = "\n$docs" if $docs and $docs !~ /^\n/;
5ce57792
NC
124
125 # Check the consistency of the flags
126 my ($embed_where, $inline_where);
127 my ($embed_may_change, $inline_may_change);
128
129 my $docref = delete $funcflags{$name};
130 if ($docref and %$docref) {
131 $embed_where = $docref->{flags} =~ /A/ ? 'api' : 'guts';
132 $embed_may_change = $docref->{flags} =~ /M/;
d4e99c76 133 $flags .= 'D' if $docref->{flags} =~ /D/;
5ce57792
NC
134 } else {
135 $missing{$name} = $file;
94bdecf9 136 }
5ce57792
NC
137 if ($flags =~ /m/) {
138 $inline_where = $flags =~ /A/ ? 'api' : 'guts';
139 $inline_may_change = $flags =~ /x/;
140
141 if (defined $embed_where && $inline_where ne $embed_where) {
142 warn "Function '$name' inconsistency: embed.fnc says $embed_where, Pod says $inline_where";
143 }
144
145 if (defined $embed_may_change
146 && $inline_may_change ne $embed_may_change) {
147 my $message = "Function '$name' inconsistency: ";
148 if ($embed_may_change) {
149 $message .= "embed.fnc says 'may change', Pod does not";
150 } else {
151 $message .= "Pod says 'may change', embed.fnc does not";
152 }
153 warn $message;
154 }
155 } elsif (!defined $embed_where) {
156 warn "Unable to place $name!\n";
157 next;
158 } else {
159 $inline_where = $embed_where;
160 $flags .= 'x' if $embed_may_change;
161 @args = @{$docref->{args}};
162 $ret = $docref->{retval};
94bdecf9 163 }
5ce57792 164
7a6610ca
DM
165 if (exists $docs{$inline_where}{$curheader}{$name}) {
166 warn "$0: duplicate API entry for '$name' in $inline_where/$curheader\n";
167 next;
168 }
5ce57792
NC
169 $docs{$inline_where}{$curheader}{$name}
170 = [$flags, $docs, $ret, $file, @args];
171
151c3fe5
KW
172 # Create a special entry with an empty-string name for the
173 # heading-level documentation.
174 if (defined $header_doc) {
175 $docs{$inline_where}{$curheader}{""} = $header_doc;
176 undef $header_doc;
177 }
178
94bdecf9 179 if (defined $doc) {
e509e693 180 if ($doc =~ /^=(?:for|head)/) {
94bdecf9
JH
181 $in = $doc;
182 redo FUNC;
183 }
184 } else {
185 warn "$file:$line:$in";
186 }
187 }
188 }
189}
190
191sub docout ($$$) { # output the docs for one function
192 my($fh, $name, $docref) = @_;
193 my($flags, $docs, $ret, $file, @args) = @$docref;
d8c40edc 194 $name =~ s/\s*$//;
94bdecf9 195
d4e99c76
KW
196 if ($flags =~ /D/) {
197 $docs = "\n\nDEPRECATED! It is planned to remove this function from a
198future release of Perl. Do not use it for new code; remove it from
199existing code.\n\n$docs";
200 }
201 else {
58a428bb
KW
202 $docs = "\n\nNOTE: this function is experimental and may change or be
203removed without notice.\n\n$docs" if $flags =~ /x/;
d4e99c76 204 }
94bdecf9
JH
205 $docs .= "NOTE: the perl_ form of this function is deprecated.\n\n"
206 if $flags =~ /p/;
5afac1eb
BM
207 $docs .= "NOTE: this function must be explicitly called as Perl_$name with an aTHX_ parameter.\n\n"
208 if $flags =~ /o/;
94bdecf9 209
d8c40edc 210 print $fh "=item $name\nX<$name>\n$docs";
94bdecf9
JH
211
212 if ($flags =~ /U/) { # no usage
213 # nothing
214 } elsif ($flags =~ /s/) { # semicolon ("dTHR;")
215 print $fh "\t\t$name;\n\n";
216 } elsif ($flags =~ /n/) { # no args
217 print $fh "\t$ret\t$name\n\n";
218 } else { # full usage
dee6204d
FC
219 my $p = $flags =~ /o/; # no #define foo Perl_foo
220 my $n = "Perl_"x$p . $name;
221 my $large_ret = length $ret > 7;
222 my $indent_size = 7+8 # nroff: 7 under =head + 8 under =item
223 +8+($large_ret ? 1 + length $ret : 8)
224 +length($n) + 1;
225 my $indent;
226 print $fh "\t$ret" . ($large_ret ? ' ' : "\t") . "$n(";
06e9ce89
FC
227 my $long_args;
228 for (@args) {
d2086f64 229 if ($indent_size + 2 + length > 79) {
06e9ce89
FC
230 $long_args=1;
231 $indent_size -= length($n) - 3;
232 last;
233 }
234 }
235 my $args = '';
236 if ($p) {
237 $args = @args ? "pTHX_ " : "pTHX";
238 if ($long_args) { print $fh $args; $args = '' }
239 }
240 $long_args and print $fh "\n";
241 my $first = !$long_args;
dee6204d
FC
242 while () {
243 if (!@args or
244 length $args
d2086f64 245 && $indent_size + 3 + length($args[0]) + length $args > 79
dee6204d
FC
246 ) {
247 print $fh
248 $first ? '' : (
249 $indent //=
250 "\t".($large_ret ? " " x (1+length $ret) : "\t")
06e9ce89 251 ." "x($long_args ? 4 : 1 + length $n)
dee6204d
FC
252 ),
253 $args, (","x($args ne 'pTHX_ ') . "\n")x!!@args;
254 $args = $first = '';
255 }
256 @args or last;
257 $args .= ", "x!!(length $args && $args ne 'pTHX_ ')
258 . shift @args;
259 }
06e9ce89 260 if ($long_args) { print $fh "\n", substr $indent, 0, -4 }
dee6204d 261 print $fh ")\n\n";
94bdecf9
JH
262 }
263 print $fh "=for hackers\nFound in file $file\n\n";
264}
265
f83c6033
KW
266sub sort_helper {
267 # Do a case-insensitive dictionary sort, with only alphabetics
268 # significant, falling back to using everything for determinancy
1354d57e 269 return (uc($a =~ s/[[:^alpha:]]//r) cmp uc($b =~ s/[[:^alpha:]]//r))
f83c6033
KW
270 || uc($a) cmp uc($b)
271 || $a cmp $b;
272}
273
7b73ff98 274sub output {
5a0155e6 275 my ($podname, $header, $dochash, $missing, $footer) = @_;
7882b24a
NC
276 my $fh = open_new("pod/$podname.pod", undef,
277 {by => "$0 extracting documentation",
f1f44974 278 from => 'the C source files'}, 1);
e0492643 279
7882b24a 280 print $fh $header;
e0492643 281
7b73ff98 282 my $key;
f83c6033 283 for $key (sort sort_helper keys %$dochash) {
7b73ff98 284 my $section = $dochash->{$key};
151c3fe5
KW
285 print $fh "\n=head1 $key\n\n";
286
287 # Output any heading-level documentation and delete so won't get in
288 # the way later
289 if (exists $section->{""}) {
290 print $fh $section->{""} . "\n";
291 delete $section->{""};
292 }
293 print $fh "=over 8\n\n";
294
f83c6033 295 for my $key (sort sort_helper keys %$section) {
7b73ff98
NC
296 docout($fh, $key, $section->{$key});
297 }
298 print $fh "\n=back\n";
299 }
300
5a0155e6 301 if (@$missing) {
a23e6e20 302 print $fh "\n=head1 Undocumented functions\n\n";
2616800a 303 print $fh $podname eq 'perlapi' ? <<'_EOB_' : <<'_EOB_';
474d0ac8 304The following functions have been flagged as part of the public API,
72d33970 305but are currently undocumented. Use them at your own risk, as the
ba4591a5
KW
306interfaces are subject to change. Functions that are not listed in this
307document are not intended for public use, and should NOT be used under any
308circumstances.
309
5a4fed09
KW
310If you feel you need to use one of these functions, first send email to
311L<perl5-porters@perl.org|mailto:perl5-porters@perl.org>. It may be
312that there is a good reason for the function not being documented, and it
313should be removed from this list; or it may just be that no one has gotten
314around to documenting it. In the latter case, you will be asked to submit a
315patch to document the function. Once your patch is accepted, it will indicate
316that the interface is stable (unless it is explicitly marked otherwise) and
317usable by you.
cf5f2f8f
KW
318
319=over
320
321_EOB_
2616800a
FC
322The following functions are currently undocumented. If you use one of
323them, you may wish to consider creating and submitting documentation for
324it.
325
326=over
327
328_EOB_
cf5f2f8f
KW
329 for my $missing (sort @$missing) {
330 print $fh "=item $missing\nX<$missing>\n\n";
5a0155e6 331 }
cf5f2f8f
KW
332 print $fh "=back\n\n";
333}
7882b24a 334 print $fh $footer, "=cut\n";
5a0155e6 335
7882b24a 336 read_only_bottom_close_and_rename($fh);
cd093254
MM
337}
338
e8e591c9
NC
339foreach (@{(setup_embed())[0]}) {
340 next if @$_ < 2;
341 my ($flags, $retval, $func, @args) = @$_;
342 s/\b(?:NN|NULLOK)\b\s+//g for @args;
bc350081 343
5ce57792
NC
344 $funcflags{$func} = {
345 flags => $flags,
346 retval => $retval,
347 args => \@args,
348 };
349}
350
5ce57792
NC
351# glob() picks up docs from extra .c or .h files that may be in unclean
352# development trees.
741c0772
NC
353open my $fh, '<', 'MANIFEST'
354 or die "Can't open MANIFEST: $!";
355while (my $line = <$fh>) {
356 next unless my ($file) = $line =~ /^(\S+\.[ch])\t/;
5ce57792 357
1ae6ead9 358 open F, '<', $file or die "Cannot open $file for docs: $!\n";
5ce57792
NC
359 $curheader = "Functions in file $file\n";
360 autodoc(\*F,$file);
361 close F or die "Error closing $file: $!\n";
362}
741c0772 363close $fh or die "Error whilst reading MANIFEST: $!";
5ce57792
NC
364
365for (sort keys %funcflags) {
366 next unless $funcflags{$_}{flags} =~ /d/;
367 warn "no docs for $_\n"
bc350081 368}
94bdecf9 369
5ce57792
NC
370foreach (sort keys %missing) {
371 next if $macro{$_};
372 # Heuristics for known not-a-function macros:
373 next if /^[A-Z]/;
374 next if /^dj?[A-Z]/;
375
376 warn "Function '$_', documented in $missing{$_}, not listed in embed.fnc";
94bdecf9
JH
377}
378
5ce57792
NC
379# walk table providing an array of components in each line to
380# subroutine, printing the result
381
8c869419
KW
382# List of funcs in the public API that aren't also marked as experimental nor
383# deprecated.
384my @missing_api = grep $funcflags{$_}{flags} =~ /A/ && $funcflags{$_}{flags} !~ /[MD]/ && !$docs{api}{$_}, keys %funcflags;
5a0155e6 385output('perlapi', <<'_EOB_', $docs{api}, \@missing_api, <<'_EOE_');
f1d36e7c
KW
386=encoding UTF-8
387
94bdecf9
JH
388=head1 NAME
389
390perlapi - autogenerated documentation for the perl public API
391
392=head1 DESCRIPTION
d8c40edc 393X<Perl API> X<API> X<api>
94bdecf9
JH
394
395This file contains the documentation of the perl public API generated by
ef9741a5 396F<embed.pl>, specifically a listing of functions, macros, flags, and variables
cf5f2f8f
KW
397that may be used by extension writers. L<At the end|/Undocumented functions>
398is a list of functions which have yet to be documented. The interfaces of
37a519b2 399those are subject to change without notice. Anything not listed here is
cf5f2f8f
KW
400not part of the public API, and should not be used by extension writers at
401all. For these reasons, blindly using functions listed in proto.h is to be
402avoided when writing extensions.
94bdecf9 403
dc6b0978
KW
404In Perl, unlike C, a string of characters may generally contain embedded
405C<NUL> characters. Sometimes in the documentation a Perl string is referred
406to as a "buffer" to distinguish it from a C string, but sometimes they are
407both just referred to as strings.
408
94bdecf9 409Note that all Perl API global variables must be referenced with the C<PL_>
37a519b2
KW
410prefix. Again, those not listed here are not to be used by extension writers,
411and can be changed or removed without notice; same with macros.
412Some macros are provided for compatibility with the older,
94bdecf9
JH
413unadorned names, but this support may be disabled in a future release.
414
2bbc8d55
SP
415Perl was originally written to handle US-ASCII only (that is characters
416whose ordinal numbers are in the range 0 - 127).
417And documentation and comments may still use the term ASCII, when
418sometimes in fact the entire range from 0 - 255 is meant.
419
14d32fa9
KW
420The non-ASCII characters below 256 can have various meanings, depending on
421various things. (See, most notably, L<perllocale>.) But usually the whole
422range can be referred to as ISO-8859-1. Often, the term "Latin-1" (or
423"Latin1") is used as an equivalent for ISO-8859-1. But some people treat
d708a1fb
KW
424"Latin1" as referring just to the characters in the range 128 through 255, or
425somethimes from 160 through 255.
14d32fa9
KW
426This documentation uses "Latin1" and "Latin-1" to refer to all 256 characters.
427
89d7b922
KW
428Note that Perl can be compiled and run under either ASCII or EBCDIC (See
429L<perlebcdic>). Most of the documentation (and even comments in the code)
2bbc8d55
SP
430ignore the EBCDIC possibility.
431For almost all purposes the differences are transparent.
432As an example, under EBCDIC,
433instead of UTF-8, UTF-EBCDIC is used to encode Unicode strings, and so
434whenever this documentation refers to C<utf8>
435(and variants of that name, including in function names),
436it also (essentially transparently) means C<UTF-EBCDIC>.
437But the ordinals of characters differ between ASCII, EBCDIC, and
c0236afe
KW
438the UTF- encodings, and a string encoded in UTF-EBCDIC may occupy a different
439number of bytes than in UTF-8.
2bbc8d55 440
2bbc8d55 441The listing below is alphabetical, case insensitive.
94bdecf9
JH
442
443_EOB_
444
94bdecf9
JH
445=head1 AUTHORS
446
447Until May 1997, this document was maintained by Jeff Okamoto
448<okamoto@corp.hp.com>. It is now maintained as part of Perl itself.
449
450With lots of help and suggestions from Dean Roehrich, Malcolm Beattie,
451Andreas Koenig, Paul Hudson, Ilya Zakharevich, Paul Marquess, Neil
452Bowers, Matthew Green, Tim Bunce, Spider Boardman, Ulrich Pfeifer,
453Stephen McCamant, and Gurusamy Sarathy.
454
455API Listing originally by Dean Roehrich <roehrich@cray.com>.
456
457Updated to be autogenerated from comments in the source by Benjamin Stuhl.
458
459=head1 SEE ALSO
460
b92fc6c1 461L<perlguts>, L<perlxs>, L<perlxstut>, L<perlintern>
94bdecf9
JH
462
463_EOE_
464
79fc8511
FC
465# List of non-static internal functions
466my @missing_guts =
467 grep $funcflags{$_}{flags} !~ /[As]/ && !$docs{guts}{$_}, keys %funcflags;
5a0155e6
TC
468
469output('perlintern', <<'END', $docs{guts}, \@missing_guts, <<'END');
94bdecf9
JH
470=head1 NAME
471
472perlintern - autogenerated documentation of purely B<internal>
473 Perl functions
474
475=head1 DESCRIPTION
d8c40edc 476X<internal Perl functions> X<interpreter functions>
94bdecf9
JH
477
478This file is the autogenerated documentation of functions in the
479Perl interpreter that are documented using Perl's internal documentation
154e47c8 480format but are not marked as part of the Perl API. In other words,
94bdecf9
JH
481B<they are not for use in extensions>!
482
483END
484
94bdecf9
JH
485=head1 AUTHORS
486
487The autodocumentation system was originally added to the Perl core by
154e47c8 488Benjamin Stuhl. Documentation is by whoever was kind enough to
94bdecf9
JH
489document their functions.
490
491=head1 SEE ALSO
492
b92fc6c1 493L<perlguts>, L<perlapi>
94bdecf9
JH
494
495END