3 # Unconditionally regenerate:
8 # from information stored in
11 # plus all the .c and .h files listed in MANIFEST
13 # Has an optional arg, which is the directory to chdir to before reading
14 # MANIFEST and *.[ch].
16 # This script is invoked as part of 'make all'
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.
25 # The meanings of the flags fields in embed.fnc and the source code is
26 # documented at the top of embed.fnc.
34 or die "Couldn't chdir to '$workdir': $!";
36 require './regen/regen_lib.pl';
37 require './regen/embed_lib.pl';
39 my %described_elsewhere;
42 # See database of global and static function prototypes in embed.fnc
43 # This is used to generate prototype headers under various configurations,
44 # export symbols lists for different platforms, and macros to provide an
45 # implicit interpreter context argument.
53 my $section = "Unknown section";
54 # Somewhat loose match for an apidoc line so we can catch minor typos.
55 # Parentheses are used to capture portions so that below we verify
56 # that things are the actual correct syntax.
57 my $apidoc_re = qr/ ^ (\s*) # $1
66 sub check_api_doc_line ($$) {
69 return unless $in =~ $apidoc_re;
71 my $is_item = defined $5;
72 my $is_in_proper_form = length $1 == 0
78 my $proto_in_file = $7;
79 my $proto = $proto_in_file;
80 $proto = "||$proto" if $proto !~ /\|/;
81 my ($flags, $ret_type, $name, @args) = split /\s*\|\s*/, $proto;
83 $name && $is_in_proper_form or die <<EOS;
84 Bad apidoc at $file line $.:
87 =for apidoc flags|returntype|name|arg|arg|...
88 =for apidoc flags|returntype|name
93 return ($name, $flags, $ret_type, $is_item, $proto_in_file, @args);
96 sub autodoc ($$) { # parse a file and extract documentation info
98 my($in, $line_num, $header);
100 my $file_is_C = $file =~ / \. [ch] $ /x;
103 my $get_next_line = sub { $line_num++; return <$fh> };
106 while (defined($in = $get_next_line->())) {
108 if ($in=~ /^=for apidoc_section\s*(.*)/) {
112 elsif ($file_is_C && $in=~ /^=head1 (.*)/) {
113 # =head1 lines only have effect in C files
117 # If the next non-space line begins with a word char, then it is
118 # the start of heading-level documentation.
119 if (defined($in = $get_next_line->())) {
120 # Skip over empty lines
121 while ($in =~ /^\s+$/) {
122 if (! defined($in = $get_next_line->())) {
132 # Continue getting the heading-level documentation until read
133 # in any pod directive (or as a fail-safe, find a closing
134 # comment to this pod in a C language file
136 while (defined($in = $get_next_line->())) {
141 if ($file_is_C && $in =~ m:^\s*\*/$:) {
142 warn "=cut missing? $file:$line_num:$in";;
151 if ($in =~ /^=for comment/) {
152 $in = $get_next_line->();
153 if ($in =~ /skip apidoc/) { # Skips the next apidoc-like line
154 while (defined($in = $get_next_line->())) {
155 last if $in =~ $apidoc_re;
161 my ($element_name, $flags, $ret_type, $is_item, $proto_in_file, @args)
162 = check_api_doc_line($file, $in);
163 next unless defined $element_name;
164 die "Unexpected apidoc_item '$in' in $file near line $." if $is_item;
166 # If the entry is also in embed.fnc, it should be defined completely
167 # there, but not here
168 my $embed_docref = delete $funcflags{$element_name};
169 if ($embed_docref and %$embed_docref) {
170 warn "embed.fnc entry overrides redundant information in"
171 . " '$proto_in_file' in $file" if $flags || $ret_type || @args;
172 $flags = $embed_docref->{'flags'};
173 warn "embed.fnc entry '$element_name' missing 'd' flag"
174 unless $flags =~ /d/;
175 $ret_type = $embed_docref->{'ret_type'};
176 @args = @{$embed_docref->{args}};
177 } elsif ($flags !~ /m/) { # Not in embed.fnc, is missing if not a
179 $missing{$element_name} = $file;
182 die "flag $1 is not legal (for function $element_name (from $file))"
183 if $flags =~ / ( [^AabCDdEeFfhiMmNnTOoPpRrSsUuWXx] ) /x;
186 die "'u' flag must also have 'm' flag' for $element_name" if $flags =~ /u/ && $flags !~ /m/;
187 warn ("'$element_name' not \\w+ in '$proto_in_file' in $file")
188 if $flags !~ /N/ && $element_name !~ / ^ [_[:alpha:]] \w* $ /x;
190 if (exists $seen{$element_name} && $flags !~ /h/) {
192 #die ("'$element_name' in $file was already documented in $seen{$element_name}");
195 $seen{$element_name} = $file;
199 my $is_link_only = ($flags =~ /h/);
200 if ($is_link_only) { # Don't put meat of entry in perlapi
201 next FUNC if $file_is_C; # Don't put anything if C source
203 # Here, is an 'h' flag in pod. We add a reference to the pod (and
204 # nothing else) to perlapi/intern. (It would be better to add a
205 # reference to the correct =item,=header, but something that makes
206 # it harder is that it that might be a duplicate, like '=item *';
207 # so that is a future enhancement XXX. Another complication is
208 # there might be more than one deserving candidates.)
210 my $podname = $file =~ s!.*/!!r; # Rmv directory name(s)
211 $podname =~ s/\.pod//;
212 $text .= "Described in L<$podname>.\n\n";
214 # Keep track of all the pod files that we refer to.
215 push $described_elsewhere{$podname}->@*, $podname;
219 while (defined($in = $get_next_line->())) {
221 # Other pod commands are considered part of the current
222 # function's docs, so can have lists, etc.
223 last DOC if $in =~ /^=(cut|for\s+apidoc|head)/;
224 if ($in =~ m:^\*/$:) {
225 warn "=cut missing? $file:$line_num:$in";;
231 $text = "\n$text" if $text and $text !~ /^\n/;
233 my $where = $flags =~ /A/ ? 'api' : 'guts';
235 if (exists $docs{$where}{$section}{$element_name}) {
236 warn "$0: duplicate API entry for '$element_name' in $where/$section\n";
239 $docs{$where}{$section}{$element_name}
240 = [$flags, $text, $ret_type, $file, @args];
242 # Create a special entry with an empty-string name for the
243 # heading-level documentation.
244 if (defined $header) {
245 $docs{$where}{$section}{""} = $header;
250 if ($in =~ /^=(?:for|head)/) {
253 } elsif (! $is_link_only) {
254 warn "No doc for $file:$line_num:$in";
259 sub docout ($$$) { # output the docs for one function
260 my($fh, $element_name, $docref) = @_;
261 my($flags, $pod, $ret_type, $file, @args) = @$docref;
262 $element_name =~ s/\s*$//;
264 warn("Empty pod for $element_name (from $file)") unless $pod =~ /\S/;
267 my $function = $flags =~ /n/ ? 'definition' : 'function';
268 $pod = "\n\nC<B<DEPRECATED!>> It is planned to remove this $function from a
269 future release of Perl. Do not use it for new code; remove it from
270 existing code.\n\n$pod";
273 $pod = "\n\nNOTE: this function is B<experimental> and may change or be
274 removed without notice.\n\n$pod" if $flags =~ /x/;
277 # Is Perl_, but no #define foo # Perl_foo
278 my $p = (($flags =~ /p/ && $flags =~ /o/ && $flags !~ /M/)
279 || ($flags =~ /f/ && $flags !~ /T/)); # Can't handle threaded varargs
281 $pod .= "NOTE: the C<perl_> form of this function is B<deprecated>.\n\n"
284 $pod .= "NOTE: this function must be explicitly called as C<Perl_$element_name>";
285 $pod .= " with an C<aTHX_> parameter" if $flags !~ /T/;
289 print $fh "=item $element_name\n";
291 # If we're printing only a link to an element, this isn't the major entry,
293 print $fh "X<$element_name>\n" unless $flags =~ /h/;
297 if ($flags =~ /U/) { # no usage
298 warn("U and s flags are incompatible") if $flags =~ /s/;
301 if ($flags =~ /n/) { # no args
302 warn("$file: $element_name: n flag without m") unless $flags =~ /m/;
303 warn("$file: $element_name: n flag but apparently has args") if @args;
304 print $fh "\t$ret_type\t$element_name";
305 } else { # full usage
306 my $n = "Perl_"x$p . $element_name;
307 my $large_ret = length $ret_type > 7;
308 my $indent_size = 7+8 # nroff: 7 under =head + 8 under =item
309 +8+($large_ret ? 1 + length $ret_type : 8)
312 print $fh "\t$ret_type" . ($large_ret ? ' ' : "\t") . "$n(";
315 if ($indent_size + 2 + length > 79) {
317 $indent_size -= length($n) - 3;
322 if ($flags !~ /T/ && ($p || ($flags =~ /m/ && $element_name =~ /^Perl_/))) {
323 $args = @args ? "pTHX_ " : "pTHX";
324 if ($long_args) { print $fh $args; $args = '' }
326 $long_args and print $fh "\n";
327 my $first = !$long_args;
331 && $indent_size + 3 + length($args[0]) + length $args > 79
336 "\t".($large_ret ? " " x (1+length $ret_type) : "\t")
337 ." "x($long_args ? 4 : 1 + length $n)
339 $args, (","x($args ne 'pTHX_ ') . "\n")x!!@args;
343 $args .= ", "x!!(length $args && $args ne 'pTHX_ ')
346 if ($long_args) { print $fh "\n", substr $indent, 0, -4 }
349 print $fh ";" if $flags =~ /s/; # semicolon "dTHR;"
352 print $fh "=for hackers\nFound in file $file\n\n";
356 # Do a case-insensitive dictionary sort, with only alphabetics
357 # significant, falling back to using everything for determinancy
358 return (uc($a =~ s/[[:^alpha:]]//r) cmp uc($b =~ s/[[:^alpha:]]//r))
364 my ($podname, $header, $dochash, $missing, $footer) = @_;
366 # strip leading '|' from each line which had been used to hide
367 # pod from pod checkers.
368 s/^\|//gm for $header, $footer;
370 my $fh = open_new("pod/$podname.pod", undef,
371 {by => "$0 extracting documentation",
372 from => 'the C source files'}, 1);
376 for my $section_name (sort sort_helper keys %$dochash) {
377 my $section_info = $dochash->{$section_name};
378 next unless keys %$section_info; # Skip empty
379 print $fh "\n=head1 $section_name\n\n";
381 # Output any heading-level documentation and delete so won't get in
383 if (exists $section_info->{""}) {
384 print $fh $section_info->{""} . "\n";
385 delete $section_info->{""};
387 next unless keys %$section_info; # Skip empty
388 print $fh "=over 8\n\n";
390 for my $function_name (sort sort_helper keys %$section_info) {
391 docout($fh, $function_name, $section_info->{$function_name});
393 print $fh "\n=back\n";
397 print $fh "\n=head1 Undocumented functions\n\n";
398 print $fh $podname eq 'perlapi' ? <<'_EOB_' : <<'_EOB_';
399 The following functions have been flagged as part of the public API,
400 but are currently undocumented. Use them at your own risk, as the
401 interfaces are subject to change. Functions that are not listed in this
402 document are not intended for public use, and should NOT be used under any
405 If you feel you need to use one of these functions, first send email to
406 L<perl5-porters@perl.org|mailto:perl5-porters@perl.org>. It may be
407 that there is a good reason for the function not being documented, and it
408 should be removed from this list; or it may just be that no one has gotten
409 around to documenting it. In the latter case, you will be asked to submit a
410 patch to document the function. Once your patch is accepted, it will indicate
411 that the interface is stable (unless it is explicitly marked otherwise) and
414 The following functions are currently undocumented. If you use one of
415 them, you may wish to consider creating and submitting documentation for
418 print $fh "\n=over\n\n";
420 for my $missing (sort sort_helper @$missing) {
421 print $fh "=item C<$missing>\nX<$missing>\n\n";
423 print $fh "=back\n\n";
425 print $fh $footer, "=cut\n";
427 read_only_bottom_close_and_rename($fh);
430 foreach (@{(setup_embed())[0]}) {
432 my ($flags, $ret_type, $func, @args) = @$_;
433 s/\b(?:NN|NULLOK)\b\s+//g for @args;
435 $funcflags{$func} = {
437 ret_type => $ret_type,
442 # glob() picks up docs from extra .c or .h files that may be in unclean
444 open my $fh, '<', 'MANIFEST'
445 or die "Can't open MANIFEST: $!";
446 while (my $line = <$fh>) {
447 next unless my ($file) = $line =~ /^(\S+\.(?:[ch]|pod))\t/;
449 # Don't pick up pods from these. (We may pick up generated stuff from
451 next if $file =~ m! ^ ( cpan | dist | ext ) / !x;
453 open F, '<', $file or die "Cannot open $file for docs: $!\n";
454 $section = "Functions in file $file\n";
456 close F or die "Error closing $file: $!\n";
458 close $fh or die "Error whilst reading MANIFEST: $!";
460 for (sort keys %funcflags) {
461 next unless $funcflags{$_}{flags} =~ /d/;
462 next if $funcflags{$_}{flags} =~ /h/;
463 warn "no docs for $_\n"
466 foreach (sort keys %missing) {
467 warn "Function '$_', documented in $missing{$_}, not listed in embed.fnc";
470 # walk table providing an array of components in each line to
471 # subroutine, printing the result
473 # List of funcs in the public API that aren't also marked as core-only,
474 # experimental nor deprecated.
475 my @missing_api = grep $funcflags{$_}{flags} =~ /A/
476 && $funcflags{$_}{flags} !~ /[xD]/
477 && !$docs{api}{$_}, keys %funcflags;
479 my $other_places = join ", ", map { "L<$_>" } sort sort_helper qw( perlclib perlxs),
480 keys %described_elsewhere;
482 output('perlapi', <<"_EOB_", $docs{api}, \@missing_api, <<"_EOE_");
487 |perlapi - autogenerated documentation for the perl public API
490 |X<Perl API> X<API> X<api>
492 |This file contains most of the documentation of the perl public API, as
493 |generated by F<embed.pl>. Specifically, it is a listing of functions,
494 |macros, flags, and variables that may be used by extension writers. Besides
495 |L<perlintern> and F<config.h>, some items are listed here as being actually
496 |documented in another pod.
498 |L<At the end|/Undocumented functions> is a list of functions which have yet
499 |to be documented. Patches welcome! The interfaces of these are subject to
500 |change without notice.
502 |Anything not listed here or in the other mentioned pods is not part of the
503 |public API, and should not be used by extension writers at all. For these
504 |reasons, blindly using functions listed in F<proto.h> is to be avoided when
507 |In Perl, unlike C, a string of characters may generally contain embedded
508 |C<NUL> characters. Sometimes in the documentation a Perl string is referred
509 |to as a "buffer" to distinguish it from a C string, but sometimes they are
510 |both just referred to as strings.
512 |Note that all Perl API global variables must be referenced with the C<PL_>
513 |prefix. Again, those not listed here are not to be used by extension writers,
514 |and can be changed or removed without notice; same with macros.
515 |Some macros are provided for compatibility with the older,
516 |unadorned names, but this support may be disabled in a future release.
518 |Perl was originally written to handle US-ASCII only (that is characters
519 |whose ordinal numbers are in the range 0 - 127).
520 |And documentation and comments may still use the term ASCII, when
521 |sometimes in fact the entire range from 0 - 255 is meant.
523 |The non-ASCII characters below 256 can have various meanings, depending on
524 |various things. (See, most notably, L<perllocale>.) But usually the whole
525 |range can be referred to as ISO-8859-1. Often, the term "Latin-1" (or
526 |"Latin1") is used as an equivalent for ISO-8859-1. But some people treat
527 |"Latin1" as referring just to the characters in the range 128 through 255, or
528 |sometimes from 160 through 255.
529 |This documentation uses "Latin1" and "Latin-1" to refer to all 256 characters.
531 |Note that Perl can be compiled and run under either ASCII or EBCDIC (See
532 |L<perlebcdic>). Most of the documentation (and even comments in the code)
533 |ignore the EBCDIC possibility.
534 |For almost all purposes the differences are transparent.
535 |As an example, under EBCDIC,
536 |instead of UTF-8, UTF-EBCDIC is used to encode Unicode strings, and so
537 |whenever this documentation refers to C<utf8>
538 |(and variants of that name, including in function names),
539 |it also (essentially transparently) means C<UTF-EBCDIC>.
540 |But the ordinals of characters differ between ASCII, EBCDIC, and
541 |the UTF- encodings, and a string encoded in UTF-EBCDIC may occupy a different
542 |number of bytes than in UTF-8.
544 |The listing below is alphabetical, case insensitive.
550 |Until May 1997, this document was maintained by Jeff Okamoto
551 |<okamoto\@corp.hp.com>. It is now maintained as part of Perl itself.
553 |With lots of help and suggestions from Dean Roehrich, Malcolm Beattie,
554 |Andreas Koenig, Paul Hudson, Ilya Zakharevich, Paul Marquess, Neil
555 |Bowers, Matthew Green, Tim Bunce, Spider Boardman, Ulrich Pfeifer,
556 |Stephen McCamant, and Gurusamy Sarathy.
558 |API Listing originally by Dean Roehrich <roehrich\@cray.com>.
560 |Updated to be autogenerated from comments in the source by Benjamin Stuhl.
564 |F<config.h>, L<perlintern>, $other_places
567 # List of non-static internal functions
569 grep $funcflags{$_}{flags} !~ /[AS]/ && !$docs{guts}{$_}, keys %funcflags;
571 output('perlintern', <<'_EOB_', $docs{guts}, \@missing_guts, <<"_EOE_");
574 |perlintern - autogenerated documentation of purely B<internal>
578 |X<internal Perl functions> X<interpreter functions>
580 |This file is the autogenerated documentation of functions in the
581 |Perl interpreter that are documented using Perl's internal documentation
582 |format but are not marked as part of the Perl API. In other words,
583 |B<they are not for use in extensions>!
589 |The autodocumentation system was originally added to the Perl core by
590 |Benjamin Stuhl. Documentation is by whoever was kind enough to
591 |document their functions.
595 |F<config.h>, L<perlapi>, $other_places