kernel-doc: html mode struct highlights
[safe/jmp/linux-2.6] / scripts / kernel-doc
1 #!/usr/bin/perl -w
2
3 use strict;
4
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             ##
14
15 # w.o. 03-11-2000: added the '-filelist' option.
16
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
21
22 # 27/06/2001 -  Allowed whitespace after initial "/**" and
23 #               allowed comments before function declarations.
24 # -- Christian Kreibich <ck@whoop.org>
25
26 # Still to do:
27 #       - add perldoc documentation
28 #       - Look more closely at some of the scarier bits :)
29
30 # 26/05/2001 -  Support for separate source and object trees.
31 #               Return error code.
32 #               Keith Owens <kaos@ocs.com.au>
33
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>
38
39
40 #
41 # This will read a 'c' file and scan for embedded comments in the
42 # style of gnome comments (+minor extensions - see below).
43 #
44
45 # Note: This only supports 'c'.
46
47 # usage:
48 # kernel-doc [ -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
52 #
53 #  Set output format using one of -docbook -html -text or -man.  Default is man.
54 #
55 #  -function funcname
56 #       If set, then only generate documentation for the given function(s).  All
57 #       other functions are ignored.
58 #
59 #  -nofunction funcname
60 #       If set, then only generate documentation for the other function(s).
61 #       Cannot be used together with -function
62 #       (yes, that's a bug -- perl hackers can fix it 8))
63 #
64 #  c files - list of 'c' files to process
65 #
66 #  All output goes to stdout, with errors to stderr.
67
68 #
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 #  (*)?*/
79 #
80 # So .. the trivial example would be:
81 #
82 # /**
83 #  * my_function
84 #  **/
85 #
86 # If the Description: header tag is omitted, 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 #  */
95 #
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.
103 #
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.
109 # e.g.
110 # /**
111 #  * struct my_struct - short description
112 #  * @a: first member
113 #  * @b: second member
114 #  *
115 #  * Longer description
116 #  */
117 # struct my_struct {
118 #     int a;
119 #     int b;
120 # /* private: */
121 #     int c;
122 # };
123 #
124 # All descriptions can be multiline, except the short function description.
125 #
126 # You can also add additional sections. When documenting kernel functions you
127 # should document the "Context:" of the function, e.g. whether the functions
128 # can be called form interrupts. Unlike other sections you can end it with an
129 # empty line.
130 # Example-sections should contain the string EXAMPLE so that they are marked
131 # appropriately in DocBook.
132 #
133 # Example:
134 # /**
135 #  * user_function - function that can only be called in user context
136 #  * @a: some argument
137 #  * Context: !in_interrupt()
138 #  *
139 #  * Some description
140 #  * Example:
141 #  *    user_function(22);
142 #  */
143 # ...
144 #
145 #
146 # All descriptive text is further processed, scanning for the following special
147 # patterns, which are highlighted appropriately.
148 #
149 # 'funcname()' - function
150 # '$ENVVAR' - environmental variable
151 # '&struct_name' - name of a structure (up to two words including 'struct')
152 # '@parameter' - name of a parameter
153 # '%CONST' - name of a constant.
154
155 my $errors = 0;
156 my $warnings = 0;
157
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_struct_xml = '\\\amp;((struct\s*)*[_\w]+)';
164 my $type_env = '(\$\w+)';
165
166 # Output conversion substitutions.
167 #  One for each output format
168
169 # these work fairly well
170 my %highlights_html = ( $type_constant, "<i>\$1</i>",
171                         $type_func, "<b>\$1</b>",
172                         $type_struct_xml, "<i>\$1</i>",
173                         $type_env, "<b><i>\$1</i></b>",
174                         $type_param, "<tt><b>\$1</b></tt>" );
175 my $blankline_html = "<p>";
176
177 # XML, docbook format
178 my %highlights_xml = ( "([^=])\\\"([^\\\"<]+)\\\"", "\$1<quote>\$2</quote>",
179                         $type_constant, "<constant>\$1</constant>",
180                         $type_func, "<function>\$1</function>",
181                         $type_struct, "<structname>\$1</structname>",
182                         $type_env, "<envar>\$1</envar>",
183                         $type_param, "<parameter>\$1</parameter>" );
184 my $blankline_xml = "</para><para>\n";
185
186 # gnome, docbook format
187 my %highlights_gnome = ( $type_constant, "<replaceable class=\"option\">\$1</replaceable>",
188                          $type_func, "<function>\$1</function>",
189                          $type_struct, "<structname>\$1</structname>",
190                          $type_env, "<envar>\$1</envar>",
191                          $type_param, "<parameter>\$1</parameter>" );
192 my $blankline_gnome = "</para><para>\n";
193
194 # these are pretty rough
195 my %highlights_man = ( $type_constant, "\$1",
196                        $type_func, "\\\\fB\$1\\\\fP",
197                        $type_struct, "\\\\fI\$1\\\\fP",
198                        $type_param, "\\\\fI\$1\\\\fP" );
199 my $blankline_man = "";
200
201 # text-mode
202 my %highlights_text = ( $type_constant, "\$1",
203                         $type_func, "\$1",
204                         $type_struct, "\$1",
205                         $type_param, "\$1" );
206 my $blankline_text = "";
207
208
209 sub usage {
210     print "Usage: $0 [ -v ] [ -docbook | -html | -text | -man ]\n";
211     print "         [ -function funcname [ -function funcname ...] ]\n";
212     print "         [ -nofunction funcname [ -nofunction funcname ...] ]\n";
213     print "         c source file(s) > outputfile\n";
214     exit 1;
215 }
216
217 # read arguments
218 if ($#ARGV==-1) {
219     usage();
220 }
221
222 my $verbose = 0;
223 my $output_mode = "man";
224 my %highlights = %highlights_man;
225 my $blankline = $blankline_man;
226 my $modulename = "Kernel API";
227 my $function_only = 0;
228 my $man_date = ('January', 'February', 'March', 'April', 'May', 'June',
229                 'July', 'August', 'September', 'October',
230                 'November', 'December')[(localtime)[4]] .
231   " " . ((localtime)[5]+1900);
232
233 # Essentially these are globals
234 # They probably want to be tidied up made more localised or summat.
235 # CAVEAT EMPTOR!  Some of the others I localised may not want to be which
236 # could cause "use of undefined value" or other bugs.
237 my ($function, %function_table,%parametertypes,$declaration_purpose);
238 my ($type,$declaration_name,$return_type);
239 my ($newsection,$newcontents,$prototype,$filelist, $brcount, %source_map);
240
241 # Generated docbook code is inserted in a template at a point where
242 # docbook v3.1 requires a non-zero sequence of RefEntry's; see:
243 # http://www.oasis-open.org/docbook/documentation/reference/html/refentry.html
244 # We keep track of number of generated entries and generate a dummy
245 # if needs be to ensure the expanded template can be postprocessed
246 # into html.
247 my $section_counter = 0;
248
249 my $lineprefix="";
250
251 # states
252 # 0 - normal code
253 # 1 - looking for function name
254 # 2 - scanning field start.
255 # 3 - scanning prototype.
256 # 4 - documentation block
257 my $state;
258 my $in_doc_sect;
259
260 #declaration types: can be
261 # 'function', 'struct', 'union', 'enum', 'typedef'
262 my $decl_type;
263
264 my $doc_special = "\@\%\$\&";
265
266 my $doc_start = '^/\*\*\s*$'; # Allow whitespace at end of comment start.
267 my $doc_end = '\*/';
268 my $doc_com = '\s*\*\s*';
269 my $doc_decl = $doc_com.'(\w+)';
270 my $doc_sect = $doc_com.'(['.$doc_special.']?[\w\s]+):(.*)';
271 my $doc_content = $doc_com.'(.*)';
272 my $doc_block = $doc_com.'DOC:\s*(.*)?';
273
274 my %constants;
275 my %parameterdescs;
276 my @parameterlist;
277 my %sections;
278 my @sectionlist;
279
280 my $contents = "";
281 my $section_default = "Description";    # default section
282 my $section_intro = "Introduction";
283 my $section = $section_default;
284 my $section_context = "Context";
285
286 my $undescribed = "-- undescribed --";
287
288 reset_state();
289
290 while ($ARGV[0] =~ m/^-(.*)/) {
291     my $cmd = shift @ARGV;
292     if ($cmd eq "-html") {
293         $output_mode = "html";
294         %highlights = %highlights_html;
295         $blankline = $blankline_html;
296     } elsif ($cmd eq "-man") {
297         $output_mode = "man";
298         %highlights = %highlights_man;
299         $blankline = $blankline_man;
300     } elsif ($cmd eq "-text") {
301         $output_mode = "text";
302         %highlights = %highlights_text;
303         $blankline = $blankline_text;
304     } elsif ($cmd eq "-docbook") {
305         $output_mode = "xml";
306         %highlights = %highlights_xml;
307         $blankline = $blankline_xml;
308     } elsif ($cmd eq "-gnome") {
309         $output_mode = "gnome";
310         %highlights = %highlights_gnome;
311         $blankline = $blankline_gnome;
312     } elsif ($cmd eq "-module") { # not needed for XML, inherits from calling document
313         $modulename = shift @ARGV;
314     } elsif ($cmd eq "-function") { # to only output specific functions
315         $function_only = 1;
316         $function = shift @ARGV;
317         $function_table{$function} = 1;
318     } elsif ($cmd eq "-nofunction") { # to only output specific functions
319         $function_only = 2;
320         $function = shift @ARGV;
321         $function_table{$function} = 1;
322     } elsif ($cmd eq "-v") {
323         $verbose = 1;
324     } elsif (($cmd eq "-h") || ($cmd eq "--help")) {
325         usage();
326     } elsif ($cmd eq '-filelist') {
327             $filelist = shift @ARGV;
328     }
329 }
330
331
332 # generate a sequence of code that will splice in highlighting information
333 # using the s// operator.
334 my $dohighlight = "";
335 foreach my $pattern (keys %highlights) {
336 #   print STDERR "scanning pattern:$pattern, highlight:($highlights{$pattern})\n";
337     $dohighlight .=  "\$contents =~ s:$pattern:$highlights{$pattern}:gs;\n";
338 }
339
340 ##
341 # dumps section contents to arrays/hashes intended for that purpose.
342 #
343 sub dump_section {
344     my $name = shift;
345     my $contents = join "\n", @_;
346
347     if ($name =~ m/$type_constant/) {
348         $name = $1;
349 #       print STDERR "constant section '$1' = '$contents'\n";
350         $constants{$name} = $contents;
351     } elsif ($name =~ m/$type_param/) {
352 #       print STDERR "parameter def '$1' = '$contents'\n";
353         $name = $1;
354         $parameterdescs{$name} = $contents;
355     } else {
356 #       print STDERR "other section '$name' = '$contents'\n";
357         $sections{$name} = $contents;
358         push @sectionlist, $name;
359     }
360 }
361
362 ##
363 # output function
364 #
365 # parameterdescs, a hash.
366 #  function => "function name"
367 #  parameterlist => @list of parameters
368 #  parameterdescs => %parameter descriptions
369 #  sectionlist => @list of sections
370 #  sections => %section descriptions
371 #
372
373 sub output_highlight {
374     my $contents = join "\n",@_;
375     my $line;
376
377 #   DEBUG
378 #   if (!defined $contents) {
379 #       use Carp;
380 #       confess "output_highlight got called with no args?\n";
381 #   }
382
383 #   print STDERR "contents b4:$contents\n";
384     eval $dohighlight;
385     die $@ if $@;
386     if ($output_mode eq "html") {
387         $contents =~ s/\\\\//;
388     }
389 #   print STDERR "contents af:$contents\n";
390
391     foreach $line (split "\n", $contents) {
392         if ($line eq ""){
393             print $lineprefix, $blankline;
394         } else {
395             $line =~ s/\\\\\\/\&/g;
396             print $lineprefix, $line;
397         }
398         print "\n";
399     }
400 }
401
402 #output sections in html
403 sub output_section_html(%) {
404     my %args = %{$_[0]};
405     my $section;
406
407     foreach $section (@{$args{'sectionlist'}}) {
408         print "<h3>$section</h3>\n";
409         print "<blockquote>\n";
410         output_highlight($args{'sections'}{$section});
411         print "</blockquote>\n";
412     }
413 }
414
415 # output enum in html
416 sub output_enum_html(%) {
417     my %args = %{$_[0]};
418     my ($parameter);
419     my $count;
420     print "<h2>enum ".$args{'enum'}."</h2>\n";
421
422     print "<b>enum ".$args{'enum'}."</b> {<br>\n";
423     $count = 0;
424     foreach $parameter (@{$args{'parameterlist'}}) {
425         print " <b>".$parameter."</b>";
426         if ($count != $#{$args{'parameterlist'}}) {
427             $count++;
428             print ",\n";
429         }
430         print "<br>";
431     }
432     print "};<br>\n";
433
434     print "<h3>Constants</h3>\n";
435     print "<dl>\n";
436     foreach $parameter (@{$args{'parameterlist'}}) {
437         print "<dt><b>".$parameter."</b>\n";
438         print "<dd>";
439         output_highlight($args{'parameterdescs'}{$parameter});
440     }
441     print "</dl>\n";
442     output_section_html(@_);
443     print "<hr>\n";
444 }
445
446 # output typedef in html
447 sub output_typedef_html(%) {
448     my %args = %{$_[0]};
449     my ($parameter);
450     my $count;
451     print "<h2>typedef ".$args{'typedef'}."</h2>\n";
452
453     print "<b>typedef ".$args{'typedef'}."</b>\n";
454     output_section_html(@_);
455     print "<hr>\n";
456 }
457
458 # output struct in html
459 sub output_struct_html(%) {
460     my %args = %{$_[0]};
461     my ($parameter);
462
463     print "<h2>".$args{'type'}." ".$args{'struct'}. " - " .$args{'purpose'}."</h2>\n";
464     print "<b>".$args{'type'}." ".$args{'struct'}."</b> {<br>\n";
465     foreach $parameter (@{$args{'parameterlist'}}) {
466         if ($parameter =~ /^#/) {
467                 print "$parameter<br>\n";
468                 next;
469         }
470         my $parameter_name = $parameter;
471         $parameter_name =~ s/\[.*//;
472
473         ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
474         $type = $args{'parametertypes'}{$parameter};
475         if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
476             # pointer-to-function
477             print "&nbsp; &nbsp; <i>$1</i><b>$parameter</b>) <i>($2)</i>;<br>\n";
478         } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
479             # bitfield
480             print "&nbsp; &nbsp; <i>$1</i> <b>$parameter</b>$2;<br>\n";
481         } else {
482             print "&nbsp; &nbsp; <i>$type</i> <b>$parameter</b>;<br>\n";
483         }
484     }
485     print "};<br>\n";
486
487     print "<h3>Members</h3>\n";
488     print "<dl>\n";
489     foreach $parameter (@{$args{'parameterlist'}}) {
490         ($parameter =~ /^#/) && next;
491
492         my $parameter_name = $parameter;
493         $parameter_name =~ s/\[.*//;
494
495         ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
496         print "<dt><b>".$parameter."</b>\n";
497         print "<dd>";
498         output_highlight($args{'parameterdescs'}{$parameter_name});
499     }
500     print "</dl>\n";
501     output_section_html(@_);
502     print "<hr>\n";
503 }
504
505 # output function in html
506 sub output_function_html(%) {
507     my %args = %{$_[0]};
508     my ($parameter, $section);
509     my $count;
510
511     print "<h2>" .$args{'function'}." - ".$args{'purpose'}."</h2>\n";
512     print "<i>".$args{'functiontype'}."</i>\n";
513     print "<b>".$args{'function'}."</b>\n";
514     print "(";
515     $count = 0;
516     foreach $parameter (@{$args{'parameterlist'}}) {
517         $type = $args{'parametertypes'}{$parameter};
518         if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
519             # pointer-to-function
520             print "<i>$1</i><b>$parameter</b>) <i>($2)</i>";
521         } else {
522             print "<i>".$type."</i> <b>".$parameter."</b>";
523         }
524         if ($count != $#{$args{'parameterlist'}}) {
525             $count++;
526             print ",\n";
527         }
528     }
529     print ")\n";
530
531     print "<h3>Arguments</h3>\n";
532     print "<dl>\n";
533     foreach $parameter (@{$args{'parameterlist'}}) {
534         my $parameter_name = $parameter;
535         $parameter_name =~ s/\[.*//;
536
537         ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
538         print "<dt><b>".$parameter."</b>\n";
539         print "<dd>";
540         output_highlight($args{'parameterdescs'}{$parameter_name});
541     }
542     print "</dl>\n";
543     output_section_html(@_);
544     print "<hr>\n";
545 }
546
547 # output intro in html
548 sub output_intro_html(%) {
549     my %args = %{$_[0]};
550     my ($parameter, $section);
551     my $count;
552
553     foreach $section (@{$args{'sectionlist'}}) {
554         print "<h3>$section</h3>\n";
555         print "<ul>\n";
556         output_highlight($args{'sections'}{$section});
557         print "</ul>\n";
558     }
559     print "<hr>\n";
560 }
561
562 sub output_section_xml(%) {
563     my %args = %{$_[0]};
564     my $section;
565     # print out each section
566     $lineprefix="   ";
567     foreach $section (@{$args{'sectionlist'}}) {
568         print "<refsect1>\n";
569         print "<title>$section</title>\n";
570         if ($section =~ m/EXAMPLE/i) {
571             print "<informalexample><programlisting>\n";
572         } else {
573             print "<para>\n";
574         }
575         output_highlight($args{'sections'}{$section});
576         if ($section =~ m/EXAMPLE/i) {
577             print "</programlisting></informalexample>\n";
578         } else {
579             print "</para>\n";
580         }
581         print "</refsect1>\n";
582     }
583 }
584
585 # output function in XML DocBook
586 sub output_function_xml(%) {
587     my %args = %{$_[0]};
588     my ($parameter, $section);
589     my $count;
590     my $id;
591
592     $id = "API-".$args{'function'};
593     $id =~ s/[^A-Za-z0-9]/-/g;
594
595     print "<refentry id=\"$id\">\n";
596     print "<refentryinfo>\n";
597     print " <title>LINUX</title>\n";
598     print " <productname>Kernel Hackers Manual</productname>\n";
599     print " <date>$man_date</date>\n";
600     print "</refentryinfo>\n";
601     print "<refmeta>\n";
602     print " <refentrytitle><phrase>".$args{'function'}."</phrase></refentrytitle>\n";
603     print " <manvolnum>9</manvolnum>\n";
604     print "</refmeta>\n";
605     print "<refnamediv>\n";
606     print " <refname>".$args{'function'}."</refname>\n";
607     print " <refpurpose>\n";
608     print "  ";
609     output_highlight ($args{'purpose'});
610     print " </refpurpose>\n";
611     print "</refnamediv>\n";
612
613     print "<refsynopsisdiv>\n";
614     print " <title>Synopsis</title>\n";
615     print "  <funcsynopsis><funcprototype>\n";
616     print "   <funcdef>".$args{'functiontype'}." ";
617     print "<function>".$args{'function'}." </function></funcdef>\n";
618
619     $count = 0;
620     if ($#{$args{'parameterlist'}} >= 0) {
621         foreach $parameter (@{$args{'parameterlist'}}) {
622             $type = $args{'parametertypes'}{$parameter};
623             if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
624                 # pointer-to-function
625                 print "   <paramdef>$1<parameter>$parameter</parameter>)\n";
626                 print "     <funcparams>$2</funcparams></paramdef>\n";
627             } else {
628                 print "   <paramdef>".$type;
629                 print " <parameter>$parameter</parameter></paramdef>\n";
630             }
631         }
632     } else {
633         print "  <void/>\n";
634     }
635     print "  </funcprototype></funcsynopsis>\n";
636     print "</refsynopsisdiv>\n";
637
638     # print parameters
639     print "<refsect1>\n <title>Arguments</title>\n";
640     if ($#{$args{'parameterlist'}} >= 0) {
641         print " <variablelist>\n";
642         foreach $parameter (@{$args{'parameterlist'}}) {
643             my $parameter_name = $parameter;
644             $parameter_name =~ s/\[.*//;
645
646             print "  <varlistentry>\n   <term><parameter>$parameter</parameter></term>\n";
647             print "   <listitem>\n    <para>\n";
648             $lineprefix="     ";
649             output_highlight($args{'parameterdescs'}{$parameter_name});
650             print "    </para>\n   </listitem>\n  </varlistentry>\n";
651         }
652         print " </variablelist>\n";
653     } else {
654         print " <para>\n  None\n </para>\n";
655     }
656     print "</refsect1>\n";
657
658     output_section_xml(@_);
659     print "</refentry>\n\n";
660 }
661
662 # output struct in XML DocBook
663 sub output_struct_xml(%) {
664     my %args = %{$_[0]};
665     my ($parameter, $section);
666     my $id;
667
668     $id = "API-struct-".$args{'struct'};
669     $id =~ s/[^A-Za-z0-9]/-/g;
670
671     print "<refentry id=\"$id\">\n";
672     print "<refentryinfo>\n";
673     print " <title>LINUX</title>\n";
674     print " <productname>Kernel Hackers Manual</productname>\n";
675     print " <date>$man_date</date>\n";
676     print "</refentryinfo>\n";
677     print "<refmeta>\n";
678     print " <refentrytitle><phrase>".$args{'type'}." ".$args{'struct'}."</phrase></refentrytitle>\n";
679     print " <manvolnum>9</manvolnum>\n";
680     print "</refmeta>\n";
681     print "<refnamediv>\n";
682     print " <refname>".$args{'type'}." ".$args{'struct'}."</refname>\n";
683     print " <refpurpose>\n";
684     print "  ";
685     output_highlight ($args{'purpose'});
686     print " </refpurpose>\n";
687     print "</refnamediv>\n";
688
689     print "<refsynopsisdiv>\n";
690     print " <title>Synopsis</title>\n";
691     print "  <programlisting>\n";
692     print $args{'type'}." ".$args{'struct'}." {\n";
693     foreach $parameter (@{$args{'parameterlist'}}) {
694         if ($parameter =~ /^#/) {
695             print "$parameter\n";
696             next;
697         }
698
699         my $parameter_name = $parameter;
700         $parameter_name =~ s/\[.*//;
701
702         defined($args{'parameterdescs'}{$parameter_name}) || next;
703         ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
704         $type = $args{'parametertypes'}{$parameter};
705         if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
706             # pointer-to-function
707             print "  $1 $parameter) ($2);\n";
708         } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
709             print "  $1 $parameter$2;\n";
710         } else {
711             print "  ".$type." ".$parameter.";\n";
712         }
713     }
714     print "};";
715     print "  </programlisting>\n";
716     print "</refsynopsisdiv>\n";
717
718     print " <refsect1>\n";
719     print "  <title>Members</title>\n";
720
721     print "  <variablelist>\n";
722     foreach $parameter (@{$args{'parameterlist'}}) {
723       ($parameter =~ /^#/) && next;
724
725       my $parameter_name = $parameter;
726       $parameter_name =~ s/\[.*//;
727
728       defined($args{'parameterdescs'}{$parameter_name}) || next;
729       ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
730       print "    <varlistentry>";
731       print "      <term>$parameter</term>\n";
732       print "      <listitem><para>\n";
733       output_highlight($args{'parameterdescs'}{$parameter_name});
734       print "      </para></listitem>\n";
735       print "    </varlistentry>\n";
736     }
737     print "  </variablelist>\n";
738     print " </refsect1>\n";
739
740     output_section_xml(@_);
741
742     print "</refentry>\n\n";
743 }
744
745 # output enum in XML DocBook
746 sub output_enum_xml(%) {
747     my %args = %{$_[0]};
748     my ($parameter, $section);
749     my $count;
750     my $id;
751
752     $id = "API-enum-".$args{'enum'};
753     $id =~ s/[^A-Za-z0-9]/-/g;
754
755     print "<refentry id=\"$id\">\n";
756     print "<refentryinfo>\n";
757     print " <title>LINUX</title>\n";
758     print " <productname>Kernel Hackers Manual</productname>\n";
759     print " <date>$man_date</date>\n";
760     print "</refentryinfo>\n";
761     print "<refmeta>\n";
762     print " <refentrytitle><phrase>enum ".$args{'enum'}."</phrase></refentrytitle>\n";
763     print " <manvolnum>9</manvolnum>\n";
764     print "</refmeta>\n";
765     print "<refnamediv>\n";
766     print " <refname>enum ".$args{'enum'}."</refname>\n";
767     print " <refpurpose>\n";
768     print "  ";
769     output_highlight ($args{'purpose'});
770     print " </refpurpose>\n";
771     print "</refnamediv>\n";
772
773     print "<refsynopsisdiv>\n";
774     print " <title>Synopsis</title>\n";
775     print "  <programlisting>\n";
776     print "enum ".$args{'enum'}." {\n";
777     $count = 0;
778     foreach $parameter (@{$args{'parameterlist'}}) {
779         print "  $parameter";
780         if ($count != $#{$args{'parameterlist'}}) {
781             $count++;
782             print ",";
783         }
784         print "\n";
785     }
786     print "};";
787     print "  </programlisting>\n";
788     print "</refsynopsisdiv>\n";
789
790     print "<refsect1>\n";
791     print " <title>Constants</title>\n";
792     print "  <variablelist>\n";
793     foreach $parameter (@{$args{'parameterlist'}}) {
794       my $parameter_name = $parameter;
795       $parameter_name =~ s/\[.*//;
796
797       print "    <varlistentry>";
798       print "      <term>$parameter</term>\n";
799       print "      <listitem><para>\n";
800       output_highlight($args{'parameterdescs'}{$parameter_name});
801       print "      </para></listitem>\n";
802       print "    </varlistentry>\n";
803     }
804     print "  </variablelist>\n";
805     print "</refsect1>\n";
806
807     output_section_xml(@_);
808
809     print "</refentry>\n\n";
810 }
811
812 # output typedef in XML DocBook
813 sub output_typedef_xml(%) {
814     my %args = %{$_[0]};
815     my ($parameter, $section);
816     my $id;
817
818     $id = "API-typedef-".$args{'typedef'};
819     $id =~ s/[^A-Za-z0-9]/-/g;
820
821     print "<refentry id=\"$id\">\n";
822     print "<refentryinfo>\n";
823     print " <title>LINUX</title>\n";
824     print " <productname>Kernel Hackers Manual</productname>\n";
825     print " <date>$man_date</date>\n";
826     print "</refentryinfo>\n";
827     print "<refmeta>\n";
828     print " <refentrytitle><phrase>typedef ".$args{'typedef'}."</phrase></refentrytitle>\n";
829     print " <manvolnum>9</manvolnum>\n";
830     print "</refmeta>\n";
831     print "<refnamediv>\n";
832     print " <refname>typedef ".$args{'typedef'}."</refname>\n";
833     print " <refpurpose>\n";
834     print "  ";
835     output_highlight ($args{'purpose'});
836     print " </refpurpose>\n";
837     print "</refnamediv>\n";
838
839     print "<refsynopsisdiv>\n";
840     print " <title>Synopsis</title>\n";
841     print "  <synopsis>typedef ".$args{'typedef'}.";</synopsis>\n";
842     print "</refsynopsisdiv>\n";
843
844     output_section_xml(@_);
845
846     print "</refentry>\n\n";
847 }
848
849 # output in XML DocBook
850 sub output_intro_xml(%) {
851     my %args = %{$_[0]};
852     my ($parameter, $section);
853     my $count;
854
855     my $id = $args{'module'};
856     $id =~ s/[^A-Za-z0-9]/-/g;
857
858     # print out each section
859     $lineprefix="   ";
860     foreach $section (@{$args{'sectionlist'}}) {
861         print "<refsect1>\n <title>$section</title>\n <para>\n";
862         if ($section =~ m/EXAMPLE/i) {
863             print "<example><para>\n";
864         }
865         output_highlight($args{'sections'}{$section});
866         if ($section =~ m/EXAMPLE/i) {
867             print "</para></example>\n";
868         }
869         print " </para>\n</refsect1>\n";
870     }
871
872     print "\n\n";
873 }
874
875 # output in XML DocBook
876 sub output_function_gnome {
877     my %args = %{$_[0]};
878     my ($parameter, $section);
879     my $count;
880     my $id;
881
882     $id = $args{'module'}."-".$args{'function'};
883     $id =~ s/[^A-Za-z0-9]/-/g;
884
885     print "<sect2>\n";
886     print " <title id=\"$id\">".$args{'function'}."</title>\n";
887
888     print "  <funcsynopsis>\n";
889     print "   <funcdef>".$args{'functiontype'}." ";
890     print "<function>".$args{'function'}." ";
891     print "</function></funcdef>\n";
892
893     $count = 0;
894     if ($#{$args{'parameterlist'}} >= 0) {
895         foreach $parameter (@{$args{'parameterlist'}}) {
896             $type = $args{'parametertypes'}{$parameter};
897             if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
898                 # pointer-to-function
899                 print "   <paramdef>$1 <parameter>$parameter</parameter>)\n";
900                 print "     <funcparams>$2</funcparams></paramdef>\n";
901             } else {
902                 print "   <paramdef>".$type;
903                 print " <parameter>$parameter</parameter></paramdef>\n";
904             }
905         }
906     } else {
907         print "  <void>\n";
908     }
909     print "  </funcsynopsis>\n";
910     if ($#{$args{'parameterlist'}} >= 0) {
911         print " <informaltable pgwide=\"1\" frame=\"none\" role=\"params\">\n";
912         print "<tgroup cols=\"2\">\n";
913         print "<colspec colwidth=\"2*\">\n";
914         print "<colspec colwidth=\"8*\">\n";
915         print "<tbody>\n";
916         foreach $parameter (@{$args{'parameterlist'}}) {
917             my $parameter_name = $parameter;
918             $parameter_name =~ s/\[.*//;
919
920             print "  <row><entry align=\"right\"><parameter>$parameter</parameter></entry>\n";
921             print "   <entry>\n";
922             $lineprefix="     ";
923             output_highlight($args{'parameterdescs'}{$parameter_name});
924             print "    </entry></row>\n";
925         }
926         print " </tbody></tgroup></informaltable>\n";
927     } else {
928         print " <para>\n  None\n </para>\n";
929     }
930
931     # print out each section
932     $lineprefix="   ";
933     foreach $section (@{$args{'sectionlist'}}) {
934         print "<simplesect>\n <title>$section</title>\n";
935         if ($section =~ m/EXAMPLE/i) {
936             print "<example><programlisting>\n";
937         } else {
938         }
939         print "<para>\n";
940         output_highlight($args{'sections'}{$section});
941         print "</para>\n";
942         if ($section =~ m/EXAMPLE/i) {
943             print "</programlisting></example>\n";
944         } else {
945         }
946         print " </simplesect>\n";
947     }
948
949     print "</sect2>\n\n";
950 }
951
952 ##
953 # output function in man
954 sub output_function_man(%) {
955     my %args = %{$_[0]};
956     my ($parameter, $section);
957     my $count;
958
959     print ".TH \"$args{'function'}\" 9 \"$args{'function'}\" \"$man_date\" \"Kernel Hacker's Manual\" LINUX\n";
960
961     print ".SH NAME\n";
962     print $args{'function'}." \\- ".$args{'purpose'}."\n";
963
964     print ".SH SYNOPSIS\n";
965     if ($args{'functiontype'} ne "") {
966         print ".B \"".$args{'functiontype'}."\" ".$args{'function'}."\n";
967     } else {
968         print ".B \"".$args{'function'}."\n";
969     }
970     $count = 0;
971     my $parenth = "(";
972     my $post = ",";
973     foreach my $parameter (@{$args{'parameterlist'}}) {
974         if ($count == $#{$args{'parameterlist'}}) {
975             $post = ");";
976         }
977         $type = $args{'parametertypes'}{$parameter};
978         if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
979             # pointer-to-function
980             print ".BI \"".$parenth.$1."\" ".$parameter." \") (".$2.")".$post."\"\n";
981         } else {
982             $type =~ s/([^\*])$/$1 /;
983             print ".BI \"".$parenth.$type."\" ".$parameter." \"".$post."\"\n";
984         }
985         $count++;
986         $parenth = "";
987     }
988
989     print ".SH ARGUMENTS\n";
990     foreach $parameter (@{$args{'parameterlist'}}) {
991         my $parameter_name = $parameter;
992         $parameter_name =~ s/\[.*//;
993
994         print ".IP \"".$parameter."\" 12\n";
995         output_highlight($args{'parameterdescs'}{$parameter_name});
996     }
997     foreach $section (@{$args{'sectionlist'}}) {
998         print ".SH \"", uc $section, "\"\n";
999         output_highlight($args{'sections'}{$section});
1000     }
1001 }
1002
1003 ##
1004 # output enum in man
1005 sub output_enum_man(%) {
1006     my %args = %{$_[0]};
1007     my ($parameter, $section);
1008     my $count;
1009
1010     print ".TH \"$args{'module'}\" 9 \"enum $args{'enum'}\" \"$man_date\" \"API Manual\" LINUX\n";
1011
1012     print ".SH NAME\n";
1013     print "enum ".$args{'enum'}." \\- ".$args{'purpose'}."\n";
1014
1015     print ".SH SYNOPSIS\n";
1016     print "enum ".$args{'enum'}." {\n";
1017     $count = 0;
1018     foreach my $parameter (@{$args{'parameterlist'}}) {
1019         print ".br\n.BI \"    $parameter\"\n";
1020         if ($count == $#{$args{'parameterlist'}}) {
1021             print "\n};\n";
1022             last;
1023         }
1024         else {
1025             print ", \n.br\n";
1026         }
1027         $count++;
1028     }
1029
1030     print ".SH Constants\n";
1031     foreach $parameter (@{$args{'parameterlist'}}) {
1032         my $parameter_name = $parameter;
1033         $parameter_name =~ s/\[.*//;
1034
1035         print ".IP \"".$parameter."\" 12\n";
1036         output_highlight($args{'parameterdescs'}{$parameter_name});
1037     }
1038     foreach $section (@{$args{'sectionlist'}}) {
1039         print ".SH \"$section\"\n";
1040         output_highlight($args{'sections'}{$section});
1041     }
1042 }
1043
1044 ##
1045 # output struct in man
1046 sub output_struct_man(%) {
1047     my %args = %{$_[0]};
1048     my ($parameter, $section);
1049
1050     print ".TH \"$args{'module'}\" 9 \"".$args{'type'}." ".$args{'struct'}."\" \"$man_date\" \"API Manual\" LINUX\n";
1051
1052     print ".SH NAME\n";
1053     print $args{'type'}." ".$args{'struct'}." \\- ".$args{'purpose'}."\n";
1054
1055     print ".SH SYNOPSIS\n";
1056     print $args{'type'}." ".$args{'struct'}." {\n.br\n";
1057
1058     foreach my $parameter (@{$args{'parameterlist'}}) {
1059         if ($parameter =~ /^#/) {
1060             print ".BI \"$parameter\"\n.br\n";
1061             next;
1062         }
1063         my $parameter_name = $parameter;
1064         $parameter_name =~ s/\[.*//;
1065
1066         ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
1067         $type = $args{'parametertypes'}{$parameter};
1068         if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1069             # pointer-to-function
1070             print ".BI \"    ".$1."\" ".$parameter." \") (".$2.")"."\"\n;\n";
1071         } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
1072             # bitfield
1073             print ".BI \"    ".$1."\ \" ".$parameter.$2." \""."\"\n;\n";
1074         } else {
1075             $type =~ s/([^\*])$/$1 /;
1076             print ".BI \"    ".$type."\" ".$parameter." \""."\"\n;\n";
1077         }
1078         print "\n.br\n";
1079     }
1080     print "};\n.br\n";
1081
1082     print ".SH Members\n";
1083     foreach $parameter (@{$args{'parameterlist'}}) {
1084         ($parameter =~ /^#/) && next;
1085
1086         my $parameter_name = $parameter;
1087         $parameter_name =~ s/\[.*//;
1088
1089         ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
1090         print ".IP \"".$parameter."\" 12\n";
1091         output_highlight($args{'parameterdescs'}{$parameter_name});
1092     }
1093     foreach $section (@{$args{'sectionlist'}}) {
1094         print ".SH \"$section\"\n";
1095         output_highlight($args{'sections'}{$section});
1096     }
1097 }
1098
1099 ##
1100 # output typedef in man
1101 sub output_typedef_man(%) {
1102     my %args = %{$_[0]};
1103     my ($parameter, $section);
1104
1105     print ".TH \"$args{'module'}\" 9 \"$args{'typedef'}\" \"$man_date\" \"API Manual\" LINUX\n";
1106
1107     print ".SH NAME\n";
1108     print "typedef ".$args{'typedef'}." \\- ".$args{'purpose'}."\n";
1109
1110     foreach $section (@{$args{'sectionlist'}}) {
1111         print ".SH \"$section\"\n";
1112         output_highlight($args{'sections'}{$section});
1113     }
1114 }
1115
1116 sub output_intro_man(%) {
1117     my %args = %{$_[0]};
1118     my ($parameter, $section);
1119     my $count;
1120
1121     print ".TH \"$args{'module'}\" 9 \"$args{'module'}\" \"$man_date\" \"API Manual\" LINUX\n";
1122
1123     foreach $section (@{$args{'sectionlist'}}) {
1124         print ".SH \"$section\"\n";
1125         output_highlight($args{'sections'}{$section});
1126     }
1127 }
1128
1129 ##
1130 # output in text
1131 sub output_function_text(%) {
1132     my %args = %{$_[0]};
1133     my ($parameter, $section);
1134     my $start;
1135
1136     print "Name:\n\n";
1137     print $args{'function'}." - ".$args{'purpose'}."\n";
1138
1139     print "\nSynopsis:\n\n";
1140     if ($args{'functiontype'} ne "") {
1141         $start = $args{'functiontype'}." ".$args{'function'}." (";
1142     } else {
1143         $start = $args{'function'}." (";
1144     }
1145     print $start;
1146
1147     my $count = 0;
1148     foreach my $parameter (@{$args{'parameterlist'}}) {
1149         $type = $args{'parametertypes'}{$parameter};
1150         if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1151             # pointer-to-function
1152             print $1.$parameter.") (".$2;
1153         } else {
1154             print $type." ".$parameter;
1155         }
1156         if ($count != $#{$args{'parameterlist'}}) {
1157             $count++;
1158             print ",\n";
1159             print " " x length($start);
1160         } else {
1161             print ");\n\n";
1162         }
1163     }
1164
1165     print "Arguments:\n\n";
1166     foreach $parameter (@{$args{'parameterlist'}}) {
1167         my $parameter_name = $parameter;
1168         $parameter_name =~ s/\[.*//;
1169
1170         print $parameter."\n\t".$args{'parameterdescs'}{$parameter_name}."\n";
1171     }
1172     output_section_text(@_);
1173 }
1174
1175 #output sections in text
1176 sub output_section_text(%) {
1177     my %args = %{$_[0]};
1178     my $section;
1179
1180     print "\n";
1181     foreach $section (@{$args{'sectionlist'}}) {
1182         print "$section:\n\n";
1183         output_highlight($args{'sections'}{$section});
1184     }
1185     print "\n\n";
1186 }
1187
1188 # output enum in text
1189 sub output_enum_text(%) {
1190     my %args = %{$_[0]};
1191     my ($parameter);
1192     my $count;
1193     print "Enum:\n\n";
1194
1195     print "enum ".$args{'enum'}." - ".$args{'purpose'}."\n\n";
1196     print "enum ".$args{'enum'}." {\n";
1197     $count = 0;
1198     foreach $parameter (@{$args{'parameterlist'}}) {
1199         print "\t$parameter";
1200         if ($count != $#{$args{'parameterlist'}}) {
1201             $count++;
1202             print ",";
1203         }
1204         print "\n";
1205     }
1206     print "};\n\n";
1207
1208     print "Constants:\n\n";
1209     foreach $parameter (@{$args{'parameterlist'}}) {
1210         print "$parameter\n\t";
1211         print $args{'parameterdescs'}{$parameter}."\n";
1212     }
1213
1214     output_section_text(@_);
1215 }
1216
1217 # output typedef in text
1218 sub output_typedef_text(%) {
1219     my %args = %{$_[0]};
1220     my ($parameter);
1221     my $count;
1222     print "Typedef:\n\n";
1223
1224     print "typedef ".$args{'typedef'}." - ".$args{'purpose'}."\n";
1225     output_section_text(@_);
1226 }
1227
1228 # output struct as text
1229 sub output_struct_text(%) {
1230     my %args = %{$_[0]};
1231     my ($parameter);
1232
1233     print $args{'type'}." ".$args{'struct'}." - ".$args{'purpose'}."\n\n";
1234     print $args{'type'}." ".$args{'struct'}." {\n";
1235     foreach $parameter (@{$args{'parameterlist'}}) {
1236         if ($parameter =~ /^#/) {
1237             print "$parameter\n";
1238             next;
1239         }
1240
1241         my $parameter_name = $parameter;
1242         $parameter_name =~ s/\[.*//;
1243
1244         ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
1245         $type = $args{'parametertypes'}{$parameter};
1246         if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1247             # pointer-to-function
1248             print "\t$1 $parameter) ($2);\n";
1249         } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
1250             print "\t$1 $parameter$2;\n";
1251         } else {
1252             print "\t".$type." ".$parameter.";\n";
1253         }
1254     }
1255     print "};\n\n";
1256
1257     print "Members:\n\n";
1258     foreach $parameter (@{$args{'parameterlist'}}) {
1259         ($parameter =~ /^#/) && next;
1260
1261         my $parameter_name = $parameter;
1262         $parameter_name =~ s/\[.*//;
1263
1264         ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
1265         print "$parameter\n\t";
1266         print $args{'parameterdescs'}{$parameter_name}."\n";
1267     }
1268     print "\n";
1269     output_section_text(@_);
1270 }
1271
1272 sub output_intro_text(%) {
1273     my %args = %{$_[0]};
1274     my ($parameter, $section);
1275
1276     foreach $section (@{$args{'sectionlist'}}) {
1277         print " $section:\n";
1278         print "    -> ";
1279         output_highlight($args{'sections'}{$section});
1280     }
1281 }
1282
1283 ##
1284 # generic output function for all types (function, struct/union, typedef, enum);
1285 # calls the generated, variable output_ function name based on
1286 # functype and output_mode
1287 sub output_declaration {
1288     no strict 'refs';
1289     my $name = shift;
1290     my $functype = shift;
1291     my $func = "output_${functype}_$output_mode";
1292     if (($function_only==0) ||
1293         ( $function_only == 1 && defined($function_table{$name})) ||
1294         ( $function_only == 2 && !defined($function_table{$name})))
1295     {
1296         &$func(@_);
1297         $section_counter++;
1298     }
1299 }
1300
1301 ##
1302 # generic output function - calls the right one based on current output mode.
1303 sub output_intro {
1304     no strict 'refs';
1305     my $func = "output_intro_".$output_mode;
1306     &$func(@_);
1307     $section_counter++;
1308 }
1309
1310 ##
1311 # takes a declaration (struct, union, enum, typedef) and
1312 # invokes the right handler. NOT called for functions.
1313 sub dump_declaration($$) {
1314     no strict 'refs';
1315     my ($prototype, $file) = @_;
1316     my $func = "dump_".$decl_type;
1317     &$func(@_);
1318 }
1319
1320 sub dump_union($$) {
1321     dump_struct(@_);
1322 }
1323
1324 sub dump_struct($$) {
1325     my $x = shift;
1326     my $file = shift;
1327
1328     if ($x =~/(struct|union)\s+(\w+)\s*{(.*)}/) {
1329         $declaration_name = $2;
1330         my $members = $3;
1331
1332         # ignore embedded structs or unions
1333         $members =~ s/{.*?}//g;
1334
1335         # ignore members marked private:
1336         $members =~ s/\/\*.*?private:.*?public:.*?\*\///gos;
1337         $members =~ s/\/\*.*?private:.*//gos;
1338         # strip comments:
1339         $members =~ s/\/\*.*?\*\///gos;
1340
1341         create_parameterlist($members, ';', $file);
1342
1343         output_declaration($declaration_name,
1344                            'struct',
1345                            {'struct' => $declaration_name,
1346                             'module' => $modulename,
1347                             'parameterlist' => \@parameterlist,
1348                             'parameterdescs' => \%parameterdescs,
1349                             'parametertypes' => \%parametertypes,
1350                             'sectionlist' => \@sectionlist,
1351                             'sections' => \%sections,
1352                             'purpose' => $declaration_purpose,
1353                             'type' => $decl_type
1354                            });
1355     }
1356     else {
1357         print STDERR "Error(${file}:$.): Cannot parse struct or union!\n";
1358         ++$errors;
1359     }
1360 }
1361
1362 sub dump_enum($$) {
1363     my $x = shift;
1364     my $file = shift;
1365
1366     $x =~ s@/\*.*?\*/@@gos;     # strip comments.
1367     if ($x =~ /enum\s+(\w+)\s*{(.*)}/) {
1368         $declaration_name = $1;
1369         my $members = $2;
1370
1371         foreach my $arg (split ',', $members) {
1372             $arg =~ s/^\s*(\w+).*/$1/;
1373             push @parameterlist, $arg;
1374             if (!$parameterdescs{$arg}) {
1375                 $parameterdescs{$arg} = $undescribed;
1376                 print STDERR "Warning(${file}:$.): Enum value '$arg' ".
1377                     "not described in enum '$declaration_name'\n";
1378             }
1379
1380         }
1381
1382         output_declaration($declaration_name,
1383                            'enum',
1384                            {'enum' => $declaration_name,
1385                             'module' => $modulename,
1386                             'parameterlist' => \@parameterlist,
1387                             'parameterdescs' => \%parameterdescs,
1388                             'sectionlist' => \@sectionlist,
1389                             'sections' => \%sections,
1390                             'purpose' => $declaration_purpose
1391                            });
1392     }
1393     else {
1394         print STDERR "Error(${file}:$.): Cannot parse enum!\n";
1395         ++$errors;
1396     }
1397 }
1398
1399 sub dump_typedef($$) {
1400     my $x = shift;
1401     my $file = shift;
1402
1403     $x =~ s@/\*.*?\*/@@gos;     # strip comments.
1404     while (($x =~ /\(*.\)\s*;$/) || ($x =~ /\[*.\]\s*;$/)) {
1405         $x =~ s/\(*.\)\s*;$/;/;
1406         $x =~ s/\[*.\]\s*;$/;/;
1407     }
1408
1409     if ($x =~ /typedef.*\s+(\w+)\s*;/) {
1410         $declaration_name = $1;
1411
1412         output_declaration($declaration_name,
1413                            'typedef',
1414                            {'typedef' => $declaration_name,
1415                             'module' => $modulename,
1416                             'sectionlist' => \@sectionlist,
1417                             'sections' => \%sections,
1418                             'purpose' => $declaration_purpose
1419                            });
1420     }
1421     else {
1422         print STDERR "Error(${file}:$.): Cannot parse typedef!\n";
1423         ++$errors;
1424     }
1425 }
1426
1427 sub create_parameterlist($$$) {
1428     my $args = shift;
1429     my $splitter = shift;
1430     my $file = shift;
1431     my $type;
1432     my $param;
1433
1434     # temporarily replace commas inside function pointer definition
1435     while ($args =~ /(\([^\),]+),/) {
1436         $args =~ s/(\([^\),]+),/$1#/g;
1437     }
1438
1439     foreach my $arg (split($splitter, $args)) {
1440         # strip comments
1441         $arg =~ s/\/\*.*\*\///;
1442         # strip leading/trailing spaces
1443         $arg =~ s/^\s*//;
1444         $arg =~ s/\s*$//;
1445         $arg =~ s/\s+/ /;
1446
1447         if ($arg =~ /^#/) {
1448             # Treat preprocessor directive as a typeless variable just to fill
1449             # corresponding data structures "correctly". Catch it later in
1450             # output_* subs.
1451             push_parameter($arg, "", $file);
1452         } elsif ($arg =~ m/\(.*\*/) {
1453             # pointer-to-function
1454             $arg =~ tr/#/,/;
1455             $arg =~ m/[^\(]+\(\*\s*([^\)]+)\)/;
1456             $param = $1;
1457             $type = $arg;
1458             $type =~ s/([^\(]+\(\*)$param/$1/;
1459             push_parameter($param, $type, $file);
1460         } elsif ($arg) {
1461             $arg =~ s/\s*:\s*/:/g;
1462             $arg =~ s/\s*\[/\[/g;
1463
1464             my @args = split('\s*,\s*', $arg);
1465             if ($args[0] =~ m/\*/) {
1466                 $args[0] =~ s/(\*+)\s*/ $1/;
1467             }
1468
1469             my @first_arg;
1470             if ($args[0] =~ /^(.*\s+)(.*?\[.*\].*)$/) {
1471                     shift @args;
1472                     push(@first_arg, split('\s+', $1));
1473                     push(@first_arg, $2);
1474             } else {
1475                     @first_arg = split('\s+', shift @args);
1476             }
1477
1478             unshift(@args, pop @first_arg);
1479             $type = join " ", @first_arg;
1480
1481             foreach $param (@args) {
1482                 if ($param =~ m/^(\*+)\s*(.*)/) {
1483                     push_parameter($2, "$type $1", $file);
1484                 }
1485                 elsif ($param =~ m/(.*?):(\d+)/) {
1486                     push_parameter($1, "$type:$2", $file)
1487                 }
1488                 else {
1489                     push_parameter($param, $type, $file);
1490                 }
1491             }
1492         }
1493     }
1494 }
1495
1496 sub push_parameter($$$) {
1497         my $param = shift;
1498         my $type = shift;
1499         my $file = shift;
1500         my $anon = 0;
1501
1502         my $param_name = $param;
1503         $param_name =~ s/\[.*//;
1504
1505         if ($type eq "" && $param =~ /\.\.\.$/)
1506         {
1507             $type="";
1508             $parameterdescs{$param} = "variable arguments";
1509         }
1510         elsif ($type eq "" && ($param eq "" or $param eq "void"))
1511         {
1512             $type="";
1513             $param="void";
1514             $parameterdescs{void} = "no arguments";
1515         }
1516         elsif ($type eq "" && ($param eq "struct" or $param eq "union"))
1517         # handle unnamed (anonymous) union or struct:
1518         {
1519                 $type = $param;
1520                 $param = "{unnamed_" . $param. "}";
1521                 $parameterdescs{$param} = "anonymous\n";
1522                 $anon = 1;
1523         }
1524
1525         # warn if parameter has no description
1526         # (but ignore ones starting with # as these are not parameters
1527         # but inline preprocessor statements);
1528         # also ignore unnamed structs/unions;
1529         if (!$anon) {
1530         if (!defined $parameterdescs{$param_name} && $param_name !~ /^#/) {
1531
1532             $parameterdescs{$param_name} = $undescribed;
1533
1534             if (($type eq 'function') || ($type eq 'enum')) {
1535                 print STDERR "Warning(${file}:$.): Function parameter ".
1536                     "or member '$param' not " .
1537                     "described in '$declaration_name'\n";
1538             }
1539             print STDERR "Warning(${file}:$.):".
1540                          " No description found for parameter '$param'\n";
1541             ++$warnings;
1542         }
1543         }
1544
1545         push @parameterlist, $param;
1546         $parametertypes{$param} = $type;
1547 }
1548
1549 ##
1550 # takes a function prototype and the name of the current file being
1551 # processed and spits out all the details stored in the global
1552 # arrays/hashes.
1553 sub dump_function($$) {
1554     my $prototype = shift;
1555     my $file = shift;
1556
1557     $prototype =~ s/^static +//;
1558     $prototype =~ s/^extern +//;
1559     $prototype =~ s/^fastcall +//;
1560     $prototype =~ s/^asmlinkage +//;
1561     $prototype =~ s/^inline +//;
1562     $prototype =~ s/^__inline__ +//;
1563     $prototype =~ s/^__inline +//;
1564     $prototype =~ s/^__always_inline +//;
1565     $prototype =~ s/^noinline +//;
1566     $prototype =~ s/__devinit +//;
1567     $prototype =~ s/^#define\s+//; #ak added
1568     $prototype =~ s/__attribute__\s*\(\([a-z,]*\)\)//;
1569
1570     # Yes, this truly is vile.  We are looking for:
1571     # 1. Return type (may be nothing if we're looking at a macro)
1572     # 2. Function name
1573     # 3. Function parameters.
1574     #
1575     # All the while we have to watch out for function pointer parameters
1576     # (which IIRC is what the two sections are for), C types (these
1577     # regexps don't even start to express all the possibilities), and
1578     # so on.
1579     #
1580     # If you mess with these regexps, it's a good idea to check that
1581     # the following functions' documentation still comes out right:
1582     # - parport_register_device (function pointer parameters)
1583     # - atomic_set (macro)
1584     # - pci_match_device, __copy_to_user (long return type)
1585
1586     if ($prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1587         $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1588         $prototype =~ m/^(\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1589         $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1590         $prototype =~ m/^(\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1591         $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1592         $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1593         $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1594         $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1595         $prototype =~ m/^(\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1596         $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1597         $prototype =~ m/^(\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1598         $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1599         $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1600         $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1601         $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1602         $prototype =~ m/^(\w+\s+\w+\s*\*\s*\w+\s*\*\s*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/)  {
1603         $return_type = $1;
1604         $declaration_name = $2;
1605         my $args = $3;
1606
1607         create_parameterlist($args, ',', $file);
1608     } else {
1609         print STDERR "Error(${file}:$.): cannot understand prototype: '$prototype'\n";
1610         ++$errors;
1611         return;
1612     }
1613
1614     output_declaration($declaration_name,
1615                        'function',
1616                        {'function' => $declaration_name,
1617                         'module' => $modulename,
1618                         'functiontype' => $return_type,
1619                         'parameterlist' => \@parameterlist,
1620                         'parameterdescs' => \%parameterdescs,
1621                         'parametertypes' => \%parametertypes,
1622                         'sectionlist' => \@sectionlist,
1623                         'sections' => \%sections,
1624                         'purpose' => $declaration_purpose
1625                        });
1626 }
1627
1628 sub process_file($);
1629
1630 # Read the file that maps relative names to absolute names for
1631 # separate source and object directories and for shadow trees.
1632 if (open(SOURCE_MAP, "<.tmp_filelist.txt")) {
1633         my ($relname, $absname);
1634         while(<SOURCE_MAP>) {
1635                 chop();
1636                 ($relname, $absname) = (split())[0..1];
1637                 $relname =~ s:^/+::;
1638                 $source_map{$relname} = $absname;
1639         }
1640         close(SOURCE_MAP);
1641 }
1642
1643 if ($filelist) {
1644         open(FLIST,"<$filelist") or die "Can't open file list $filelist";
1645         while(<FLIST>) {
1646                 chop;
1647                 process_file($_);
1648         }
1649 }
1650
1651 foreach (@ARGV) {
1652     chomp;
1653     process_file($_);
1654 }
1655 if ($verbose && $errors) {
1656   print STDERR "$errors errors\n";
1657 }
1658 if ($verbose && $warnings) {
1659   print STDERR "$warnings warnings\n";
1660 }
1661
1662 exit($errors);
1663
1664 sub reset_state {
1665     $function = "";
1666     %constants = ();
1667     %parameterdescs = ();
1668     %parametertypes = ();
1669     @parameterlist = ();
1670     %sections = ();
1671     @sectionlist = ();
1672     $prototype = "";
1673
1674     $state = 0;
1675 }
1676
1677 sub process_state3_function($$) {
1678     my $x = shift;
1679     my $file = shift;
1680
1681     if ($x =~ m#\s*/\*\s+MACDOC\s*#io || ($x =~ /^#/ && $x !~ /^#define/)) {
1682         # do nothing
1683     }
1684     elsif ($x =~ /([^\{]*)/) {
1685         $prototype .= $1;
1686     }
1687     if (($x =~ /\{/) || ($x =~ /\#define/) || ($x =~ /;/)) {
1688         $prototype =~ s@/\*.*?\*/@@gos; # strip comments.
1689         $prototype =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1690         $prototype =~ s@^\s+@@gos; # strip leading spaces
1691         dump_function($prototype,$file);
1692         reset_state();
1693     }
1694 }
1695
1696 sub process_state3_type($$) {
1697     my $x = shift;
1698     my $file = shift;
1699
1700     $x =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1701     $x =~ s@^\s+@@gos; # strip leading spaces
1702     $x =~ s@\s+$@@gos; # strip trailing spaces
1703     if ($x =~ /^#/) {
1704         # To distinguish preprocessor directive from regular declaration later.
1705         $x .= ";";
1706     }
1707
1708     while (1) {
1709         if ( $x =~ /([^{};]*)([{};])(.*)/ ) {
1710             $prototype .= $1 . $2;
1711             ($2 eq '{') && $brcount++;
1712             ($2 eq '}') && $brcount--;
1713             if (($2 eq ';') && ($brcount == 0)) {
1714                 dump_declaration($prototype,$file);
1715                 reset_state();
1716                 last;
1717             }
1718             $x = $3;
1719         } else {
1720             $prototype .= $x;
1721             last;
1722         }
1723     }
1724 }
1725
1726 # replace <, >, and &
1727 sub xml_escape($) {
1728         my $text = shift;
1729         if (($output_mode eq "text") || ($output_mode eq "man")) {
1730                 return $text;
1731         }
1732         $text =~ s/\&/\\\\\\amp;/g;
1733         $text =~ s/\</\\\\\\lt;/g;
1734         $text =~ s/\>/\\\\\\gt;/g;
1735         return $text;
1736 }
1737
1738 sub process_file($) {
1739     my $file;
1740     my $identifier;
1741     my $func;
1742     my $descr;
1743     my $initial_section_counter = $section_counter;
1744
1745     if (defined($ENV{'SRCTREE'})) {
1746         $file = "$ENV{'SRCTREE'}" . "/" . "@_";
1747     }
1748     else {
1749         $file = "@_";
1750     }
1751     if (defined($source_map{$file})) {
1752         $file = $source_map{$file};
1753     }
1754
1755     if (!open(IN,"<$file")) {
1756         print STDERR "Error: Cannot open file $file\n";
1757         ++$errors;
1758         return;
1759     }
1760
1761     $section_counter = 0;
1762     while (<IN>) {
1763         if ($state == 0) {
1764             if (/$doc_start/o) {
1765                 $state = 1;             # next line is always the function name
1766                 $in_doc_sect = 0;
1767             }
1768         } elsif ($state == 1) { # this line is the function name (always)
1769             if (/$doc_block/o) {
1770                 $state = 4;
1771                 $contents = "";
1772                 if ( $1 eq "" ) {
1773                         $section = $section_intro;
1774                 } else {
1775                         $section = $1;
1776                 }
1777             }
1778             elsif (/$doc_decl/o) {
1779                 $identifier = $1;
1780                 if (/\s*([\w\s]+?)\s*-/) {
1781                     $identifier = $1;
1782                 }
1783
1784                 $state = 2;
1785                 if (/-(.*)/) {
1786                     # strip leading/trailing/multiple spaces #RDD:T:
1787                     $descr= $1;
1788                     $descr =~ s/^\s*//;
1789                     $descr =~ s/\s*$//;
1790                     $descr =~ s/\s+/ /;
1791                     $declaration_purpose = xml_escape($descr);
1792                 } else {
1793                     $declaration_purpose = "";
1794                 }
1795                 if ($identifier =~ m/^struct/) {
1796                     $decl_type = 'struct';
1797                 } elsif ($identifier =~ m/^union/) {
1798                     $decl_type = 'union';
1799                 } elsif ($identifier =~ m/^enum/) {
1800                     $decl_type = 'enum';
1801                 } elsif ($identifier =~ m/^typedef/) {
1802                     $decl_type = 'typedef';
1803                 } else {
1804                     $decl_type = 'function';
1805                 }
1806
1807                 if ($verbose) {
1808                     print STDERR "Info(${file}:$.): Scanning doc for $identifier\n";
1809                 }
1810             } else {
1811                 print STDERR "Warning(${file}:$.): Cannot understand $_ on line $.",
1812                 " - I thought it was a doc line\n";
1813                 ++$warnings;
1814                 $state = 0;
1815             }
1816         } elsif ($state == 2) { # look for head: lines, and include content
1817             if (/$doc_sect/o) {
1818                 $newsection = $1;
1819                 $newcontents = $2;
1820
1821                 if ($contents ne "") {
1822                     if (!$in_doc_sect && $verbose) {
1823                         print STDERR "Warning(${file}:$.): contents before sections\n";
1824                         ++$warnings;
1825                     }
1826                     dump_section($section, xml_escape($contents));
1827                     $section = $section_default;
1828                 }
1829
1830                 $in_doc_sect = 1;
1831                 $contents = $newcontents;
1832                 if ($contents ne "") {
1833                     while ((substr($contents, 0, 1) eq " ") ||
1834                         substr($contents, 0, 1) eq "\t") {
1835                             $contents = substr($contents, 1);
1836                     }
1837                     $contents .= "\n";
1838                 }
1839                 $section = $newsection;
1840             } elsif (/$doc_end/) {
1841
1842                 if ($contents ne "") {
1843                     dump_section($section, xml_escape($contents));
1844                     $section = $section_default;
1845                     $contents = "";
1846                 }
1847
1848                 $prototype = "";
1849                 $state = 3;
1850                 $brcount = 0;
1851 #               print STDERR "end of doc comment, looking for prototype\n";
1852             } elsif (/$doc_content/) {
1853                 # miguel-style comment kludge, look for blank lines after
1854                 # @parameter line to signify start of description
1855                 if ($1 eq "" &&
1856                         ($section =~ m/^@/ || $section eq $section_context)) {
1857                     dump_section($section, xml_escape($contents));
1858                     $section = $section_default;
1859                     $contents = "";
1860                 } else {
1861                     $contents .= $1."\n";
1862                 }
1863             } else {
1864                 # i dont know - bad line?  ignore.
1865                 print STDERR "Warning(${file}:$.): bad line: $_";
1866                 ++$warnings;
1867             }
1868         } elsif ($state == 3) { # scanning for function '{' (end of prototype)
1869             if ($decl_type eq 'function') {
1870                 process_state3_function($_, $file);
1871             } else {
1872                 process_state3_type($_, $file);
1873             }
1874         } elsif ($state == 4) {
1875                 # Documentation block
1876                 if (/$doc_block/) {
1877                         dump_section($section, $contents);
1878                         output_intro({'sectionlist' => \@sectionlist,
1879                                       'sections' => \%sections });
1880                         $contents = "";
1881                         $function = "";
1882                         %constants = ();
1883                         %parameterdescs = ();
1884                         %parametertypes = ();
1885                         @parameterlist = ();
1886                         %sections = ();
1887                         @sectionlist = ();
1888                         $prototype = "";
1889                         if ( $1 eq "" ) {
1890                                 $section = $section_intro;
1891                         } else {
1892                                 $section = $1;
1893                         }
1894                 }
1895                 elsif (/$doc_end/)
1896                 {
1897                         dump_section($section, $contents);
1898                         output_intro({'sectionlist' => \@sectionlist,
1899                                       'sections' => \%sections });
1900                         $contents = "";
1901                         $function = "";
1902                         %constants = ();
1903                         %parameterdescs = ();
1904                         %parametertypes = ();
1905                         @parameterlist = ();
1906                         %sections = ();
1907                         @sectionlist = ();
1908                         $prototype = "";
1909                         $state = 0;
1910                 }
1911                 elsif (/$doc_content/)
1912                 {
1913                         if ( $1 eq "" )
1914                         {
1915                                 $contents .= $blankline;
1916                         }
1917                         else
1918                         {
1919                                 $contents .= $1 . "\n";
1920                         }
1921                 }
1922         }
1923     }
1924     if ($initial_section_counter == $section_counter) {
1925         print STDERR "Warning(${file}): no structured comments found\n";
1926         if ($output_mode eq "xml") {
1927             # The template wants at least one RefEntry here; make one.
1928             print "<refentry>\n";
1929             print " <refnamediv>\n";
1930             print "  <refname>\n";
1931             print "   ${file}\n";
1932             print "  </refname>\n";
1933             print "  <refpurpose>\n";
1934             print "   Document generation inconsistency\n";
1935             print "  </refpurpose>\n";
1936             print " </refnamediv>\n";
1937             print " <refsect1>\n";
1938             print "  <title>\n";
1939             print "   Oops\n";
1940             print "  </title>\n";
1941             print "  <warning>\n";
1942             print "   <para>\n";
1943             print "    The template for this document tried to insert\n";
1944             print "    the structured comment from the file\n";
1945             print "    <filename>${file}</filename> at this point,\n";
1946             print "    but none was found.\n";
1947             print "    This dummy section is inserted to allow\n";
1948             print "    generation to continue.\n";
1949             print "   </para>\n";
1950             print "  </warning>\n";
1951             print " </refsect1>\n";
1952             print "</refentry>\n";
1953         }
1954     }
1955 }