xref: /linux/scripts/kernel-doc (revision cc794812eba917f271c6a09ac4cb1750497e404c)
1#!/usr/bin/env perl
2
3use warnings;
4use strict;
5
6## Copyright (c) 1998 Michael Zucchi, All Rights Reserved        ##
7## Copyright (C) 2000, 1  Tim Waugh <twaugh@redhat.com>          ##
8## Copyright (C) 2001  Simon Huggins                             ##
9## Copyright (C) 2005-2012  Randy Dunlap                         ##
10## Copyright (C) 2012  Dan Luedtke                               ##
11## 								 ##
12## #define enhancements by Armin Kuster <akuster@mvista.com>	 ##
13## Copyright (c) 2000 MontaVista Software, Inc.			 ##
14## 								 ##
15## This software falls under the GNU General Public License.     ##
16## Please read the COPYING file for more information             ##
17
18# 18/01/2001 - 	Cleanups
19# 		Functions prototyped as foo(void) same as foo()
20# 		Stop eval'ing where we don't need to.
21# -- huggie@earth.li
22
23# 27/06/2001 -  Allowed whitespace after initial "/**" and
24#               allowed comments before function declarations.
25# -- Christian Kreibich <ck@whoop.org>
26
27# Still to do:
28# 	- add perldoc documentation
29# 	- Look more closely at some of the scarier bits :)
30
31# 26/05/2001 - 	Support for separate source and object trees.
32#		Return error code.
33# 		Keith Owens <kaos@ocs.com.au>
34
35# 23/09/2001 - Added support for typedefs, structs, enums and unions
36#              Support for Context section; can be terminated using empty line
37#              Small fixes (like spaces vs. \s in regex)
38# -- Tim Jansen <tim@tjansen.de>
39
40# 25/07/2012 - Added support for HTML5
41# -- Dan Luedtke <mail@danrl.de>
42
43sub usage {
44    my $message = <<"EOF";
45Usage: $0 [OPTION ...] FILE ...
46
47Read C language source or header FILEs, extract embedded documentation comments,
48and print formatted documentation to standard output.
49
50The documentation comments are identified by "/**" opening comment mark. See
51Documentation/doc-guide/kernel-doc.rst for the documentation comment syntax.
52
53Output format selection (mutually exclusive):
54  -man			Output troff manual page format. This is the default.
55  -rst			Output reStructuredText format.
56  -none			Do not output documentation, only warnings.
57
58Output selection (mutually exclusive):
59  -export		Only output documentation for symbols that have been
60			exported using EXPORT_SYMBOL() or EXPORT_SYMBOL_GPL()
61                        in any input FILE or -export-file FILE.
62  -internal		Only output documentation for symbols that have NOT been
63			exported using EXPORT_SYMBOL() or EXPORT_SYMBOL_GPL()
64                        in any input FILE or -export-file FILE.
65  -function NAME	Only output documentation for the given function(s)
66			or DOC: section title(s). All other functions and DOC:
67			sections are ignored. May be specified multiple times.
68  -nofunction NAME	Do NOT output documentation for the given function(s);
69			only output documentation for the other functions and
70			DOC: sections. May be specified multiple times.
71
72Output selection modifiers:
73  -no-doc-sections	Do not output DOC: sections.
74  -enable-lineno        Enable output of #define LINENO lines. Only works with
75                        reStructuredText format.
76  -export-file FILE     Specify an additional FILE in which to look for
77                        EXPORT_SYMBOL() and EXPORT_SYMBOL_GPL(). To be used with
78                        -export or -internal. May be specified multiple times.
79
80Other parameters:
81  -v			Verbose output, more warnings and other information.
82  -h			Print this help.
83
84EOF
85    print $message;
86    exit 1;
87}
88
89#
90# format of comments.
91# In the following table, (...)? signifies optional structure.
92#                         (...)* signifies 0 or more structure elements
93# /**
94#  * function_name(:)? (- short description)?
95# (* @parameterx: (description of parameter x)?)*
96# (* a blank line)?
97#  * (Description:)? (Description of function)?
98#  * (section header: (section description)? )*
99#  (*)?*/
100#
101# So .. the trivial example would be:
102#
103# /**
104#  * my_function
105#  */
106#
107# If the Description: header tag is omitted, then there must be a blank line
108# after the last parameter specification.
109# e.g.
110# /**
111#  * my_function - does my stuff
112#  * @my_arg: its mine damnit
113#  *
114#  * Does my stuff explained.
115#  */
116#
117#  or, could also use:
118# /**
119#  * my_function - does my stuff
120#  * @my_arg: its mine damnit
121#  * Description: Does my stuff explained.
122#  */
123# etc.
124#
125# Besides functions you can also write documentation for structs, unions,
126# enums and typedefs. Instead of the function name you must write the name
127# of the declaration;  the struct/union/enum/typedef must always precede
128# the name. Nesting of declarations is not supported.
129# Use the argument mechanism to document members or constants.
130# e.g.
131# /**
132#  * struct my_struct - short description
133#  * @a: first member
134#  * @b: second member
135#  *
136#  * Longer description
137#  */
138# struct my_struct {
139#     int a;
140#     int b;
141# /* private: */
142#     int c;
143# };
144#
145# All descriptions can be multiline, except the short function description.
146#
147# For really longs structs, you can also describe arguments inside the
148# body of the struct.
149# eg.
150# /**
151#  * struct my_struct - short description
152#  * @a: first member
153#  * @b: second member
154#  *
155#  * Longer description
156#  */
157# struct my_struct {
158#     int a;
159#     int b;
160#     /**
161#      * @c: This is longer description of C
162#      *
163#      * You can use paragraphs to describe arguments
164#      * using this method.
165#      */
166#     int c;
167# };
168#
169# This should be use only for struct/enum members.
170#
171# You can also add additional sections. When documenting kernel functions you
172# should document the "Context:" of the function, e.g. whether the functions
173# can be called form interrupts. Unlike other sections you can end it with an
174# empty line.
175# A non-void function should have a "Return:" section describing the return
176# value(s).
177# Example-sections should contain the string EXAMPLE so that they are marked
178# appropriately in DocBook.
179#
180# Example:
181# /**
182#  * user_function - function that can only be called in user context
183#  * @a: some argument
184#  * Context: !in_interrupt()
185#  *
186#  * Some description
187#  * Example:
188#  *    user_function(22);
189#  */
190# ...
191#
192#
193# All descriptive text is further processed, scanning for the following special
194# patterns, which are highlighted appropriately.
195#
196# 'funcname()' - function
197# '$ENVVAR' - environmental variable
198# '&struct_name' - name of a structure (up to two words including 'struct')
199# '&struct_name.member' - name of a structure member
200# '@parameter' - name of a parameter
201# '%CONST' - name of a constant.
202# '``LITERAL``' - literal string without any spaces on it.
203
204## init lots of data
205
206my $errors = 0;
207my $warnings = 0;
208my $anon_struct_union = 0;
209
210# match expressions used to find embedded type information
211my $type_constant = '\b``([^\`]+)``\b';
212my $type_constant2 = '\%([-_\w]+)';
213my $type_func = '(\w+)\(\)';
214my $type_param = '\@(\w*(\.\w+)*(\.\.\.)?)';
215my $type_fp_param = '\@(\w+)\(\)';  # Special RST handling for func ptr params
216my $type_env = '(\$\w+)';
217my $type_enum = '\&(enum\s*([_\w]+))';
218my $type_struct = '\&(struct\s*([_\w]+))';
219my $type_typedef = '\&(typedef\s*([_\w]+))';
220my $type_union = '\&(union\s*([_\w]+))';
221my $type_member = '\&([_\w]+)(\.|->)([_\w]+)';
222my $type_fallback = '\&([_\w]+)';
223my $type_member_func = $type_member . '\(\)';
224
225# Output conversion substitutions.
226#  One for each output format
227
228# these are pretty rough
229my @highlights_man = (
230                      [$type_constant, "\$1"],
231                      [$type_constant2, "\$1"],
232                      [$type_func, "\\\\fB\$1\\\\fP"],
233                      [$type_enum, "\\\\fI\$1\\\\fP"],
234                      [$type_struct, "\\\\fI\$1\\\\fP"],
235                      [$type_typedef, "\\\\fI\$1\\\\fP"],
236                      [$type_union, "\\\\fI\$1\\\\fP"],
237                      [$type_param, "\\\\fI\$1\\\\fP"],
238                      [$type_member, "\\\\fI\$1\$2\$3\\\\fP"],
239                      [$type_fallback, "\\\\fI\$1\\\\fP"]
240		     );
241my $blankline_man = "";
242
243# rst-mode
244my @highlights_rst = (
245                       [$type_constant, "``\$1``"],
246                       [$type_constant2, "``\$1``"],
247                       # Note: need to escape () to avoid func matching later
248                       [$type_member_func, "\\:c\\:type\\:`\$1\$2\$3\\\\(\\\\) <\$1>`"],
249                       [$type_member, "\\:c\\:type\\:`\$1\$2\$3 <\$1>`"],
250		       [$type_fp_param, "**\$1\\\\(\\\\)**"],
251                       [$type_func, "\\:c\\:func\\:`\$1()`"],
252                       [$type_enum, "\\:c\\:type\\:`\$1 <\$2>`"],
253                       [$type_struct, "\\:c\\:type\\:`\$1 <\$2>`"],
254                       [$type_typedef, "\\:c\\:type\\:`\$1 <\$2>`"],
255                       [$type_union, "\\:c\\:type\\:`\$1 <\$2>`"],
256                       # in rst this can refer to any type
257                       [$type_fallback, "\\:c\\:type\\:`\$1`"],
258                       [$type_param, "**\$1**"]
259		      );
260my $blankline_rst = "\n";
261
262# read arguments
263if ($#ARGV == -1) {
264    usage();
265}
266
267my $kernelversion;
268my $dohighlight = "";
269
270my $verbose = 0;
271my $output_mode = "rst";
272my $output_preformatted = 0;
273my $no_doc_sections = 0;
274my $enable_lineno = 0;
275my @highlights = @highlights_rst;
276my $blankline = $blankline_rst;
277my $modulename = "Kernel API";
278
279use constant {
280    OUTPUT_ALL          => 0, # output all symbols and doc sections
281    OUTPUT_INCLUDE      => 1, # output only specified symbols
282    OUTPUT_EXCLUDE      => 2, # output everything except specified symbols
283    OUTPUT_EXPORTED     => 3, # output exported symbols
284    OUTPUT_INTERNAL     => 4, # output non-exported symbols
285};
286my $output_selection = OUTPUT_ALL;
287my $show_not_found = 0;
288
289my @export_file_list;
290
291my @build_time;
292if (defined($ENV{'KBUILD_BUILD_TIMESTAMP'}) &&
293    (my $seconds = `date -d"${ENV{'KBUILD_BUILD_TIMESTAMP'}}" +%s`) ne '') {
294    @build_time = gmtime($seconds);
295} else {
296    @build_time = localtime;
297}
298
299my $man_date = ('January', 'February', 'March', 'April', 'May', 'June',
300		'July', 'August', 'September', 'October',
301		'November', 'December')[$build_time[4]] .
302  " " . ($build_time[5]+1900);
303
304# Essentially these are globals.
305# They probably want to be tidied up, made more localised or something.
306# CAVEAT EMPTOR!  Some of the others I localised may not want to be, which
307# could cause "use of undefined value" or other bugs.
308my ($function, %function_table, %parametertypes, $declaration_purpose);
309my $declaration_start_line;
310my ($type, $declaration_name, $return_type);
311my ($newsection, $newcontents, $prototype, $brcount, %source_map);
312
313if (defined($ENV{'KBUILD_VERBOSE'})) {
314	$verbose = "$ENV{'KBUILD_VERBOSE'}";
315}
316
317# Generated docbook code is inserted in a template at a point where
318# docbook v3.1 requires a non-zero sequence of RefEntry's; see:
319# http://www.oasis-open.org/docbook/documentation/reference/html/refentry.html
320# We keep track of number of generated entries and generate a dummy
321# if needs be to ensure the expanded template can be postprocessed
322# into html.
323my $section_counter = 0;
324
325my $lineprefix="";
326
327# Parser states
328use constant {
329    STATE_NORMAL        => 0, # normal code
330    STATE_NAME          => 1, # looking for function name
331    STATE_BODY_MAYBE    => 2, # body - or maybe more description
332    STATE_BODY          => 3, # the body of the comment
333    STATE_PROTO         => 4, # scanning prototype
334    STATE_DOCBLOCK      => 5, # documentation block
335    STATE_INLINE        => 6, # gathering documentation outside main block
336};
337my $state;
338my $in_doc_sect;
339my $leading_space;
340
341# Inline documentation state
342use constant {
343    STATE_INLINE_NA     => 0, # not applicable ($state != STATE_INLINE)
344    STATE_INLINE_NAME   => 1, # looking for member name (@foo:)
345    STATE_INLINE_TEXT   => 2, # looking for member documentation
346    STATE_INLINE_END    => 3, # done
347    STATE_INLINE_ERROR  => 4, # error - Comment without header was found.
348                              # Spit a warning as it's not
349                              # proper kernel-doc and ignore the rest.
350};
351my $inline_doc_state;
352
353#declaration types: can be
354# 'function', 'struct', 'union', 'enum', 'typedef'
355my $decl_type;
356
357my $doc_start = '^/\*\*\s*$'; # Allow whitespace at end of comment start.
358my $doc_end = '\*/';
359my $doc_com = '\s*\*\s*';
360my $doc_com_body = '\s*\* ?';
361my $doc_decl = $doc_com . '(\w+)';
362# @params and a strictly limited set of supported section names
363my $doc_sect = $doc_com .
364    '\s*(\@[.\w]+|\@\.\.\.|description|context|returns?|notes?|examples?)\s*:(.*)';
365my $doc_content = $doc_com_body . '(.*)';
366my $doc_block = $doc_com . 'DOC:\s*(.*)?';
367my $doc_inline_start = '^\s*/\*\*\s*$';
368my $doc_inline_sect = '\s*\*\s*(@[\w\s]+):(.*)';
369my $doc_inline_end = '^\s*\*/\s*$';
370my $doc_inline_oneline = '^\s*/\*\*\s*(@[\w\s]+):\s*(.*)\s*\*/\s*$';
371my $export_symbol = '^\s*EXPORT_SYMBOL(_GPL)?\s*\(\s*(\w+)\s*\)\s*;';
372
373my %parameterdescs;
374my %parameterdesc_start_lines;
375my @parameterlist;
376my %sections;
377my @sectionlist;
378my %section_start_lines;
379my $sectcheck;
380my $struct_actual;
381
382my $contents = "";
383my $new_start_line = 0;
384
385# the canonical section names. see also $doc_sect above.
386my $section_default = "Description";	# default section
387my $section_intro = "Introduction";
388my $section = $section_default;
389my $section_context = "Context";
390my $section_return = "Return";
391
392my $undescribed = "-- undescribed --";
393
394reset_state();
395
396while ($ARGV[0] =~ m/^--?(.*)/) {
397    my $cmd = $1;
398    shift @ARGV;
399    if ($cmd eq "man") {
400	$output_mode = "man";
401	@highlights = @highlights_man;
402	$blankline = $blankline_man;
403    } elsif ($cmd eq "rst") {
404	$output_mode = "rst";
405	@highlights = @highlights_rst;
406	$blankline = $blankline_rst;
407    } elsif ($cmd eq "none") {
408	$output_mode = "none";
409    } elsif ($cmd eq "module") { # not needed for XML, inherits from calling document
410	$modulename = shift @ARGV;
411    } elsif ($cmd eq "function") { # to only output specific functions
412	$output_selection = OUTPUT_INCLUDE;
413	$function = shift @ARGV;
414	$function_table{$function} = 1;
415    } elsif ($cmd eq "nofunction") { # output all except specific functions
416	$output_selection = OUTPUT_EXCLUDE;
417	$function = shift @ARGV;
418	$function_table{$function} = 1;
419    } elsif ($cmd eq "export") { # only exported symbols
420	$output_selection = OUTPUT_EXPORTED;
421	%function_table = ();
422    } elsif ($cmd eq "internal") { # only non-exported symbols
423	$output_selection = OUTPUT_INTERNAL;
424	%function_table = ();
425    } elsif ($cmd eq "export-file") {
426	my $file = shift @ARGV;
427	push(@export_file_list, $file);
428    } elsif ($cmd eq "v") {
429	$verbose = 1;
430    } elsif (($cmd eq "h") || ($cmd eq "help")) {
431	usage();
432    } elsif ($cmd eq 'no-doc-sections') {
433	    $no_doc_sections = 1;
434    } elsif ($cmd eq 'enable-lineno') {
435	    $enable_lineno = 1;
436    } elsif ($cmd eq 'show-not-found') {
437	$show_not_found = 1;
438    } else {
439	# Unknown argument
440        usage();
441    }
442}
443
444# continue execution near EOF;
445
446# get kernel version from env
447sub get_kernel_version() {
448    my $version = 'unknown kernel version';
449
450    if (defined($ENV{'KERNELVERSION'})) {
451	$version = $ENV{'KERNELVERSION'};
452    }
453    return $version;
454}
455
456#
457sub print_lineno {
458    my $lineno = shift;
459    if ($enable_lineno && defined($lineno)) {
460        print "#define LINENO " . $lineno . "\n";
461    }
462}
463##
464# dumps section contents to arrays/hashes intended for that purpose.
465#
466sub dump_section {
467    my $file = shift;
468    my $name = shift;
469    my $contents = join "\n", @_;
470
471    if ($name =~ m/$type_param/) {
472	$name = $1;
473	$parameterdescs{$name} = $contents;
474	$sectcheck = $sectcheck . $name . " ";
475        $parameterdesc_start_lines{$name} = $new_start_line;
476        $new_start_line = 0;
477    } elsif ($name eq "@\.\.\.") {
478	$name = "...";
479	$parameterdescs{$name} = $contents;
480	$sectcheck = $sectcheck . $name . " ";
481        $parameterdesc_start_lines{$name} = $new_start_line;
482        $new_start_line = 0;
483    } else {
484	if (defined($sections{$name}) && ($sections{$name} ne "")) {
485	    # Only warn on user specified duplicate section names.
486	    if ($name ne $section_default) {
487		print STDERR "${file}:$.: warning: duplicate section name '$name'\n";
488		++$warnings;
489	    }
490	    $sections{$name} .= $contents;
491	} else {
492	    $sections{$name} = $contents;
493	    push @sectionlist, $name;
494            $section_start_lines{$name} = $new_start_line;
495            $new_start_line = 0;
496	}
497    }
498}
499
500##
501# dump DOC: section after checking that it should go out
502#
503sub dump_doc_section {
504    my $file = shift;
505    my $name = shift;
506    my $contents = join "\n", @_;
507
508    if ($no_doc_sections) {
509        return;
510    }
511
512    if (($output_selection == OUTPUT_ALL) ||
513	($output_selection == OUTPUT_INCLUDE &&
514	 defined($function_table{$name})) ||
515	($output_selection == OUTPUT_EXCLUDE &&
516	 !defined($function_table{$name})))
517    {
518	dump_section($file, $name, $contents);
519	output_blockhead({'sectionlist' => \@sectionlist,
520			  'sections' => \%sections,
521			  'module' => $modulename,
522			  'content-only' => ($output_selection != OUTPUT_ALL), });
523    }
524}
525
526##
527# output function
528#
529# parameterdescs, a hash.
530#  function => "function name"
531#  parameterlist => @list of parameters
532#  parameterdescs => %parameter descriptions
533#  sectionlist => @list of sections
534#  sections => %section descriptions
535#
536
537sub output_highlight {
538    my $contents = join "\n",@_;
539    my $line;
540
541#   DEBUG
542#   if (!defined $contents) {
543#	use Carp;
544#	confess "output_highlight got called with no args?\n";
545#   }
546
547#   print STDERR "contents b4:$contents\n";
548    eval $dohighlight;
549    die $@ if $@;
550#   print STDERR "contents af:$contents\n";
551
552    foreach $line (split "\n", $contents) {
553	if (! $output_preformatted) {
554	    $line =~ s/^\s*//;
555	}
556	if ($line eq ""){
557	    if (! $output_preformatted) {
558		print $lineprefix, $blankline;
559	    }
560	} else {
561	    if ($output_mode eq "man" && substr($line, 0, 1) eq ".") {
562		print "\\&$line";
563	    } else {
564		print $lineprefix, $line;
565	    }
566	}
567	print "\n";
568    }
569}
570
571##
572# output function in man
573sub output_function_man(%) {
574    my %args = %{$_[0]};
575    my ($parameter, $section);
576    my $count;
577
578    print ".TH \"$args{'function'}\" 9 \"$args{'function'}\" \"$man_date\" \"Kernel Hacker's Manual\" LINUX\n";
579
580    print ".SH NAME\n";
581    print $args{'function'} . " \\- " . $args{'purpose'} . "\n";
582
583    print ".SH SYNOPSIS\n";
584    if ($args{'functiontype'} ne "") {
585	print ".B \"" . $args{'functiontype'} . "\" " . $args{'function'} . "\n";
586    } else {
587	print ".B \"" . $args{'function'} . "\n";
588    }
589    $count = 0;
590    my $parenth = "(";
591    my $post = ",";
592    foreach my $parameter (@{$args{'parameterlist'}}) {
593	if ($count == $#{$args{'parameterlist'}}) {
594	    $post = ");";
595	}
596	$type = $args{'parametertypes'}{$parameter};
597	if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
598	    # pointer-to-function
599	    print ".BI \"" . $parenth . $1 . "\" " . $parameter . " \") (" . $2 . ")" . $post . "\"\n";
600	} else {
601	    $type =~ s/([^\*])$/$1 /;
602	    print ".BI \"" . $parenth . $type . "\" " . $parameter . " \"" . $post . "\"\n";
603	}
604	$count++;
605	$parenth = "";
606    }
607
608    print ".SH ARGUMENTS\n";
609    foreach $parameter (@{$args{'parameterlist'}}) {
610	my $parameter_name = $parameter;
611	$parameter_name =~ s/\[.*//;
612
613	print ".IP \"" . $parameter . "\" 12\n";
614	output_highlight($args{'parameterdescs'}{$parameter_name});
615    }
616    foreach $section (@{$args{'sectionlist'}}) {
617	print ".SH \"", uc $section, "\"\n";
618	output_highlight($args{'sections'}{$section});
619    }
620}
621
622##
623# output enum in man
624sub output_enum_man(%) {
625    my %args = %{$_[0]};
626    my ($parameter, $section);
627    my $count;
628
629    print ".TH \"$args{'module'}\" 9 \"enum $args{'enum'}\" \"$man_date\" \"API Manual\" LINUX\n";
630
631    print ".SH NAME\n";
632    print "enum " . $args{'enum'} . " \\- " . $args{'purpose'} . "\n";
633
634    print ".SH SYNOPSIS\n";
635    print "enum " . $args{'enum'} . " {\n";
636    $count = 0;
637    foreach my $parameter (@{$args{'parameterlist'}}) {
638	print ".br\n.BI \"    $parameter\"\n";
639	if ($count == $#{$args{'parameterlist'}}) {
640	    print "\n};\n";
641	    last;
642	}
643	else {
644	    print ", \n.br\n";
645	}
646	$count++;
647    }
648
649    print ".SH Constants\n";
650    foreach $parameter (@{$args{'parameterlist'}}) {
651	my $parameter_name = $parameter;
652	$parameter_name =~ s/\[.*//;
653
654	print ".IP \"" . $parameter . "\" 12\n";
655	output_highlight($args{'parameterdescs'}{$parameter_name});
656    }
657    foreach $section (@{$args{'sectionlist'}}) {
658	print ".SH \"$section\"\n";
659	output_highlight($args{'sections'}{$section});
660    }
661}
662
663##
664# output struct in man
665sub output_struct_man(%) {
666    my %args = %{$_[0]};
667    my ($parameter, $section);
668
669    print ".TH \"$args{'module'}\" 9 \"" . $args{'type'} . " " . $args{'struct'} . "\" \"$man_date\" \"API Manual\" LINUX\n";
670
671    print ".SH NAME\n";
672    print $args{'type'} . " " . $args{'struct'} . " \\- " . $args{'purpose'} . "\n";
673
674    my $declaration = $args{'definition'};
675    $declaration =~ s/\t/  /g;
676    $declaration =~ s/\n/"\n.br\n.BI \"/g;
677    print ".SH SYNOPSIS\n";
678    print $args{'type'} . " " . $args{'struct'} . " {\n.br\n";
679    print ".BI \"$declaration\n};\n.br\n\n";
680
681    print ".SH Members\n";
682    foreach $parameter (@{$args{'parameterlist'}}) {
683	($parameter =~ /^#/) && next;
684
685	my $parameter_name = $parameter;
686	$parameter_name =~ s/\[.*//;
687
688	($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
689	print ".IP \"" . $parameter . "\" 12\n";
690	output_highlight($args{'parameterdescs'}{$parameter_name});
691    }
692    foreach $section (@{$args{'sectionlist'}}) {
693	print ".SH \"$section\"\n";
694	output_highlight($args{'sections'}{$section});
695    }
696}
697
698##
699# output typedef in man
700sub output_typedef_man(%) {
701    my %args = %{$_[0]};
702    my ($parameter, $section);
703
704    print ".TH \"$args{'module'}\" 9 \"$args{'typedef'}\" \"$man_date\" \"API Manual\" LINUX\n";
705
706    print ".SH NAME\n";
707    print "typedef " . $args{'typedef'} . " \\- " . $args{'purpose'} . "\n";
708
709    foreach $section (@{$args{'sectionlist'}}) {
710	print ".SH \"$section\"\n";
711	output_highlight($args{'sections'}{$section});
712    }
713}
714
715sub output_blockhead_man(%) {
716    my %args = %{$_[0]};
717    my ($parameter, $section);
718    my $count;
719
720    print ".TH \"$args{'module'}\" 9 \"$args{'module'}\" \"$man_date\" \"API Manual\" LINUX\n";
721
722    foreach $section (@{$args{'sectionlist'}}) {
723	print ".SH \"$section\"\n";
724	output_highlight($args{'sections'}{$section});
725    }
726}
727
728##
729# output in restructured text
730#
731
732#
733# This could use some work; it's used to output the DOC: sections, and
734# starts by putting out the name of the doc section itself, but that tends
735# to duplicate a header already in the template file.
736#
737sub output_blockhead_rst(%) {
738    my %args = %{$_[0]};
739    my ($parameter, $section);
740
741    foreach $section (@{$args{'sectionlist'}}) {
742	if ($output_selection != OUTPUT_INCLUDE) {
743	    print "**$section**\n\n";
744	}
745        print_lineno($section_start_lines{$section});
746	output_highlight_rst($args{'sections'}{$section});
747	print "\n";
748    }
749}
750
751sub output_highlight_rst {
752    my $contents = join "\n",@_;
753    my $line;
754
755    eval $dohighlight;
756    die $@ if $@;
757
758    foreach $line (split "\n", $contents) {
759	print $lineprefix . $line . "\n";
760    }
761}
762
763sub output_function_rst(%) {
764    my %args = %{$_[0]};
765    my ($parameter, $section);
766    my $oldprefix = $lineprefix;
767    my $start = "";
768
769    if ($args{'typedef'}) {
770	print ".. c:type:: ". $args{'function'} . "\n\n";
771	print_lineno($declaration_start_line);
772	print "   **Typedef**: ";
773	$lineprefix = "";
774	output_highlight_rst($args{'purpose'});
775	$start = "\n\n**Syntax**\n\n  ``";
776    } else {
777	print ".. c:function:: ";
778    }
779    if ($args{'functiontype'} ne "") {
780	$start .= $args{'functiontype'} . " " . $args{'function'} . " (";
781    } else {
782	$start .= $args{'function'} . " (";
783    }
784    print $start;
785
786    my $count = 0;
787    foreach my $parameter (@{$args{'parameterlist'}}) {
788	if ($count ne 0) {
789	    print ", ";
790	}
791	$count++;
792	$type = $args{'parametertypes'}{$parameter};
793
794	if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
795	    # pointer-to-function
796	    print $1 . $parameter . ") (" . $2;
797	} else {
798	    print $type . " " . $parameter;
799	}
800    }
801    if ($args{'typedef'}) {
802	print ");``\n\n";
803    } else {
804	print ")\n\n";
805	print_lineno($declaration_start_line);
806	$lineprefix = "   ";
807	output_highlight_rst($args{'purpose'});
808	print "\n";
809    }
810
811    print "**Parameters**\n\n";
812    $lineprefix = "  ";
813    foreach $parameter (@{$args{'parameterlist'}}) {
814	my $parameter_name = $parameter;
815	$parameter_name =~ s/\[.*//;
816	$type = $args{'parametertypes'}{$parameter};
817
818	if ($type ne "") {
819	    print "``$type $parameter``\n";
820	} else {
821	    print "``$parameter``\n";
822	}
823
824        print_lineno($parameterdesc_start_lines{$parameter_name});
825
826	if (defined($args{'parameterdescs'}{$parameter_name}) &&
827	    $args{'parameterdescs'}{$parameter_name} ne $undescribed) {
828	    output_highlight_rst($args{'parameterdescs'}{$parameter_name});
829	} else {
830	    print "  *undescribed*\n";
831	}
832	print "\n";
833    }
834
835    $lineprefix = $oldprefix;
836    output_section_rst(@_);
837}
838
839sub output_section_rst(%) {
840    my %args = %{$_[0]};
841    my $section;
842    my $oldprefix = $lineprefix;
843    $lineprefix = "";
844
845    foreach $section (@{$args{'sectionlist'}}) {
846	print "**$section**\n\n";
847        print_lineno($section_start_lines{$section});
848	output_highlight_rst($args{'sections'}{$section});
849	print "\n";
850    }
851    print "\n";
852    $lineprefix = $oldprefix;
853}
854
855sub output_enum_rst(%) {
856    my %args = %{$_[0]};
857    my ($parameter);
858    my $oldprefix = $lineprefix;
859    my $count;
860    my $name = "enum " . $args{'enum'};
861
862    print "\n\n.. c:type:: " . $name . "\n\n";
863    print_lineno($declaration_start_line);
864    $lineprefix = "   ";
865    output_highlight_rst($args{'purpose'});
866    print "\n";
867
868    print "**Constants**\n\n";
869    $lineprefix = "  ";
870    foreach $parameter (@{$args{'parameterlist'}}) {
871	print "``$parameter``\n";
872	if ($args{'parameterdescs'}{$parameter} ne $undescribed) {
873	    output_highlight_rst($args{'parameterdescs'}{$parameter});
874	} else {
875	    print "  *undescribed*\n";
876	}
877	print "\n";
878    }
879
880    $lineprefix = $oldprefix;
881    output_section_rst(@_);
882}
883
884sub output_typedef_rst(%) {
885    my %args = %{$_[0]};
886    my ($parameter);
887    my $oldprefix = $lineprefix;
888    my $name = "typedef " . $args{'typedef'};
889
890    print "\n\n.. c:type:: " . $name . "\n\n";
891    print_lineno($declaration_start_line);
892    $lineprefix = "   ";
893    output_highlight_rst($args{'purpose'});
894    print "\n";
895
896    $lineprefix = $oldprefix;
897    output_section_rst(@_);
898}
899
900sub output_struct_rst(%) {
901    my %args = %{$_[0]};
902    my ($parameter);
903    my $oldprefix = $lineprefix;
904    my $name = $args{'type'} . " " . $args{'struct'};
905
906    print "\n\n.. c:type:: " . $name . "\n\n";
907    print_lineno($declaration_start_line);
908    $lineprefix = "   ";
909    output_highlight_rst($args{'purpose'});
910    print "\n";
911
912    print "**Definition**\n\n";
913    print "::\n\n";
914    my $declaration = $args{'definition'};
915    $declaration =~ s/\t/  /g;
916    print "  " . $args{'type'} . " " . $args{'struct'} . " {\n$declaration  };\n\n";
917
918    print "**Members**\n\n";
919    $lineprefix = "  ";
920    foreach $parameter (@{$args{'parameterlist'}}) {
921	($parameter =~ /^#/) && next;
922
923	my $parameter_name = $parameter;
924	$parameter_name =~ s/\[.*//;
925
926	($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
927	$type = $args{'parametertypes'}{$parameter};
928        print_lineno($parameterdesc_start_lines{$parameter_name});
929	print "``" . $parameter . "``\n";
930	output_highlight_rst($args{'parameterdescs'}{$parameter_name});
931	print "\n";
932    }
933    print "\n";
934
935    $lineprefix = $oldprefix;
936    output_section_rst(@_);
937}
938
939## none mode output functions
940
941sub output_function_none(%) {
942}
943
944sub output_enum_none(%) {
945}
946
947sub output_typedef_none(%) {
948}
949
950sub output_struct_none(%) {
951}
952
953sub output_blockhead_none(%) {
954}
955
956##
957# generic output function for all types (function, struct/union, typedef, enum);
958# calls the generated, variable output_ function name based on
959# functype and output_mode
960sub output_declaration {
961    no strict 'refs';
962    my $name = shift;
963    my $functype = shift;
964    my $func = "output_${functype}_$output_mode";
965    if (($output_selection == OUTPUT_ALL) ||
966	(($output_selection == OUTPUT_INCLUDE ||
967	  $output_selection == OUTPUT_EXPORTED) &&
968	 defined($function_table{$name})) ||
969	(($output_selection == OUTPUT_EXCLUDE ||
970	  $output_selection == OUTPUT_INTERNAL) &&
971	 !($functype eq "function" && defined($function_table{$name}))))
972    {
973	&$func(@_);
974	$section_counter++;
975    }
976}
977
978##
979# generic output function - calls the right one based on current output mode.
980sub output_blockhead {
981    no strict 'refs';
982    my $func = "output_blockhead_" . $output_mode;
983    &$func(@_);
984    $section_counter++;
985}
986
987##
988# takes a declaration (struct, union, enum, typedef) and
989# invokes the right handler. NOT called for functions.
990sub dump_declaration($$) {
991    no strict 'refs';
992    my ($prototype, $file) = @_;
993    my $func = "dump_" . $decl_type;
994    &$func(@_);
995}
996
997sub dump_union($$) {
998    dump_struct(@_);
999}
1000
1001sub dump_struct($$) {
1002    my $x = shift;
1003    my $file = shift;
1004
1005    if ($x =~ /(struct|union)\s+(\w+)\s*{(.*)}/) {
1006	my $decl_type = $1;
1007	$declaration_name = $2;
1008	my $members = $3;
1009
1010	# ignore members marked private:
1011	$members =~ s/\/\*\s*private:.*?\/\*\s*public:.*?\*\///gosi;
1012	$members =~ s/\/\*\s*private:.*//gosi;
1013	# strip comments:
1014	$members =~ s/\/\*.*?\*\///gos;
1015	# strip attributes
1016	$members =~ s/__attribute__\s*\(\([a-z,_\*\s\(\)]*\)\)//i;
1017	$members =~ s/__aligned\s*\([^;]*\)//gos;
1018	$members =~ s/\s*CRYPTO_MINALIGN_ATTR//gos;
1019	# replace DECLARE_BITMAP
1020	$members =~ s/DECLARE_BITMAP\s*\(([^,)]+),\s*([^,)]+)\)/unsigned long $1\[BITS_TO_LONGS($2)\]/gos;
1021	# replace DECLARE_HASHTABLE
1022	$members =~ s/DECLARE_HASHTABLE\s*\(([^,)]+),\s*([^,)]+)\)/unsigned long $1\[1 << (($2) - 1)\]/gos;
1023	# replace DECLARE_KFIFO
1024	$members =~ s/DECLARE_KFIFO\s*\(([^,)]+),\s*([^,)]+),\s*([^,)]+)\)/$2 \*$1/gos;
1025	# replace DECLARE_KFIFO_PTR
1026	$members =~ s/DECLARE_KFIFO_PTR\s*\(([^,)]+),\s*([^,)]+)\)/$2 \*$1/gos;
1027
1028	my $declaration = $members;
1029
1030	# Split nested struct/union elements as newer ones
1031	while ($members =~ m/(struct|union)([^\{\};]+)\{([^\{\}]*)\}([^\{\}\;]*)\;/) {
1032		my $newmember;
1033		my $maintype = $1;
1034		my $ids = $4;
1035		my $content = $3;
1036		foreach my $id(split /,/, $ids) {
1037			$newmember .= "$maintype $id; ";
1038
1039			$id =~ s/[:\[].*//;
1040			$id =~ s/^\s*\**(\S+)\s*/$1/;
1041			foreach my $arg (split /;/, $content) {
1042				next if ($arg =~ m/^\s*$/);
1043				if ($arg =~ m/^([^\(]+\(\*?\s*)([\w\.]*)(\s*\).*)/) {
1044					# pointer-to-function
1045					my $type = $1;
1046					my $name = $2;
1047					my $extra = $3;
1048					next if (!$name);
1049					if ($id =~ m/^\s*$/) {
1050						# anonymous struct/union
1051						$newmember .= "$type$name$extra; ";
1052					} else {
1053						$newmember .= "$type$id.$name$extra; ";
1054					}
1055				} else {
1056					my $type;
1057					my $names;
1058					$arg =~ s/^\s+//;
1059					$arg =~ s/\s+$//;
1060					# Handle bitmaps
1061					$arg =~ s/:\s*\d+\s*//g;
1062					# Handle arrays
1063					$arg =~ s/\[\S+\]//g;
1064					# The type may have multiple words,
1065					# and multiple IDs can be defined, like:
1066					#	const struct foo, *bar, foobar
1067					# So, we remove spaces when parsing the
1068					# names, in order to match just names
1069					# and commas for the names
1070					$arg =~ s/\s*,\s*/,/g;
1071					if ($arg =~ m/(.*)\s+([\S+,]+)/) {
1072						$type = $1;
1073						$names = $2;
1074					} else {
1075						$newmember .= "$arg; ";
1076						next;
1077					}
1078					foreach my $name (split /,/, $names) {
1079						$name =~ s/^\s*\**(\S+)\s*/$1/;
1080						next if (($name =~ m/^\s*$/));
1081						if ($id =~ m/^\s*$/) {
1082							# anonymous struct/union
1083							$newmember .= "$type $name; ";
1084						} else {
1085							$newmember .= "$type $id.$name; ";
1086						}
1087					}
1088				}
1089			}
1090		}
1091		$members =~ s/(struct|union)([^\{\};]+)\{([^\{\}]*)}([^\{\}\;]*)\;/$newmember/;
1092	}
1093
1094	# Ignore other nested elements, like enums
1095	$members =~ s/({[^\{\}]*})//g;
1096
1097	create_parameterlist($members, ';', $file, $declaration_name);
1098	check_sections($file, $declaration_name, $decl_type, $sectcheck, $struct_actual);
1099
1100	# Adjust declaration for better display
1101	$declaration =~ s/([{;])/$1\n/g;
1102	$declaration =~ s/}\s+;/};/g;
1103	# Better handle inlined enums
1104	do {} while ($declaration =~ s/(enum\s+{[^}]+),([^\n])/$1,\n$2/);
1105
1106	my @def_args = split /\n/, $declaration;
1107	my $level = 1;
1108	$declaration = "";
1109	foreach my $clause (@def_args) {
1110		$clause =~ s/^\s+//;
1111		$clause =~ s/\s+$//;
1112		$clause =~ s/\s+/ /;
1113		next if (!$clause);
1114		$level-- if ($clause =~ m/(})/ && $level > 1);
1115		if (!($clause =~ m/^\s*#/)) {
1116			$declaration .= "\t" x $level;
1117		}
1118		$declaration .= "\t" . $clause . "\n";
1119		$level++ if ($clause =~ m/({)/ && !($clause =~m/}/));
1120	}
1121	output_declaration($declaration_name,
1122			   'struct',
1123			   {'struct' => $declaration_name,
1124			    'module' => $modulename,
1125			    'definition' => $declaration,
1126			    'parameterlist' => \@parameterlist,
1127			    'parameterdescs' => \%parameterdescs,
1128			    'parametertypes' => \%parametertypes,
1129			    'sectionlist' => \@sectionlist,
1130			    'sections' => \%sections,
1131			    'purpose' => $declaration_purpose,
1132			    'type' => $decl_type
1133			   });
1134    }
1135    else {
1136	print STDERR "${file}:$.: error: Cannot parse struct or union!\n";
1137	++$errors;
1138    }
1139}
1140
1141
1142sub show_warnings($$) {
1143	my $functype = shift;
1144	my $name = shift;
1145
1146	return 1 if ($output_selection == OUTPUT_ALL);
1147
1148	if ($output_selection == OUTPUT_EXPORTED) {
1149		if (defined($function_table{$name})) {
1150			return 1;
1151		} else {
1152			return 0;
1153		}
1154	}
1155        if ($output_selection == OUTPUT_INTERNAL) {
1156		if (!($functype eq "function" && defined($function_table{$name}))) {
1157			return 1;
1158		} else {
1159			return 0;
1160		}
1161	}
1162	if ($output_selection == OUTPUT_INCLUDE) {
1163		if (defined($function_table{$name})) {
1164			return 1;
1165		} else {
1166			return 0;
1167		}
1168	}
1169	if ($output_selection == OUTPUT_EXCLUDE) {
1170		if (!defined($function_table{$name})) {
1171			return 1;
1172		} else {
1173			return 0;
1174		}
1175	}
1176	die("Please add the new output type at show_warnings()");
1177}
1178
1179sub dump_enum($$) {
1180    my $x = shift;
1181    my $file = shift;
1182
1183    $x =~ s@/\*.*?\*/@@gos;	# strip comments.
1184    # strip #define macros inside enums
1185    $x =~ s@#\s*((define|ifdef)\s+|endif)[^;]*;@@gos;
1186
1187    if ($x =~ /enum\s+(\w+)\s*{(.*)}/) {
1188	$declaration_name = $1;
1189	my $members = $2;
1190	my %_members;
1191
1192	$members =~ s/\s+$//;
1193
1194	foreach my $arg (split ',', $members) {
1195	    $arg =~ s/^\s*(\w+).*/$1/;
1196	    push @parameterlist, $arg;
1197	    if (!$parameterdescs{$arg}) {
1198		$parameterdescs{$arg} = $undescribed;
1199	        if (show_warnings("enum", $declaration_name)) {
1200			print STDERR "${file}:$.: warning: Enum value '$arg' not described in enum '$declaration_name'\n";
1201		}
1202	    }
1203	    $_members{$arg} = 1;
1204	}
1205
1206	while (my ($k, $v) = each %parameterdescs) {
1207	    if (!exists($_members{$k})) {
1208	        if (show_warnings("enum", $declaration_name)) {
1209		     print STDERR "${file}:$.: warning: Excess enum value '$k' description in '$declaration_name'\n";
1210		}
1211	    }
1212        }
1213
1214	output_declaration($declaration_name,
1215			   'enum',
1216			   {'enum' => $declaration_name,
1217			    'module' => $modulename,
1218			    'parameterlist' => \@parameterlist,
1219			    'parameterdescs' => \%parameterdescs,
1220			    'sectionlist' => \@sectionlist,
1221			    'sections' => \%sections,
1222			    'purpose' => $declaration_purpose
1223			   });
1224    }
1225    else {
1226	print STDERR "${file}:$.: error: Cannot parse enum!\n";
1227	++$errors;
1228    }
1229}
1230
1231sub dump_typedef($$) {
1232    my $x = shift;
1233    my $file = shift;
1234
1235    $x =~ s@/\*.*?\*/@@gos;	# strip comments.
1236
1237    # Parse function prototypes
1238    if ($x =~ /typedef\s+(\w+)\s*\(\*\s*(\w\S+)\s*\)\s*\((.*)\);/ ||
1239	$x =~ /typedef\s+(\w+)\s*(\w\S+)\s*\s*\((.*)\);/) {
1240
1241	# Function typedefs
1242	$return_type = $1;
1243	$declaration_name = $2;
1244	my $args = $3;
1245
1246	create_parameterlist($args, ',', $file, $declaration_name);
1247
1248	output_declaration($declaration_name,
1249			   'function',
1250			   {'function' => $declaration_name,
1251			    'typedef' => 1,
1252			    'module' => $modulename,
1253			    'functiontype' => $return_type,
1254			    'parameterlist' => \@parameterlist,
1255			    'parameterdescs' => \%parameterdescs,
1256			    'parametertypes' => \%parametertypes,
1257			    'sectionlist' => \@sectionlist,
1258			    'sections' => \%sections,
1259			    'purpose' => $declaration_purpose
1260			   });
1261	return;
1262    }
1263
1264    while (($x =~ /\(*.\)\s*;$/) || ($x =~ /\[*.\]\s*;$/)) {
1265	$x =~ s/\(*.\)\s*;$/;/;
1266	$x =~ s/\[*.\]\s*;$/;/;
1267    }
1268
1269    if ($x =~ /typedef.*\s+(\w+)\s*;/) {
1270	$declaration_name = $1;
1271
1272	output_declaration($declaration_name,
1273			   'typedef',
1274			   {'typedef' => $declaration_name,
1275			    'module' => $modulename,
1276			    'sectionlist' => \@sectionlist,
1277			    'sections' => \%sections,
1278			    'purpose' => $declaration_purpose
1279			   });
1280    }
1281    else {
1282	print STDERR "${file}:$.: error: Cannot parse typedef!\n";
1283	++$errors;
1284    }
1285}
1286
1287sub save_struct_actual($) {
1288    my $actual = shift;
1289
1290    # strip all spaces from the actual param so that it looks like one string item
1291    $actual =~ s/\s*//g;
1292    $struct_actual = $struct_actual . $actual . " ";
1293}
1294
1295sub create_parameterlist($$$$) {
1296    my $args = shift;
1297    my $splitter = shift;
1298    my $file = shift;
1299    my $declaration_name = shift;
1300    my $type;
1301    my $param;
1302
1303    # temporarily replace commas inside function pointer definition
1304    while ($args =~ /(\([^\),]+),/) {
1305	$args =~ s/(\([^\),]+),/$1#/g;
1306    }
1307
1308    foreach my $arg (split($splitter, $args)) {
1309	# strip comments
1310	$arg =~ s/\/\*.*\*\///;
1311	# strip leading/trailing spaces
1312	$arg =~ s/^\s*//;
1313	$arg =~ s/\s*$//;
1314	$arg =~ s/\s+/ /;
1315
1316	if ($arg =~ /^#/) {
1317	    # Treat preprocessor directive as a typeless variable just to fill
1318	    # corresponding data structures "correctly". Catch it later in
1319	    # output_* subs.
1320	    push_parameter($arg, "", $file);
1321	} elsif ($arg =~ m/\(.+\)\s*\(/) {
1322	    # pointer-to-function
1323	    $arg =~ tr/#/,/;
1324	    $arg =~ m/[^\(]+\(\*?\s*([\w\.]*)\s*\)/;
1325	    $param = $1;
1326	    $type = $arg;
1327	    $type =~ s/([^\(]+\(\*?)\s*$param/$1/;
1328	    save_struct_actual($param);
1329	    push_parameter($param, $type, $file, $declaration_name);
1330	} elsif ($arg) {
1331	    $arg =~ s/\s*:\s*/:/g;
1332	    $arg =~ s/\s*\[/\[/g;
1333
1334	    my @args = split('\s*,\s*', $arg);
1335	    if ($args[0] =~ m/\*/) {
1336		$args[0] =~ s/(\*+)\s*/ $1/;
1337	    }
1338
1339	    my @first_arg;
1340	    if ($args[0] =~ /^(.*\s+)(.*?\[.*\].*)$/) {
1341		    shift @args;
1342		    push(@first_arg, split('\s+', $1));
1343		    push(@first_arg, $2);
1344	    } else {
1345		    @first_arg = split('\s+', shift @args);
1346	    }
1347
1348	    unshift(@args, pop @first_arg);
1349	    $type = join " ", @first_arg;
1350
1351	    foreach $param (@args) {
1352		if ($param =~ m/^(\*+)\s*(.*)/) {
1353		    save_struct_actual($2);
1354		    push_parameter($2, "$type $1", $file, $declaration_name);
1355		}
1356		elsif ($param =~ m/(.*?):(\d+)/) {
1357		    if ($type ne "") { # skip unnamed bit-fields
1358			save_struct_actual($1);
1359			push_parameter($1, "$type:$2", $file, $declaration_name)
1360		    }
1361		}
1362		else {
1363		    save_struct_actual($param);
1364		    push_parameter($param, $type, $file, $declaration_name);
1365		}
1366	    }
1367	}
1368    }
1369}
1370
1371sub push_parameter($$$$) {
1372	my $param = shift;
1373	my $type = shift;
1374	my $file = shift;
1375	my $declaration_name = shift;
1376
1377	if (($anon_struct_union == 1) && ($type eq "") &&
1378	    ($param eq "}")) {
1379		return;		# ignore the ending }; from anon. struct/union
1380	}
1381
1382	$anon_struct_union = 0;
1383	$param =~ s/[\[\)].*//;
1384
1385	if ($type eq "" && $param =~ /\.\.\.$/)
1386	{
1387	    if (!$param =~ /\w\.\.\.$/) {
1388	      # handles unnamed variable parameters
1389	      $param = "...";
1390	    }
1391	    if (!defined $parameterdescs{$param} || $parameterdescs{$param} eq "") {
1392		$parameterdescs{$param} = "variable arguments";
1393	    }
1394	}
1395	elsif ($type eq "" && ($param eq "" or $param eq "void"))
1396	{
1397	    $param="void";
1398	    $parameterdescs{void} = "no arguments";
1399	}
1400	elsif ($type eq "" && ($param eq "struct" or $param eq "union"))
1401	# handle unnamed (anonymous) union or struct:
1402	{
1403		$type = $param;
1404		$param = "{unnamed_" . $param . "}";
1405		$parameterdescs{$param} = "anonymous\n";
1406		$anon_struct_union = 1;
1407	}
1408
1409	# warn if parameter has no description
1410	# (but ignore ones starting with # as these are not parameters
1411	# but inline preprocessor statements);
1412	# Note: It will also ignore void params and unnamed structs/unions
1413	if (!defined $parameterdescs{$param} && $param !~ /^#/) {
1414		$parameterdescs{$param} = $undescribed;
1415
1416	        if (show_warnings($type, $declaration_name)) {
1417			print STDERR
1418			      "${file}:$.: warning: Function parameter or member '$param' not described in '$declaration_name'\n";
1419			++$warnings;
1420		}
1421	}
1422
1423	# strip spaces from $param so that it is one continuous string
1424	# on @parameterlist;
1425	# this fixes a problem where check_sections() cannot find
1426	# a parameter like "addr[6 + 2]" because it actually appears
1427	# as "addr[6", "+", "2]" on the parameter list;
1428	# but it's better to maintain the param string unchanged for output,
1429	# so just weaken the string compare in check_sections() to ignore
1430	# "[blah" in a parameter string;
1431	###$param =~ s/\s*//g;
1432	push @parameterlist, $param;
1433	$type =~ s/\s\s+/ /g;
1434	$parametertypes{$param} = $type;
1435}
1436
1437sub check_sections($$$$$) {
1438	my ($file, $decl_name, $decl_type, $sectcheck, $prmscheck) = @_;
1439	my @sects = split ' ', $sectcheck;
1440	my @prms = split ' ', $prmscheck;
1441	my $err;
1442	my ($px, $sx);
1443	my $prm_clean;		# strip trailing "[array size]" and/or beginning "*"
1444
1445	foreach $sx (0 .. $#sects) {
1446		$err = 1;
1447		foreach $px (0 .. $#prms) {
1448			$prm_clean = $prms[$px];
1449			$prm_clean =~ s/\[.*\]//;
1450			$prm_clean =~ s/__attribute__\s*\(\([a-z,_\*\s\(\)]*\)\)//i;
1451			# ignore array size in a parameter string;
1452			# however, the original param string may contain
1453			# spaces, e.g.:  addr[6 + 2]
1454			# and this appears in @prms as "addr[6" since the
1455			# parameter list is split at spaces;
1456			# hence just ignore "[..." for the sections check;
1457			$prm_clean =~ s/\[.*//;
1458
1459			##$prm_clean =~ s/^\**//;
1460			if ($prm_clean eq $sects[$sx]) {
1461				$err = 0;
1462				last;
1463			}
1464		}
1465		if ($err) {
1466			if ($decl_type eq "function") {
1467				print STDERR "${file}:$.: warning: " .
1468					"Excess function parameter " .
1469					"'$sects[$sx]' " .
1470					"description in '$decl_name'\n";
1471				++$warnings;
1472			}
1473		}
1474	}
1475}
1476
1477##
1478# Checks the section describing the return value of a function.
1479sub check_return_section {
1480        my $file = shift;
1481        my $declaration_name = shift;
1482        my $return_type = shift;
1483
1484        # Ignore an empty return type (It's a macro)
1485        # Ignore functions with a "void" return type. (But don't ignore "void *")
1486        if (($return_type eq "") || ($return_type =~ /void\s*\w*\s*$/)) {
1487                return;
1488        }
1489
1490        if (!defined($sections{$section_return}) ||
1491            $sections{$section_return} eq "") {
1492                print STDERR "${file}:$.: warning: " .
1493                        "No description found for return value of " .
1494                        "'$declaration_name'\n";
1495                ++$warnings;
1496        }
1497}
1498
1499##
1500# takes a function prototype and the name of the current file being
1501# processed and spits out all the details stored in the global
1502# arrays/hashes.
1503sub dump_function($$) {
1504    my $prototype = shift;
1505    my $file = shift;
1506    my $noret = 0;
1507
1508    $prototype =~ s/^static +//;
1509    $prototype =~ s/^extern +//;
1510    $prototype =~ s/^asmlinkage +//;
1511    $prototype =~ s/^inline +//;
1512    $prototype =~ s/^__inline__ +//;
1513    $prototype =~ s/^__inline +//;
1514    $prototype =~ s/^__always_inline +//;
1515    $prototype =~ s/^noinline +//;
1516    $prototype =~ s/__init +//;
1517    $prototype =~ s/__init_or_module +//;
1518    $prototype =~ s/__meminit +//;
1519    $prototype =~ s/__must_check +//;
1520    $prototype =~ s/__weak +//;
1521    my $define = $prototype =~ s/^#\s*define\s+//; #ak added
1522    $prototype =~ s/__attribute__\s*\(\(
1523            (?:
1524                 [\w\s]++          # attribute name
1525                 (?:\([^)]*+\))?   # attribute arguments
1526                 \s*+,?            # optional comma at the end
1527            )+
1528          \)\)\s+//x;
1529
1530    # Yes, this truly is vile.  We are looking for:
1531    # 1. Return type (may be nothing if we're looking at a macro)
1532    # 2. Function name
1533    # 3. Function parameters.
1534    #
1535    # All the while we have to watch out for function pointer parameters
1536    # (which IIRC is what the two sections are for), C types (these
1537    # regexps don't even start to express all the possibilities), and
1538    # so on.
1539    #
1540    # If you mess with these regexps, it's a good idea to check that
1541    # the following functions' documentation still comes out right:
1542    # - parport_register_device (function pointer parameters)
1543    # - atomic_set (macro)
1544    # - pci_match_device, __copy_to_user (long return type)
1545
1546    if ($define && $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s+/) {
1547        # This is an object-like macro, it has no return type and no parameter
1548        # list.
1549        # Function-like macros are not allowed to have spaces between
1550        # declaration_name and opening parenthesis (notice the \s+).
1551        $return_type = $1;
1552        $declaration_name = $2;
1553        $noret = 1;
1554    } elsif ($prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1555	$prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1556	$prototype =~ m/^(\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1557	$prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1558	$prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1559	$prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1560	$prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1561	$prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1562	$prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1563	$prototype =~ m/^(\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1564	$prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1565	$prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1566	$prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1567	$prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1568	$prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1569	$prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1570	$prototype =~ m/^(\w+\s+\w+\s*\*+\s*\w+\s*\*+\s*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/)  {
1571	$return_type = $1;
1572	$declaration_name = $2;
1573	my $args = $3;
1574
1575	create_parameterlist($args, ',', $file, $declaration_name);
1576    } else {
1577	print STDERR "${file}:$.: warning: cannot understand function prototype: '$prototype'\n";
1578	return;
1579    }
1580
1581	my $prms = join " ", @parameterlist;
1582	check_sections($file, $declaration_name, "function", $sectcheck, $prms);
1583
1584        # This check emits a lot of warnings at the moment, because many
1585        # functions don't have a 'Return' doc section. So until the number
1586        # of warnings goes sufficiently down, the check is only performed in
1587        # verbose mode.
1588        # TODO: always perform the check.
1589        if ($verbose && !$noret) {
1590                check_return_section($file, $declaration_name, $return_type);
1591        }
1592
1593    output_declaration($declaration_name,
1594		       'function',
1595		       {'function' => $declaration_name,
1596			'module' => $modulename,
1597			'functiontype' => $return_type,
1598			'parameterlist' => \@parameterlist,
1599			'parameterdescs' => \%parameterdescs,
1600			'parametertypes' => \%parametertypes,
1601			'sectionlist' => \@sectionlist,
1602			'sections' => \%sections,
1603			'purpose' => $declaration_purpose
1604		       });
1605}
1606
1607sub reset_state {
1608    $function = "";
1609    %parameterdescs = ();
1610    %parametertypes = ();
1611    @parameterlist = ();
1612    %sections = ();
1613    @sectionlist = ();
1614    $sectcheck = "";
1615    $struct_actual = "";
1616    $prototype = "";
1617
1618    $state = STATE_NORMAL;
1619    $inline_doc_state = STATE_INLINE_NA;
1620}
1621
1622sub tracepoint_munge($) {
1623	my $file = shift;
1624	my $tracepointname = 0;
1625	my $tracepointargs = 0;
1626
1627	if ($prototype =~ m/TRACE_EVENT\((.*?),/) {
1628		$tracepointname = $1;
1629	}
1630	if ($prototype =~ m/DEFINE_SINGLE_EVENT\((.*?),/) {
1631		$tracepointname = $1;
1632	}
1633	if ($prototype =~ m/DEFINE_EVENT\((.*?),(.*?),/) {
1634		$tracepointname = $2;
1635	}
1636	$tracepointname =~ s/^\s+//; #strip leading whitespace
1637	if ($prototype =~ m/TP_PROTO\((.*?)\)/) {
1638		$tracepointargs = $1;
1639	}
1640	if (($tracepointname eq 0) || ($tracepointargs eq 0)) {
1641		print STDERR "${file}:$.: warning: Unrecognized tracepoint format: \n".
1642			     "$prototype\n";
1643	} else {
1644		$prototype = "static inline void trace_$tracepointname($tracepointargs)";
1645	}
1646}
1647
1648sub syscall_munge() {
1649	my $void = 0;
1650
1651	$prototype =~ s@[\r\n]+@ @gos; # strip newlines/CR's
1652##	if ($prototype =~ m/SYSCALL_DEFINE0\s*\(\s*(a-zA-Z0-9_)*\s*\)/) {
1653	if ($prototype =~ m/SYSCALL_DEFINE0/) {
1654		$void = 1;
1655##		$prototype = "long sys_$1(void)";
1656	}
1657
1658	$prototype =~ s/SYSCALL_DEFINE.*\(/long sys_/; # fix return type & func name
1659	if ($prototype =~ m/long (sys_.*?),/) {
1660		$prototype =~ s/,/\(/;
1661	} elsif ($void) {
1662		$prototype =~ s/\)/\(void\)/;
1663	}
1664
1665	# now delete all of the odd-number commas in $prototype
1666	# so that arg types & arg names don't have a comma between them
1667	my $count = 0;
1668	my $len = length($prototype);
1669	if ($void) {
1670		$len = 0;	# skip the for-loop
1671	}
1672	for (my $ix = 0; $ix < $len; $ix++) {
1673		if (substr($prototype, $ix, 1) eq ',') {
1674			$count++;
1675			if ($count % 2 == 1) {
1676				substr($prototype, $ix, 1) = ' ';
1677			}
1678		}
1679	}
1680}
1681
1682sub process_proto_function($$) {
1683    my $x = shift;
1684    my $file = shift;
1685
1686    $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
1687
1688    if ($x =~ m#\s*/\*\s+MACDOC\s*#io || ($x =~ /^#/ && $x !~ /^#\s*define/)) {
1689	# do nothing
1690    }
1691    elsif ($x =~ /([^\{]*)/) {
1692	$prototype .= $1;
1693    }
1694
1695    if (($x =~ /\{/) || ($x =~ /\#\s*define/) || ($x =~ /;/)) {
1696	$prototype =~ s@/\*.*?\*/@@gos;	# strip comments.
1697	$prototype =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1698	$prototype =~ s@^\s+@@gos; # strip leading spaces
1699	if ($prototype =~ /SYSCALL_DEFINE/) {
1700		syscall_munge();
1701	}
1702	if ($prototype =~ /TRACE_EVENT/ || $prototype =~ /DEFINE_EVENT/ ||
1703	    $prototype =~ /DEFINE_SINGLE_EVENT/)
1704	{
1705		tracepoint_munge($file);
1706	}
1707	dump_function($prototype, $file);
1708	reset_state();
1709    }
1710}
1711
1712sub process_proto_type($$) {
1713    my $x = shift;
1714    my $file = shift;
1715
1716    $x =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1717    $x =~ s@^\s+@@gos; # strip leading spaces
1718    $x =~ s@\s+$@@gos; # strip trailing spaces
1719    $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
1720
1721    if ($x =~ /^#/) {
1722	# To distinguish preprocessor directive from regular declaration later.
1723	$x .= ";";
1724    }
1725
1726    while (1) {
1727	if ( $x =~ /([^{};]*)([{};])(.*)/ ) {
1728            if( length $prototype ) {
1729                $prototype .= " "
1730            }
1731	    $prototype .= $1 . $2;
1732	    ($2 eq '{') && $brcount++;
1733	    ($2 eq '}') && $brcount--;
1734	    if (($2 eq ';') && ($brcount == 0)) {
1735		dump_declaration($prototype, $file);
1736		reset_state();
1737		last;
1738	    }
1739	    $x = $3;
1740	} else {
1741	    $prototype .= $x;
1742	    last;
1743	}
1744    }
1745}
1746
1747
1748sub map_filename($) {
1749    my $file;
1750    my ($orig_file) = @_;
1751
1752    if (defined($ENV{'SRCTREE'})) {
1753	$file = "$ENV{'SRCTREE'}" . "/" . $orig_file;
1754    } else {
1755	$file = $orig_file;
1756    }
1757
1758    if (defined($source_map{$file})) {
1759	$file = $source_map{$file};
1760    }
1761
1762    return $file;
1763}
1764
1765sub process_export_file($) {
1766    my ($orig_file) = @_;
1767    my $file = map_filename($orig_file);
1768
1769    if (!open(IN,"<$file")) {
1770	print STDERR "Error: Cannot open file $file\n";
1771	++$errors;
1772	return;
1773    }
1774
1775    while (<IN>) {
1776	if (/$export_symbol/) {
1777	    $function_table{$2} = 1;
1778	}
1779    }
1780
1781    close(IN);
1782}
1783
1784#
1785# Parsers for the various processing states.
1786#
1787# STATE_NORMAL: looking for the /** to begin everything.
1788#
1789sub process_normal() {
1790    if (/$doc_start/o) {
1791	$state = STATE_NAME;	# next line is always the function name
1792	$in_doc_sect = 0;
1793	$declaration_start_line = $. + 1;
1794    }
1795}
1796
1797#
1798# STATE_NAME: Looking for the "name - description" line
1799#
1800sub process_name($$) {
1801    my $file = shift;
1802    my $identifier;
1803    my $descr;
1804
1805    if (/$doc_block/o) {
1806	$state = STATE_DOCBLOCK;
1807	$contents = "";
1808	$new_start_line = $. + 1;
1809
1810	if ( $1 eq "" ) {
1811	    $section = $section_intro;
1812	} else {
1813	    $section = $1;
1814	}
1815    }
1816    elsif (/$doc_decl/o) {
1817	$identifier = $1;
1818	if (/\s*([\w\s]+?)\s*-/) {
1819	    $identifier = $1;
1820	}
1821
1822	$state = STATE_BODY;
1823	# if there's no @param blocks need to set up default section
1824	# here
1825	$contents = "";
1826	$section = $section_default;
1827	$new_start_line = $. + 1;
1828	if (/-(.*)/) {
1829	    # strip leading/trailing/multiple spaces
1830	    $descr= $1;
1831	    $descr =~ s/^\s*//;
1832	    $descr =~ s/\s*$//;
1833	    $descr =~ s/\s+/ /g;
1834	    $declaration_purpose = $descr;
1835	    $state = STATE_BODY_MAYBE;
1836	} else {
1837	    $declaration_purpose = "";
1838	}
1839
1840	if (($declaration_purpose eq "") && $verbose) {
1841	    print STDERR "${file}:$.: warning: missing initial short description on line:\n";
1842	    print STDERR $_;
1843	    ++$warnings;
1844	}
1845
1846	if ($identifier =~ m/^struct/) {
1847	    $decl_type = 'struct';
1848	} elsif ($identifier =~ m/^union/) {
1849	    $decl_type = 'union';
1850	} elsif ($identifier =~ m/^enum/) {
1851	    $decl_type = 'enum';
1852	} elsif ($identifier =~ m/^typedef/) {
1853	    $decl_type = 'typedef';
1854	} else {
1855	    $decl_type = 'function';
1856	}
1857
1858	if ($verbose) {
1859	    print STDERR "${file}:$.: info: Scanning doc for $identifier\n";
1860	}
1861    } else {
1862	print STDERR "${file}:$.: warning: Cannot understand $_ on line $.",
1863	    " - I thought it was a doc line\n";
1864	++$warnings;
1865	$state = STATE_NORMAL;
1866    }
1867}
1868
1869
1870#
1871# STATE_BODY and STATE_BODY_MAYBE: the bulk of a kerneldoc comment.
1872#
1873sub process_body($$) {
1874    my $file = shift;
1875
1876    if (/$doc_sect/i) { # case insensitive for supported section names
1877	$newsection = $1;
1878	$newcontents = $2;
1879
1880	# map the supported section names to the canonical names
1881	if ($newsection =~ m/^description$/i) {
1882	    $newsection = $section_default;
1883	} elsif ($newsection =~ m/^context$/i) {
1884	    $newsection = $section_context;
1885	} elsif ($newsection =~ m/^returns?$/i) {
1886	    $newsection = $section_return;
1887	} elsif ($newsection =~ m/^\@return$/) {
1888	    # special: @return is a section, not a param description
1889	    $newsection = $section_return;
1890	}
1891
1892	if (($contents ne "") && ($contents ne "\n")) {
1893	    if (!$in_doc_sect && $verbose) {
1894		print STDERR "${file}:$.: warning: contents before sections\n";
1895		++$warnings;
1896	    }
1897	    dump_section($file, $section, $contents);
1898	    $section = $section_default;
1899	}
1900
1901	$in_doc_sect = 1;
1902	$state = STATE_BODY;
1903	$contents = $newcontents;
1904	$new_start_line = $.;
1905	while (substr($contents, 0, 1) eq " ") {
1906	    $contents = substr($contents, 1);
1907	}
1908	if ($contents ne "") {
1909	    $contents .= "\n";
1910	}
1911	$section = $newsection;
1912	$leading_space = undef;
1913    } elsif (/$doc_end/) {
1914	if (($contents ne "") && ($contents ne "\n")) {
1915	    dump_section($file, $section, $contents);
1916	    $section = $section_default;
1917	    $contents = "";
1918	}
1919	# look for doc_com + <text> + doc_end:
1920	if ($_ =~ m'\s*\*\s*[a-zA-Z_0-9:\.]+\*/') {
1921	    print STDERR "${file}:$.: warning: suspicious ending line: $_";
1922	    ++$warnings;
1923	}
1924
1925	$prototype = "";
1926	$state = STATE_PROTO;
1927	$brcount = 0;
1928    } elsif (/$doc_content/) {
1929	# miguel-style comment kludge, look for blank lines after
1930	# @parameter line to signify start of description
1931	if ($1 eq "") {
1932	    if ($section =~ m/^@/ || $section eq $section_context) {
1933		dump_section($file, $section, $contents);
1934		$section = $section_default;
1935		$contents = "";
1936		$new_start_line = $.;
1937	    } else {
1938		$contents .= "\n";
1939	    }
1940	    $state = STATE_BODY;
1941	} elsif ($state == STATE_BODY_MAYBE) {
1942	    # Continued declaration purpose
1943	    chomp($declaration_purpose);
1944	    $declaration_purpose .= " " . $1;
1945	    $declaration_purpose =~ s/\s+/ /g;
1946	} else {
1947	    my $cont = $1;
1948	    if ($section =~ m/^@/ || $section eq $section_context) {
1949		if (!defined $leading_space) {
1950		    if ($cont =~ m/^(\s+)/) {
1951			$leading_space = $1;
1952		    } else {
1953			$leading_space = "";
1954		    }
1955		}
1956		$cont =~ s/^$leading_space//;
1957	    }
1958	    $contents .= $cont . "\n";
1959	}
1960    } else {
1961	# i dont know - bad line?  ignore.
1962	print STDERR "${file}:$.: warning: bad line: $_";
1963	++$warnings;
1964    }
1965}
1966
1967
1968#
1969# STATE_PROTO: reading a function/whatever prototype.
1970#
1971sub process_proto($$) {
1972    my $file = shift;
1973
1974    if (/$doc_inline_oneline/) {
1975	$section = $1;
1976	$contents = $2;
1977	if ($contents ne "") {
1978	    $contents .= "\n";
1979	    dump_section($file, $section, $contents);
1980	    $section = $section_default;
1981	    $contents = "";
1982	}
1983    } elsif (/$doc_inline_start/) {
1984	$state = STATE_INLINE;
1985	$inline_doc_state = STATE_INLINE_NAME;
1986    } elsif ($decl_type eq 'function') {
1987	process_proto_function($_, $file);
1988    } else {
1989	process_proto_type($_, $file);
1990    }
1991}
1992
1993
1994sub process_file($) {
1995    my $file;
1996    my $func;
1997    my $initial_section_counter = $section_counter;
1998    my ($orig_file) = @_;
1999
2000    $file = map_filename($orig_file);
2001
2002    if (!open(IN,"<$file")) {
2003	print STDERR "Error: Cannot open file $file\n";
2004	++$errors;
2005	return;
2006    }
2007
2008    $. = 1;
2009
2010    $section_counter = 0;
2011    while (<IN>) {
2012	while (s/\\\s*$//) {
2013	    $_ .= <IN>;
2014	}
2015	# Replace tabs by spaces
2016        while ($_ =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {};
2017	if ($state == STATE_NORMAL) {
2018	    process_normal();
2019	} elsif ($state == STATE_NAME) {
2020	    process_name($file, $_);
2021	} elsif ($state == STATE_BODY || $state == STATE_BODY_MAYBE) {
2022	    process_body($file, $_);
2023	} elsif ($state == STATE_INLINE) { # scanning for inline parameters
2024	    # First line (state 1) needs to be a @parameter
2025	    if ($inline_doc_state == STATE_INLINE_NAME && /$doc_inline_sect/o) {
2026		$section = $1;
2027		$contents = $2;
2028                $new_start_line = $.;
2029		if ($contents ne "") {
2030		    while (substr($contents, 0, 1) eq " ") {
2031			$contents = substr($contents, 1);
2032		    }
2033		    $contents .= "\n";
2034		}
2035		$inline_doc_state = STATE_INLINE_TEXT;
2036	    # Documentation block end */
2037	    } elsif (/$doc_inline_end/) {
2038		if (($contents ne "") && ($contents ne "\n")) {
2039		    dump_section($file, $section, $contents);
2040		    $section = $section_default;
2041		    $contents = "";
2042		}
2043		$state = STATE_PROTO;
2044		$inline_doc_state = STATE_INLINE_NA;
2045	    # Regular text
2046	    } elsif (/$doc_content/) {
2047		if ($inline_doc_state == STATE_INLINE_TEXT) {
2048		    $contents .= $1 . "\n";
2049		    # nuke leading blank lines
2050		    if ($contents =~ /^\s*$/) {
2051			$contents = "";
2052		    }
2053		} elsif ($inline_doc_state == STATE_INLINE_NAME) {
2054		    $inline_doc_state = STATE_INLINE_ERROR;
2055		    print STDERR "${file}:$.: warning: ";
2056		    print STDERR "Incorrect use of kernel-doc format: $_";
2057		    ++$warnings;
2058		}
2059	    }
2060	} elsif ($state == STATE_PROTO) {
2061	    process_proto($file, $_);
2062	} elsif ($state == STATE_DOCBLOCK) {
2063		if (/$doc_end/)
2064		{
2065			dump_doc_section($file, $section, $contents);
2066			$section = $section_default;
2067			$contents = "";
2068			$function = "";
2069			%parameterdescs = ();
2070			%parametertypes = ();
2071			@parameterlist = ();
2072			%sections = ();
2073			@sectionlist = ();
2074			$prototype = "";
2075			$state = STATE_NORMAL;
2076		}
2077		elsif (/$doc_content/)
2078		{
2079			if ( $1 eq "" )
2080			{
2081				$contents .= $blankline;
2082			}
2083			else
2084			{
2085				$contents .= $1 . "\n";
2086			}
2087		}
2088	}
2089    }
2090    if ($initial_section_counter == $section_counter) {
2091	if ($output_mode ne "none") {
2092	    print STDERR "${file}:1: warning: no structured comments found\n";
2093	}
2094	if (($output_selection == OUTPUT_INCLUDE) && ($show_not_found == 1)) {
2095	    print STDERR "    Was looking for '$_'.\n" for keys %function_table;
2096	}
2097    }
2098}
2099
2100
2101$kernelversion = get_kernel_version();
2102
2103# generate a sequence of code that will splice in highlighting information
2104# using the s// operator.
2105for (my $k = 0; $k < @highlights; $k++) {
2106    my $pattern = $highlights[$k][0];
2107    my $result = $highlights[$k][1];
2108#   print STDERR "scanning pattern:$pattern, highlight:($result)\n";
2109    $dohighlight .=  "\$contents =~ s:$pattern:$result:gs;\n";
2110}
2111
2112# Read the file that maps relative names to absolute names for
2113# separate source and object directories and for shadow trees.
2114if (open(SOURCE_MAP, "<.tmp_filelist.txt")) {
2115	my ($relname, $absname);
2116	while(<SOURCE_MAP>) {
2117		chop();
2118		($relname, $absname) = (split())[0..1];
2119		$relname =~ s:^/+::;
2120		$source_map{$relname} = $absname;
2121	}
2122	close(SOURCE_MAP);
2123}
2124
2125if ($output_selection == OUTPUT_EXPORTED ||
2126    $output_selection == OUTPUT_INTERNAL) {
2127
2128    push(@export_file_list, @ARGV);
2129
2130    foreach (@export_file_list) {
2131	chomp;
2132	process_export_file($_);
2133    }
2134}
2135
2136foreach (@ARGV) {
2137    chomp;
2138    process_file($_);
2139}
2140if ($verbose && $errors) {
2141  print STDERR "$errors errors\n";
2142}
2143if ($verbose && $warnings) {
2144  print STDERR "$warnings warnings\n";
2145}
2146
2147exit($output_mode eq "none" ? 0 : $errors);
2148