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