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