Add to mips64 also for symmetry.
[linux-2.6/linux-mips.git] / scripts / kernel-doc
blob3c02ef90abc82caf6fb47b4c24522f9e8e6fa34b
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;
157 my $warnings = 0;
159 # match expressions used to find embedded type information
160 my $type_constant = '\%([-_\w]+)';
161 my $type_func = '(\w+)\(\)';
162 my $type_param = '\@(\w+)';
163 my $type_struct = '\&((struct\s*)?[_\w]+)';
164 my $type_env = '(\$\w+)';
166 # Output conversion substitutions.
167 # One for each output format
169 # these work fairly well
170 my %highlights_html = ( $type_constant, "<i>\$1</i>",
171 $type_func, "<b>\$1</b>",
172 $type_struct, "<i>\$1</i>",
173 $type_param, "<tt><b>\$1</b></tt>" );
174 my $blankline_html = "<p>";
176 # sgml, docbook format
177 my %highlights_sgml = ( "([^=])\\\"([^\\\"<]+)\\\"", "\$1<quote>\$2</quote>",
178 $type_constant, "<constant>\$1</constant>",
179 $type_func, "<function>\$1</function>",
180 $type_struct, "<structname>\$1</structname>",
181 $type_env, "<envar>\$1</envar>",
182 $type_param, "<parameter>\$1</parameter>" );
183 my $blankline_sgml = "</para><para>\n";
185 # gnome, docbook format
186 my %highlights_gnome = ( $type_constant, "<replaceable class=\"option\">\$1</replaceable>",
187 $type_func, "<function>\$1</function>",
188 $type_struct, "<structname>\$1</structname>",
189 $type_env, "<envar>\$1</envar>",
190 $type_param, "<parameter>\$1</parameter>" );
191 my $blankline_gnome = "</para><para>\n";
193 # these are pretty rough
194 my %highlights_man = ( $type_constant, "\$1",
195 $type_func, "\\\\fB\$1\\\\fP",
196 $type_struct, "\\\\fI\$1\\\\fP",
197 $type_param, "\\\\fI\$1\\\\fP" );
198 my $blankline_man = "";
200 # text-mode
201 my %highlights_text = ( $type_constant, "\$1",
202 $type_func, "\$1",
203 $type_struct, "\$1",
204 $type_param, "\$1" );
205 my $blankline_text = "";
208 sub usage {
209 print "Usage: $0 [ -v ] [ -docbook | -html | -text | -man ]\n";
210 print " [ -function funcname [ -function funcname ...] ]\n";
211 print " [ -nofunction funcname [ -nofunction funcname ...] ]\n";
212 print " c source file(s) > outputfile\n";
213 exit 1;
216 # read arguments
217 if ($#ARGV==-1) {
218 usage();
221 my $verbose = 0;
222 my $output_mode = "man";
223 my %highlights = %highlights_man;
224 my $blankline = $blankline_man;
225 my $modulename = "Kernel API";
226 my $function_only = 0;
227 my $man_date = ('January', 'February', 'March', 'April', 'May', 'June',
228 'July', 'August', 'September', 'October',
229 'November', 'December')[(localtime)[4]] .
230 " " . ((localtime)[5]+1900);
232 # Essentially these are globals
233 # They probably want to be tidied up made more localised or summat.
234 # CAVEAT EMPTOR! Some of the others I localised may not want to be which
235 # could cause "use of undefined value" or other bugs.
236 my ($function, %function_table,%parametertypes,$declaration_purpose);
237 my ($type,$declaration_name,$return_type);
238 my ($newsection,$newcontents,$prototype,$filelist, $brcount, %source_map);
240 # Generated docbook code is inserted in a template at a point where
241 # docbook v3.1 requires a non-zero sequence of RefEntry's; see:
242 # http://www.oasis-open.org/docbook/documentation/reference/html/refentry.html
243 # We keep track of number of generated entries and generate a dummy
244 # if needs be to ensure the expanded template can be postprocessed
245 # into html.
246 my $section_counter = 0;
248 my $lineprefix="";
250 # states
251 # 0 - normal code
252 # 1 - looking for function name
253 # 2 - scanning field start.
254 # 3 - scanning prototype.
255 # 4 - documentation block
256 my $state;
258 #declaration types: can be
259 # 'function', 'struct', 'union', 'enum', 'typedef'
260 my $decl_type;
262 my $doc_special = "\@\%\$\&";
264 my $doc_start = '^/\*\*\s*$'; # Allow whitespace at end of comment start.
265 my $doc_end = '\*/';
266 my $doc_com = '\s*\*\s*';
267 my $doc_decl = $doc_com.'(\w+)';
268 my $doc_sect = $doc_com.'(['.$doc_special.']?[\w ]+):(.*)';
269 my $doc_content = $doc_com.'(.*)';
270 my $doc_block = $doc_com.'DOC:\s*(.*)?';
272 my %constants;
273 my %parameterdescs;
274 my @parameterlist;
275 my %sections;
276 my @sectionlist;
278 my $contents = "";
279 my $section_default = "Description"; # default section
280 my $section_intro = "Introduction";
281 my $section = $section_default;
282 my $section_context = "Context";
284 my $undescribed = "-- undescribed --";
286 reset_state();
288 while ($ARGV[0] =~ m/^-(.*)/) {
289 my $cmd = shift @ARGV;
290 if ($cmd eq "-html") {
291 $output_mode = "html";
292 %highlights = %highlights_html;
293 $blankline = $blankline_html;
294 } elsif ($cmd eq "-man") {
295 $output_mode = "man";
296 %highlights = %highlights_man;
297 $blankline = $blankline_man;
298 } elsif ($cmd eq "-text") {
299 $output_mode = "text";
300 %highlights = %highlights_text;
301 $blankline = $blankline_text;
302 } elsif ($cmd eq "-docbook") {
303 $output_mode = "sgml";
304 %highlights = %highlights_sgml;
305 $blankline = $blankline_sgml;
306 } elsif ($cmd eq "-gnome") {
307 $output_mode = "gnome";
308 %highlights = %highlights_gnome;
309 $blankline = $blankline_gnome;
310 } elsif ($cmd eq "-module") { # not needed for sgml, inherits from calling document
311 $modulename = shift @ARGV;
312 } elsif ($cmd eq "-function") { # to only output specific functions
313 $function_only = 1;
314 $function = shift @ARGV;
315 $function_table{$function} = 1;
316 } elsif ($cmd eq "-nofunction") { # to only output specific functions
317 $function_only = 2;
318 $function = shift @ARGV;
319 $function_table{$function} = 1;
320 } elsif ($cmd eq "-v") {
321 $verbose = 1;
322 } elsif (($cmd eq "-h") || ($cmd eq "--help")) {
323 usage();
324 } elsif ($cmd eq '-filelist') {
325 $filelist = shift @ARGV;
330 # generate a sequence of code that will splice in highlighting information
331 # using the s// operator.
332 my $dohighlight = "";
333 foreach my $pattern (keys %highlights) {
334 # print "scanning pattern $pattern ($highlights{$pattern})\n";
335 $dohighlight .= "\$contents =~ s:$pattern:$highlights{$pattern}:gs;\n";
339 # dumps section contents to arrays/hashes intended for that purpose.
341 sub dump_section {
342 my $name = shift;
343 my $contents = join "\n", @_;
345 if ($name =~ m/$type_constant/) {
346 $name = $1;
347 # print STDERR "constant section '$1' = '$contents'\n";
348 $constants{$name} = $contents;
349 } elsif ($name =~ m/$type_param/) {
350 # print STDERR "parameter def '$1' = '$contents'\n";
351 $name = $1;
352 $parameterdescs{$name} = $contents;
353 } else {
354 # print STDERR "other section '$name' = '$contents'\n";
355 $sections{$name} = $contents;
356 push @sectionlist, $name;
361 # output function
363 # parameterdescs, a hash.
364 # function => "function name"
365 # parameterlist => @list of parameters
366 # parameterdescs => %parameter descriptions
367 # sectionlist => @list of sections
368 # sections => %descriont descriptions
371 sub output_highlight {
372 my $contents = join "\n",@_;
373 my $line;
375 # DEBUG
376 # if (!defined $contents) {
377 # use Carp;
378 # confess "output_highlight got called with no args?\n";
381 eval $dohighlight;
382 die $@ if $@;
383 foreach $line (split "\n", $contents) {
384 if ($line eq ""){
385 print $lineprefix, $blankline;
386 } else {
387 $line =~ s/\\\\\\/\&/g;
388 print $lineprefix, $line;
390 print "\n";
394 #output sections in html
395 sub output_section_html(%) {
396 my %args = %{$_[0]};
397 my $section;
399 foreach $section (@{$args{'sectionlist'}}) {
400 print "<h3>$section</h3>\n";
401 print "<blockquote>\n";
402 output_highlight($args{'sections'}{$section});
403 print "</blockquote>\n";
407 # output enum in html
408 sub output_enum_html(%) {
409 my %args = %{$_[0]};
410 my ($parameter);
411 my $count;
412 print "<h2>enum ".$args{'enum'}."</h2>\n";
414 print "<b>enum ".$args{'enum'}."</b> {<br>\n";
415 $count = 0;
416 foreach $parameter (@{$args{'parameterlist'}}) {
417 print " <b>".$parameter."</b>";
418 if ($count != $#{$args{'parameterlist'}}) {
419 $count++;
420 print ",\n";
422 print "<br>";
424 print "};<br>\n";
426 print "<h3>Constants</h3>\n";
427 print "<dl>\n";
428 foreach $parameter (@{$args{'parameterlist'}}) {
429 print "<dt><b>".$parameter."</b>\n";
430 print "<dd>";
431 output_highlight($args{'parameterdescs'}{$parameter});
433 print "</dl>\n";
434 output_section_html(@_);
435 print "<hr>\n";
438 # output tyepdef in html
439 sub output_typedef_html(%) {
440 my %args = %{$_[0]};
441 my ($parameter);
442 my $count;
443 print "<h2>typedef ".$args{'typedef'}."</h2>\n";
445 print "<b>typedef ".$args{'typedef'}."</b>\n";
446 output_section_html(@_);
447 print "<hr>\n";
450 # output struct in html
451 sub output_struct_html(%) {
452 my %args = %{$_[0]};
453 my ($parameter);
455 print "<h2>".$args{'type'}." ".$args{'struct'}."</h2>\n";
456 print "<b>".$args{'type'}." ".$args{'struct'}."</b> {<br>\n";
457 foreach $parameter (@{$args{'parameterlist'}}) {
458 ($args{'parameterdescs'}{$parameter} ne $undescribed) || next;
459 $type = $args{'parametertypes'}{$parameter};
460 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
461 # pointer-to-function
462 print " <i>$1</i><b>$parameter</b>) <i>($2)</i>;<br>\n";
463 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
464 print " <i>$1</i> <b>$parameter</b>$2;<br>\n";
465 } else {
466 print " <i>$type</i> <b>$parameter</b>;<br>\n";
469 print "};<br>\n";
471 print "<h3>Members</h3>\n";
472 print "<dl>\n";
473 foreach $parameter (@{$args{'parameterlist'}}) {
474 ($args{'parameterdescs'}{$parameter} ne $undescribed) || next;
475 print "<dt><b>".$parameter."</b>\n";
476 print "<dd>";
477 output_highlight($args{'parameterdescs'}{$parameter});
479 print "</dl>\n";
480 output_section_html(@_);
481 print "<hr>\n";
484 # output function in html
485 sub output_function_html(%) {
486 my %args = %{$_[0]};
487 my ($parameter, $section);
488 my $count;
489 print "<h2>Function</h2>\n";
491 print "<i>".$args{'functiontype'}."</i>\n";
492 print "<b>".$args{'function'}."</b>\n";
493 print "(";
494 $count = 0;
495 foreach $parameter (@{$args{'parameterlist'}}) {
496 $type = $args{'parametertypes'}{$parameter};
497 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
498 # pointer-to-function
499 print "<i>$1</i><b>$parameter</b>) <i>($2)</i>";
500 } else {
501 print "<i>".$type."</i> <b>".$parameter."</b>";
503 if ($count != $#{$args{'parameterlist'}}) {
504 $count++;
505 print ",\n";
508 print ")\n";
510 print "<h3>Arguments</h3>\n";
511 print "<dl>\n";
512 foreach $parameter (@{$args{'parameterlist'}}) {
513 ($args{'parameterdescs'}{$parameter} ne $undescribed) || next;
514 print "<dt><b>".$parameter."</b>\n";
515 print "<dd>";
516 output_highlight($args{'parameterdescs'}{$parameter});
518 print "</dl>\n";
519 output_section_html(@_);
520 print "<hr>\n";
523 # output intro in html
524 sub output_intro_html(%) {
525 my %args = %{$_[0]};
526 my ($parameter, $section);
527 my $count;
529 foreach $section (@{$args{'sectionlist'}}) {
530 print "<h3>$section</h3>\n";
531 print "<ul>\n";
532 output_highlight($args{'sections'}{$section});
533 print "</ul>\n";
535 print "<hr>\n";
538 sub output_section_sgml(%) {
539 my %args = %{$_[0]};
540 my $section;
541 # print out each section
542 $lineprefix=" ";
543 foreach $section (@{$args{'sectionlist'}}) {
544 print "<refsect1>\n <title>$section</title>\n <para>\n";
545 if ($section =~ m/EXAMPLE/i) {
546 print "<example><para>\n";
548 output_highlight($args{'sections'}{$section});
549 if ($section =~ m/EXAMPLE/i) {
550 print "</para></example>\n";
552 print " </para>\n</refsect1>\n";
556 # output function in sgml DocBook
557 sub output_function_sgml(%) {
558 my %args = %{$_[0]};
559 my ($parameter, $section);
560 my $count;
561 my $id;
563 $id = "API-".$args{'function'};
564 $id =~ s/[^A-Za-z0-9]/-/g;
566 print "<refentry>\n";
567 print "<refmeta>\n";
568 print "<refentrytitle><phrase id=\"$id\">".$args{'function'}."</phrase></refentrytitle>\n";
569 print "</refmeta>\n";
570 print "<refnamediv>\n";
571 print " <refname>".$args{'function'}."</refname>\n";
572 print " <refpurpose>\n";
573 print " ";
574 output_highlight ($args{'purpose'});
575 print " </refpurpose>\n";
576 print "</refnamediv>\n";
578 print "<refsynopsisdiv>\n";
579 print " <title>Synopsis</title>\n";
580 print " <funcsynopsis><funcprototype>\n";
581 print " <funcdef>".$args{'functiontype'}." ";
582 print "<function>".$args{'function'}." </function></funcdef>\n";
584 $count = 0;
585 if ($#{$args{'parameterlist'}} >= 0) {
586 foreach $parameter (@{$args{'parameterlist'}}) {
587 $type = $args{'parametertypes'}{$parameter};
588 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
589 # pointer-to-function
590 print " <paramdef>$1<parameter>$parameter</parameter>)\n";
591 print " <funcparams>$2</funcparams></paramdef>\n";
592 } else {
593 print " <paramdef>".$type;
594 print " <parameter>$parameter</parameter></paramdef>\n";
597 } else {
598 print " <void>\n";
600 print " </funcprototype></funcsynopsis>\n";
601 print "</refsynopsisdiv>\n";
603 # print parameters
604 print "<refsect1>\n <title>Arguments</title>\n";
605 if ($#{$args{'parameterlist'}} >= 0) {
606 print " <variablelist>\n";
607 foreach $parameter (@{$args{'parameterlist'}}) {
608 print " <varlistentry>\n <term><parameter>$parameter</parameter></term>\n";
609 print " <listitem>\n <para>\n";
610 $lineprefix=" ";
611 output_highlight($args{'parameterdescs'}{$parameter});
612 print " </para>\n </listitem>\n </varlistentry>\n";
614 print " </variablelist>\n";
615 } else {
616 print " <para>\n None\n </para>\n";
618 print "</refsect1>\n";
620 output_section_sgml(@_);
621 print "</refentry>\n\n";
624 # output struct in sgml DocBook
625 sub output_struct_sgml(%) {
626 my %args = %{$_[0]};
627 my ($parameter, $section);
628 my $id;
630 $id = "API-struct-".$args{'struct'};
631 $id =~ s/[^A-Za-z0-9]/-/g;
633 print "<refentry>\n";
634 print "<refmeta>\n";
635 print "<refentrytitle><phrase id=\"$id\">".$args{'type'}." ".$args{'struct'}."</phrase></refentrytitle>\n";
636 print "</refmeta>\n";
637 print "<refnamediv>\n";
638 print " <refname>".$args{'type'}." ".$args{'struct'}."</refname>\n";
639 print " <refpurpose>\n";
640 print " ";
641 output_highlight ($args{'purpose'});
642 print " </refpurpose>\n";
643 print "</refnamediv>\n";
645 print "<refsynopsisdiv>\n";
646 print " <title>Synopsis</title>\n";
647 print " <programlisting>\n";
648 print $args{'type'}." ".$args{'struct'}." {\n";
649 foreach $parameter (@{$args{'parameterlist'}}) {
650 defined($args{'parameterdescs'}{$parameter}) || next;
651 ($args{'parameterdescs'}{$parameter} ne $undescribed) || next;
652 $type = $args{'parametertypes'}{$parameter};
653 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
654 # pointer-to-function
655 print " $1 $parameter ($2);\n";
656 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
657 print " $1 $parameter$2;\n";
658 } else {
659 print " ".$type." ".$parameter.";\n";
662 print "};";
663 print " </programlisting>\n";
664 print "</refsynopsisdiv>\n";
666 print " <refsect1>\n";
667 print " <title>Members</title>\n";
669 print " <variablelist>\n";
670 foreach $parameter (@{$args{'parameterlist'}}) {
671 defined($args{'parameterdescs'}{$parameter}) || next;
672 ($args{'parameterdescs'}{$parameter} ne $undescribed) || next;
673 print " <varlistentry>";
674 print " <term>$parameter</term>\n";
675 print " <listitem><para>\n";
676 output_highlight($args{'parameterdescs'}{$parameter});
677 print " </para></listitem>\n";
678 print " </varlistentry>\n";
680 print " </variablelist>\n";
681 print " </refsect1>\n";
683 output_section_sgml(@_);
685 print "</refentry>\n\n";
688 # output enum in sgml DocBook
689 sub output_enum_sgml(%) {
690 my %args = %{$_[0]};
691 my ($parameter, $section);
692 my $count;
693 my $id;
695 $id = "API-enum-".$args{'enum'};
696 $id =~ s/[^A-Za-z0-9]/-/g;
698 print "<refentry>\n";
699 print "<refmeta>\n";
700 print "<refentrytitle><phrase id=\"$id\">enum ".$args{'enum'}."</phrase></refentrytitle>\n";
701 print "</refmeta>\n";
702 print "<refnamediv>\n";
703 print " <refname>enum ".$args{'enum'}."</refname>\n";
704 print " <refpurpose>\n";
705 print " ";
706 output_highlight ($args{'purpose'});
707 print " </refpurpose>\n";
708 print "</refnamediv>\n";
710 print "<refsynopsisdiv>\n";
711 print " <title>Synopsis</title>\n";
712 print " <programlisting>\n";
713 print "enum ".$args{'enum'}." {\n";
714 $count = 0;
715 foreach $parameter (@{$args{'parameterlist'}}) {
716 print " $parameter";
717 if ($count != $#{$args{'parameterlist'}}) {
718 $count++;
719 print ",";
721 print "\n";
723 print "};";
724 print " </programlisting>\n";
725 print "</refsynopsisdiv>\n";
727 print "<refsect1>\n";
728 print " <title>Constants</title>\n";
729 print " <variablelist>\n";
730 foreach $parameter (@{$args{'parameterlist'}}) {
731 print " <varlistentry>";
732 print " <term>$parameter</term>\n";
733 print " <listitem><para>\n";
734 output_highlight($args{'parameterdescs'}{$parameter});
735 print " </para></listitem>\n";
736 print " </varlistentry>\n";
738 print " </variablelist>\n";
739 print "</refsect1>\n";
741 output_section_sgml(@_);
743 print "</refentry>\n\n";
746 # output typedef in sgml DocBook
747 sub output_typedef_sgml(%) {
748 my %args = %{$_[0]};
749 my ($parameter, $section);
750 my $id;
752 $id = "API-typedef-".$args{'typedef'};
753 $id =~ s/[^A-Za-z0-9]/-/g;
755 print "<refentry>\n";
756 print "<refmeta>\n";
757 print "<refentrytitle><phrase id=\"$id\">typedef ".$args{'typedef'}."</phrase></refentrytitle>\n";
758 print "</refmeta>\n";
759 print "<refnamediv>\n";
760 print " <refname>typedef ".$args{'typedef'}."</refname>\n";
761 print " <refpurpose>\n";
762 print " ";
763 output_highlight ($args{'purpose'});
764 print " </refpurpose>\n";
765 print "</refnamediv>\n";
767 print "<refsynopsisdiv>\n";
768 print " <title>Synopsis</title>\n";
769 print " <synopsis>typedef ".$args{'typedef'}.";</synopsis>\n";
770 print "</refsynopsisdiv>\n";
772 output_section_sgml(@_);
774 print "</refentry>\n\n";
777 # output in sgml DocBook
778 sub output_intro_sgml(%) {
779 my %args = %{$_[0]};
780 my ($parameter, $section);
781 my $count;
783 my $id = $args{'module'};
784 $id =~ s/[^A-Za-z0-9]/-/g;
786 # print out each section
787 $lineprefix=" ";
788 foreach $section (@{$args{'sectionlist'}}) {
789 print "<refsect1>\n <title>$section</title>\n <para>\n";
790 if ($section =~ m/EXAMPLE/i) {
791 print "<example><para>\n";
793 output_highlight($args{'sections'}{$section});
794 if ($section =~ m/EXAMPLE/i) {
795 print "</para></example>\n";
797 print " </para>\n</refsect1>\n";
800 print "\n\n";
803 # output in sgml DocBook
804 sub output_function_gnome {
805 my %args = %{$_[0]};
806 my ($parameter, $section);
807 my $count;
808 my $id;
810 $id = $args{'module'}."-".$args{'function'};
811 $id =~ s/[^A-Za-z0-9]/-/g;
813 print "<sect2>\n";
814 print " <title id=\"$id\">".$args{'function'}."</title>\n";
816 print " <funcsynopsis>\n";
817 print " <funcdef>".$args{'functiontype'}." ";
818 print "<function>".$args{'function'}." ";
819 print "</function></funcdef>\n";
821 $count = 0;
822 if ($#{$args{'parameterlist'}} >= 0) {
823 foreach $parameter (@{$args{'parameterlist'}}) {
824 $type = $args{'parametertypes'}{$parameter};
825 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
826 # pointer-to-function
827 print " <paramdef>$1 <parameter>$parameter</parameter>)\n";
828 print " <funcparams>$2</funcparams></paramdef>\n";
829 } else {
830 print " <paramdef>".$type;
831 print " <parameter>$parameter</parameter></paramdef>\n";
834 } else {
835 print " <void>\n";
837 print " </funcsynopsis>\n";
838 if ($#{$args{'parameterlist'}} >= 0) {
839 print " <informaltable pgwide=\"1\" frame=\"none\" role=\"params\">\n";
840 print "<tgroup cols=\"2\">\n";
841 print "<colspec colwidth=\"2*\">\n";
842 print "<colspec colwidth=\"8*\">\n";
843 print "<tbody>\n";
844 foreach $parameter (@{$args{'parameterlist'}}) {
845 print " <row><entry align=\"right\"><parameter>$parameter</parameter></entry>\n";
846 print " <entry>\n";
847 $lineprefix=" ";
848 output_highlight($args{'parameterdescs'}{$parameter});
849 print " </entry></row>\n";
851 print " </tbody></tgroup></informaltable>\n";
852 } else {
853 print " <para>\n None\n </para>\n";
856 # print out each section
857 $lineprefix=" ";
858 foreach $section (@{$args{'sectionlist'}}) {
859 print "<simplesect>\n <title>$section</title>\n";
860 if ($section =~ m/EXAMPLE/i) {
861 print "<example><programlisting>\n";
862 } else {
864 print "<para>\n";
865 output_highlight($args{'sections'}{$section});
866 print "</para>\n";
867 if ($section =~ m/EXAMPLE/i) {
868 print "</programlisting></example>\n";
869 } else {
871 print " </simplesect>\n";
874 print "</sect2>\n\n";
878 # output function in man
879 sub output_function_man(%) {
880 my %args = %{$_[0]};
881 my ($parameter, $section);
882 my $count;
884 print ".TH \"$args{'function'}\" 9 \"$args{'function'}\" \"$man_date\" \"Kernel Hacker's Manual\" LINUX\n";
886 print ".SH NAME\n";
887 print $args{'function'}." \\- ".$args{'purpose'}."\n";
889 print ".SH SYNOPSIS\n";
890 print ".B \"".$args{'functiontype'}."\" ".$args{'function'}."\n";
891 $count = 0;
892 my $parenth = "(";
893 my $post = ",";
894 foreach my $parameter (@{$args{'parameterlist'}}) {
895 if ($count == $#{$args{'parameterlist'}}) {
896 $post = ");";
898 $type = $args{'parametertypes'}{$parameter};
899 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
900 # pointer-to-function
901 print ".BI \"".$parenth.$1."\" ".$parameter." \") (".$2.")".$post."\"\n";
902 } else {
903 $type =~ s/([^\*])$/$1 /;
904 print ".BI \"".$parenth.$type."\" ".$parameter." \"".$post."\"\n";
906 $count++;
907 $parenth = "";
910 print ".SH ARGUMENTS\n";
911 foreach $parameter (@{$args{'parameterlist'}}) {
912 print ".IP \"".$parameter."\" 12\n";
913 output_highlight($args{'parameterdescs'}{$parameter});
915 foreach $section (@{$args{'sectionlist'}}) {
916 print ".SH \"", uc $section, "\"\n";
917 output_highlight($args{'sections'}{$section});
922 # output enum in man
923 sub output_enum_man(%) {
924 my %args = %{$_[0]};
925 my ($parameter, $section);
926 my $count;
928 print ".TH \"$args{'module'}\" 9 \"enum $args{'enum'}\" \"$man_date\" \"API Manual\" LINUX\n";
930 print ".SH NAME\n";
931 print "enum ".$args{'enum'}." \\- ".$args{'purpose'}."\n";
933 print ".SH SYNOPSIS\n";
934 print "enum ".$args{'enum'}." {\n";
935 $count = 0;
936 foreach my $parameter (@{$args{'parameterlist'}}) {
937 print ".br\n.BI \" $parameter\"\n";
938 if ($count == $#{$args{'parameterlist'}}) {
939 print "\n};\n";
940 last;
942 else {
943 print ", \n.br\n";
945 $count++;
948 print ".SH Constants\n";
949 foreach $parameter (@{$args{'parameterlist'}}) {
950 print ".IP \"".$parameter."\" 12\n";
951 output_highlight($args{'parameterdescs'}{$parameter});
953 foreach $section (@{$args{'sectionlist'}}) {
954 print ".SH \"$section\"\n";
955 output_highlight($args{'sections'}{$section});
960 # output struct in man
961 sub output_struct_man(%) {
962 my %args = %{$_[0]};
963 my ($parameter, $section);
965 print ".TH \"$args{'module'}\" 9 \"".$args{'type'}." ".$args{'struct'}."\" \"$man_date\" \"API Manual\" LINUX\n";
967 print ".SH NAME\n";
968 print $args{'type'}." ".$args{'struct'}." \\- ".$args{'purpose'}."\n";
970 print ".SH SYNOPSIS\n";
971 print $args{'type'}." ".$args{'struct'}." {\n";
973 foreach my $parameter (@{$args{'parameterlist'}}) {
974 ($args{'parameterdescs'}{$parameter} ne $undescribed) || next;
975 print "\n.br\n";
976 $type = $args{'parametertypes'}{$parameter};
977 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
978 # pointer-to-function
979 print ".BI \" ".$1."\" ".$parameter." \") (".$2.")"."\"\n;\n";
980 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
981 print ".BI \" ".$1."\" ".$parameter.$2." \""."\"\n;\n";
982 } else {
983 $type =~ s/([^\*])$/$1 /;
984 print ".BI \" ".$type."\" ".$parameter." \""."\"\n;\n";
986 print "\n.br\n";
988 print "};\n.br\n";
990 print ".SH Arguments\n";
991 foreach $parameter (@{$args{'parameterlist'}}) {
992 ($args{'parameterdescs'}{$parameter} ne $undescribed) || next;
993 print ".IP \"".$parameter."\" 12\n";
994 output_highlight($args{'parameterdescs'}{$parameter});
996 foreach $section (@{$args{'sectionlist'}}) {
997 print ".SH \"$section\"\n";
998 output_highlight($args{'sections'}{$section});
1003 # output typedef in man
1004 sub output_typedef_man(%) {
1005 my %args = %{$_[0]};
1006 my ($parameter, $section);
1008 print ".TH \"$args{'module'}\" 9 \"$args{'typedef'}\" \"$man_date\" \"API Manual\" LINUX\n";
1010 print ".SH NAME\n";
1011 print "typedef ".$args{'typedef'}." \\- ".$args{'purpose'}."\n";
1013 foreach $section (@{$args{'sectionlist'}}) {
1014 print ".SH \"$section\"\n";
1015 output_highlight($args{'sections'}{$section});
1019 sub output_intro_man(%) {
1020 my %args = %{$_[0]};
1021 my ($parameter, $section);
1022 my $count;
1024 print ".TH \"$args{'module'}\" 9 \"$args{'module'}\" \"$man_date\" \"API Manual\" LINUX\n";
1026 foreach $section (@{$args{'sectionlist'}}) {
1027 print ".SH \"$section\"\n";
1028 output_highlight($args{'sections'}{$section});
1033 # output in text
1034 sub output_function_text(%) {
1035 my %args = %{$_[0]};
1036 my ($parameter, $section);
1038 print "Function:\n\n";
1039 my $start=$args{'functiontype'}." ".$args{'function'}." (";
1040 print $start;
1041 my $count = 0;
1042 foreach my $parameter (@{$args{'parameterlist'}}) {
1043 $type = $args{'parametertypes'}{$parameter};
1044 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1045 # pointer-to-function
1046 print $1.$parameter.") (".$2;
1047 } else {
1048 print $type." ".$parameter;
1050 if ($count != $#{$args{'parameterlist'}}) {
1051 $count++;
1052 print ",\n";
1053 print " " x length($start);
1054 } else {
1055 print ");\n\n";
1059 print "Arguments:\n\n";
1060 foreach $parameter (@{$args{'parameterlist'}}) {
1061 print $parameter."\n\t".$args{'parameterdescs'}{$parameter}."\n";
1063 output_section_text(@_);
1066 #output sections in text
1067 sub output_section_text(%) {
1068 my %args = %{$_[0]};
1069 my $section;
1071 print "\n";
1072 foreach $section (@{$args{'sectionlist'}}) {
1073 print "$section:\n\n";
1074 output_highlight($args{'sections'}{$section});
1076 print "\n\n";
1079 # output enum in text
1080 sub output_enum_text(%) {
1081 my %args = %{$_[0]};
1082 my ($parameter);
1083 my $count;
1084 print "Enum:\n\n";
1086 print "enum ".$args{'enum'}." {\n";
1087 $count = 0;
1088 foreach $parameter (@{$args{'parameterlist'}}) {
1089 print "\t$parameter";
1090 if ($count != $#{$args{'parameterlist'}}) {
1091 $count++;
1092 print ",";
1094 print "\n";
1096 print "};\n\n";
1098 print "Constants:\n\n";
1099 foreach $parameter (@{$args{'parameterlist'}}) {
1100 print "$parameter\n\t";
1101 print $args{'parameterdescs'}{$parameter}."\n";
1104 output_section_text(@_);
1107 # output typedef in text
1108 sub output_typedef_text(%) {
1109 my %args = %{$_[0]};
1110 my ($parameter);
1111 my $count;
1112 print "Typedef:\n\n";
1114 print "typedef ".$args{'typedef'}."\n";
1115 output_section_text(@_);
1118 # output struct as text
1119 sub output_struct_text(%) {
1120 my %args = %{$_[0]};
1121 my ($parameter);
1123 print $args{'type'}." ".$args{'struct'}.":\n\n";
1124 print $args{'type'}." ".$args{'struct'}." {\n";
1125 foreach $parameter (@{$args{'parameterlist'}}) {
1126 ($args{'parameterdescs'}{$parameter} ne $undescribed) || next;
1127 $type = $args{'parametertypes'}{$parameter};
1128 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1129 # pointer-to-function
1130 print "\t$1 $parameter) ($2);\n";
1131 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
1132 print "\t$1 $parameter$2;\n";
1133 } else {
1134 print "\t".$type." ".$parameter.";\n";
1137 print "};\n\n";
1139 print "Members:\n\n";
1140 foreach $parameter (@{$args{'parameterlist'}}) {
1141 ($args{'parameterdescs'}{$parameter} ne $undescribed) || next;
1142 print "$parameter\n\t";
1143 print $args{'parameterdescs'}{$parameter}."\n";
1145 print "\n";
1146 output_section_text(@_);
1149 sub output_intro_text(%) {
1150 my %args = %{$_[0]};
1151 my ($parameter, $section);
1153 foreach $section (@{$args{'sectionlist'}}) {
1154 print " $section:\n";
1155 print " -> ";
1156 output_highlight($args{'sections'}{$section});
1161 # generic output function for typedefs
1162 sub output_declaration {
1163 no strict 'refs';
1164 my $name = shift;
1165 my $functype = shift;
1166 my $func = "output_${functype}_$output_mode";
1167 if (($function_only==0) ||
1168 ( $function_only == 1 && defined($function_table{$name})) ||
1169 ( $function_only == 2 && !defined($function_table{$name})))
1171 &$func(@_);
1172 $section_counter++;
1177 # generic output function - calls the right one based
1178 # on current output mode.
1179 sub output_intro {
1180 no strict 'refs';
1181 my $func = "output_intro_".$output_mode;
1182 &$func(@_);
1183 $section_counter++;
1187 # takes a declaration (struct, union, enum, typedef) and
1188 # invokes the right handler. NOT called for functions.
1189 sub dump_declaration($$) {
1190 no strict 'refs';
1191 my ($prototype, $file) = @_;
1192 my $func = "dump_".$decl_type;
1193 &$func(@_);
1196 sub dump_union($$) {
1197 dump_struct(@_);
1200 sub dump_struct($$) {
1201 my $x = shift;
1202 my $file = shift;
1204 if ($x =~/(struct|union)\s+(\w+)\s*{(.*)}/) {
1205 $declaration_name = $2;
1206 my $members = $3;
1208 # ignore embedded structs or unions
1209 $members =~ s/{.*}//g;
1211 create_parameterlist($members, ';', $file);
1213 output_declaration($declaration_name,
1214 'struct',
1215 {'struct' => $declaration_name,
1216 'module' => $modulename,
1217 'parameterlist' => \@parameterlist,
1218 'parameterdescs' => \%parameterdescs,
1219 'parametertypes' => \%parametertypes,
1220 'sectionlist' => \@sectionlist,
1221 'sections' => \%sections,
1222 'purpose' => $declaration_purpose,
1223 'type' => $decl_type
1226 else {
1227 print STDERR "Error(${file}:$.): Cannot parse struct or union!\n";
1228 ++$errors;
1232 sub dump_enum($$) {
1233 my $x = shift;
1234 my $file = shift;
1236 if ($x =~ /enum\s+(\w+)\s*{(.*)}/) {
1237 $declaration_name = $1;
1238 my $members = $2;
1240 foreach my $arg (split ',', $members) {
1241 $arg =~ s/^\s*(\w+).*/$1/;
1242 push @parameterlist, $arg;
1243 if (!$parameterdescs{$arg}) {
1244 $parameterdescs{$arg} = $undescribed;
1245 print STDERR "Warning(${file}:$.): Enum value '$arg' ".
1246 "not described in enum '$declaration_name'\n";
1251 output_declaration($declaration_name,
1252 'enum',
1253 {'enum' => $declaration_name,
1254 'module' => $modulename,
1255 'parameterlist' => \@parameterlist,
1256 'parameterdescs' => \%parameterdescs,
1257 'sectionlist' => \@sectionlist,
1258 'sections' => \%sections,
1259 'purpose' => $declaration_purpose
1262 else {
1263 print STDERR "Error(${file}:$.): Cannot parse enum!\n";
1264 ++$errors;
1268 sub dump_typedef($$) {
1269 my $x = shift;
1270 my $file = shift;
1272 while (($x =~ /\(*.\)\s*;$/) || ($x =~ /\[*.\]\s*;$/)) {
1273 $x =~ s/\(*.\)\s*;$/;/;
1274 $x =~ s/\[*.\]\s*;$/;/;
1277 if ($x =~ /typedef.*\s+(\w+)\s*;/) {
1278 $declaration_name = $1;
1280 output_declaration($declaration_name,
1281 'typedef',
1282 {'typedef' => $declaration_name,
1283 'module' => $modulename,
1284 'sectionlist' => \@sectionlist,
1285 'sections' => \%sections,
1286 'purpose' => $declaration_purpose
1289 else {
1290 print STDERR "Error(${file}:$.): Cannot parse typedef!\n";
1291 ++$errors;
1295 sub create_parameterlist($$$) {
1296 my $args = shift;
1297 my $splitter = shift;
1298 my $file = shift;
1299 my $type;
1300 my $param;
1302 while ($args =~ /(\([^\),]+),/) {
1303 $args =~ s/(\([^\),]+),/$1#/g;
1306 foreach my $arg (split($splitter, $args)) {
1307 # strip leading/trailing spaces
1308 $arg =~ s/^\s*//;
1309 $arg =~ s/\s*$//;
1310 $arg =~ s/\s+/ /;
1312 if ($arg =~ m/\(/) {
1313 # pointer-to-function
1314 $arg =~ tr/#/,/;
1315 $arg =~ m/[^\(]+\(\*([^\)]+)\)/;
1316 $param = $1;
1317 $type = $arg;
1318 $type =~ s/([^\(]+\(\*)$param/$1/;
1319 } else {
1320 # evil magic to get fixed array parameters to work
1321 $arg =~ s/(.+\s+)(.+)\[.*/$1* $2/;
1322 my @args = split('\s', $arg);
1324 $param = pop @args;
1325 if ($param =~ m/^(\*+)(.*)/) {
1326 $param = $2;
1327 push @args, $1;
1329 elsif ($param =~ m/(.*?)\s*:\s*(\d+)/) {
1330 $param = $1;
1331 push @args, ":$2";
1333 $type = join " ", @args;
1336 if ($type eq "" && $param eq "...")
1338 $type="...";
1339 $param="...";
1340 $parameterdescs{"..."} = "variable arguments";
1342 elsif ($type eq "" && ($param eq "" or $param eq "void"))
1344 $type="";
1345 $param="void";
1346 $parameterdescs{void} = "no arguments";
1348 if (defined $type && $type && !defined $parameterdescs{$param}) {
1349 $parameterdescs{$param} = $undescribed;
1351 if (($type eq 'function') || ($type eq 'enum')) {
1352 print STDERR "Warning(${file}:$.): Function parameter ".
1353 "or member '$param' not " .
1354 "described in '$declaration_name'\n";
1356 print STDERR "Warning(${file}:$.):".
1357 " No description found for parameter '$param'\n";
1358 ++$warnings;
1361 push @parameterlist, $param;
1362 $parametertypes{$param} = $type;
1367 # takes a function prototype and the name of the current file being
1368 # processed and spits out all the details stored in the global
1369 # arrays/hashes.
1370 sub dump_function($$) {
1371 my $prototype = shift;
1372 my $file = shift;
1374 $prototype =~ s/^static +//;
1375 $prototype =~ s/^extern +//;
1376 $prototype =~ s/^inline +//;
1377 $prototype =~ s/^__inline__ +//;
1378 $prototype =~ s/^#define +//; #ak added
1380 # Yes, this truly is vile. We are looking for:
1381 # 1. Return type (may be nothing if we're looking at a macro)
1382 # 2. Function name
1383 # 3. Function parameters.
1385 # All the while we have to watch out for function pointer parameters
1386 # (which IIRC is what the two sections are for), C types (these
1387 # regexps don't even start to express all the possibilities), and
1388 # so on.
1390 # If you mess with these regexps, it's a good idea to check that
1391 # the following functions' documentation still comes out right:
1392 # - parport_register_device (function pointer parameters)
1393 # - atomic_set (macro)
1394 # - pci_match_device (long return type)
1396 if ($prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1397 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1398 $prototype =~ m/^(\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1399 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1400 $prototype =~ m/^(\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1401 $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1402 $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1403 $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1404 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1405 $prototype =~ m/^(\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1406 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1407 $prototype =~ m/^(\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1408 $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1409 $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/) {
1410 $return_type = $1;
1411 $declaration_name = $2;
1412 my $args = $3;
1414 create_parameterlist($args, ',', $file);
1415 } else {
1416 print STDERR "Error(${file}:$.): cannot understand prototype: '$prototype'\n";
1417 ++$errors;
1418 return;
1421 output_declaration($declaration_name,
1422 'function',
1423 {'function' => $declaration_name,
1424 'module' => $modulename,
1425 'functiontype' => $return_type,
1426 'parameterlist' => \@parameterlist,
1427 'parameterdescs' => \%parameterdescs,
1428 'parametertypes' => \%parametertypes,
1429 'sectionlist' => \@sectionlist,
1430 'sections' => \%sections,
1431 'purpose' => $declaration_purpose
1435 sub process_file($);
1437 # Read the file that maps relative names to absolute names for
1438 # separate source and object directories and for shadow trees.
1439 if (open(SOURCE_MAP, "<.tmp_filelist.txt")) {
1440 my ($relname, $absname);
1441 while(<SOURCE_MAP>) {
1442 chop();
1443 ($relname, $absname) = (split())[0..1];
1444 $relname =~ s:^/+::;
1445 $source_map{$relname} = $absname;
1447 close(SOURCE_MAP);
1450 if ($filelist) {
1451 open(FLIST,"<$filelist") or die "Can't open file list $filelist";
1452 while(<FLIST>) {
1453 chop;
1454 process_file($_);
1458 foreach (@ARGV) {
1459 chomp;
1460 process_file($_);
1462 if ($verbose && $errors) {
1463 print STDERR "$errors errors\n";
1465 if ($verbose && $warnings) {
1466 print STDERR "$warnings warnings\n";
1469 exit($errors);
1471 sub reset_state {
1472 $function = "";
1473 %constants = ();
1474 %parameterdescs = ();
1475 %parametertypes = ();
1476 @parameterlist = ();
1477 %sections = ();
1478 @sectionlist = ();
1479 $prototype = "";
1481 $state = 0;
1484 sub process_state3_function($$) {
1485 my $x = shift;
1486 my $file = shift;
1488 if ($x =~ m#\s*/\*\s+MACDOC\s*#io) {
1489 # do nothing
1491 elsif ($x =~ /([^\{]*)/) {
1492 $prototype .= $1;
1494 if (($x =~ /\{/) || ($x =~ /\#/) || ($x =~ /;/)) {
1495 $prototype =~ s@/\*.*?\*/@@gos; # strip comments.
1496 $prototype =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1497 $prototype =~ s@^\s+@@gos; # strip leading spaces
1498 dump_function($prototype,$file);
1499 reset_state();
1503 sub process_state3_type($$) {
1504 my $x = shift;
1505 my $file = shift;
1507 $x =~ s@/\*.*?\*/@@gos; # strip comments.
1508 $x =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1509 $x =~ s@^\s+@@gos; # strip leading spaces
1510 $x =~ s@\s+$@@gos; # strip trailing spaces
1512 while (1) {
1513 if ( $x =~ /([^{};]*)([{};])(.*)/ ) {
1514 $prototype .= $1 . $2;
1515 ($2 eq '{') && $brcount++;
1516 ($2 eq '}') && $brcount--;
1517 if (($2 eq ';') && ($brcount == 0)) {
1518 dump_declaration($prototype,$file);
1519 reset_state();
1520 last;
1522 $x = $3;
1523 } else {
1524 $prototype .= $x;
1525 last;
1530 sub process_file($) {
1531 my ($file) = @_;
1532 my $identifier;
1533 my $func;
1534 my $initial_section_counter = $section_counter;
1536 if (defined($source_map{$file})) {
1537 $file = $source_map{$file};
1540 if (!open(IN,"<$file")) {
1541 print STDERR "Error: Cannot open file $file\n";
1542 ++$errors;
1543 return;
1546 $section_counter = 0;
1547 while (<IN>) {
1548 if ($state == 0) {
1549 if (/$doc_start/o) {
1550 $state = 1; # next line is always the function name
1552 } elsif ($state == 1) { # this line is the function name (always)
1553 if (/$doc_block/o) {
1554 $state = 4;
1555 $contents = "";
1556 if ( $1 eq "" ) {
1557 $section = $section_intro;
1558 } else {
1559 $section = $1;
1562 elsif (/$doc_decl/o) {
1563 $identifier = $1;
1564 if (/\s*([\w\s]+?)\s*-/) {
1565 $identifier = $1;
1568 $state = 2;
1569 if (/-(.*)/) {
1570 $declaration_purpose = $1;
1571 } else {
1572 $declaration_purpose = "";
1574 if ($identifier =~ m/^struct/) {
1575 $decl_type = 'struct';
1576 } elsif ($identifier =~ m/^union/) {
1577 $decl_type = 'union';
1578 } elsif ($identifier =~ m/^enum/) {
1579 $decl_type = 'enum';
1580 } elsif ($identifier =~ m/^typedef/) {
1581 $decl_type = 'typedef';
1582 } else {
1583 $decl_type = 'function';
1586 if ($verbose) {
1587 print STDERR "Info(${file}:$.): Scanning doc for $identifier\n";
1589 } else {
1590 print STDERR "Warning(${file}:$.): Cannot understand $_ on line $.",
1591 " - I thought it was a doc line\n";
1592 ++$warnings;
1593 $state = 0;
1595 } elsif ($state == 2) { # look for head: lines, and include content
1596 if (/$doc_sect/o) {
1597 $newsection = $1;
1598 $newcontents = $2;
1600 if ($contents ne "") {
1601 $contents =~ s/\&/\\\\\\amp;/g;
1602 $contents =~ s/\</\\\\\\lt;/g;
1603 $contents =~ s/\>/\\\\\\gt;/g;
1604 dump_section($section, $contents);
1605 $section = $section_default;
1608 $contents = $newcontents;
1609 if ($contents ne "") {
1610 $contents .= "\n";
1612 $section = $newsection;
1613 } elsif (/$doc_end/) {
1615 if ($contents ne "") {
1616 $contents =~ s/\&/\\\\\\amp;/g;
1617 $contents =~ s/\</\\\\\\lt;/g;
1618 $contents =~ s/\>/\\\\\\gt;/g;
1619 dump_section($section, $contents);
1620 $section = $section_default;
1621 $contents = "";
1624 $prototype = "";
1625 $state = 3;
1626 $brcount = 0;
1627 # print STDERR "end of doc comment, looking for prototype\n";
1628 } elsif (/$doc_content/) {
1629 # miguel-style comment kludge, look for blank lines after
1630 # @parameter line to signify start of description
1631 if ($1 eq "" &&
1632 ($section =~ m/^@/ || $section eq $section_context)) {
1633 $contents =~ s/\&/\\\\\\amp;/g;
1634 $contents =~ s/\</\\\\\\lt;/g;
1635 $contents =~ s/\>/\\\\\\gt;/g;
1636 dump_section($section, $contents);
1637 $section = $section_default;
1638 $contents = "";
1639 } else {
1640 $contents .= $1."\n";
1642 } else {
1643 # i dont know - bad line? ignore.
1644 print STDERR "Warning(${file}:$.): bad line: $_";
1645 ++$warnings;
1647 } elsif ($state == 3) { # scanning for function { (end of prototype)
1648 if ($decl_type eq 'function') {
1649 process_state3_function($_, $file);
1650 } else {
1651 process_state3_type($_, $file);
1653 } elsif ($state == 4) {
1654 # Documentation block
1655 if (/$doc_block/) {
1656 dump_section($section, $contents);
1657 output_intro({'sectionlist' => \@sectionlist,
1658 'sections' => \%sections });
1659 $contents = "";
1660 $function = "";
1661 %constants = ();
1662 %parameterdescs = ();
1663 %parametertypes = ();
1664 @parameterlist = ();
1665 %sections = ();
1666 @sectionlist = ();
1667 $prototype = "";
1668 if ( $1 eq "" ) {
1669 $section = $section_intro;
1670 } else {
1671 $section = $1;
1674 elsif (/$doc_end/)
1676 dump_section($section, $contents);
1677 output_intro({'sectionlist' => \@sectionlist,
1678 'sections' => \%sections });
1679 $contents = "";
1680 $function = "";
1681 %constants = ();
1682 %parameterdescs = ();
1683 %parametertypes = ();
1684 @parameterlist = ();
1685 %sections = ();
1686 @sectionlist = ();
1687 $prototype = "";
1688 $state = 0;
1690 elsif (/$doc_content/)
1692 if ( $1 eq "" )
1694 $contents .= $blankline;
1696 else
1698 $contents .= $1 . "\n";
1703 if ($initial_section_counter == $section_counter) {
1704 print STDERR "Warning(${file}): no structured comments found\n";
1705 if ($output_mode eq "sgml") {
1706 # The template wants at least one RefEntry here; make one.
1707 print "<refentry>\n";
1708 print " <refnamediv>\n";
1709 print " <refname>\n";
1710 print " ${file}\n";
1711 print " </refname>\n";
1712 print " <refpurpose>\n";
1713 print " Document generation inconsistency\n";
1714 print " </refpurpose>\n";
1715 print " </refnamediv>\n";
1716 print " <refsect1>\n";
1717 print " <title>\n";
1718 print " Oops\n";
1719 print " </title>\n";
1720 print " <warning>\n";
1721 print " <para>\n";
1722 print " The template for this document tried to insert\n";
1723 print " the structured comment from the file\n";
1724 print " <filename>${file}</filename> at this point,\n";
1725 print " but none was found.\n";
1726 print " This dummy section is inserted to allow\n";
1727 print " generation to continue.\n";
1728 print " </para>\n";
1729 print " </warning>\n";
1730 print " </refsect1>\n";
1731 print "</refentry>\n";