xref: /linux/scripts/kernel-doc (revision 60004ca5dcfbf43182a9ba42bcbbf216c3684433)
1#!/usr/bin/perl -w
2
3use 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## Copyright (C) 2005-2012  Randy Dunlap                         ##
9## Copyright (C) 2012  Dan Luedtke                               ##
10## 								 ##
11## #define enhancements by Armin Kuster <akuster@mvista.com>	 ##
12## Copyright (c) 2000 MontaVista Software, Inc.			 ##
13## 								 ##
14## This software falls under the GNU General Public License.     ##
15## Please read the COPYING file for more information             ##
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# 25/07/2012 - Added support for HTML5
40# -- Dan Luedtke <mail@danrl.de>
41
42#
43# This will read a 'c' file and scan for embedded comments in the
44# style of gnome comments (+minor extensions - see below).
45#
46
47# Note: This only supports 'c'.
48
49# usage:
50# kernel-doc [ -docbook | -html | -html5 | -text | -man | -list ]
51#            [ -no-doc-sections ]
52#            [ -function funcname [ -function funcname ...] ]
53#            c file(s)s > outputfile
54# or
55#            [ -nofunction funcname [ -function funcname ...] ]
56#            c file(s)s > outputfile
57#
58#  Set output format using one of -docbook -html -html5 -text or -man.
59#  Default is man.
60#  The -list format is for internal use by docproc.
61#
62#  -no-doc-sections
63#	Do not output DOC: sections
64#
65#  -function funcname
66#	If set, then only generate documentation for the given function(s) or
67#	DOC: section titles.  All other functions and DOC: sections are ignored.
68#
69#  -nofunction funcname
70#	If set, then only generate documentation for the other function(s)/DOC:
71#	sections. Cannot be used together with -function (yes, that's a bug --
72#	perl hackers can fix it 8))
73#
74#  c files - list of 'c' files to process
75#
76#  All output goes to stdout, with errors to stderr.
77
78#
79# format of comments.
80# In the following table, (...)? signifies optional structure.
81#                         (...)* signifies 0 or more structure elements
82# /**
83#  * function_name(:)? (- short description)?
84# (* @parameterx: (description of parameter x)?)*
85# (* a blank line)?
86#  * (Description:)? (Description of function)?
87#  * (section header: (section description)? )*
88#  (*)?*/
89#
90# So .. the trivial example would be:
91#
92# /**
93#  * my_function
94#  */
95#
96# If the Description: header tag is omitted, then there must be a blank line
97# after the last parameter specification.
98# e.g.
99# /**
100#  * my_function - does my stuff
101#  * @my_arg: its mine damnit
102#  *
103#  * Does my stuff explained.
104#  */
105#
106#  or, could also use:
107# /**
108#  * my_function - does my stuff
109#  * @my_arg: its mine damnit
110#  * Description: Does my stuff explained.
111#  */
112# etc.
113#
114# Besides functions you can also write documentation for structs, unions,
115# enums and typedefs. Instead of the function name you must write the name
116# of the declaration;  the struct/union/enum/typedef must always precede
117# the name. Nesting of declarations is not supported.
118# Use the argument mechanism to document members or constants.
119# e.g.
120# /**
121#  * struct my_struct - short description
122#  * @a: first member
123#  * @b: second member
124#  *
125#  * Longer description
126#  */
127# struct my_struct {
128#     int a;
129#     int b;
130# /* private: */
131#     int c;
132# };
133#
134# All descriptions can be multiline, except the short function description.
135#
136# You can also add additional sections. When documenting kernel functions you
137# should document the "Context:" of the function, e.g. whether the functions
138# can be called form interrupts. Unlike other sections you can end it with an
139# empty line.
140# A non-void function should have a "Return:" section describing the return
141# value(s).
142# Example-sections should contain the string EXAMPLE so that they are marked
143# appropriately in DocBook.
144#
145# Example:
146# /**
147#  * user_function - function that can only be called in user context
148#  * @a: some argument
149#  * Context: !in_interrupt()
150#  *
151#  * Some description
152#  * Example:
153#  *    user_function(22);
154#  */
155# ...
156#
157#
158# All descriptive text is further processed, scanning for the following special
159# patterns, which are highlighted appropriately.
160#
161# 'funcname()' - function
162# '$ENVVAR' - environmental variable
163# '&struct_name' - name of a structure (up to two words including 'struct')
164# '@parameter' - name of a parameter
165# '%CONST' - name of a constant.
166
167## init lots of data
168
169my $errors = 0;
170my $warnings = 0;
171my $anon_struct_union = 0;
172
173# match expressions used to find embedded type information
174my $type_constant = '\%([-_\w]+)';
175my $type_func = '(\w+)\(\)';
176my $type_param = '\@(\w+)';
177my $type_struct = '\&((struct\s*)*[_\w]+)';
178my $type_struct_xml = '\\&amp;((struct\s*)*[_\w]+)';
179my $type_env = '(\$\w+)';
180
181# Output conversion substitutions.
182#  One for each output format
183
184# these work fairly well
185my %highlights_html = ( $type_constant, "<i>\$1</i>",
186			$type_func, "<b>\$1</b>",
187			$type_struct_xml, "<i>\$1</i>",
188			$type_env, "<b><i>\$1</i></b>",
189			$type_param, "<tt><b>\$1</b></tt>" );
190my $local_lt = "\\\\\\\\lt:";
191my $local_gt = "\\\\\\\\gt:";
192my $blankline_html = $local_lt . "p" . $local_gt;	# was "<p>"
193
194# html version 5
195my %highlights_html5 = ( $type_constant, "<span class=\"const\">\$1</span>",
196			$type_func, "<span class=\"func\">\$1</span>",
197			$type_struct_xml, "<span class=\"struct\">\$1</span>",
198			$type_env, "<span class=\"env\">\$1</span>",
199			$type_param, "<span class=\"param\">\$1</span>" );
200my $blankline_html5 = $local_lt . "br /" . $local_gt;
201
202# XML, docbook format
203my %highlights_xml = ( "([^=])\\\"([^\\\"<]+)\\\"", "\$1<quote>\$2</quote>",
204			$type_constant, "<constant>\$1</constant>",
205			$type_func, "<function>\$1</function>",
206			$type_struct_xml, "<structname>\$1</structname>",
207			$type_env, "<envar>\$1</envar>",
208			$type_param, "<parameter>\$1</parameter>" );
209my $blankline_xml = $local_lt . "/para" . $local_gt . $local_lt . "para" . $local_gt . "\n";
210
211# gnome, docbook format
212my %highlights_gnome = ( $type_constant, "<replaceable class=\"option\">\$1</replaceable>",
213			 $type_func, "<function>\$1</function>",
214			 $type_struct, "<structname>\$1</structname>",
215			 $type_env, "<envar>\$1</envar>",
216			 $type_param, "<parameter>\$1</parameter>" );
217my $blankline_gnome = "</para><para>\n";
218
219# these are pretty rough
220my %highlights_man = ( $type_constant, "\$1",
221		       $type_func, "\\\\fB\$1\\\\fP",
222		       $type_struct, "\\\\fI\$1\\\\fP",
223		       $type_param, "\\\\fI\$1\\\\fP" );
224my $blankline_man = "";
225
226# text-mode
227my %highlights_text = ( $type_constant, "\$1",
228			$type_func, "\$1",
229			$type_struct, "\$1",
230			$type_param, "\$1" );
231my $blankline_text = "";
232
233# list mode
234my %highlights_list = ( $type_constant, "\$1",
235			$type_func, "\$1",
236			$type_struct, "\$1",
237			$type_param, "\$1" );
238my $blankline_list = "";
239
240# read arguments
241if ($#ARGV == -1) {
242    usage();
243}
244
245my $kernelversion;
246my $dohighlight = "";
247
248my $verbose = 0;
249my $output_mode = "man";
250my $output_preformatted = 0;
251my $no_doc_sections = 0;
252my %highlights = %highlights_man;
253my $blankline = $blankline_man;
254my $modulename = "Kernel API";
255my $function_only = 0;
256my $show_not_found = 0;
257
258my @build_time;
259if (defined($ENV{'KBUILD_BUILD_TIMESTAMP'}) &&
260    (my $seconds = `date -d"${ENV{'KBUILD_BUILD_TIMESTAMP'}}" +%s`) ne '') {
261    @build_time = gmtime($seconds);
262} else {
263    @build_time = localtime;
264}
265
266my $man_date = ('January', 'February', 'March', 'April', 'May', 'June',
267		'July', 'August', 'September', 'October',
268		'November', 'December')[$build_time[4]] .
269  " " . ($build_time[5]+1900);
270
271# Essentially these are globals.
272# They probably want to be tidied up, made more localised or something.
273# CAVEAT EMPTOR!  Some of the others I localised may not want to be, which
274# could cause "use of undefined value" or other bugs.
275my ($function, %function_table, %parametertypes, $declaration_purpose);
276my ($type, $declaration_name, $return_type);
277my ($newsection, $newcontents, $prototype, $brcount, %source_map);
278
279if (defined($ENV{'KBUILD_VERBOSE'})) {
280	$verbose = "$ENV{'KBUILD_VERBOSE'}";
281}
282
283# Generated docbook code is inserted in a template at a point where
284# docbook v3.1 requires a non-zero sequence of RefEntry's; see:
285# http://www.oasis-open.org/docbook/documentation/reference/html/refentry.html
286# We keep track of number of generated entries and generate a dummy
287# if needs be to ensure the expanded template can be postprocessed
288# into html.
289my $section_counter = 0;
290
291my $lineprefix="";
292
293# states
294# 0 - normal code
295# 1 - looking for function name
296# 2 - scanning field start.
297# 3 - scanning prototype.
298# 4 - documentation block
299my $state;
300my $in_doc_sect;
301
302#declaration types: can be
303# 'function', 'struct', 'union', 'enum', 'typedef'
304my $decl_type;
305
306my $doc_special = "\@\%\$\&";
307
308my $doc_start = '^/\*\*\s*$'; # Allow whitespace at end of comment start.
309my $doc_end = '\*/';
310my $doc_com = '\s*\*\s*';
311my $doc_com_body = '\s*\* ?';
312my $doc_decl = $doc_com . '(\w+)';
313my $doc_sect = $doc_com . '([' . $doc_special . ']?[\w\s]+):(.*)';
314my $doc_content = $doc_com_body . '(.*)';
315my $doc_block = $doc_com . 'DOC:\s*(.*)?';
316
317my %constants;
318my %parameterdescs;
319my @parameterlist;
320my %sections;
321my @sectionlist;
322my $sectcheck;
323my $struct_actual;
324
325my $contents = "";
326my $section_default = "Description";	# default section
327my $section_intro = "Introduction";
328my $section = $section_default;
329my $section_context = "Context";
330my $section_return = "Return";
331
332my $undescribed = "-- undescribed --";
333
334reset_state();
335
336while ($ARGV[0] =~ m/^-(.*)/) {
337    my $cmd = shift @ARGV;
338    if ($cmd eq "-html") {
339	$output_mode = "html";
340	%highlights = %highlights_html;
341	$blankline = $blankline_html;
342    } elsif ($cmd eq "-html5") {
343	$output_mode = "html5";
344	%highlights = %highlights_html5;
345	$blankline = $blankline_html5;
346    } elsif ($cmd eq "-man") {
347	$output_mode = "man";
348	%highlights = %highlights_man;
349	$blankline = $blankline_man;
350    } elsif ($cmd eq "-text") {
351	$output_mode = "text";
352	%highlights = %highlights_text;
353	$blankline = $blankline_text;
354    } elsif ($cmd eq "-docbook") {
355	$output_mode = "xml";
356	%highlights = %highlights_xml;
357	$blankline = $blankline_xml;
358    } elsif ($cmd eq "-list") {
359	$output_mode = "list";
360	%highlights = %highlights_list;
361	$blankline = $blankline_list;
362    } elsif ($cmd eq "-gnome") {
363	$output_mode = "gnome";
364	%highlights = %highlights_gnome;
365	$blankline = $blankline_gnome;
366    } elsif ($cmd eq "-module") { # not needed for XML, inherits from calling document
367	$modulename = shift @ARGV;
368    } elsif ($cmd eq "-function") { # to only output specific functions
369	$function_only = 1;
370	$function = shift @ARGV;
371	$function_table{$function} = 1;
372    } elsif ($cmd eq "-nofunction") { # to only output specific functions
373	$function_only = 2;
374	$function = shift @ARGV;
375	$function_table{$function} = 1;
376    } elsif ($cmd eq "-v") {
377	$verbose = 1;
378    } elsif (($cmd eq "-h") || ($cmd eq "--help")) {
379	usage();
380    } elsif ($cmd eq '-no-doc-sections') {
381	    $no_doc_sections = 1;
382    } elsif ($cmd eq '-show-not-found') {
383	$show_not_found = 1;
384    }
385}
386
387# continue execution near EOF;
388
389sub usage {
390    print "Usage: $0 [ -docbook | -html | -html5 | -text | -man | -list ]\n";
391    print "         [ -no-doc-sections ]\n";
392    print "         [ -function funcname [ -function funcname ...] ]\n";
393    print "         [ -nofunction funcname [ -nofunction funcname ...] ]\n";
394    print "         [ -v ]\n";
395    print "         c source file(s) > outputfile\n";
396    print "         -v : verbose output, more warnings & other info listed\n";
397    exit 1;
398}
399
400# get kernel version from env
401sub get_kernel_version() {
402    my $version = 'unknown kernel version';
403
404    if (defined($ENV{'KERNELVERSION'})) {
405	$version = $ENV{'KERNELVERSION'};
406    }
407    return $version;
408}
409
410##
411# dumps section contents to arrays/hashes intended for that purpose.
412#
413sub dump_section {
414    my $file = shift;
415    my $name = shift;
416    my $contents = join "\n", @_;
417
418    if ($name =~ m/$type_constant/) {
419	$name = $1;
420#	print STDERR "constant section '$1' = '$contents'\n";
421	$constants{$name} = $contents;
422    } elsif ($name =~ m/$type_param/) {
423#	print STDERR "parameter def '$1' = '$contents'\n";
424	$name = $1;
425	$parameterdescs{$name} = $contents;
426	$sectcheck = $sectcheck . $name . " ";
427    } elsif ($name eq "@\.\.\.") {
428#	print STDERR "parameter def '...' = '$contents'\n";
429	$name = "...";
430	$parameterdescs{$name} = $contents;
431	$sectcheck = $sectcheck . $name . " ";
432    } else {
433#	print STDERR "other section '$name' = '$contents'\n";
434	if (defined($sections{$name}) && ($sections{$name} ne "")) {
435		print STDERR "Error(${file}:$.): duplicate section name '$name'\n";
436		++$errors;
437	}
438	$sections{$name} = $contents;
439	push @sectionlist, $name;
440    }
441}
442
443##
444# dump DOC: section after checking that it should go out
445#
446sub dump_doc_section {
447    my $file = shift;
448    my $name = shift;
449    my $contents = join "\n", @_;
450
451    if ($no_doc_sections) {
452        return;
453    }
454
455    if (($function_only == 0) ||
456	( $function_only == 1 && defined($function_table{$name})) ||
457	( $function_only == 2 && !defined($function_table{$name})))
458    {
459	dump_section($file, $name, $contents);
460	output_blockhead({'sectionlist' => \@sectionlist,
461			  'sections' => \%sections,
462			  'module' => $modulename,
463			  'content-only' => ($function_only != 0), });
464    }
465}
466
467##
468# output function
469#
470# parameterdescs, a hash.
471#  function => "function name"
472#  parameterlist => @list of parameters
473#  parameterdescs => %parameter descriptions
474#  sectionlist => @list of sections
475#  sections => %section descriptions
476#
477
478sub output_highlight {
479    my $contents = join "\n",@_;
480    my $line;
481
482#   DEBUG
483#   if (!defined $contents) {
484#	use Carp;
485#	confess "output_highlight got called with no args?\n";
486#   }
487
488    if ($output_mode eq "html" || $output_mode eq "html5" ||
489	$output_mode eq "xml") {
490	$contents = local_unescape($contents);
491	# convert data read & converted thru xml_escape() into &xyz; format:
492	$contents =~ s/\\\\\\/\&/g;
493    }
494#   print STDERR "contents b4:$contents\n";
495    eval $dohighlight;
496    die $@ if $@;
497#   print STDERR "contents af:$contents\n";
498
499#   strip whitespaces when generating html5
500    if ($output_mode eq "html5") {
501	$contents =~ s/^\s+//;
502	$contents =~ s/\s+$//;
503    }
504    foreach $line (split "\n", $contents) {
505	if (! $output_preformatted) {
506	    $line =~ s/^\s*//;
507	}
508	if ($line eq ""){
509	    if (! $output_preformatted) {
510		print $lineprefix, local_unescape($blankline);
511	    }
512	} else {
513	    $line =~ s/\\\\\\/\&/g;
514	    if ($output_mode eq "man" && substr($line, 0, 1) eq ".") {
515		print "\\&$line";
516	    } else {
517		print $lineprefix, $line;
518	    }
519	}
520	print "\n";
521    }
522}
523
524# output sections in html
525sub output_section_html(%) {
526    my %args = %{$_[0]};
527    my $section;
528
529    foreach $section (@{$args{'sectionlist'}}) {
530	print "<h3>$section</h3>\n";
531	print "<blockquote>\n";
532	output_highlight($args{'sections'}{$section});
533	print "</blockquote>\n";
534    }
535}
536
537# output enum in html
538sub output_enum_html(%) {
539    my %args = %{$_[0]};
540    my ($parameter);
541    my $count;
542    print "<h2>enum " . $args{'enum'} . "</h2>\n";
543
544    print "<b>enum " . $args{'enum'} . "</b> {<br>\n";
545    $count = 0;
546    foreach $parameter (@{$args{'parameterlist'}}) {
547	print " <b>" . $parameter . "</b>";
548	if ($count != $#{$args{'parameterlist'}}) {
549	    $count++;
550	    print ",\n";
551	}
552	print "<br>";
553    }
554    print "};<br>\n";
555
556    print "<h3>Constants</h3>\n";
557    print "<dl>\n";
558    foreach $parameter (@{$args{'parameterlist'}}) {
559	print "<dt><b>" . $parameter . "</b>\n";
560	print "<dd>";
561	output_highlight($args{'parameterdescs'}{$parameter});
562    }
563    print "</dl>\n";
564    output_section_html(@_);
565    print "<hr>\n";
566}
567
568# output typedef in html
569sub output_typedef_html(%) {
570    my %args = %{$_[0]};
571    my ($parameter);
572    my $count;
573    print "<h2>typedef " . $args{'typedef'} . "</h2>\n";
574
575    print "<b>typedef " . $args{'typedef'} . "</b>\n";
576    output_section_html(@_);
577    print "<hr>\n";
578}
579
580# output struct in html
581sub output_struct_html(%) {
582    my %args = %{$_[0]};
583    my ($parameter);
584
585    print "<h2>" . $args{'type'} . " " . $args{'struct'} . " - " . $args{'purpose'} . "</h2>\n";
586    print "<b>" . $args{'type'} . " " . $args{'struct'} . "</b> {<br>\n";
587    foreach $parameter (@{$args{'parameterlist'}}) {
588	if ($parameter =~ /^#/) {
589		print "$parameter<br>\n";
590		next;
591	}
592	my $parameter_name = $parameter;
593	$parameter_name =~ s/\[.*//;
594
595	($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
596	$type = $args{'parametertypes'}{$parameter};
597	if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
598	    # pointer-to-function
599	    print "&nbsp; &nbsp; <i>$1</i><b>$parameter</b>) <i>($2)</i>;<br>\n";
600	} elsif ($type =~ m/^(.*?)\s*(:.*)/) {
601	    # bitfield
602	    print "&nbsp; &nbsp; <i>$1</i> <b>$parameter</b>$2;<br>\n";
603	} else {
604	    print "&nbsp; &nbsp; <i>$type</i> <b>$parameter</b>;<br>\n";
605	}
606    }
607    print "};<br>\n";
608
609    print "<h3>Members</h3>\n";
610    print "<dl>\n";
611    foreach $parameter (@{$args{'parameterlist'}}) {
612	($parameter =~ /^#/) && next;
613
614	my $parameter_name = $parameter;
615	$parameter_name =~ s/\[.*//;
616
617	($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
618	print "<dt><b>" . $parameter . "</b>\n";
619	print "<dd>";
620	output_highlight($args{'parameterdescs'}{$parameter_name});
621    }
622    print "</dl>\n";
623    output_section_html(@_);
624    print "<hr>\n";
625}
626
627# output function in html
628sub output_function_html(%) {
629    my %args = %{$_[0]};
630    my ($parameter, $section);
631    my $count;
632
633    print "<h2>" . $args{'function'} . " - " . $args{'purpose'} . "</h2>\n";
634    print "<i>" . $args{'functiontype'} . "</i>\n";
635    print "<b>" . $args{'function'} . "</b>\n";
636    print "(";
637    $count = 0;
638    foreach $parameter (@{$args{'parameterlist'}}) {
639	$type = $args{'parametertypes'}{$parameter};
640	if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
641	    # pointer-to-function
642	    print "<i>$1</i><b>$parameter</b>) <i>($2)</i>";
643	} else {
644	    print "<i>" . $type . "</i> <b>" . $parameter . "</b>";
645	}
646	if ($count != $#{$args{'parameterlist'}}) {
647	    $count++;
648	    print ",\n";
649	}
650    }
651    print ")\n";
652
653    print "<h3>Arguments</h3>\n";
654    print "<dl>\n";
655    foreach $parameter (@{$args{'parameterlist'}}) {
656	my $parameter_name = $parameter;
657	$parameter_name =~ s/\[.*//;
658
659	($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
660	print "<dt><b>" . $parameter . "</b>\n";
661	print "<dd>";
662	output_highlight($args{'parameterdescs'}{$parameter_name});
663    }
664    print "</dl>\n";
665    output_section_html(@_);
666    print "<hr>\n";
667}
668
669# output DOC: block header in html
670sub output_blockhead_html(%) {
671    my %args = %{$_[0]};
672    my ($parameter, $section);
673    my $count;
674
675    foreach $section (@{$args{'sectionlist'}}) {
676	print "<h3>$section</h3>\n";
677	print "<ul>\n";
678	output_highlight($args{'sections'}{$section});
679	print "</ul>\n";
680    }
681    print "<hr>\n";
682}
683
684# output sections in html5
685sub output_section_html5(%) {
686    my %args = %{$_[0]};
687    my $section;
688
689    foreach $section (@{$args{'sectionlist'}}) {
690	print "<section>\n";
691	print "<h1>$section</h1>\n";
692	print "<p>\n";
693	output_highlight($args{'sections'}{$section});
694	print "</p>\n";
695	print "</section>\n";
696    }
697}
698
699# output enum in html5
700sub output_enum_html5(%) {
701    my %args = %{$_[0]};
702    my ($parameter);
703    my $count;
704    my $html5id;
705
706    $html5id = $args{'enum'};
707    $html5id =~ s/[^a-zA-Z0-9\-]+/_/g;
708    print "<article class=\"enum\" id=\"enum:". $html5id . "\">";
709    print "<h1>enum " . $args{'enum'} . "</h1>\n";
710    print "<ol class=\"code\">\n";
711    print "<li>";
712    print "<span class=\"keyword\">enum</span> ";
713    print "<span class=\"identifier\">" . $args{'enum'} . "</span> {";
714    print "</li>\n";
715    $count = 0;
716    foreach $parameter (@{$args{'parameterlist'}}) {
717	print "<li class=\"indent\">";
718	print "<span class=\"param\">" . $parameter . "</span>";
719	if ($count != $#{$args{'parameterlist'}}) {
720	    $count++;
721	    print ",";
722	}
723	print "</li>\n";
724    }
725    print "<li>};</li>\n";
726    print "</ol>\n";
727
728    print "<section>\n";
729    print "<h1>Constants</h1>\n";
730    print "<dl>\n";
731    foreach $parameter (@{$args{'parameterlist'}}) {
732	print "<dt>" . $parameter . "</dt>\n";
733	print "<dd>";
734	output_highlight($args{'parameterdescs'}{$parameter});
735	print "</dd>\n";
736    }
737    print "</dl>\n";
738    print "</section>\n";
739    output_section_html5(@_);
740    print "</article>\n";
741}
742
743# output typedef in html5
744sub output_typedef_html5(%) {
745    my %args = %{$_[0]};
746    my ($parameter);
747    my $count;
748    my $html5id;
749
750    $html5id = $args{'typedef'};
751    $html5id =~ s/[^a-zA-Z0-9\-]+/_/g;
752    print "<article class=\"typedef\" id=\"typedef:" . $html5id . "\">\n";
753    print "<h1>typedef " . $args{'typedef'} . "</h1>\n";
754
755    print "<ol class=\"code\">\n";
756    print "<li>";
757    print "<span class=\"keyword\">typedef</span> ";
758    print "<span class=\"identifier\">" . $args{'typedef'} . "</span>";
759    print "</li>\n";
760    print "</ol>\n";
761    output_section_html5(@_);
762    print "</article>\n";
763}
764
765# output struct in html5
766sub output_struct_html5(%) {
767    my %args = %{$_[0]};
768    my ($parameter);
769    my $html5id;
770
771    $html5id = $args{'struct'};
772    $html5id =~ s/[^a-zA-Z0-9\-]+/_/g;
773    print "<article class=\"struct\" id=\"struct:" . $html5id . "\">\n";
774    print "<hgroup>\n";
775    print "<h1>" . $args{'type'} . " " . $args{'struct'} . "</h1>";
776    print "<h2>". $args{'purpose'} . "</h2>\n";
777    print "</hgroup>\n";
778    print "<ol class=\"code\">\n";
779    print "<li>";
780    print "<span class=\"type\">" . $args{'type'} . "</span> ";
781    print "<span class=\"identifier\">" . $args{'struct'} . "</span> {";
782    print "</li>\n";
783    foreach $parameter (@{$args{'parameterlist'}}) {
784	print "<li class=\"indent\">";
785	if ($parameter =~ /^#/) {
786		print "<span class=\"param\">" . $parameter ."</span>\n";
787		print "</li>\n";
788		next;
789	}
790	my $parameter_name = $parameter;
791	$parameter_name =~ s/\[.*//;
792
793	($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
794	$type = $args{'parametertypes'}{$parameter};
795	if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
796	    # pointer-to-function
797	    print "<span class=\"type\">$1</span> ";
798	    print "<span class=\"param\">$parameter</span>";
799	    print "<span class=\"type\">)</span> ";
800	    print "(<span class=\"args\">$2</span>);";
801	} elsif ($type =~ m/^(.*?)\s*(:.*)/) {
802	    # bitfield
803	    print "<span class=\"type\">$1</span> ";
804	    print "<span class=\"param\">$parameter</span>";
805	    print "<span class=\"bits\">$2</span>;";
806	} else {
807	    print "<span class=\"type\">$type</span> ";
808	    print "<span class=\"param\">$parameter</span>;";
809	}
810	print "</li>\n";
811    }
812    print "<li>};</li>\n";
813    print "</ol>\n";
814
815    print "<section>\n";
816    print "<h1>Members</h1>\n";
817    print "<dl>\n";
818    foreach $parameter (@{$args{'parameterlist'}}) {
819	($parameter =~ /^#/) && next;
820
821	my $parameter_name = $parameter;
822	$parameter_name =~ s/\[.*//;
823
824	($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
825	print "<dt>" . $parameter . "</dt>\n";
826	print "<dd>";
827	output_highlight($args{'parameterdescs'}{$parameter_name});
828	print "</dd>\n";
829    }
830    print "</dl>\n";
831    print "</section>\n";
832    output_section_html5(@_);
833    print "</article>\n";
834}
835
836# output function in html5
837sub output_function_html5(%) {
838    my %args = %{$_[0]};
839    my ($parameter, $section);
840    my $count;
841    my $html5id;
842
843    $html5id = $args{'function'};
844    $html5id =~ s/[^a-zA-Z0-9\-]+/_/g;
845    print "<article class=\"function\" id=\"func:". $html5id . "\">\n";
846    print "<hgroup>\n";
847    print "<h1>" . $args{'function'} . "</h1>";
848    print "<h2>" . $args{'purpose'} . "</h2>\n";
849    print "</hgroup>\n";
850    print "<ol class=\"code\">\n";
851    print "<li>";
852    print "<span class=\"type\">" . $args{'functiontype'} . "</span> ";
853    print "<span class=\"identifier\">" . $args{'function'} . "</span> (";
854    print "</li>";
855    $count = 0;
856    foreach $parameter (@{$args{'parameterlist'}}) {
857	print "<li class=\"indent\">";
858	$type = $args{'parametertypes'}{$parameter};
859	if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
860	    # pointer-to-function
861	    print "<span class=\"type\">$1</span> ";
862	    print "<span class=\"param\">$parameter</span>";
863	    print "<span class=\"type\">)</span> ";
864	    print "(<span class=\"args\">$2</span>)";
865	} else {
866	    print "<span class=\"type\">$type</span> ";
867	    print "<span class=\"param\">$parameter</span>";
868	}
869	if ($count != $#{$args{'parameterlist'}}) {
870	    $count++;
871	    print ",";
872	}
873	print "</li>\n";
874    }
875    print "<li>)</li>\n";
876    print "</ol>\n";
877
878    print "<section>\n";
879    print "<h1>Arguments</h1>\n";
880    print "<p>\n";
881    print "<dl>\n";
882    foreach $parameter (@{$args{'parameterlist'}}) {
883	my $parameter_name = $parameter;
884	$parameter_name =~ s/\[.*//;
885
886	($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
887	print "<dt>" . $parameter . "</dt>\n";
888	print "<dd>";
889	output_highlight($args{'parameterdescs'}{$parameter_name});
890	print "</dd>\n";
891    }
892    print "</dl>\n";
893    print "</section>\n";
894    output_section_html5(@_);
895    print "</article>\n";
896}
897
898# output DOC: block header in html5
899sub output_blockhead_html5(%) {
900    my %args = %{$_[0]};
901    my ($parameter, $section);
902    my $count;
903    my $html5id;
904
905    foreach $section (@{$args{'sectionlist'}}) {
906	$html5id = $section;
907	$html5id =~ s/[^a-zA-Z0-9\-]+/_/g;
908	print "<article class=\"doc\" id=\"doc:". $html5id . "\">\n";
909	print "<h1>$section</h1>\n";
910	print "<p>\n";
911	output_highlight($args{'sections'}{$section});
912	print "</p>\n";
913    }
914    print "</article>\n";
915}
916
917sub output_section_xml(%) {
918    my %args = %{$_[0]};
919    my $section;
920    # print out each section
921    $lineprefix="   ";
922    foreach $section (@{$args{'sectionlist'}}) {
923	print "<refsect1>\n";
924	print "<title>$section</title>\n";
925	if ($section =~ m/EXAMPLE/i) {
926	    print "<informalexample><programlisting>\n";
927	    $output_preformatted = 1;
928	} else {
929	    print "<para>\n";
930	}
931	output_highlight($args{'sections'}{$section});
932	$output_preformatted = 0;
933	if ($section =~ m/EXAMPLE/i) {
934	    print "</programlisting></informalexample>\n";
935	} else {
936	    print "</para>\n";
937	}
938	print "</refsect1>\n";
939    }
940}
941
942# output function in XML DocBook
943sub output_function_xml(%) {
944    my %args = %{$_[0]};
945    my ($parameter, $section);
946    my $count;
947    my $id;
948
949    $id = "API-" . $args{'function'};
950    $id =~ s/[^A-Za-z0-9]/-/g;
951
952    print "<refentry id=\"$id\">\n";
953    print "<refentryinfo>\n";
954    print " <title>LINUX</title>\n";
955    print " <productname>Kernel Hackers Manual</productname>\n";
956    print " <date>$man_date</date>\n";
957    print "</refentryinfo>\n";
958    print "<refmeta>\n";
959    print " <refentrytitle><phrase>" . $args{'function'} . "</phrase></refentrytitle>\n";
960    print " <manvolnum>9</manvolnum>\n";
961    print " <refmiscinfo class=\"version\">" . $kernelversion . "</refmiscinfo>\n";
962    print "</refmeta>\n";
963    print "<refnamediv>\n";
964    print " <refname>" . $args{'function'} . "</refname>\n";
965    print " <refpurpose>\n";
966    print "  ";
967    output_highlight ($args{'purpose'});
968    print " </refpurpose>\n";
969    print "</refnamediv>\n";
970
971    print "<refsynopsisdiv>\n";
972    print " <title>Synopsis</title>\n";
973    print "  <funcsynopsis><funcprototype>\n";
974    print "   <funcdef>" . $args{'functiontype'} . " ";
975    print "<function>" . $args{'function'} . " </function></funcdef>\n";
976
977    $count = 0;
978    if ($#{$args{'parameterlist'}} >= 0) {
979	foreach $parameter (@{$args{'parameterlist'}}) {
980	    $type = $args{'parametertypes'}{$parameter};
981	    if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
982		# pointer-to-function
983		print "   <paramdef>$1<parameter>$parameter</parameter>)\n";
984		print "     <funcparams>$2</funcparams></paramdef>\n";
985	    } else {
986		print "   <paramdef>" . $type;
987		print " <parameter>$parameter</parameter></paramdef>\n";
988	    }
989	}
990    } else {
991	print "  <void/>\n";
992    }
993    print "  </funcprototype></funcsynopsis>\n";
994    print "</refsynopsisdiv>\n";
995
996    # print parameters
997    print "<refsect1>\n <title>Arguments</title>\n";
998    if ($#{$args{'parameterlist'}} >= 0) {
999	print " <variablelist>\n";
1000	foreach $parameter (@{$args{'parameterlist'}}) {
1001	    my $parameter_name = $parameter;
1002	    $parameter_name =~ s/\[.*//;
1003
1004	    print "  <varlistentry>\n   <term><parameter>$parameter</parameter></term>\n";
1005	    print "   <listitem>\n    <para>\n";
1006	    $lineprefix="     ";
1007	    output_highlight($args{'parameterdescs'}{$parameter_name});
1008	    print "    </para>\n   </listitem>\n  </varlistentry>\n";
1009	}
1010	print " </variablelist>\n";
1011    } else {
1012	print " <para>\n  None\n </para>\n";
1013    }
1014    print "</refsect1>\n";
1015
1016    output_section_xml(@_);
1017    print "</refentry>\n\n";
1018}
1019
1020# output struct in XML DocBook
1021sub output_struct_xml(%) {
1022    my %args = %{$_[0]};
1023    my ($parameter, $section);
1024    my $id;
1025
1026    $id = "API-struct-" . $args{'struct'};
1027    $id =~ s/[^A-Za-z0-9]/-/g;
1028
1029    print "<refentry id=\"$id\">\n";
1030    print "<refentryinfo>\n";
1031    print " <title>LINUX</title>\n";
1032    print " <productname>Kernel Hackers Manual</productname>\n";
1033    print " <date>$man_date</date>\n";
1034    print "</refentryinfo>\n";
1035    print "<refmeta>\n";
1036    print " <refentrytitle><phrase>" . $args{'type'} . " " . $args{'struct'} . "</phrase></refentrytitle>\n";
1037    print " <manvolnum>9</manvolnum>\n";
1038    print " <refmiscinfo class=\"version\">" . $kernelversion . "</refmiscinfo>\n";
1039    print "</refmeta>\n";
1040    print "<refnamediv>\n";
1041    print " <refname>" . $args{'type'} . " " . $args{'struct'} . "</refname>\n";
1042    print " <refpurpose>\n";
1043    print "  ";
1044    output_highlight ($args{'purpose'});
1045    print " </refpurpose>\n";
1046    print "</refnamediv>\n";
1047
1048    print "<refsynopsisdiv>\n";
1049    print " <title>Synopsis</title>\n";
1050    print "  <programlisting>\n";
1051    print $args{'type'} . " " . $args{'struct'} . " {\n";
1052    foreach $parameter (@{$args{'parameterlist'}}) {
1053	if ($parameter =~ /^#/) {
1054	    my $prm = $parameter;
1055	    # convert data read & converted thru xml_escape() into &xyz; format:
1056	    # This allows us to have #define macros interspersed in a struct.
1057	    $prm =~ s/\\\\\\/\&/g;
1058	    print "$prm\n";
1059	    next;
1060	}
1061
1062	my $parameter_name = $parameter;
1063	$parameter_name =~ s/\[.*//;
1064
1065	defined($args{'parameterdescs'}{$parameter_name}) || next;
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 "  $1 $parameter) ($2);\n";
1071	} elsif ($type =~ m/^(.*?)\s*(:.*)/) {
1072	    # bitfield
1073	    print "  $1 $parameter$2;\n";
1074	} else {
1075	    print "  " . $type . " " . $parameter . ";\n";
1076	}
1077    }
1078    print "};";
1079    print "  </programlisting>\n";
1080    print "</refsynopsisdiv>\n";
1081
1082    print " <refsect1>\n";
1083    print "  <title>Members</title>\n";
1084
1085    if ($#{$args{'parameterlist'}} >= 0) {
1086    print "  <variablelist>\n";
1087    foreach $parameter (@{$args{'parameterlist'}}) {
1088      ($parameter =~ /^#/) && next;
1089
1090      my $parameter_name = $parameter;
1091      $parameter_name =~ s/\[.*//;
1092
1093      defined($args{'parameterdescs'}{$parameter_name}) || next;
1094      ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
1095      print "    <varlistentry>";
1096      print "      <term>$parameter</term>\n";
1097      print "      <listitem><para>\n";
1098      output_highlight($args{'parameterdescs'}{$parameter_name});
1099      print "      </para></listitem>\n";
1100      print "    </varlistentry>\n";
1101    }
1102    print "  </variablelist>\n";
1103    } else {
1104	print " <para>\n  None\n </para>\n";
1105    }
1106    print " </refsect1>\n";
1107
1108    output_section_xml(@_);
1109
1110    print "</refentry>\n\n";
1111}
1112
1113# output enum in XML DocBook
1114sub output_enum_xml(%) {
1115    my %args = %{$_[0]};
1116    my ($parameter, $section);
1117    my $count;
1118    my $id;
1119
1120    $id = "API-enum-" . $args{'enum'};
1121    $id =~ s/[^A-Za-z0-9]/-/g;
1122
1123    print "<refentry id=\"$id\">\n";
1124    print "<refentryinfo>\n";
1125    print " <title>LINUX</title>\n";
1126    print " <productname>Kernel Hackers Manual</productname>\n";
1127    print " <date>$man_date</date>\n";
1128    print "</refentryinfo>\n";
1129    print "<refmeta>\n";
1130    print " <refentrytitle><phrase>enum " . $args{'enum'} . "</phrase></refentrytitle>\n";
1131    print " <manvolnum>9</manvolnum>\n";
1132    print " <refmiscinfo class=\"version\">" . $kernelversion . "</refmiscinfo>\n";
1133    print "</refmeta>\n";
1134    print "<refnamediv>\n";
1135    print " <refname>enum " . $args{'enum'} . "</refname>\n";
1136    print " <refpurpose>\n";
1137    print "  ";
1138    output_highlight ($args{'purpose'});
1139    print " </refpurpose>\n";
1140    print "</refnamediv>\n";
1141
1142    print "<refsynopsisdiv>\n";
1143    print " <title>Synopsis</title>\n";
1144    print "  <programlisting>\n";
1145    print "enum " . $args{'enum'} . " {\n";
1146    $count = 0;
1147    foreach $parameter (@{$args{'parameterlist'}}) {
1148	print "  $parameter";
1149	if ($count != $#{$args{'parameterlist'}}) {
1150	    $count++;
1151	    print ",";
1152	}
1153	print "\n";
1154    }
1155    print "};";
1156    print "  </programlisting>\n";
1157    print "</refsynopsisdiv>\n";
1158
1159    print "<refsect1>\n";
1160    print " <title>Constants</title>\n";
1161    print "  <variablelist>\n";
1162    foreach $parameter (@{$args{'parameterlist'}}) {
1163      my $parameter_name = $parameter;
1164      $parameter_name =~ s/\[.*//;
1165
1166      print "    <varlistentry>";
1167      print "      <term>$parameter</term>\n";
1168      print "      <listitem><para>\n";
1169      output_highlight($args{'parameterdescs'}{$parameter_name});
1170      print "      </para></listitem>\n";
1171      print "    </varlistentry>\n";
1172    }
1173    print "  </variablelist>\n";
1174    print "</refsect1>\n";
1175
1176    output_section_xml(@_);
1177
1178    print "</refentry>\n\n";
1179}
1180
1181# output typedef in XML DocBook
1182sub output_typedef_xml(%) {
1183    my %args = %{$_[0]};
1184    my ($parameter, $section);
1185    my $id;
1186
1187    $id = "API-typedef-" . $args{'typedef'};
1188    $id =~ s/[^A-Za-z0-9]/-/g;
1189
1190    print "<refentry id=\"$id\">\n";
1191    print "<refentryinfo>\n";
1192    print " <title>LINUX</title>\n";
1193    print " <productname>Kernel Hackers Manual</productname>\n";
1194    print " <date>$man_date</date>\n";
1195    print "</refentryinfo>\n";
1196    print "<refmeta>\n";
1197    print " <refentrytitle><phrase>typedef " . $args{'typedef'} . "</phrase></refentrytitle>\n";
1198    print " <manvolnum>9</manvolnum>\n";
1199    print "</refmeta>\n";
1200    print "<refnamediv>\n";
1201    print " <refname>typedef " . $args{'typedef'} . "</refname>\n";
1202    print " <refpurpose>\n";
1203    print "  ";
1204    output_highlight ($args{'purpose'});
1205    print " </refpurpose>\n";
1206    print "</refnamediv>\n";
1207
1208    print "<refsynopsisdiv>\n";
1209    print " <title>Synopsis</title>\n";
1210    print "  <synopsis>typedef " . $args{'typedef'} . ";</synopsis>\n";
1211    print "</refsynopsisdiv>\n";
1212
1213    output_section_xml(@_);
1214
1215    print "</refentry>\n\n";
1216}
1217
1218# output in XML DocBook
1219sub output_blockhead_xml(%) {
1220    my %args = %{$_[0]};
1221    my ($parameter, $section);
1222    my $count;
1223
1224    my $id = $args{'module'};
1225    $id =~ s/[^A-Za-z0-9]/-/g;
1226
1227    # print out each section
1228    $lineprefix="   ";
1229    foreach $section (@{$args{'sectionlist'}}) {
1230	if (!$args{'content-only'}) {
1231		print "<refsect1>\n <title>$section</title>\n";
1232	}
1233	if ($section =~ m/EXAMPLE/i) {
1234	    print "<example><para>\n";
1235	    $output_preformatted = 1;
1236	} else {
1237	    print "<para>\n";
1238	}
1239	output_highlight($args{'sections'}{$section});
1240	$output_preformatted = 0;
1241	if ($section =~ m/EXAMPLE/i) {
1242	    print "</para></example>\n";
1243	} else {
1244	    print "</para>";
1245	}
1246	if (!$args{'content-only'}) {
1247		print "\n</refsect1>\n";
1248	}
1249    }
1250
1251    print "\n\n";
1252}
1253
1254# output in XML DocBook
1255sub output_function_gnome {
1256    my %args = %{$_[0]};
1257    my ($parameter, $section);
1258    my $count;
1259    my $id;
1260
1261    $id = $args{'module'} . "-" . $args{'function'};
1262    $id =~ s/[^A-Za-z0-9]/-/g;
1263
1264    print "<sect2>\n";
1265    print " <title id=\"$id\">" . $args{'function'} . "</title>\n";
1266
1267    print "  <funcsynopsis>\n";
1268    print "   <funcdef>" . $args{'functiontype'} . " ";
1269    print "<function>" . $args{'function'} . " ";
1270    print "</function></funcdef>\n";
1271
1272    $count = 0;
1273    if ($#{$args{'parameterlist'}} >= 0) {
1274	foreach $parameter (@{$args{'parameterlist'}}) {
1275	    $type = $args{'parametertypes'}{$parameter};
1276	    if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1277		# pointer-to-function
1278		print "   <paramdef>$1 <parameter>$parameter</parameter>)\n";
1279		print "     <funcparams>$2</funcparams></paramdef>\n";
1280	    } else {
1281		print "   <paramdef>" . $type;
1282		print " <parameter>$parameter</parameter></paramdef>\n";
1283	    }
1284	}
1285    } else {
1286	print "  <void>\n";
1287    }
1288    print "  </funcsynopsis>\n";
1289    if ($#{$args{'parameterlist'}} >= 0) {
1290	print " <informaltable pgwide=\"1\" frame=\"none\" role=\"params\">\n";
1291	print "<tgroup cols=\"2\">\n";
1292	print "<colspec colwidth=\"2*\">\n";
1293	print "<colspec colwidth=\"8*\">\n";
1294	print "<tbody>\n";
1295	foreach $parameter (@{$args{'parameterlist'}}) {
1296	    my $parameter_name = $parameter;
1297	    $parameter_name =~ s/\[.*//;
1298
1299	    print "  <row><entry align=\"right\"><parameter>$parameter</parameter></entry>\n";
1300	    print "   <entry>\n";
1301	    $lineprefix="     ";
1302	    output_highlight($args{'parameterdescs'}{$parameter_name});
1303	    print "    </entry></row>\n";
1304	}
1305	print " </tbody></tgroup></informaltable>\n";
1306    } else {
1307	print " <para>\n  None\n </para>\n";
1308    }
1309
1310    # print out each section
1311    $lineprefix="   ";
1312    foreach $section (@{$args{'sectionlist'}}) {
1313	print "<simplesect>\n <title>$section</title>\n";
1314	if ($section =~ m/EXAMPLE/i) {
1315	    print "<example><programlisting>\n";
1316	    $output_preformatted = 1;
1317	} else {
1318	}
1319	print "<para>\n";
1320	output_highlight($args{'sections'}{$section});
1321	$output_preformatted = 0;
1322	print "</para>\n";
1323	if ($section =~ m/EXAMPLE/i) {
1324	    print "</programlisting></example>\n";
1325	} else {
1326	}
1327	print " </simplesect>\n";
1328    }
1329
1330    print "</sect2>\n\n";
1331}
1332
1333##
1334# output function in man
1335sub output_function_man(%) {
1336    my %args = %{$_[0]};
1337    my ($parameter, $section);
1338    my $count;
1339
1340    print ".TH \"$args{'function'}\" 9 \"$args{'function'}\" \"$man_date\" \"Kernel Hacker's Manual\" LINUX\n";
1341
1342    print ".SH NAME\n";
1343    print $args{'function'} . " \\- " . $args{'purpose'} . "\n";
1344
1345    print ".SH SYNOPSIS\n";
1346    if ($args{'functiontype'} ne "") {
1347	print ".B \"" . $args{'functiontype'} . "\" " . $args{'function'} . "\n";
1348    } else {
1349	print ".B \"" . $args{'function'} . "\n";
1350    }
1351    $count = 0;
1352    my $parenth = "(";
1353    my $post = ",";
1354    foreach my $parameter (@{$args{'parameterlist'}}) {
1355	if ($count == $#{$args{'parameterlist'}}) {
1356	    $post = ");";
1357	}
1358	$type = $args{'parametertypes'}{$parameter};
1359	if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1360	    # pointer-to-function
1361	    print ".BI \"" . $parenth . $1 . "\" " . $parameter . " \") (" . $2 . ")" . $post . "\"\n";
1362	} else {
1363	    $type =~ s/([^\*])$/$1 /;
1364	    print ".BI \"" . $parenth . $type . "\" " . $parameter . " \"" . $post . "\"\n";
1365	}
1366	$count++;
1367	$parenth = "";
1368    }
1369
1370    print ".SH ARGUMENTS\n";
1371    foreach $parameter (@{$args{'parameterlist'}}) {
1372	my $parameter_name = $parameter;
1373	$parameter_name =~ s/\[.*//;
1374
1375	print ".IP \"" . $parameter . "\" 12\n";
1376	output_highlight($args{'parameterdescs'}{$parameter_name});
1377    }
1378    foreach $section (@{$args{'sectionlist'}}) {
1379	print ".SH \"", uc $section, "\"\n";
1380	output_highlight($args{'sections'}{$section});
1381    }
1382}
1383
1384##
1385# output enum in man
1386sub output_enum_man(%) {
1387    my %args = %{$_[0]};
1388    my ($parameter, $section);
1389    my $count;
1390
1391    print ".TH \"$args{'module'}\" 9 \"enum $args{'enum'}\" \"$man_date\" \"API Manual\" LINUX\n";
1392
1393    print ".SH NAME\n";
1394    print "enum " . $args{'enum'} . " \\- " . $args{'purpose'} . "\n";
1395
1396    print ".SH SYNOPSIS\n";
1397    print "enum " . $args{'enum'} . " {\n";
1398    $count = 0;
1399    foreach my $parameter (@{$args{'parameterlist'}}) {
1400	print ".br\n.BI \"    $parameter\"\n";
1401	if ($count == $#{$args{'parameterlist'}}) {
1402	    print "\n};\n";
1403	    last;
1404	}
1405	else {
1406	    print ", \n.br\n";
1407	}
1408	$count++;
1409    }
1410
1411    print ".SH Constants\n";
1412    foreach $parameter (@{$args{'parameterlist'}}) {
1413	my $parameter_name = $parameter;
1414	$parameter_name =~ s/\[.*//;
1415
1416	print ".IP \"" . $parameter . "\" 12\n";
1417	output_highlight($args{'parameterdescs'}{$parameter_name});
1418    }
1419    foreach $section (@{$args{'sectionlist'}}) {
1420	print ".SH \"$section\"\n";
1421	output_highlight($args{'sections'}{$section});
1422    }
1423}
1424
1425##
1426# output struct in man
1427sub output_struct_man(%) {
1428    my %args = %{$_[0]};
1429    my ($parameter, $section);
1430
1431    print ".TH \"$args{'module'}\" 9 \"" . $args{'type'} . " " . $args{'struct'} . "\" \"$man_date\" \"API Manual\" LINUX\n";
1432
1433    print ".SH NAME\n";
1434    print $args{'type'} . " " . $args{'struct'} . " \\- " . $args{'purpose'} . "\n";
1435
1436    print ".SH SYNOPSIS\n";
1437    print $args{'type'} . " " . $args{'struct'} . " {\n.br\n";
1438
1439    foreach my $parameter (@{$args{'parameterlist'}}) {
1440	if ($parameter =~ /^#/) {
1441	    print ".BI \"$parameter\"\n.br\n";
1442	    next;
1443	}
1444	my $parameter_name = $parameter;
1445	$parameter_name =~ s/\[.*//;
1446
1447	($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
1448	$type = $args{'parametertypes'}{$parameter};
1449	if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1450	    # pointer-to-function
1451	    print ".BI \"    " . $1 . "\" " . $parameter . " \") (" . $2 . ")" . "\"\n;\n";
1452	} elsif ($type =~ m/^(.*?)\s*(:.*)/) {
1453	    # bitfield
1454	    print ".BI \"    " . $1 . "\ \" " . $parameter . $2 . " \"" . "\"\n;\n";
1455	} else {
1456	    $type =~ s/([^\*])$/$1 /;
1457	    print ".BI \"    " . $type . "\" " . $parameter . " \"" . "\"\n;\n";
1458	}
1459	print "\n.br\n";
1460    }
1461    print "};\n.br\n";
1462
1463    print ".SH Members\n";
1464    foreach $parameter (@{$args{'parameterlist'}}) {
1465	($parameter =~ /^#/) && next;
1466
1467	my $parameter_name = $parameter;
1468	$parameter_name =~ s/\[.*//;
1469
1470	($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
1471	print ".IP \"" . $parameter . "\" 12\n";
1472	output_highlight($args{'parameterdescs'}{$parameter_name});
1473    }
1474    foreach $section (@{$args{'sectionlist'}}) {
1475	print ".SH \"$section\"\n";
1476	output_highlight($args{'sections'}{$section});
1477    }
1478}
1479
1480##
1481# output typedef in man
1482sub output_typedef_man(%) {
1483    my %args = %{$_[0]};
1484    my ($parameter, $section);
1485
1486    print ".TH \"$args{'module'}\" 9 \"$args{'typedef'}\" \"$man_date\" \"API Manual\" LINUX\n";
1487
1488    print ".SH NAME\n";
1489    print "typedef " . $args{'typedef'} . " \\- " . $args{'purpose'} . "\n";
1490
1491    foreach $section (@{$args{'sectionlist'}}) {
1492	print ".SH \"$section\"\n";
1493	output_highlight($args{'sections'}{$section});
1494    }
1495}
1496
1497sub output_blockhead_man(%) {
1498    my %args = %{$_[0]};
1499    my ($parameter, $section);
1500    my $count;
1501
1502    print ".TH \"$args{'module'}\" 9 \"$args{'module'}\" \"$man_date\" \"API Manual\" LINUX\n";
1503
1504    foreach $section (@{$args{'sectionlist'}}) {
1505	print ".SH \"$section\"\n";
1506	output_highlight($args{'sections'}{$section});
1507    }
1508}
1509
1510##
1511# output in text
1512sub output_function_text(%) {
1513    my %args = %{$_[0]};
1514    my ($parameter, $section);
1515    my $start;
1516
1517    print "Name:\n\n";
1518    print $args{'function'} . " - " . $args{'purpose'} . "\n";
1519
1520    print "\nSynopsis:\n\n";
1521    if ($args{'functiontype'} ne "") {
1522	$start = $args{'functiontype'} . " " . $args{'function'} . " (";
1523    } else {
1524	$start = $args{'function'} . " (";
1525    }
1526    print $start;
1527
1528    my $count = 0;
1529    foreach my $parameter (@{$args{'parameterlist'}}) {
1530	$type = $args{'parametertypes'}{$parameter};
1531	if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1532	    # pointer-to-function
1533	    print $1 . $parameter . ") (" . $2;
1534	} else {
1535	    print $type . " " . $parameter;
1536	}
1537	if ($count != $#{$args{'parameterlist'}}) {
1538	    $count++;
1539	    print ",\n";
1540	    print " " x length($start);
1541	} else {
1542	    print ");\n\n";
1543	}
1544    }
1545
1546    print "Arguments:\n\n";
1547    foreach $parameter (@{$args{'parameterlist'}}) {
1548	my $parameter_name = $parameter;
1549	$parameter_name =~ s/\[.*//;
1550
1551	print $parameter . "\n\t" . $args{'parameterdescs'}{$parameter_name} . "\n";
1552    }
1553    output_section_text(@_);
1554}
1555
1556#output sections in text
1557sub output_section_text(%) {
1558    my %args = %{$_[0]};
1559    my $section;
1560
1561    print "\n";
1562    foreach $section (@{$args{'sectionlist'}}) {
1563	print "$section:\n\n";
1564	output_highlight($args{'sections'}{$section});
1565    }
1566    print "\n\n";
1567}
1568
1569# output enum in text
1570sub output_enum_text(%) {
1571    my %args = %{$_[0]};
1572    my ($parameter);
1573    my $count;
1574    print "Enum:\n\n";
1575
1576    print "enum " . $args{'enum'} . " - " . $args{'purpose'} . "\n\n";
1577    print "enum " . $args{'enum'} . " {\n";
1578    $count = 0;
1579    foreach $parameter (@{$args{'parameterlist'}}) {
1580	print "\t$parameter";
1581	if ($count != $#{$args{'parameterlist'}}) {
1582	    $count++;
1583	    print ",";
1584	}
1585	print "\n";
1586    }
1587    print "};\n\n";
1588
1589    print "Constants:\n\n";
1590    foreach $parameter (@{$args{'parameterlist'}}) {
1591	print "$parameter\n\t";
1592	print $args{'parameterdescs'}{$parameter} . "\n";
1593    }
1594
1595    output_section_text(@_);
1596}
1597
1598# output typedef in text
1599sub output_typedef_text(%) {
1600    my %args = %{$_[0]};
1601    my ($parameter);
1602    my $count;
1603    print "Typedef:\n\n";
1604
1605    print "typedef " . $args{'typedef'} . " - " . $args{'purpose'} . "\n";
1606    output_section_text(@_);
1607}
1608
1609# output struct as text
1610sub output_struct_text(%) {
1611    my %args = %{$_[0]};
1612    my ($parameter);
1613
1614    print $args{'type'} . " " . $args{'struct'} . " - " . $args{'purpose'} . "\n\n";
1615    print $args{'type'} . " " . $args{'struct'} . " {\n";
1616    foreach $parameter (@{$args{'parameterlist'}}) {
1617	if ($parameter =~ /^#/) {
1618	    print "$parameter\n";
1619	    next;
1620	}
1621
1622	my $parameter_name = $parameter;
1623	$parameter_name =~ s/\[.*//;
1624
1625	($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
1626	$type = $args{'parametertypes'}{$parameter};
1627	if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1628	    # pointer-to-function
1629	    print "\t$1 $parameter) ($2);\n";
1630	} elsif ($type =~ m/^(.*?)\s*(:.*)/) {
1631	    # bitfield
1632	    print "\t$1 $parameter$2;\n";
1633	} else {
1634	    print "\t" . $type . " " . $parameter . ";\n";
1635	}
1636    }
1637    print "};\n\n";
1638
1639    print "Members:\n\n";
1640    foreach $parameter (@{$args{'parameterlist'}}) {
1641	($parameter =~ /^#/) && next;
1642
1643	my $parameter_name = $parameter;
1644	$parameter_name =~ s/\[.*//;
1645
1646	($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
1647	print "$parameter\n\t";
1648	print $args{'parameterdescs'}{$parameter_name} . "\n";
1649    }
1650    print "\n";
1651    output_section_text(@_);
1652}
1653
1654sub output_blockhead_text(%) {
1655    my %args = %{$_[0]};
1656    my ($parameter, $section);
1657
1658    foreach $section (@{$args{'sectionlist'}}) {
1659	print " $section:\n";
1660	print "    -> ";
1661	output_highlight($args{'sections'}{$section});
1662    }
1663}
1664
1665## list mode output functions
1666
1667sub output_function_list(%) {
1668    my %args = %{$_[0]};
1669
1670    print $args{'function'} . "\n";
1671}
1672
1673# output enum in list
1674sub output_enum_list(%) {
1675    my %args = %{$_[0]};
1676    print $args{'enum'} . "\n";
1677}
1678
1679# output typedef in list
1680sub output_typedef_list(%) {
1681    my %args = %{$_[0]};
1682    print $args{'typedef'} . "\n";
1683}
1684
1685# output struct as list
1686sub output_struct_list(%) {
1687    my %args = %{$_[0]};
1688
1689    print $args{'struct'} . "\n";
1690}
1691
1692sub output_blockhead_list(%) {
1693    my %args = %{$_[0]};
1694    my ($parameter, $section);
1695
1696    foreach $section (@{$args{'sectionlist'}}) {
1697	print "DOC: $section\n";
1698    }
1699}
1700
1701##
1702# generic output function for all types (function, struct/union, typedef, enum);
1703# calls the generated, variable output_ function name based on
1704# functype and output_mode
1705sub output_declaration {
1706    no strict 'refs';
1707    my $name = shift;
1708    my $functype = shift;
1709    my $func = "output_${functype}_$output_mode";
1710    if (($function_only==0) ||
1711	( $function_only == 1 && defined($function_table{$name})) ||
1712	( $function_only == 2 && !defined($function_table{$name})))
1713    {
1714	&$func(@_);
1715	$section_counter++;
1716    }
1717}
1718
1719##
1720# generic output function - calls the right one based on current output mode.
1721sub output_blockhead {
1722    no strict 'refs';
1723    my $func = "output_blockhead_" . $output_mode;
1724    &$func(@_);
1725    $section_counter++;
1726}
1727
1728##
1729# takes a declaration (struct, union, enum, typedef) and
1730# invokes the right handler. NOT called for functions.
1731sub dump_declaration($$) {
1732    no strict 'refs';
1733    my ($prototype, $file) = @_;
1734    my $func = "dump_" . $decl_type;
1735    &$func(@_);
1736}
1737
1738sub dump_union($$) {
1739    dump_struct(@_);
1740}
1741
1742sub dump_struct($$) {
1743    my $x = shift;
1744    my $file = shift;
1745    my $nested;
1746
1747    if ($x =~ /(struct|union)\s+(\w+)\s*{(.*)}/) {
1748	#my $decl_type = $1;
1749	$declaration_name = $2;
1750	my $members = $3;
1751
1752	# ignore embedded structs or unions
1753	$members =~ s/({.*})//g;
1754	$nested = $1;
1755
1756	# ignore members marked private:
1757	$members =~ s/\/\*\s*private:.*?\/\*\s*public:.*?\*\///gos;
1758	$members =~ s/\/\*\s*private:.*//gos;
1759	# strip comments:
1760	$members =~ s/\/\*.*?\*\///gos;
1761	$nested =~ s/\/\*.*?\*\///gos;
1762	# strip kmemcheck_bitfield_{begin,end}.*;
1763	$members =~ s/kmemcheck_bitfield_.*?;//gos;
1764	# strip attributes
1765	$members =~ s/__aligned\s*\([^;]*\)//gos;
1766
1767	create_parameterlist($members, ';', $file);
1768	check_sections($file, $declaration_name, "struct", $sectcheck, $struct_actual, $nested);
1769
1770	output_declaration($declaration_name,
1771			   'struct',
1772			   {'struct' => $declaration_name,
1773			    'module' => $modulename,
1774			    'parameterlist' => \@parameterlist,
1775			    'parameterdescs' => \%parameterdescs,
1776			    'parametertypes' => \%parametertypes,
1777			    'sectionlist' => \@sectionlist,
1778			    'sections' => \%sections,
1779			    'purpose' => $declaration_purpose,
1780			    'type' => $decl_type
1781			   });
1782    }
1783    else {
1784	print STDERR "Error(${file}:$.): Cannot parse struct or union!\n";
1785	++$errors;
1786    }
1787}
1788
1789sub dump_enum($$) {
1790    my $x = shift;
1791    my $file = shift;
1792
1793    $x =~ s@/\*.*?\*/@@gos;	# strip comments.
1794    $x =~ s/^#\s*define\s+.*$//; # strip #define macros inside enums
1795
1796    if ($x =~ /enum\s+(\w+)\s*{(.*)}/) {
1797	$declaration_name = $1;
1798	my $members = $2;
1799
1800	foreach my $arg (split ',', $members) {
1801	    $arg =~ s/^\s*(\w+).*/$1/;
1802	    push @parameterlist, $arg;
1803	    if (!$parameterdescs{$arg}) {
1804		$parameterdescs{$arg} = $undescribed;
1805		print STDERR "Warning(${file}:$.): Enum value '$arg' ".
1806		    "not described in enum '$declaration_name'\n";
1807	    }
1808
1809	}
1810
1811	output_declaration($declaration_name,
1812			   'enum',
1813			   {'enum' => $declaration_name,
1814			    'module' => $modulename,
1815			    'parameterlist' => \@parameterlist,
1816			    'parameterdescs' => \%parameterdescs,
1817			    'sectionlist' => \@sectionlist,
1818			    'sections' => \%sections,
1819			    'purpose' => $declaration_purpose
1820			   });
1821    }
1822    else {
1823	print STDERR "Error(${file}:$.): Cannot parse enum!\n";
1824	++$errors;
1825    }
1826}
1827
1828sub dump_typedef($$) {
1829    my $x = shift;
1830    my $file = shift;
1831
1832    $x =~ s@/\*.*?\*/@@gos;	# strip comments.
1833    while (($x =~ /\(*.\)\s*;$/) || ($x =~ /\[*.\]\s*;$/)) {
1834	$x =~ s/\(*.\)\s*;$/;/;
1835	$x =~ s/\[*.\]\s*;$/;/;
1836    }
1837
1838    if ($x =~ /typedef.*\s+(\w+)\s*;/) {
1839	$declaration_name = $1;
1840
1841	output_declaration($declaration_name,
1842			   'typedef',
1843			   {'typedef' => $declaration_name,
1844			    'module' => $modulename,
1845			    'sectionlist' => \@sectionlist,
1846			    'sections' => \%sections,
1847			    'purpose' => $declaration_purpose
1848			   });
1849    }
1850    else {
1851	print STDERR "Error(${file}:$.): Cannot parse typedef!\n";
1852	++$errors;
1853    }
1854}
1855
1856sub save_struct_actual($) {
1857    my $actual = shift;
1858
1859    # strip all spaces from the actual param so that it looks like one string item
1860    $actual =~ s/\s*//g;
1861    $struct_actual = $struct_actual . $actual . " ";
1862}
1863
1864sub create_parameterlist($$$) {
1865    my $args = shift;
1866    my $splitter = shift;
1867    my $file = shift;
1868    my $type;
1869    my $param;
1870
1871    # temporarily replace commas inside function pointer definition
1872    while ($args =~ /(\([^\),]+),/) {
1873	$args =~ s/(\([^\),]+),/$1#/g;
1874    }
1875
1876    foreach my $arg (split($splitter, $args)) {
1877	# strip comments
1878	$arg =~ s/\/\*.*\*\///;
1879	# strip leading/trailing spaces
1880	$arg =~ s/^\s*//;
1881	$arg =~ s/\s*$//;
1882	$arg =~ s/\s+/ /;
1883
1884	if ($arg =~ /^#/) {
1885	    # Treat preprocessor directive as a typeless variable just to fill
1886	    # corresponding data structures "correctly". Catch it later in
1887	    # output_* subs.
1888	    push_parameter($arg, "", $file);
1889	} elsif ($arg =~ m/\(.+\)\s*\(/) {
1890	    # pointer-to-function
1891	    $arg =~ tr/#/,/;
1892	    $arg =~ m/[^\(]+\(\*?\s*(\w*)\s*\)/;
1893	    $param = $1;
1894	    $type = $arg;
1895	    $type =~ s/([^\(]+\(\*?)\s*$param/$1/;
1896	    save_struct_actual($param);
1897	    push_parameter($param, $type, $file);
1898	} elsif ($arg) {
1899	    $arg =~ s/\s*:\s*/:/g;
1900	    $arg =~ s/\s*\[/\[/g;
1901
1902	    my @args = split('\s*,\s*', $arg);
1903	    if ($args[0] =~ m/\*/) {
1904		$args[0] =~ s/(\*+)\s*/ $1/;
1905	    }
1906
1907	    my @first_arg;
1908	    if ($args[0] =~ /^(.*\s+)(.*?\[.*\].*)$/) {
1909		    shift @args;
1910		    push(@first_arg, split('\s+', $1));
1911		    push(@first_arg, $2);
1912	    } else {
1913		    @first_arg = split('\s+', shift @args);
1914	    }
1915
1916	    unshift(@args, pop @first_arg);
1917	    $type = join " ", @first_arg;
1918
1919	    foreach $param (@args) {
1920		if ($param =~ m/^(\*+)\s*(.*)/) {
1921		    save_struct_actual($2);
1922		    push_parameter($2, "$type $1", $file);
1923		}
1924		elsif ($param =~ m/(.*?):(\d+)/) {
1925		    if ($type ne "") { # skip unnamed bit-fields
1926			save_struct_actual($1);
1927			push_parameter($1, "$type:$2", $file)
1928		    }
1929		}
1930		else {
1931		    save_struct_actual($param);
1932		    push_parameter($param, $type, $file);
1933		}
1934	    }
1935	}
1936    }
1937}
1938
1939sub push_parameter($$$) {
1940	my $param = shift;
1941	my $type = shift;
1942	my $file = shift;
1943
1944	if (($anon_struct_union == 1) && ($type eq "") &&
1945	    ($param eq "}")) {
1946		return;		# ignore the ending }; from anon. struct/union
1947	}
1948
1949	$anon_struct_union = 0;
1950	my $param_name = $param;
1951	$param_name =~ s/\[.*//;
1952
1953	if ($type eq "" && $param =~ /\.\.\.$/)
1954	{
1955	    if (!defined $parameterdescs{$param} || $parameterdescs{$param} eq "") {
1956		$parameterdescs{$param} = "variable arguments";
1957	    }
1958	}
1959	elsif ($type eq "" && ($param eq "" or $param eq "void"))
1960	{
1961	    $param="void";
1962	    $parameterdescs{void} = "no arguments";
1963	}
1964	elsif ($type eq "" && ($param eq "struct" or $param eq "union"))
1965	# handle unnamed (anonymous) union or struct:
1966	{
1967		$type = $param;
1968		$param = "{unnamed_" . $param . "}";
1969		$parameterdescs{$param} = "anonymous\n";
1970		$anon_struct_union = 1;
1971	}
1972
1973	# warn if parameter has no description
1974	# (but ignore ones starting with # as these are not parameters
1975	# but inline preprocessor statements);
1976	# also ignore unnamed structs/unions;
1977	if (!$anon_struct_union) {
1978	if (!defined $parameterdescs{$param_name} && $param_name !~ /^#/) {
1979
1980	    $parameterdescs{$param_name} = $undescribed;
1981
1982	    if (($type eq 'function') || ($type eq 'enum')) {
1983		print STDERR "Warning(${file}:$.): Function parameter ".
1984		    "or member '$param' not " .
1985		    "described in '$declaration_name'\n";
1986	    }
1987	    print STDERR "Warning(${file}:$.):" .
1988			 " No description found for parameter '$param'\n";
1989	    ++$warnings;
1990	}
1991	}
1992
1993	$param = xml_escape($param);
1994
1995	# strip spaces from $param so that it is one continuous string
1996	# on @parameterlist;
1997	# this fixes a problem where check_sections() cannot find
1998	# a parameter like "addr[6 + 2]" because it actually appears
1999	# as "addr[6", "+", "2]" on the parameter list;
2000	# but it's better to maintain the param string unchanged for output,
2001	# so just weaken the string compare in check_sections() to ignore
2002	# "[blah" in a parameter string;
2003	###$param =~ s/\s*//g;
2004	push @parameterlist, $param;
2005	$parametertypes{$param} = $type;
2006}
2007
2008sub check_sections($$$$$$) {
2009	my ($file, $decl_name, $decl_type, $sectcheck, $prmscheck, $nested) = @_;
2010	my @sects = split ' ', $sectcheck;
2011	my @prms = split ' ', $prmscheck;
2012	my $err;
2013	my ($px, $sx);
2014	my $prm_clean;		# strip trailing "[array size]" and/or beginning "*"
2015
2016	foreach $sx (0 .. $#sects) {
2017		$err = 1;
2018		foreach $px (0 .. $#prms) {
2019			$prm_clean = $prms[$px];
2020			$prm_clean =~ s/\[.*\]//;
2021			$prm_clean =~ s/__attribute__\s*\(\([a-z,_\*\s\(\)]*\)\)//i;
2022			# ignore array size in a parameter string;
2023			# however, the original param string may contain
2024			# spaces, e.g.:  addr[6 + 2]
2025			# and this appears in @prms as "addr[6" since the
2026			# parameter list is split at spaces;
2027			# hence just ignore "[..." for the sections check;
2028			$prm_clean =~ s/\[.*//;
2029
2030			##$prm_clean =~ s/^\**//;
2031			if ($prm_clean eq $sects[$sx]) {
2032				$err = 0;
2033				last;
2034			}
2035		}
2036		if ($err) {
2037			if ($decl_type eq "function") {
2038				print STDERR "Warning(${file}:$.): " .
2039					"Excess function parameter " .
2040					"'$sects[$sx]' " .
2041					"description in '$decl_name'\n";
2042				++$warnings;
2043			} else {
2044				if ($nested !~ m/\Q$sects[$sx]\E/) {
2045				    print STDERR "Warning(${file}:$.): " .
2046					"Excess struct/union/enum/typedef member " .
2047					"'$sects[$sx]' " .
2048					"description in '$decl_name'\n";
2049				    ++$warnings;
2050				}
2051			}
2052		}
2053	}
2054}
2055
2056##
2057# Checks the section describing the return value of a function.
2058sub check_return_section {
2059        my $file = shift;
2060        my $declaration_name = shift;
2061        my $return_type = shift;
2062
2063        # Ignore an empty return type (It's a macro)
2064        # Ignore functions with a "void" return type. (But don't ignore "void *")
2065        if (($return_type eq "") || ($return_type =~ /void\s*\w*\s*$/)) {
2066                return;
2067        }
2068
2069        if (!defined($sections{$section_return}) ||
2070            $sections{$section_return} eq "") {
2071                print STDERR "Warning(${file}:$.): " .
2072                        "No description found for return value of " .
2073                        "'$declaration_name'\n";
2074                ++$warnings;
2075        }
2076}
2077
2078##
2079# takes a function prototype and the name of the current file being
2080# processed and spits out all the details stored in the global
2081# arrays/hashes.
2082sub dump_function($$) {
2083    my $prototype = shift;
2084    my $file = shift;
2085    my $noret = 0;
2086
2087    $prototype =~ s/^static +//;
2088    $prototype =~ s/^extern +//;
2089    $prototype =~ s/^asmlinkage +//;
2090    $prototype =~ s/^inline +//;
2091    $prototype =~ s/^__inline__ +//;
2092    $prototype =~ s/^__inline +//;
2093    $prototype =~ s/^__always_inline +//;
2094    $prototype =~ s/^noinline +//;
2095    $prototype =~ s/__init +//;
2096    $prototype =~ s/__init_or_module +//;
2097    $prototype =~ s/__meminit +//;
2098    $prototype =~ s/__must_check +//;
2099    $prototype =~ s/__weak +//;
2100    my $define = $prototype =~ s/^#\s*define\s+//; #ak added
2101    $prototype =~ s/__attribute__\s*\(\([a-z,]*\)\)//;
2102
2103    # Yes, this truly is vile.  We are looking for:
2104    # 1. Return type (may be nothing if we're looking at a macro)
2105    # 2. Function name
2106    # 3. Function parameters.
2107    #
2108    # All the while we have to watch out for function pointer parameters
2109    # (which IIRC is what the two sections are for), C types (these
2110    # regexps don't even start to express all the possibilities), and
2111    # so on.
2112    #
2113    # If you mess with these regexps, it's a good idea to check that
2114    # the following functions' documentation still comes out right:
2115    # - parport_register_device (function pointer parameters)
2116    # - atomic_set (macro)
2117    # - pci_match_device, __copy_to_user (long return type)
2118
2119    if ($define && $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s+/) {
2120        # This is an object-like macro, it has no return type and no parameter
2121        # list.
2122        # Function-like macros are not allowed to have spaces between
2123        # declaration_name and opening parenthesis (notice the \s+).
2124        $return_type = $1;
2125        $declaration_name = $2;
2126        $noret = 1;
2127    } elsif ($prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
2128	$prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
2129	$prototype =~ m/^(\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
2130	$prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
2131	$prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
2132	$prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
2133	$prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
2134	$prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
2135	$prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
2136	$prototype =~ m/^(\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
2137	$prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
2138	$prototype =~ m/^(\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
2139	$prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
2140	$prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
2141	$prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
2142	$prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
2143	$prototype =~ m/^(\w+\s+\w+\s*\*\s*\w+\s*\*\s*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/)  {
2144	$return_type = $1;
2145	$declaration_name = $2;
2146	my $args = $3;
2147
2148	create_parameterlist($args, ',', $file);
2149    } else {
2150	print STDERR "Warning(${file}:$.): cannot understand function prototype: '$prototype'\n";
2151	return;
2152    }
2153
2154	my $prms = join " ", @parameterlist;
2155	check_sections($file, $declaration_name, "function", $sectcheck, $prms, "");
2156
2157        # This check emits a lot of warnings at the moment, because many
2158        # functions don't have a 'Return' doc section. So until the number
2159        # of warnings goes sufficiently down, the check is only performed in
2160        # verbose mode.
2161        # TODO: always perform the check.
2162        if ($verbose && !$noret) {
2163                check_return_section($file, $declaration_name, $return_type);
2164        }
2165
2166    output_declaration($declaration_name,
2167		       'function',
2168		       {'function' => $declaration_name,
2169			'module' => $modulename,
2170			'functiontype' => $return_type,
2171			'parameterlist' => \@parameterlist,
2172			'parameterdescs' => \%parameterdescs,
2173			'parametertypes' => \%parametertypes,
2174			'sectionlist' => \@sectionlist,
2175			'sections' => \%sections,
2176			'purpose' => $declaration_purpose
2177		       });
2178}
2179
2180sub reset_state {
2181    $function = "";
2182    %constants = ();
2183    %parameterdescs = ();
2184    %parametertypes = ();
2185    @parameterlist = ();
2186    %sections = ();
2187    @sectionlist = ();
2188    $sectcheck = "";
2189    $struct_actual = "";
2190    $prototype = "";
2191
2192    $state = 0;
2193}
2194
2195sub tracepoint_munge($) {
2196	my $file = shift;
2197	my $tracepointname = 0;
2198	my $tracepointargs = 0;
2199
2200	if ($prototype =~ m/TRACE_EVENT\((.*?),/) {
2201		$tracepointname = $1;
2202	}
2203	if ($prototype =~ m/DEFINE_SINGLE_EVENT\((.*?),/) {
2204		$tracepointname = $1;
2205	}
2206	if ($prototype =~ m/DEFINE_EVENT\((.*?),(.*?),/) {
2207		$tracepointname = $2;
2208	}
2209	$tracepointname =~ s/^\s+//; #strip leading whitespace
2210	if ($prototype =~ m/TP_PROTO\((.*?)\)/) {
2211		$tracepointargs = $1;
2212	}
2213	if (($tracepointname eq 0) || ($tracepointargs eq 0)) {
2214		print STDERR "Warning(${file}:$.): Unrecognized tracepoint format: \n".
2215			     "$prototype\n";
2216	} else {
2217		$prototype = "static inline void trace_$tracepointname($tracepointargs)";
2218	}
2219}
2220
2221sub syscall_munge() {
2222	my $void = 0;
2223
2224	$prototype =~ s@[\r\n\t]+@ @gos; # strip newlines/CR's/tabs
2225##	if ($prototype =~ m/SYSCALL_DEFINE0\s*\(\s*(a-zA-Z0-9_)*\s*\)/) {
2226	if ($prototype =~ m/SYSCALL_DEFINE0/) {
2227		$void = 1;
2228##		$prototype = "long sys_$1(void)";
2229	}
2230
2231	$prototype =~ s/SYSCALL_DEFINE.*\(/long sys_/; # fix return type & func name
2232	if ($prototype =~ m/long (sys_.*?),/) {
2233		$prototype =~ s/,/\(/;
2234	} elsif ($void) {
2235		$prototype =~ s/\)/\(void\)/;
2236	}
2237
2238	# now delete all of the odd-number commas in $prototype
2239	# so that arg types & arg names don't have a comma between them
2240	my $count = 0;
2241	my $len = length($prototype);
2242	if ($void) {
2243		$len = 0;	# skip the for-loop
2244	}
2245	for (my $ix = 0; $ix < $len; $ix++) {
2246		if (substr($prototype, $ix, 1) eq ',') {
2247			$count++;
2248			if ($count % 2 == 1) {
2249				substr($prototype, $ix, 1) = ' ';
2250			}
2251		}
2252	}
2253}
2254
2255sub process_state3_function($$) {
2256    my $x = shift;
2257    my $file = shift;
2258
2259    $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
2260
2261    if ($x =~ m#\s*/\*\s+MACDOC\s*#io || ($x =~ /^#/ && $x !~ /^#\s*define/)) {
2262	# do nothing
2263    }
2264    elsif ($x =~ /([^\{]*)/) {
2265	$prototype .= $1;
2266    }
2267
2268    if (($x =~ /\{/) || ($x =~ /\#\s*define/) || ($x =~ /;/)) {
2269	$prototype =~ s@/\*.*?\*/@@gos;	# strip comments.
2270	$prototype =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
2271	$prototype =~ s@^\s+@@gos; # strip leading spaces
2272	if ($prototype =~ /SYSCALL_DEFINE/) {
2273		syscall_munge();
2274	}
2275	if ($prototype =~ /TRACE_EVENT/ || $prototype =~ /DEFINE_EVENT/ ||
2276	    $prototype =~ /DEFINE_SINGLE_EVENT/)
2277	{
2278		tracepoint_munge($file);
2279	}
2280	dump_function($prototype, $file);
2281	reset_state();
2282    }
2283}
2284
2285sub process_state3_type($$) {
2286    my $x = shift;
2287    my $file = shift;
2288
2289    $x =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
2290    $x =~ s@^\s+@@gos; # strip leading spaces
2291    $x =~ s@\s+$@@gos; # strip trailing spaces
2292    $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
2293
2294    if ($x =~ /^#/) {
2295	# To distinguish preprocessor directive from regular declaration later.
2296	$x .= ";";
2297    }
2298
2299    while (1) {
2300	if ( $x =~ /([^{};]*)([{};])(.*)/ ) {
2301	    $prototype .= $1 . $2;
2302	    ($2 eq '{') && $brcount++;
2303	    ($2 eq '}') && $brcount--;
2304	    if (($2 eq ';') && ($brcount == 0)) {
2305		dump_declaration($prototype, $file);
2306		reset_state();
2307		last;
2308	    }
2309	    $x = $3;
2310	} else {
2311	    $prototype .= $x;
2312	    last;
2313	}
2314    }
2315}
2316
2317# xml_escape: replace <, >, and & in the text stream;
2318#
2319# however, formatting controls that are generated internally/locally in the
2320# kernel-doc script are not escaped here; instead, they begin life like
2321# $blankline_html (4 of '\' followed by a mnemonic + ':'), then these strings
2322# are converted to their mnemonic-expected output, without the 4 * '\' & ':',
2323# just before actual output; (this is done by local_unescape())
2324sub xml_escape($) {
2325	my $text = shift;
2326	if (($output_mode eq "text") || ($output_mode eq "man")) {
2327		return $text;
2328	}
2329	$text =~ s/\&/\\\\\\amp;/g;
2330	$text =~ s/\</\\\\\\lt;/g;
2331	$text =~ s/\>/\\\\\\gt;/g;
2332	return $text;
2333}
2334
2335# convert local escape strings to html
2336# local escape strings look like:  '\\\\menmonic:' (that's 4 backslashes)
2337sub local_unescape($) {
2338	my $text = shift;
2339	if (($output_mode eq "text") || ($output_mode eq "man")) {
2340		return $text;
2341	}
2342	$text =~ s/\\\\\\\\lt:/</g;
2343	$text =~ s/\\\\\\\\gt:/>/g;
2344	return $text;
2345}
2346
2347sub process_file($) {
2348    my $file;
2349    my $identifier;
2350    my $func;
2351    my $descr;
2352    my $in_purpose = 0;
2353    my $initial_section_counter = $section_counter;
2354
2355    if (defined($ENV{'SRCTREE'})) {
2356	$file = "$ENV{'SRCTREE'}" . "/" . "@_";
2357    }
2358    else {
2359	$file = "@_";
2360    }
2361    if (defined($source_map{$file})) {
2362	$file = $source_map{$file};
2363    }
2364
2365    if (!open(IN,"<$file")) {
2366	print STDERR "Error: Cannot open file $file\n";
2367	++$errors;
2368	return;
2369    }
2370
2371    $. = 1;
2372
2373    $section_counter = 0;
2374    while (<IN>) {
2375	while (s/\\\s*$//) {
2376	    $_ .= <IN>;
2377	}
2378	if ($state == 0) {
2379	    if (/$doc_start/o) {
2380		$state = 1;		# next line is always the function name
2381		$in_doc_sect = 0;
2382	    }
2383	} elsif ($state == 1) {	# this line is the function name (always)
2384	    if (/$doc_block/o) {
2385		$state = 4;
2386		$contents = "";
2387		if ( $1 eq "" ) {
2388			$section = $section_intro;
2389		} else {
2390			$section = $1;
2391		}
2392	    }
2393	    elsif (/$doc_decl/o) {
2394		$identifier = $1;
2395		if (/\s*([\w\s]+?)\s*-/) {
2396		    $identifier = $1;
2397		}
2398
2399		$state = 2;
2400		if (/-(.*)/) {
2401		    # strip leading/trailing/multiple spaces
2402		    $descr= $1;
2403		    $descr =~ s/^\s*//;
2404		    $descr =~ s/\s*$//;
2405		    $descr =~ s/\s+/ /g;
2406		    $declaration_purpose = xml_escape($descr);
2407		    $in_purpose = 1;
2408		} else {
2409		    $declaration_purpose = "";
2410		}
2411
2412		if (($declaration_purpose eq "") && $verbose) {
2413			print STDERR "Warning(${file}:$.): missing initial short description on line:\n";
2414			print STDERR $_;
2415			++$warnings;
2416		}
2417
2418		if ($identifier =~ m/^struct/) {
2419		    $decl_type = 'struct';
2420		} elsif ($identifier =~ m/^union/) {
2421		    $decl_type = 'union';
2422		} elsif ($identifier =~ m/^enum/) {
2423		    $decl_type = 'enum';
2424		} elsif ($identifier =~ m/^typedef/) {
2425		    $decl_type = 'typedef';
2426		} else {
2427		    $decl_type = 'function';
2428		}
2429
2430		if ($verbose) {
2431		    print STDERR "Info(${file}:$.): Scanning doc for $identifier\n";
2432		}
2433	    } else {
2434		print STDERR "Warning(${file}:$.): Cannot understand $_ on line $.",
2435		" - I thought it was a doc line\n";
2436		++$warnings;
2437		$state = 0;
2438	    }
2439	} elsif ($state == 2) {	# look for head: lines, and include content
2440	    if (/$doc_sect/o) {
2441		$newsection = $1;
2442		$newcontents = $2;
2443
2444		if (($contents ne "") && ($contents ne "\n")) {
2445		    if (!$in_doc_sect && $verbose) {
2446			print STDERR "Warning(${file}:$.): contents before sections\n";
2447			++$warnings;
2448		    }
2449		    dump_section($file, $section, xml_escape($contents));
2450		    $section = $section_default;
2451		}
2452
2453		$in_doc_sect = 1;
2454		$in_purpose = 0;
2455		$contents = $newcontents;
2456		if ($contents ne "") {
2457		    while ((substr($contents, 0, 1) eq " ") ||
2458			substr($contents, 0, 1) eq "\t") {
2459			    $contents = substr($contents, 1);
2460		    }
2461		    $contents .= "\n";
2462		}
2463		$section = $newsection;
2464	    } elsif (/$doc_end/) {
2465
2466		if (($contents ne "") && ($contents ne "\n")) {
2467		    dump_section($file, $section, xml_escape($contents));
2468		    $section = $section_default;
2469		    $contents = "";
2470		}
2471		# look for doc_com + <text> + doc_end:
2472		if ($_ =~ m'\s*\*\s*[a-zA-Z_0-9:\.]+\*/') {
2473		    print STDERR "Warning(${file}:$.): suspicious ending line: $_";
2474		    ++$warnings;
2475		}
2476
2477		$prototype = "";
2478		$state = 3;
2479		$brcount = 0;
2480#		print STDERR "end of doc comment, looking for prototype\n";
2481	    } elsif (/$doc_content/) {
2482		# miguel-style comment kludge, look for blank lines after
2483		# @parameter line to signify start of description
2484		if ($1 eq "") {
2485		    if ($section =~ m/^@/ || $section eq $section_context) {
2486			dump_section($file, $section, xml_escape($contents));
2487			$section = $section_default;
2488			$contents = "";
2489		    } else {
2490			$contents .= "\n";
2491		    }
2492		    $in_purpose = 0;
2493		} elsif ($in_purpose == 1) {
2494		    # Continued declaration purpose
2495		    chomp($declaration_purpose);
2496		    $declaration_purpose .= " " . xml_escape($1);
2497		    $declaration_purpose =~ s/\s+/ /g;
2498		} else {
2499		    $contents .= $1 . "\n";
2500		}
2501	    } else {
2502		# i dont know - bad line?  ignore.
2503		print STDERR "Warning(${file}:$.): bad line: $_";
2504		++$warnings;
2505	    }
2506	} elsif ($state == 3) {	# scanning for function '{' (end of prototype)
2507	    if ($decl_type eq 'function') {
2508		process_state3_function($_, $file);
2509	    } else {
2510		process_state3_type($_, $file);
2511	    }
2512	} elsif ($state == 4) {
2513		# Documentation block
2514		if (/$doc_block/) {
2515			dump_doc_section($file, $section, xml_escape($contents));
2516			$contents = "";
2517			$function = "";
2518			%constants = ();
2519			%parameterdescs = ();
2520			%parametertypes = ();
2521			@parameterlist = ();
2522			%sections = ();
2523			@sectionlist = ();
2524			$prototype = "";
2525			if ( $1 eq "" ) {
2526				$section = $section_intro;
2527			} else {
2528				$section = $1;
2529			}
2530		}
2531		elsif (/$doc_end/)
2532		{
2533			dump_doc_section($file, $section, xml_escape($contents));
2534			$contents = "";
2535			$function = "";
2536			%constants = ();
2537			%parameterdescs = ();
2538			%parametertypes = ();
2539			@parameterlist = ();
2540			%sections = ();
2541			@sectionlist = ();
2542			$prototype = "";
2543			$state = 0;
2544		}
2545		elsif (/$doc_content/)
2546		{
2547			if ( $1 eq "" )
2548			{
2549				$contents .= $blankline;
2550			}
2551			else
2552			{
2553				$contents .= $1 . "\n";
2554			}
2555		}
2556	}
2557    }
2558    if ($initial_section_counter == $section_counter) {
2559	print STDERR "Warning(${file}): no structured comments found\n";
2560	if (($function_only == 1) && ($show_not_found == 1)) {
2561	    print STDERR "    Was looking for '$_'.\n" for keys %function_table;
2562	}
2563	if ($output_mode eq "xml") {
2564	    # The template wants at least one RefEntry here; make one.
2565	    print "<refentry>\n";
2566	    print " <refnamediv>\n";
2567	    print "  <refname>\n";
2568	    print "   ${file}\n";
2569	    print "  </refname>\n";
2570	    print "  <refpurpose>\n";
2571	    print "   Document generation inconsistency\n";
2572	    print "  </refpurpose>\n";
2573	    print " </refnamediv>\n";
2574	    print " <refsect1>\n";
2575	    print "  <title>\n";
2576	    print "   Oops\n";
2577	    print "  </title>\n";
2578	    print "  <warning>\n";
2579	    print "   <para>\n";
2580	    print "    The template for this document tried to insert\n";
2581	    print "    the structured comment from the file\n";
2582	    print "    <filename>${file}</filename> at this point,\n";
2583	    print "    but none was found.\n";
2584	    print "    This dummy section is inserted to allow\n";
2585	    print "    generation to continue.\n";
2586	    print "   </para>\n";
2587	    print "  </warning>\n";
2588	    print " </refsect1>\n";
2589	    print "</refentry>\n";
2590	}
2591    }
2592}
2593
2594
2595$kernelversion = get_kernel_version();
2596
2597# generate a sequence of code that will splice in highlighting information
2598# using the s// operator.
2599foreach my $pattern (sort keys %highlights) {
2600#   print STDERR "scanning pattern:$pattern, highlight:($highlights{$pattern})\n";
2601    $dohighlight .=  "\$contents =~ s:$pattern:$highlights{$pattern}:gs;\n";
2602}
2603
2604# Read the file that maps relative names to absolute names for
2605# separate source and object directories and for shadow trees.
2606if (open(SOURCE_MAP, "<.tmp_filelist.txt")) {
2607	my ($relname, $absname);
2608	while(<SOURCE_MAP>) {
2609		chop();
2610		($relname, $absname) = (split())[0..1];
2611		$relname =~ s:^/+::;
2612		$source_map{$relname} = $absname;
2613	}
2614	close(SOURCE_MAP);
2615}
2616
2617foreach (@ARGV) {
2618    chomp;
2619    process_file($_);
2620}
2621if ($verbose && $errors) {
2622  print STDERR "$errors errors\n";
2623}
2624if ($verbose && $warnings) {
2625  print STDERR "$warnings warnings\n";
2626}
2627
2628exit($errors);
2629