xref: /linux/scripts/kernel-doc (revision c73894c1e1160296552d8713e88bc76eac43beba)
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## 								 ##
9## #define enhancements by Armin Kuster <akuster@mvista.com>	 ##
10## Copyright (c) 2000 MontaVista Software, Inc.			 ##
11## 								 ##
12## This software falls under the GNU General Public License.     ##
13## Please read the COPYING file for more information             ##
14
15# w.o. 03-11-2000: added the '-filelist' option.
16
17# 18/01/2001 - 	Cleanups
18# 		Functions prototyped as foo(void) same as foo()
19# 		Stop eval'ing where we don't need to.
20# -- huggie@earth.li
21
22# 27/06/2001 -  Allowed whitespace after initial "/**" and
23#               allowed comments before function declarations.
24# -- Christian Kreibich <ck@whoop.org>
25
26# Still to do:
27# 	- add perldoc documentation
28# 	- Look more closely at some of the scarier bits :)
29
30# 26/05/2001 - 	Support for separate source and object trees.
31#		Return error code.
32# 		Keith Owens <kaos@ocs.com.au>
33
34# 23/09/2001 - Added support for typedefs, structs, enums and unions
35#              Support for Context section; can be terminated using empty line
36#              Small fixes (like spaces vs. \s in regex)
37# -- Tim Jansen <tim@tjansen.de>
38
39
40#
41# This will read a 'c' file and scan for embedded comments in the
42# style of gnome comments (+minor extensions - see below).
43#
44
45# Note: This only supports 'c'.
46
47# usage:
48# kerneldoc [ -docbook | -html | -text | -man ]
49#           [ -function funcname [ -function funcname ...] ] c file(s)s > outputfile
50# or
51#           [ -nofunction funcname [ -function funcname ...] ] c file(s)s > outputfile
52#
53#  Set output format using one of -docbook -html -text or -man.  Default is man.
54#
55#  -function funcname
56#	If set, then only generate documentation for the given function(s).  All
57#	other functions are ignored.
58#
59#  -nofunction funcname
60#	If set, then only generate documentation for the other function(s).  All
61#	other functions are ignored. Cannot be used with -function together
62#	(yes thats a bug - perl hackers can fix it 8))
63#
64#  c files - list of 'c' files to process
65#
66#  All output goes to stdout, with errors to stderr.
67
68#
69# format of comments.
70# In the following table, (...)? signifies optional structure.
71#                         (...)* signifies 0 or more structure elements
72# /**
73#  * function_name(:)? (- short description)?
74# (* @parameterx: (description of parameter x)?)*
75# (* a blank line)?
76#  * (Description:)? (Description of function)?
77#  * (section header: (section description)? )*
78#  (*)?*/
79#
80# So .. the trivial example would be:
81#
82# /**
83#  * my_function
84#  **/
85#
86# If the Description: header tag is ommitted, then there must be a blank line
87# after the last parameter specification.
88# e.g.
89# /**
90#  * my_function - does my stuff
91#  * @my_arg: its mine damnit
92#  *
93#  * Does my stuff explained.
94#  */
95#
96#  or, could also use:
97# /**
98#  * my_function - does my stuff
99#  * @my_arg: its mine damnit
100#  * Description: Does my stuff explained.
101#  */
102# etc.
103#
104# Beside functions you can also write documentation for structs, unions,
105# enums and typedefs. Instead of the function name you must write the name
106# of the declaration;  the struct/union/enum/typedef must always precede
107# the name. Nesting of declarations is not supported.
108# Use the argument mechanism to document members or constants.
109# e.g.
110# /**
111#  * struct my_struct - short description
112#  * @a: first member
113#  * @b: second member
114#  *
115#  * Longer description
116#  */
117# struct my_struct {
118#     int a;
119#     int b;
120# };
121#
122# All descriptions can be multiline, except the short function description.
123#
124# You can also add additional sections. When documenting kernel functions you
125# should document the "Context:" of the function, e.g. whether the functions
126# can be called form interrupts. Unlike other sections you can end it with an
127# empty line.
128# Example-sections should contain the string EXAMPLE so that they are marked
129# appropriately in DocBook.
130#
131# Example:
132# /**
133#  * user_function - function that can only be called in user context
134#  * @a: some argument
135#  * Context: !in_interrupt()
136#  *
137#  * Some description
138#  * Example:
139#  *    user_function(22);
140#  */
141# ...
142#
143#
144# All descriptive text is further processed, scanning for the following special
145# patterns, which are highlighted appropriately.
146#
147# 'funcname()' - function
148# '$ENVVAR' - environmental variable
149# '&struct_name' - name of a structure (up to two words including 'struct')
150# '@parameter' - name of a parameter
151# '%CONST' - name of a constant.
152
153my $errors = 0;
154my $warnings = 0;
155
156# match expressions used to find embedded type information
157my $type_constant = '\%([-_\w]+)';
158my $type_func = '(\w+)\(\)';
159my $type_param = '\@(\w+)';
160my $type_struct = '\&((struct\s*)?[_\w]+)';
161my $type_env = '(\$\w+)';
162
163# Output conversion substitutions.
164#  One for each output format
165
166# these work fairly well
167my %highlights_html = ( $type_constant, "<i>\$1</i>",
168			$type_func, "<b>\$1</b>",
169			$type_struct, "<i>\$1</i>",
170			$type_param, "<tt><b>\$1</b></tt>" );
171my $blankline_html = "<p>";
172
173# XML, docbook format
174my %highlights_xml = ( "([^=])\\\"([^\\\"<]+)\\\"", "\$1<quote>\$2</quote>",
175			$type_constant, "<constant>\$1</constant>",
176			$type_func, "<function>\$1</function>",
177			$type_struct, "<structname>\$1</structname>",
178			$type_env, "<envar>\$1</envar>",
179			$type_param, "<parameter>\$1</parameter>" );
180my $blankline_xml = "</para><para>\n";
181
182# gnome, docbook format
183my %highlights_gnome = ( $type_constant, "<replaceable class=\"option\">\$1</replaceable>",
184			 $type_func, "<function>\$1</function>",
185			 $type_struct, "<structname>\$1</structname>",
186			 $type_env, "<envar>\$1</envar>",
187			 $type_param, "<parameter>\$1</parameter>" );
188my $blankline_gnome = "</para><para>\n";
189
190# these are pretty rough
191my %highlights_man = ( $type_constant, "\$1",
192		       $type_func, "\\\\fB\$1\\\\fP",
193		       $type_struct, "\\\\fI\$1\\\\fP",
194		       $type_param, "\\\\fI\$1\\\\fP" );
195my $blankline_man = "";
196
197# text-mode
198my %highlights_text = ( $type_constant, "\$1",
199			$type_func, "\$1",
200			$type_struct, "\$1",
201			$type_param, "\$1" );
202my $blankline_text = "";
203
204
205sub usage {
206    print "Usage: $0 [ -v ] [ -docbook | -html | -text | -man ]\n";
207    print "         [ -function funcname [ -function funcname ...] ]\n";
208    print "         [ -nofunction funcname [ -nofunction funcname ...] ]\n";
209    print "         c source file(s) > outputfile\n";
210    exit 1;
211}
212
213# read arguments
214if ($#ARGV==-1) {
215    usage();
216}
217
218my $verbose = 0;
219my $output_mode = "man";
220my %highlights = %highlights_man;
221my $blankline = $blankline_man;
222my $modulename = "Kernel API";
223my $function_only = 0;
224my $man_date = ('January', 'February', 'March', 'April', 'May', 'June',
225		'July', 'August', 'September', 'October',
226		'November', 'December')[(localtime)[4]] .
227  " " . ((localtime)[5]+1900);
228
229# Essentially these are globals
230# They probably want to be tidied up made more localised or summat.
231# CAVEAT EMPTOR!  Some of the others I localised may not want to be which
232# could cause "use of undefined value" or other bugs.
233my ($function, %function_table,%parametertypes,$declaration_purpose);
234my ($type,$declaration_name,$return_type);
235my ($newsection,$newcontents,$prototype,$filelist, $brcount, %source_map);
236
237# Generated docbook code is inserted in a template at a point where
238# docbook v3.1 requires a non-zero sequence of RefEntry's; see:
239# http://www.oasis-open.org/docbook/documentation/reference/html/refentry.html
240# We keep track of number of generated entries and generate a dummy
241# if needs be to ensure the expanded template can be postprocessed
242# into html.
243my $section_counter = 0;
244
245my $lineprefix="";
246
247# states
248# 0 - normal code
249# 1 - looking for function name
250# 2 - scanning field start.
251# 3 - scanning prototype.
252# 4 - documentation block
253my $state;
254
255#declaration types: can be
256# 'function', 'struct', 'union', 'enum', 'typedef'
257my $decl_type;
258
259my $doc_special = "\@\%\$\&";
260
261my $doc_start = '^/\*\*\s*$'; # Allow whitespace at end of comment start.
262my $doc_end = '\*/';
263my $doc_com = '\s*\*\s*';
264my $doc_decl = $doc_com.'(\w+)';
265my $doc_sect = $doc_com.'(['.$doc_special.']?[\w ]+):(.*)';
266my $doc_content = $doc_com.'(.*)';
267my $doc_block = $doc_com.'DOC:\s*(.*)?';
268
269my %constants;
270my %parameterdescs;
271my @parameterlist;
272my %sections;
273my @sectionlist;
274
275my $contents = "";
276my $section_default = "Description";	# default section
277my $section_intro = "Introduction";
278my $section = $section_default;
279my $section_context = "Context";
280
281my $undescribed = "-- undescribed --";
282
283reset_state();
284
285while ($ARGV[0] =~ m/^-(.*)/) {
286    my $cmd = shift @ARGV;
287    if ($cmd eq "-html") {
288	$output_mode = "html";
289	%highlights = %highlights_html;
290	$blankline = $blankline_html;
291    } elsif ($cmd eq "-man") {
292	$output_mode = "man";
293	%highlights = %highlights_man;
294	$blankline = $blankline_man;
295    } elsif ($cmd eq "-text") {
296	$output_mode = "text";
297	%highlights = %highlights_text;
298	$blankline = $blankline_text;
299    } elsif ($cmd eq "-docbook") {
300	$output_mode = "xml";
301	%highlights = %highlights_xml;
302	$blankline = $blankline_xml;
303    } elsif ($cmd eq "-gnome") {
304	$output_mode = "gnome";
305	%highlights = %highlights_gnome;
306	$blankline = $blankline_gnome;
307    } elsif ($cmd eq "-module") { # not needed for XML, inherits from calling document
308	$modulename = shift @ARGV;
309    } elsif ($cmd eq "-function") { # to only output specific functions
310	$function_only = 1;
311	$function = shift @ARGV;
312	$function_table{$function} = 1;
313    } elsif ($cmd eq "-nofunction") { # to only output specific functions
314	$function_only = 2;
315	$function = shift @ARGV;
316	$function_table{$function} = 1;
317    } elsif ($cmd eq "-v") {
318	$verbose = 1;
319    } elsif (($cmd eq "-h") || ($cmd eq "--help")) {
320	usage();
321    } elsif ($cmd eq '-filelist') {
322	    $filelist = shift @ARGV;
323    }
324}
325
326
327# generate a sequence of code that will splice in highlighting information
328# using the s// operator.
329my $dohighlight = "";
330foreach my $pattern (keys %highlights) {
331#    print "scanning pattern $pattern ($highlights{$pattern})\n";
332    $dohighlight .=  "\$contents =~ s:$pattern:$highlights{$pattern}:gs;\n";
333}
334
335##
336# dumps section contents to arrays/hashes intended for that purpose.
337#
338sub dump_section {
339    my $name = shift;
340    my $contents = join "\n", @_;
341
342    if ($name =~ m/$type_constant/) {
343	$name = $1;
344#	print STDERR "constant section '$1' = '$contents'\n";
345	$constants{$name} = $contents;
346    } elsif ($name =~ m/$type_param/) {
347#	print STDERR "parameter def '$1' = '$contents'\n";
348	$name = $1;
349	$parameterdescs{$name} = $contents;
350    } else {
351#	print STDERR "other section '$name' = '$contents'\n";
352	$sections{$name} = $contents;
353	push @sectionlist, $name;
354    }
355}
356
357##
358# output function
359#
360# parameterdescs, a hash.
361#  function => "function name"
362#  parameterlist => @list of parameters
363#  parameterdescs => %parameter descriptions
364#  sectionlist => @list of sections
365#  sections => %descriont descriptions
366#
367
368sub output_highlight {
369    my $contents = join "\n",@_;
370    my $line;
371
372#   DEBUG
373#   if (!defined $contents) {
374#	use Carp;
375#	confess "output_highlight got called with no args?\n";
376#   }
377
378    eval $dohighlight;
379    die $@ if $@;
380    foreach $line (split "\n", $contents) {
381      if ($line eq ""){
382	    print $lineprefix, $blankline;
383	} else {
384            $line =~ s/\\\\\\/\&/g;
385	    print $lineprefix, $line;
386	}
387	print "\n";
388    }
389}
390
391#output sections in html
392sub output_section_html(%) {
393    my %args = %{$_[0]};
394    my $section;
395
396    foreach $section (@{$args{'sectionlist'}}) {
397	print "<h3>$section</h3>\n";
398	print "<blockquote>\n";
399	output_highlight($args{'sections'}{$section});
400	print "</blockquote>\n";
401    }
402}
403
404# output enum in html
405sub output_enum_html(%) {
406    my %args = %{$_[0]};
407    my ($parameter);
408    my $count;
409    print "<h2>enum ".$args{'enum'}."</h2>\n";
410
411    print "<b>enum ".$args{'enum'}."</b> {<br>\n";
412    $count = 0;
413    foreach $parameter (@{$args{'parameterlist'}}) {
414        print " <b>".$parameter."</b>";
415	if ($count != $#{$args{'parameterlist'}}) {
416	    $count++;
417	    print ",\n";
418	}
419	print "<br>";
420    }
421    print "};<br>\n";
422
423    print "<h3>Constants</h3>\n";
424    print "<dl>\n";
425    foreach $parameter (@{$args{'parameterlist'}}) {
426	print "<dt><b>".$parameter."</b>\n";
427	print "<dd>";
428	output_highlight($args{'parameterdescs'}{$parameter});
429    }
430    print "</dl>\n";
431    output_section_html(@_);
432    print "<hr>\n";
433}
434
435# output tyepdef in html
436sub output_typedef_html(%) {
437    my %args = %{$_[0]};
438    my ($parameter);
439    my $count;
440    print "<h2>typedef ".$args{'typedef'}."</h2>\n";
441
442    print "<b>typedef ".$args{'typedef'}."</b>\n";
443    output_section_html(@_);
444    print "<hr>\n";
445}
446
447# output struct in html
448sub output_struct_html(%) {
449    my %args = %{$_[0]};
450    my ($parameter);
451
452    print "<h2>".$args{'type'}." ".$args{'struct'}."</h2>\n";
453    print "<b>".$args{'type'}." ".$args{'struct'}."</b> {<br>\n";
454    foreach $parameter (@{$args{'parameterlist'}}) {
455	if ($parameter =~ /^#/) {
456		print "$parameter<br>\n";
457		next;
458	}
459	my $parameter_name = $parameter;
460	$parameter_name =~ s/\[.*//;
461
462        ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
463	$type = $args{'parametertypes'}{$parameter};
464	if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
465	    # pointer-to-function
466	    print " <i>$1</i><b>$parameter</b>) <i>($2)</i>;<br>\n";
467	} elsif ($type =~ m/^(.*?)\s*(:.*)/) {
468	    print " <i>$1</i> <b>$parameter</b>$2;<br>\n";
469	} else {
470	    print " <i>$type</i> <b>$parameter</b>;<br>\n";
471	}
472    }
473    print "};<br>\n";
474
475    print "<h3>Members</h3>\n";
476    print "<dl>\n";
477    foreach $parameter (@{$args{'parameterlist'}}) {
478	($parameter =~ /^#/) && next;
479
480	my $parameter_name = $parameter;
481	$parameter_name =~ s/\[.*//;
482
483        ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
484	print "<dt><b>".$parameter."</b>\n";
485	print "<dd>";
486	output_highlight($args{'parameterdescs'}{$parameter_name});
487    }
488    print "</dl>\n";
489    output_section_html(@_);
490    print "<hr>\n";
491}
492
493# output function in html
494sub output_function_html(%) {
495    my %args = %{$_[0]};
496    my ($parameter, $section);
497    my $count;
498    print "<h2>Function</h2>\n";
499
500    print "<i>".$args{'functiontype'}."</i>\n";
501    print "<b>".$args{'function'}."</b>\n";
502    print "(";
503    $count = 0;
504    foreach $parameter (@{$args{'parameterlist'}}) {
505	$type = $args{'parametertypes'}{$parameter};
506	if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
507	    # pointer-to-function
508	    print "<i>$1</i><b>$parameter</b>) <i>($2)</i>";
509	} else {
510	    print "<i>".$type."</i> <b>".$parameter."</b>";
511	}
512	if ($count != $#{$args{'parameterlist'}}) {
513	    $count++;
514	    print ",\n";
515	}
516    }
517    print ")\n";
518
519    print "<h3>Arguments</h3>\n";
520    print "<dl>\n";
521    foreach $parameter (@{$args{'parameterlist'}}) {
522	my $parameter_name = $parameter;
523	$parameter_name =~ s/\[.*//;
524
525        ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
526	print "<dt><b>".$parameter."</b>\n";
527	print "<dd>";
528	output_highlight($args{'parameterdescs'}{$parameter_name});
529    }
530    print "</dl>\n";
531    output_section_html(@_);
532    print "<hr>\n";
533}
534
535# output intro in html
536sub output_intro_html(%) {
537    my %args = %{$_[0]};
538    my ($parameter, $section);
539    my $count;
540
541    foreach $section (@{$args{'sectionlist'}}) {
542	print "<h3>$section</h3>\n";
543	print "<ul>\n";
544	output_highlight($args{'sections'}{$section});
545	print "</ul>\n";
546    }
547    print "<hr>\n";
548}
549
550sub output_section_xml(%) {
551    my %args = %{$_[0]};
552    my $section;
553    # print out each section
554    $lineprefix="   ";
555    foreach $section (@{$args{'sectionlist'}}) {
556	print "<refsect1>\n";
557	print "<title>$section</title>\n";
558	if ($section =~ m/EXAMPLE/i) {
559	    print "<informalexample><programlisting>\n";
560	} else {
561	    print "<para>\n";
562	}
563	output_highlight($args{'sections'}{$section});
564	if ($section =~ m/EXAMPLE/i) {
565	    print "</programlisting></informalexample>\n";
566	} else {
567	    print "</para>\n";
568	}
569	print "</refsect1>\n";
570    }
571}
572
573# output function in XML DocBook
574sub output_function_xml(%) {
575    my %args = %{$_[0]};
576    my ($parameter, $section);
577    my $count;
578    my $id;
579
580    $id = "API-".$args{'function'};
581    $id =~ s/[^A-Za-z0-9]/-/g;
582
583    print "<refentry>\n";
584    print "<refmeta>\n";
585    print "<refentrytitle><phrase id=\"$id\">".$args{'function'}."</phrase></refentrytitle>\n";
586    print "</refmeta>\n";
587    print "<refnamediv>\n";
588    print " <refname>".$args{'function'}."</refname>\n";
589    print " <refpurpose>\n";
590    print "  ";
591    output_highlight ($args{'purpose'});
592    print " </refpurpose>\n";
593    print "</refnamediv>\n";
594
595    print "<refsynopsisdiv>\n";
596    print " <title>Synopsis</title>\n";
597    print "  <funcsynopsis><funcprototype>\n";
598    print "   <funcdef>".$args{'functiontype'}." ";
599    print "<function>".$args{'function'}." </function></funcdef>\n";
600
601    $count = 0;
602    if ($#{$args{'parameterlist'}} >= 0) {
603	foreach $parameter (@{$args{'parameterlist'}}) {
604	    $type = $args{'parametertypes'}{$parameter};
605	    if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
606		# pointer-to-function
607		print "   <paramdef>$1<parameter>$parameter</parameter>)\n";
608		print "     <funcparams>$2</funcparams></paramdef>\n";
609	    } else {
610		print "   <paramdef>".$type;
611		print " <parameter>$parameter</parameter></paramdef>\n";
612	    }
613	}
614    } else {
615	print "  <void/>\n";
616    }
617    print "  </funcprototype></funcsynopsis>\n";
618    print "</refsynopsisdiv>\n";
619
620    # print parameters
621    print "<refsect1>\n <title>Arguments</title>\n";
622    if ($#{$args{'parameterlist'}} >= 0) {
623	print " <variablelist>\n";
624	foreach $parameter (@{$args{'parameterlist'}}) {
625	    my $parameter_name = $parameter;
626	    $parameter_name =~ s/\[.*//;
627
628	    print "  <varlistentry>\n   <term><parameter>$parameter</parameter></term>\n";
629	    print "   <listitem>\n    <para>\n";
630	    $lineprefix="     ";
631	    output_highlight($args{'parameterdescs'}{$parameter_name});
632	    print "    </para>\n   </listitem>\n  </varlistentry>\n";
633	}
634	print " </variablelist>\n";
635    } else {
636	print " <para>\n  None\n </para>\n";
637    }
638    print "</refsect1>\n";
639
640    output_section_xml(@_);
641    print "</refentry>\n\n";
642}
643
644# output struct in XML DocBook
645sub output_struct_xml(%) {
646    my %args = %{$_[0]};
647    my ($parameter, $section);
648    my $id;
649
650    $id = "API-struct-".$args{'struct'};
651    $id =~ s/[^A-Za-z0-9]/-/g;
652
653    print "<refentry>\n";
654    print "<refmeta>\n";
655    print "<refentrytitle><phrase id=\"$id\">".$args{'type'}." ".$args{'struct'}."</phrase></refentrytitle>\n";
656    print "</refmeta>\n";
657    print "<refnamediv>\n";
658    print " <refname>".$args{'type'}." ".$args{'struct'}."</refname>\n";
659    print " <refpurpose>\n";
660    print "  ";
661    output_highlight ($args{'purpose'});
662    print " </refpurpose>\n";
663    print "</refnamediv>\n";
664
665    print "<refsynopsisdiv>\n";
666    print " <title>Synopsis</title>\n";
667    print "  <programlisting>\n";
668    print $args{'type'}." ".$args{'struct'}." {\n";
669    foreach $parameter (@{$args{'parameterlist'}}) {
670	if ($parameter =~ /^#/) {
671	    print "$parameter\n";
672	    next;
673	}
674
675	my $parameter_name = $parameter;
676	$parameter_name =~ s/\[.*//;
677
678	defined($args{'parameterdescs'}{$parameter_name}) || next;
679        ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
680	$type = $args{'parametertypes'}{$parameter};
681	if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
682	    # pointer-to-function
683	    print "  $1 $parameter) ($2);\n";
684	} elsif ($type =~ m/^(.*?)\s*(:.*)/) {
685	    print "  $1 $parameter$2;\n";
686	} else {
687	    print "  ".$type." ".$parameter.";\n";
688	}
689    }
690    print "};";
691    print "  </programlisting>\n";
692    print "</refsynopsisdiv>\n";
693
694    print " <refsect1>\n";
695    print "  <title>Members</title>\n";
696
697    print "  <variablelist>\n";
698    foreach $parameter (@{$args{'parameterlist'}}) {
699      ($parameter =~ /^#/) && next;
700
701      my $parameter_name = $parameter;
702      $parameter_name =~ s/\[.*//;
703
704      defined($args{'parameterdescs'}{$parameter_name}) || next;
705      ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
706      print "    <varlistentry>";
707      print "      <term>$parameter</term>\n";
708      print "      <listitem><para>\n";
709      output_highlight($args{'parameterdescs'}{$parameter_name});
710      print "      </para></listitem>\n";
711      print "    </varlistentry>\n";
712    }
713    print "  </variablelist>\n";
714    print " </refsect1>\n";
715
716    output_section_xml(@_);
717
718    print "</refentry>\n\n";
719}
720
721# output enum in XML DocBook
722sub output_enum_xml(%) {
723    my %args = %{$_[0]};
724    my ($parameter, $section);
725    my $count;
726    my $id;
727
728    $id = "API-enum-".$args{'enum'};
729    $id =~ s/[^A-Za-z0-9]/-/g;
730
731    print "<refentry>\n";
732    print "<refmeta>\n";
733    print "<refentrytitle><phrase id=\"$id\">enum ".$args{'enum'}."</phrase></refentrytitle>\n";
734    print "</refmeta>\n";
735    print "<refnamediv>\n";
736    print " <refname>enum ".$args{'enum'}."</refname>\n";
737    print " <refpurpose>\n";
738    print "  ";
739    output_highlight ($args{'purpose'});
740    print " </refpurpose>\n";
741    print "</refnamediv>\n";
742
743    print "<refsynopsisdiv>\n";
744    print " <title>Synopsis</title>\n";
745    print "  <programlisting>\n";
746    print "enum ".$args{'enum'}." {\n";
747    $count = 0;
748    foreach $parameter (@{$args{'parameterlist'}}) {
749        print "  $parameter";
750        if ($count != $#{$args{'parameterlist'}}) {
751	    $count++;
752	    print ",";
753        }
754	print "\n";
755    }
756    print "};";
757    print "  </programlisting>\n";
758    print "</refsynopsisdiv>\n";
759
760    print "<refsect1>\n";
761    print " <title>Constants</title>\n";
762    print "  <variablelist>\n";
763    foreach $parameter (@{$args{'parameterlist'}}) {
764      my $parameter_name = $parameter;
765      $parameter_name =~ s/\[.*//;
766
767      print "    <varlistentry>";
768      print "      <term>$parameter</term>\n";
769      print "      <listitem><para>\n";
770      output_highlight($args{'parameterdescs'}{$parameter_name});
771      print "      </para></listitem>\n";
772      print "    </varlistentry>\n";
773    }
774    print "  </variablelist>\n";
775    print "</refsect1>\n";
776
777    output_section_xml(@_);
778
779    print "</refentry>\n\n";
780}
781
782# output typedef in XML DocBook
783sub output_typedef_xml(%) {
784    my %args = %{$_[0]};
785    my ($parameter, $section);
786    my $id;
787
788    $id = "API-typedef-".$args{'typedef'};
789    $id =~ s/[^A-Za-z0-9]/-/g;
790
791    print "<refentry>\n";
792    print "<refmeta>\n";
793    print "<refentrytitle><phrase id=\"$id\">typedef ".$args{'typedef'}."</phrase></refentrytitle>\n";
794    print "</refmeta>\n";
795    print "<refnamediv>\n";
796    print " <refname>typedef ".$args{'typedef'}."</refname>\n";
797    print " <refpurpose>\n";
798    print "  ";
799    output_highlight ($args{'purpose'});
800    print " </refpurpose>\n";
801    print "</refnamediv>\n";
802
803    print "<refsynopsisdiv>\n";
804    print " <title>Synopsis</title>\n";
805    print "  <synopsis>typedef ".$args{'typedef'}.";</synopsis>\n";
806    print "</refsynopsisdiv>\n";
807
808    output_section_xml(@_);
809
810    print "</refentry>\n\n";
811}
812
813# output in XML DocBook
814sub output_intro_xml(%) {
815    my %args = %{$_[0]};
816    my ($parameter, $section);
817    my $count;
818
819    my $id = $args{'module'};
820    $id =~ s/[^A-Za-z0-9]/-/g;
821
822    # print out each section
823    $lineprefix="   ";
824    foreach $section (@{$args{'sectionlist'}}) {
825	print "<refsect1>\n <title>$section</title>\n <para>\n";
826	if ($section =~ m/EXAMPLE/i) {
827	    print "<example><para>\n";
828	}
829	output_highlight($args{'sections'}{$section});
830	if ($section =~ m/EXAMPLE/i) {
831	    print "</para></example>\n";
832	}
833	print " </para>\n</refsect1>\n";
834    }
835
836    print "\n\n";
837}
838
839# output in XML DocBook
840sub output_function_gnome {
841    my %args = %{$_[0]};
842    my ($parameter, $section);
843    my $count;
844    my $id;
845
846    $id = $args{'module'}."-".$args{'function'};
847    $id =~ s/[^A-Za-z0-9]/-/g;
848
849    print "<sect2>\n";
850    print " <title id=\"$id\">".$args{'function'}."</title>\n";
851
852    print "  <funcsynopsis>\n";
853    print "   <funcdef>".$args{'functiontype'}." ";
854    print "<function>".$args{'function'}." ";
855    print "</function></funcdef>\n";
856
857    $count = 0;
858    if ($#{$args{'parameterlist'}} >= 0) {
859	foreach $parameter (@{$args{'parameterlist'}}) {
860	    $type = $args{'parametertypes'}{$parameter};
861	    if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
862		# pointer-to-function
863		print "   <paramdef>$1 <parameter>$parameter</parameter>)\n";
864		print "     <funcparams>$2</funcparams></paramdef>\n";
865	    } else {
866		print "   <paramdef>".$type;
867		print " <parameter>$parameter</parameter></paramdef>\n";
868	    }
869	}
870    } else {
871	print "  <void>\n";
872    }
873    print "  </funcsynopsis>\n";
874    if ($#{$args{'parameterlist'}} >= 0) {
875	print " <informaltable pgwide=\"1\" frame=\"none\" role=\"params\">\n";
876	print "<tgroup cols=\"2\">\n";
877	print "<colspec colwidth=\"2*\">\n";
878	print "<colspec colwidth=\"8*\">\n";
879	print "<tbody>\n";
880	foreach $parameter (@{$args{'parameterlist'}}) {
881	    my $parameter_name = $parameter;
882	    $parameter_name =~ s/\[.*//;
883
884	    print "  <row><entry align=\"right\"><parameter>$parameter</parameter></entry>\n";
885	    print "   <entry>\n";
886	    $lineprefix="     ";
887	    output_highlight($args{'parameterdescs'}{$parameter_name});
888	    print "    </entry></row>\n";
889	}
890	print " </tbody></tgroup></informaltable>\n";
891    } else {
892	print " <para>\n  None\n </para>\n";
893    }
894
895    # print out each section
896    $lineprefix="   ";
897    foreach $section (@{$args{'sectionlist'}}) {
898	print "<simplesect>\n <title>$section</title>\n";
899	if ($section =~ m/EXAMPLE/i) {
900	    print "<example><programlisting>\n";
901	} else {
902	}
903	print "<para>\n";
904	output_highlight($args{'sections'}{$section});
905	print "</para>\n";
906	if ($section =~ m/EXAMPLE/i) {
907	    print "</programlisting></example>\n";
908	} else {
909	}
910	print " </simplesect>\n";
911    }
912
913    print "</sect2>\n\n";
914}
915
916##
917# output function in man
918sub output_function_man(%) {
919    my %args = %{$_[0]};
920    my ($parameter, $section);
921    my $count;
922
923    print ".TH \"$args{'function'}\" 9 \"$args{'function'}\" \"$man_date\" \"Kernel Hacker's Manual\" LINUX\n";
924
925    print ".SH NAME\n";
926    print $args{'function'}." \\- ".$args{'purpose'}."\n";
927
928    print ".SH SYNOPSIS\n";
929    print ".B \"".$args{'functiontype'}."\" ".$args{'function'}."\n";
930    $count = 0;
931    my $parenth = "(";
932    my $post = ",";
933    foreach my $parameter (@{$args{'parameterlist'}}) {
934	if ($count == $#{$args{'parameterlist'}}) {
935	    $post = ");";
936	}
937	$type = $args{'parametertypes'}{$parameter};
938	if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
939	    # pointer-to-function
940	    print ".BI \"".$parenth.$1."\" ".$parameter." \") (".$2.")".$post."\"\n";
941	} else {
942	    $type =~ s/([^\*])$/$1 /;
943	    print ".BI \"".$parenth.$type."\" ".$parameter." \"".$post."\"\n";
944	}
945	$count++;
946	$parenth = "";
947    }
948
949    print ".SH ARGUMENTS\n";
950    foreach $parameter (@{$args{'parameterlist'}}) {
951	my $parameter_name = $parameter;
952	$parameter_name =~ s/\[.*//;
953
954	print ".IP \"".$parameter."\" 12\n";
955	output_highlight($args{'parameterdescs'}{$parameter_name});
956    }
957    foreach $section (@{$args{'sectionlist'}}) {
958	print ".SH \"", uc $section, "\"\n";
959	output_highlight($args{'sections'}{$section});
960    }
961}
962
963##
964# output enum in man
965sub output_enum_man(%) {
966    my %args = %{$_[0]};
967    my ($parameter, $section);
968    my $count;
969
970    print ".TH \"$args{'module'}\" 9 \"enum $args{'enum'}\" \"$man_date\" \"API Manual\" LINUX\n";
971
972    print ".SH NAME\n";
973    print "enum ".$args{'enum'}." \\- ".$args{'purpose'}."\n";
974
975    print ".SH SYNOPSIS\n";
976    print "enum ".$args{'enum'}." {\n";
977    $count = 0;
978    foreach my $parameter (@{$args{'parameterlist'}}) {
979        print ".br\n.BI \"    $parameter\"\n";
980	if ($count == $#{$args{'parameterlist'}}) {
981	    print "\n};\n";
982	    last;
983	}
984	else {
985	    print ", \n.br\n";
986	}
987	$count++;
988    }
989
990    print ".SH Constants\n";
991    foreach $parameter (@{$args{'parameterlist'}}) {
992	my $parameter_name = $parameter;
993	$parameter_name =~ s/\[.*//;
994
995	print ".IP \"".$parameter."\" 12\n";
996	output_highlight($args{'parameterdescs'}{$parameter_name});
997    }
998    foreach $section (@{$args{'sectionlist'}}) {
999	print ".SH \"$section\"\n";
1000	output_highlight($args{'sections'}{$section});
1001    }
1002}
1003
1004##
1005# output struct in man
1006sub output_struct_man(%) {
1007    my %args = %{$_[0]};
1008    my ($parameter, $section);
1009
1010    print ".TH \"$args{'module'}\" 9 \"".$args{'type'}." ".$args{'struct'}."\" \"$man_date\" \"API Manual\" LINUX\n";
1011
1012    print ".SH NAME\n";
1013    print $args{'type'}." ".$args{'struct'}." \\- ".$args{'purpose'}."\n";
1014
1015    print ".SH SYNOPSIS\n";
1016    print $args{'type'}." ".$args{'struct'}." {\n.br\n";
1017
1018    foreach my $parameter (@{$args{'parameterlist'}}) {
1019	if ($parameter =~ /^#/) {
1020	    print ".BI \"$parameter\"\n.br\n";
1021	    next;
1022	}
1023	my $parameter_name = $parameter;
1024	$parameter_name =~ s/\[.*//;
1025
1026        ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
1027	$type = $args{'parametertypes'}{$parameter};
1028	if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1029	    # pointer-to-function
1030	    print ".BI \"    ".$1."\" ".$parameter." \") (".$2.")"."\"\n;\n";
1031	} elsif ($type =~ m/^(.*?)\s*(:.*)/) {
1032	    print ".BI \"    ".$1."\" ".$parameter.$2." \""."\"\n;\n";
1033	} else {
1034	    $type =~ s/([^\*])$/$1 /;
1035	    print ".BI \"    ".$type."\" ".$parameter." \""."\"\n;\n";
1036	}
1037	print "\n.br\n";
1038    }
1039    print "};\n.br\n";
1040
1041    print ".SH Arguments\n";
1042    foreach $parameter (@{$args{'parameterlist'}}) {
1043	($parameter =~ /^#/) && next;
1044
1045	my $parameter_name = $parameter;
1046	$parameter_name =~ s/\[.*//;
1047
1048        ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
1049	print ".IP \"".$parameter."\" 12\n";
1050	output_highlight($args{'parameterdescs'}{$parameter_name});
1051    }
1052    foreach $section (@{$args{'sectionlist'}}) {
1053	print ".SH \"$section\"\n";
1054	output_highlight($args{'sections'}{$section});
1055    }
1056}
1057
1058##
1059# output typedef in man
1060sub output_typedef_man(%) {
1061    my %args = %{$_[0]};
1062    my ($parameter, $section);
1063
1064    print ".TH \"$args{'module'}\" 9 \"$args{'typedef'}\" \"$man_date\" \"API Manual\" LINUX\n";
1065
1066    print ".SH NAME\n";
1067    print "typedef ".$args{'typedef'}." \\- ".$args{'purpose'}."\n";
1068
1069    foreach $section (@{$args{'sectionlist'}}) {
1070	print ".SH \"$section\"\n";
1071	output_highlight($args{'sections'}{$section});
1072    }
1073}
1074
1075sub output_intro_man(%) {
1076    my %args = %{$_[0]};
1077    my ($parameter, $section);
1078    my $count;
1079
1080    print ".TH \"$args{'module'}\" 9 \"$args{'module'}\" \"$man_date\" \"API Manual\" LINUX\n";
1081
1082    foreach $section (@{$args{'sectionlist'}}) {
1083	print ".SH \"$section\"\n";
1084	output_highlight($args{'sections'}{$section});
1085    }
1086}
1087
1088##
1089# output in text
1090sub output_function_text(%) {
1091    my %args = %{$_[0]};
1092    my ($parameter, $section);
1093
1094    print "Function:\n\n";
1095    my $start=$args{'functiontype'}." ".$args{'function'}." (";
1096    print $start;
1097    my $count = 0;
1098    foreach my $parameter (@{$args{'parameterlist'}}) {
1099	$type = $args{'parametertypes'}{$parameter};
1100	if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1101	    # pointer-to-function
1102	    print $1.$parameter.") (".$2;
1103	} else {
1104	    print $type." ".$parameter;
1105	}
1106	if ($count != $#{$args{'parameterlist'}}) {
1107	    $count++;
1108	    print ",\n";
1109	    print " " x length($start);
1110	} else {
1111	    print ");\n\n";
1112	}
1113    }
1114
1115    print "Arguments:\n\n";
1116    foreach $parameter (@{$args{'parameterlist'}}) {
1117	my $parameter_name = $parameter;
1118	$parameter_name =~ s/\[.*//;
1119
1120	print $parameter."\n\t".$args{'parameterdescs'}{$parameter_name}."\n";
1121    }
1122    output_section_text(@_);
1123}
1124
1125#output sections in text
1126sub output_section_text(%) {
1127    my %args = %{$_[0]};
1128    my $section;
1129
1130    print "\n";
1131    foreach $section (@{$args{'sectionlist'}}) {
1132	print "$section:\n\n";
1133	output_highlight($args{'sections'}{$section});
1134    }
1135    print "\n\n";
1136}
1137
1138# output enum in text
1139sub output_enum_text(%) {
1140    my %args = %{$_[0]};
1141    my ($parameter);
1142    my $count;
1143    print "Enum:\n\n";
1144
1145    print "enum ".$args{'enum'}." {\n";
1146    $count = 0;
1147    foreach $parameter (@{$args{'parameterlist'}}) {
1148        print "\t$parameter";
1149	if ($count != $#{$args{'parameterlist'}}) {
1150	    $count++;
1151	    print ",";
1152	}
1153	print "\n";
1154    }
1155    print "};\n\n";
1156
1157    print "Constants:\n\n";
1158    foreach $parameter (@{$args{'parameterlist'}}) {
1159	print "$parameter\n\t";
1160	print $args{'parameterdescs'}{$parameter}."\n";
1161    }
1162
1163    output_section_text(@_);
1164}
1165
1166# output typedef in text
1167sub output_typedef_text(%) {
1168    my %args = %{$_[0]};
1169    my ($parameter);
1170    my $count;
1171    print "Typedef:\n\n";
1172
1173    print "typedef ".$args{'typedef'}."\n";
1174    output_section_text(@_);
1175}
1176
1177# output struct as text
1178sub output_struct_text(%) {
1179    my %args = %{$_[0]};
1180    my ($parameter);
1181
1182    print $args{'type'}." ".$args{'struct'}.":\n\n";
1183    print $args{'type'}." ".$args{'struct'}." {\n";
1184    foreach $parameter (@{$args{'parameterlist'}}) {
1185	if ($parameter =~ /^#/) {
1186	    print "$parameter\n";
1187	    next;
1188	}
1189
1190	my $parameter_name = $parameter;
1191	$parameter_name =~ s/\[.*//;
1192
1193        ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
1194	$type = $args{'parametertypes'}{$parameter};
1195	if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1196	    # pointer-to-function
1197	    print "\t$1 $parameter) ($2);\n";
1198	} elsif ($type =~ m/^(.*?)\s*(:.*)/) {
1199	    print "\t$1 $parameter$2;\n";
1200	} else {
1201	    print "\t".$type." ".$parameter.";\n";
1202	}
1203    }
1204    print "};\n\n";
1205
1206    print "Members:\n\n";
1207    foreach $parameter (@{$args{'parameterlist'}}) {
1208	($parameter =~ /^#/) && next;
1209
1210	my $parameter_name = $parameter;
1211	$parameter_name =~ s/\[.*//;
1212
1213        ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
1214	print "$parameter\n\t";
1215	print $args{'parameterdescs'}{$parameter_name}."\n";
1216    }
1217    print "\n";
1218    output_section_text(@_);
1219}
1220
1221sub output_intro_text(%) {
1222    my %args = %{$_[0]};
1223    my ($parameter, $section);
1224
1225    foreach $section (@{$args{'sectionlist'}}) {
1226	print " $section:\n";
1227	print "    -> ";
1228	output_highlight($args{'sections'}{$section});
1229    }
1230}
1231
1232##
1233# generic output function for typedefs
1234sub output_declaration {
1235    no strict 'refs';
1236    my $name = shift;
1237    my $functype = shift;
1238    my $func = "output_${functype}_$output_mode";
1239    if (($function_only==0) ||
1240	( $function_only == 1 && defined($function_table{$name})) ||
1241	( $function_only == 2 && !defined($function_table{$name})))
1242    {
1243        &$func(@_);
1244	$section_counter++;
1245    }
1246}
1247
1248##
1249# generic output function - calls the right one based
1250# on current output mode.
1251sub output_intro {
1252    no strict 'refs';
1253    my $func = "output_intro_".$output_mode;
1254    &$func(@_);
1255    $section_counter++;
1256}
1257
1258##
1259# takes a declaration (struct, union, enum, typedef) and
1260# invokes the right handler. NOT called for functions.
1261sub dump_declaration($$) {
1262    no strict 'refs';
1263    my ($prototype, $file) = @_;
1264    my $func = "dump_".$decl_type;
1265    &$func(@_);
1266}
1267
1268sub dump_union($$) {
1269    dump_struct(@_);
1270}
1271
1272sub dump_struct($$) {
1273    my $x = shift;
1274    my $file = shift;
1275
1276    if ($x =~/(struct|union)\s+(\w+)\s*{(.*)}/) {
1277        $declaration_name = $2;
1278        my $members = $3;
1279
1280	# ignore embedded structs or unions
1281	$members =~ s/{.*?}//g;
1282
1283	create_parameterlist($members, ';', $file);
1284
1285	output_declaration($declaration_name,
1286			   'struct',
1287			   {'struct' => $declaration_name,
1288			    'module' => $modulename,
1289			    'parameterlist' => \@parameterlist,
1290			    'parameterdescs' => \%parameterdescs,
1291			    'parametertypes' => \%parametertypes,
1292			    'sectionlist' => \@sectionlist,
1293			    'sections' => \%sections,
1294			    'purpose' => $declaration_purpose,
1295			    'type' => $decl_type
1296			   });
1297    }
1298    else {
1299        print STDERR "Error(${file}:$.): Cannot parse struct or union!\n";
1300	++$errors;
1301    }
1302}
1303
1304sub dump_enum($$) {
1305    my $x = shift;
1306    my $file = shift;
1307
1308    if ($x =~ /enum\s+(\w+)\s*{(.*)}/) {
1309        $declaration_name = $1;
1310        my $members = $2;
1311
1312	foreach my $arg (split ',', $members) {
1313	    $arg =~ s/^\s*(\w+).*/$1/;
1314	    push @parameterlist, $arg;
1315	    if (!$parameterdescs{$arg}) {
1316	        $parameterdescs{$arg} = $undescribed;
1317	        print STDERR "Warning(${file}:$.): Enum value '$arg' ".
1318		    "not described in enum '$declaration_name'\n";
1319	    }
1320
1321	}
1322
1323	output_declaration($declaration_name,
1324			   'enum',
1325			   {'enum' => $declaration_name,
1326			    'module' => $modulename,
1327			    'parameterlist' => \@parameterlist,
1328			    'parameterdescs' => \%parameterdescs,
1329			    'sectionlist' => \@sectionlist,
1330			    'sections' => \%sections,
1331			    'purpose' => $declaration_purpose
1332			   });
1333    }
1334    else {
1335        print STDERR "Error(${file}:$.): Cannot parse enum!\n";
1336	++$errors;
1337    }
1338}
1339
1340sub dump_typedef($$) {
1341    my $x = shift;
1342    my $file = shift;
1343
1344    while (($x =~ /\(*.\)\s*;$/) || ($x =~ /\[*.\]\s*;$/)) {
1345        $x =~ s/\(*.\)\s*;$/;/;
1346	$x =~ s/\[*.\]\s*;$/;/;
1347    }
1348
1349    if ($x =~ /typedef.*\s+(\w+)\s*;/) {
1350        $declaration_name = $1;
1351
1352	output_declaration($declaration_name,
1353			   'typedef',
1354			   {'typedef' => $declaration_name,
1355			    'module' => $modulename,
1356			    'sectionlist' => \@sectionlist,
1357			    'sections' => \%sections,
1358			    'purpose' => $declaration_purpose
1359			   });
1360    }
1361    else {
1362        print STDERR "Error(${file}:$.): Cannot parse typedef!\n";
1363	++$errors;
1364    }
1365}
1366
1367sub create_parameterlist($$$) {
1368    my $args = shift;
1369    my $splitter = shift;
1370    my $file = shift;
1371    my $type;
1372    my $param;
1373
1374    while ($args =~ /(\([^\),]+),/) {
1375        $args =~ s/(\([^\),]+),/$1#/g;
1376    }
1377
1378    foreach my $arg (split($splitter, $args)) {
1379	# strip comments
1380	$arg =~ s/\/\*.*\*\///;
1381        # strip leading/trailing spaces
1382        $arg =~ s/^\s*//;
1383	$arg =~ s/\s*$//;
1384	$arg =~ s/\s+/ /;
1385
1386	if ($arg =~ /^#/) {
1387	    # Treat preprocessor directive as a typeless variable just to fill
1388	    # corresponding data structures "correctly". Catch it later in
1389	    # output_* subs.
1390	    push_parameter($arg, "", $file);
1391	} elsif ($arg =~ m/\(/) {
1392	    # pointer-to-function
1393	    $arg =~ tr/#/,/;
1394	    $arg =~ m/[^\(]+\(\*([^\)]+)\)/;
1395	    $param = $1;
1396	    $type = $arg;
1397	    $type =~ s/([^\(]+\(\*)$param/$1/;
1398	    push_parameter($param, $type, $file);
1399	} else {
1400	    $arg =~ s/\s*:\s*/:/g;
1401	    $arg =~ s/\s*\[/\[/g;
1402
1403	    my @args = split('\s*,\s*', $arg);
1404	    if ($args[0] =~ m/\*/) {
1405		$args[0] =~ s/(\*+)\s*/ $1/;
1406	    }
1407	    my @first_arg = split('\s+', shift @args);
1408	    unshift(@args, pop @first_arg);
1409	    $type = join " ", @first_arg;
1410
1411	    foreach $param (@args) {
1412		if ($param =~ m/^(\*+)\s*(.*)/) {
1413		    push_parameter($2, "$type $1", $file);
1414		}
1415		elsif ($param =~ m/(.*?):(\d+)/) {
1416		    push_parameter($1, "$type:$2", $file)
1417		}
1418		else {
1419		    push_parameter($param, $type, $file);
1420		}
1421	    }
1422	}
1423    }
1424}
1425
1426sub push_parameter($$$) {
1427	my $param = shift;
1428	my $type = shift;
1429	my $file = shift;
1430
1431	my $param_name = $param;
1432	$param_name =~ s/\[.*//;
1433
1434	if ($type eq "" && $param eq "...")
1435	{
1436	    $type="";
1437	    $param="...";
1438	    $parameterdescs{"..."} = "variable arguments";
1439	}
1440	elsif ($type eq "" && ($param eq "" or $param eq "void"))
1441	{
1442	    $type="";
1443	    $param="void";
1444	    $parameterdescs{void} = "no arguments";
1445	}
1446	if (defined $type && $type && !defined $parameterdescs{$param_name}) {
1447	    $parameterdescs{$param_name} = $undescribed;
1448
1449	    if (($type eq 'function') || ($type eq 'enum')) {
1450	        print STDERR "Warning(${file}:$.): Function parameter ".
1451		    "or member '$param' not " .
1452		    "described in '$declaration_name'\n";
1453	    }
1454	    print STDERR "Warning(${file}:$.):".
1455	                 " No description found for parameter '$param'\n";
1456	    ++$warnings;
1457        }
1458
1459	push @parameterlist, $param;
1460	$parametertypes{$param} = $type;
1461}
1462
1463##
1464# takes a function prototype and the name of the current file being
1465# processed and spits out all the details stored in the global
1466# arrays/hashes.
1467sub dump_function($$) {
1468    my $prototype = shift;
1469    my $file = shift;
1470
1471    $prototype =~ s/^static +//;
1472    $prototype =~ s/^extern +//;
1473    $prototype =~ s/^fastcall +//;
1474    $prototype =~ s/^asmlinkage +//;
1475    $prototype =~ s/^inline +//;
1476    $prototype =~ s/^__inline__ +//;
1477    $prototype =~ s/^#define +//; #ak added
1478    $prototype =~ s/__attribute__ \(\([a-z,]*\)\)//;
1479
1480    # Yes, this truly is vile.  We are looking for:
1481    # 1. Return type (may be nothing if we're looking at a macro)
1482    # 2. Function name
1483    # 3. Function parameters.
1484    #
1485    # All the while we have to watch out for function pointer parameters
1486    # (which IIRC is what the two sections are for), C types (these
1487    # regexps don't even start to express all the possibilities), and
1488    # so on.
1489    #
1490    # If you mess with these regexps, it's a good idea to check that
1491    # the following functions' documentation still comes out right:
1492    # - parport_register_device (function pointer parameters)
1493    # - atomic_set (macro)
1494    # - pci_match_device (long return type)
1495
1496    if ($prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1497	$prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1498	$prototype =~ m/^(\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1499	$prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1500	$prototype =~ m/^(\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1501	$prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1502	$prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1503	$prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1504	$prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1505	$prototype =~ m/^(\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1506	$prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1507	$prototype =~ m/^(\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1508	$prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1509	$prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/)  {
1510	$return_type = $1;
1511	$declaration_name = $2;
1512	my $args = $3;
1513
1514	create_parameterlist($args, ',', $file);
1515    } else {
1516	print STDERR "Error(${file}:$.): cannot understand prototype: '$prototype'\n";
1517	++$errors;
1518	return;
1519    }
1520
1521    output_declaration($declaration_name,
1522		       'function',
1523		       {'function' => $declaration_name,
1524			'module' => $modulename,
1525			'functiontype' => $return_type,
1526			'parameterlist' => \@parameterlist,
1527			'parameterdescs' => \%parameterdescs,
1528			'parametertypes' => \%parametertypes,
1529			'sectionlist' => \@sectionlist,
1530			'sections' => \%sections,
1531			'purpose' => $declaration_purpose
1532		       });
1533}
1534
1535sub process_file($);
1536
1537# Read the file that maps relative names to absolute names for
1538# separate source and object directories and for shadow trees.
1539if (open(SOURCE_MAP, "<.tmp_filelist.txt")) {
1540	my ($relname, $absname);
1541	while(<SOURCE_MAP>) {
1542		chop();
1543		($relname, $absname) = (split())[0..1];
1544		$relname =~ s:^/+::;
1545		$source_map{$relname} = $absname;
1546	}
1547	close(SOURCE_MAP);
1548}
1549
1550if ($filelist) {
1551	open(FLIST,"<$filelist") or die "Can't open file list $filelist";
1552	while(<FLIST>) {
1553		chop;
1554		process_file($_);
1555	}
1556}
1557
1558foreach (@ARGV) {
1559    chomp;
1560    process_file($_);
1561}
1562if ($verbose && $errors) {
1563  print STDERR "$errors errors\n";
1564}
1565if ($verbose && $warnings) {
1566  print STDERR "$warnings warnings\n";
1567}
1568
1569exit($errors);
1570
1571sub reset_state {
1572    $function = "";
1573    %constants = ();
1574    %parameterdescs = ();
1575    %parametertypes = ();
1576    @parameterlist = ();
1577    %sections = ();
1578    @sectionlist = ();
1579    $prototype = "";
1580
1581    $state = 0;
1582}
1583
1584sub process_state3_function($$) {
1585    my $x = shift;
1586    my $file = shift;
1587
1588    if ($x =~ m#\s*/\*\s+MACDOC\s*#io || ($x =~ /^#/ && $x !~ /^#define/)) {
1589	# do nothing
1590    }
1591    elsif ($x =~ /([^\{]*)/) {
1592        $prototype .= $1;
1593    }
1594    if (($x =~ /\{/) || ($x =~ /\#define/) || ($x =~ /;/)) {
1595        $prototype =~ s@/\*.*?\*/@@gos;	# strip comments.
1596	$prototype =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1597	$prototype =~ s@^\s+@@gos; # strip leading spaces
1598	dump_function($prototype,$file);
1599	reset_state();
1600    }
1601}
1602
1603sub process_state3_type($$) {
1604    my $x = shift;
1605    my $file = shift;
1606
1607    $x =~ s@/\*.*?\*/@@gos;	# strip comments.
1608    $x =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1609    $x =~ s@^\s+@@gos; # strip leading spaces
1610    $x =~ s@\s+$@@gos; # strip trailing spaces
1611    if ($x =~ /^#/) {
1612	# To distinguish preprocessor directive from regular declaration later.
1613	$x .= ";";
1614    }
1615
1616    while (1) {
1617        if ( $x =~ /([^{};]*)([{};])(.*)/ ) {
1618	    $prototype .= $1 . $2;
1619	    ($2 eq '{') && $brcount++;
1620	    ($2 eq '}') && $brcount--;
1621	    if (($2 eq ';') && ($brcount == 0)) {
1622	        dump_declaration($prototype,$file);
1623		reset_state();
1624	        last;
1625	    }
1626	    $x = $3;
1627        } else {
1628	    $prototype .= $x;
1629	    last;
1630	}
1631    }
1632}
1633
1634# replace <, >, and &
1635sub xml_escape($) {
1636	my $text = shift;
1637	$text =~ s/\&/\\\\\\amp;/g;
1638	$text =~ s/\</\\\\\\lt;/g;
1639	$text =~ s/\>/\\\\\\gt;/g;
1640	return $text;
1641}
1642
1643sub process_file($) {
1644    my ($file) = "$ENV{'SRCTREE'}@_";
1645    my $identifier;
1646    my $func;
1647    my $initial_section_counter = $section_counter;
1648
1649    if (defined($source_map{$file})) {
1650	$file = $source_map{$file};
1651    }
1652
1653    if (!open(IN,"<$file")) {
1654	print STDERR "Error: Cannot open file $file\n";
1655	++$errors;
1656	return;
1657    }
1658
1659    $section_counter = 0;
1660    while (<IN>) {
1661	if ($state == 0) {
1662	    if (/$doc_start/o) {
1663		$state = 1;		# next line is always the function name
1664	    }
1665	} elsif ($state == 1) {	# this line is the function name (always)
1666	    if (/$doc_block/o) {
1667		$state = 4;
1668		$contents = "";
1669		if ( $1 eq "" ) {
1670			$section = $section_intro;
1671		} else {
1672			$section = $1;
1673		}
1674            }
1675	    elsif (/$doc_decl/o) {
1676		$identifier = $1;
1677		if (/\s*([\w\s]+?)\s*-/) {
1678		    $identifier = $1;
1679		}
1680
1681		$state = 2;
1682		if (/-(.*)/) {
1683		    $declaration_purpose = xml_escape($1);
1684		} else {
1685		    $declaration_purpose = "";
1686		}
1687		if ($identifier =~ m/^struct/) {
1688		    $decl_type = 'struct';
1689		} elsif ($identifier =~ m/^union/) {
1690		    $decl_type = 'union';
1691		} elsif ($identifier =~ m/^enum/) {
1692		    $decl_type = 'enum';
1693		} elsif ($identifier =~ m/^typedef/) {
1694		    $decl_type = 'typedef';
1695		} else {
1696		    $decl_type = 'function';
1697		}
1698
1699		if ($verbose) {
1700		    print STDERR "Info(${file}:$.): Scanning doc for $identifier\n";
1701		}
1702	    } else {
1703		print STDERR "Warning(${file}:$.): Cannot understand $_ on line $.",
1704		" - I thought it was a doc line\n";
1705		++$warnings;
1706		$state = 0;
1707	    }
1708	} elsif ($state == 2) {	# look for head: lines, and include content
1709	    if (/$doc_sect/o) {
1710		$newsection = $1;
1711		$newcontents = $2;
1712
1713		if ($contents ne "") {
1714		    dump_section($section, xml_escape($contents));
1715		    $section = $section_default;
1716		}
1717
1718		$contents = $newcontents;
1719		if ($contents ne "") {
1720		    $contents .= "\n";
1721		}
1722		$section = $newsection;
1723	    } elsif (/$doc_end/) {
1724
1725		if ($contents ne "") {
1726		    dump_section($section, xml_escape($contents));
1727		    $section = $section_default;
1728		    $contents = "";
1729		}
1730
1731		$prototype = "";
1732		$state = 3;
1733		$brcount = 0;
1734#	    print STDERR "end of doc comment, looking for prototype\n";
1735	    } elsif (/$doc_content/) {
1736		# miguel-style comment kludge, look for blank lines after
1737		# @parameter line to signify start of description
1738		if ($1 eq "" &&
1739			($section =~ m/^@/ || $section eq $section_context)) {
1740		    dump_section($section, xml_escape($contents));
1741		    $section = $section_default;
1742		    $contents = "";
1743		} else {
1744		    $contents .= $1."\n";
1745		}
1746	    } else {
1747		# i dont know - bad line?  ignore.
1748		print STDERR "Warning(${file}:$.): bad line: $_";
1749		++$warnings;
1750	    }
1751	} elsif ($state == 3) {	# scanning for function { (end of prototype)
1752	    if ($decl_type eq 'function') {
1753	        process_state3_function($_, $file);
1754	    } else {
1755	        process_state3_type($_, $file);
1756	    }
1757	} elsif ($state == 4) {
1758		# Documentation block
1759	        if (/$doc_block/) {
1760			dump_section($section, $contents);
1761			output_intro({'sectionlist' => \@sectionlist,
1762				      'sections' => \%sections });
1763			$contents = "";
1764			$function = "";
1765			%constants = ();
1766			%parameterdescs = ();
1767			%parametertypes = ();
1768			@parameterlist = ();
1769			%sections = ();
1770			@sectionlist = ();
1771			$prototype = "";
1772			if ( $1 eq "" ) {
1773				$section = $section_intro;
1774			} else {
1775				$section = $1;
1776			}
1777                }
1778		elsif (/$doc_end/)
1779		{
1780			dump_section($section, $contents);
1781			output_intro({'sectionlist' => \@sectionlist,
1782				      'sections' => \%sections });
1783			$contents = "";
1784			$function = "";
1785			%constants = ();
1786			%parameterdescs = ();
1787			%parametertypes = ();
1788			@parameterlist = ();
1789			%sections = ();
1790			@sectionlist = ();
1791			$prototype = "";
1792			$state = 0;
1793		}
1794		elsif (/$doc_content/)
1795		{
1796			if ( $1 eq "" )
1797			{
1798				$contents .= $blankline;
1799			}
1800			else
1801			{
1802				$contents .= $1 . "\n";
1803			}
1804        	}
1805          }
1806    }
1807    if ($initial_section_counter == $section_counter) {
1808	print STDERR "Warning(${file}): no structured comments found\n";
1809	if ($output_mode eq "xml") {
1810	    # The template wants at least one RefEntry here; make one.
1811	    print "<refentry>\n";
1812	    print " <refnamediv>\n";
1813	    print "  <refname>\n";
1814	    print "   ${file}\n";
1815	    print "  </refname>\n";
1816	    print "  <refpurpose>\n";
1817	    print "   Document generation inconsistency\n";
1818	    print "  </refpurpose>\n";
1819	    print " </refnamediv>\n";
1820	    print " <refsect1>\n";
1821	    print "  <title>\n";
1822	    print "   Oops\n";
1823	    print "  </title>\n";
1824	    print "  <warning>\n";
1825	    print "   <para>\n";
1826	    print "    The template for this document tried to insert\n";
1827	    print "    the structured comment from the file\n";
1828	    print "    <filename>${file}</filename> at this point,\n";
1829	    print "    but none was found.\n";
1830	    print "    This dummy section is inserted to allow\n";
1831	    print "    generation to continue.\n";
1832	    print "   </para>\n";
1833	    print "  </warning>\n";
1834	    print " </refsect1>\n";
1835	    print "</refentry>\n";
1836	}
1837    }
1838}
1839