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