TX39 fixes.
[linux-2.6/linux-mips.git] / scripts / kernel-doc
blobb586ca4cacea6fa3646258fc1297f64da1d806b3
1 #!/usr/bin/perl -w
3 use strict;
5 ## Copyright (c) 1998 Michael Zucchi, All Rights Reserved ##
6 ## Copyright (C) 2000, 1 Tim Waugh <twaugh@redhat.com> ##
7 ## Copyright (C) 2001 Simon Huggins ##
8 ## ##
9 ## #define enhancements by Armin Kuster <akuster@mvista.com> ##
10 ## Copyright (c) 2000 MontaVista Software, Inc. ##
11 ## ##
12 ## This software falls under the GNU General Public License. ##
13 ## Please read the COPYING file for more information ##
15 # w.o. 03-11-2000: added the '-filelist' option.
17 # 18/01/2001 - Cleanups
18 # Functions prototyped as foo(void) same as foo()
19 # Stop eval'ing where we don't need to.
20 # -- huggie@earth.li
22 # 27/06/2001 - Allowed whitespace after initial "/**" and
23 # allowed comments before function declarations.
24 # -- Christian Kreibich <ck@whoop.org>
26 # Still to do:
27 # - add perldoc documentation
28 # - Look more closely at some of the scarier bits :)
30 # 26/05/2001 - Support for separate source and object trees.
31 # Return error code.
32 # Keith Owens <kaos@ocs.com.au>
34 # 23/09/2001 - Added support for typedefs, structs, enums and unions
35 # Support for Context section; can be terminated using empty line
36 # Small fixes (like spaces vs. \s in regex)
37 # -- Tim Jansen <tim@tjansen.de>
41 # This will read a 'c' file and scan for embedded comments in the
42 # style of gnome comments (+minor extensions - see below).
45 # Note: This only supports 'c'.
47 # usage:
48 # kerneldoc [ -docbook | -html | -text | -man ]
49 # [ -function funcname [ -function funcname ...] ] c file(s)s > outputfile
50 # or
51 # [ -nofunction funcname [ -function funcname ...] ] c file(s)s > outputfile
53 # Set output format using one of -docbook -html -text or -man. Default is man.
55 # -function funcname
56 # If set, then only generate documentation for the given function(s). All
57 # other functions are ignored.
59 # -nofunction funcname
60 # If set, then only generate documentation for the other function(s). All
61 # other functions are ignored. Cannot be used with -function together
62 # (yes thats a bug - perl hackers can fix it 8))
64 # c files - list of 'c' files to process
66 # All output goes to stdout, with errors to stderr.
69 # format of comments.
70 # In the following table, (...)? signifies optional structure.
71 # (...)* signifies 0 or more structure elements
72 # /**
73 # * function_name(:)? (- short description)?
74 # (* @parameterx: (description of parameter x)?)*
75 # (* a blank line)?
76 # * (Description:)? (Description of function)?
77 # * (section header: (section description)? )*
78 # (*)?*/
80 # So .. the trivial example would be:
82 # /**
83 # * my_function
84 # **/
86 # If the Description: header tag is ommitted, then there must be a blank line
87 # after the last parameter specification.
88 # e.g.
89 # /**
90 # * my_function - does my stuff
91 # * @my_arg: its mine damnit
92 # *
93 # * Does my stuff explained.
94 # */
96 # or, could also use:
97 # /**
98 # * my_function - does my stuff
99 # * @my_arg: its mine damnit
100 # * Description: Does my stuff explained.
101 # */
102 # etc.
104 # Beside functions you can also write documentation for structs, unions,
105 # enums and typedefs. Instead of the function name you must write the name
106 # of the declaration; the struct/union/enum/typedef must always precede
107 # the name. Nesting of declarations is not supported.
108 # Use the argument mechanism to document members or constants. In
109 # structs and unions you must declare one member per declaration
110 # (comma-separated members are not allowed - the parser does not support
111 # this).
112 # e.g.
113 # /**
114 # * struct my_struct - short description
115 # * @a: first member
116 # * @b: second member
117 # *
118 # * Longer description
119 # */
120 # struct my_struct {
121 # int a;
122 # int b;
123 # };
125 # All descriptions can be multiline, except the short function description.
127 # You can also add additional sections. When documenting kernel functions you
128 # should document the "Context:" of the function, e.g. whether the functions
129 # can be called form interrupts. Unlike other sections you can end it with an
130 # empty line.
131 # Example-sections should contain the string EXAMPLE so that they are marked
132 # appropriately in DocBook.
134 # Example:
135 # /**
136 # * user_function - function that can only be called in user context
137 # * @a: some argument
138 # * Context: !in_interrupt()
139 # *
140 # * Some description
141 # * Example:
142 # * user_function(22);
143 # */
144 # ...
147 # All descriptive text is further processed, scanning for the following special
148 # patterns, which are highlighted appropriately.
150 # 'funcname()' - function
151 # '$ENVVAR' - environmental variable
152 # '&struct_name' - name of a structure (up to two words including 'struct')
153 # '@parameter' - name of a parameter
154 # '%CONST' - name of a constant.
156 my $errors = 0;
158 # match expressions used to find embedded type information
159 my $type_constant = '\%([-_\w]+)';
160 my $type_func = '(\w+)\(\)';
161 my $type_param = '\@(\w+)';
162 my $type_struct = '\&((struct\s*)?[_\w]+)';
163 my $type_env = '(\$\w+)';
165 # Output conversion substitutions.
166 # One for each output format
168 # these work fairly well
169 my %highlights_html = ( $type_constant, "<i>\$1</i>",
170 $type_func, "<b>\$1</b>",
171 $type_struct, "<i>\$1</i>",
172 $type_param, "<tt><b>\$1</b></tt>" );
173 my $blankline_html = "<p>";
175 # sgml, docbook format
176 my %highlights_sgml = ( "([^=])\\\"([^\\\"<]+)\\\"", "\$1<quote>\$2</quote>",
177 $type_constant, "<constant>\$1</constant>",
178 $type_func, "<function>\$1</function>",
179 $type_struct, "<structname>\$1</structname>",
180 $type_env, "<envar>\$1</envar>",
181 $type_param, "<parameter>\$1</parameter>" );
182 my $blankline_sgml = "</para><para>\n";
184 # gnome, docbook format
185 my %highlights_gnome = ( $type_constant, "<replaceable class=\"option\">\$1</replaceable>",
186 $type_func, "<function>\$1</function>",
187 $type_struct, "<structname>\$1</structname>",
188 $type_env, "<envar>\$1</envar>",
189 $type_param, "<parameter>\$1</parameter>" );
190 my $blankline_gnome = "</para><para>\n";
192 # these are pretty rough
193 my %highlights_man = ( $type_constant, "\$1",
194 $type_func, "\\\\fB\$1\\\\fP",
195 $type_struct, "\\\\fI\$1\\\\fP",
196 $type_param, "\\\\fI\$1\\\\fP" );
197 my $blankline_man = "";
199 # text-mode
200 my %highlights_text = ( $type_constant, "\$1",
201 $type_func, "\$1",
202 $type_struct, "\$1",
203 $type_param, "\$1" );
204 my $blankline_text = "";
207 sub usage {
208 print "Usage: $0 [ -v ] [ -docbook | -html | -text | -man ]\n";
209 print " [ -function funcname [ -function funcname ...] ]\n";
210 print " [ -nofunction funcname [ -nofunction funcname ...] ]\n";
211 print " c source file(s) > outputfile\n";
212 exit 1;
215 # read arguments
216 if ($#ARGV==-1) {
217 usage();
220 my $verbose = 0;
221 my $output_mode = "man";
222 my %highlights = %highlights_man;
223 my $blankline = $blankline_man;
224 my $modulename = "Kernel API";
225 my $function_only = 0;
226 my $man_date = ('January', 'February', 'March', 'April', 'May', 'June',
227 'July', 'August', 'September', 'October',
228 'November', 'December')[(localtime)[4]] .
229 " " . ((localtime)[5]+1900);
231 # Essentially these are globals
232 # They probably want to be tidied up made more localised or summat.
233 # CAVEAT EMPTOR! Some of the others I localised may not want to be which
234 # could cause "use of undefined value" or other bugs.
235 my ($function, %function_table,%parametertypes,$declaration_purpose);
236 my ($type,$declaration_name,$return_type);
237 my ($newsection,$newcontents,$prototype,$filelist, $brcount, %source_map);
239 # Generated docbook code is inserted in a template at a point where
240 # docbook v3.1 requires a non-zero sequence of RefEntry's; see:
241 # http://www.oasis-open.org/docbook/documentation/reference/html/refentry.html
242 # We keep track of number of generated entries and generate a dummy
243 # if needs be to ensure the expanded template can be postprocessed
244 # into html.
245 my $section_counter = 0;
247 my $lineprefix="";
249 # states
250 # 0 - normal code
251 # 1 - looking for function name
252 # 2 - scanning field start.
253 # 3 - scanning prototype.
254 # 4 - documentation block
255 my $state;
257 #declaration types: can be
258 # 'function', 'struct', 'union', 'enum', 'typedef'
259 my $decl_type;
261 my $doc_special = "\@\%\$\&";
263 my $doc_start = '^/\*\*\s*$'; # Allow whitespace at end of comment start.
264 my $doc_end = '\*/';
265 my $doc_com = '\s*\*\s*';
266 my $doc_decl = $doc_com.'(\w+)';
267 my $doc_sect = $doc_com.'(['.$doc_special.']?[\w ]+):(.*)';
268 my $doc_content = $doc_com.'(.*)';
269 my $doc_block = $doc_com.'DOC:\s*(.*)?';
271 my %constants;
272 my %parameterdescs;
273 my @parameterlist;
274 my %sections;
275 my @sectionlist;
277 my $contents = "";
278 my $section_default = "Description"; # default section
279 my $section_intro = "Introduction";
280 my $section = $section_default;
281 my $section_context = "Context";
283 my $undescribed = "-- undescribed --";
285 reset_state();
287 while ($ARGV[0] =~ m/^-(.*)/) {
288 my $cmd = shift @ARGV;
289 if ($cmd eq "-html") {
290 $output_mode = "html";
291 %highlights = %highlights_html;
292 $blankline = $blankline_html;
293 } elsif ($cmd eq "-man") {
294 $output_mode = "man";
295 %highlights = %highlights_man;
296 $blankline = $blankline_man;
297 } elsif ($cmd eq "-text") {
298 $output_mode = "text";
299 %highlights = %highlights_text;
300 $blankline = $blankline_text;
301 } elsif ($cmd eq "-docbook") {
302 $output_mode = "sgml";
303 %highlights = %highlights_sgml;
304 $blankline = $blankline_sgml;
305 } elsif ($cmd eq "-gnome") {
306 $output_mode = "gnome";
307 %highlights = %highlights_gnome;
308 $blankline = $blankline_gnome;
309 } elsif ($cmd eq "-module") { # not needed for sgml, inherits from calling document
310 $modulename = shift @ARGV;
311 } elsif ($cmd eq "-function") { # to only output specific functions
312 $function_only = 1;
313 $function = shift @ARGV;
314 $function_table{$function} = 1;
315 } elsif ($cmd eq "-nofunction") { # to only output specific functions
316 $function_only = 2;
317 $function = shift @ARGV;
318 $function_table{$function} = 1;
319 } elsif ($cmd eq "-v") {
320 $verbose = 1;
321 } elsif (($cmd eq "-h") || ($cmd eq "--help")) {
322 usage();
323 } elsif ($cmd eq '-filelist') {
324 $filelist = shift @ARGV;
329 # generate a sequence of code that will splice in highlighting information
330 # using the s// operator.
331 my $dohighlight = "";
332 foreach my $pattern (keys %highlights) {
333 # print "scanning pattern $pattern ($highlights{$pattern})\n";
334 $dohighlight .= "\$contents =~ s:$pattern:$highlights{$pattern}:gs;\n";
338 # dumps section contents to arrays/hashes intended for that purpose.
340 sub dump_section {
341 my $name = shift;
342 my $contents = join "\n", @_;
344 if ($name =~ m/$type_constant/) {
345 $name = $1;
346 # print STDERR "constant section '$1' = '$contents'\n";
347 $constants{$name} = $contents;
348 } elsif ($name =~ m/$type_param/) {
349 # print STDERR "parameter def '$1' = '$contents'\n";
350 $name = $1;
351 $parameterdescs{$name} = $contents;
352 } else {
353 # print STDERR "other section '$name' = '$contents'\n";
354 $sections{$name} = $contents;
355 push @sectionlist, $name;
360 # output function
362 # parameterdescs, a hash.
363 # function => "function name"
364 # parameterlist => @list of parameters
365 # parameterdescs => %parameter descriptions
366 # sectionlist => @list of sections
367 # sections => %descriont descriptions
370 sub output_highlight {
371 my $contents = join "\n",@_;
372 my $line;
374 # DEBUG
375 # if (!defined $contents) {
376 # use Carp;
377 # confess "output_highlight got called with no args?\n";
380 eval $dohighlight;
381 die $@ if $@;
382 foreach $line (split "\n", $contents) {
383 if ($line eq ""){
384 print $lineprefix, $blankline;
385 } else {
386 $line =~ s/\\\\\\/\&/g;
387 print $lineprefix, $line;
389 print "\n";
393 #output sections in html
394 sub output_section_html(%) {
395 my %args = %{$_[0]};
396 my $section;
398 foreach $section (@{$args{'sectionlist'}}) {
399 print "<h3>$section</h3>\n";
400 print "<blockquote>\n";
401 output_highlight($args{'sections'}{$section});
402 print "</blockquote>\n";
406 # output enum in html
407 sub output_enum_html(%) {
408 my %args = %{$_[0]};
409 my ($parameter);
410 my $count;
411 print "<h2>enum ".$args{'enum'}."</h2>\n";
413 print "<b>enum ".$args{'enum'}."</b> {<br>\n";
414 $count = 0;
415 foreach $parameter (@{$args{'parameterlist'}}) {
416 print " <b>".$parameter."</b>";
417 if ($count != $#{$args{'parameterlist'}}) {
418 $count++;
419 print ",\n";
421 print "<br>";
423 print "};<br>\n";
425 print "<h3>Constants</h3>\n";
426 print "<dl>\n";
427 foreach $parameter (@{$args{'parameterlist'}}) {
428 print "<dt><b>".$parameter."</b>\n";
429 print "<dd>";
430 output_highlight($args{'parameterdescs'}{$parameter});
432 print "</dl>\n";
433 output_section_html(@_);
434 print "<hr>\n";
437 # output tyepdef in html
438 sub output_typedef_html(%) {
439 my %args = %{$_[0]};
440 my ($parameter);
441 my $count;
442 print "<h2>typedef ".$args{'typedef'}."</h2>\n";
444 print "<b>typedef ".$args{'typedef'}."</b>\n";
445 output_section_html(@_);
446 print "<hr>\n";
449 # output struct in html
450 sub output_struct_html(%) {
451 my %args = %{$_[0]};
452 my ($parameter);
454 print "<h2>".$args{'type'}." ".$args{'struct'}."</h2>\n";
455 print "<b>".$args{'type'}." ".$args{'struct'}."</b> {<br>\n";
456 foreach $parameter (@{$args{'parameterlist'}}) {
457 ($args{'parameterdescs'}{$parameter} ne $undescribed) || next;
458 $type = $args{'parametertypes'}{$parameter};
459 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
460 # pointer-to-function
461 print " <i>$1</i><b>$parameter</b>) <i>($2)</i>;<br>\n";
462 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
463 print " <i>$1</i> <b>$parameter</b>$2;<br>\n";
464 } else {
465 print " <i>$type</i> <b>$parameter</b>;<br>\n";
468 print "};<br>\n";
470 print "<h3>Members</h3>\n";
471 print "<dl>\n";
472 foreach $parameter (@{$args{'parameterlist'}}) {
473 ($args{'parameterdescs'}{$parameter} ne $undescribed) || next;
474 print "<dt><b>".$parameter."</b>\n";
475 print "<dd>";
476 output_highlight($args{'parameterdescs'}{$parameter});
478 print "</dl>\n";
479 output_section_html(@_);
480 print "<hr>\n";
483 # output function in html
484 sub output_function_html(%) {
485 my %args = %{$_[0]};
486 my ($parameter, $section);
487 my $count;
488 print "<h2>Function</h2>\n";
490 print "<i>".$args{'functiontype'}."</i>\n";
491 print "<b>".$args{'function'}."</b>\n";
492 print "(";
493 $count = 0;
494 foreach $parameter (@{$args{'parameterlist'}}) {
495 $type = $args{'parametertypes'}{$parameter};
496 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
497 # pointer-to-function
498 print "<i>$1</i><b>$parameter</b>) <i>($2)</i>";
499 } else {
500 print "<i>".$type."</i> <b>".$parameter."</b>";
502 if ($count != $#{$args{'parameterlist'}}) {
503 $count++;
504 print ",\n";
507 print ")\n";
509 print "<h3>Arguments</h3>\n";
510 print "<dl>\n";
511 foreach $parameter (@{$args{'parameterlist'}}) {
512 ($args{'parameterdescs'}{$parameter} ne $undescribed) || next;
513 print "<dt><b>".$parameter."</b>\n";
514 print "<dd>";
515 output_highlight($args{'parameterdescs'}{$parameter});
517 print "</dl>\n";
518 output_section_html(@_);
519 print "<hr>\n";
522 # output intro in html
523 sub output_intro_html(%) {
524 my %args = %{$_[0]};
525 my ($parameter, $section);
526 my $count;
528 foreach $section (@{$args{'sectionlist'}}) {
529 print "<h3>$section</h3>\n";
530 print "<ul>\n";
531 output_highlight($args{'sections'}{$section});
532 print "</ul>\n";
534 print "<hr>\n";
537 sub output_section_sgml(%) {
538 my %args = %{$_[0]};
539 my $section;
540 # print out each section
541 $lineprefix=" ";
542 foreach $section (@{$args{'sectionlist'}}) {
543 print "<refsect1>\n <title>$section</title>\n <para>\n";
544 if ($section =~ m/EXAMPLE/i) {
545 print "<example><para>\n";
547 output_highlight($args{'sections'}{$section});
548 if ($section =~ m/EXAMPLE/i) {
549 print "</para></example>\n";
551 print " </para>\n</refsect1>\n";
555 # output function in sgml DocBook
556 sub output_function_sgml(%) {
557 my %args = %{$_[0]};
558 my ($parameter, $section);
559 my $count;
560 my $id;
562 $id = "API-".$args{'function'};
563 $id =~ s/[^A-Za-z0-9]/-/g;
565 print "<refentry>\n";
566 print "<refmeta>\n";
567 print "<refentrytitle><phrase id=\"$id\">".$args{'function'}."</phrase></refentrytitle>\n";
568 print "</refmeta>\n";
569 print "<refnamediv>\n";
570 print " <refname>".$args{'function'}."</refname>\n";
571 print " <refpurpose>\n";
572 print " ";
573 output_highlight ($args{'purpose'});
574 print " </refpurpose>\n";
575 print "</refnamediv>\n";
577 print "<refsynopsisdiv>\n";
578 print " <title>Synopsis</title>\n";
579 print " <funcsynopsis><funcprototype>\n";
580 print " <funcdef>".$args{'functiontype'}." ";
581 print "<function>".$args{'function'}." </function></funcdef>\n";
583 $count = 0;
584 if ($#{$args{'parameterlist'}} >= 0) {
585 foreach $parameter (@{$args{'parameterlist'}}) {
586 $type = $args{'parametertypes'}{$parameter};
587 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
588 # pointer-to-function
589 print " <paramdef>$1<parameter>$parameter</parameter>)\n";
590 print " <funcparams>$2</funcparams></paramdef>\n";
591 } else {
592 print " <paramdef>".$type;
593 print " <parameter>$parameter</parameter></paramdef>\n";
596 } else {
597 print " <void>\n";
599 print " </funcprototype></funcsynopsis>\n";
600 print "</refsynopsisdiv>\n";
602 # print parameters
603 print "<refsect1>\n <title>Arguments</title>\n";
604 if ($#{$args{'parameterlist'}} >= 0) {
605 print " <variablelist>\n";
606 foreach $parameter (@{$args{'parameterlist'}}) {
607 print " <varlistentry>\n <term><parameter>$parameter</parameter></term>\n";
608 print " <listitem>\n <para>\n";
609 $lineprefix=" ";
610 output_highlight($args{'parameterdescs'}{$parameter});
611 print " </para>\n </listitem>\n </varlistentry>\n";
613 print " </variablelist>\n";
614 } else {
615 print " <para>\n None\n </para>\n";
617 print "</refsect1>\n";
619 output_section_sgml(@_);
620 print "</refentry>\n\n";
623 # output struct in sgml DocBook
624 sub output_struct_sgml(%) {
625 my %args = %{$_[0]};
626 my ($parameter, $section);
627 my $id;
629 $id = "API-struct-".$args{'struct'};
630 $id =~ s/[^A-Za-z0-9]/-/g;
632 print "<refentry>\n";
633 print "<refmeta>\n";
634 print "<refentrytitle><phrase id=\"$id\">".$args{'type'}." ".$args{'struct'}."</phrase></refentrytitle>\n";
635 print "</refmeta>\n";
636 print "<refnamediv>\n";
637 print " <refname>".$args{'type'}." ".$args{'struct'}."</refname>\n";
638 print " <refpurpose>\n";
639 print " ";
640 output_highlight ($args{'purpose'});
641 print " </refpurpose>\n";
642 print "</refnamediv>\n";
644 print "<refsynopsisdiv>\n";
645 print " <title>Synopsis</title>\n";
646 print " <programlisting>\n";
647 print $args{'type'}." ".$args{'struct'}." {\n";
648 foreach $parameter (@{$args{'parameterlist'}}) {
649 defined($args{'parameterdescs'}{$parameter}) || next;
650 ($args{'parameterdescs'}{$parameter} ne $undescribed) || next;
651 $type = $args{'parametertypes'}{$parameter};
652 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
653 # pointer-to-function
654 print " $1 $parameter ($2);\n";
655 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
656 print " $1 $parameter$2;\n";
657 } else {
658 print " ".$type." ".$parameter.";\n";
661 print "};";
662 print " </programlisting>\n";
663 print "</refsynopsisdiv>\n";
665 print " <refsect1>\n";
666 print " <title>Members</title>\n";
668 print " <variablelist>\n";
669 foreach $parameter (@{$args{'parameterlist'}}) {
670 defined($args{'parameterdescs'}{$parameter}) || next;
671 ($args{'parameterdescs'}{$parameter} ne $undescribed) || next;
672 print " <varlistentry>";
673 print " <term>$parameter</term>\n";
674 print " <listitem><para>\n";
675 output_highlight($args{'parameterdescs'}{$parameter});
676 print " </para></listitem>\n";
677 print " </varlistentry>\n";
679 print " </variablelist>\n";
680 print " </refsect1>\n";
682 output_section_sgml(@_);
684 print "</refentry>\n\n";
687 # output enum in sgml DocBook
688 sub output_enum_sgml(%) {
689 my %args = %{$_[0]};
690 my ($parameter, $section);
691 my $count;
692 my $id;
694 $id = "API-enum-".$args{'enum'};
695 $id =~ s/[^A-Za-z0-9]/-/g;
697 print "<refentry>\n";
698 print "<refmeta>\n";
699 print "<refentrytitle><phrase id=\"$id\">enum ".$args{'enum'}."</phrase></refentrytitle>\n";
700 print "</refmeta>\n";
701 print "<refnamediv>\n";
702 print " <refname>enum ".$args{'enum'}."</refname>\n";
703 print " <refpurpose>\n";
704 print " ";
705 output_highlight ($args{'purpose'});
706 print " </refpurpose>\n";
707 print "</refnamediv>\n";
709 print "<refsynopsisdiv>\n";
710 print " <title>Synopsis</title>\n";
711 print " <programlisting>\n";
712 print "enum ".$args{'enum'}." {\n";
713 $count = 0;
714 foreach $parameter (@{$args{'parameterlist'}}) {
715 print " $parameter";
716 if ($count != $#{$args{'parameterlist'}}) {
717 $count++;
718 print ",";
720 print "\n";
722 print "};";
723 print " </programlisting>\n";
724 print "</refsynopsisdiv>\n";
726 print "<refsect1>\n";
727 print " <title>Constants</title>\n";
728 print " <variablelist>\n";
729 foreach $parameter (@{$args{'parameterlist'}}) {
730 print " <varlistentry>";
731 print " <term>$parameter</term>\n";
732 print " <listitem><para>\n";
733 output_highlight($args{'parameterdescs'}{$parameter});
734 print " </para></listitem>\n";
735 print " </varlistentry>\n";
737 print " </variablelist>\n";
738 print "</refsect1>\n";
740 output_section_sgml(@_);
742 print "</refentry>\n\n";
745 # output typedef in sgml DocBook
746 sub output_typedef_sgml(%) {
747 my %args = %{$_[0]};
748 my ($parameter, $section);
749 my $id;
751 $id = "API-typedef-".$args{'typedef'};
752 $id =~ s/[^A-Za-z0-9]/-/g;
754 print "<refentry>\n";
755 print "<refmeta>\n";
756 print "<refentrytitle><phrase id=\"$id\">typedef ".$args{'typedef'}."</phrase></refentrytitle>\n";
757 print "</refmeta>\n";
758 print "<refnamediv>\n";
759 print " <refname>typedef ".$args{'typedef'}."</refname>\n";
760 print " <refpurpose>\n";
761 print " ";
762 output_highlight ($args{'purpose'});
763 print " </refpurpose>\n";
764 print "</refnamediv>\n";
766 print "<refsynopsisdiv>\n";
767 print " <title>Synopsis</title>\n";
768 print " <synopsis>typedef ".$args{'typedef'}.";</synopsis>\n";
769 print "</refsynopsisdiv>\n";
771 output_section_sgml(@_);
773 print "</refentry>\n\n";
776 # output in sgml DocBook
777 sub output_intro_sgml(%) {
778 my %args = %{$_[0]};
779 my ($parameter, $section);
780 my $count;
782 my $id = $args{'module'};
783 $id =~ s/[^A-Za-z0-9]/-/g;
785 # print out each section
786 $lineprefix=" ";
787 foreach $section (@{$args{'sectionlist'}}) {
788 print "<refsect1>\n <title>$section</title>\n <para>\n";
789 if ($section =~ m/EXAMPLE/i) {
790 print "<example><para>\n";
792 output_highlight($args{'sections'}{$section});
793 if ($section =~ m/EXAMPLE/i) {
794 print "</para></example>\n";
796 print " </para>\n</refsect1>\n";
799 print "\n\n";
802 # output in sgml DocBook
803 sub output_function_gnome {
804 my %args = %{$_[0]};
805 my ($parameter, $section);
806 my $count;
807 my $id;
809 $id = $args{'module'}."-".$args{'function'};
810 $id =~ s/[^A-Za-z0-9]/-/g;
812 print "<sect2>\n";
813 print " <title id=\"$id\">".$args{'function'}."</title>\n";
815 print " <funcsynopsis>\n";
816 print " <funcdef>".$args{'functiontype'}." ";
817 print "<function>".$args{'function'}." ";
818 print "</function></funcdef>\n";
820 $count = 0;
821 if ($#{$args{'parameterlist'}} >= 0) {
822 foreach $parameter (@{$args{'parameterlist'}}) {
823 $type = $args{'parametertypes'}{$parameter};
824 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
825 # pointer-to-function
826 print " <paramdef>$1 <parameter>$parameter</parameter>)\n";
827 print " <funcparams>$2</funcparams></paramdef>\n";
828 } else {
829 print " <paramdef>".$type;
830 print " <parameter>$parameter</parameter></paramdef>\n";
833 } else {
834 print " <void>\n";
836 print " </funcsynopsis>\n";
837 if ($#{$args{'parameterlist'}} >= 0) {
838 print " <informaltable pgwide=\"1\" frame=\"none\" role=\"params\">\n";
839 print "<tgroup cols=\"2\">\n";
840 print "<colspec colwidth=\"2*\">\n";
841 print "<colspec colwidth=\"8*\">\n";
842 print "<tbody>\n";
843 foreach $parameter (@{$args{'parameterlist'}}) {
844 print " <row><entry align=\"right\"><parameter>$parameter</parameter></entry>\n";
845 print " <entry>\n";
846 $lineprefix=" ";
847 output_highlight($args{'parameterdescs'}{$parameter});
848 print " </entry></row>\n";
850 print " </tbody></tgroup></informaltable>\n";
851 } else {
852 print " <para>\n None\n </para>\n";
855 # print out each section
856 $lineprefix=" ";
857 foreach $section (@{$args{'sectionlist'}}) {
858 print "<simplesect>\n <title>$section</title>\n";
859 if ($section =~ m/EXAMPLE/i) {
860 print "<example><programlisting>\n";
861 } else {
863 print "<para>\n";
864 output_highlight($args{'sections'}{$section});
865 print "</para>\n";
866 if ($section =~ m/EXAMPLE/i) {
867 print "</programlisting></example>\n";
868 } else {
870 print " </simplesect>\n";
873 print "</sect2>\n\n";
877 # output function in man
878 sub output_function_man(%) {
879 my %args = %{$_[0]};
880 my ($parameter, $section);
881 my $count;
883 print ".TH \"$args{'function'}\" 9 \"$args{'function'}\" \"$man_date\" \"Kernel Hacker's Manual\" LINUX\n";
885 print ".SH NAME\n";
886 print $args{'function'}." \\- ".$args{'purpose'}."\n";
888 print ".SH SYNOPSIS\n";
889 print ".B \"".$args{'functiontype'}."\" ".$args{'function'}."\n";
890 $count = 0;
891 my $parenth = "(";
892 my $post = ",";
893 foreach my $parameter (@{$args{'parameterlist'}}) {
894 if ($count == $#{$args{'parameterlist'}}) {
895 $post = ");";
897 $type = $args{'parametertypes'}{$parameter};
898 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
899 # pointer-to-function
900 print ".BI \"".$parenth.$1."\" ".$parameter." \") (".$2.")".$post."\"\n";
901 } else {
902 $type =~ s/([^\*])$/$1 /;
903 print ".BI \"".$parenth.$type."\" ".$parameter." \"".$post."\"\n";
905 $count++;
906 $parenth = "";
909 print ".SH ARGUMENTS\n";
910 foreach $parameter (@{$args{'parameterlist'}}) {
911 print ".IP \"".$parameter."\" 12\n";
912 output_highlight($args{'parameterdescs'}{$parameter});
914 foreach $section (@{$args{'sectionlist'}}) {
915 print ".SH \"", uc $section, "\"\n";
916 output_highlight($args{'sections'}{$section});
921 # output enum in man
922 sub output_enum_man(%) {
923 my %args = %{$_[0]};
924 my ($parameter, $section);
925 my $count;
927 print ".TH \"$args{'module'}\" 9 \"enum $args{'enum'}\" \"$man_date\" \"API Manual\" LINUX\n";
929 print ".SH NAME\n";
930 print "enum ".$args{'enum'}." \\- ".$args{'purpose'}."\n";
932 print ".SH SYNOPSIS\n";
933 print "enum ".$args{'enum'}." {\n";
934 $count = 0;
935 foreach my $parameter (@{$args{'parameterlist'}}) {
936 print ".br\n.BI \" $parameter\"\n";
937 if ($count == $#{$args{'parameterlist'}}) {
938 print "\n};\n";
939 last;
941 else {
942 print ", \n.br\n";
944 $count++;
947 print ".SH Constants\n";
948 foreach $parameter (@{$args{'parameterlist'}}) {
949 print ".IP \"".$parameter."\" 12\n";
950 output_highlight($args{'parameterdescs'}{$parameter});
952 foreach $section (@{$args{'sectionlist'}}) {
953 print ".SH \"$section\"\n";
954 output_highlight($args{'sections'}{$section});
959 # output struct in man
960 sub output_struct_man(%) {
961 my %args = %{$_[0]};
962 my ($parameter, $section);
964 print ".TH \"$args{'module'}\" 9 \"".$args{'type'}." ".$args{'struct'}."\" \"$man_date\" \"API Manual\" LINUX\n";
966 print ".SH NAME\n";
967 print $args{'type'}." ".$args{'struct'}." \\- ".$args{'purpose'}."\n";
969 print ".SH SYNOPSIS\n";
970 print $args{'type'}." ".$args{'struct'}." {\n";
972 foreach my $parameter (@{$args{'parameterlist'}}) {
973 ($args{'parameterdescs'}{$parameter} ne $undescribed) || next;
974 print "\n.br\n";
975 $type = $args{'parametertypes'}{$parameter};
976 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
977 # pointer-to-function
978 print ".BI \" ".$1."\" ".$parameter." \") (".$2.")"."\"\n;\n";
979 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
980 print ".BI \" ".$1."\" ".$parameter.$2." \""."\"\n;\n";
981 } else {
982 $type =~ s/([^\*])$/$1 /;
983 print ".BI \" ".$type."\" ".$parameter." \""."\"\n;\n";
985 print "\n.br\n";
987 print "};\n.br\n";
989 print ".SH Arguments\n";
990 foreach $parameter (@{$args{'parameterlist'}}) {
991 ($args{'parameterdescs'}{$parameter} ne $undescribed) || next;
992 print ".IP \"".$parameter."\" 12\n";
993 output_highlight($args{'parameterdescs'}{$parameter});
995 foreach $section (@{$args{'sectionlist'}}) {
996 print ".SH \"$section\"\n";
997 output_highlight($args{'sections'}{$section});
1002 # output typedef in man
1003 sub output_typedef_man(%) {
1004 my %args = %{$_[0]};
1005 my ($parameter, $section);
1007 print ".TH \"$args{'module'}\" 9 \"$args{'typedef'}\" \"$man_date\" \"API Manual\" LINUX\n";
1009 print ".SH NAME\n";
1010 print "typedef ".$args{'typedef'}." \\- ".$args{'purpose'}."\n";
1012 foreach $section (@{$args{'sectionlist'}}) {
1013 print ".SH \"$section\"\n";
1014 output_highlight($args{'sections'}{$section});
1018 sub output_intro_man(%) {
1019 my %args = %{$_[0]};
1020 my ($parameter, $section);
1021 my $count;
1023 print ".TH \"$args{'module'}\" 9 \"$args{'module'}\" \"$man_date\" \"API Manual\" LINUX\n";
1025 foreach $section (@{$args{'sectionlist'}}) {
1026 print ".SH \"$section\"\n";
1027 output_highlight($args{'sections'}{$section});
1032 # output in text
1033 sub output_function_text(%) {
1034 my %args = %{$_[0]};
1035 my ($parameter, $section);
1037 print "Function:\n\n";
1038 my $start=$args{'functiontype'}." ".$args{'function'}." (";
1039 print $start;
1040 my $count = 0;
1041 foreach my $parameter (@{$args{'parameterlist'}}) {
1042 $type = $args{'parametertypes'}{$parameter};
1043 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1044 # pointer-to-function
1045 print $1.$parameter.") (".$2;
1046 } else {
1047 print $type." ".$parameter;
1049 if ($count != $#{$args{'parameterlist'}}) {
1050 $count++;
1051 print ",\n";
1052 print " " x length($start);
1053 } else {
1054 print ");\n\n";
1058 print "Arguments:\n\n";
1059 foreach $parameter (@{$args{'parameterlist'}}) {
1060 print $parameter."\n\t".$args{'parameterdescs'}{$parameter}."\n";
1062 output_section_text(@_);
1065 #output sections in text
1066 sub output_section_text(%) {
1067 my %args = %{$_[0]};
1068 my $section;
1070 print "\n";
1071 foreach $section (@{$args{'sectionlist'}}) {
1072 print "$section:\n\n";
1073 output_highlight($args{'sections'}{$section});
1075 print "\n\n";
1078 # output enum in text
1079 sub output_enum_text(%) {
1080 my %args = %{$_[0]};
1081 my ($parameter);
1082 my $count;
1083 print "Enum:\n\n";
1085 print "enum ".$args{'enum'}." {\n";
1086 $count = 0;
1087 foreach $parameter (@{$args{'parameterlist'}}) {
1088 print "\t$parameter";
1089 if ($count != $#{$args{'parameterlist'}}) {
1090 $count++;
1091 print ",";
1093 print "\n";
1095 print "};\n\n";
1097 print "Constants:\n\n";
1098 foreach $parameter (@{$args{'parameterlist'}}) {
1099 print "$parameter\n\t";
1100 print $args{'parameterdescs'}{$parameter}."\n";
1103 output_section_text(@_);
1106 # output typedef in text
1107 sub output_typedef_text(%) {
1108 my %args = %{$_[0]};
1109 my ($parameter);
1110 my $count;
1111 print "Typedef:\n\n";
1113 print "typedef ".$args{'typedef'}."\n";
1114 output_section_text(@_);
1117 # output struct as text
1118 sub output_struct_text(%) {
1119 my %args = %{$_[0]};
1120 my ($parameter);
1122 print $args{'type'}." ".$args{'struct'}.":\n\n";
1123 print $args{'type'}." ".$args{'struct'}." {\n";
1124 foreach $parameter (@{$args{'parameterlist'}}) {
1125 ($args{'parameterdescs'}{$parameter} ne $undescribed) || next;
1126 $type = $args{'parametertypes'}{$parameter};
1127 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1128 # pointer-to-function
1129 print "\t$1 $parameter) ($2);\n";
1130 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
1131 print "\t$1 $parameter$2;\n";
1132 } else {
1133 print "\t".$type." ".$parameter.";\n";
1136 print "};\n\n";
1138 print "Members:\n\n";
1139 foreach $parameter (@{$args{'parameterlist'}}) {
1140 ($args{'parameterdescs'}{$parameter} ne $undescribed) || next;
1141 print "$parameter\n\t";
1142 print $args{'parameterdescs'}{$parameter}."\n";
1144 print "\n";
1145 output_section_text(@_);
1148 sub output_intro_text(%) {
1149 my %args = %{$_[0]};
1150 my ($parameter, $section);
1152 foreach $section (@{$args{'sectionlist'}}) {
1153 print " $section:\n";
1154 print " -> ";
1155 output_highlight($args{'sections'}{$section});
1160 # generic output function for typedefs
1161 sub output_declaration {
1162 no strict 'refs';
1163 my $name = shift;
1164 my $functype = shift;
1165 my $func = "output_${functype}_$output_mode";
1166 if (($function_only==0) ||
1167 ( $function_only == 1 && defined($function_table{$name})) ||
1168 ( $function_only == 2 && !defined($function_table{$name})))
1170 &$func(@_);
1171 $section_counter++;
1176 # generic output function - calls the right one based
1177 # on current output mode.
1178 sub output_intro {
1179 no strict 'refs';
1180 my $func = "output_intro_".$output_mode;
1181 &$func(@_);
1182 $section_counter++;
1186 # takes a declaration (struct, union, enum, typedef) and
1187 # invokes the right handler. NOT called for functions.
1188 sub dump_declaration($$) {
1189 no strict 'refs';
1190 my ($prototype, $file) = @_;
1191 my $func = "dump_".$decl_type;
1192 &$func(@_);
1195 sub dump_union($$) {
1196 dump_struct(@_);
1199 sub dump_struct($$) {
1200 my $x = shift;
1201 my $file = shift;
1203 if ($x =~/(struct|union)\s+(\w+)\s*{(.*)}/) {
1204 $declaration_name = $2;
1205 my $members = $3;
1207 # ignore embedded structs or unions
1208 $members =~ s/{.*}//g;
1210 create_parameterlist($members, ';', $file);
1212 output_declaration($declaration_name,
1213 'struct',
1214 {'struct' => $declaration_name,
1215 'module' => $modulename,
1216 'parameterlist' => \@parameterlist,
1217 'parameterdescs' => \%parameterdescs,
1218 'parametertypes' => \%parametertypes,
1219 'sectionlist' => \@sectionlist,
1220 'sections' => \%sections,
1221 'purpose' => $declaration_purpose,
1222 'type' => $decl_type
1225 else {
1226 print STDERR "Error(${file}:$.): Cannot parse struct or union!\n";
1227 ++$errors;
1231 sub dump_enum($$) {
1232 my $x = shift;
1233 my $file = shift;
1235 if ($x =~ /enum\s+(\w+)\s*{(.*)}/) {
1236 $declaration_name = $1;
1237 my $members = $2;
1239 foreach my $arg (split ',', $members) {
1240 $arg =~ s/^\s*(\w+).*/$1/;
1241 push @parameterlist, $arg;
1242 if (!$parameterdescs{$arg}) {
1243 $parameterdescs{$arg} = $undescribed;
1244 print STDERR "Warning(${file}:$.): Enum value '$arg' ".
1245 "not described in enum '$declaration_name'\n";
1250 output_declaration($declaration_name,
1251 'enum',
1252 {'enum' => $declaration_name,
1253 'module' => $modulename,
1254 'parameterlist' => \@parameterlist,
1255 'parameterdescs' => \%parameterdescs,
1256 'sectionlist' => \@sectionlist,
1257 'sections' => \%sections,
1258 'purpose' => $declaration_purpose
1261 else {
1262 print STDERR "Error(${file}:$.): Cannot parse enum!\n";
1263 ++$errors;
1267 sub dump_typedef($$) {
1268 my $x = shift;
1269 my $file = shift;
1271 while (($x =~ /\(*.\)\s*;$/) || ($x =~ /\[*.\]\s*;$/)) {
1272 $x =~ s/\(*.\)\s*;$/;/;
1273 $x =~ s/\[*.\]\s*;$/;/;
1276 if ($x =~ /typedef.*\s+(\w+)\s*;/) {
1277 $declaration_name = $1;
1279 output_declaration($declaration_name,
1280 'typedef',
1281 {'typedef' => $declaration_name,
1282 'module' => $modulename,
1283 'sectionlist' => \@sectionlist,
1284 'sections' => \%sections,
1285 'purpose' => $declaration_purpose
1288 else {
1289 print STDERR "Error(${file}:$.): Cannot parse typedef!\n";
1290 ++$errors;
1294 sub create_parameterlist($$$) {
1295 my $args = shift;
1296 my $splitter = shift;
1297 my $file = shift;
1298 my $type;
1299 my $param;
1301 while ($args =~ /(\([^\),]+),/) {
1302 $args =~ s/(\([^\),]+),/$1#/g;
1305 foreach my $arg (split($splitter, $args)) {
1306 # strip leading/trailing spaces
1307 $arg =~ s/^\s*//;
1308 $arg =~ s/\s*$//;
1309 $arg =~ s/\s+/ /;
1311 if ($arg =~ m/\(/) {
1312 # pointer-to-function
1313 $arg =~ tr/#/,/;
1314 $arg =~ m/[^\(]+\(\*([^\)]+)\)/;
1315 $param = $1;
1316 $type = $arg;
1317 $type =~ s/([^\(]+\(\*)$param/$1/;
1318 } else {
1319 # evil magic to get fixed array parameters to work
1320 $arg =~ s/(.+\s+)(.+)\[.*/$1* $2/;
1321 my @args = split('\s', $arg);
1323 $param = pop @args;
1324 if ($param =~ m/^(\*+)(.*)/) {
1325 $param = $2;
1326 push @args, $1;
1328 elsif ($param =~ m/(.*?)\s*:\s*(\d+)/) {
1329 $param = $1;
1330 push @args, ":$2";
1332 $type = join " ", @args;
1335 if ($type eq "" && $param eq "...")
1337 $type="...";
1338 $param="...";
1339 $parameterdescs{"..."} = "variable arguments";
1341 elsif ($type eq "" && ($param eq "" or $param eq "void"))
1343 $type="";
1344 $param="void";
1345 $parameterdescs{void} = "no arguments";
1347 if (defined $type && $type && !defined $parameterdescs{$param}) {
1348 $parameterdescs{$param} = $undescribed;
1350 if (($type eq 'function') || ($type eq 'enum')) {
1351 print STDERR "Warning(${file}:$.): Function parameter ".
1352 "or member '$param' not " .
1353 "described in '$declaration_name'\n";
1355 ++$errors;
1358 push @parameterlist, $param;
1359 $parametertypes{$param} = $type;
1364 # takes a function prototype and the name of the current file being
1365 # processed and spits out all the details stored in the global
1366 # arrays/hashes.
1367 sub dump_function($$) {
1368 my $prototype = shift;
1369 my $file = shift;
1371 $prototype =~ s/^static +//;
1372 $prototype =~ s/^extern +//;
1373 $prototype =~ s/^inline +//;
1374 $prototype =~ s/^__inline__ +//;
1375 $prototype =~ s/^#define +//; #ak added
1377 # Yes, this truly is vile. We are looking for:
1378 # 1. Return type (may be nothing if we're looking at a macro)
1379 # 2. Function name
1380 # 3. Function parameters.
1382 # All the while we have to watch out for function pointer parameters
1383 # (which IIRC is what the two sections are for), C types (these
1384 # regexps don't even start to express all the possibilities), and
1385 # so on.
1387 # If you mess with these regexps, it's a good idea to check that
1388 # the following functions' documentation still comes out right:
1389 # - parport_register_device (function pointer parameters)
1390 # - atomic_set (macro)
1391 # - pci_match_device (long return type)
1393 if ($prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1394 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1395 $prototype =~ m/^(\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1396 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1397 $prototype =~ m/^(\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1398 $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1399 $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1400 $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1401 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1402 $prototype =~ m/^(\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1403 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1404 $prototype =~ m/^(\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1405 $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1406 $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/) {
1407 $return_type = $1;
1408 $declaration_name = $2;
1409 my $args = $3;
1411 create_parameterlist($args, ',', $file);
1412 } else {
1413 print STDERR "Error(${file}:$.): cannot understand prototype: '$prototype'\n";
1414 ++$errors;
1415 return;
1418 output_declaration($declaration_name,
1419 'function',
1420 {'function' => $declaration_name,
1421 'module' => $modulename,
1422 'functiontype' => $return_type,
1423 'parameterlist' => \@parameterlist,
1424 'parameterdescs' => \%parameterdescs,
1425 'parametertypes' => \%parametertypes,
1426 'sectionlist' => \@sectionlist,
1427 'sections' => \%sections,
1428 'purpose' => $declaration_purpose
1432 sub process_file($);
1434 # Read the file that maps relative names to absolute names for
1435 # separate source and object directories and for shadow trees.
1436 if (open(SOURCE_MAP, "<.tmp_filelist.txt")) {
1437 my ($relname, $absname);
1438 while(<SOURCE_MAP>) {
1439 chop();
1440 ($relname, $absname) = (split())[0..1];
1441 $relname =~ s:^/+::;
1442 $source_map{$relname} = $absname;
1444 close(SOURCE_MAP);
1447 if ($filelist) {
1448 open(FLIST,"<$filelist") or die "Can't open file list $filelist";
1449 while(<FLIST>) {
1450 chop;
1451 process_file($_);
1455 foreach (@ARGV) {
1456 chomp;
1457 process_file($_);
1460 exit($errors);
1462 sub reset_state {
1463 $function = "";
1464 %constants = ();
1465 %parameterdescs = ();
1466 %parametertypes = ();
1467 @parameterlist = ();
1468 %sections = ();
1469 @sectionlist = ();
1470 $prototype = "";
1472 $state = 0;
1475 sub process_state3_function($$) {
1476 my $x = shift;
1477 my $file = shift;
1479 if ($x =~ m#\s*/\*\s+MACDOC\s*#io) {
1480 # do nothing
1482 elsif ($x =~ /([^\{]*)/) {
1483 $prototype .= $1;
1485 if (($x =~ /\{/) || ($x =~ /\#/) || ($x =~ /;/)) {
1486 $prototype =~ s@/\*.*?\*/@@gos; # strip comments.
1487 $prototype =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1488 $prototype =~ s@^\s+@@gos; # strip leading spaces
1489 dump_function($prototype,$file);
1490 reset_state();
1494 sub process_state3_type($$) {
1495 my $x = shift;
1496 my $file = shift;
1498 $x =~ s@/\*.*?\*/@@gos; # strip comments.
1499 $x =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1500 $x =~ s@^\s+@@gos; # strip leading spaces
1501 $x =~ s@\s+$@@gos; # strip trailing spaces
1503 while (1) {
1504 if ( $x =~ /([^{};]*)([{};])(.*)/ ) {
1505 $prototype .= $1 . $2;
1506 ($2 eq '{') && $brcount++;
1507 ($2 eq '}') && $brcount--;
1508 if (($2 eq ';') && ($brcount == 0)) {
1509 dump_declaration($prototype,$file);
1510 reset_state();
1511 last;
1513 $x = $3;
1514 } else {
1515 $prototype .= $x;
1516 last;
1521 sub process_file($) {
1522 my ($file) = @_;
1523 my $identifier;
1524 my $func;
1525 my $initial_section_counter = $section_counter;
1527 if (defined($source_map{$file})) {
1528 $file = $source_map{$file};
1531 if (!open(IN,"<$file")) {
1532 print STDERR "Error: Cannot open file $file\n";
1533 ++$errors;
1534 return;
1537 $section_counter = 0;
1538 while (<IN>) {
1539 if ($state == 0) {
1540 if (/$doc_start/o) {
1541 $state = 1; # next line is always the function name
1543 } elsif ($state == 1) { # this line is the function name (always)
1544 if (/$doc_block/o) {
1545 $state = 4;
1546 $contents = "";
1547 if ( $1 eq "" ) {
1548 $section = $section_intro;
1549 } else {
1550 $section = $1;
1553 elsif (/$doc_decl/o) {
1554 $identifier = $1;
1555 if (/\s*([\w\s]+?)\s*-/) {
1556 $identifier = $1;
1559 $state = 2;
1560 if (/-(.*)/) {
1561 $declaration_purpose = $1;
1562 } else {
1563 $declaration_purpose = "";
1565 if ($identifier =~ m/^struct/) {
1566 $decl_type = 'struct';
1567 } elsif ($identifier =~ m/^union/) {
1568 $decl_type = 'union';
1569 } elsif ($identifier =~ m/^enum/) {
1570 $decl_type = 'enum';
1571 } elsif ($identifier =~ m/^typedef/) {
1572 $decl_type = 'typedef';
1573 } else {
1574 $decl_type = 'function';
1577 if ($verbose) {
1578 print STDERR "Info(${file}:$.): Scanning doc for $identifier\n";
1580 } else {
1581 print STDERR "Warning(${file}:$.): Cannot understand $_ on line $.",
1582 " - I thought it was a doc line\n";
1583 ++$errors;
1584 $state = 0;
1586 } elsif ($state == 2) { # look for head: lines, and include content
1587 if (/$doc_sect/o) {
1588 $newsection = $1;
1589 $newcontents = $2;
1591 if ($contents ne "") {
1592 $contents =~ s/\&/\\\\\\amp;/g;
1593 $contents =~ s/\</\\\\\\lt;/g;
1594 $contents =~ s/\>/\\\\\\gt;/g;
1595 dump_section($section, $contents);
1596 $section = $section_default;
1599 $contents = $newcontents;
1600 if ($contents ne "") {
1601 $contents .= "\n";
1603 $section = $newsection;
1604 } elsif (/$doc_end/) {
1606 if ($contents ne "") {
1607 $contents =~ s/\&/\\\\\\amp;/g;
1608 $contents =~ s/\</\\\\\\lt;/g;
1609 $contents =~ s/\>/\\\\\\gt;/g;
1610 dump_section($section, $contents);
1611 $section = $section_default;
1612 $contents = "";
1615 $prototype = "";
1616 $state = 3;
1617 $brcount = 0;
1618 # print STDERR "end of doc comment, looking for prototype\n";
1619 } elsif (/$doc_content/) {
1620 # miguel-style comment kludge, look for blank lines after
1621 # @parameter line to signify start of description
1622 if ($1 eq "" &&
1623 ($section =~ m/^@/ || $section eq $section_context)) {
1624 $contents =~ s/\&/\\\\\\amp;/g;
1625 $contents =~ s/\</\\\\\\lt;/g;
1626 $contents =~ s/\>/\\\\\\gt;/g;
1627 dump_section($section, $contents);
1628 $section = $section_default;
1629 $contents = "";
1630 } else {
1631 $contents .= $1."\n";
1633 } else {
1634 # i dont know - bad line? ignore.
1635 print STDERR "Warning(${file}:$.): bad line: $_";
1636 ++$errors;
1638 } elsif ($state == 3) { # scanning for function { (end of prototype)
1639 if ($decl_type eq 'function') {
1640 process_state3_function($_, $file);
1641 } else {
1642 process_state3_type($_, $file);
1644 } elsif ($state == 4) {
1645 # Documentation block
1646 if (/$doc_block/) {
1647 dump_section($section, $contents);
1648 output_intro({'sectionlist' => \@sectionlist,
1649 'sections' => \%sections });
1650 $contents = "";
1651 $function = "";
1652 %constants = ();
1653 %parameterdescs = ();
1654 %parametertypes = ();
1655 @parameterlist = ();
1656 %sections = ();
1657 @sectionlist = ();
1658 $prototype = "";
1659 if ( $1 eq "" ) {
1660 $section = $section_intro;
1661 } else {
1662 $section = $1;
1665 elsif (/$doc_end/)
1667 dump_section($section, $contents);
1668 output_intro({'sectionlist' => \@sectionlist,
1669 'sections' => \%sections });
1670 $contents = "";
1671 $function = "";
1672 %constants = ();
1673 %parameterdescs = ();
1674 %parametertypes = ();
1675 @parameterlist = ();
1676 %sections = ();
1677 @sectionlist = ();
1678 $prototype = "";
1679 $state = 0;
1681 elsif (/$doc_content/)
1683 if ( $1 eq "" )
1685 $contents .= $blankline;
1687 else
1689 $contents .= $1 . "\n";
1694 if ($initial_section_counter == $section_counter) {
1695 print STDERR "Warning(${file}): no structured comments found\n";
1696 if ($output_mode eq "sgml") {
1697 # The template wants at least one RefEntry here; make one.
1698 print "<refentry>\n";
1699 print " <refnamediv>\n";
1700 print " <refname>\n";
1701 print " ${file}\n";
1702 print " </refname>\n";
1703 print " <refpurpose>\n";
1704 print " Document generation inconsistency\n";
1705 print " </refpurpose>\n";
1706 print " </refnamediv>\n";
1707 print " <refsect1>\n";
1708 print " <title>\n";
1709 print " Oops\n";
1710 print " </title>\n";
1711 print " <warning>\n";
1712 print " <para>\n";
1713 print " The template for this document tried to insert\n";
1714 print " the structured comment from the file\n";
1715 print " <filename>${file}</filename> at this point,\n";
1716 print " but none was found.\n";
1717 print " This dummy section is inserted to allow\n";
1718 print " generation to continue.\n";
1719 print " </para>\n";
1720 print " </warning>\n";
1721 print " </refsect1>\n";
1722 print "</refentry>\n";