xref: /linux/scripts/kernel-doc (revision eda603f6cdba4b14dbf80531fab2fe545232e7a0)
11da177e4SLinus Torvalds#!/usr/bin/perl -w
21da177e4SLinus Torvalds
31da177e4SLinus Torvaldsuse strict;
41da177e4SLinus Torvalds
51da177e4SLinus Torvalds## Copyright (c) 1998 Michael Zucchi, All Rights Reserved        ##
61da177e4SLinus Torvalds## Copyright (C) 2000, 1  Tim Waugh <twaugh@redhat.com>          ##
71da177e4SLinus Torvalds## Copyright (C) 2001  Simon Huggins                             ##
8b9d97328SRandy Dunlap## Copyright (C) 2005-2009  Randy Dunlap                         ##
91da177e4SLinus Torvalds## 								 ##
101da177e4SLinus Torvalds## #define enhancements by Armin Kuster <akuster@mvista.com>	 ##
111da177e4SLinus Torvalds## Copyright (c) 2000 MontaVista Software, Inc.			 ##
121da177e4SLinus Torvalds## 								 ##
131da177e4SLinus Torvalds## This software falls under the GNU General Public License.     ##
141da177e4SLinus Torvalds## Please read the COPYING file for more information             ##
151da177e4SLinus Torvalds
161da177e4SLinus Torvalds# 18/01/2001 - 	Cleanups
171da177e4SLinus Torvalds# 		Functions prototyped as foo(void) same as foo()
181da177e4SLinus Torvalds# 		Stop eval'ing where we don't need to.
191da177e4SLinus Torvalds# -- huggie@earth.li
201da177e4SLinus Torvalds
211da177e4SLinus Torvalds# 27/06/2001 -  Allowed whitespace after initial "/**" and
221da177e4SLinus Torvalds#               allowed comments before function declarations.
231da177e4SLinus Torvalds# -- Christian Kreibich <ck@whoop.org>
241da177e4SLinus Torvalds
251da177e4SLinus Torvalds# Still to do:
261da177e4SLinus Torvalds# 	- add perldoc documentation
271da177e4SLinus Torvalds# 	- Look more closely at some of the scarier bits :)
281da177e4SLinus Torvalds
291da177e4SLinus Torvalds# 26/05/2001 - 	Support for separate source and object trees.
301da177e4SLinus Torvalds#		Return error code.
311da177e4SLinus Torvalds# 		Keith Owens <kaos@ocs.com.au>
321da177e4SLinus Torvalds
331da177e4SLinus Torvalds# 23/09/2001 - Added support for typedefs, structs, enums and unions
341da177e4SLinus Torvalds#              Support for Context section; can be terminated using empty line
351da177e4SLinus Torvalds#              Small fixes (like spaces vs. \s in regex)
361da177e4SLinus Torvalds# -- Tim Jansen <tim@tjansen.de>
371da177e4SLinus Torvalds
381da177e4SLinus Torvalds
391da177e4SLinus Torvalds#
401da177e4SLinus Torvalds# This will read a 'c' file and scan for embedded comments in the
411da177e4SLinus Torvalds# style of gnome comments (+minor extensions - see below).
421da177e4SLinus Torvalds#
431da177e4SLinus Torvalds
441da177e4SLinus Torvalds# Note: This only supports 'c'.
451da177e4SLinus Torvalds
461da177e4SLinus Torvalds# usage:
47*eda603f6SJohannes Berg# kernel-doc [ -docbook | -html | -text | -man | -list ] [ -no-doc-sections ]
481da177e4SLinus Torvalds#           [ -function funcname [ -function funcname ...] ] c file(s)s > outputfile
491da177e4SLinus Torvalds# or
501da177e4SLinus Torvalds#           [ -nofunction funcname [ -function funcname ...] ] c file(s)s > outputfile
511da177e4SLinus Torvalds#
521da177e4SLinus Torvalds#  Set output format using one of -docbook -html -text or -man.  Default is man.
53*eda603f6SJohannes Berg#  The -list format is for internal use by docproc.
541da177e4SLinus Torvalds#
554b44595aSJohannes Berg#  -no-doc-sections
564b44595aSJohannes Berg#	Do not output DOC: sections
574b44595aSJohannes Berg#
581da177e4SLinus Torvalds#  -function funcname
59b112e0f7SJohannes Berg#	If set, then only generate documentation for the given function(s) or
60b112e0f7SJohannes Berg#	DOC: section titles.  All other functions and DOC: sections are ignored.
611da177e4SLinus Torvalds#
621da177e4SLinus Torvalds#  -nofunction funcname
63b112e0f7SJohannes Berg#	If set, then only generate documentation for the other function(s)/DOC:
64b112e0f7SJohannes Berg#	sections. Cannot be used together with -function (yes, that's a bug --
65b112e0f7SJohannes Berg#	perl hackers can fix it 8))
661da177e4SLinus Torvalds#
671da177e4SLinus Torvalds#  c files - list of 'c' files to process
681da177e4SLinus Torvalds#
691da177e4SLinus Torvalds#  All output goes to stdout, with errors to stderr.
701da177e4SLinus Torvalds
711da177e4SLinus Torvalds#
721da177e4SLinus Torvalds# format of comments.
731da177e4SLinus Torvalds# In the following table, (...)? signifies optional structure.
741da177e4SLinus Torvalds#                         (...)* signifies 0 or more structure elements
751da177e4SLinus Torvalds# /**
761da177e4SLinus Torvalds#  * function_name(:)? (- short description)?
771da177e4SLinus Torvalds# (* @parameterx: (description of parameter x)?)*
781da177e4SLinus Torvalds# (* a blank line)?
791da177e4SLinus Torvalds#  * (Description:)? (Description of function)?
801da177e4SLinus Torvalds#  * (section header: (section description)? )*
811da177e4SLinus Torvalds#  (*)?*/
821da177e4SLinus Torvalds#
831da177e4SLinus Torvalds# So .. the trivial example would be:
841da177e4SLinus Torvalds#
851da177e4SLinus Torvalds# /**
861da177e4SLinus Torvalds#  * my_function
87b9d97328SRandy Dunlap#  */
881da177e4SLinus Torvalds#
89891dcd2fSRandy Dunlap# If the Description: header tag is omitted, then there must be a blank line
901da177e4SLinus Torvalds# after the last parameter specification.
911da177e4SLinus Torvalds# e.g.
921da177e4SLinus Torvalds# /**
931da177e4SLinus Torvalds#  * my_function - does my stuff
941da177e4SLinus Torvalds#  * @my_arg: its mine damnit
951da177e4SLinus Torvalds#  *
961da177e4SLinus Torvalds#  * Does my stuff explained.
971da177e4SLinus Torvalds#  */
981da177e4SLinus Torvalds#
991da177e4SLinus Torvalds#  or, could also use:
1001da177e4SLinus Torvalds# /**
1011da177e4SLinus Torvalds#  * my_function - does my stuff
1021da177e4SLinus Torvalds#  * @my_arg: its mine damnit
1031da177e4SLinus Torvalds#  * Description: Does my stuff explained.
1041da177e4SLinus Torvalds#  */
1051da177e4SLinus Torvalds# etc.
1061da177e4SLinus Torvalds#
107b9d97328SRandy Dunlap# Besides functions you can also write documentation for structs, unions,
1081da177e4SLinus Torvalds# enums and typedefs. Instead of the function name you must write the name
1091da177e4SLinus Torvalds# of the declaration;  the struct/union/enum/typedef must always precede
1101da177e4SLinus Torvalds# the name. Nesting of declarations is not supported.
1111da177e4SLinus Torvalds# Use the argument mechanism to document members or constants.
1121da177e4SLinus Torvalds# e.g.
1131da177e4SLinus Torvalds# /**
1141da177e4SLinus Torvalds#  * struct my_struct - short description
1151da177e4SLinus Torvalds#  * @a: first member
1161da177e4SLinus Torvalds#  * @b: second member
1171da177e4SLinus Torvalds#  *
1181da177e4SLinus Torvalds#  * Longer description
1191da177e4SLinus Torvalds#  */
1201da177e4SLinus Torvalds# struct my_struct {
1211da177e4SLinus Torvalds#     int a;
1221da177e4SLinus Torvalds#     int b;
123aeec46b9SMartin Waitz# /* private: */
124aeec46b9SMartin Waitz#     int c;
1251da177e4SLinus Torvalds# };
1261da177e4SLinus Torvalds#
1271da177e4SLinus Torvalds# All descriptions can be multiline, except the short function description.
1281da177e4SLinus Torvalds#
1291da177e4SLinus Torvalds# You can also add additional sections. When documenting kernel functions you
1301da177e4SLinus Torvalds# should document the "Context:" of the function, e.g. whether the functions
1311da177e4SLinus Torvalds# can be called form interrupts. Unlike other sections you can end it with an
1321da177e4SLinus Torvalds# empty line.
1331da177e4SLinus Torvalds# Example-sections should contain the string EXAMPLE so that they are marked
1341da177e4SLinus Torvalds# appropriately in DocBook.
1351da177e4SLinus Torvalds#
1361da177e4SLinus Torvalds# Example:
1371da177e4SLinus Torvalds# /**
1381da177e4SLinus Torvalds#  * user_function - function that can only be called in user context
1391da177e4SLinus Torvalds#  * @a: some argument
1401da177e4SLinus Torvalds#  * Context: !in_interrupt()
1411da177e4SLinus Torvalds#  *
1421da177e4SLinus Torvalds#  * Some description
1431da177e4SLinus Torvalds#  * Example:
1441da177e4SLinus Torvalds#  *    user_function(22);
1451da177e4SLinus Torvalds#  */
1461da177e4SLinus Torvalds# ...
1471da177e4SLinus Torvalds#
1481da177e4SLinus Torvalds#
1491da177e4SLinus Torvalds# All descriptive text is further processed, scanning for the following special
1501da177e4SLinus Torvalds# patterns, which are highlighted appropriately.
1511da177e4SLinus Torvalds#
1521da177e4SLinus Torvalds# 'funcname()' - function
1531da177e4SLinus Torvalds# '$ENVVAR' - environmental variable
1541da177e4SLinus Torvalds# '&struct_name' - name of a structure (up to two words including 'struct')
1551da177e4SLinus Torvalds# '@parameter' - name of a parameter
1561da177e4SLinus Torvalds# '%CONST' - name of a constant.
1571da177e4SLinus Torvalds
1581da177e4SLinus Torvaldsmy $errors = 0;
1591da177e4SLinus Torvaldsmy $warnings = 0;
1605f8c7c98SRandy Dunlapmy $anon_struct_union = 0;
1611da177e4SLinus Torvalds
1621da177e4SLinus Torvalds# match expressions used to find embedded type information
1631da177e4SLinus Torvaldsmy $type_constant = '\%([-_\w]+)';
1641da177e4SLinus Torvaldsmy $type_func = '(\w+)\(\)';
1651da177e4SLinus Torvaldsmy $type_param = '\@(\w+)';
1663eb014a1SRandy Dunlapmy $type_struct = '\&((struct\s*)*[_\w]+)';
1676b5b55f6SRandy Dunlapmy $type_struct_xml = '\\&amp;((struct\s*)*[_\w]+)';
1681da177e4SLinus Torvaldsmy $type_env = '(\$\w+)';
1691da177e4SLinus Torvalds
1701da177e4SLinus Torvalds# Output conversion substitutions.
1711da177e4SLinus Torvalds#  One for each output format
1721da177e4SLinus Torvalds
1731da177e4SLinus Torvalds# these work fairly well
1741da177e4SLinus Torvaldsmy %highlights_html = ( $type_constant, "<i>\$1</i>",
1751da177e4SLinus Torvalds			$type_func, "<b>\$1</b>",
1763eb014a1SRandy Dunlap			$type_struct_xml, "<i>\$1</i>",
1773eb014a1SRandy Dunlap			$type_env, "<b><i>\$1</i></b>",
1781da177e4SLinus Torvalds			$type_param, "<tt><b>\$1</b></tt>" );
1796b5b55f6SRandy Dunlapmy $local_lt = "\\\\\\\\lt:";
1806b5b55f6SRandy Dunlapmy $local_gt = "\\\\\\\\gt:";
1816b5b55f6SRandy Dunlapmy $blankline_html = $local_lt . "p" . $local_gt;	# was "<p>"
1821da177e4SLinus Torvalds
1831da177e4SLinus Torvalds# XML, docbook format
1841da177e4SLinus Torvaldsmy %highlights_xml = ( "([^=])\\\"([^\\\"<]+)\\\"", "\$1<quote>\$2</quote>",
1851da177e4SLinus Torvalds			$type_constant, "<constant>\$1</constant>",
1861da177e4SLinus Torvalds			$type_func, "<function>\$1</function>",
1875c98fc03SJohannes Berg			$type_struct_xml, "<structname>\$1</structname>",
1881da177e4SLinus Torvalds			$type_env, "<envar>\$1</envar>",
1891da177e4SLinus Torvalds			$type_param, "<parameter>\$1</parameter>" );
1905c98fc03SJohannes Bergmy $blankline_xml = $local_lt . "/para" . $local_gt . $local_lt . "para" . $local_gt . "\n";
1911da177e4SLinus Torvalds
1921da177e4SLinus Torvalds# gnome, docbook format
1931da177e4SLinus Torvaldsmy %highlights_gnome = ( $type_constant, "<replaceable class=\"option\">\$1</replaceable>",
1941da177e4SLinus Torvalds			 $type_func, "<function>\$1</function>",
1951da177e4SLinus Torvalds			 $type_struct, "<structname>\$1</structname>",
1961da177e4SLinus Torvalds			 $type_env, "<envar>\$1</envar>",
1971da177e4SLinus Torvalds			 $type_param, "<parameter>\$1</parameter>" );
1981da177e4SLinus Torvaldsmy $blankline_gnome = "</para><para>\n";
1991da177e4SLinus Torvalds
2001da177e4SLinus Torvalds# these are pretty rough
2011da177e4SLinus Torvaldsmy %highlights_man = ( $type_constant, "\$1",
2021da177e4SLinus Torvalds		       $type_func, "\\\\fB\$1\\\\fP",
2031da177e4SLinus Torvalds		       $type_struct, "\\\\fI\$1\\\\fP",
2041da177e4SLinus Torvalds		       $type_param, "\\\\fI\$1\\\\fP" );
2051da177e4SLinus Torvaldsmy $blankline_man = "";
2061da177e4SLinus Torvalds
2071da177e4SLinus Torvalds# text-mode
2081da177e4SLinus Torvaldsmy %highlights_text = ( $type_constant, "\$1",
2091da177e4SLinus Torvalds			$type_func, "\$1",
2101da177e4SLinus Torvalds			$type_struct, "\$1",
2111da177e4SLinus Torvalds			$type_param, "\$1" );
2121da177e4SLinus Torvaldsmy $blankline_text = "";
2131da177e4SLinus Torvalds
214*eda603f6SJohannes Berg# list mode
215*eda603f6SJohannes Bergmy %highlights_list = ( $type_constant, "\$1",
216*eda603f6SJohannes Berg			$type_func, "\$1",
217*eda603f6SJohannes Berg			$type_struct, "\$1",
218*eda603f6SJohannes Berg			$type_param, "\$1" );
219*eda603f6SJohannes Bergmy $blankline_list = "";
2201da177e4SLinus Torvalds
2211da177e4SLinus Torvaldssub usage {
222*eda603f6SJohannes Berg    print "Usage: $0 [ -v ] [ -docbook | -html | -text | -man | -list ]\n";
223*eda603f6SJohannes Berg    print "         [ -no-doc-sections ]\n";
2241da177e4SLinus Torvalds    print "         [ -function funcname [ -function funcname ...] ]\n";
2251da177e4SLinus Torvalds    print "         [ -nofunction funcname [ -nofunction funcname ...] ]\n";
2261da177e4SLinus Torvalds    print "         c source file(s) > outputfile\n";
22777cc23b8SRandy Dunlap    print "         -v : verbose output, more warnings & other info listed\n";
2281da177e4SLinus Torvalds    exit 1;
2291da177e4SLinus Torvalds}
2301da177e4SLinus Torvalds
2311da177e4SLinus Torvalds# read arguments
2321da177e4SLinus Torvaldsif ($#ARGV == -1) {
2331da177e4SLinus Torvalds    usage();
2341da177e4SLinus Torvalds}
2351da177e4SLinus Torvalds
2361da177e4SLinus Torvaldsmy $verbose = 0;
2371da177e4SLinus Torvaldsmy $output_mode = "man";
2384b44595aSJohannes Bergmy $no_doc_sections = 0;
2391da177e4SLinus Torvaldsmy %highlights = %highlights_man;
2401da177e4SLinus Torvaldsmy $blankline = $blankline_man;
2411da177e4SLinus Torvaldsmy $modulename = "Kernel API";
2421da177e4SLinus Torvaldsmy $function_only = 0;
2431da177e4SLinus Torvaldsmy $man_date = ('January', 'February', 'March', 'April', 'May', 'June',
2441da177e4SLinus Torvalds		'July', 'August', 'September', 'October',
2451da177e4SLinus Torvalds		'November', 'December')[(localtime)[4]] .
2461da177e4SLinus Torvalds  " " . ((localtime)[5]+1900);
2471da177e4SLinus Torvalds
2481da177e4SLinus Torvalds# Essentially these are globals
249b9d97328SRandy Dunlap# They probably want to be tidied up, made more localised or something.
250b9d97328SRandy Dunlap# CAVEAT EMPTOR!  Some of the others I localised may not want to be, which
2511da177e4SLinus Torvalds# could cause "use of undefined value" or other bugs.
2521da177e4SLinus Torvaldsmy ($function, %function_table, %parametertypes, $declaration_purpose);
2531da177e4SLinus Torvaldsmy ($type, $declaration_name, $return_type);
2541c32fd0cSIlya Dryomovmy ($newsection, $newcontents, $prototype, $brcount, %source_map);
2551da177e4SLinus Torvalds
256bd0e88e5SRandy Dunlapif (defined($ENV{'KBUILD_VERBOSE'})) {
257bd0e88e5SRandy Dunlap	$verbose = "$ENV{'KBUILD_VERBOSE'}";
258bd0e88e5SRandy Dunlap}
259bd0e88e5SRandy Dunlap
2601da177e4SLinus Torvalds# Generated docbook code is inserted in a template at a point where
2611da177e4SLinus Torvalds# docbook v3.1 requires a non-zero sequence of RefEntry's; see:
2621da177e4SLinus Torvalds# http://www.oasis-open.org/docbook/documentation/reference/html/refentry.html
2631da177e4SLinus Torvalds# We keep track of number of generated entries and generate a dummy
2641da177e4SLinus Torvalds# if needs be to ensure the expanded template can be postprocessed
2651da177e4SLinus Torvalds# into html.
2661da177e4SLinus Torvaldsmy $section_counter = 0;
2671da177e4SLinus Torvalds
2681da177e4SLinus Torvaldsmy $lineprefix="";
2691da177e4SLinus Torvalds
2701da177e4SLinus Torvalds# states
2711da177e4SLinus Torvalds# 0 - normal code
2721da177e4SLinus Torvalds# 1 - looking for function name
2731da177e4SLinus Torvalds# 2 - scanning field start.
2741da177e4SLinus Torvalds# 3 - scanning prototype.
2751da177e4SLinus Torvalds# 4 - documentation block
2761da177e4SLinus Torvaldsmy $state;
277850622dfSRandy Dunlapmy $in_doc_sect;
2781da177e4SLinus Torvalds
2791da177e4SLinus Torvalds#declaration types: can be
2801da177e4SLinus Torvalds# 'function', 'struct', 'union', 'enum', 'typedef'
2811da177e4SLinus Torvaldsmy $decl_type;
2821da177e4SLinus Torvalds
2831da177e4SLinus Torvaldsmy $doc_special = "\@\%\$\&";
2841da177e4SLinus Torvalds
2851da177e4SLinus Torvaldsmy $doc_start = '^/\*\*\s*$'; # Allow whitespace at end of comment start.
2861da177e4SLinus Torvaldsmy $doc_end = '\*/';
2871da177e4SLinus Torvaldsmy $doc_com = '\s*\*\s*';
2881da177e4SLinus Torvaldsmy $doc_decl = $doc_com . '(\w+)';
289891dcd2fSRandy Dunlapmy $doc_sect = $doc_com . '([' . $doc_special . ']?[\w\s]+):(.*)';
2901da177e4SLinus Torvaldsmy $doc_content = $doc_com . '(.*)';
2911da177e4SLinus Torvaldsmy $doc_block = $doc_com . 'DOC:\s*(.*)?';
2921da177e4SLinus Torvalds
2931da177e4SLinus Torvaldsmy %constants;
2941da177e4SLinus Torvaldsmy %parameterdescs;
2951da177e4SLinus Torvaldsmy @parameterlist;
2961da177e4SLinus Torvaldsmy %sections;
2971da177e4SLinus Torvaldsmy @sectionlist;
298a1d94aa5SRandy Dunlapmy $sectcheck;
299a1d94aa5SRandy Dunlapmy $struct_actual;
3001da177e4SLinus Torvalds
3011da177e4SLinus Torvaldsmy $contents = "";
3021da177e4SLinus Torvaldsmy $section_default = "Description";	# default section
3031da177e4SLinus Torvaldsmy $section_intro = "Introduction";
3041da177e4SLinus Torvaldsmy $section = $section_default;
3051da177e4SLinus Torvaldsmy $section_context = "Context";
3061da177e4SLinus Torvalds
3071da177e4SLinus Torvaldsmy $undescribed = "-- undescribed --";
3081da177e4SLinus Torvalds
3091da177e4SLinus Torvaldsreset_state();
3101da177e4SLinus Torvalds
3111da177e4SLinus Torvaldswhile ($ARGV[0] =~ m/^-(.*)/) {
3121da177e4SLinus Torvalds    my $cmd = shift @ARGV;
3131da177e4SLinus Torvalds    if ($cmd eq "-html") {
3141da177e4SLinus Torvalds	$output_mode = "html";
3151da177e4SLinus Torvalds	%highlights = %highlights_html;
3161da177e4SLinus Torvalds	$blankline = $blankline_html;
3171da177e4SLinus Torvalds    } elsif ($cmd eq "-man") {
3181da177e4SLinus Torvalds	$output_mode = "man";
3191da177e4SLinus Torvalds	%highlights = %highlights_man;
3201da177e4SLinus Torvalds	$blankline = $blankline_man;
3211da177e4SLinus Torvalds    } elsif ($cmd eq "-text") {
3221da177e4SLinus Torvalds	$output_mode = "text";
3231da177e4SLinus Torvalds	%highlights = %highlights_text;
3241da177e4SLinus Torvalds	$blankline = $blankline_text;
3251da177e4SLinus Torvalds    } elsif ($cmd eq "-docbook") {
3261da177e4SLinus Torvalds	$output_mode = "xml";
3271da177e4SLinus Torvalds	%highlights = %highlights_xml;
3281da177e4SLinus Torvalds	$blankline = $blankline_xml;
329*eda603f6SJohannes Berg    } elsif ($cmd eq "-list") {
330*eda603f6SJohannes Berg	$output_mode = "list";
331*eda603f6SJohannes Berg	%highlights = %highlights_list;
332*eda603f6SJohannes Berg	$blankline = $blankline_list;
3331da177e4SLinus Torvalds    } elsif ($cmd eq "-gnome") {
3341da177e4SLinus Torvalds	$output_mode = "gnome";
3351da177e4SLinus Torvalds	%highlights = %highlights_gnome;
3361da177e4SLinus Torvalds	$blankline = $blankline_gnome;
3371da177e4SLinus Torvalds    } elsif ($cmd eq "-module") { # not needed for XML, inherits from calling document
3381da177e4SLinus Torvalds	$modulename = shift @ARGV;
3391da177e4SLinus Torvalds    } elsif ($cmd eq "-function") { # to only output specific functions
3401da177e4SLinus Torvalds	$function_only = 1;
3411da177e4SLinus Torvalds	$function = shift @ARGV;
3421da177e4SLinus Torvalds	$function_table{$function} = 1;
3431da177e4SLinus Torvalds    } elsif ($cmd eq "-nofunction") { # to only output specific functions
3441da177e4SLinus Torvalds	$function_only = 2;
3451da177e4SLinus Torvalds	$function = shift @ARGV;
3461da177e4SLinus Torvalds	$function_table{$function} = 1;
3471da177e4SLinus Torvalds    } elsif ($cmd eq "-v") {
3481da177e4SLinus Torvalds	$verbose = 1;
3491da177e4SLinus Torvalds    } elsif (($cmd eq "-h") || ($cmd eq "--help")) {
3501da177e4SLinus Torvalds	usage();
3514b44595aSJohannes Berg    } elsif ($cmd eq '-no-doc-sections') {
3524b44595aSJohannes Berg	    $no_doc_sections = 1;
3531da177e4SLinus Torvalds    }
3541da177e4SLinus Torvalds}
3551da177e4SLinus Torvalds
35653f049faSBorislav Petkov# get kernel version from env
35753f049faSBorislav Petkovsub get_kernel_version() {
3581b9bc22dSJohannes Berg    my $version = 'unknown kernel version';
35953f049faSBorislav Petkov
36053f049faSBorislav Petkov    if (defined($ENV{'KERNELVERSION'})) {
36153f049faSBorislav Petkov	$version = $ENV{'KERNELVERSION'};
36253f049faSBorislav Petkov    }
36353f049faSBorislav Petkov    return $version;
36453f049faSBorislav Petkov}
3650366299bSBorislav Petkovmy $kernelversion = get_kernel_version();
3661da177e4SLinus Torvalds
3671da177e4SLinus Torvalds# generate a sequence of code that will splice in highlighting information
3681da177e4SLinus Torvalds# using the s// operator.
3691da177e4SLinus Torvaldsmy $dohighlight = "";
3701da177e4SLinus Torvaldsforeach my $pattern (keys %highlights) {
3713eb014a1SRandy Dunlap#   print STDERR "scanning pattern:$pattern, highlight:($highlights{$pattern})\n";
3721da177e4SLinus Torvalds    $dohighlight .=  "\$contents =~ s:$pattern:$highlights{$pattern}:gs;\n";
3731da177e4SLinus Torvalds}
3741da177e4SLinus Torvalds
3751da177e4SLinus Torvalds##
3761da177e4SLinus Torvalds# dumps section contents to arrays/hashes intended for that purpose.
3771da177e4SLinus Torvalds#
3781da177e4SLinus Torvaldssub dump_section {
37994dc7ad5SRandy Dunlap    my $file = shift;
3801da177e4SLinus Torvalds    my $name = shift;
3811da177e4SLinus Torvalds    my $contents = join "\n", @_;
3821da177e4SLinus Torvalds
3831da177e4SLinus Torvalds    if ($name =~ m/$type_constant/) {
3841da177e4SLinus Torvalds	$name = $1;
3851da177e4SLinus Torvalds#	print STDERR "constant section '$1' = '$contents'\n";
3861da177e4SLinus Torvalds	$constants{$name} = $contents;
3871da177e4SLinus Torvalds    } elsif ($name =~ m/$type_param/) {
3881da177e4SLinus Torvalds#	print STDERR "parameter def '$1' = '$contents'\n";
3891da177e4SLinus Torvalds	$name = $1;
3901da177e4SLinus Torvalds	$parameterdescs{$name} = $contents;
391a1d94aa5SRandy Dunlap	$sectcheck = $sectcheck . $name . " ";
392ced69090SRandy Dunlap    } elsif ($name eq "@\.\.\.") {
393ced69090SRandy Dunlap#	print STDERR "parameter def '...' = '$contents'\n";
394ced69090SRandy Dunlap	$name = "...";
395ced69090SRandy Dunlap	$parameterdescs{$name} = $contents;
396a1d94aa5SRandy Dunlap	$sectcheck = $sectcheck . $name . " ";
3971da177e4SLinus Torvalds    } else {
3981da177e4SLinus Torvalds#	print STDERR "other section '$name' = '$contents'\n";
39994dc7ad5SRandy Dunlap	if (defined($sections{$name}) && ($sections{$name} ne "")) {
40094dc7ad5SRandy Dunlap		print STDERR "Error(${file}:$.): duplicate section name '$name'\n";
40194dc7ad5SRandy Dunlap		++$errors;
40294dc7ad5SRandy Dunlap	}
4031da177e4SLinus Torvalds	$sections{$name} = $contents;
4041da177e4SLinus Torvalds	push @sectionlist, $name;
4051da177e4SLinus Torvalds    }
4061da177e4SLinus Torvalds}
4071da177e4SLinus Torvalds
4081da177e4SLinus Torvalds##
409b112e0f7SJohannes Berg# dump DOC: section after checking that it should go out
410b112e0f7SJohannes Berg#
411b112e0f7SJohannes Bergsub dump_doc_section {
41294dc7ad5SRandy Dunlap    my $file = shift;
413b112e0f7SJohannes Berg    my $name = shift;
414b112e0f7SJohannes Berg    my $contents = join "\n", @_;
415b112e0f7SJohannes Berg
4164b44595aSJohannes Berg    if ($no_doc_sections) {
4174b44595aSJohannes Berg        return;
4184b44595aSJohannes Berg    }
4194b44595aSJohannes Berg
420b112e0f7SJohannes Berg    if (($function_only == 0) ||
421b112e0f7SJohannes Berg	( $function_only == 1 && defined($function_table{$name})) ||
422b112e0f7SJohannes Berg	( $function_only == 2 && !defined($function_table{$name})))
423b112e0f7SJohannes Berg    {
42494dc7ad5SRandy Dunlap	dump_section($file, $name, $contents);
425b112e0f7SJohannes Berg	output_blockhead({'sectionlist' => \@sectionlist,
426b112e0f7SJohannes Berg			  'sections' => \%sections,
427b112e0f7SJohannes Berg			  'module' => $modulename,
428b112e0f7SJohannes Berg			  'content-only' => ($function_only != 0), });
429b112e0f7SJohannes Berg    }
430b112e0f7SJohannes Berg}
431b112e0f7SJohannes Berg
432b112e0f7SJohannes Berg##
4331da177e4SLinus Torvalds# output function
4341da177e4SLinus Torvalds#
4351da177e4SLinus Torvalds# parameterdescs, a hash.
4361da177e4SLinus Torvalds#  function => "function name"
4371da177e4SLinus Torvalds#  parameterlist => @list of parameters
4381da177e4SLinus Torvalds#  parameterdescs => %parameter descriptions
4391da177e4SLinus Torvalds#  sectionlist => @list of sections
440a21217daSRandy Dunlap#  sections => %section descriptions
4411da177e4SLinus Torvalds#
4421da177e4SLinus Torvalds
4431da177e4SLinus Torvaldssub output_highlight {
4441da177e4SLinus Torvalds    my $contents = join "\n",@_;
4451da177e4SLinus Torvalds    my $line;
4461da177e4SLinus Torvalds
4471da177e4SLinus Torvalds#   DEBUG
4481da177e4SLinus Torvalds#   if (!defined $contents) {
4491da177e4SLinus Torvalds#	use Carp;
4501da177e4SLinus Torvalds#	confess "output_highlight got called with no args?\n";
4511da177e4SLinus Torvalds#   }
4521da177e4SLinus Torvalds
4535c98fc03SJohannes Berg    if ($output_mode eq "html" || $output_mode eq "xml") {
4546b5b55f6SRandy Dunlap	$contents = local_unescape($contents);
4556b5b55f6SRandy Dunlap	# convert data read & converted thru xml_escape() into &xyz; format:
4566b5b55f6SRandy Dunlap	$contents =~ s/\\\\\\/&/g;
4576b5b55f6SRandy Dunlap    }
4583eb014a1SRandy Dunlap#   print STDERR "contents b4:$contents\n";
4591da177e4SLinus Torvalds    eval $dohighlight;
4601da177e4SLinus Torvalds    die $@ if $@;
4613eb014a1SRandy Dunlap#   print STDERR "contents af:$contents\n";
4623eb014a1SRandy Dunlap
4631da177e4SLinus Torvalds    foreach $line (split "\n", $contents) {
4641da177e4SLinus Torvalds	if ($line eq ""){
4656b5b55f6SRandy Dunlap	    print $lineprefix, local_unescape($blankline);
4661da177e4SLinus Torvalds	} else {
4671da177e4SLinus Torvalds	    $line =~ s/\\\\\\/\&/g;
468cdccb316SRandy Dunlap	    if ($output_mode eq "man" && substr($line, 0, 1) eq ".") {
469cdccb316SRandy Dunlap		print "\\&$line";
470cdccb316SRandy Dunlap	    } else {
4711da177e4SLinus Torvalds		print $lineprefix, $line;
4721da177e4SLinus Torvalds	    }
473cdccb316SRandy Dunlap	}
4741da177e4SLinus Torvalds	print "\n";
4751da177e4SLinus Torvalds    }
4761da177e4SLinus Torvalds}
4771da177e4SLinus Torvalds
4781da177e4SLinus Torvalds#output sections in html
4791da177e4SLinus Torvaldssub output_section_html(%) {
4801da177e4SLinus Torvalds    my %args = %{$_[0]};
4811da177e4SLinus Torvalds    my $section;
4821da177e4SLinus Torvalds
4831da177e4SLinus Torvalds    foreach $section (@{$args{'sectionlist'}}) {
4841da177e4SLinus Torvalds	print "<h3>$section</h3>\n";
4851da177e4SLinus Torvalds	print "<blockquote>\n";
4861da177e4SLinus Torvalds	output_highlight($args{'sections'}{$section});
4871da177e4SLinus Torvalds	print "</blockquote>\n";
4881da177e4SLinus Torvalds    }
4891da177e4SLinus Torvalds}
4901da177e4SLinus Torvalds
4911da177e4SLinus Torvalds# output enum in html
4921da177e4SLinus Torvaldssub output_enum_html(%) {
4931da177e4SLinus Torvalds    my %args = %{$_[0]};
4941da177e4SLinus Torvalds    my ($parameter);
4951da177e4SLinus Torvalds    my $count;
4961da177e4SLinus Torvalds    print "<h2>enum " . $args{'enum'} . "</h2>\n";
4971da177e4SLinus Torvalds
4981da177e4SLinus Torvalds    print "<b>enum " . $args{'enum'} . "</b> {<br>\n";
4991da177e4SLinus Torvalds    $count = 0;
5001da177e4SLinus Torvalds    foreach $parameter (@{$args{'parameterlist'}}) {
5011da177e4SLinus Torvalds	print " <b>" . $parameter . "</b>";
5021da177e4SLinus Torvalds	if ($count != $#{$args{'parameterlist'}}) {
5031da177e4SLinus Torvalds	    $count++;
5041da177e4SLinus Torvalds	    print ",\n";
5051da177e4SLinus Torvalds	}
5061da177e4SLinus Torvalds	print "<br>";
5071da177e4SLinus Torvalds    }
5081da177e4SLinus Torvalds    print "};<br>\n";
5091da177e4SLinus Torvalds
5101da177e4SLinus Torvalds    print "<h3>Constants</h3>\n";
5111da177e4SLinus Torvalds    print "<dl>\n";
5121da177e4SLinus Torvalds    foreach $parameter (@{$args{'parameterlist'}}) {
5131da177e4SLinus Torvalds	print "<dt><b>" . $parameter . "</b>\n";
5141da177e4SLinus Torvalds	print "<dd>";
5151da177e4SLinus Torvalds	output_highlight($args{'parameterdescs'}{$parameter});
5161da177e4SLinus Torvalds    }
5171da177e4SLinus Torvalds    print "</dl>\n";
5181da177e4SLinus Torvalds    output_section_html(@_);
5191da177e4SLinus Torvalds    print "<hr>\n";
5201da177e4SLinus Torvalds}
5211da177e4SLinus Torvalds
522d28bee0cSRandy Dunlap# output typedef in html
5231da177e4SLinus Torvaldssub output_typedef_html(%) {
5241da177e4SLinus Torvalds    my %args = %{$_[0]};
5251da177e4SLinus Torvalds    my ($parameter);
5261da177e4SLinus Torvalds    my $count;
5271da177e4SLinus Torvalds    print "<h2>typedef " . $args{'typedef'} . "</h2>\n";
5281da177e4SLinus Torvalds
5291da177e4SLinus Torvalds    print "<b>typedef " . $args{'typedef'} . "</b>\n";
5301da177e4SLinus Torvalds    output_section_html(@_);
5311da177e4SLinus Torvalds    print "<hr>\n";
5321da177e4SLinus Torvalds}
5331da177e4SLinus Torvalds
5341da177e4SLinus Torvalds# output struct in html
5351da177e4SLinus Torvaldssub output_struct_html(%) {
5361da177e4SLinus Torvalds    my %args = %{$_[0]};
5371da177e4SLinus Torvalds    my ($parameter);
5381da177e4SLinus Torvalds
539262d9b01SRandy Dunlap    print "<h2>" . $args{'type'} . " " . $args{'struct'} . " - " . $args{'purpose'} . "</h2>\n";
5401da177e4SLinus Torvalds    print "<b>" . $args{'type'} . " " . $args{'struct'} . "</b> {<br>\n";
5411da177e4SLinus Torvalds    foreach $parameter (@{$args{'parameterlist'}}) {
5421da177e4SLinus Torvalds	if ($parameter =~ /^#/) {
5431da177e4SLinus Torvalds		print "$parameter<br>\n";
5441da177e4SLinus Torvalds		next;
5451da177e4SLinus Torvalds	}
5461da177e4SLinus Torvalds	my $parameter_name = $parameter;
5471da177e4SLinus Torvalds	$parameter_name =~ s/\[.*//;
5481da177e4SLinus Torvalds
5491da177e4SLinus Torvalds	($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
5501da177e4SLinus Torvalds	$type = $args{'parametertypes'}{$parameter};
5511da177e4SLinus Torvalds	if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
5521da177e4SLinus Torvalds	    # pointer-to-function
5533eb014a1SRandy Dunlap	    print "&nbsp; &nbsp; <i>$1</i><b>$parameter</b>) <i>($2)</i>;<br>\n";
5541da177e4SLinus Torvalds	} elsif ($type =~ m/^(.*?)\s*(:.*)/) {
5553eb014a1SRandy Dunlap	    # bitfield
5563eb014a1SRandy Dunlap	    print "&nbsp; &nbsp; <i>$1</i> <b>$parameter</b>$2;<br>\n";
5571da177e4SLinus Torvalds	} else {
5583eb014a1SRandy Dunlap	    print "&nbsp; &nbsp; <i>$type</i> <b>$parameter</b>;<br>\n";
5591da177e4SLinus Torvalds	}
5601da177e4SLinus Torvalds    }
5611da177e4SLinus Torvalds    print "};<br>\n";
5621da177e4SLinus Torvalds
5631da177e4SLinus Torvalds    print "<h3>Members</h3>\n";
5641da177e4SLinus Torvalds    print "<dl>\n";
5651da177e4SLinus Torvalds    foreach $parameter (@{$args{'parameterlist'}}) {
5661da177e4SLinus Torvalds	($parameter =~ /^#/) && next;
5671da177e4SLinus Torvalds
5681da177e4SLinus Torvalds	my $parameter_name = $parameter;
5691da177e4SLinus Torvalds	$parameter_name =~ s/\[.*//;
5701da177e4SLinus Torvalds
5711da177e4SLinus Torvalds	($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
5721da177e4SLinus Torvalds	print "<dt><b>" . $parameter . "</b>\n";
5731da177e4SLinus Torvalds	print "<dd>";
5741da177e4SLinus Torvalds	output_highlight($args{'parameterdescs'}{$parameter_name});
5751da177e4SLinus Torvalds    }
5761da177e4SLinus Torvalds    print "</dl>\n";
5771da177e4SLinus Torvalds    output_section_html(@_);
5781da177e4SLinus Torvalds    print "<hr>\n";
5791da177e4SLinus Torvalds}
5801da177e4SLinus Torvalds
5811da177e4SLinus Torvalds# output function in html
5821da177e4SLinus Torvaldssub output_function_html(%) {
5831da177e4SLinus Torvalds    my %args = %{$_[0]};
5841da177e4SLinus Torvalds    my ($parameter, $section);
5851da177e4SLinus Torvalds    my $count;
5861da177e4SLinus Torvalds
587262d9b01SRandy Dunlap    print "<h2>" . $args{'function'} . " - " . $args{'purpose'} . "</h2>\n";
5881da177e4SLinus Torvalds    print "<i>" . $args{'functiontype'} . "</i>\n";
5891da177e4SLinus Torvalds    print "<b>" . $args{'function'} . "</b>\n";
5901da177e4SLinus Torvalds    print "(";
5911da177e4SLinus Torvalds    $count = 0;
5921da177e4SLinus Torvalds    foreach $parameter (@{$args{'parameterlist'}}) {
5931da177e4SLinus Torvalds	$type = $args{'parametertypes'}{$parameter};
5941da177e4SLinus Torvalds	if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
5951da177e4SLinus Torvalds	    # pointer-to-function
5961da177e4SLinus Torvalds	    print "<i>$1</i><b>$parameter</b>) <i>($2)</i>";
5971da177e4SLinus Torvalds	} else {
5981da177e4SLinus Torvalds	    print "<i>" . $type . "</i> <b>" . $parameter . "</b>";
5991da177e4SLinus Torvalds	}
6001da177e4SLinus Torvalds	if ($count != $#{$args{'parameterlist'}}) {
6011da177e4SLinus Torvalds	    $count++;
6021da177e4SLinus Torvalds	    print ",\n";
6031da177e4SLinus Torvalds	}
6041da177e4SLinus Torvalds    }
6051da177e4SLinus Torvalds    print ")\n";
6061da177e4SLinus Torvalds
6071da177e4SLinus Torvalds    print "<h3>Arguments</h3>\n";
6081da177e4SLinus Torvalds    print "<dl>\n";
6091da177e4SLinus Torvalds    foreach $parameter (@{$args{'parameterlist'}}) {
6101da177e4SLinus Torvalds	my $parameter_name = $parameter;
6111da177e4SLinus Torvalds	$parameter_name =~ s/\[.*//;
6121da177e4SLinus Torvalds
6131da177e4SLinus Torvalds	($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
6141da177e4SLinus Torvalds	print "<dt><b>" . $parameter . "</b>\n";
6151da177e4SLinus Torvalds	print "<dd>";
6161da177e4SLinus Torvalds	output_highlight($args{'parameterdescs'}{$parameter_name});
6171da177e4SLinus Torvalds    }
6181da177e4SLinus Torvalds    print "</dl>\n";
6191da177e4SLinus Torvalds    output_section_html(@_);
6201da177e4SLinus Torvalds    print "<hr>\n";
6211da177e4SLinus Torvalds}
6221da177e4SLinus Torvalds
623b112e0f7SJohannes Berg# output DOC: block header in html
624b112e0f7SJohannes Bergsub output_blockhead_html(%) {
6251da177e4SLinus Torvalds    my %args = %{$_[0]};
6261da177e4SLinus Torvalds    my ($parameter, $section);
6271da177e4SLinus Torvalds    my $count;
6281da177e4SLinus Torvalds
6291da177e4SLinus Torvalds    foreach $section (@{$args{'sectionlist'}}) {
6301da177e4SLinus Torvalds	print "<h3>$section</h3>\n";
6311da177e4SLinus Torvalds	print "<ul>\n";
6321da177e4SLinus Torvalds	output_highlight($args{'sections'}{$section});
6331da177e4SLinus Torvalds	print "</ul>\n";
6341da177e4SLinus Torvalds    }
6351da177e4SLinus Torvalds    print "<hr>\n";
6361da177e4SLinus Torvalds}
6371da177e4SLinus Torvalds
6381da177e4SLinus Torvaldssub output_section_xml(%) {
6391da177e4SLinus Torvalds    my %args = %{$_[0]};
6401da177e4SLinus Torvalds    my $section;
6411da177e4SLinus Torvalds    # print out each section
6421da177e4SLinus Torvalds    $lineprefix="   ";
6431da177e4SLinus Torvalds    foreach $section (@{$args{'sectionlist'}}) {
644c73894c1SRich Walker	print "<refsect1>\n";
645c73894c1SRich Walker	print "<title>$section</title>\n";
6461da177e4SLinus Torvalds	if ($section =~ m/EXAMPLE/i) {
647c73894c1SRich Walker	    print "<informalexample><programlisting>\n";
648c73894c1SRich Walker	} else {
649c73894c1SRich Walker	    print "<para>\n";
6501da177e4SLinus Torvalds	}
6511da177e4SLinus Torvalds	output_highlight($args{'sections'}{$section});
6521da177e4SLinus Torvalds	if ($section =~ m/EXAMPLE/i) {
653c73894c1SRich Walker	    print "</programlisting></informalexample>\n";
654c73894c1SRich Walker	} else {
655c73894c1SRich Walker	    print "</para>\n";
6561da177e4SLinus Torvalds	}
657c73894c1SRich Walker	print "</refsect1>\n";
6581da177e4SLinus Torvalds    }
6591da177e4SLinus Torvalds}
6601da177e4SLinus Torvalds
6611da177e4SLinus Torvalds# output function in XML DocBook
6621da177e4SLinus Torvaldssub output_function_xml(%) {
6631da177e4SLinus Torvalds    my %args = %{$_[0]};
6641da177e4SLinus Torvalds    my ($parameter, $section);
6651da177e4SLinus Torvalds    my $count;
6661da177e4SLinus Torvalds    my $id;
6671da177e4SLinus Torvalds
6681da177e4SLinus Torvalds    $id = "API-" . $args{'function'};
6691da177e4SLinus Torvalds    $id =~ s/[^A-Za-z0-9]/-/g;
6701da177e4SLinus Torvalds
6715449bc94SPavel Pisa    print "<refentry id=\"$id\">\n";
6728b0c2d98SMartin Waitz    print "<refentryinfo>\n";
6738b0c2d98SMartin Waitz    print " <title>LINUX</title>\n";
6748b0c2d98SMartin Waitz    print " <productname>Kernel Hackers Manual</productname>\n";
6758b0c2d98SMartin Waitz    print " <date>$man_date</date>\n";
6768b0c2d98SMartin Waitz    print "</refentryinfo>\n";
6771da177e4SLinus Torvalds    print "<refmeta>\n";
6785449bc94SPavel Pisa    print " <refentrytitle><phrase>" . $args{'function'} . "</phrase></refentrytitle>\n";
6798b0c2d98SMartin Waitz    print " <manvolnum>9</manvolnum>\n";
6800366299bSBorislav Petkov    print " <refmiscinfo class=\"version\">" . $kernelversion . "</refmiscinfo>\n";
6811da177e4SLinus Torvalds    print "</refmeta>\n";
6821da177e4SLinus Torvalds    print "<refnamediv>\n";
6831da177e4SLinus Torvalds    print " <refname>" . $args{'function'} . "</refname>\n";
6841da177e4SLinus Torvalds    print " <refpurpose>\n";
6851da177e4SLinus Torvalds    print "  ";
6861da177e4SLinus Torvalds    output_highlight ($args{'purpose'});
6871da177e4SLinus Torvalds    print " </refpurpose>\n";
6881da177e4SLinus Torvalds    print "</refnamediv>\n";
6891da177e4SLinus Torvalds
6901da177e4SLinus Torvalds    print "<refsynopsisdiv>\n";
6911da177e4SLinus Torvalds    print " <title>Synopsis</title>\n";
6921da177e4SLinus Torvalds    print "  <funcsynopsis><funcprototype>\n";
6931da177e4SLinus Torvalds    print "   <funcdef>" . $args{'functiontype'} . " ";
6941da177e4SLinus Torvalds    print "<function>" . $args{'function'} . " </function></funcdef>\n";
6951da177e4SLinus Torvalds
6961da177e4SLinus Torvalds    $count = 0;
6971da177e4SLinus Torvalds    if ($#{$args{'parameterlist'}} >= 0) {
6981da177e4SLinus Torvalds	foreach $parameter (@{$args{'parameterlist'}}) {
6991da177e4SLinus Torvalds	    $type = $args{'parametertypes'}{$parameter};
7001da177e4SLinus Torvalds	    if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
7011da177e4SLinus Torvalds		# pointer-to-function
7021da177e4SLinus Torvalds		print "   <paramdef>$1<parameter>$parameter</parameter>)\n";
7031da177e4SLinus Torvalds		print "     <funcparams>$2</funcparams></paramdef>\n";
7041da177e4SLinus Torvalds	    } else {
7051da177e4SLinus Torvalds		print "   <paramdef>" . $type;
7061da177e4SLinus Torvalds		print " <parameter>$parameter</parameter></paramdef>\n";
7071da177e4SLinus Torvalds	    }
7081da177e4SLinus Torvalds	}
7091da177e4SLinus Torvalds    } else {
7106013d544SMartin Waitz	print "  <void/>\n";
7111da177e4SLinus Torvalds    }
7121da177e4SLinus Torvalds    print "  </funcprototype></funcsynopsis>\n";
7131da177e4SLinus Torvalds    print "</refsynopsisdiv>\n";
7141da177e4SLinus Torvalds
7151da177e4SLinus Torvalds    # print parameters
7161da177e4SLinus Torvalds    print "<refsect1>\n <title>Arguments</title>\n";
7171da177e4SLinus Torvalds    if ($#{$args{'parameterlist'}} >= 0) {
7181da177e4SLinus Torvalds	print " <variablelist>\n";
7191da177e4SLinus Torvalds	foreach $parameter (@{$args{'parameterlist'}}) {
7201da177e4SLinus Torvalds	    my $parameter_name = $parameter;
7211da177e4SLinus Torvalds	    $parameter_name =~ s/\[.*//;
7221da177e4SLinus Torvalds
7231da177e4SLinus Torvalds	    print "  <varlistentry>\n   <term><parameter>$parameter</parameter></term>\n";
7241da177e4SLinus Torvalds	    print "   <listitem>\n    <para>\n";
7251da177e4SLinus Torvalds	    $lineprefix="     ";
7261da177e4SLinus Torvalds	    output_highlight($args{'parameterdescs'}{$parameter_name});
7271da177e4SLinus Torvalds	    print "    </para>\n   </listitem>\n  </varlistentry>\n";
7281da177e4SLinus Torvalds	}
7291da177e4SLinus Torvalds	print " </variablelist>\n";
7301da177e4SLinus Torvalds    } else {
7311da177e4SLinus Torvalds	print " <para>\n  None\n </para>\n";
7321da177e4SLinus Torvalds    }
7331da177e4SLinus Torvalds    print "</refsect1>\n";
7341da177e4SLinus Torvalds
7351da177e4SLinus Torvalds    output_section_xml(@_);
7361da177e4SLinus Torvalds    print "</refentry>\n\n";
7371da177e4SLinus Torvalds}
7381da177e4SLinus Torvalds
7391da177e4SLinus Torvalds# output struct in XML DocBook
7401da177e4SLinus Torvaldssub output_struct_xml(%) {
7411da177e4SLinus Torvalds    my %args = %{$_[0]};
7421da177e4SLinus Torvalds    my ($parameter, $section);
7431da177e4SLinus Torvalds    my $id;
7441da177e4SLinus Torvalds
7451da177e4SLinus Torvalds    $id = "API-struct-" . $args{'struct'};
7461da177e4SLinus Torvalds    $id =~ s/[^A-Za-z0-9]/-/g;
7471da177e4SLinus Torvalds
7485449bc94SPavel Pisa    print "<refentry id=\"$id\">\n";
7498b0c2d98SMartin Waitz    print "<refentryinfo>\n";
7508b0c2d98SMartin Waitz    print " <title>LINUX</title>\n";
7518b0c2d98SMartin Waitz    print " <productname>Kernel Hackers Manual</productname>\n";
7528b0c2d98SMartin Waitz    print " <date>$man_date</date>\n";
7538b0c2d98SMartin Waitz    print "</refentryinfo>\n";
7541da177e4SLinus Torvalds    print "<refmeta>\n";
7555449bc94SPavel Pisa    print " <refentrytitle><phrase>" . $args{'type'} . " " . $args{'struct'} . "</phrase></refentrytitle>\n";
7568b0c2d98SMartin Waitz    print " <manvolnum>9</manvolnum>\n";
7570366299bSBorislav Petkov    print " <refmiscinfo class=\"version\">" . $kernelversion . "</refmiscinfo>\n";
7581da177e4SLinus Torvalds    print "</refmeta>\n";
7591da177e4SLinus Torvalds    print "<refnamediv>\n";
7601da177e4SLinus Torvalds    print " <refname>" . $args{'type'} . " " . $args{'struct'} . "</refname>\n";
7611da177e4SLinus Torvalds    print " <refpurpose>\n";
7621da177e4SLinus Torvalds    print "  ";
7631da177e4SLinus Torvalds    output_highlight ($args{'purpose'});
7641da177e4SLinus Torvalds    print " </refpurpose>\n";
7651da177e4SLinus Torvalds    print "</refnamediv>\n";
7661da177e4SLinus Torvalds
7671da177e4SLinus Torvalds    print "<refsynopsisdiv>\n";
7681da177e4SLinus Torvalds    print " <title>Synopsis</title>\n";
7691da177e4SLinus Torvalds    print "  <programlisting>\n";
7701da177e4SLinus Torvalds    print $args{'type'} . " " . $args{'struct'} . " {\n";
7711da177e4SLinus Torvalds    foreach $parameter (@{$args{'parameterlist'}}) {
7721da177e4SLinus Torvalds	if ($parameter =~ /^#/) {
7731da177e4SLinus Torvalds	    print "$parameter\n";
7741da177e4SLinus Torvalds	    next;
7751da177e4SLinus Torvalds	}
7761da177e4SLinus Torvalds
7771da177e4SLinus Torvalds	my $parameter_name = $parameter;
7781da177e4SLinus Torvalds	$parameter_name =~ s/\[.*//;
7791da177e4SLinus Torvalds
7801da177e4SLinus Torvalds	defined($args{'parameterdescs'}{$parameter_name}) || next;
7811da177e4SLinus Torvalds	($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
7821da177e4SLinus Torvalds	$type = $args{'parametertypes'}{$parameter};
7831da177e4SLinus Torvalds	if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
7841da177e4SLinus Torvalds	    # pointer-to-function
7851da177e4SLinus Torvalds	    print "  $1 $parameter) ($2);\n";
7861da177e4SLinus Torvalds	} elsif ($type =~ m/^(.*?)\s*(:.*)/) {
78751f5a0c8SRandy Dunlap	    # bitfield
7881da177e4SLinus Torvalds	    print "  $1 $parameter$2;\n";
7891da177e4SLinus Torvalds	} else {
7901da177e4SLinus Torvalds	    print "  " . $type . " " . $parameter . ";\n";
7911da177e4SLinus Torvalds	}
7921da177e4SLinus Torvalds    }
7931da177e4SLinus Torvalds    print "};";
7941da177e4SLinus Torvalds    print "  </programlisting>\n";
7951da177e4SLinus Torvalds    print "</refsynopsisdiv>\n";
7961da177e4SLinus Torvalds
7971da177e4SLinus Torvalds    print " <refsect1>\n";
7981da177e4SLinus Torvalds    print "  <title>Members</title>\n";
7991da177e4SLinus Torvalds
80039f00c08SRandy Dunlap    if ($#{$args{'parameterlist'}} >= 0) {
8011da177e4SLinus Torvalds    print "  <variablelist>\n";
8021da177e4SLinus Torvalds    foreach $parameter (@{$args{'parameterlist'}}) {
8031da177e4SLinus Torvalds      ($parameter =~ /^#/) && next;
8041da177e4SLinus Torvalds
8051da177e4SLinus Torvalds      my $parameter_name = $parameter;
8061da177e4SLinus Torvalds      $parameter_name =~ s/\[.*//;
8071da177e4SLinus Torvalds
8081da177e4SLinus Torvalds      defined($args{'parameterdescs'}{$parameter_name}) || next;
8091da177e4SLinus Torvalds      ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
8101da177e4SLinus Torvalds      print "    <varlistentry>";
8111da177e4SLinus Torvalds      print "      <term>$parameter</term>\n";
8121da177e4SLinus Torvalds      print "      <listitem><para>\n";
8131da177e4SLinus Torvalds      output_highlight($args{'parameterdescs'}{$parameter_name});
8141da177e4SLinus Torvalds      print "      </para></listitem>\n";
8151da177e4SLinus Torvalds      print "    </varlistentry>\n";
8161da177e4SLinus Torvalds    }
8171da177e4SLinus Torvalds    print "  </variablelist>\n";
81839f00c08SRandy Dunlap    } else {
81939f00c08SRandy Dunlap	print " <para>\n  None\n </para>\n";
82039f00c08SRandy Dunlap    }
8211da177e4SLinus Torvalds    print " </refsect1>\n";
8221da177e4SLinus Torvalds
8231da177e4SLinus Torvalds    output_section_xml(@_);
8241da177e4SLinus Torvalds
8251da177e4SLinus Torvalds    print "</refentry>\n\n";
8261da177e4SLinus Torvalds}
8271da177e4SLinus Torvalds
8281da177e4SLinus Torvalds# output enum in XML DocBook
8291da177e4SLinus Torvaldssub output_enum_xml(%) {
8301da177e4SLinus Torvalds    my %args = %{$_[0]};
8311da177e4SLinus Torvalds    my ($parameter, $section);
8321da177e4SLinus Torvalds    my $count;
8331da177e4SLinus Torvalds    my $id;
8341da177e4SLinus Torvalds
8351da177e4SLinus Torvalds    $id = "API-enum-" . $args{'enum'};
8361da177e4SLinus Torvalds    $id =~ s/[^A-Za-z0-9]/-/g;
8371da177e4SLinus Torvalds
8385449bc94SPavel Pisa    print "<refentry id=\"$id\">\n";
8398b0c2d98SMartin Waitz    print "<refentryinfo>\n";
8408b0c2d98SMartin Waitz    print " <title>LINUX</title>\n";
8418b0c2d98SMartin Waitz    print " <productname>Kernel Hackers Manual</productname>\n";
8428b0c2d98SMartin Waitz    print " <date>$man_date</date>\n";
8438b0c2d98SMartin Waitz    print "</refentryinfo>\n";
8441da177e4SLinus Torvalds    print "<refmeta>\n";
8455449bc94SPavel Pisa    print " <refentrytitle><phrase>enum " . $args{'enum'} . "</phrase></refentrytitle>\n";
8468b0c2d98SMartin Waitz    print " <manvolnum>9</manvolnum>\n";
8470366299bSBorislav Petkov    print " <refmiscinfo class=\"version\">" . $kernelversion . "</refmiscinfo>\n";
8481da177e4SLinus Torvalds    print "</refmeta>\n";
8491da177e4SLinus Torvalds    print "<refnamediv>\n";
8501da177e4SLinus Torvalds    print " <refname>enum " . $args{'enum'} . "</refname>\n";
8511da177e4SLinus Torvalds    print " <refpurpose>\n";
8521da177e4SLinus Torvalds    print "  ";
8531da177e4SLinus Torvalds    output_highlight ($args{'purpose'});
8541da177e4SLinus Torvalds    print " </refpurpose>\n";
8551da177e4SLinus Torvalds    print "</refnamediv>\n";
8561da177e4SLinus Torvalds
8571da177e4SLinus Torvalds    print "<refsynopsisdiv>\n";
8581da177e4SLinus Torvalds    print " <title>Synopsis</title>\n";
8591da177e4SLinus Torvalds    print "  <programlisting>\n";
8601da177e4SLinus Torvalds    print "enum " . $args{'enum'} . " {\n";
8611da177e4SLinus Torvalds    $count = 0;
8621da177e4SLinus Torvalds    foreach $parameter (@{$args{'parameterlist'}}) {
8631da177e4SLinus Torvalds	print "  $parameter";
8641da177e4SLinus Torvalds	if ($count != $#{$args{'parameterlist'}}) {
8651da177e4SLinus Torvalds	    $count++;
8661da177e4SLinus Torvalds	    print ",";
8671da177e4SLinus Torvalds	}
8681da177e4SLinus Torvalds	print "\n";
8691da177e4SLinus Torvalds    }
8701da177e4SLinus Torvalds    print "};";
8711da177e4SLinus Torvalds    print "  </programlisting>\n";
8721da177e4SLinus Torvalds    print "</refsynopsisdiv>\n";
8731da177e4SLinus Torvalds
8741da177e4SLinus Torvalds    print "<refsect1>\n";
8751da177e4SLinus Torvalds    print " <title>Constants</title>\n";
8761da177e4SLinus Torvalds    print "  <variablelist>\n";
8771da177e4SLinus Torvalds    foreach $parameter (@{$args{'parameterlist'}}) {
8781da177e4SLinus Torvalds      my $parameter_name = $parameter;
8791da177e4SLinus Torvalds      $parameter_name =~ s/\[.*//;
8801da177e4SLinus Torvalds
8811da177e4SLinus Torvalds      print "    <varlistentry>";
8821da177e4SLinus Torvalds      print "      <term>$parameter</term>\n";
8831da177e4SLinus Torvalds      print "      <listitem><para>\n";
8841da177e4SLinus Torvalds      output_highlight($args{'parameterdescs'}{$parameter_name});
8851da177e4SLinus Torvalds      print "      </para></listitem>\n";
8861da177e4SLinus Torvalds      print "    </varlistentry>\n";
8871da177e4SLinus Torvalds    }
8881da177e4SLinus Torvalds    print "  </variablelist>\n";
8891da177e4SLinus Torvalds    print "</refsect1>\n";
8901da177e4SLinus Torvalds
8911da177e4SLinus Torvalds    output_section_xml(@_);
8921da177e4SLinus Torvalds
8931da177e4SLinus Torvalds    print "</refentry>\n\n";
8941da177e4SLinus Torvalds}
8951da177e4SLinus Torvalds
8961da177e4SLinus Torvalds# output typedef in XML DocBook
8971da177e4SLinus Torvaldssub output_typedef_xml(%) {
8981da177e4SLinus Torvalds    my %args = %{$_[0]};
8991da177e4SLinus Torvalds    my ($parameter, $section);
9001da177e4SLinus Torvalds    my $id;
9011da177e4SLinus Torvalds
9021da177e4SLinus Torvalds    $id = "API-typedef-" . $args{'typedef'};
9031da177e4SLinus Torvalds    $id =~ s/[^A-Za-z0-9]/-/g;
9041da177e4SLinus Torvalds
9055449bc94SPavel Pisa    print "<refentry id=\"$id\">\n";
9068b0c2d98SMartin Waitz    print "<refentryinfo>\n";
9078b0c2d98SMartin Waitz    print " <title>LINUX</title>\n";
9088b0c2d98SMartin Waitz    print " <productname>Kernel Hackers Manual</productname>\n";
9098b0c2d98SMartin Waitz    print " <date>$man_date</date>\n";
9108b0c2d98SMartin Waitz    print "</refentryinfo>\n";
9111da177e4SLinus Torvalds    print "<refmeta>\n";
9125449bc94SPavel Pisa    print " <refentrytitle><phrase>typedef " . $args{'typedef'} . "</phrase></refentrytitle>\n";
9138b0c2d98SMartin Waitz    print " <manvolnum>9</manvolnum>\n";
9141da177e4SLinus Torvalds    print "</refmeta>\n";
9151da177e4SLinus Torvalds    print "<refnamediv>\n";
9161da177e4SLinus Torvalds    print " <refname>typedef " . $args{'typedef'} . "</refname>\n";
9171da177e4SLinus Torvalds    print " <refpurpose>\n";
9181da177e4SLinus Torvalds    print "  ";
9191da177e4SLinus Torvalds    output_highlight ($args{'purpose'});
9201da177e4SLinus Torvalds    print " </refpurpose>\n";
9211da177e4SLinus Torvalds    print "</refnamediv>\n";
9221da177e4SLinus Torvalds
9231da177e4SLinus Torvalds    print "<refsynopsisdiv>\n";
9241da177e4SLinus Torvalds    print " <title>Synopsis</title>\n";
9251da177e4SLinus Torvalds    print "  <synopsis>typedef " . $args{'typedef'} . ";</synopsis>\n";
9261da177e4SLinus Torvalds    print "</refsynopsisdiv>\n";
9271da177e4SLinus Torvalds
9281da177e4SLinus Torvalds    output_section_xml(@_);
9291da177e4SLinus Torvalds
9301da177e4SLinus Torvalds    print "</refentry>\n\n";
9311da177e4SLinus Torvalds}
9321da177e4SLinus Torvalds
9331da177e4SLinus Torvalds# output in XML DocBook
934b112e0f7SJohannes Bergsub output_blockhead_xml(%) {
9351da177e4SLinus Torvalds    my %args = %{$_[0]};
9361da177e4SLinus Torvalds    my ($parameter, $section);
9371da177e4SLinus Torvalds    my $count;
9381da177e4SLinus Torvalds
9391da177e4SLinus Torvalds    my $id = $args{'module'};
9401da177e4SLinus Torvalds    $id =~ s/[^A-Za-z0-9]/-/g;
9411da177e4SLinus Torvalds
9421da177e4SLinus Torvalds    # print out each section
9431da177e4SLinus Torvalds    $lineprefix="   ";
9441da177e4SLinus Torvalds    foreach $section (@{$args{'sectionlist'}}) {
945b112e0f7SJohannes Berg	if (!$args{'content-only'}) {
946b112e0f7SJohannes Berg		print "<refsect1>\n <title>$section</title>\n";
947b112e0f7SJohannes Berg	}
9481da177e4SLinus Torvalds	if ($section =~ m/EXAMPLE/i) {
9491da177e4SLinus Torvalds	    print "<example><para>\n";
950b112e0f7SJohannes Berg	} else {
951b112e0f7SJohannes Berg	    print "<para>\n";
9521da177e4SLinus Torvalds	}
9531da177e4SLinus Torvalds	output_highlight($args{'sections'}{$section});
9541da177e4SLinus Torvalds	if ($section =~ m/EXAMPLE/i) {
9551da177e4SLinus Torvalds	    print "</para></example>\n";
956b112e0f7SJohannes Berg	} else {
957b112e0f7SJohannes Berg	    print "</para>";
9581da177e4SLinus Torvalds	}
959b112e0f7SJohannes Berg	if (!$args{'content-only'}) {
960b112e0f7SJohannes Berg		print "\n</refsect1>\n";
961b112e0f7SJohannes Berg	}
9621da177e4SLinus Torvalds    }
9631da177e4SLinus Torvalds
9641da177e4SLinus Torvalds    print "\n\n";
9651da177e4SLinus Torvalds}
9661da177e4SLinus Torvalds
9671da177e4SLinus Torvalds# output in XML DocBook
9681da177e4SLinus Torvaldssub output_function_gnome {
9691da177e4SLinus Torvalds    my %args = %{$_[0]};
9701da177e4SLinus Torvalds    my ($parameter, $section);
9711da177e4SLinus Torvalds    my $count;
9721da177e4SLinus Torvalds    my $id;
9731da177e4SLinus Torvalds
9741da177e4SLinus Torvalds    $id = $args{'module'} . "-" . $args{'function'};
9751da177e4SLinus Torvalds    $id =~ s/[^A-Za-z0-9]/-/g;
9761da177e4SLinus Torvalds
9771da177e4SLinus Torvalds    print "<sect2>\n";
9781da177e4SLinus Torvalds    print " <title id=\"$id\">" . $args{'function'} . "</title>\n";
9791da177e4SLinus Torvalds
9801da177e4SLinus Torvalds    print "  <funcsynopsis>\n";
9811da177e4SLinus Torvalds    print "   <funcdef>" . $args{'functiontype'} . " ";
9821da177e4SLinus Torvalds    print "<function>" . $args{'function'} . " ";
9831da177e4SLinus Torvalds    print "</function></funcdef>\n";
9841da177e4SLinus Torvalds
9851da177e4SLinus Torvalds    $count = 0;
9861da177e4SLinus Torvalds    if ($#{$args{'parameterlist'}} >= 0) {
9871da177e4SLinus Torvalds	foreach $parameter (@{$args{'parameterlist'}}) {
9881da177e4SLinus Torvalds	    $type = $args{'parametertypes'}{$parameter};
9891da177e4SLinus Torvalds	    if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
9901da177e4SLinus Torvalds		# pointer-to-function
9911da177e4SLinus Torvalds		print "   <paramdef>$1 <parameter>$parameter</parameter>)\n";
9921da177e4SLinus Torvalds		print "     <funcparams>$2</funcparams></paramdef>\n";
9931da177e4SLinus Torvalds	    } else {
9941da177e4SLinus Torvalds		print "   <paramdef>" . $type;
9951da177e4SLinus Torvalds		print " <parameter>$parameter</parameter></paramdef>\n";
9961da177e4SLinus Torvalds	    }
9971da177e4SLinus Torvalds	}
9981da177e4SLinus Torvalds    } else {
9991da177e4SLinus Torvalds	print "  <void>\n";
10001da177e4SLinus Torvalds    }
10011da177e4SLinus Torvalds    print "  </funcsynopsis>\n";
10021da177e4SLinus Torvalds    if ($#{$args{'parameterlist'}} >= 0) {
10031da177e4SLinus Torvalds	print " <informaltable pgwide=\"1\" frame=\"none\" role=\"params\">\n";
10041da177e4SLinus Torvalds	print "<tgroup cols=\"2\">\n";
10051da177e4SLinus Torvalds	print "<colspec colwidth=\"2*\">\n";
10061da177e4SLinus Torvalds	print "<colspec colwidth=\"8*\">\n";
10071da177e4SLinus Torvalds	print "<tbody>\n";
10081da177e4SLinus Torvalds	foreach $parameter (@{$args{'parameterlist'}}) {
10091da177e4SLinus Torvalds	    my $parameter_name = $parameter;
10101da177e4SLinus Torvalds	    $parameter_name =~ s/\[.*//;
10111da177e4SLinus Torvalds
10121da177e4SLinus Torvalds	    print "  <row><entry align=\"right\"><parameter>$parameter</parameter></entry>\n";
10131da177e4SLinus Torvalds	    print "   <entry>\n";
10141da177e4SLinus Torvalds	    $lineprefix="     ";
10151da177e4SLinus Torvalds	    output_highlight($args{'parameterdescs'}{$parameter_name});
10161da177e4SLinus Torvalds	    print "    </entry></row>\n";
10171da177e4SLinus Torvalds	}
10181da177e4SLinus Torvalds	print " </tbody></tgroup></informaltable>\n";
10191da177e4SLinus Torvalds    } else {
10201da177e4SLinus Torvalds	print " <para>\n  None\n </para>\n";
10211da177e4SLinus Torvalds    }
10221da177e4SLinus Torvalds
10231da177e4SLinus Torvalds    # print out each section
10241da177e4SLinus Torvalds    $lineprefix="   ";
10251da177e4SLinus Torvalds    foreach $section (@{$args{'sectionlist'}}) {
10261da177e4SLinus Torvalds	print "<simplesect>\n <title>$section</title>\n";
10271da177e4SLinus Torvalds	if ($section =~ m/EXAMPLE/i) {
10281da177e4SLinus Torvalds	    print "<example><programlisting>\n";
10291da177e4SLinus Torvalds	} else {
10301da177e4SLinus Torvalds	}
10311da177e4SLinus Torvalds	print "<para>\n";
10321da177e4SLinus Torvalds	output_highlight($args{'sections'}{$section});
10331da177e4SLinus Torvalds	print "</para>\n";
10341da177e4SLinus Torvalds	if ($section =~ m/EXAMPLE/i) {
10351da177e4SLinus Torvalds	    print "</programlisting></example>\n";
10361da177e4SLinus Torvalds	} else {
10371da177e4SLinus Torvalds	}
10381da177e4SLinus Torvalds	print " </simplesect>\n";
10391da177e4SLinus Torvalds    }
10401da177e4SLinus Torvalds
10411da177e4SLinus Torvalds    print "</sect2>\n\n";
10421da177e4SLinus Torvalds}
10431da177e4SLinus Torvalds
10441da177e4SLinus Torvalds##
10451da177e4SLinus Torvalds# output function in man
10461da177e4SLinus Torvaldssub output_function_man(%) {
10471da177e4SLinus Torvalds    my %args = %{$_[0]};
10481da177e4SLinus Torvalds    my ($parameter, $section);
10491da177e4SLinus Torvalds    my $count;
10501da177e4SLinus Torvalds
10511da177e4SLinus Torvalds    print ".TH \"$args{'function'}\" 9 \"$args{'function'}\" \"$man_date\" \"Kernel Hacker's Manual\" LINUX\n";
10521da177e4SLinus Torvalds
10531da177e4SLinus Torvalds    print ".SH NAME\n";
10541da177e4SLinus Torvalds    print $args{'function'} . " \\- " . $args{'purpose'} . "\n";
10551da177e4SLinus Torvalds
10561da177e4SLinus Torvalds    print ".SH SYNOPSIS\n";
1057a21217daSRandy Dunlap    if ($args{'functiontype'} ne "") {
10581da177e4SLinus Torvalds	print ".B \"" . $args{'functiontype'} . "\" " . $args{'function'} . "\n";
1059a21217daSRandy Dunlap    } else {
1060a21217daSRandy Dunlap	print ".B \"" . $args{'function'} . "\n";
1061a21217daSRandy Dunlap    }
10621da177e4SLinus Torvalds    $count = 0;
10631da177e4SLinus Torvalds    my $parenth = "(";
10641da177e4SLinus Torvalds    my $post = ",";
10651da177e4SLinus Torvalds    foreach my $parameter (@{$args{'parameterlist'}}) {
10661da177e4SLinus Torvalds	if ($count == $#{$args{'parameterlist'}}) {
10671da177e4SLinus Torvalds	    $post = ");";
10681da177e4SLinus Torvalds	}
10691da177e4SLinus Torvalds	$type = $args{'parametertypes'}{$parameter};
10701da177e4SLinus Torvalds	if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
10711da177e4SLinus Torvalds	    # pointer-to-function
10721da177e4SLinus Torvalds	    print ".BI \"" . $parenth . $1 . "\" " . $parameter . " \") (" . $2 . ")" . $post . "\"\n";
10731da177e4SLinus Torvalds	} else {
10741da177e4SLinus Torvalds	    $type =~ s/([^\*])$/$1 /;
10751da177e4SLinus Torvalds	    print ".BI \"" . $parenth . $type . "\" " . $parameter . " \"" . $post . "\"\n";
10761da177e4SLinus Torvalds	}
10771da177e4SLinus Torvalds	$count++;
10781da177e4SLinus Torvalds	$parenth = "";
10791da177e4SLinus Torvalds    }
10801da177e4SLinus Torvalds
10811da177e4SLinus Torvalds    print ".SH ARGUMENTS\n";
10821da177e4SLinus Torvalds    foreach $parameter (@{$args{'parameterlist'}}) {
10831da177e4SLinus Torvalds	my $parameter_name = $parameter;
10841da177e4SLinus Torvalds	$parameter_name =~ s/\[.*//;
10851da177e4SLinus Torvalds
10861da177e4SLinus Torvalds	print ".IP \"" . $parameter . "\" 12\n";
10871da177e4SLinus Torvalds	output_highlight($args{'parameterdescs'}{$parameter_name});
10881da177e4SLinus Torvalds    }
10891da177e4SLinus Torvalds    foreach $section (@{$args{'sectionlist'}}) {
10901da177e4SLinus Torvalds	print ".SH \"", uc $section, "\"\n";
10911da177e4SLinus Torvalds	output_highlight($args{'sections'}{$section});
10921da177e4SLinus Torvalds    }
10931da177e4SLinus Torvalds}
10941da177e4SLinus Torvalds
10951da177e4SLinus Torvalds##
10961da177e4SLinus Torvalds# output enum in man
10971da177e4SLinus Torvaldssub output_enum_man(%) {
10981da177e4SLinus Torvalds    my %args = %{$_[0]};
10991da177e4SLinus Torvalds    my ($parameter, $section);
11001da177e4SLinus Torvalds    my $count;
11011da177e4SLinus Torvalds
11021da177e4SLinus Torvalds    print ".TH \"$args{'module'}\" 9 \"enum $args{'enum'}\" \"$man_date\" \"API Manual\" LINUX\n";
11031da177e4SLinus Torvalds
11041da177e4SLinus Torvalds    print ".SH NAME\n";
11051da177e4SLinus Torvalds    print "enum " . $args{'enum'} . " \\- " . $args{'purpose'} . "\n";
11061da177e4SLinus Torvalds
11071da177e4SLinus Torvalds    print ".SH SYNOPSIS\n";
11081da177e4SLinus Torvalds    print "enum " . $args{'enum'} . " {\n";
11091da177e4SLinus Torvalds    $count = 0;
11101da177e4SLinus Torvalds    foreach my $parameter (@{$args{'parameterlist'}}) {
11111da177e4SLinus Torvalds	print ".br\n.BI \"    $parameter\"\n";
11121da177e4SLinus Torvalds	if ($count == $#{$args{'parameterlist'}}) {
11131da177e4SLinus Torvalds	    print "\n};\n";
11141da177e4SLinus Torvalds	    last;
11151da177e4SLinus Torvalds	}
11161da177e4SLinus Torvalds	else {
11171da177e4SLinus Torvalds	    print ", \n.br\n";
11181da177e4SLinus Torvalds	}
11191da177e4SLinus Torvalds	$count++;
11201da177e4SLinus Torvalds    }
11211da177e4SLinus Torvalds
11221da177e4SLinus Torvalds    print ".SH Constants\n";
11231da177e4SLinus Torvalds    foreach $parameter (@{$args{'parameterlist'}}) {
11241da177e4SLinus Torvalds	my $parameter_name = $parameter;
11251da177e4SLinus Torvalds	$parameter_name =~ s/\[.*//;
11261da177e4SLinus Torvalds
11271da177e4SLinus Torvalds	print ".IP \"" . $parameter . "\" 12\n";
11281da177e4SLinus Torvalds	output_highlight($args{'parameterdescs'}{$parameter_name});
11291da177e4SLinus Torvalds    }
11301da177e4SLinus Torvalds    foreach $section (@{$args{'sectionlist'}}) {
11311da177e4SLinus Torvalds	print ".SH \"$section\"\n";
11321da177e4SLinus Torvalds	output_highlight($args{'sections'}{$section});
11331da177e4SLinus Torvalds    }
11341da177e4SLinus Torvalds}
11351da177e4SLinus Torvalds
11361da177e4SLinus Torvalds##
11371da177e4SLinus Torvalds# output struct in man
11381da177e4SLinus Torvaldssub output_struct_man(%) {
11391da177e4SLinus Torvalds    my %args = %{$_[0]};
11401da177e4SLinus Torvalds    my ($parameter, $section);
11411da177e4SLinus Torvalds
11421da177e4SLinus Torvalds    print ".TH \"$args{'module'}\" 9 \"" . $args{'type'} . " " . $args{'struct'} . "\" \"$man_date\" \"API Manual\" LINUX\n";
11431da177e4SLinus Torvalds
11441da177e4SLinus Torvalds    print ".SH NAME\n";
11451da177e4SLinus Torvalds    print $args{'type'} . " " . $args{'struct'} . " \\- " . $args{'purpose'} . "\n";
11461da177e4SLinus Torvalds
11471da177e4SLinus Torvalds    print ".SH SYNOPSIS\n";
11481da177e4SLinus Torvalds    print $args{'type'} . " " . $args{'struct'} . " {\n.br\n";
11491da177e4SLinus Torvalds
11501da177e4SLinus Torvalds    foreach my $parameter (@{$args{'parameterlist'}}) {
11511da177e4SLinus Torvalds	if ($parameter =~ /^#/) {
11521da177e4SLinus Torvalds	    print ".BI \"$parameter\"\n.br\n";
11531da177e4SLinus Torvalds	    next;
11541da177e4SLinus Torvalds	}
11551da177e4SLinus Torvalds	my $parameter_name = $parameter;
11561da177e4SLinus Torvalds	$parameter_name =~ s/\[.*//;
11571da177e4SLinus Torvalds
11581da177e4SLinus Torvalds	($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
11591da177e4SLinus Torvalds	$type = $args{'parametertypes'}{$parameter};
11601da177e4SLinus Torvalds	if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
11611da177e4SLinus Torvalds	    # pointer-to-function
11621da177e4SLinus Torvalds	    print ".BI \"    " . $1 . "\" " . $parameter . " \") (" . $2 . ")" . "\"\n;\n";
11631da177e4SLinus Torvalds	} elsif ($type =~ m/^(.*?)\s*(:.*)/) {
11641d7e1d45SRandy.Dunlap	    # bitfield
11651d7e1d45SRandy.Dunlap	    print ".BI \"    " . $1 . "\ \" " . $parameter . $2 . " \"" . "\"\n;\n";
11661da177e4SLinus Torvalds	} else {
11671da177e4SLinus Torvalds	    $type =~ s/([^\*])$/$1 /;
11681da177e4SLinus Torvalds	    print ".BI \"    " . $type . "\" " . $parameter . " \"" . "\"\n;\n";
11691da177e4SLinus Torvalds	}
11701da177e4SLinus Torvalds	print "\n.br\n";
11711da177e4SLinus Torvalds    }
11721da177e4SLinus Torvalds    print "};\n.br\n";
11731da177e4SLinus Torvalds
1174c51d3dacSRandy Dunlap    print ".SH Members\n";
11751da177e4SLinus Torvalds    foreach $parameter (@{$args{'parameterlist'}}) {
11761da177e4SLinus Torvalds	($parameter =~ /^#/) && next;
11771da177e4SLinus Torvalds
11781da177e4SLinus Torvalds	my $parameter_name = $parameter;
11791da177e4SLinus Torvalds	$parameter_name =~ s/\[.*//;
11801da177e4SLinus Torvalds
11811da177e4SLinus Torvalds	($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
11821da177e4SLinus Torvalds	print ".IP \"" . $parameter . "\" 12\n";
11831da177e4SLinus Torvalds	output_highlight($args{'parameterdescs'}{$parameter_name});
11841da177e4SLinus Torvalds    }
11851da177e4SLinus Torvalds    foreach $section (@{$args{'sectionlist'}}) {
11861da177e4SLinus Torvalds	print ".SH \"$section\"\n";
11871da177e4SLinus Torvalds	output_highlight($args{'sections'}{$section});
11881da177e4SLinus Torvalds    }
11891da177e4SLinus Torvalds}
11901da177e4SLinus Torvalds
11911da177e4SLinus Torvalds##
11921da177e4SLinus Torvalds# output typedef in man
11931da177e4SLinus Torvaldssub output_typedef_man(%) {
11941da177e4SLinus Torvalds    my %args = %{$_[0]};
11951da177e4SLinus Torvalds    my ($parameter, $section);
11961da177e4SLinus Torvalds
11971da177e4SLinus Torvalds    print ".TH \"$args{'module'}\" 9 \"$args{'typedef'}\" \"$man_date\" \"API Manual\" LINUX\n";
11981da177e4SLinus Torvalds
11991da177e4SLinus Torvalds    print ".SH NAME\n";
12001da177e4SLinus Torvalds    print "typedef " . $args{'typedef'} . " \\- " . $args{'purpose'} . "\n";
12011da177e4SLinus Torvalds
12021da177e4SLinus Torvalds    foreach $section (@{$args{'sectionlist'}}) {
12031da177e4SLinus Torvalds	print ".SH \"$section\"\n";
12041da177e4SLinus Torvalds	output_highlight($args{'sections'}{$section});
12051da177e4SLinus Torvalds    }
12061da177e4SLinus Torvalds}
12071da177e4SLinus Torvalds
1208b112e0f7SJohannes Bergsub output_blockhead_man(%) {
12091da177e4SLinus Torvalds    my %args = %{$_[0]};
12101da177e4SLinus Torvalds    my ($parameter, $section);
12111da177e4SLinus Torvalds    my $count;
12121da177e4SLinus Torvalds
12131da177e4SLinus Torvalds    print ".TH \"$args{'module'}\" 9 \"$args{'module'}\" \"$man_date\" \"API Manual\" LINUX\n";
12141da177e4SLinus Torvalds
12151da177e4SLinus Torvalds    foreach $section (@{$args{'sectionlist'}}) {
12161da177e4SLinus Torvalds	print ".SH \"$section\"\n";
12171da177e4SLinus Torvalds	output_highlight($args{'sections'}{$section});
12181da177e4SLinus Torvalds    }
12191da177e4SLinus Torvalds}
12201da177e4SLinus Torvalds
12211da177e4SLinus Torvalds##
12221da177e4SLinus Torvalds# output in text
12231da177e4SLinus Torvaldssub output_function_text(%) {
12241da177e4SLinus Torvalds    my %args = %{$_[0]};
12251da177e4SLinus Torvalds    my ($parameter, $section);
1226a21217daSRandy Dunlap    my $start;
12271da177e4SLinus Torvalds
1228f47634b2SRandy Dunlap    print "Name:\n\n";
1229f47634b2SRandy Dunlap    print $args{'function'} . " - " . $args{'purpose'} . "\n";
1230f47634b2SRandy Dunlap
1231f47634b2SRandy Dunlap    print "\nSynopsis:\n\n";
1232a21217daSRandy Dunlap    if ($args{'functiontype'} ne "") {
1233a21217daSRandy Dunlap	$start = $args{'functiontype'} . " " . $args{'function'} . " (";
1234a21217daSRandy Dunlap    } else {
1235a21217daSRandy Dunlap	$start = $args{'function'} . " (";
1236a21217daSRandy Dunlap    }
12371da177e4SLinus Torvalds    print $start;
1238a21217daSRandy Dunlap
12391da177e4SLinus Torvalds    my $count = 0;
12401da177e4SLinus Torvalds    foreach my $parameter (@{$args{'parameterlist'}}) {
12411da177e4SLinus Torvalds	$type = $args{'parametertypes'}{$parameter};
12421da177e4SLinus Torvalds	if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
12431da177e4SLinus Torvalds	    # pointer-to-function
12441da177e4SLinus Torvalds	    print $1 . $parameter . ") (" . $2;
12451da177e4SLinus Torvalds	} else {
12461da177e4SLinus Torvalds	    print $type . " " . $parameter;
12471da177e4SLinus Torvalds	}
12481da177e4SLinus Torvalds	if ($count != $#{$args{'parameterlist'}}) {
12491da177e4SLinus Torvalds	    $count++;
12501da177e4SLinus Torvalds	    print ",\n";
12511da177e4SLinus Torvalds	    print " " x length($start);
12521da177e4SLinus Torvalds	} else {
12531da177e4SLinus Torvalds	    print ");\n\n";
12541da177e4SLinus Torvalds	}
12551da177e4SLinus Torvalds    }
12561da177e4SLinus Torvalds
12571da177e4SLinus Torvalds    print "Arguments:\n\n";
12581da177e4SLinus Torvalds    foreach $parameter (@{$args{'parameterlist'}}) {
12591da177e4SLinus Torvalds	my $parameter_name = $parameter;
12601da177e4SLinus Torvalds	$parameter_name =~ s/\[.*//;
12611da177e4SLinus Torvalds
12621da177e4SLinus Torvalds	print $parameter . "\n\t" . $args{'parameterdescs'}{$parameter_name} . "\n";
12631da177e4SLinus Torvalds    }
12641da177e4SLinus Torvalds    output_section_text(@_);
12651da177e4SLinus Torvalds}
12661da177e4SLinus Torvalds
12671da177e4SLinus Torvalds#output sections in text
12681da177e4SLinus Torvaldssub output_section_text(%) {
12691da177e4SLinus Torvalds    my %args = %{$_[0]};
12701da177e4SLinus Torvalds    my $section;
12711da177e4SLinus Torvalds
12721da177e4SLinus Torvalds    print "\n";
12731da177e4SLinus Torvalds    foreach $section (@{$args{'sectionlist'}}) {
12741da177e4SLinus Torvalds	print "$section:\n\n";
12751da177e4SLinus Torvalds	output_highlight($args{'sections'}{$section});
12761da177e4SLinus Torvalds    }
12771da177e4SLinus Torvalds    print "\n\n";
12781da177e4SLinus Torvalds}
12791da177e4SLinus Torvalds
12801da177e4SLinus Torvalds# output enum in text
12811da177e4SLinus Torvaldssub output_enum_text(%) {
12821da177e4SLinus Torvalds    my %args = %{$_[0]};
12831da177e4SLinus Torvalds    my ($parameter);
12841da177e4SLinus Torvalds    my $count;
12851da177e4SLinus Torvalds    print "Enum:\n\n";
12861da177e4SLinus Torvalds
12871d7e1d45SRandy.Dunlap    print "enum " . $args{'enum'} . " - " . $args{'purpose'} . "\n\n";
12881da177e4SLinus Torvalds    print "enum " . $args{'enum'} . " {\n";
12891da177e4SLinus Torvalds    $count = 0;
12901da177e4SLinus Torvalds    foreach $parameter (@{$args{'parameterlist'}}) {
12911da177e4SLinus Torvalds	print "\t$parameter";
12921da177e4SLinus Torvalds	if ($count != $#{$args{'parameterlist'}}) {
12931da177e4SLinus Torvalds	    $count++;
12941da177e4SLinus Torvalds	    print ",";
12951da177e4SLinus Torvalds	}
12961da177e4SLinus Torvalds	print "\n";
12971da177e4SLinus Torvalds    }
12981da177e4SLinus Torvalds    print "};\n\n";
12991da177e4SLinus Torvalds
13001da177e4SLinus Torvalds    print "Constants:\n\n";
13011da177e4SLinus Torvalds    foreach $parameter (@{$args{'parameterlist'}}) {
13021da177e4SLinus Torvalds	print "$parameter\n\t";
13031da177e4SLinus Torvalds	print $args{'parameterdescs'}{$parameter} . "\n";
13041da177e4SLinus Torvalds    }
13051da177e4SLinus Torvalds
13061da177e4SLinus Torvalds    output_section_text(@_);
13071da177e4SLinus Torvalds}
13081da177e4SLinus Torvalds
13091da177e4SLinus Torvalds# output typedef in text
13101da177e4SLinus Torvaldssub output_typedef_text(%) {
13111da177e4SLinus Torvalds    my %args = %{$_[0]};
13121da177e4SLinus Torvalds    my ($parameter);
13131da177e4SLinus Torvalds    my $count;
13141da177e4SLinus Torvalds    print "Typedef:\n\n";
13151da177e4SLinus Torvalds
13161d7e1d45SRandy.Dunlap    print "typedef " . $args{'typedef'} . " - " . $args{'purpose'} . "\n";
13171da177e4SLinus Torvalds    output_section_text(@_);
13181da177e4SLinus Torvalds}
13191da177e4SLinus Torvalds
13201da177e4SLinus Torvalds# output struct as text
13211da177e4SLinus Torvaldssub output_struct_text(%) {
13221da177e4SLinus Torvalds    my %args = %{$_[0]};
13231da177e4SLinus Torvalds    my ($parameter);
13241da177e4SLinus Torvalds
13251d7e1d45SRandy.Dunlap    print $args{'type'} . " " . $args{'struct'} . " - " . $args{'purpose'} . "\n\n";
13261da177e4SLinus Torvalds    print $args{'type'} . " " . $args{'struct'} . " {\n";
13271da177e4SLinus Torvalds    foreach $parameter (@{$args{'parameterlist'}}) {
13281da177e4SLinus Torvalds	if ($parameter =~ /^#/) {
13291da177e4SLinus Torvalds	    print "$parameter\n";
13301da177e4SLinus Torvalds	    next;
13311da177e4SLinus Torvalds	}
13321da177e4SLinus Torvalds
13331da177e4SLinus Torvalds	my $parameter_name = $parameter;
13341da177e4SLinus Torvalds	$parameter_name =~ s/\[.*//;
13351da177e4SLinus Torvalds
13361da177e4SLinus Torvalds	($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
13371da177e4SLinus Torvalds	$type = $args{'parametertypes'}{$parameter};
13381da177e4SLinus Torvalds	if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
13391da177e4SLinus Torvalds	    # pointer-to-function
13401da177e4SLinus Torvalds	    print "\t$1 $parameter) ($2);\n";
13411da177e4SLinus Torvalds	} elsif ($type =~ m/^(.*?)\s*(:.*)/) {
134251f5a0c8SRandy Dunlap	    # bitfield
13431da177e4SLinus Torvalds	    print "\t$1 $parameter$2;\n";
13441da177e4SLinus Torvalds	} else {
13451da177e4SLinus Torvalds	    print "\t" . $type . " " . $parameter . ";\n";
13461da177e4SLinus Torvalds	}
13471da177e4SLinus Torvalds    }
13481da177e4SLinus Torvalds    print "};\n\n";
13491da177e4SLinus Torvalds
13501da177e4SLinus Torvalds    print "Members:\n\n";
13511da177e4SLinus Torvalds    foreach $parameter (@{$args{'parameterlist'}}) {
13521da177e4SLinus Torvalds	($parameter =~ /^#/) && next;
13531da177e4SLinus Torvalds
13541da177e4SLinus Torvalds	my $parameter_name = $parameter;
13551da177e4SLinus Torvalds	$parameter_name =~ s/\[.*//;
13561da177e4SLinus Torvalds
13571da177e4SLinus Torvalds	($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
13581da177e4SLinus Torvalds	print "$parameter\n\t";
13591da177e4SLinus Torvalds	print $args{'parameterdescs'}{$parameter_name} . "\n";
13601da177e4SLinus Torvalds    }
13611da177e4SLinus Torvalds    print "\n";
13621da177e4SLinus Torvalds    output_section_text(@_);
13631da177e4SLinus Torvalds}
13641da177e4SLinus Torvalds
1365b112e0f7SJohannes Bergsub output_blockhead_text(%) {
13661da177e4SLinus Torvalds    my %args = %{$_[0]};
13671da177e4SLinus Torvalds    my ($parameter, $section);
13681da177e4SLinus Torvalds
13691da177e4SLinus Torvalds    foreach $section (@{$args{'sectionlist'}}) {
13701da177e4SLinus Torvalds	print " $section:\n";
13711da177e4SLinus Torvalds	print "    -> ";
13721da177e4SLinus Torvalds	output_highlight($args{'sections'}{$section});
13731da177e4SLinus Torvalds    }
13741da177e4SLinus Torvalds}
13751da177e4SLinus Torvalds
1376*eda603f6SJohannes Berg## list mode output functions
1377*eda603f6SJohannes Berg
1378*eda603f6SJohannes Bergsub output_function_list(%) {
1379*eda603f6SJohannes Berg    my %args = %{$_[0]};
1380*eda603f6SJohannes Berg
1381*eda603f6SJohannes Berg    print $args{'function'} . "\n";
1382*eda603f6SJohannes Berg}
1383*eda603f6SJohannes Berg
1384*eda603f6SJohannes Berg# output enum in list
1385*eda603f6SJohannes Bergsub output_enum_list(%) {
1386*eda603f6SJohannes Berg    my %args = %{$_[0]};
1387*eda603f6SJohannes Berg    print $args{'enum'} . "\n";
1388*eda603f6SJohannes Berg}
1389*eda603f6SJohannes Berg
1390*eda603f6SJohannes Berg# output typedef in list
1391*eda603f6SJohannes Bergsub output_typedef_list(%) {
1392*eda603f6SJohannes Berg    my %args = %{$_[0]};
1393*eda603f6SJohannes Berg    print $args{'typedef'} . "\n";
1394*eda603f6SJohannes Berg}
1395*eda603f6SJohannes Berg
1396*eda603f6SJohannes Berg# output struct as list
1397*eda603f6SJohannes Bergsub output_struct_list(%) {
1398*eda603f6SJohannes Berg    my %args = %{$_[0]};
1399*eda603f6SJohannes Berg
1400*eda603f6SJohannes Berg    print $args{'struct'} . "\n";
1401*eda603f6SJohannes Berg}
1402*eda603f6SJohannes Berg
1403*eda603f6SJohannes Bergsub output_blockhead_list(%) {
1404*eda603f6SJohannes Berg    my %args = %{$_[0]};
1405*eda603f6SJohannes Berg    my ($parameter, $section);
1406*eda603f6SJohannes Berg
1407*eda603f6SJohannes Berg    foreach $section (@{$args{'sectionlist'}}) {
1408*eda603f6SJohannes Berg	print "DOC: $section\n";
1409*eda603f6SJohannes Berg    }
1410*eda603f6SJohannes Berg}
1411*eda603f6SJohannes Berg
14121da177e4SLinus Torvalds##
141327205744SRandy Dunlap# generic output function for all types (function, struct/union, typedef, enum);
141427205744SRandy Dunlap# calls the generated, variable output_ function name based on
141527205744SRandy Dunlap# functype and output_mode
14161da177e4SLinus Torvaldssub output_declaration {
14171da177e4SLinus Torvalds    no strict 'refs';
14181da177e4SLinus Torvalds    my $name = shift;
14191da177e4SLinus Torvalds    my $functype = shift;
14201da177e4SLinus Torvalds    my $func = "output_${functype}_$output_mode";
14211da177e4SLinus Torvalds    if (($function_only==0) ||
14221da177e4SLinus Torvalds	( $function_only == 1 && defined($function_table{$name})) ||
14231da177e4SLinus Torvalds	( $function_only == 2 && !defined($function_table{$name})))
14241da177e4SLinus Torvalds    {
14251da177e4SLinus Torvalds	&$func(@_);
14261da177e4SLinus Torvalds	$section_counter++;
14271da177e4SLinus Torvalds    }
14281da177e4SLinus Torvalds}
14291da177e4SLinus Torvalds
14301da177e4SLinus Torvalds##
143127205744SRandy Dunlap# generic output function - calls the right one based on current output mode.
1432b112e0f7SJohannes Bergsub output_blockhead {
14331da177e4SLinus Torvalds    no strict 'refs';
1434b112e0f7SJohannes Berg    my $func = "output_blockhead_" . $output_mode;
14351da177e4SLinus Torvalds    &$func(@_);
14361da177e4SLinus Torvalds    $section_counter++;
14371da177e4SLinus Torvalds}
14381da177e4SLinus Torvalds
14391da177e4SLinus Torvalds##
14401da177e4SLinus Torvalds# takes a declaration (struct, union, enum, typedef) and
14411da177e4SLinus Torvalds# invokes the right handler. NOT called for functions.
14421da177e4SLinus Torvaldssub dump_declaration($$) {
14431da177e4SLinus Torvalds    no strict 'refs';
14441da177e4SLinus Torvalds    my ($prototype, $file) = @_;
14451da177e4SLinus Torvalds    my $func = "dump_" . $decl_type;
14461da177e4SLinus Torvalds    &$func(@_);
14471da177e4SLinus Torvalds}
14481da177e4SLinus Torvalds
14491da177e4SLinus Torvaldssub dump_union($$) {
14501da177e4SLinus Torvalds    dump_struct(@_);
14511da177e4SLinus Torvalds}
14521da177e4SLinus Torvalds
14531da177e4SLinus Torvaldssub dump_struct($$) {
14541da177e4SLinus Torvalds    my $x = shift;
14551da177e4SLinus Torvalds    my $file = shift;
1456a1d94aa5SRandy Dunlap    my $nested;
14571da177e4SLinus Torvalds
14581da177e4SLinus Torvalds    if ($x =~ /(struct|union)\s+(\w+)\s*{(.*)}/) {
145952dc5aecSRandy Dunlap	#my $decl_type = $1;
14601da177e4SLinus Torvalds	$declaration_name = $2;
14611da177e4SLinus Torvalds	my $members = $3;
14621da177e4SLinus Torvalds
14631da177e4SLinus Torvalds	# ignore embedded structs or unions
1464a1d94aa5SRandy Dunlap	$members =~ s/({.*})//g;
1465a1d94aa5SRandy Dunlap	$nested = $1;
14661da177e4SLinus Torvalds
1467aeec46b9SMartin Waitz	# ignore members marked private:
146852dc5aecSRandy Dunlap	$members =~ s/\/\*\s*private:.*?\/\*\s*public:.*?\*\///gos;
146952dc5aecSRandy Dunlap	$members =~ s/\/\*\s*private:.*//gos;
1470aeec46b9SMartin Waitz	# strip comments:
1471aeec46b9SMartin Waitz	$members =~ s/\/\*.*?\*\///gos;
1472a1d94aa5SRandy Dunlap	$nested =~ s/\/\*.*?\*\///gos;
1473d960eea9SRandy Dunlap	# strip kmemcheck_bitfield_{begin,end}.*;
1474d960eea9SRandy Dunlap	$members =~ s/kmemcheck_bitfield_.*?;//gos;
1475ef5da59fSRandy Dunlap	# strip attributes
1476ef5da59fSRandy Dunlap	$members =~ s/__aligned\s*\(\d+\)//gos;
1477aeec46b9SMartin Waitz
14781da177e4SLinus Torvalds	create_parameterlist($members, ';', $file);
1479a1d94aa5SRandy Dunlap	check_sections($file, $declaration_name, "struct", $sectcheck, $struct_actual, $nested);
14801da177e4SLinus Torvalds
14811da177e4SLinus Torvalds	output_declaration($declaration_name,
14821da177e4SLinus Torvalds			   'struct',
14831da177e4SLinus Torvalds			   {'struct' => $declaration_name,
14841da177e4SLinus Torvalds			    'module' => $modulename,
14851da177e4SLinus Torvalds			    'parameterlist' => \@parameterlist,
14861da177e4SLinus Torvalds			    'parameterdescs' => \%parameterdescs,
14871da177e4SLinus Torvalds			    'parametertypes' => \%parametertypes,
14881da177e4SLinus Torvalds			    'sectionlist' => \@sectionlist,
14891da177e4SLinus Torvalds			    'sections' => \%sections,
14901da177e4SLinus Torvalds			    'purpose' => $declaration_purpose,
14911da177e4SLinus Torvalds			    'type' => $decl_type
14921da177e4SLinus Torvalds			   });
14931da177e4SLinus Torvalds    }
14941da177e4SLinus Torvalds    else {
14951da177e4SLinus Torvalds	print STDERR "Error(${file}:$.): Cannot parse struct or union!\n";
14961da177e4SLinus Torvalds	++$errors;
14971da177e4SLinus Torvalds    }
14981da177e4SLinus Torvalds}
14991da177e4SLinus Torvalds
15001da177e4SLinus Torvaldssub dump_enum($$) {
15011da177e4SLinus Torvalds    my $x = shift;
15021da177e4SLinus Torvalds    my $file = shift;
15031da177e4SLinus Torvalds
1504aeec46b9SMartin Waitz    $x =~ s@/\*.*?\*/@@gos;	# strip comments.
1505b6d676dbSRandy Dunlap    $x =~ s/^#\s*define\s+.*$//; # strip #define macros inside enums
1506b6d676dbSRandy Dunlap
15071da177e4SLinus Torvalds    if ($x =~ /enum\s+(\w+)\s*{(.*)}/) {
15081da177e4SLinus Torvalds	$declaration_name = $1;
15091da177e4SLinus Torvalds	my $members = $2;
15101da177e4SLinus Torvalds
15111da177e4SLinus Torvalds	foreach my $arg (split ',', $members) {
15121da177e4SLinus Torvalds	    $arg =~ s/^\s*(\w+).*/$1/;
15131da177e4SLinus Torvalds	    push @parameterlist, $arg;
15141da177e4SLinus Torvalds	    if (!$parameterdescs{$arg}) {
15151da177e4SLinus Torvalds		$parameterdescs{$arg} = $undescribed;
15161da177e4SLinus Torvalds		print STDERR "Warning(${file}:$.): Enum value '$arg' ".
15171da177e4SLinus Torvalds		    "not described in enum '$declaration_name'\n";
15181da177e4SLinus Torvalds	    }
15191da177e4SLinus Torvalds
15201da177e4SLinus Torvalds	}
15211da177e4SLinus Torvalds
15221da177e4SLinus Torvalds	output_declaration($declaration_name,
15231da177e4SLinus Torvalds			   'enum',
15241da177e4SLinus Torvalds			   {'enum' => $declaration_name,
15251da177e4SLinus Torvalds			    'module' => $modulename,
15261da177e4SLinus Torvalds			    'parameterlist' => \@parameterlist,
15271da177e4SLinus Torvalds			    'parameterdescs' => \%parameterdescs,
15281da177e4SLinus Torvalds			    'sectionlist' => \@sectionlist,
15291da177e4SLinus Torvalds			    'sections' => \%sections,
15301da177e4SLinus Torvalds			    'purpose' => $declaration_purpose
15311da177e4SLinus Torvalds			   });
15321da177e4SLinus Torvalds    }
15331da177e4SLinus Torvalds    else {
15341da177e4SLinus Torvalds	print STDERR "Error(${file}:$.): Cannot parse enum!\n";
15351da177e4SLinus Torvalds	++$errors;
15361da177e4SLinus Torvalds    }
15371da177e4SLinus Torvalds}
15381da177e4SLinus Torvalds
15391da177e4SLinus Torvaldssub dump_typedef($$) {
15401da177e4SLinus Torvalds    my $x = shift;
15411da177e4SLinus Torvalds    my $file = shift;
15421da177e4SLinus Torvalds
1543aeec46b9SMartin Waitz    $x =~ s@/\*.*?\*/@@gos;	# strip comments.
15441da177e4SLinus Torvalds    while (($x =~ /\(*.\)\s*;$/) || ($x =~ /\[*.\]\s*;$/)) {
15451da177e4SLinus Torvalds	$x =~ s/\(*.\)\s*;$/;/;
15461da177e4SLinus Torvalds	$x =~ s/\[*.\]\s*;$/;/;
15471da177e4SLinus Torvalds    }
15481da177e4SLinus Torvalds
15491da177e4SLinus Torvalds    if ($x =~ /typedef.*\s+(\w+)\s*;/) {
15501da177e4SLinus Torvalds	$declaration_name = $1;
15511da177e4SLinus Torvalds
15521da177e4SLinus Torvalds	output_declaration($declaration_name,
15531da177e4SLinus Torvalds			   'typedef',
15541da177e4SLinus Torvalds			   {'typedef' => $declaration_name,
15551da177e4SLinus Torvalds			    'module' => $modulename,
15561da177e4SLinus Torvalds			    'sectionlist' => \@sectionlist,
15571da177e4SLinus Torvalds			    'sections' => \%sections,
15581da177e4SLinus Torvalds			    'purpose' => $declaration_purpose
15591da177e4SLinus Torvalds			   });
15601da177e4SLinus Torvalds    }
15611da177e4SLinus Torvalds    else {
15621da177e4SLinus Torvalds	print STDERR "Error(${file}:$.): Cannot parse typedef!\n";
15631da177e4SLinus Torvalds	++$errors;
15641da177e4SLinus Torvalds    }
15651da177e4SLinus Torvalds}
15661da177e4SLinus Torvalds
1567a1d94aa5SRandy Dunlapsub save_struct_actual($) {
1568a1d94aa5SRandy Dunlap    my $actual = shift;
1569a1d94aa5SRandy Dunlap
1570a1d94aa5SRandy Dunlap    # strip all spaces from the actual param so that it looks like one string item
1571a1d94aa5SRandy Dunlap    $actual =~ s/\s*//g;
1572a1d94aa5SRandy Dunlap    $struct_actual = $struct_actual . $actual . " ";
1573a1d94aa5SRandy Dunlap}
1574a1d94aa5SRandy Dunlap
15751da177e4SLinus Torvaldssub create_parameterlist($$$) {
15761da177e4SLinus Torvalds    my $args = shift;
15771da177e4SLinus Torvalds    my $splitter = shift;
15781da177e4SLinus Torvalds    my $file = shift;
15791da177e4SLinus Torvalds    my $type;
15801da177e4SLinus Torvalds    my $param;
15811da177e4SLinus Torvalds
1582a6d3fe77SMartin Waitz    # temporarily replace commas inside function pointer definition
15831da177e4SLinus Torvalds    while ($args =~ /(\([^\),]+),/) {
15841da177e4SLinus Torvalds	$args =~ s/(\([^\),]+),/$1#/g;
15851da177e4SLinus Torvalds    }
15861da177e4SLinus Torvalds
15871da177e4SLinus Torvalds    foreach my $arg (split($splitter, $args)) {
15881da177e4SLinus Torvalds	# strip comments
15891da177e4SLinus Torvalds	$arg =~ s/\/\*.*\*\///;
15901da177e4SLinus Torvalds	# strip leading/trailing spaces
15911da177e4SLinus Torvalds	$arg =~ s/^\s*//;
15921da177e4SLinus Torvalds	$arg =~ s/\s*$//;
15931da177e4SLinus Torvalds	$arg =~ s/\s+/ /;
15941da177e4SLinus Torvalds
15951da177e4SLinus Torvalds	if ($arg =~ /^#/) {
15961da177e4SLinus Torvalds	    # Treat preprocessor directive as a typeless variable just to fill
15971da177e4SLinus Torvalds	    # corresponding data structures "correctly". Catch it later in
15981da177e4SLinus Torvalds	    # output_* subs.
15991da177e4SLinus Torvalds	    push_parameter($arg, "", $file);
160000d62961SRichard Kennedy	} elsif ($arg =~ m/\(.+\)\s*\(/) {
16011da177e4SLinus Torvalds	    # pointer-to-function
16021da177e4SLinus Torvalds	    $arg =~ tr/#/,/;
160300d62961SRichard Kennedy	    $arg =~ m/[^\(]+\(\*?\s*(\w*)\s*\)/;
16041da177e4SLinus Torvalds	    $param = $1;
16051da177e4SLinus Torvalds	    $type = $arg;
160600d62961SRichard Kennedy	    $type =~ s/([^\(]+\(\*?)\s*$param/$1/;
1607a1d94aa5SRandy Dunlap	    save_struct_actual($param);
16081da177e4SLinus Torvalds	    push_parameter($param, $type, $file);
1609aeec46b9SMartin Waitz	} elsif ($arg) {
16101da177e4SLinus Torvalds	    $arg =~ s/\s*:\s*/:/g;
16111da177e4SLinus Torvalds	    $arg =~ s/\s*\[/\[/g;
16121da177e4SLinus Torvalds
16131da177e4SLinus Torvalds	    my @args = split('\s*,\s*', $arg);
16141da177e4SLinus Torvalds	    if ($args[0] =~ m/\*/) {
16151da177e4SLinus Torvalds		$args[0] =~ s/(\*+)\s*/ $1/;
16161da177e4SLinus Torvalds	    }
1617884f2810SBorislav Petkov
1618884f2810SBorislav Petkov	    my @first_arg;
1619884f2810SBorislav Petkov	    if ($args[0] =~ /^(.*\s+)(.*?\[.*\].*)$/) {
1620884f2810SBorislav Petkov		    shift @args;
1621884f2810SBorislav Petkov		    push(@first_arg, split('\s+', $1));
1622884f2810SBorislav Petkov		    push(@first_arg, $2);
1623884f2810SBorislav Petkov	    } else {
1624884f2810SBorislav Petkov		    @first_arg = split('\s+', shift @args);
1625884f2810SBorislav Petkov	    }
1626884f2810SBorislav Petkov
16271da177e4SLinus Torvalds	    unshift(@args, pop @first_arg);
16281da177e4SLinus Torvalds	    $type = join " ", @first_arg;
16291da177e4SLinus Torvalds
16301da177e4SLinus Torvalds	    foreach $param (@args) {
16311da177e4SLinus Torvalds		if ($param =~ m/^(\*+)\s*(.*)/) {
1632a1d94aa5SRandy Dunlap		    save_struct_actual($2);
16331da177e4SLinus Torvalds		    push_parameter($2, "$type $1", $file);
16341da177e4SLinus Torvalds		}
16351da177e4SLinus Torvalds		elsif ($param =~ m/(.*?):(\d+)/) {
16367b97887eSRandy Dunlap		    if ($type ne "") { # skip unnamed bit-fields
1637a1d94aa5SRandy Dunlap			save_struct_actual($1);
16381da177e4SLinus Torvalds			push_parameter($1, "$type:$2", $file)
16391da177e4SLinus Torvalds		    }
16407b97887eSRandy Dunlap		}
16411da177e4SLinus Torvalds		else {
1642a1d94aa5SRandy Dunlap		    save_struct_actual($param);
16431da177e4SLinus Torvalds		    push_parameter($param, $type, $file);
16441da177e4SLinus Torvalds		}
16451da177e4SLinus Torvalds	    }
16461da177e4SLinus Torvalds	}
16471da177e4SLinus Torvalds    }
16481da177e4SLinus Torvalds}
16491da177e4SLinus Torvalds
16501da177e4SLinus Torvaldssub push_parameter($$$) {
16511da177e4SLinus Torvalds	my $param = shift;
16521da177e4SLinus Torvalds	my $type = shift;
16531da177e4SLinus Torvalds	my $file = shift;
16541da177e4SLinus Torvalds
16555f8c7c98SRandy Dunlap	if (($anon_struct_union == 1) && ($type eq "") &&
16565f8c7c98SRandy Dunlap	    ($param eq "}")) {
16575f8c7c98SRandy Dunlap		return;		# ignore the ending }; from anon. struct/union
16585f8c7c98SRandy Dunlap	}
16595f8c7c98SRandy Dunlap
16605f8c7c98SRandy Dunlap	$anon_struct_union = 0;
16611da177e4SLinus Torvalds	my $param_name = $param;
16621da177e4SLinus Torvalds	$param_name =~ s/\[.*//;
16631da177e4SLinus Torvalds
1664a6d3fe77SMartin Waitz	if ($type eq "" && $param =~ /\.\.\.$/)
16651da177e4SLinus Torvalds	{
1666ced69090SRandy Dunlap	    if (!defined $parameterdescs{$param} || $parameterdescs{$param} eq "") {
1667a6d3fe77SMartin Waitz		$parameterdescs{$param} = "variable arguments";
16681da177e4SLinus Torvalds	    }
1669ced69090SRandy Dunlap	}
16701da177e4SLinus Torvalds	elsif ($type eq "" && ($param eq "" or $param eq "void"))
16711da177e4SLinus Torvalds	{
16721da177e4SLinus Torvalds	    $param="void";
16731da177e4SLinus Torvalds	    $parameterdescs{void} = "no arguments";
16741da177e4SLinus Torvalds	}
1675134fe01bSRandy Dunlap	elsif ($type eq "" && ($param eq "struct" or $param eq "union"))
1676134fe01bSRandy Dunlap	# handle unnamed (anonymous) union or struct:
1677134fe01bSRandy Dunlap	{
1678134fe01bSRandy Dunlap		$type = $param;
1679134fe01bSRandy Dunlap		$param = "{unnamed_" . $param . "}";
1680134fe01bSRandy Dunlap		$parameterdescs{$param} = "anonymous\n";
16815f8c7c98SRandy Dunlap		$anon_struct_union = 1;
1682134fe01bSRandy Dunlap	}
1683134fe01bSRandy Dunlap
1684a6d3fe77SMartin Waitz	# warn if parameter has no description
1685134fe01bSRandy Dunlap	# (but ignore ones starting with # as these are not parameters
1686134fe01bSRandy Dunlap	# but inline preprocessor statements);
1687134fe01bSRandy Dunlap	# also ignore unnamed structs/unions;
16885f8c7c98SRandy Dunlap	if (!$anon_struct_union) {
1689a6d3fe77SMartin Waitz	if (!defined $parameterdescs{$param_name} && $param_name !~ /^#/) {
1690a6d3fe77SMartin Waitz
16911da177e4SLinus Torvalds	    $parameterdescs{$param_name} = $undescribed;
16921da177e4SLinus Torvalds
16931da177e4SLinus Torvalds	    if (($type eq 'function') || ($type eq 'enum')) {
16941da177e4SLinus Torvalds		print STDERR "Warning(${file}:$.): Function parameter ".
16951da177e4SLinus Torvalds		    "or member '$param' not " .
16961da177e4SLinus Torvalds		    "described in '$declaration_name'\n";
16971da177e4SLinus Torvalds	    }
16981da177e4SLinus Torvalds	    print STDERR "Warning(${file}:$.):" .
16991da177e4SLinus Torvalds			 " No description found for parameter '$param'\n";
17001da177e4SLinus Torvalds	    ++$warnings;
17011da177e4SLinus Torvalds	}
1702134fe01bSRandy Dunlap	}
17031da177e4SLinus Torvalds
1704e34e7dbbSRandy Dunlap	# strip spaces from $param so that it is one continous string
1705e34e7dbbSRandy Dunlap	# on @parameterlist;
1706e34e7dbbSRandy Dunlap	# this fixes a problem where check_sections() cannot find
1707e34e7dbbSRandy Dunlap	# a parameter like "addr[6 + 2]" because it actually appears
1708e34e7dbbSRandy Dunlap	# as "addr[6", "+", "2]" on the parameter list;
1709e34e7dbbSRandy Dunlap	# but it's better to maintain the param string unchanged for output,
1710e34e7dbbSRandy Dunlap	# so just weaken the string compare in check_sections() to ignore
1711e34e7dbbSRandy Dunlap	# "[blah" in a parameter string;
1712e34e7dbbSRandy Dunlap	###$param =~ s/\s*//g;
17131da177e4SLinus Torvalds	push @parameterlist, $param;
17141da177e4SLinus Torvalds	$parametertypes{$param} = $type;
17151da177e4SLinus Torvalds}
17161da177e4SLinus Torvalds
1717a1d94aa5SRandy Dunlapsub check_sections($$$$$$) {
1718a1d94aa5SRandy Dunlap	my ($file, $decl_name, $decl_type, $sectcheck, $prmscheck, $nested) = @_;
1719a1d94aa5SRandy Dunlap	my @sects = split ' ', $sectcheck;
1720a1d94aa5SRandy Dunlap	my @prms = split ' ', $prmscheck;
1721a1d94aa5SRandy Dunlap	my $err;
1722a1d94aa5SRandy Dunlap	my ($px, $sx);
1723a1d94aa5SRandy Dunlap	my $prm_clean;		# strip trailing "[array size]" and/or beginning "*"
1724a1d94aa5SRandy Dunlap
1725a1d94aa5SRandy Dunlap	foreach $sx (0 .. $#sects) {
1726a1d94aa5SRandy Dunlap		$err = 1;
1727a1d94aa5SRandy Dunlap		foreach $px (0 .. $#prms) {
1728a1d94aa5SRandy Dunlap			$prm_clean = $prms[$px];
1729a1d94aa5SRandy Dunlap			$prm_clean =~ s/\[.*\]//;
17301f3a6688SJohannes Berg			$prm_clean =~ s/__attribute__\s*\(\([a-z,_\*\s\(\)]*\)\)//i;
1731e34e7dbbSRandy Dunlap			# ignore array size in a parameter string;
1732e34e7dbbSRandy Dunlap			# however, the original param string may contain
1733e34e7dbbSRandy Dunlap			# spaces, e.g.:  addr[6 + 2]
1734e34e7dbbSRandy Dunlap			# and this appears in @prms as "addr[6" since the
1735e34e7dbbSRandy Dunlap			# parameter list is split at spaces;
1736e34e7dbbSRandy Dunlap			# hence just ignore "[..." for the sections check;
1737e34e7dbbSRandy Dunlap			$prm_clean =~ s/\[.*//;
1738e34e7dbbSRandy Dunlap
1739a1d94aa5SRandy Dunlap			##$prm_clean =~ s/^\**//;
1740a1d94aa5SRandy Dunlap			if ($prm_clean eq $sects[$sx]) {
1741a1d94aa5SRandy Dunlap				$err = 0;
1742a1d94aa5SRandy Dunlap				last;
1743a1d94aa5SRandy Dunlap			}
1744a1d94aa5SRandy Dunlap		}
1745a1d94aa5SRandy Dunlap		if ($err) {
1746a1d94aa5SRandy Dunlap			if ($decl_type eq "function") {
1747a1d94aa5SRandy Dunlap				print STDERR "Warning(${file}:$.): " .
1748a1d94aa5SRandy Dunlap					"Excess function parameter " .
1749a1d94aa5SRandy Dunlap					"'$sects[$sx]' " .
1750a1d94aa5SRandy Dunlap					"description in '$decl_name'\n";
1751a1d94aa5SRandy Dunlap				++$warnings;
1752a1d94aa5SRandy Dunlap			} else {
1753a1d94aa5SRandy Dunlap				if ($nested !~ m/\Q$sects[$sx]\E/) {
1754a1d94aa5SRandy Dunlap				    print STDERR "Warning(${file}:$.): " .
1755a1d94aa5SRandy Dunlap					"Excess struct/union/enum/typedef member " .
1756a1d94aa5SRandy Dunlap					"'$sects[$sx]' " .
1757a1d94aa5SRandy Dunlap					"description in '$decl_name'\n";
1758a1d94aa5SRandy Dunlap				    ++$warnings;
1759a1d94aa5SRandy Dunlap				}
1760a1d94aa5SRandy Dunlap			}
1761a1d94aa5SRandy Dunlap		}
1762a1d94aa5SRandy Dunlap	}
1763a1d94aa5SRandy Dunlap}
1764a1d94aa5SRandy Dunlap
17651da177e4SLinus Torvalds##
17661da177e4SLinus Torvalds# takes a function prototype and the name of the current file being
17671da177e4SLinus Torvalds# processed and spits out all the details stored in the global
17681da177e4SLinus Torvalds# arrays/hashes.
17691da177e4SLinus Torvaldssub dump_function($$) {
17701da177e4SLinus Torvalds    my $prototype = shift;
17711da177e4SLinus Torvalds    my $file = shift;
17721da177e4SLinus Torvalds
17731da177e4SLinus Torvalds    $prototype =~ s/^static +//;
17741da177e4SLinus Torvalds    $prototype =~ s/^extern +//;
17754dc3b16bSPavel Pisa    $prototype =~ s/^asmlinkage +//;
17761da177e4SLinus Torvalds    $prototype =~ s/^inline +//;
17771da177e4SLinus Torvalds    $prototype =~ s/^__inline__ +//;
177832e79401SRandy Dunlap    $prototype =~ s/^__inline +//;
177932e79401SRandy Dunlap    $prototype =~ s/^__always_inline +//;
178032e79401SRandy Dunlap    $prototype =~ s/^noinline +//;
17810129a057SRandy Dunlap    $prototype =~ s/__devinit +//;
178274fc5c65SRandy Dunlap    $prototype =~ s/__init +//;
178320072205SRandy Dunlap    $prototype =~ s/__init_or_module +//;
1784890c78c2SRandy Dunlap    $prototype =~ s/^#\s*define\s+//; #ak added
1785328d2440SRandy Dunlap    $prototype =~ s/__attribute__\s*\(\([a-z,]*\)\)//;
17861da177e4SLinus Torvalds
17871da177e4SLinus Torvalds    # Yes, this truly is vile.  We are looking for:
17881da177e4SLinus Torvalds    # 1. Return type (may be nothing if we're looking at a macro)
17891da177e4SLinus Torvalds    # 2. Function name
17901da177e4SLinus Torvalds    # 3. Function parameters.
17911da177e4SLinus Torvalds    #
17921da177e4SLinus Torvalds    # All the while we have to watch out for function pointer parameters
17931da177e4SLinus Torvalds    # (which IIRC is what the two sections are for), C types (these
17941da177e4SLinus Torvalds    # regexps don't even start to express all the possibilities), and
17951da177e4SLinus Torvalds    # so on.
17961da177e4SLinus Torvalds    #
17971da177e4SLinus Torvalds    # If you mess with these regexps, it's a good idea to check that
17981da177e4SLinus Torvalds    # the following functions' documentation still comes out right:
17991da177e4SLinus Torvalds    # - parport_register_device (function pointer parameters)
18001da177e4SLinus Torvalds    # - atomic_set (macro)
18019598f91fSMartin Waitz    # - pci_match_device, __copy_to_user (long return type)
18021da177e4SLinus Torvalds
18031da177e4SLinus Torvalds    if ($prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
18041da177e4SLinus Torvalds	$prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
18051da177e4SLinus Torvalds	$prototype =~ m/^(\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
18061da177e4SLinus Torvalds	$prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
180794b3e03cSRandy Dunlap	$prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
18081da177e4SLinus Torvalds	$prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
18091da177e4SLinus Torvalds	$prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
18101da177e4SLinus Torvalds	$prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
18111da177e4SLinus Torvalds	$prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
18121da177e4SLinus Torvalds	$prototype =~ m/^(\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
18131da177e4SLinus Torvalds	$prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
18141da177e4SLinus Torvalds	$prototype =~ m/^(\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
18151da177e4SLinus Torvalds	$prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
18169598f91fSMartin Waitz	$prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
18179598f91fSMartin Waitz	$prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1818412ecd77SRandy Dunlap	$prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1819412ecd77SRandy Dunlap	$prototype =~ m/^(\w+\s+\w+\s*\*\s*\w+\s*\*\s*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/)  {
18201da177e4SLinus Torvalds	$return_type = $1;
18211da177e4SLinus Torvalds	$declaration_name = $2;
18221da177e4SLinus Torvalds	my $args = $3;
18231da177e4SLinus Torvalds
18241da177e4SLinus Torvalds	create_parameterlist($args, ',', $file);
18251da177e4SLinus Torvalds    } else {
18261da177e4SLinus Torvalds	print STDERR "Error(${file}:$.): cannot understand prototype: '$prototype'\n";
18271da177e4SLinus Torvalds	++$errors;
18281da177e4SLinus Torvalds	return;
18291da177e4SLinus Torvalds    }
18301da177e4SLinus Torvalds
1831a1d94aa5SRandy Dunlap	my $prms = join " ", @parameterlist;
1832a1d94aa5SRandy Dunlap	check_sections($file, $declaration_name, "function", $sectcheck, $prms, "");
1833a1d94aa5SRandy Dunlap
18341da177e4SLinus Torvalds    output_declaration($declaration_name,
18351da177e4SLinus Torvalds		       'function',
18361da177e4SLinus Torvalds		       {'function' => $declaration_name,
18371da177e4SLinus Torvalds			'module' => $modulename,
18381da177e4SLinus Torvalds			'functiontype' => $return_type,
18391da177e4SLinus Torvalds			'parameterlist' => \@parameterlist,
18401da177e4SLinus Torvalds			'parameterdescs' => \%parameterdescs,
18411da177e4SLinus Torvalds			'parametertypes' => \%parametertypes,
18421da177e4SLinus Torvalds			'sectionlist' => \@sectionlist,
18431da177e4SLinus Torvalds			'sections' => \%sections,
18441da177e4SLinus Torvalds			'purpose' => $declaration_purpose
18451da177e4SLinus Torvalds		       });
18461da177e4SLinus Torvalds}
18471da177e4SLinus Torvalds
18481da177e4SLinus Torvaldssub process_file($);
18491da177e4SLinus Torvalds
18501da177e4SLinus Torvalds# Read the file that maps relative names to absolute names for
18511da177e4SLinus Torvalds# separate source and object directories and for shadow trees.
18521da177e4SLinus Torvaldsif (open(SOURCE_MAP, "<.tmp_filelist.txt")) {
18531da177e4SLinus Torvalds	my ($relname, $absname);
18541da177e4SLinus Torvalds	while(<SOURCE_MAP>) {
18551da177e4SLinus Torvalds		chop();
18561da177e4SLinus Torvalds		($relname, $absname) = (split())[0..1];
18571da177e4SLinus Torvalds		$relname =~ s:^/+::;
18581da177e4SLinus Torvalds		$source_map{$relname} = $absname;
18591da177e4SLinus Torvalds	}
18601da177e4SLinus Torvalds	close(SOURCE_MAP);
18611da177e4SLinus Torvalds}
18621da177e4SLinus Torvalds
18631da177e4SLinus Torvaldsforeach (@ARGV) {
18641da177e4SLinus Torvalds    chomp;
18651da177e4SLinus Torvalds    process_file($_);
18661da177e4SLinus Torvalds}
18671da177e4SLinus Torvaldsif ($verbose && $errors) {
18681da177e4SLinus Torvalds  print STDERR "$errors errors\n";
18691da177e4SLinus Torvalds}
18701da177e4SLinus Torvaldsif ($verbose && $warnings) {
18711da177e4SLinus Torvalds  print STDERR "$warnings warnings\n";
18721da177e4SLinus Torvalds}
18731da177e4SLinus Torvalds
18741da177e4SLinus Torvaldsexit($errors);
18751da177e4SLinus Torvalds
18761da177e4SLinus Torvaldssub reset_state {
18771da177e4SLinus Torvalds    $function = "";
18781da177e4SLinus Torvalds    %constants = ();
18791da177e4SLinus Torvalds    %parameterdescs = ();
18801da177e4SLinus Torvalds    %parametertypes = ();
18811da177e4SLinus Torvalds    @parameterlist = ();
18821da177e4SLinus Torvalds    %sections = ();
18831da177e4SLinus Torvalds    @sectionlist = ();
1884a1d94aa5SRandy Dunlap    $sectcheck = "";
1885a1d94aa5SRandy Dunlap    $struct_actual = "";
18861da177e4SLinus Torvalds    $prototype = "";
18871da177e4SLinus Torvalds
18881da177e4SLinus Torvalds    $state = 0;
18891da177e4SLinus Torvalds}
18901da177e4SLinus Torvalds
189156afb0f8SJason Baronsub tracepoint_munge($) {
189256afb0f8SJason Baron	my $file = shift;
189356afb0f8SJason Baron	my $tracepointname = 0;
189456afb0f8SJason Baron	my $tracepointargs = 0;
189556afb0f8SJason Baron
189656afb0f8SJason Baron	if ($prototype =~ m/TRACE_EVENT\((.*?),/) {
189756afb0f8SJason Baron		$tracepointname = $1;
189856afb0f8SJason Baron	}
18993a9089fdSJason Baron	if ($prototype =~ m/DEFINE_SINGLE_EVENT\((.*?),/) {
19003a9089fdSJason Baron		$tracepointname = $1;
19013a9089fdSJason Baron	}
19023a9089fdSJason Baron	if ($prototype =~ m/DEFINE_EVENT\((.*?),(.*?),/) {
19033a9089fdSJason Baron		$tracepointname = $2;
19043a9089fdSJason Baron	}
19053a9089fdSJason Baron	$tracepointname =~ s/^\s+//; #strip leading whitespace
190656afb0f8SJason Baron	if ($prototype =~ m/TP_PROTO\((.*?)\)/) {
190756afb0f8SJason Baron		$tracepointargs = $1;
190856afb0f8SJason Baron	}
190956afb0f8SJason Baron	if (($tracepointname eq 0) || ($tracepointargs eq 0)) {
191056afb0f8SJason Baron		print STDERR "Warning(${file}:$.): Unrecognized tracepoint format: \n".
191156afb0f8SJason Baron			     "$prototype\n";
191256afb0f8SJason Baron	} else {
191356afb0f8SJason Baron		$prototype = "static inline void trace_$tracepointname($tracepointargs)";
191456afb0f8SJason Baron	}
191556afb0f8SJason Baron}
191656afb0f8SJason Baron
1917b4870bc5SRandy Dunlapsub syscall_munge() {
1918b4870bc5SRandy Dunlap	my $void = 0;
1919b4870bc5SRandy Dunlap
1920b4870bc5SRandy Dunlap	$prototype =~ s@[\r\n\t]+@ @gos; # strip newlines/CR's/tabs
1921b4870bc5SRandy Dunlap##	if ($prototype =~ m/SYSCALL_DEFINE0\s*\(\s*(a-zA-Z0-9_)*\s*\)/) {
1922b4870bc5SRandy Dunlap	if ($prototype =~ m/SYSCALL_DEFINE0/) {
1923b4870bc5SRandy Dunlap		$void = 1;
1924b4870bc5SRandy Dunlap##		$prototype = "long sys_$1(void)";
1925b4870bc5SRandy Dunlap	}
1926b4870bc5SRandy Dunlap
1927b4870bc5SRandy Dunlap	$prototype =~ s/SYSCALL_DEFINE.*\(/long sys_/; # fix return type & func name
1928b4870bc5SRandy Dunlap	if ($prototype =~ m/long (sys_.*?),/) {
1929b4870bc5SRandy Dunlap		$prototype =~ s/,/\(/;
1930b4870bc5SRandy Dunlap	} elsif ($void) {
1931b4870bc5SRandy Dunlap		$prototype =~ s/\)/\(void\)/;
1932b4870bc5SRandy Dunlap	}
1933b4870bc5SRandy Dunlap
1934b4870bc5SRandy Dunlap	# now delete all of the odd-number commas in $prototype
1935b4870bc5SRandy Dunlap	# so that arg types & arg names don't have a comma between them
1936b4870bc5SRandy Dunlap	my $count = 0;
1937b4870bc5SRandy Dunlap	my $len = length($prototype);
1938b4870bc5SRandy Dunlap	if ($void) {
1939b4870bc5SRandy Dunlap		$len = 0;	# skip the for-loop
1940b4870bc5SRandy Dunlap	}
1941b4870bc5SRandy Dunlap	for (my $ix = 0; $ix < $len; $ix++) {
1942b4870bc5SRandy Dunlap		if (substr($prototype, $ix, 1) eq ',') {
1943b4870bc5SRandy Dunlap			$count++;
1944b4870bc5SRandy Dunlap			if ($count % 2 == 1) {
1945b4870bc5SRandy Dunlap				substr($prototype, $ix, 1) = ' ';
1946b4870bc5SRandy Dunlap			}
1947b4870bc5SRandy Dunlap		}
1948b4870bc5SRandy Dunlap	}
1949b4870bc5SRandy Dunlap}
1950b4870bc5SRandy Dunlap
19511da177e4SLinus Torvaldssub process_state3_function($$) {
19521da177e4SLinus Torvalds    my $x = shift;
19531da177e4SLinus Torvalds    my $file = shift;
19541da177e4SLinus Torvalds
195551f5a0c8SRandy Dunlap    $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
195651f5a0c8SRandy Dunlap
1957890c78c2SRandy Dunlap    if ($x =~ m#\s*/\*\s+MACDOC\s*#io || ($x =~ /^#/ && $x !~ /^#\s*define/)) {
19581da177e4SLinus Torvalds	# do nothing
19591da177e4SLinus Torvalds    }
19601da177e4SLinus Torvalds    elsif ($x =~ /([^\{]*)/) {
19611da177e4SLinus Torvalds	$prototype .= $1;
19621da177e4SLinus Torvalds    }
1963b4870bc5SRandy Dunlap
1964890c78c2SRandy Dunlap    if (($x =~ /\{/) || ($x =~ /\#\s*define/) || ($x =~ /;/)) {
19651da177e4SLinus Torvalds	$prototype =~ s@/\*.*?\*/@@gos;	# strip comments.
19661da177e4SLinus Torvalds	$prototype =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
19671da177e4SLinus Torvalds	$prototype =~ s@^\s+@@gos; # strip leading spaces
1968b4870bc5SRandy Dunlap	if ($prototype =~ /SYSCALL_DEFINE/) {
1969b4870bc5SRandy Dunlap		syscall_munge();
1970b4870bc5SRandy Dunlap	}
19713a9089fdSJason Baron	if ($prototype =~ /TRACE_EVENT/ || $prototype =~ /DEFINE_EVENT/ ||
19723a9089fdSJason Baron	    $prototype =~ /DEFINE_SINGLE_EVENT/)
19733a9089fdSJason Baron	{
197456afb0f8SJason Baron		tracepoint_munge($file);
197556afb0f8SJason Baron	}
19761da177e4SLinus Torvalds	dump_function($prototype, $file);
19771da177e4SLinus Torvalds	reset_state();
19781da177e4SLinus Torvalds    }
19791da177e4SLinus Torvalds}
19801da177e4SLinus Torvalds
19811da177e4SLinus Torvaldssub process_state3_type($$) {
19821da177e4SLinus Torvalds    my $x = shift;
19831da177e4SLinus Torvalds    my $file = shift;
19841da177e4SLinus Torvalds
19851da177e4SLinus Torvalds    $x =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
19861da177e4SLinus Torvalds    $x =~ s@^\s+@@gos; # strip leading spaces
19871da177e4SLinus Torvalds    $x =~ s@\s+$@@gos; # strip trailing spaces
198851f5a0c8SRandy Dunlap    $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
198951f5a0c8SRandy Dunlap
19901da177e4SLinus Torvalds    if ($x =~ /^#/) {
19911da177e4SLinus Torvalds	# To distinguish preprocessor directive from regular declaration later.
19921da177e4SLinus Torvalds	$x .= ";";
19931da177e4SLinus Torvalds    }
19941da177e4SLinus Torvalds
19951da177e4SLinus Torvalds    while (1) {
19961da177e4SLinus Torvalds	if ( $x =~ /([^{};]*)([{};])(.*)/ ) {
19971da177e4SLinus Torvalds	    $prototype .= $1 . $2;
19981da177e4SLinus Torvalds	    ($2 eq '{') && $brcount++;
19991da177e4SLinus Torvalds	    ($2 eq '}') && $brcount--;
20001da177e4SLinus Torvalds	    if (($2 eq ';') && ($brcount == 0)) {
20011da177e4SLinus Torvalds		dump_declaration($prototype, $file);
20021da177e4SLinus Torvalds		reset_state();
20031da177e4SLinus Torvalds		last;
20041da177e4SLinus Torvalds	    }
20051da177e4SLinus Torvalds	    $x = $3;
20061da177e4SLinus Torvalds	} else {
20071da177e4SLinus Torvalds	    $prototype .= $x;
20081da177e4SLinus Torvalds	    last;
20091da177e4SLinus Torvalds	}
20101da177e4SLinus Torvalds    }
20111da177e4SLinus Torvalds}
20121da177e4SLinus Torvalds
20136b5b55f6SRandy Dunlap# xml_escape: replace <, >, and & in the text stream;
20146b5b55f6SRandy Dunlap#
20156b5b55f6SRandy Dunlap# however, formatting controls that are generated internally/locally in the
20166b5b55f6SRandy Dunlap# kernel-doc script are not escaped here; instead, they begin life like
20176b5b55f6SRandy Dunlap# $blankline_html (4 of '\' followed by a mnemonic + ':'), then these strings
20186b5b55f6SRandy Dunlap# are converted to their mnemonic-expected output, without the 4 * '\' & ':',
20196b5b55f6SRandy Dunlap# just before actual output; (this is done by local_unescape())
20201da177e4SLinus Torvaldssub xml_escape($) {
20211da177e4SLinus Torvalds	my $text = shift;
2022ecfb251aSRandy Dunlap	if (($output_mode eq "text") || ($output_mode eq "man")) {
2023ecfb251aSRandy Dunlap		return $text;
2024ecfb251aSRandy Dunlap	}
20251da177e4SLinus Torvalds	$text =~ s/\&/\\\\\\amp;/g;
20261da177e4SLinus Torvalds	$text =~ s/\</\\\\\\lt;/g;
20271da177e4SLinus Torvalds	$text =~ s/\>/\\\\\\gt;/g;
20281da177e4SLinus Torvalds	return $text;
20291da177e4SLinus Torvalds}
20301da177e4SLinus Torvalds
20316b5b55f6SRandy Dunlap# convert local escape strings to html
20326b5b55f6SRandy Dunlap# local escape strings look like:  '\\\\menmonic:' (that's 4 backslashes)
20336b5b55f6SRandy Dunlapsub local_unescape($) {
20346b5b55f6SRandy Dunlap	my $text = shift;
20356b5b55f6SRandy Dunlap	if (($output_mode eq "text") || ($output_mode eq "man")) {
20366b5b55f6SRandy Dunlap		return $text;
20376b5b55f6SRandy Dunlap	}
20386b5b55f6SRandy Dunlap	$text =~ s/\\\\\\\\lt:/</g;
20396b5b55f6SRandy Dunlap	$text =~ s/\\\\\\\\gt:/>/g;
20406b5b55f6SRandy Dunlap	return $text;
20416b5b55f6SRandy Dunlap}
20426b5b55f6SRandy Dunlap
20431da177e4SLinus Torvaldssub process_file($) {
20442283a117SRandy Dunlap    my $file;
20451da177e4SLinus Torvalds    my $identifier;
20461da177e4SLinus Torvalds    my $func;
2047a21217daSRandy Dunlap    my $descr;
20486423133bSJohannes Weiner    my $in_purpose = 0;
20491da177e4SLinus Torvalds    my $initial_section_counter = $section_counter;
20501da177e4SLinus Torvalds
20512283a117SRandy Dunlap    if (defined($ENV{'SRCTREE'})) {
20522283a117SRandy Dunlap	$file = "$ENV{'SRCTREE'}" . "/" . "@_";
20532283a117SRandy Dunlap    }
20542283a117SRandy Dunlap    else {
20552283a117SRandy Dunlap	$file = "@_";
20562283a117SRandy Dunlap    }
20571da177e4SLinus Torvalds    if (defined($source_map{$file})) {
20581da177e4SLinus Torvalds	$file = $source_map{$file};
20591da177e4SLinus Torvalds    }
20601da177e4SLinus Torvalds
20611da177e4SLinus Torvalds    if (!open(IN,"<$file")) {
20621da177e4SLinus Torvalds	print STDERR "Error: Cannot open file $file\n";
20631da177e4SLinus Torvalds	++$errors;
20641da177e4SLinus Torvalds	return;
20651da177e4SLinus Torvalds    }
20661da177e4SLinus Torvalds
2067a9e7314bSIlya Dryomov    $. = 1;
2068a9e7314bSIlya Dryomov
20691da177e4SLinus Torvalds    $section_counter = 0;
20701da177e4SLinus Torvalds    while (<IN>) {
20711da177e4SLinus Torvalds	if ($state == 0) {
20721da177e4SLinus Torvalds	    if (/$doc_start/o) {
20731da177e4SLinus Torvalds		$state = 1;		# next line is always the function name
2074850622dfSRandy Dunlap		$in_doc_sect = 0;
20751da177e4SLinus Torvalds	    }
20761da177e4SLinus Torvalds	} elsif ($state == 1) {	# this line is the function name (always)
20771da177e4SLinus Torvalds	    if (/$doc_block/o) {
20781da177e4SLinus Torvalds		$state = 4;
20791da177e4SLinus Torvalds		$contents = "";
20801da177e4SLinus Torvalds		if ( $1 eq "" ) {
20811da177e4SLinus Torvalds			$section = $section_intro;
20821da177e4SLinus Torvalds		} else {
20831da177e4SLinus Torvalds			$section = $1;
20841da177e4SLinus Torvalds		}
20851da177e4SLinus Torvalds	    }
20861da177e4SLinus Torvalds	    elsif (/$doc_decl/o) {
20871da177e4SLinus Torvalds		$identifier = $1;
20881da177e4SLinus Torvalds		if (/\s*([\w\s]+?)\s*-/) {
20891da177e4SLinus Torvalds		    $identifier = $1;
20901da177e4SLinus Torvalds		}
20911da177e4SLinus Torvalds
20921da177e4SLinus Torvalds		$state = 2;
20931da177e4SLinus Torvalds		if (/-(.*)/) {
209451f5a0c8SRandy Dunlap		    # strip leading/trailing/multiple spaces
2095a21217daSRandy Dunlap		    $descr= $1;
2096a21217daSRandy Dunlap		    $descr =~ s/^\s*//;
2097a21217daSRandy Dunlap		    $descr =~ s/\s*$//;
2098a21217daSRandy Dunlap		    $descr =~ s/\s+/ /;
2099a21217daSRandy Dunlap		    $declaration_purpose = xml_escape($descr);
21006423133bSJohannes Weiner		    $in_purpose = 1;
21011da177e4SLinus Torvalds		} else {
21021da177e4SLinus Torvalds		    $declaration_purpose = "";
21031da177e4SLinus Torvalds		}
210477cc23b8SRandy Dunlap
210577cc23b8SRandy Dunlap		if (($declaration_purpose eq "") && $verbose) {
210677cc23b8SRandy Dunlap			print STDERR "Warning(${file}:$.): missing initial short description on line:\n";
210777cc23b8SRandy Dunlap			print STDERR $_;
210877cc23b8SRandy Dunlap			++$warnings;
210977cc23b8SRandy Dunlap		}
211077cc23b8SRandy Dunlap
21111da177e4SLinus Torvalds		if ($identifier =~ m/^struct/) {
21121da177e4SLinus Torvalds		    $decl_type = 'struct';
21131da177e4SLinus Torvalds		} elsif ($identifier =~ m/^union/) {
21141da177e4SLinus Torvalds		    $decl_type = 'union';
21151da177e4SLinus Torvalds		} elsif ($identifier =~ m/^enum/) {
21161da177e4SLinus Torvalds		    $decl_type = 'enum';
21171da177e4SLinus Torvalds		} elsif ($identifier =~ m/^typedef/) {
21181da177e4SLinus Torvalds		    $decl_type = 'typedef';
21191da177e4SLinus Torvalds		} else {
21201da177e4SLinus Torvalds		    $decl_type = 'function';
21211da177e4SLinus Torvalds		}
21221da177e4SLinus Torvalds
21231da177e4SLinus Torvalds		if ($verbose) {
21241da177e4SLinus Torvalds		    print STDERR "Info(${file}:$.): Scanning doc for $identifier\n";
21251da177e4SLinus Torvalds		}
21261da177e4SLinus Torvalds	    } else {
21271da177e4SLinus Torvalds		print STDERR "Warning(${file}:$.): Cannot understand $_ on line $.",
21281da177e4SLinus Torvalds		" - I thought it was a doc line\n";
21291da177e4SLinus Torvalds		++$warnings;
21301da177e4SLinus Torvalds		$state = 0;
21311da177e4SLinus Torvalds	    }
21321da177e4SLinus Torvalds	} elsif ($state == 2) {	# look for head: lines, and include content
21331da177e4SLinus Torvalds	    if (/$doc_sect/o) {
21341da177e4SLinus Torvalds		$newsection = $1;
21351da177e4SLinus Torvalds		$newcontents = $2;
21361da177e4SLinus Torvalds
2137792aa2f2SRandy Dunlap		if (($contents ne "") && ($contents ne "\n")) {
2138850622dfSRandy Dunlap		    if (!$in_doc_sect && $verbose) {
2139850622dfSRandy Dunlap			print STDERR "Warning(${file}:$.): contents before sections\n";
2140850622dfSRandy Dunlap			++$warnings;
2141850622dfSRandy Dunlap		    }
214294dc7ad5SRandy Dunlap		    dump_section($file, $section, xml_escape($contents));
21431da177e4SLinus Torvalds		    $section = $section_default;
21441da177e4SLinus Torvalds		}
21451da177e4SLinus Torvalds
2146850622dfSRandy Dunlap		$in_doc_sect = 1;
21476423133bSJohannes Weiner		$in_purpose = 0;
21481da177e4SLinus Torvalds		$contents = $newcontents;
21491da177e4SLinus Torvalds		if ($contents ne "") {
215027205744SRandy Dunlap		    while ((substr($contents, 0, 1) eq " ") ||
215127205744SRandy Dunlap			substr($contents, 0, 1) eq "\t") {
215205189497SRandy Dunlap			    $contents = substr($contents, 1);
215305189497SRandy Dunlap		    }
21541da177e4SLinus Torvalds		    $contents .= "\n";
21551da177e4SLinus Torvalds		}
21561da177e4SLinus Torvalds		$section = $newsection;
21571da177e4SLinus Torvalds	    } elsif (/$doc_end/) {
21581da177e4SLinus Torvalds
21594c98ecafSRandy Dunlap		if (($contents ne "") && ($contents ne "\n")) {
216094dc7ad5SRandy Dunlap		    dump_section($file, $section, xml_escape($contents));
21611da177e4SLinus Torvalds		    $section = $section_default;
21621da177e4SLinus Torvalds		    $contents = "";
21631da177e4SLinus Torvalds		}
216446b958ebSRandy Dunlap		# look for doc_com + <text> + doc_end:
216546b958ebSRandy Dunlap		if ($_ =~ m'\s*\*\s*[a-zA-Z_0-9:\.]+\*/') {
216646b958ebSRandy Dunlap		    print STDERR "Warning(${file}:$.): suspicious ending line: $_";
216746b958ebSRandy Dunlap		    ++$warnings;
216846b958ebSRandy Dunlap		}
21691da177e4SLinus Torvalds
21701da177e4SLinus Torvalds		$prototype = "";
21711da177e4SLinus Torvalds		$state = 3;
21721da177e4SLinus Torvalds		$brcount = 0;
21731da177e4SLinus Torvalds#		print STDERR "end of doc comment, looking for prototype\n";
21741da177e4SLinus Torvalds	    } elsif (/$doc_content/) {
21751da177e4SLinus Torvalds		# miguel-style comment kludge, look for blank lines after
21761da177e4SLinus Torvalds		# @parameter line to signify start of description
21776423133bSJohannes Weiner		if ($1 eq "") {
21786423133bSJohannes Weiner		    if ($section =~ m/^@/ || $section eq $section_context) {
217994dc7ad5SRandy Dunlap			dump_section($file, $section, xml_escape($contents));
21801da177e4SLinus Torvalds			$section = $section_default;
21811da177e4SLinus Torvalds			$contents = "";
21821da177e4SLinus Torvalds		    } else {
21836423133bSJohannes Weiner			$contents .= "\n";
21846423133bSJohannes Weiner		    }
21856423133bSJohannes Weiner		    $in_purpose = 0;
21866423133bSJohannes Weiner		} elsif ($in_purpose == 1) {
21876423133bSJohannes Weiner		    # Continued declaration purpose
21886423133bSJohannes Weiner		    chomp($declaration_purpose);
21896423133bSJohannes Weiner		    $declaration_purpose .= " " . xml_escape($1);
21906423133bSJohannes Weiner		} else {
21911da177e4SLinus Torvalds		    $contents .= $1 . "\n";
21921da177e4SLinus Torvalds		}
21931da177e4SLinus Torvalds	    } else {
21941da177e4SLinus Torvalds		# i dont know - bad line?  ignore.
21951da177e4SLinus Torvalds		print STDERR "Warning(${file}:$.): bad line: $_";
21961da177e4SLinus Torvalds		++$warnings;
21971da177e4SLinus Torvalds	    }
2198232acbcfSRandy Dunlap	} elsif ($state == 3) {	# scanning for function '{' (end of prototype)
21991da177e4SLinus Torvalds	    if ($decl_type eq 'function') {
22001da177e4SLinus Torvalds		process_state3_function($_, $file);
22011da177e4SLinus Torvalds	    } else {
22021da177e4SLinus Torvalds		process_state3_type($_, $file);
22031da177e4SLinus Torvalds	    }
22041da177e4SLinus Torvalds	} elsif ($state == 4) {
22051da177e4SLinus Torvalds		# Documentation block
22061da177e4SLinus Torvalds		if (/$doc_block/) {
220794dc7ad5SRandy Dunlap			dump_doc_section($file, $section, xml_escape($contents));
22081da177e4SLinus Torvalds			$contents = "";
22091da177e4SLinus Torvalds			$function = "";
22101da177e4SLinus Torvalds			%constants = ();
22111da177e4SLinus Torvalds			%parameterdescs = ();
22121da177e4SLinus Torvalds			%parametertypes = ();
22131da177e4SLinus Torvalds			@parameterlist = ();
22141da177e4SLinus Torvalds			%sections = ();
22151da177e4SLinus Torvalds			@sectionlist = ();
22161da177e4SLinus Torvalds			$prototype = "";
22171da177e4SLinus Torvalds			if ( $1 eq "" ) {
22181da177e4SLinus Torvalds				$section = $section_intro;
22191da177e4SLinus Torvalds			} else {
22201da177e4SLinus Torvalds				$section = $1;
22211da177e4SLinus Torvalds			}
22221da177e4SLinus Torvalds		}
22231da177e4SLinus Torvalds		elsif (/$doc_end/)
22241da177e4SLinus Torvalds		{
222594dc7ad5SRandy Dunlap			dump_doc_section($file, $section, xml_escape($contents));
22261da177e4SLinus Torvalds			$contents = "";
22271da177e4SLinus Torvalds			$function = "";
22281da177e4SLinus Torvalds			%constants = ();
22291da177e4SLinus Torvalds			%parameterdescs = ();
22301da177e4SLinus Torvalds			%parametertypes = ();
22311da177e4SLinus Torvalds			@parameterlist = ();
22321da177e4SLinus Torvalds			%sections = ();
22331da177e4SLinus Torvalds			@sectionlist = ();
22341da177e4SLinus Torvalds			$prototype = "";
22351da177e4SLinus Torvalds			$state = 0;
22361da177e4SLinus Torvalds		}
22371da177e4SLinus Torvalds		elsif (/$doc_content/)
22381da177e4SLinus Torvalds		{
22391da177e4SLinus Torvalds			if ( $1 eq "" )
22401da177e4SLinus Torvalds			{
22411da177e4SLinus Torvalds				$contents .= $blankline;
22421da177e4SLinus Torvalds			}
22431da177e4SLinus Torvalds			else
22441da177e4SLinus Torvalds			{
22451da177e4SLinus Torvalds				$contents .= $1 . "\n";
22461da177e4SLinus Torvalds			}
22471da177e4SLinus Torvalds		}
22481da177e4SLinus Torvalds	}
22491da177e4SLinus Torvalds    }
22501da177e4SLinus Torvalds    if ($initial_section_counter == $section_counter) {
22511da177e4SLinus Torvalds	print STDERR "Warning(${file}): no structured comments found\n";
22521da177e4SLinus Torvalds	if ($output_mode eq "xml") {
22531da177e4SLinus Torvalds	    # The template wants at least one RefEntry here; make one.
22541da177e4SLinus Torvalds	    print "<refentry>\n";
22551da177e4SLinus Torvalds	    print " <refnamediv>\n";
22561da177e4SLinus Torvalds	    print "  <refname>\n";
22571da177e4SLinus Torvalds	    print "   ${file}\n";
22581da177e4SLinus Torvalds	    print "  </refname>\n";
22591da177e4SLinus Torvalds	    print "  <refpurpose>\n";
22601da177e4SLinus Torvalds	    print "   Document generation inconsistency\n";
22611da177e4SLinus Torvalds	    print "  </refpurpose>\n";
22621da177e4SLinus Torvalds	    print " </refnamediv>\n";
22631da177e4SLinus Torvalds	    print " <refsect1>\n";
22641da177e4SLinus Torvalds	    print "  <title>\n";
22651da177e4SLinus Torvalds	    print "   Oops\n";
22661da177e4SLinus Torvalds	    print "  </title>\n";
22671da177e4SLinus Torvalds	    print "  <warning>\n";
22681da177e4SLinus Torvalds	    print "   <para>\n";
22691da177e4SLinus Torvalds	    print "    The template for this document tried to insert\n";
22701da177e4SLinus Torvalds	    print "    the structured comment from the file\n";
22711da177e4SLinus Torvalds	    print "    <filename>${file}</filename> at this point,\n";
22721da177e4SLinus Torvalds	    print "    but none was found.\n";
22731da177e4SLinus Torvalds	    print "    This dummy section is inserted to allow\n";
22741da177e4SLinus Torvalds	    print "    generation to continue.\n";
22751da177e4SLinus Torvalds	    print "   </para>\n";
22761da177e4SLinus Torvalds	    print "  </warning>\n";
22771da177e4SLinus Torvalds	    print " </refsect1>\n";
22781da177e4SLinus Torvalds	    print "</refentry>\n";
22791da177e4SLinus Torvalds	}
22801da177e4SLinus Torvalds    }
22811da177e4SLinus Torvalds}
2282