]>
git.ipfire.org Git - thirdparty/bird.git/blob - doc/kernel-doc
3 ## Copyright (c) 1998 Michael Zucchi, All Rights Reserved ##
5 ## This software falls under the GNU Public License. Please read ##
6 ## the COPYING file for more information ##
9 # This will read a 'c' file and scan for embedded comments in the
10 # style of gnome comments (+minor extensions - see below).
13 # Note: This only supports 'c'.
16 # kerneldoc [ -docbook | -html | -text | -man ]
17 # [ -function funcname [ -function funcname ...] ] c file(s)s > outputfile
19 # [ -nofunction funcname [ -function funcname ...] ] c file(s)s > outputfile
21 # Set output format using one of -docbook -html -text or -man. Default is man.
24 # If set, then only generate documentation for the given function(s). All
25 # other functions are ignored.
27 # -nofunction funcname
28 # If set, then only generate documentation for the other function(s). All
29 # other functions are ignored. Cannot be used with -function together
30 # (yes thats a bug - perl hackers can fix it 8))
32 # c files - list of 'c' files to process
34 # All output goes to stdout, with errors to stderr.
38 # In the following table, (...)? signifies optional structure.
39 # (...)* signifies 0 or more structure elements
41 # * function_name(:)? (- short description)?
42 # (* @parameterx: (description of parameter x)?)*
44 # * (Description:)? (Description of function)?
45 # * (section header: (section description)? )*
48 # So .. the trivial example would be:
54 # If the Description: header tag is ommitted, then there must be a blank line
55 # after the last parameter specification.
58 # * my_function - does my stuff
59 # * @my_arg: its mine damnit
61 # * Does my stuff explained.
66 # * my_function - does my stuff
67 # * @my_arg: its mine damnit
68 # * Description: Does my stuff explained.
72 # All descriptions can be multiline, apart from the short function description.
74 # All descriptive text is further processed, scanning for the following special
75 # patterns, which are highlighted appropriately.
77 # 'funcname()' - function
78 # '$ENVVAR' - environmental variable
79 # '&struct_name' - name of a structure
80 # '@parameter' - name of a parameter
81 # '%CONST' - name of a constant.
83 # match expressions used to find embedded type information
84 $type_constant = "\\\%(\\w+)";
85 $type_func = "(\\w+\\(\\))";
86 $type_param = "\\\@(\\w+)";
87 $type_struct = "\\\&(\\w+)";
88 $type_env = "(\\\$\\w+)";
91 # Output conversion substitutions.
92 # One for each output format
94 # these work fairly well
95 %highlights_html = ( $type_constant, "<i>\$1</i>",
96 $type_func, "<b>\$1</b>",
97 $type_struct, "<i>\$1</i>",
98 $type_param, "<tt><b>\$1</b></tt>" );
99 $blankline_html = "<p>";
101 # sgml, docbook format
102 %highlights_sgml = ( $type_constant, "<replaceable class=\"option\">\$1</replaceable>",
103 $type_func, "<function>\$1</function>",
104 $type_struct, "<structname>\$1</structname>",
105 $type_env, "<envar>\$1</envar>",
106 $type_param, "<parameter>\$1</parameter>" );
107 $blankline_sgml = "</para><para>\n";
109 # gnome, docbook format
110 %highlights_gnome = ( $type_constant, "<replaceable class=\"option\">\$1</replaceable>",
111 $type_func, "<function>\$1</function>",
112 $type_struct, "<structname>\$1</structname>",
113 $type_env, "<envar>\$1</envar>",
114 $type_param, "<parameter>\$1</parameter>" );
115 $blankline_gnome = "</para><para>\n";
117 # these are pretty rough
118 %highlights_man = ( $type_constant, "\\n.I \\\"\$1\\\"\\n",
119 $type_func, "\\n.B \\\"\$1\\\"\\n",
120 $type_struct, "\\n.I \\\"\$1\\\"\\n",
121 $type_param."([\.\, ]*)\n?", "\\n.I \\\"\$1\$2\\\"\\n" );
125 %highlights_text = ( $type_constant, "\$1",
128 $type_param, "\$1" );
129 $blankline_text = "";
133 print "Usage: $0 [ -v ] [ -docbook | -html | -text | -man ]\n";
134 print " [ -function funcname [ -function funcname ...] ]\n";
135 print " [ -nofunction funcname [ -nofunction funcname ...] ]\n";
136 print " c source file(s) > outputfile\n";
146 $output_mode = "man";
147 %highlights = %highlights_man;
148 $blankline = $blankline_man;
149 $modulename = "API Documentation";
151 while ($ARGV[0] =~ m/^-(.*)/) {
153 if ($cmd eq "-html") {
154 $output_mode = "html";
155 %highlights = %highlights_html;
156 $blankline = $blankline_html;
157 } elsif ($cmd eq "-man") {
158 $output_mode = "man";
159 %highlights = %highlights_man;
160 $blankline = $blankline_man;
161 } elsif ($cmd eq "-text") {
162 $output_mode = "text";
163 %highlights = %highlights_text;
164 $blankline = $blankline_text;
165 } elsif ($cmd eq "-docbook") {
166 $output_mode = "sgml";
167 %highlights = %highlights_sgml;
168 $blankline = $blankline_sgml;
169 } elsif ($cmd eq "-gnome") {
170 $output_mode = "gnome";
171 %highlights = %highlights_gnome;
172 $blankline = $blankline_gnome;
173 } elsif ($cmd eq "-module") { # not needed for sgml, inherits from calling document
174 $modulename = shift @ARGV;
175 } elsif ($cmd eq "-function") { # to only output specific functions
177 $function = shift @ARGV;
178 $function_table{$function} = 1;
179 } elsif ($cmd eq "-nofunction") { # to only output specific functions
181 $function = shift @ARGV;
182 $function_table{$function} = 1;
183 } elsif ($cmd eq "-v") {
185 } elsif (($cmd eq "-h") || ($cmd eq "--help")) {
191 # generate a sequence of code that will splice in highlighting information
192 # using the s// operator.
194 foreach $pattern (keys %highlights) {
195 # print "scanning pattern $pattern ($highlights{$pattern})\n";
196 $dohighlight .= "\$contents =~ s:$pattern:$highlights{$pattern}:gs;\n";
200 # dumps section contents to arrays/hashes intended for that purpose.
204 my $contents = join "\n", @_;
206 if ($name =~ m/$type_constant/) {
208 # print STDERR "constant section '$1' = '$contents'\n";
209 $constants{$name} = $contents;
210 } elsif ($name =~ m/$type_param/) {
211 # print STDERR "parameter def '$1' = '$contents'\n";
213 $parameters{$name} = $contents;
215 # print STDERR "other section '$name' = '$contents'\n";
216 $sections{$name} = $contents;
217 push @sectionlist, $name;
224 # parameters, a hash.
225 # function => "function name"
226 # parameterlist => @list of parameters
227 # parameters => %parameter descriptions
228 # sectionlist => @list of sections
229 # sections => %descriont descriptions
232 sub output_highlight
{
233 my $contents = join "\n", @_;
237 foreach $line (split "\n", $contents) {
239 print $lineprefix, $blankline;
241 $line =~ s/\\\\\\/\&/g;
242 print $lineprefix, $line;
252 my ($parameter, $section);
254 print "<h2>Function</h2>\n";
256 print "<i>".$args{'functiontype'}."</i>\n";
257 print "<b>".$args{'function'}."</b>\n";
260 foreach $parameter (@
{$args{'parameterlist'}}) {
261 print "<i>".$args{'parametertypes'}{$parameter}."</i> <b>".$parameter."</b>\n";
262 if ($count != $#{$args{'parameterlist'}}) {
269 print "<h3>Arguments</h3>\n";
271 foreach $parameter (@
{$args{'parameterlist'}}) {
272 print "<dt><i>".$args{'parametertypes'}{$parameter}."</i> <b>".$parameter."</b>\n";
274 output_highlight
($args{'parameters'}{$parameter});
277 foreach $section (@
{$args{'sectionlist'}}) {
278 print "<h3>$section</h3>\n";
280 output_highlight
($args{'sections'}{$section});
288 sub output_intro_html
{
290 my ($parameter, $section);
293 foreach $section (@
{$args{'sectionlist'}}) {
294 print "<h3>$section</h3>\n";
296 output_highlight
($args{'sections'}{$section});
304 # output in sgml DocBook
307 my ($parameter, $section);
311 $id = $args{'module'}."-".$args{'function'};
312 $id =~ s/[^A-Za-z0-9]/-/g;
314 print "<refentry>\n";
316 print "<refentrytitle><phrase id=\"$id\">".$args{'function'}."</phrase></refentrytitle>\n";
317 print "</refmeta>\n";
318 print "<refnamediv>\n";
319 print " <refname>".$args{'function'}."</refname>\n";
320 print " <refpurpose>\n";
321 print " ".$args{'purpose'}."\n";
322 print " </refpurpose>\n";
323 print "</refnamediv>\n";
325 print "<refsynopsisdiv>\n";
326 print " <title>Synopsis</title>\n";
327 print " <funcsynopsis>\n";
328 print " <funcdef>".$args{'functiontype'}." ";
329 print "<function>".$args{'function'}." ";
330 print "</function></funcdef>\n";
332 # print "<refsect1>\n";
333 # print " <title>Synopsis</title>\n";
334 # print " <funcsynopsis>\n";
335 # print " <funcdef>".$args{'functiontype'}." ";
336 # print "<function>".$args{'function'}." ";
337 # print "</function></funcdef>\n";
340 if ($#{$args{'parameterlist'}} >= 0) {
341 foreach $parameter (@
{$args{'parameterlist'}}) {
342 print " <paramdef>".$args{'parametertypes'}{$parameter};
343 print " <parameter>$parameter</parameter></paramdef>\n";
348 print " </funcsynopsis>\n";
349 print "</refsynopsisdiv>\n";
350 # print "</refsect1>\n";
353 print "<refsect1>\n <title>Arguments</title>\n";
354 # print "<para>\nArguments\n";
355 if ($#{$args{'parameterlist'}} >= 0) {
356 print " <variablelist>\n";
357 foreach $parameter (@
{$args{'parameterlist'}}) {
358 print " <varlistentry>\n <term><parameter>$parameter</parameter></term>\n";
359 print " <listitem>\n <para>\n";
361 output_highlight
($args{'parameters'}{$parameter});
362 print " </para>\n </listitem>\n </varlistentry>\n";
364 print " </variablelist>\n";
366 print " <para>\n None\n </para>\n";
368 print "</refsect1>\n";
370 # print out each section
372 foreach $section (@
{$args{'sectionlist'}}) {
373 print "<refsect1>\n <title>$section</title>\n <para>\n";
374 # print "<para>\n$section\n";
375 if ($section =~ m/EXAMPLE/i) {
376 print "<example><para>\n";
378 output_highlight
($args{'sections'}{$section});
380 if ($section =~ m/EXAMPLE/i) {
381 print "</para></example>\n";
383 print " </para>\n</refsect1>\n";
386 print "</refentry>\n\n";
389 # output in sgml DocBook
390 sub output_intro_sgml
{
392 my ($parameter, $section);
396 $id = $args{'module'};
397 $id =~ s/[^A-Za-z0-9]/-/g;
399 # print out each section
401 foreach $section (@
{$args{'sectionlist'}}) {
402 print "<refsect1>\n <title>$section</title>\n <para>\n";
403 # print "<para>\n$section\n";
404 if ($section =~ m/EXAMPLE/i) {
405 print "<example><para>\n";
407 output_highlight
($args{'sections'}{$section});
409 if ($section =~ m/EXAMPLE/i) {
410 print "</para></example>\n";
412 print " </para>\n</refsect1>\n";
418 # output in sgml DocBook
421 my ($parameter, $section);
425 $id = $args{'module'}."-".$args{'function'};
426 $id =~ s/[^A-Za-z0-9]/-/g;
429 print " <title id=\"$id\">".$args{'function'}."</title>\n";
431 # print "<simplesect>\n";
432 # print " <title>Synopsis</title>\n";
433 print " <funcsynopsis>\n";
434 print " <funcdef>".$args{'functiontype'}." ";
435 print "<function>".$args{'function'}." ";
436 print "</function></funcdef>\n";
439 if ($#{$args{'parameterlist'}} >= 0) {
440 foreach $parameter (@
{$args{'parameterlist'}}) {
441 print " <paramdef>".$args{'parametertypes'}{$parameter};
442 print " <parameter>$parameter</parameter></paramdef>\n";
447 print " </funcsynopsis>\n";
448 # print "</simplesect>\n";
449 # print "</refsect1>\n";
452 # print "<simplesect>\n <title>Arguments</title>\n";
453 # if ($#{$args{'parameterlist'}} >= 0) {
454 # print " <variablelist>\n";
455 # foreach $parameter (@{$args{'parameterlist'}}) {
456 # print " <varlistentry>\n <term><parameter>$parameter</parameter></term>\n";
457 # print " <listitem>\n <para>\n";
459 # output_highlight($args{'parameters'}{$parameter});
460 # print " </para>\n </listitem>\n </varlistentry>\n";
462 # print " </variablelist>\n";
464 # print " <para>\n None\n </para>\n";
466 # print "</simplesect>\n";
468 # print "<simplesect>\n <title>Arguments</title>\n";
469 if ($#{$args{'parameterlist'}} >= 0) {
470 print " <informaltable pgwide=\"1\" frame=\"none\" role=\"params\">\n";
471 print "<tgroup cols=\"2\">\n";
472 print "<colspec colwidth=\"2*\">\n";
473 print "<colspec colwidth=\"8*\">\n";
475 foreach $parameter (@
{$args{'parameterlist'}}) {
476 print " <row><entry align=\"right\"><parameter>$parameter</parameter></entry>\n";
479 output_highlight
($args{'parameters'}{$parameter});
480 print " </entry></row>\n";
482 print " </tbody></tgroup></informaltable>\n";
484 print " <para>\n None\n </para>\n";
486 # print "</simplesect>\n";
488 # print out each section
490 foreach $section (@
{$args{'sectionlist'}}) {
491 print "<simplesect>\n <title>$section</title>\n";
492 # print "<para>\n$section\n";
493 if ($section =~ m/EXAMPLE/i) {
494 print "<example><programlisting>\n";
498 output_highlight
($args{'sections'}{$section});
501 if ($section =~ m/EXAMPLE/i) {
502 print "</programlisting></example>\n";
505 print " </simplesect>\n";
508 print "</sect2>\n\n";
515 my ($parameter, $section);
518 print ".TH \"$args{'module'}\" \"$args{'function'}\" \"25 May 1998\" \"API Manual\" LINUX\n";
520 print ".SH Function\n";
522 print ".I \"".$args{'functiontype'}."\"\n";
523 print ".B \"".$args{'function'}."\"\n";
526 foreach $parameter (@
{$args{'parameterlist'}}) {
527 print ".I \"".$args{'parametertypes'}{$parameter}."\"\n.B \"".$parameter."\"\n";
528 if ($count != $#{$args{'parameterlist'}}) {
535 print ".SH Arguments\n";
536 foreach $parameter (@
{$args{'parameterlist'}}) {
537 print ".IP \"".$args{'parametertypes'}{$parameter}." ".$parameter."\" 12\n";
538 output_highlight
($args{'parameters'}{$parameter});
540 foreach $section (@
{$args{'sectionlist'}}) {
541 print ".SH \"$section\"\n";
542 output_highlight
($args{'sections'}{$section});
546 sub output_intro_man
{
548 my ($parameter, $section);
551 print ".TH \"$args{'module'}\" \"$args{'module'}\" \"25 May 1998\" \"API Manual\" LINUX\n";
553 foreach $section (@
{$args{'sectionlist'}}) {
554 print ".SH \"$section\"\n";
555 output_highlight
($args{'sections'}{$section});
563 my ($parameter, $section);
565 print "Function = ".$args{'function'}."\n";
566 print " return type: ".$args{'functiontype'}."\n\n";
567 foreach $parameter (@
{$args{'parameterlist'}}) {
568 print " ".$args{'parametertypes'}{$parameter}." ".$parameter."\n";
569 print " -> ".$args{'parameters'}{$parameter}."\n";
571 foreach $section (@
{$args{'sectionlist'}}) {
572 print " $section:\n";
574 output_highlight
($args{'sections'}{$section});
578 sub output_intro_text
{
580 my ($parameter, $section);
582 foreach $section (@
{$args{'sectionlist'}}) {
583 print " $section:\n";
585 output_highlight
($args{'sections'}{$section});
590 # generic output function - calls the right one based
591 # on current output mode.
592 sub output_function
{
594 eval "output_".$output_mode."(\@_);";
598 # generic output function - calls the right one based
599 # on current output mode.
602 eval "output_intro_".$output_mode."(\@_);";
607 # takes a function prototype and spits out all the details
608 # stored in the global arrays/hsahes.
610 my $prototype = shift @_;
612 $prototype =~ s/^static+ //;
613 $prototype =~ s/^extern+ //;
614 $prototype =~ s/^inline+ //;
615 $prototype =~ s/^__inline__+ //;
617 if ($prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\)]*)\)/ ||
618 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\)]*)\)/ ||
619 $prototype =~ m/^(\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\)]*)\)/ ||
620 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\)]*)\)/ ||
621 $prototype =~ m/^(\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\)]*)\)/) {
626 # print STDERR "ARGS = '$args'\n";
628 foreach $arg (split ',', $args) {
629 # strip leading/trailing spaces
632 # print STDERR "SCAN ARG: '$arg'\n";
633 @args = split('\s', $arg);
635 # print STDERR " -> @args\n";
637 # print STDERR " -> @args\n";
638 if ($param =~ m/^(\*+)(.*)/) {
642 $type = join " ", @args;
644 if ($type eq "" && $param eq "...")
648 $parameters{"..."} = "variable arguments";
654 $parameters{void
} = "no arguments";
656 if ($parameters{$param} eq "") {
657 $parameters{$param} = "-- undescribed --";
658 print STDERR
"Warning($lineno): Function parameter '$param' not described in '$function_name'\n";
661 push @parameterlist, $param;
662 $parametertypes{$param} = $type;
663 # print STDERR "param = '$param', type = '$type'\n";
666 print STDERR
"Error($lineno): cannot understand prototype: '$prototype'\n";
670 if ($function_only==0 ||
671 ( $function_only == 1 && defined($function_table{$function_name})) ||
672 ( $function_only == 2 && !defined($function_table{$function_name})))
674 output_function
({'function' => $function_name,
675 'module' => $modulename,
676 'functiontype' => $return_type,
677 'parameterlist' => \
@parameterlist,
678 'parameters' => \
%parameters,
679 'parametertypes' => \
%parametertypes,
680 'sectionlist' => \
@sectionlist,
681 'sections' => \
%sections,
682 'purpose' => $function_purpose
687 ######################################################################
691 # 1 - looking for function name
692 # 2 - scanning field start.
693 # 3 - scanning prototype.
697 $doc_special = "\@\%\$\&";
699 $doc_start = "^/\\*\\*\$";
701 $doc_com = "\\s*\\*\\s*";
702 $doc_func = $doc_com."(\\w+):?";
703 $doc_sect = $doc_com."([".$doc_special."]?[\\w ]+):(.*)";
704 $doc_content = $doc_com."(.*)";
705 $doc_block = $doc_com."DOC:\\s*(.*)?";
714 $section_default = "Description"; # default section
715 $section_intro = "Introduction";
716 $section = $section_default;
719 foreach $file (@ARGV) {
720 if (!open(IN
,"<$file")) {
721 print STDERR
"Error: Cannot open file $file\n";
729 $state = 1; # next line is always the function name
731 } elsif ($state == 1) { # this line is the function name (always)
736 $section = $section_intro;
741 elsif (/$doc_func/o) {
745 $function_purpose = $1;
747 $function_purpose = "";
750 print STDERR
"Info($lineno): Scanning doc for $function\n";
753 print STDERR
"WARN($lineno): Cannot understand $_ on line $lineno",
754 " - I thought it was a doc line\n";
757 } elsif ($state == 2) { # look for head: lines, and include content
762 if ($contents ne "") {
763 $contents =~ s/\&/\\\\\\amp;/g;
764 $contents =~ s/\</\\\\\\lt;/g;
765 $contents =~ s/\>/\\\\\\gt;/g;
766 dump_section
($section, $contents);
767 $section = $section_default;
770 $contents = $newcontents;
771 if ($contents ne "") {
774 $section = $newsection;
775 } elsif (/$doc_end/) {
777 if ($contents ne "") {
778 $contents =~ s/\&/\\\\\\amp;/g;
779 $contents =~ s/\</\\\\\\lt;/g;
780 $contents =~ s/\>/\\\\\\gt;/g;
781 dump_section
($section, $contents);
782 $section = $section_default;
786 # print STDERR "end of doc comment, looking for prototype\n";
789 } elsif (/$doc_content/) {
790 # miguel-style comment kludge, look for blank lines after
791 # @parameter line to signify start of description
792 if ($1 eq "" && $section =~ m/^@/) {
793 $contents =~ s/\&/\\\\\\amp;/g;
794 $contents =~ s/\</\\\\\\lt;/g;
795 $contents =~ s/\>/\\\\\\gt;/g;
796 dump_section
($section, $contents);
797 $section = $section_default;
800 $contents .= $1."\n";
803 # i dont know - bad line? ignore.
804 print STDERR
"WARNING($lineno): bad line: $_";
806 } elsif ($state == 3) { # scanning for function { (end of prototype)
807 if (m
#\s*/\*\s+MACDOC\s*#io) {
814 $prototype =~ s@
/\*.*?\*/@
@gos; # strip comments.
815 $prototype =~ s@
[\r\n]+@
@gos; # strip newlines/cr's.
816 $prototype =~ s@
^ +@
@gos; # strip leading spaces
817 dump_function
($prototype);
822 %parametertypes = ();
830 } elsif ($state == 4) {
831 # Documentation block
833 dump_section
($section, $contents);
834 output_intro
({'sectionlist' => \
@sectionlist,
835 'sections' => \
%sections });
840 %parametertypes = ();
846 $section = $section_intro;
853 dump_section
($section, $contents);
854 output_intro
({'sectionlist' => \
@sectionlist,
855 'sections' => \
%sections });
860 %parametertypes = ();
867 elsif (/$doc_content/)
871 $contents .= $blankline;
875 $contents .= $1 . "\n";