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