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