3 ## Copyright (c) 1998 Michael Zucchi, All Rights Reserved ##
4 ## Copyright (C) 2000 Tim Waugh <twaugh@redhat.com> ##
6 ## This software falls under the GNU General Public License. ##
7 ## Please read the COPYING file for more information ##
10 # This will read a 'c' file and scan for embedded comments in the
11 # style of gnome comments (+minor extensions - see below).
14 # Note: This only supports 'c'.
17 # kerneldoc [ -docbook | -html | -text | -man ]
18 # [ -function funcname [ -function funcname ...] ] c file(s)s > outputfile
20 # [ -nofunction funcname [ -function funcname ...] ] c file(s)s > outputfile
22 # Set output format using one of -docbook -html -text or -man. Default is man.
25 # If set, then only generate documentation for the given function(s). All
26 # other functions are ignored.
28 # -nofunction funcname
29 # If set, then only generate documentation for the other function(s). All
30 # other functions are ignored. Cannot be used with -function together
31 # (yes thats a bug - perl hackers can fix it 8))
33 # c files - list of 'c' files to process
35 # All output goes to stdout, with errors to stderr.
39 # In the following table, (...)? signifies optional structure.
40 # (...)* signifies 0 or more structure elements
42 # * function_name(:)? (- short description)?
43 # (* @parameterx: (description of parameter x)?)*
45 # * (Description:)? (Description of function)?
46 # * (section header: (section description)? )*
49 # So .. the trivial example would be:
55 # If the Description: header tag is ommitted, then there must be a blank line
56 # after the last parameter specification.
59 # * my_function - does my stuff
60 # * @my_arg: its mine damnit
62 # * Does my stuff explained.
67 # * my_function - does my stuff
68 # * @my_arg: its mine damnit
69 # * Description: Does my stuff explained.
73 # All descriptions can be multiline, apart from the short function description.
75 # All descriptive text is further processed, scanning for the following special
76 # patterns, which are highlighted appropriately.
78 # 'funcname()' - function
79 # '$ENVVAR' - environmental variable
80 # '&struct_name' - name of a structure (up to two words including 'struct')
81 # '@parameter' - name of a parameter
82 # '%CONST' - name of a constant.
84 # match expressions used to find embedded type information
85 $type_constant = "\\\%([-_\\w]+)";
86 $type_func = "(\\w+)\\(\\)";
87 $type_param = "\\\@(\\w+)";
88 $type_struct = "\\\&((struct\\s*)?[_\\w]+)";
89 $type_env = "(\\\$\\w+)";
92 # Output conversion substitutions.
93 # One for each output format
95 # these work fairly well
96 %highlights_html = ( $type_constant, "<i>\$1</i>",
97 $type_func, "<b>\$1</b>",
98 $type_struct, "<i>\$1</i>",
99 $type_param, "<tt><b>\$1</b></tt>" );
100 $blankline_html = "<p>";
102 # sgml, docbook format
103 %highlights_sgml = ( "([^=])\\\"([^\\\"<]+)\\\"", "\$1<quote>\$2</quote>",
104 $type_constant, "<constant>\$1</constant>",
105 $type_func, "<function>\$1</function>",
106 $type_struct, "<structname>\$1</structname>",
107 $type_env, "<envar>\$1</envar>",
108 $type_param, "<parameter>\$1</parameter>" );
109 $blankline_sgml = "</para><para>\n";
111 # gnome, docbook format
112 %highlights_gnome = ( $type_constant, "<replaceable class=\"option\">\$1</replaceable>",
113 $type_func, "<function>\$1</function>",
114 $type_struct, "<structname>\$1</structname>",
115 $type_env, "<envar>\$1</envar>",
116 $type_param, "<parameter>\$1</parameter>" );
117 $blankline_gnome = "</para><para>\n";
119 # these are pretty rough
120 %highlights_man = ( $type_constant, "\$1",
121 $type_func, "\\\\fB\$1\\\\fP",
122 $type_struct, "\\\\fI\$1\\\\fP",
123 $type_param, "\\\\fI\$1\\\\fP" );
127 %highlights_text = ( $type_constant, "\$1",
130 $type_param, "\$1" );
131 $blankline_text = "";
135 print "Usage: $0 [ -v ] [ -docbook | -html | -text | -man ]\n";
136 print " [ -function funcname [ -function funcname ...] ]\n";
137 print " [ -nofunction funcname [ -nofunction funcname ...] ]\n";
138 print " c source file(s) > outputfile\n";
148 $output_mode = "man";
149 %highlights = %highlights_man;
150 $blankline = $blankline_man;
151 $modulename = "API Documentation";
153 while ($ARGV[0] =~ m/^-(.*)/) {
155 if ($cmd eq "-html") {
156 $output_mode = "html";
157 %highlights = %highlights_html;
158 $blankline = $blankline_html;
159 } elsif ($cmd eq "-man") {
160 $output_mode = "man";
161 %highlights = %highlights_man;
162 $blankline = $blankline_man;
163 } elsif ($cmd eq "-text") {
164 $output_mode = "text";
165 %highlights = %highlights_text;
166 $blankline = $blankline_text;
167 } elsif ($cmd eq "-docbook") {
168 $output_mode = "sgml";
169 %highlights = %highlights_sgml;
170 $blankline = $blankline_sgml;
171 } elsif ($cmd eq "-gnome") {
172 $output_mode = "gnome";
173 %highlights = %highlights_gnome;
174 $blankline = $blankline_gnome;
175 } elsif ($cmd eq "-module") { # not needed for sgml, inherits from calling document
176 $modulename = shift @ARGV;
177 } elsif ($cmd eq "-function") { # to only output specific functions
179 $function = shift @ARGV;
180 $function_table{$function} = 1;
181 } elsif ($cmd eq "-nofunction") { # to only output specific functions
183 $function = shift @ARGV;
184 $function_table{$function} = 1;
185 } elsif ($cmd eq "-v") {
187 } elsif (($cmd eq "-h") || ($cmd eq "--help")) {
193 # generate a sequence of code that will splice in highlighting information
194 # using the s// operator.
196 foreach $pattern (keys %highlights) {
197 # print "scanning pattern $pattern ($highlights{$pattern})\n";
198 $dohighlight .= "\$contents =~ s:$pattern:$highlights{$pattern}:gs;\n";
202 # dumps section contents to arrays/hashes intended for that purpose.
206 my $contents = join "\n", @_;
208 if ($name =~ m/$type_constant/) {
210 # print STDERR "constant section '$1' = '$contents'\n";
211 $constants{$name} = $contents;
212 } elsif ($name =~ m/$type_param/) {
213 # print STDERR "parameter def '$1' = '$contents'\n";
215 $parameters{$name} = $contents;
217 # print STDERR "other section '$name' = '$contents'\n";
218 $sections{$name} = $contents;
219 push @sectionlist, $name;
226 # parameters, a hash.
227 # function => "function name"
228 # parameterlist => @list of parameters
229 # parameters => %parameter descriptions
230 # sectionlist => @list of sections
231 # sections => %descriont descriptions
234 sub output_highlight
{
235 my $contents = join "\n", @_;
239 foreach $line (split "\n", $contents) {
241 print $lineprefix, $blankline;
243 $line =~ s/\\\\\\/\&/g;
244 print $lineprefix, $line;
254 my ($parameter, $section);
256 print "<h2>Function</h2>\n";
258 print "<i>".$args{'functiontype'}."</i>\n";
259 print "<b>".$args{'function'}."</b>\n";
262 foreach $parameter (@
{$args{'parameterlist'}}) {
263 $type = $args{'parametertypes'}{$parameter};
264 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
265 # pointer-to-function
266 print "<i>$1</i><b>$parameter</b>) <i>($2)</i>";
268 print "<i>".$type."</i> <b>".$parameter."</b>";
270 if ($count != $#{$args{'parameterlist'}}) {
277 print "<h3>Arguments</h3>\n";
279 foreach $parameter (@
{$args{'parameterlist'}}) {
280 print "<dt><b>".$parameter."</b>\n";
282 output_highlight
($args{'parameters'}{$parameter});
285 foreach $section (@
{$args{'sectionlist'}}) {
286 print "<h3>$section</h3>\n";
287 print "<blockquote>\n";
288 output_highlight
($args{'sections'}{$section});
289 print "</blockquote>\n";
296 sub output_intro_html
{
298 my ($parameter, $section);
301 foreach $section (@
{$args{'sectionlist'}}) {
302 print "<h3>$section</h3>\n";
304 output_highlight
($args{'sections'}{$section});
312 # output in sgml DocBook
315 my ($parameter, $section);
319 $id = "API-".$args{'function'};
320 $id =~ s/[^A-Za-z0-9]/-/g;
322 print "<refentry>\n";
324 print "<refentrytitle><phrase id=\"$id\">".$args{'function'}."</phrase></refentrytitle>\n";
325 print "</refmeta>\n";
326 print "<refnamediv>\n";
327 print " <refname>".$args{'function'}."</refname>\n";
328 print " <refpurpose>\n";
330 output_highlight
($args{'purpose'});
331 print " </refpurpose>\n";
332 print "</refnamediv>\n";
334 print "<refsynopsisdiv>\n";
335 print " <title>Synopsis</title>\n";
336 print " <funcsynopsis>\n";
337 print " <funcdef>".$args{'functiontype'}." ";
338 print "<function>".$args{'function'}." ";
339 print "</function></funcdef>\n";
341 # print "<refsect1>\n";
342 # print " <title>Synopsis</title>\n";
343 # print " <funcsynopsis>\n";
344 # print " <funcdef>".$args{'functiontype'}." ";
345 # print "<function>".$args{'function'}." ";
346 # print "</function></funcdef>\n";
349 if ($#{$args{'parameterlist'}} >= 0) {
350 foreach $parameter (@
{$args{'parameterlist'}}) {
351 $type = $args{'parametertypes'}{$parameter};
352 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
353 # pointer-to-function
354 print " <paramdef>$1<parameter>$parameter</parameter>)\n";
355 print " <funcparams>$2</funcparams></paramdef>\n";
357 print " <paramdef>".$type;
358 print " <parameter>$parameter</parameter></paramdef>\n";
364 print " </funcsynopsis>\n";
365 print "</refsynopsisdiv>\n";
366 # print "</refsect1>\n";
369 print "<refsect1>\n <title>Arguments</title>\n";
370 # print "<para>\nArguments\n";
371 if ($#{$args{'parameterlist'}} >= 0) {
372 print " <variablelist>\n";
373 foreach $parameter (@
{$args{'parameterlist'}}) {
374 print " <varlistentry>\n <term><parameter>$parameter</parameter></term>\n";
375 print " <listitem>\n <para>\n";
377 output_highlight
($args{'parameters'}{$parameter});
378 print " </para>\n </listitem>\n </varlistentry>\n";
380 print " </variablelist>\n";
382 print " <para>\n None\n </para>\n";
384 print "</refsect1>\n";
386 # print out each section
388 foreach $section (@
{$args{'sectionlist'}}) {
389 print "<refsect1>\n <title>$section</title>\n <para>\n";
390 # print "<para>\n$section\n";
391 if ($section =~ m/EXAMPLE/i) {
392 print "<example><para>\n";
394 output_highlight
($args{'sections'}{$section});
396 if ($section =~ m/EXAMPLE/i) {
397 print "</para></example>\n";
399 print " </para>\n</refsect1>\n";
402 print "</refentry>\n\n";
405 # output in sgml DocBook
406 sub output_intro_sgml
{
408 my ($parameter, $section);
412 $id = $args{'module'};
413 $id =~ s/[^A-Za-z0-9]/-/g;
415 # print out each section
417 foreach $section (@
{$args{'sectionlist'}}) {
418 print "<refsect1>\n <title>$section</title>\n <para>\n";
419 # print "<para>\n$section\n";
420 if ($section =~ m/EXAMPLE/i) {
421 print "<example><para>\n";
423 output_highlight
($args{'sections'}{$section});
425 if ($section =~ m/EXAMPLE/i) {
426 print "</para></example>\n";
428 print " </para>\n</refsect1>\n";
434 # output in sgml DocBook
437 my ($parameter, $section);
441 $id = $args{'module'}."-".$args{'function'};
442 $id =~ s/[^A-Za-z0-9]/-/g;
445 print " <title id=\"$id\">".$args{'function'}."</title>\n";
447 # print "<simplesect>\n";
448 # print " <title>Synopsis</title>\n";
449 print " <funcsynopsis>\n";
450 print " <funcdef>".$args{'functiontype'}." ";
451 print "<function>".$args{'function'}." ";
452 print "</function></funcdef>\n";
455 if ($#{$args{'parameterlist'}} >= 0) {
456 foreach $parameter (@
{$args{'parameterlist'}}) {
457 $type = $args{'parametertypes'}{$parameter};
458 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
459 # pointer-to-function
460 print " <paramdef>$1 <parameter>$parameter</parameter>)\n";
461 print " <funcparams>$2</funcparams></paramdef>\n";
463 print " <paramdef>".$type;
464 print " <parameter>$parameter</parameter></paramdef>\n";
470 print " </funcsynopsis>\n";
471 # print "</simplesect>\n";
472 # print "</refsect1>\n";
475 # print "<simplesect>\n <title>Arguments</title>\n";
476 # if ($#{$args{'parameterlist'}} >= 0) {
477 # print " <variablelist>\n";
478 # foreach $parameter (@{$args{'parameterlist'}}) {
479 # print " <varlistentry>\n <term><parameter>$parameter</parameter></term>\n";
480 # print " <listitem>\n <para>\n";
482 # output_highlight($args{'parameters'}{$parameter});
483 # print " </para>\n </listitem>\n </varlistentry>\n";
485 # print " </variablelist>\n";
487 # print " <para>\n None\n </para>\n";
489 # print "</simplesect>\n";
491 # print "<simplesect>\n <title>Arguments</title>\n";
492 if ($#{$args{'parameterlist'}} >= 0) {
493 print " <informaltable pgwide=\"1\" frame=\"none\" role=\"params\">\n";
494 print "<tgroup cols=\"2\">\n";
495 print "<colspec colwidth=\"2*\">\n";
496 print "<colspec colwidth=\"8*\">\n";
498 foreach $parameter (@
{$args{'parameterlist'}}) {
499 print " <row><entry align=\"right\"><parameter>$parameter</parameter></entry>\n";
502 output_highlight
($args{'parameters'}{$parameter});
503 print " </entry></row>\n";
505 print " </tbody></tgroup></informaltable>\n";
507 print " <para>\n None\n </para>\n";
509 # print "</simplesect>\n";
511 # print out each section
513 foreach $section (@
{$args{'sectionlist'}}) {
514 print "<simplesect>\n <title>$section</title>\n";
515 # print "<para>\n$section\n";
516 if ($section =~ m/EXAMPLE/i) {
517 print "<example><programlisting>\n";
521 output_highlight
($args{'sections'}{$section});
524 if ($section =~ m/EXAMPLE/i) {
525 print "</programlisting></example>\n";
528 print " </simplesect>\n";
531 print "</sect2>\n\n";
538 my ($parameter, $section);
541 print ".TH \"$args{'module'}\" 4 \"$args{'function'}\" \"25 May 1998\" \"API Manual\" LINUX\n";
544 print $args{'function'}." \\- ".$args{'purpose'}."\n";
546 print ".SH SYNOPSIS\n";
547 print ".B \"".$args{'functiontype'}."\" ".$args{'function'}."\n";
551 foreach $parameter (@
{$args{'parameterlist'}}) {
552 if ($count == $#{$args{'parameterlist'}}) {
555 $type = $args{'parametertypes'}{$parameter};
556 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
557 # pointer-to-function
558 print ".BI \"".$parenth.$1."\" ".$parameter." \") (".$2.")".$post."\"\n";
560 $type =~ s/([^\*])$/\1 /;
561 print ".BI \"".$parenth.$type."\" ".$parameter." \"".$post."\"\n";
567 print ".SH Arguments\n";
568 foreach $parameter (@
{$args{'parameterlist'}}) {
569 print ".IP \"".$parameter."\" 12\n";
570 output_highlight
($args{'parameters'}{$parameter});
572 foreach $section (@
{$args{'sectionlist'}}) {
573 print ".SH \"$section\"\n";
574 output_highlight
($args{'sections'}{$section});
578 sub output_intro_man
{
580 my ($parameter, $section);
583 print ".TH \"$args{'module'}\" 4 \"$args{'module'}\" \"25 May 1998\" \"API Manual\" LINUX\n";
585 foreach $section (@
{$args{'sectionlist'}}) {
586 print ".SH \"$section\"\n";
587 output_highlight
($args{'sections'}{$section});
595 my ($parameter, $section);
597 print "Function:\n\n";
598 $start=$args{'functiontype'}." ".$args{'function'}." (";
601 foreach $parameter (@
{$args{'parameterlist'}}) {
602 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
603 # pointer-to-function
604 print $1.$parameter.") (".$2;
606 print $type." ".$parameter;
608 if ($count != $#{$args{'parameterlist'}}) {
611 print " " x
length($start);
617 print "Arguments:\n\n";
618 foreach $parameter (@
{$args{'parameterlist'}}) {
619 print $parameter."\n\t".$args{'parameters'}{$parameter}."\n";
621 foreach $section (@
{$args{'sectionlist'}}) {
622 print "$section:\n\n";
623 output_highlight
($args{'sections'}{$section});
628 sub output_intro_text
{
630 my ($parameter, $section);
632 foreach $section (@
{$args{'sectionlist'}}) {
633 print " $section:\n";
635 output_highlight
($args{'sections'}{$section});
640 # generic output function - calls the right one based
641 # on current output mode.
642 sub output_function
{
644 eval "output_".$output_mode."(\@_);";
648 # generic output function - calls the right one based
649 # on current output mode.
652 eval "output_intro_".$output_mode."(\@_);";
657 # takes a function prototype and spits out all the details
658 # stored in the global arrays/hsahes.
660 my $prototype = shift @_;
662 $prototype =~ s/^static+ //;
663 $prototype =~ s/^extern+ //;
664 $prototype =~ s/^inline+ //;
665 $prototype =~ s/^__inline__+ //;
667 if ($prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
668 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
669 $prototype =~ m/^(\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
670 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
671 $prototype =~ m/^(\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/) {
676 # allow for up to fours args to function pointers
677 $args =~ s/(\([^\),]+),/\1#/g;
678 $args =~ s/(\([^\),]+),/\1#/g;
679 $args =~ s/(\([^\),]+),/\1#/g;
680 # print STDERR "ARGS = '$args'\n";
682 foreach $arg (split ',', $args) {
683 # strip leading/trailing spaces
688 # pointer-to-function
690 $arg =~ m/[^\(]+\(\*([^\)]+)\)/;
693 $type =~ s/([^\(]+\(\*)$param/\1/;
695 # evil magic to get fixed array parameters to work
696 $arg =~ s/(.+\s+)(.+)\[.*/\1* \2/;
697 # print STDERR "SCAN ARG: '$arg'\n";
698 @args = split('\s', $arg);
700 # print STDERR " -> @args\n";
702 # print STDERR " -> @args\n";
703 if ($param =~ m/^(\*+)(.*)/) {
707 $type = join " ", @args;
710 if ($type eq "" && $param eq "...")
714 $parameters{"..."} = "variable arguments";
720 $parameters{void
} = "no arguments";
722 if ($parameters{$param} eq "") {
723 $parameters{$param} = "-- undescribed --";
724 print STDERR
"Warning($file:$lineno): Function parameter '$param' not described in '$function_name'\n";
727 push @parameterlist, $param;
728 $parametertypes{$param} = $type;
729 # print STDERR "param = '$param', type = '$type'\n";
732 print STDERR
"Error($lineno): cannot understand prototype: '$prototype'\n";
736 if ($function_only==0 ||
737 ( $function_only == 1 && defined($function_table{$function_name})) ||
738 ( $function_only == 2 && !defined($function_table{$function_name})))
740 output_function
({'function' => $function_name,
741 'module' => $modulename,
742 'functiontype' => $return_type,
743 'parameterlist' => \
@parameterlist,
744 'parameters' => \
%parameters,
745 'parametertypes' => \
%parametertypes,
746 'sectionlist' => \
@sectionlist,
747 'sections' => \
%sections,
748 'purpose' => $function_purpose
753 ######################################################################
757 # 1 - looking for function name
758 # 2 - scanning field start.
759 # 3 - scanning prototype.
763 $doc_special = "\@\%\$\&";
765 $doc_start = "^/\\*\\*\$";
767 $doc_com = "\\s*\\*\\s*";
768 $doc_func = $doc_com."(\\w+):?";
769 $doc_sect = $doc_com."([".$doc_special."]?[\\w ]+):(.*)";
770 $doc_content = $doc_com."(.*)";
771 $doc_block = $doc_com."DOC:\\s*(.*)?";
780 $section_default = "Description"; # default section
781 $section_intro = "Introduction";
782 $section = $section_default;
785 foreach $file (@ARGV) {
787 if (!open(IN
,"<$file")) {
788 print STDERR
"Error: Cannot open file $file\n";
796 $state = 1; # next line is always the function name
798 } elsif ($state == 1) { # this line is the function name (always)
803 $section = $section_intro;
808 elsif (/$doc_func/o) {
812 $function_purpose = $1;
814 $function_purpose = "";
817 print STDERR
"Info($lineno): Scanning doc for $function\n";
820 print STDERR
"WARN($lineno): Cannot understand $_ on line $lineno",
821 " - I thought it was a doc line\n";
824 } elsif ($state == 2) { # look for head: lines, and include content
829 if ($contents ne "") {
830 $contents =~ s/\&/\\\\\\amp;/g;
831 $contents =~ s/\</\\\\\\lt;/g;
832 $contents =~ s/\>/\\\\\\gt;/g;
833 dump_section
($section, $contents);
834 $section = $section_default;
837 $contents = $newcontents;
838 if ($contents ne "") {
841 $section = $newsection;
842 } elsif (/$doc_end/) {
844 if ($contents ne "") {
845 $contents =~ s/\&/\\\\\\amp;/g;
846 $contents =~ s/\</\\\\\\lt;/g;
847 $contents =~ s/\>/\\\\\\gt;/g;
848 dump_section
($section, $contents);
849 $section = $section_default;
853 # print STDERR "end of doc comment, looking for prototype\n";
856 } elsif (/$doc_content/) {
857 # miguel-style comment kludge, look for blank lines after
858 # @parameter line to signify start of description
859 if ($1 eq "" && $section =~ m/^@/) {
860 $contents =~ s/\&/\\\\\\amp;/g;
861 $contents =~ s/\</\\\\\\lt;/g;
862 $contents =~ s/\>/\\\\\\gt;/g;
863 dump_section
($section, $contents);
864 $section = $section_default;
867 $contents .= $1."\n";
870 # i dont know - bad line? ignore.
871 print STDERR
"WARNING($lineno): bad line: $_";
873 } elsif ($state == 3) { # scanning for function { (end of prototype)
874 if (m
#\s*/\*\s+MACDOC\s*#io) {
881 $prototype =~ s@
/\*.*?\*/@
@gos; # strip comments.
882 $prototype =~ s@
[\r\n]+@
@gos; # strip newlines/cr's.
883 $prototype =~ s@
^ +@
@gos; # strip leading spaces
884 dump_function
($prototype);
889 %parametertypes = ();
897 } elsif ($state == 4) {
898 # Documentation block
900 dump_section
($section, $contents);
901 output_intro
({'sectionlist' => \
@sectionlist,
902 'sections' => \
%sections });
907 %parametertypes = ();
913 $section = $section_intro;
920 dump_section
($section, $contents);
921 output_intro
({'sectionlist' => \
@sectionlist,
922 'sections' => \
%sections });
927 %parametertypes = ();
934 elsif (/$doc_content/)
938 $contents .= $blankline;
942 $contents .= $1 . "\n";