blob: 0276d2b5eefe31218adf9475d1f263be6fe979bb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#!/usr/bin/perl -w
2
3use strict;
4
5## Copyright (c) 1998 Michael Zucchi, All Rights Reserved ##
6## Copyright (C) 2000, 1 Tim Waugh <twaugh@redhat.com> ##
7## Copyright (C) 2001 Simon Huggins ##
Randy Dunlap70c95b02012-01-21 10:31:54 -08008## Copyright (C) 2005-2012 Randy Dunlap ##
Dan Luedtke1b40c192012-08-12 10:46:15 +02009## Copyright (C) 2012 Dan Luedtke ##
Linus Torvalds1da177e2005-04-16 15:20:36 -070010## ##
11## #define enhancements by Armin Kuster <akuster@mvista.com> ##
12## Copyright (c) 2000 MontaVista Software, Inc. ##
13## ##
14## This software falls under the GNU General Public License. ##
15## Please read the COPYING file for more information ##
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017# 18/01/2001 - Cleanups
18# Functions prototyped as foo(void) same as foo()
19# Stop eval'ing where we don't need to.
20# -- huggie@earth.li
21
22# 27/06/2001 - Allowed whitespace after initial "/**" and
23# allowed comments before function declarations.
24# -- Christian Kreibich <ck@whoop.org>
25
26# Still to do:
27# - add perldoc documentation
28# - Look more closely at some of the scarier bits :)
29
30# 26/05/2001 - Support for separate source and object trees.
31# Return error code.
32# Keith Owens <kaos@ocs.com.au>
33
34# 23/09/2001 - Added support for typedefs, structs, enums and unions
35# Support for Context section; can be terminated using empty line
36# Small fixes (like spaces vs. \s in regex)
37# -- Tim Jansen <tim@tjansen.de>
38
Dan Luedtke1b40c192012-08-12 10:46:15 +020039# 25/07/2012 - Added support for HTML5
40# -- Dan Luedtke <mail@danrl.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#
43# This will read a 'c' file and scan for embedded comments in the
44# style of gnome comments (+minor extensions - see below).
45#
46
47# Note: This only supports 'c'.
48
49# usage:
Dan Luedtke1b40c192012-08-12 10:46:15 +020050# kernel-doc [ -docbook | -html | -html5 | -text | -man | -list ]
51# [ -no-doc-sections ]
52# [ -function funcname [ -function funcname ...] ]
53# c file(s)s > outputfile
Linus Torvalds1da177e2005-04-16 15:20:36 -070054# or
Dan Luedtke1b40c192012-08-12 10:46:15 +020055# [ -nofunction funcname [ -function funcname ...] ]
56# c file(s)s > outputfile
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#
Dan Luedtke1b40c192012-08-12 10:46:15 +020058# Set output format using one of -docbook -html -html5 -text or -man.
59# Default is man.
Johannes Bergeda603f2010-09-11 15:55:22 -070060# The -list format is for internal use by docproc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#
Johannes Berg4b445952007-10-24 15:08:48 -070062# -no-doc-sections
63# Do not output DOC: sections
64#
Linus Torvalds1da177e2005-04-16 15:20:36 -070065# -function funcname
Johannes Bergb112e0f2007-10-24 15:08:48 -070066# If set, then only generate documentation for the given function(s) or
67# DOC: section titles. All other functions and DOC: sections are ignored.
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#
69# -nofunction funcname
Johannes Bergb112e0f2007-10-24 15:08:48 -070070# If set, then only generate documentation for the other function(s)/DOC:
71# sections. Cannot be used together with -function (yes, that's a bug --
72# perl hackers can fix it 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#
74# c files - list of 'c' files to process
75#
76# All output goes to stdout, with errors to stderr.
77
78#
79# format of comments.
80# In the following table, (...)? signifies optional structure.
81# (...)* signifies 0 or more structure elements
82# /**
83# * function_name(:)? (- short description)?
84# (* @parameterx: (description of parameter x)?)*
85# (* a blank line)?
86# * (Description:)? (Description of function)?
87# * (section header: (section description)? )*
88# (*)?*/
89#
90# So .. the trivial example would be:
91#
92# /**
93# * my_function
Randy Dunlapb9d973282009-06-09 08:50:38 -070094# */
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#
Randy Dunlap891dcd22007-02-10 01:45:53 -080096# If the Description: header tag is omitted, then there must be a blank line
Linus Torvalds1da177e2005-04-16 15:20:36 -070097# after the last parameter specification.
98# e.g.
99# /**
100# * my_function - does my stuff
101# * @my_arg: its mine damnit
102# *
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800103# * Does my stuff explained.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104# */
105#
106# or, could also use:
107# /**
108# * my_function - does my stuff
109# * @my_arg: its mine damnit
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800110# * Description: Does my stuff explained.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111# */
112# etc.
113#
Randy Dunlapb9d973282009-06-09 08:50:38 -0700114# Besides functions you can also write documentation for structs, unions,
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800115# enums and typedefs. Instead of the function name you must write the name
116# of the declaration; the struct/union/enum/typedef must always precede
117# the name. Nesting of declarations is not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118# Use the argument mechanism to document members or constants.
119# e.g.
120# /**
121# * struct my_struct - short description
122# * @a: first member
123# * @b: second member
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800124# *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125# * Longer description
126# */
127# struct my_struct {
128# int a;
129# int b;
Martin Waitzaeec46b2005-11-13 16:08:13 -0800130# /* private: */
131# int c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132# };
133#
134# All descriptions can be multiline, except the short function description.
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800135#
Danilo Cesar Lemes de Paulaa4c6ebe2015-08-04 09:04:08 -0300136# For really longs structs, you can also describe arguments inside the
137# body of the struct.
138# eg.
139# /**
140# * struct my_struct - short description
141# * @a: first member
142# * @b: second member
143# *
144# * Longer description
145# */
146# struct my_struct {
147# int a;
148# int b;
149# /**
150# * @c: This is longer description of C
151# *
152# * You can use paragraphs to describe arguments
153# * using this method.
154# */
155# int c;
156# };
157#
158# This should be use only for struct/enum members.
159#
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800160# You can also add additional sections. When documenting kernel functions you
161# should document the "Context:" of the function, e.g. whether the functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162# can be called form interrupts. Unlike other sections you can end it with an
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800163# empty line.
Yacine Belkadi4092bac2012-11-26 22:22:27 +0100164# A non-void function should have a "Return:" section describing the return
165# value(s).
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800166# Example-sections should contain the string EXAMPLE so that they are marked
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167# appropriately in DocBook.
168#
169# Example:
170# /**
171# * user_function - function that can only be called in user context
172# * @a: some argument
173# * Context: !in_interrupt()
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800174# *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175# * Some description
176# * Example:
177# * user_function(22);
178# */
179# ...
180#
181#
182# All descriptive text is further processed, scanning for the following special
183# patterns, which are highlighted appropriately.
184#
185# 'funcname()' - function
186# '$ENVVAR' - environmental variable
187# '&struct_name' - name of a structure (up to two words including 'struct')
188# '@parameter' - name of a parameter
189# '%CONST' - name of a constant.
190
Randy Dunlap8484baa2011-01-05 16:28:43 -0800191## init lots of data
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193my $errors = 0;
194my $warnings = 0;
Randy Dunlap5f8c7c92007-07-19 01:48:24 -0700195my $anon_struct_union = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197# match expressions used to find embedded type information
198my $type_constant = '\%([-_\w]+)';
199my $type_func = '(\w+)\(\)';
200my $type_param = '\@(\w+)';
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700201my $type_struct = '\&((struct\s*)*[_\w]+)';
Randy Dunlap6b5b55f2007-10-16 23:31:20 -0700202my $type_struct_xml = '\\&amp;((struct\s*)*[_\w]+)';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203my $type_env = '(\$\w+)';
204
205# Output conversion substitutions.
206# One for each output format
207
208# these work fairly well
209my %highlights_html = ( $type_constant, "<i>\$1</i>",
210 $type_func, "<b>\$1</b>",
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700211 $type_struct_xml, "<i>\$1</i>",
212 $type_env, "<b><i>\$1</i></b>",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 $type_param, "<tt><b>\$1</b></tt>" );
Randy Dunlap6b5b55f2007-10-16 23:31:20 -0700214my $local_lt = "\\\\\\\\lt:";
215my $local_gt = "\\\\\\\\gt:";
216my $blankline_html = $local_lt . "p" . $local_gt; # was "<p>"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Dan Luedtke1b40c192012-08-12 10:46:15 +0200218# html version 5
219my %highlights_html5 = ( $type_constant, "<span class=\"const\">\$1</span>",
220 $type_func, "<span class=\"func\">\$1</span>",
221 $type_struct_xml, "<span class=\"struct\">\$1</span>",
222 $type_env, "<span class=\"env\">\$1</span>",
223 $type_param, "<span class=\"param\">\$1</span>" );
224my $blankline_html5 = $local_lt . "br /" . $local_gt;
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226# XML, docbook format
227my %highlights_xml = ( "([^=])\\\"([^\\\"<]+)\\\"", "\$1<quote>\$2</quote>",
228 $type_constant, "<constant>\$1</constant>",
229 $type_func, "<function>\$1</function>",
Johannes Berg5c98fc02007-10-24 15:08:48 -0700230 $type_struct_xml, "<structname>\$1</structname>",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 $type_env, "<envar>\$1</envar>",
232 $type_param, "<parameter>\$1</parameter>" );
Johannes Berg5c98fc02007-10-24 15:08:48 -0700233my $blankline_xml = $local_lt . "/para" . $local_gt . $local_lt . "para" . $local_gt . "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235# gnome, docbook format
236my %highlights_gnome = ( $type_constant, "<replaceable class=\"option\">\$1</replaceable>",
237 $type_func, "<function>\$1</function>",
238 $type_struct, "<structname>\$1</structname>",
239 $type_env, "<envar>\$1</envar>",
240 $type_param, "<parameter>\$1</parameter>" );
241my $blankline_gnome = "</para><para>\n";
242
243# these are pretty rough
244my %highlights_man = ( $type_constant, "\$1",
245 $type_func, "\\\\fB\$1\\\\fP",
246 $type_struct, "\\\\fI\$1\\\\fP",
247 $type_param, "\\\\fI\$1\\\\fP" );
248my $blankline_man = "";
249
250# text-mode
251my %highlights_text = ( $type_constant, "\$1",
252 $type_func, "\$1",
253 $type_struct, "\$1",
254 $type_param, "\$1" );
255my $blankline_text = "";
256
Johannes Bergeda603f2010-09-11 15:55:22 -0700257# list mode
258my %highlights_list = ( $type_constant, "\$1",
259 $type_func, "\$1",
260 $type_struct, "\$1",
261 $type_param, "\$1" );
262my $blankline_list = "";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264# read arguments
Randy Dunlapb9d973282009-06-09 08:50:38 -0700265if ($#ARGV == -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 usage();
267}
268
Randy Dunlap8484baa2011-01-05 16:28:43 -0800269my $kernelversion;
270my $dohighlight = "";
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272my $verbose = 0;
273my $output_mode = "man";
Daniel Santose314ba32012-10-04 17:15:08 -0700274my $output_preformatted = 0;
Johannes Berg4b445952007-10-24 15:08:48 -0700275my $no_doc_sections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276my %highlights = %highlights_man;
277my $blankline = $blankline_man;
278my $modulename = "Kernel API";
279my $function_only = 0;
Ben Hutchingsb2c41052015-07-08 20:07:16 +0100280my $show_not_found = 0;
281
282my @build_time;
283if (defined($ENV{'KBUILD_BUILD_TIMESTAMP'}) &&
284 (my $seconds = `date -d"${ENV{'KBUILD_BUILD_TIMESTAMP'}}" +%s`) ne '') {
285 @build_time = gmtime($seconds);
286} else {
287 @build_time = localtime;
288}
289
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800290my $man_date = ('January', 'February', 'March', 'April', 'May', 'June',
291 'July', 'August', 'September', 'October',
Ben Hutchingsb2c41052015-07-08 20:07:16 +0100292 'November', 'December')[$build_time[4]] .
293 " " . ($build_time[5]+1900);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Randy Dunlap8484baa2011-01-05 16:28:43 -0800295# Essentially these are globals.
Randy Dunlapb9d973282009-06-09 08:50:38 -0700296# They probably want to be tidied up, made more localised or something.
297# CAVEAT EMPTOR! Some of the others I localised may not want to be, which
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298# could cause "use of undefined value" or other bugs.
Randy Dunlapb9d973282009-06-09 08:50:38 -0700299my ($function, %function_table, %parametertypes, $declaration_purpose);
300my ($type, $declaration_name, $return_type);
Ilya Dryomov1c32fd02010-02-26 13:06:03 -0800301my ($newsection, $newcontents, $prototype, $brcount, %source_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Randy Dunlapbd0e88e2008-03-13 12:32:43 -0700303if (defined($ENV{'KBUILD_VERBOSE'})) {
304 $verbose = "$ENV{'KBUILD_VERBOSE'}";
305}
306
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800307# Generated docbook code is inserted in a template at a point where
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308# docbook v3.1 requires a non-zero sequence of RefEntry's; see:
309# http://www.oasis-open.org/docbook/documentation/reference/html/refentry.html
310# We keep track of number of generated entries and generate a dummy
311# if needs be to ensure the expanded template can be postprocessed
312# into html.
313my $section_counter = 0;
314
315my $lineprefix="";
316
317# states
318# 0 - normal code
319# 1 - looking for function name
320# 2 - scanning field start.
321# 3 - scanning prototype.
322# 4 - documentation block
Danilo Cesar Lemes de Paulaa4c6ebe2015-08-04 09:04:08 -0300323# 5 - gathering documentation outside main block
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324my $state;
Randy Dunlap850622d2006-06-25 05:48:55 -0700325my $in_doc_sect;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Danilo Cesar Lemes de Paulaa4c6ebe2015-08-04 09:04:08 -0300327# Split Doc State
328# 0 - Invalid (Before start or after finish)
329# 1 - Is started (the /** was found inside a struct)
330# 2 - The @parameter header was found, start accepting multi paragraph text.
331# 3 - Finished (the */ was found)
332# 4 - Error - Comment without header was found. Spit a warning as it's not
333# proper kernel-doc and ignore the rest.
334my $split_doc_state;
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336#declaration types: can be
337# 'function', 'struct', 'union', 'enum', 'typedef'
338my $decl_type;
339
340my $doc_special = "\@\%\$\&";
341
342my $doc_start = '^/\*\*\s*$'; # Allow whitespace at end of comment start.
343my $doc_end = '\*/';
344my $doc_com = '\s*\*\s*';
Daniel Santos12ae6772012-10-04 17:15:10 -0700345my $doc_com_body = '\s*\* ?';
Randy Dunlapb9d973282009-06-09 08:50:38 -0700346my $doc_decl = $doc_com . '(\w+)';
347my $doc_sect = $doc_com . '([' . $doc_special . ']?[\w\s]+):(.*)';
Daniel Santos12ae6772012-10-04 17:15:10 -0700348my $doc_content = $doc_com_body . '(.*)';
Randy Dunlapb9d973282009-06-09 08:50:38 -0700349my $doc_block = $doc_com . 'DOC:\s*(.*)?';
Danilo Cesar Lemes de Paulaa4c6ebe2015-08-04 09:04:08 -0300350my $doc_split_start = '^\s*/\*\*\s*$';
351my $doc_split_sect = '\s*\*\s*(@[\w\s]+):(.*)';
352my $doc_split_end = '^\s*\*/\s*$';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354my %constants;
355my %parameterdescs;
356my @parameterlist;
357my %sections;
358my @sectionlist;
Randy Dunlapa1d94aa2008-12-19 08:49:30 -0800359my $sectcheck;
360my $struct_actual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362my $contents = "";
363my $section_default = "Description"; # default section
364my $section_intro = "Introduction";
365my $section = $section_default;
366my $section_context = "Context";
Yacine Belkadi4092bac2012-11-26 22:22:27 +0100367my $section_return = "Return";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369my $undescribed = "-- undescribed --";
370
371reset_state();
372
373while ($ARGV[0] =~ m/^-(.*)/) {
374 my $cmd = shift @ARGV;
375 if ($cmd eq "-html") {
376 $output_mode = "html";
377 %highlights = %highlights_html;
378 $blankline = $blankline_html;
Dan Luedtke1b40c192012-08-12 10:46:15 +0200379 } elsif ($cmd eq "-html5") {
380 $output_mode = "html5";
381 %highlights = %highlights_html5;
382 $blankline = $blankline_html5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 } elsif ($cmd eq "-man") {
384 $output_mode = "man";
385 %highlights = %highlights_man;
386 $blankline = $blankline_man;
387 } elsif ($cmd eq "-text") {
388 $output_mode = "text";
389 %highlights = %highlights_text;
390 $blankline = $blankline_text;
391 } elsif ($cmd eq "-docbook") {
392 $output_mode = "xml";
393 %highlights = %highlights_xml;
394 $blankline = $blankline_xml;
Johannes Bergeda603f2010-09-11 15:55:22 -0700395 } elsif ($cmd eq "-list") {
396 $output_mode = "list";
397 %highlights = %highlights_list;
398 $blankline = $blankline_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 } elsif ($cmd eq "-gnome") {
400 $output_mode = "gnome";
401 %highlights = %highlights_gnome;
402 $blankline = $blankline_gnome;
403 } elsif ($cmd eq "-module") { # not needed for XML, inherits from calling document
404 $modulename = shift @ARGV;
405 } elsif ($cmd eq "-function") { # to only output specific functions
406 $function_only = 1;
407 $function = shift @ARGV;
408 $function_table{$function} = 1;
409 } elsif ($cmd eq "-nofunction") { # to only output specific functions
410 $function_only = 2;
411 $function = shift @ARGV;
412 $function_table{$function} = 1;
413 } elsif ($cmd eq "-v") {
414 $verbose = 1;
415 } elsif (($cmd eq "-h") || ($cmd eq "--help")) {
416 usage();
Johannes Berg4b445952007-10-24 15:08:48 -0700417 } elsif ($cmd eq '-no-doc-sections') {
418 $no_doc_sections = 1;
Johannes Berge946c43a2013-11-12 15:11:12 -0800419 } elsif ($cmd eq '-show-not-found') {
420 $show_not_found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
422}
423
Randy Dunlap8484baa2011-01-05 16:28:43 -0800424# continue execution near EOF;
425
426sub usage {
Dan Luedtke1b40c192012-08-12 10:46:15 +0200427 print "Usage: $0 [ -docbook | -html | -html5 | -text | -man | -list ]\n";
Randy Dunlap8484baa2011-01-05 16:28:43 -0800428 print " [ -no-doc-sections ]\n";
429 print " [ -function funcname [ -function funcname ...] ]\n";
430 print " [ -nofunction funcname [ -nofunction funcname ...] ]\n";
Dan Luedtke1b40c192012-08-12 10:46:15 +0200431 print " [ -v ]\n";
Randy Dunlap8484baa2011-01-05 16:28:43 -0800432 print " c source file(s) > outputfile\n";
433 print " -v : verbose output, more warnings & other info listed\n";
434 exit 1;
435}
436
Borislav Petkov53f049f2007-05-08 00:30:54 -0700437# get kernel version from env
438sub get_kernel_version() {
Johannes Berg1b9bc222007-10-24 15:08:48 -0700439 my $version = 'unknown kernel version';
Borislav Petkov53f049f2007-05-08 00:30:54 -0700440
441 if (defined($ENV{'KERNELVERSION'})) {
442 $version = $ENV{'KERNELVERSION'};
443 }
444 return $version;
445}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447##
448# dumps section contents to arrays/hashes intended for that purpose.
449#
450sub dump_section {
Randy Dunlap94dc7ad2008-04-28 02:16:34 -0700451 my $file = shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 my $name = shift;
453 my $contents = join "\n", @_;
454
455 if ($name =~ m/$type_constant/) {
456 $name = $1;
457# print STDERR "constant section '$1' = '$contents'\n";
458 $constants{$name} = $contents;
459 } elsif ($name =~ m/$type_param/) {
460# print STDERR "parameter def '$1' = '$contents'\n";
461 $name = $1;
462 $parameterdescs{$name} = $contents;
Randy Dunlapa1d94aa2008-12-19 08:49:30 -0800463 $sectcheck = $sectcheck . $name . " ";
Randy Dunlapced69092008-12-01 13:14:03 -0800464 } elsif ($name eq "@\.\.\.") {
465# print STDERR "parameter def '...' = '$contents'\n";
466 $name = "...";
467 $parameterdescs{$name} = $contents;
Randy Dunlapa1d94aa2008-12-19 08:49:30 -0800468 $sectcheck = $sectcheck . $name . " ";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 } else {
470# print STDERR "other section '$name' = '$contents'\n";
Randy Dunlap94dc7ad2008-04-28 02:16:34 -0700471 if (defined($sections{$name}) && ($sections{$name} ne "")) {
Bart Van Assched40e1e62015-09-04 15:43:21 -0700472 print STDERR "${file}:$.: error: duplicate section name '$name'\n";
Randy Dunlap94dc7ad2008-04-28 02:16:34 -0700473 ++$errors;
474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 $sections{$name} = $contents;
476 push @sectionlist, $name;
477 }
478}
479
480##
Johannes Bergb112e0f2007-10-24 15:08:48 -0700481# dump DOC: section after checking that it should go out
482#
483sub dump_doc_section {
Randy Dunlap94dc7ad2008-04-28 02:16:34 -0700484 my $file = shift;
Johannes Bergb112e0f2007-10-24 15:08:48 -0700485 my $name = shift;
486 my $contents = join "\n", @_;
487
Johannes Berg4b445952007-10-24 15:08:48 -0700488 if ($no_doc_sections) {
489 return;
490 }
491
Johannes Bergb112e0f2007-10-24 15:08:48 -0700492 if (($function_only == 0) ||
493 ( $function_only == 1 && defined($function_table{$name})) ||
494 ( $function_only == 2 && !defined($function_table{$name})))
495 {
Randy Dunlap94dc7ad2008-04-28 02:16:34 -0700496 dump_section($file, $name, $contents);
Johannes Bergb112e0f2007-10-24 15:08:48 -0700497 output_blockhead({'sectionlist' => \@sectionlist,
498 'sections' => \%sections,
499 'module' => $modulename,
500 'content-only' => ($function_only != 0), });
501 }
502}
503
504##
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505# output function
506#
507# parameterdescs, a hash.
508# function => "function name"
509# parameterlist => @list of parameters
510# parameterdescs => %parameter descriptions
511# sectionlist => @list of sections
Randy Dunlapa21217d2007-02-10 01:46:04 -0800512# sections => %section descriptions
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800513#
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515sub output_highlight {
516 my $contents = join "\n",@_;
517 my $line;
518
519# DEBUG
520# if (!defined $contents) {
521# use Carp;
522# confess "output_highlight got called with no args?\n";
523# }
524
Dan Luedtke1b40c192012-08-12 10:46:15 +0200525 if ($output_mode eq "html" || $output_mode eq "html5" ||
526 $output_mode eq "xml") {
Randy Dunlap6b5b55f2007-10-16 23:31:20 -0700527 $contents = local_unescape($contents);
528 # convert data read & converted thru xml_escape() into &xyz; format:
Randy Dunlap2b35f4d2010-11-18 12:27:31 -0800529 $contents =~ s/\\\\\\/\&/g;
Randy Dunlap6b5b55f2007-10-16 23:31:20 -0700530 }
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700531# print STDERR "contents b4:$contents\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 eval $dohighlight;
533 die $@ if $@;
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700534# print STDERR "contents af:$contents\n";
535
Dan Luedtke1b40c192012-08-12 10:46:15 +0200536# strip whitespaces when generating html5
537 if ($output_mode eq "html5") {
538 $contents =~ s/^\s+//;
539 $contents =~ s/\s+$//;
540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 foreach $line (split "\n", $contents) {
Daniel Santos12ae6772012-10-04 17:15:10 -0700542 if (! $output_preformatted) {
543 $line =~ s/^\s*//;
544 }
Randy Dunlap3c308792007-05-08 00:24:39 -0700545 if ($line eq ""){
Daniel Santose314ba32012-10-04 17:15:08 -0700546 if (! $output_preformatted) {
547 print $lineprefix, local_unescape($blankline);
548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 } else {
Randy Dunlap3c308792007-05-08 00:24:39 -0700550 $line =~ s/\\\\\\/\&/g;
Randy Dunlapcdccb312007-07-19 01:48:25 -0700551 if ($output_mode eq "man" && substr($line, 0, 1) eq ".") {
552 print "\\&$line";
553 } else {
554 print $lineprefix, $line;
555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
557 print "\n";
558 }
559}
560
Dan Luedtke1b40c192012-08-12 10:46:15 +0200561# output sections in html
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562sub output_section_html(%) {
563 my %args = %{$_[0]};
564 my $section;
565
566 foreach $section (@{$args{'sectionlist'}}) {
567 print "<h3>$section</h3>\n";
568 print "<blockquote>\n";
569 output_highlight($args{'sections'}{$section});
570 print "</blockquote>\n";
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
574# output enum in html
575sub output_enum_html(%) {
576 my %args = %{$_[0]};
577 my ($parameter);
578 my $count;
Randy Dunlapb9d973282009-06-09 08:50:38 -0700579 print "<h2>enum " . $args{'enum'} . "</h2>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Randy Dunlapb9d973282009-06-09 08:50:38 -0700581 print "<b>enum " . $args{'enum'} . "</b> {<br>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 $count = 0;
583 foreach $parameter (@{$args{'parameterlist'}}) {
Randy Dunlapb9d973282009-06-09 08:50:38 -0700584 print " <b>" . $parameter . "</b>";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if ($count != $#{$args{'parameterlist'}}) {
586 $count++;
587 print ",\n";
588 }
589 print "<br>";
590 }
591 print "};<br>\n";
592
593 print "<h3>Constants</h3>\n";
594 print "<dl>\n";
595 foreach $parameter (@{$args{'parameterlist'}}) {
Randy Dunlapb9d973282009-06-09 08:50:38 -0700596 print "<dt><b>" . $parameter . "</b>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 print "<dd>";
598 output_highlight($args{'parameterdescs'}{$parameter});
599 }
600 print "</dl>\n";
601 output_section_html(@_);
602 print "<hr>\n";
603}
604
Randy Dunlapd28bee02006-02-01 03:06:57 -0800605# output typedef in html
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606sub output_typedef_html(%) {
607 my %args = %{$_[0]};
608 my ($parameter);
609 my $count;
Randy Dunlapb9d973282009-06-09 08:50:38 -0700610 print "<h2>typedef " . $args{'typedef'} . "</h2>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Randy Dunlapb9d973282009-06-09 08:50:38 -0700612 print "<b>typedef " . $args{'typedef'} . "</b>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 output_section_html(@_);
614 print "<hr>\n";
615}
616
617# output struct in html
618sub output_struct_html(%) {
619 my %args = %{$_[0]};
620 my ($parameter);
621
Randy Dunlapb9d973282009-06-09 08:50:38 -0700622 print "<h2>" . $args{'type'} . " " . $args{'struct'} . " - " . $args{'purpose'} . "</h2>\n";
623 print "<b>" . $args{'type'} . " " . $args{'struct'} . "</b> {<br>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 foreach $parameter (@{$args{'parameterlist'}}) {
625 if ($parameter =~ /^#/) {
626 print "$parameter<br>\n";
627 next;
628 }
629 my $parameter_name = $parameter;
630 $parameter_name =~ s/\[.*//;
631
Randy Dunlap3c308792007-05-08 00:24:39 -0700632 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 $type = $args{'parametertypes'}{$parameter};
634 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
635 # pointer-to-function
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700636 print "&nbsp; &nbsp; <i>$1</i><b>$parameter</b>) <i>($2)</i>;<br>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700638 # bitfield
639 print "&nbsp; &nbsp; <i>$1</i> <b>$parameter</b>$2;<br>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 } else {
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700641 print "&nbsp; &nbsp; <i>$type</i> <b>$parameter</b>;<br>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
643 }
644 print "};<br>\n";
645
646 print "<h3>Members</h3>\n";
647 print "<dl>\n";
648 foreach $parameter (@{$args{'parameterlist'}}) {
649 ($parameter =~ /^#/) && next;
650
651 my $parameter_name = $parameter;
652 $parameter_name =~ s/\[.*//;
653
Randy Dunlap3c308792007-05-08 00:24:39 -0700654 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Randy Dunlapb9d973282009-06-09 08:50:38 -0700655 print "<dt><b>" . $parameter . "</b>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 print "<dd>";
657 output_highlight($args{'parameterdescs'}{$parameter_name});
658 }
659 print "</dl>\n";
660 output_section_html(@_);
661 print "<hr>\n";
662}
663
664# output function in html
665sub output_function_html(%) {
666 my %args = %{$_[0]};
667 my ($parameter, $section);
668 my $count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Randy Dunlapb9d973282009-06-09 08:50:38 -0700670 print "<h2>" . $args{'function'} . " - " . $args{'purpose'} . "</h2>\n";
671 print "<i>" . $args{'functiontype'} . "</i>\n";
672 print "<b>" . $args{'function'} . "</b>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 print "(";
674 $count = 0;
675 foreach $parameter (@{$args{'parameterlist'}}) {
676 $type = $args{'parametertypes'}{$parameter};
677 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
678 # pointer-to-function
679 print "<i>$1</i><b>$parameter</b>) <i>($2)</i>";
680 } else {
Randy Dunlapb9d973282009-06-09 08:50:38 -0700681 print "<i>" . $type . "</i> <b>" . $parameter . "</b>";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 }
683 if ($count != $#{$args{'parameterlist'}}) {
684 $count++;
685 print ",\n";
686 }
687 }
688 print ")\n";
689
690 print "<h3>Arguments</h3>\n";
691 print "<dl>\n";
692 foreach $parameter (@{$args{'parameterlist'}}) {
693 my $parameter_name = $parameter;
694 $parameter_name =~ s/\[.*//;
695
Randy Dunlap3c308792007-05-08 00:24:39 -0700696 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Randy Dunlapb9d973282009-06-09 08:50:38 -0700697 print "<dt><b>" . $parameter . "</b>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 print "<dd>";
699 output_highlight($args{'parameterdescs'}{$parameter_name});
700 }
701 print "</dl>\n";
702 output_section_html(@_);
703 print "<hr>\n";
704}
705
Johannes Bergb112e0f2007-10-24 15:08:48 -0700706# output DOC: block header in html
707sub output_blockhead_html(%) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 my %args = %{$_[0]};
709 my ($parameter, $section);
710 my $count;
711
712 foreach $section (@{$args{'sectionlist'}}) {
713 print "<h3>$section</h3>\n";
714 print "<ul>\n";
715 output_highlight($args{'sections'}{$section});
716 print "</ul>\n";
717 }
718 print "<hr>\n";
719}
720
Dan Luedtke1b40c192012-08-12 10:46:15 +0200721# output sections in html5
722sub output_section_html5(%) {
723 my %args = %{$_[0]};
724 my $section;
725
726 foreach $section (@{$args{'sectionlist'}}) {
727 print "<section>\n";
728 print "<h1>$section</h1>\n";
729 print "<p>\n";
730 output_highlight($args{'sections'}{$section});
731 print "</p>\n";
732 print "</section>\n";
733 }
734}
735
736# output enum in html5
737sub output_enum_html5(%) {
738 my %args = %{$_[0]};
739 my ($parameter);
740 my $count;
741 my $html5id;
742
743 $html5id = $args{'enum'};
744 $html5id =~ s/[^a-zA-Z0-9\-]+/_/g;
745 print "<article class=\"enum\" id=\"enum:". $html5id . "\">";
746 print "<h1>enum " . $args{'enum'} . "</h1>\n";
747 print "<ol class=\"code\">\n";
748 print "<li>";
749 print "<span class=\"keyword\">enum</span> ";
750 print "<span class=\"identifier\">" . $args{'enum'} . "</span> {";
751 print "</li>\n";
752 $count = 0;
753 foreach $parameter (@{$args{'parameterlist'}}) {
754 print "<li class=\"indent\">";
755 print "<span class=\"param\">" . $parameter . "</span>";
756 if ($count != $#{$args{'parameterlist'}}) {
757 $count++;
758 print ",";
759 }
760 print "</li>\n";
761 }
762 print "<li>};</li>\n";
763 print "</ol>\n";
764
765 print "<section>\n";
766 print "<h1>Constants</h1>\n";
767 print "<dl>\n";
768 foreach $parameter (@{$args{'parameterlist'}}) {
769 print "<dt>" . $parameter . "</dt>\n";
770 print "<dd>";
771 output_highlight($args{'parameterdescs'}{$parameter});
772 print "</dd>\n";
773 }
774 print "</dl>\n";
775 print "</section>\n";
776 output_section_html5(@_);
777 print "</article>\n";
778}
779
780# output typedef in html5
781sub output_typedef_html5(%) {
782 my %args = %{$_[0]};
783 my ($parameter);
784 my $count;
785 my $html5id;
786
787 $html5id = $args{'typedef'};
788 $html5id =~ s/[^a-zA-Z0-9\-]+/_/g;
789 print "<article class=\"typedef\" id=\"typedef:" . $html5id . "\">\n";
790 print "<h1>typedef " . $args{'typedef'} . "</h1>\n";
791
792 print "<ol class=\"code\">\n";
793 print "<li>";
794 print "<span class=\"keyword\">typedef</span> ";
795 print "<span class=\"identifier\">" . $args{'typedef'} . "</span>";
796 print "</li>\n";
797 print "</ol>\n";
798 output_section_html5(@_);
799 print "</article>\n";
800}
801
802# output struct in html5
803sub output_struct_html5(%) {
804 my %args = %{$_[0]};
805 my ($parameter);
806 my $html5id;
807
808 $html5id = $args{'struct'};
809 $html5id =~ s/[^a-zA-Z0-9\-]+/_/g;
810 print "<article class=\"struct\" id=\"struct:" . $html5id . "\">\n";
811 print "<hgroup>\n";
812 print "<h1>" . $args{'type'} . " " . $args{'struct'} . "</h1>";
813 print "<h2>". $args{'purpose'} . "</h2>\n";
814 print "</hgroup>\n";
815 print "<ol class=\"code\">\n";
816 print "<li>";
817 print "<span class=\"type\">" . $args{'type'} . "</span> ";
818 print "<span class=\"identifier\">" . $args{'struct'} . "</span> {";
819 print "</li>\n";
820 foreach $parameter (@{$args{'parameterlist'}}) {
821 print "<li class=\"indent\">";
822 if ($parameter =~ /^#/) {
823 print "<span class=\"param\">" . $parameter ."</span>\n";
824 print "</li>\n";
825 next;
826 }
827 my $parameter_name = $parameter;
828 $parameter_name =~ s/\[.*//;
829
830 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
831 $type = $args{'parametertypes'}{$parameter};
832 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
833 # pointer-to-function
834 print "<span class=\"type\">$1</span> ";
835 print "<span class=\"param\">$parameter</span>";
836 print "<span class=\"type\">)</span> ";
837 print "(<span class=\"args\">$2</span>);";
838 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
839 # bitfield
840 print "<span class=\"type\">$1</span> ";
841 print "<span class=\"param\">$parameter</span>";
842 print "<span class=\"bits\">$2</span>;";
843 } else {
844 print "<span class=\"type\">$type</span> ";
845 print "<span class=\"param\">$parameter</span>;";
846 }
847 print "</li>\n";
848 }
849 print "<li>};</li>\n";
850 print "</ol>\n";
851
852 print "<section>\n";
853 print "<h1>Members</h1>\n";
854 print "<dl>\n";
855 foreach $parameter (@{$args{'parameterlist'}}) {
856 ($parameter =~ /^#/) && next;
857
858 my $parameter_name = $parameter;
859 $parameter_name =~ s/\[.*//;
860
861 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
862 print "<dt>" . $parameter . "</dt>\n";
863 print "<dd>";
864 output_highlight($args{'parameterdescs'}{$parameter_name});
865 print "</dd>\n";
866 }
867 print "</dl>\n";
868 print "</section>\n";
869 output_section_html5(@_);
870 print "</article>\n";
871}
872
873# output function in html5
874sub output_function_html5(%) {
875 my %args = %{$_[0]};
876 my ($parameter, $section);
877 my $count;
878 my $html5id;
879
880 $html5id = $args{'function'};
881 $html5id =~ s/[^a-zA-Z0-9\-]+/_/g;
882 print "<article class=\"function\" id=\"func:". $html5id . "\">\n";
883 print "<hgroup>\n";
884 print "<h1>" . $args{'function'} . "</h1>";
885 print "<h2>" . $args{'purpose'} . "</h2>\n";
886 print "</hgroup>\n";
887 print "<ol class=\"code\">\n";
888 print "<li>";
889 print "<span class=\"type\">" . $args{'functiontype'} . "</span> ";
890 print "<span class=\"identifier\">" . $args{'function'} . "</span> (";
891 print "</li>";
892 $count = 0;
893 foreach $parameter (@{$args{'parameterlist'}}) {
894 print "<li class=\"indent\">";
895 $type = $args{'parametertypes'}{$parameter};
896 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
897 # pointer-to-function
898 print "<span class=\"type\">$1</span> ";
899 print "<span class=\"param\">$parameter</span>";
900 print "<span class=\"type\">)</span> ";
901 print "(<span class=\"args\">$2</span>)";
902 } else {
903 print "<span class=\"type\">$type</span> ";
904 print "<span class=\"param\">$parameter</span>";
905 }
906 if ($count != $#{$args{'parameterlist'}}) {
907 $count++;
908 print ",";
909 }
910 print "</li>\n";
911 }
912 print "<li>)</li>\n";
913 print "</ol>\n";
914
915 print "<section>\n";
916 print "<h1>Arguments</h1>\n";
917 print "<p>\n";
918 print "<dl>\n";
919 foreach $parameter (@{$args{'parameterlist'}}) {
920 my $parameter_name = $parameter;
921 $parameter_name =~ s/\[.*//;
922
923 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
924 print "<dt>" . $parameter . "</dt>\n";
925 print "<dd>";
926 output_highlight($args{'parameterdescs'}{$parameter_name});
927 print "</dd>\n";
928 }
929 print "</dl>\n";
930 print "</section>\n";
931 output_section_html5(@_);
932 print "</article>\n";
933}
934
935# output DOC: block header in html5
936sub output_blockhead_html5(%) {
937 my %args = %{$_[0]};
938 my ($parameter, $section);
939 my $count;
940 my $html5id;
941
942 foreach $section (@{$args{'sectionlist'}}) {
943 $html5id = $section;
944 $html5id =~ s/[^a-zA-Z0-9\-]+/_/g;
945 print "<article class=\"doc\" id=\"doc:". $html5id . "\">\n";
946 print "<h1>$section</h1>\n";
947 print "<p>\n";
948 output_highlight($args{'sections'}{$section});
949 print "</p>\n";
950 }
951 print "</article>\n";
952}
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954sub output_section_xml(%) {
955 my %args = %{$_[0]};
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800956 my $section;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 # print out each section
958 $lineprefix=" ";
959 foreach $section (@{$args{'sectionlist'}}) {
Rich Walkerc73894c2005-05-01 08:59:26 -0700960 print "<refsect1>\n";
961 print "<title>$section</title>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 if ($section =~ m/EXAMPLE/i) {
Rich Walkerc73894c2005-05-01 08:59:26 -0700963 print "<informalexample><programlisting>\n";
Daniel Santose314ba32012-10-04 17:15:08 -0700964 $output_preformatted = 1;
Rich Walkerc73894c2005-05-01 08:59:26 -0700965 } else {
966 print "<para>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 }
968 output_highlight($args{'sections'}{$section});
Daniel Santose314ba32012-10-04 17:15:08 -0700969 $output_preformatted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 if ($section =~ m/EXAMPLE/i) {
Rich Walkerc73894c2005-05-01 08:59:26 -0700971 print "</programlisting></informalexample>\n";
972 } else {
973 print "</para>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 }
Rich Walkerc73894c2005-05-01 08:59:26 -0700975 print "</refsect1>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 }
977}
978
979# output function in XML DocBook
980sub output_function_xml(%) {
981 my %args = %{$_[0]};
982 my ($parameter, $section);
983 my $count;
984 my $id;
985
Randy Dunlapb9d973282009-06-09 08:50:38 -0700986 $id = "API-" . $args{'function'};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 $id =~ s/[^A-Za-z0-9]/-/g;
988
Pavel Pisa5449bc92007-02-10 01:45:37 -0800989 print "<refentry id=\"$id\">\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700990 print "<refentryinfo>\n";
991 print " <title>LINUX</title>\n";
992 print " <productname>Kernel Hackers Manual</productname>\n";
993 print " <date>$man_date</date>\n";
994 print "</refentryinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 print "<refmeta>\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -0700996 print " <refentrytitle><phrase>" . $args{'function'} . "</phrase></refentrytitle>\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700997 print " <manvolnum>9</manvolnum>\n";
Borislav Petkov03662992007-05-09 02:33:43 -0700998 print " <refmiscinfo class=\"version\">" . $kernelversion . "</refmiscinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 print "</refmeta>\n";
1000 print "<refnamediv>\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001001 print " <refname>" . $args{'function'} . "</refname>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 print " <refpurpose>\n";
1003 print " ";
1004 output_highlight ($args{'purpose'});
1005 print " </refpurpose>\n";
1006 print "</refnamediv>\n";
1007
1008 print "<refsynopsisdiv>\n";
1009 print " <title>Synopsis</title>\n";
1010 print " <funcsynopsis><funcprototype>\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001011 print " <funcdef>" . $args{'functiontype'} . " ";
1012 print "<function>" . $args{'function'} . " </function></funcdef>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014 $count = 0;
1015 if ($#{$args{'parameterlist'}} >= 0) {
1016 foreach $parameter (@{$args{'parameterlist'}}) {
1017 $type = $args{'parametertypes'}{$parameter};
1018 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1019 # pointer-to-function
1020 print " <paramdef>$1<parameter>$parameter</parameter>)\n";
1021 print " <funcparams>$2</funcparams></paramdef>\n";
1022 } else {
Randy Dunlapb9d973282009-06-09 08:50:38 -07001023 print " <paramdef>" . $type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 print " <parameter>$parameter</parameter></paramdef>\n";
1025 }
1026 }
1027 } else {
Martin Waitz6013d542005-05-01 08:59:25 -07001028 print " <void/>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 }
1030 print " </funcprototype></funcsynopsis>\n";
1031 print "</refsynopsisdiv>\n";
1032
1033 # print parameters
1034 print "<refsect1>\n <title>Arguments</title>\n";
1035 if ($#{$args{'parameterlist'}} >= 0) {
1036 print " <variablelist>\n";
1037 foreach $parameter (@{$args{'parameterlist'}}) {
1038 my $parameter_name = $parameter;
1039 $parameter_name =~ s/\[.*//;
1040
1041 print " <varlistentry>\n <term><parameter>$parameter</parameter></term>\n";
1042 print " <listitem>\n <para>\n";
1043 $lineprefix=" ";
1044 output_highlight($args{'parameterdescs'}{$parameter_name});
1045 print " </para>\n </listitem>\n </varlistentry>\n";
1046 }
1047 print " </variablelist>\n";
1048 } else {
1049 print " <para>\n None\n </para>\n";
1050 }
1051 print "</refsect1>\n";
1052
1053 output_section_xml(@_);
1054 print "</refentry>\n\n";
1055}
1056
1057# output struct in XML DocBook
1058sub output_struct_xml(%) {
1059 my %args = %{$_[0]};
1060 my ($parameter, $section);
1061 my $id;
1062
Randy Dunlapb9d973282009-06-09 08:50:38 -07001063 $id = "API-struct-" . $args{'struct'};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 $id =~ s/[^A-Za-z0-9]/-/g;
1065
Pavel Pisa5449bc92007-02-10 01:45:37 -08001066 print "<refentry id=\"$id\">\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -07001067 print "<refentryinfo>\n";
1068 print " <title>LINUX</title>\n";
1069 print " <productname>Kernel Hackers Manual</productname>\n";
1070 print " <date>$man_date</date>\n";
1071 print "</refentryinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 print "<refmeta>\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001073 print " <refentrytitle><phrase>" . $args{'type'} . " " . $args{'struct'} . "</phrase></refentrytitle>\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -07001074 print " <manvolnum>9</manvolnum>\n";
Borislav Petkov03662992007-05-09 02:33:43 -07001075 print " <refmiscinfo class=\"version\">" . $kernelversion . "</refmiscinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 print "</refmeta>\n";
1077 print "<refnamediv>\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001078 print " <refname>" . $args{'type'} . " " . $args{'struct'} . "</refname>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 print " <refpurpose>\n";
1080 print " ";
1081 output_highlight ($args{'purpose'});
1082 print " </refpurpose>\n";
1083 print "</refnamediv>\n";
1084
1085 print "<refsynopsisdiv>\n";
1086 print " <title>Synopsis</title>\n";
1087 print " <programlisting>\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001088 print $args{'type'} . " " . $args{'struct'} . " {\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 foreach $parameter (@{$args{'parameterlist'}}) {
1090 if ($parameter =~ /^#/) {
Randy Dunlap2b35f4d2010-11-18 12:27:31 -08001091 my $prm = $parameter;
1092 # convert data read & converted thru xml_escape() into &xyz; format:
1093 # This allows us to have #define macros interspersed in a struct.
1094 $prm =~ s/\\\\\\/\&/g;
1095 print "$prm\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 next;
1097 }
1098
1099 my $parameter_name = $parameter;
1100 $parameter_name =~ s/\[.*//;
1101
1102 defined($args{'parameterdescs'}{$parameter_name}) || next;
Randy Dunlap3c308792007-05-08 00:24:39 -07001103 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 $type = $args{'parametertypes'}{$parameter};
1105 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1106 # pointer-to-function
1107 print " $1 $parameter) ($2);\n";
1108 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
Randy Dunlap51f5a0c2007-07-19 01:48:24 -07001109 # bitfield
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 print " $1 $parameter$2;\n";
1111 } else {
Randy Dunlapb9d973282009-06-09 08:50:38 -07001112 print " " . $type . " " . $parameter . ";\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 }
1114 }
1115 print "};";
1116 print " </programlisting>\n";
1117 print "</refsynopsisdiv>\n";
1118
1119 print " <refsect1>\n";
1120 print " <title>Members</title>\n";
1121
Randy Dunlap39f00c02008-09-22 13:57:44 -07001122 if ($#{$args{'parameterlist'}} >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 print " <variablelist>\n";
1124 foreach $parameter (@{$args{'parameterlist'}}) {
1125 ($parameter =~ /^#/) && next;
1126
1127 my $parameter_name = $parameter;
1128 $parameter_name =~ s/\[.*//;
1129
1130 defined($args{'parameterdescs'}{$parameter_name}) || next;
1131 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
1132 print " <varlistentry>";
1133 print " <term>$parameter</term>\n";
1134 print " <listitem><para>\n";
1135 output_highlight($args{'parameterdescs'}{$parameter_name});
1136 print " </para></listitem>\n";
1137 print " </varlistentry>\n";
1138 }
1139 print " </variablelist>\n";
Randy Dunlap39f00c02008-09-22 13:57:44 -07001140 } else {
1141 print " <para>\n None\n </para>\n";
1142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 print " </refsect1>\n";
1144
1145 output_section_xml(@_);
1146
1147 print "</refentry>\n\n";
1148}
1149
1150# output enum in XML DocBook
1151sub output_enum_xml(%) {
1152 my %args = %{$_[0]};
1153 my ($parameter, $section);
1154 my $count;
1155 my $id;
1156
Randy Dunlapb9d973282009-06-09 08:50:38 -07001157 $id = "API-enum-" . $args{'enum'};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 $id =~ s/[^A-Za-z0-9]/-/g;
1159
Pavel Pisa5449bc92007-02-10 01:45:37 -08001160 print "<refentry id=\"$id\">\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -07001161 print "<refentryinfo>\n";
1162 print " <title>LINUX</title>\n";
1163 print " <productname>Kernel Hackers Manual</productname>\n";
1164 print " <date>$man_date</date>\n";
1165 print "</refentryinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 print "<refmeta>\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001167 print " <refentrytitle><phrase>enum " . $args{'enum'} . "</phrase></refentrytitle>\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -07001168 print " <manvolnum>9</manvolnum>\n";
Borislav Petkov03662992007-05-09 02:33:43 -07001169 print " <refmiscinfo class=\"version\">" . $kernelversion . "</refmiscinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 print "</refmeta>\n";
1171 print "<refnamediv>\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001172 print " <refname>enum " . $args{'enum'} . "</refname>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 print " <refpurpose>\n";
1174 print " ";
1175 output_highlight ($args{'purpose'});
1176 print " </refpurpose>\n";
1177 print "</refnamediv>\n";
1178
1179 print "<refsynopsisdiv>\n";
1180 print " <title>Synopsis</title>\n";
1181 print " <programlisting>\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001182 print "enum " . $args{'enum'} . " {\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 $count = 0;
1184 foreach $parameter (@{$args{'parameterlist'}}) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001185 print " $parameter";
1186 if ($count != $#{$args{'parameterlist'}}) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 $count++;
1188 print ",";
Randy Dunlap3c308792007-05-08 00:24:39 -07001189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 print "\n";
1191 }
1192 print "};";
1193 print " </programlisting>\n";
1194 print "</refsynopsisdiv>\n";
1195
1196 print "<refsect1>\n";
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001197 print " <title>Constants</title>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 print " <variablelist>\n";
1199 foreach $parameter (@{$args{'parameterlist'}}) {
1200 my $parameter_name = $parameter;
1201 $parameter_name =~ s/\[.*//;
1202
1203 print " <varlistentry>";
1204 print " <term>$parameter</term>\n";
1205 print " <listitem><para>\n";
1206 output_highlight($args{'parameterdescs'}{$parameter_name});
1207 print " </para></listitem>\n";
1208 print " </varlistentry>\n";
1209 }
1210 print " </variablelist>\n";
1211 print "</refsect1>\n";
1212
1213 output_section_xml(@_);
1214
1215 print "</refentry>\n\n";
1216}
1217
1218# output typedef in XML DocBook
1219sub output_typedef_xml(%) {
1220 my %args = %{$_[0]};
1221 my ($parameter, $section);
1222 my $id;
1223
Randy Dunlapb9d973282009-06-09 08:50:38 -07001224 $id = "API-typedef-" . $args{'typedef'};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 $id =~ s/[^A-Za-z0-9]/-/g;
1226
Pavel Pisa5449bc92007-02-10 01:45:37 -08001227 print "<refentry id=\"$id\">\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -07001228 print "<refentryinfo>\n";
1229 print " <title>LINUX</title>\n";
1230 print " <productname>Kernel Hackers Manual</productname>\n";
1231 print " <date>$man_date</date>\n";
1232 print "</refentryinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 print "<refmeta>\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001234 print " <refentrytitle><phrase>typedef " . $args{'typedef'} . "</phrase></refentrytitle>\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -07001235 print " <manvolnum>9</manvolnum>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 print "</refmeta>\n";
1237 print "<refnamediv>\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001238 print " <refname>typedef " . $args{'typedef'} . "</refname>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 print " <refpurpose>\n";
1240 print " ";
1241 output_highlight ($args{'purpose'});
1242 print " </refpurpose>\n";
1243 print "</refnamediv>\n";
1244
1245 print "<refsynopsisdiv>\n";
1246 print " <title>Synopsis</title>\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001247 print " <synopsis>typedef " . $args{'typedef'} . ";</synopsis>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 print "</refsynopsisdiv>\n";
1249
1250 output_section_xml(@_);
1251
1252 print "</refentry>\n\n";
1253}
1254
1255# output in XML DocBook
Johannes Bergb112e0f2007-10-24 15:08:48 -07001256sub output_blockhead_xml(%) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 my %args = %{$_[0]};
1258 my ($parameter, $section);
1259 my $count;
1260
1261 my $id = $args{'module'};
1262 $id =~ s/[^A-Za-z0-9]/-/g;
1263
1264 # print out each section
1265 $lineprefix=" ";
1266 foreach $section (@{$args{'sectionlist'}}) {
Johannes Bergb112e0f2007-10-24 15:08:48 -07001267 if (!$args{'content-only'}) {
1268 print "<refsect1>\n <title>$section</title>\n";
1269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 if ($section =~ m/EXAMPLE/i) {
1271 print "<example><para>\n";
Daniel Santose314ba32012-10-04 17:15:08 -07001272 $output_preformatted = 1;
Johannes Bergb112e0f2007-10-24 15:08:48 -07001273 } else {
1274 print "<para>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 }
1276 output_highlight($args{'sections'}{$section});
Daniel Santose314ba32012-10-04 17:15:08 -07001277 $output_preformatted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 if ($section =~ m/EXAMPLE/i) {
1279 print "</para></example>\n";
Johannes Bergb112e0f2007-10-24 15:08:48 -07001280 } else {
1281 print "</para>";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 }
Johannes Bergb112e0f2007-10-24 15:08:48 -07001283 if (!$args{'content-only'}) {
1284 print "\n</refsect1>\n";
1285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 }
1287
1288 print "\n\n";
1289}
1290
1291# output in XML DocBook
1292sub output_function_gnome {
1293 my %args = %{$_[0]};
1294 my ($parameter, $section);
1295 my $count;
1296 my $id;
1297
Randy Dunlapb9d973282009-06-09 08:50:38 -07001298 $id = $args{'module'} . "-" . $args{'function'};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 $id =~ s/[^A-Za-z0-9]/-/g;
1300
1301 print "<sect2>\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001302 print " <title id=\"$id\">" . $args{'function'} . "</title>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 print " <funcsynopsis>\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001305 print " <funcdef>" . $args{'functiontype'} . " ";
1306 print "<function>" . $args{'function'} . " ";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 print "</function></funcdef>\n";
1308
1309 $count = 0;
1310 if ($#{$args{'parameterlist'}} >= 0) {
1311 foreach $parameter (@{$args{'parameterlist'}}) {
1312 $type = $args{'parametertypes'}{$parameter};
1313 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1314 # pointer-to-function
1315 print " <paramdef>$1 <parameter>$parameter</parameter>)\n";
1316 print " <funcparams>$2</funcparams></paramdef>\n";
1317 } else {
Randy Dunlapb9d973282009-06-09 08:50:38 -07001318 print " <paramdef>" . $type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 print " <parameter>$parameter</parameter></paramdef>\n";
1320 }
1321 }
1322 } else {
1323 print " <void>\n";
1324 }
1325 print " </funcsynopsis>\n";
1326 if ($#{$args{'parameterlist'}} >= 0) {
1327 print " <informaltable pgwide=\"1\" frame=\"none\" role=\"params\">\n";
1328 print "<tgroup cols=\"2\">\n";
1329 print "<colspec colwidth=\"2*\">\n";
1330 print "<colspec colwidth=\"8*\">\n";
1331 print "<tbody>\n";
1332 foreach $parameter (@{$args{'parameterlist'}}) {
1333 my $parameter_name = $parameter;
1334 $parameter_name =~ s/\[.*//;
1335
1336 print " <row><entry align=\"right\"><parameter>$parameter</parameter></entry>\n";
1337 print " <entry>\n";
1338 $lineprefix=" ";
1339 output_highlight($args{'parameterdescs'}{$parameter_name});
1340 print " </entry></row>\n";
1341 }
1342 print " </tbody></tgroup></informaltable>\n";
1343 } else {
1344 print " <para>\n None\n </para>\n";
1345 }
1346
1347 # print out each section
1348 $lineprefix=" ";
1349 foreach $section (@{$args{'sectionlist'}}) {
1350 print "<simplesect>\n <title>$section</title>\n";
1351 if ($section =~ m/EXAMPLE/i) {
1352 print "<example><programlisting>\n";
Daniel Santose314ba32012-10-04 17:15:08 -07001353 $output_preformatted = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 } else {
1355 }
1356 print "<para>\n";
1357 output_highlight($args{'sections'}{$section});
Daniel Santose314ba32012-10-04 17:15:08 -07001358 $output_preformatted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 print "</para>\n";
1360 if ($section =~ m/EXAMPLE/i) {
1361 print "</programlisting></example>\n";
1362 } else {
1363 }
1364 print " </simplesect>\n";
1365 }
1366
1367 print "</sect2>\n\n";
1368}
1369
1370##
1371# output function in man
1372sub output_function_man(%) {
1373 my %args = %{$_[0]};
1374 my ($parameter, $section);
1375 my $count;
1376
1377 print ".TH \"$args{'function'}\" 9 \"$args{'function'}\" \"$man_date\" \"Kernel Hacker's Manual\" LINUX\n";
1378
1379 print ".SH NAME\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001380 print $args{'function'} . " \\- " . $args{'purpose'} . "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
1382 print ".SH SYNOPSIS\n";
Randy Dunlapa21217d2007-02-10 01:46:04 -08001383 if ($args{'functiontype'} ne "") {
Randy Dunlapb9d973282009-06-09 08:50:38 -07001384 print ".B \"" . $args{'functiontype'} . "\" " . $args{'function'} . "\n";
Randy Dunlapa21217d2007-02-10 01:46:04 -08001385 } else {
Randy Dunlapb9d973282009-06-09 08:50:38 -07001386 print ".B \"" . $args{'function'} . "\n";
Randy Dunlapa21217d2007-02-10 01:46:04 -08001387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 $count = 0;
1389 my $parenth = "(";
1390 my $post = ",";
1391 foreach my $parameter (@{$args{'parameterlist'}}) {
1392 if ($count == $#{$args{'parameterlist'}}) {
1393 $post = ");";
1394 }
1395 $type = $args{'parametertypes'}{$parameter};
1396 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1397 # pointer-to-function
Randy Dunlapb9d973282009-06-09 08:50:38 -07001398 print ".BI \"" . $parenth . $1 . "\" " . $parameter . " \") (" . $2 . ")" . $post . "\"\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 } else {
1400 $type =~ s/([^\*])$/$1 /;
Randy Dunlapb9d973282009-06-09 08:50:38 -07001401 print ".BI \"" . $parenth . $type . "\" " . $parameter . " \"" . $post . "\"\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 }
1403 $count++;
1404 $parenth = "";
1405 }
1406
1407 print ".SH ARGUMENTS\n";
1408 foreach $parameter (@{$args{'parameterlist'}}) {
1409 my $parameter_name = $parameter;
1410 $parameter_name =~ s/\[.*//;
1411
Randy Dunlapb9d973282009-06-09 08:50:38 -07001412 print ".IP \"" . $parameter . "\" 12\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 output_highlight($args{'parameterdescs'}{$parameter_name});
1414 }
1415 foreach $section (@{$args{'sectionlist'}}) {
1416 print ".SH \"", uc $section, "\"\n";
1417 output_highlight($args{'sections'}{$section});
1418 }
1419}
1420
1421##
1422# output enum in man
1423sub output_enum_man(%) {
1424 my %args = %{$_[0]};
1425 my ($parameter, $section);
1426 my $count;
1427
1428 print ".TH \"$args{'module'}\" 9 \"enum $args{'enum'}\" \"$man_date\" \"API Manual\" LINUX\n";
1429
1430 print ".SH NAME\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001431 print "enum " . $args{'enum'} . " \\- " . $args{'purpose'} . "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 print ".SH SYNOPSIS\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001434 print "enum " . $args{'enum'} . " {\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 $count = 0;
1436 foreach my $parameter (@{$args{'parameterlist'}}) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001437 print ".br\n.BI \" $parameter\"\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 if ($count == $#{$args{'parameterlist'}}) {
1439 print "\n};\n";
1440 last;
1441 }
1442 else {
1443 print ", \n.br\n";
1444 }
1445 $count++;
1446 }
1447
1448 print ".SH Constants\n";
1449 foreach $parameter (@{$args{'parameterlist'}}) {
1450 my $parameter_name = $parameter;
1451 $parameter_name =~ s/\[.*//;
1452
Randy Dunlapb9d973282009-06-09 08:50:38 -07001453 print ".IP \"" . $parameter . "\" 12\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 output_highlight($args{'parameterdescs'}{$parameter_name});
1455 }
1456 foreach $section (@{$args{'sectionlist'}}) {
1457 print ".SH \"$section\"\n";
1458 output_highlight($args{'sections'}{$section});
1459 }
1460}
1461
1462##
1463# output struct in man
1464sub output_struct_man(%) {
1465 my %args = %{$_[0]};
1466 my ($parameter, $section);
1467
Randy Dunlapb9d973282009-06-09 08:50:38 -07001468 print ".TH \"$args{'module'}\" 9 \"" . $args{'type'} . " " . $args{'struct'} . "\" \"$man_date\" \"API Manual\" LINUX\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
1470 print ".SH NAME\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001471 print $args{'type'} . " " . $args{'struct'} . " \\- " . $args{'purpose'} . "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 print ".SH SYNOPSIS\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001474 print $args{'type'} . " " . $args{'struct'} . " {\n.br\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
1476 foreach my $parameter (@{$args{'parameterlist'}}) {
1477 if ($parameter =~ /^#/) {
1478 print ".BI \"$parameter\"\n.br\n";
1479 next;
1480 }
1481 my $parameter_name = $parameter;
1482 $parameter_name =~ s/\[.*//;
1483
Randy Dunlap3c308792007-05-08 00:24:39 -07001484 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 $type = $args{'parametertypes'}{$parameter};
1486 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1487 # pointer-to-function
Randy Dunlapb9d973282009-06-09 08:50:38 -07001488 print ".BI \" " . $1 . "\" " . $parameter . " \") (" . $2 . ")" . "\"\n;\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
Randy.Dunlap1d7e1d42006-07-01 04:36:34 -07001490 # bitfield
Randy Dunlapb9d973282009-06-09 08:50:38 -07001491 print ".BI \" " . $1 . "\ \" " . $parameter . $2 . " \"" . "\"\n;\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 } else {
1493 $type =~ s/([^\*])$/$1 /;
Randy Dunlapb9d973282009-06-09 08:50:38 -07001494 print ".BI \" " . $type . "\" " . $parameter . " \"" . "\"\n;\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 }
1496 print "\n.br\n";
1497 }
1498 print "};\n.br\n";
1499
Randy Dunlapc51d3da2006-06-25 05:49:14 -07001500 print ".SH Members\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 foreach $parameter (@{$args{'parameterlist'}}) {
1502 ($parameter =~ /^#/) && next;
1503
1504 my $parameter_name = $parameter;
1505 $parameter_name =~ s/\[.*//;
1506
Randy Dunlap3c308792007-05-08 00:24:39 -07001507 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Randy Dunlapb9d973282009-06-09 08:50:38 -07001508 print ".IP \"" . $parameter . "\" 12\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 output_highlight($args{'parameterdescs'}{$parameter_name});
1510 }
1511 foreach $section (@{$args{'sectionlist'}}) {
1512 print ".SH \"$section\"\n";
1513 output_highlight($args{'sections'}{$section});
1514 }
1515}
1516
1517##
1518# output typedef in man
1519sub output_typedef_man(%) {
1520 my %args = %{$_[0]};
1521 my ($parameter, $section);
1522
1523 print ".TH \"$args{'module'}\" 9 \"$args{'typedef'}\" \"$man_date\" \"API Manual\" LINUX\n";
1524
1525 print ".SH NAME\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001526 print "typedef " . $args{'typedef'} . " \\- " . $args{'purpose'} . "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
1528 foreach $section (@{$args{'sectionlist'}}) {
1529 print ".SH \"$section\"\n";
1530 output_highlight($args{'sections'}{$section});
1531 }
1532}
1533
Johannes Bergb112e0f2007-10-24 15:08:48 -07001534sub output_blockhead_man(%) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 my %args = %{$_[0]};
1536 my ($parameter, $section);
1537 my $count;
1538
1539 print ".TH \"$args{'module'}\" 9 \"$args{'module'}\" \"$man_date\" \"API Manual\" LINUX\n";
1540
1541 foreach $section (@{$args{'sectionlist'}}) {
1542 print ".SH \"$section\"\n";
1543 output_highlight($args{'sections'}{$section});
1544 }
1545}
1546
1547##
1548# output in text
1549sub output_function_text(%) {
1550 my %args = %{$_[0]};
1551 my ($parameter, $section);
Randy Dunlapa21217d2007-02-10 01:46:04 -08001552 my $start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Randy Dunlapf47634b2006-07-01 04:36:36 -07001554 print "Name:\n\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001555 print $args{'function'} . " - " . $args{'purpose'} . "\n";
Randy Dunlapf47634b2006-07-01 04:36:36 -07001556
1557 print "\nSynopsis:\n\n";
Randy Dunlapa21217d2007-02-10 01:46:04 -08001558 if ($args{'functiontype'} ne "") {
Randy Dunlapb9d973282009-06-09 08:50:38 -07001559 $start = $args{'functiontype'} . " " . $args{'function'} . " (";
Randy Dunlapa21217d2007-02-10 01:46:04 -08001560 } else {
Randy Dunlapb9d973282009-06-09 08:50:38 -07001561 $start = $args{'function'} . " (";
Randy Dunlapa21217d2007-02-10 01:46:04 -08001562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 print $start;
Randy Dunlapa21217d2007-02-10 01:46:04 -08001564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 my $count = 0;
1566 foreach my $parameter (@{$args{'parameterlist'}}) {
1567 $type = $args{'parametertypes'}{$parameter};
1568 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1569 # pointer-to-function
Randy Dunlapb9d973282009-06-09 08:50:38 -07001570 print $1 . $parameter . ") (" . $2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 } else {
Randy Dunlapb9d973282009-06-09 08:50:38 -07001572 print $type . " " . $parameter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 }
1574 if ($count != $#{$args{'parameterlist'}}) {
1575 $count++;
1576 print ",\n";
1577 print " " x length($start);
1578 } else {
1579 print ");\n\n";
1580 }
1581 }
1582
1583 print "Arguments:\n\n";
1584 foreach $parameter (@{$args{'parameterlist'}}) {
1585 my $parameter_name = $parameter;
1586 $parameter_name =~ s/\[.*//;
1587
Randy Dunlapb9d973282009-06-09 08:50:38 -07001588 print $parameter . "\n\t" . $args{'parameterdescs'}{$parameter_name} . "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 }
1590 output_section_text(@_);
1591}
1592
1593#output sections in text
1594sub output_section_text(%) {
1595 my %args = %{$_[0]};
1596 my $section;
1597
1598 print "\n";
1599 foreach $section (@{$args{'sectionlist'}}) {
1600 print "$section:\n\n";
1601 output_highlight($args{'sections'}{$section});
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 print "\n\n";
1604}
1605
1606# output enum in text
1607sub output_enum_text(%) {
1608 my %args = %{$_[0]};
1609 my ($parameter);
1610 my $count;
1611 print "Enum:\n\n";
1612
Randy Dunlapb9d973282009-06-09 08:50:38 -07001613 print "enum " . $args{'enum'} . " - " . $args{'purpose'} . "\n\n";
1614 print "enum " . $args{'enum'} . " {\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 $count = 0;
1616 foreach $parameter (@{$args{'parameterlist'}}) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001617 print "\t$parameter";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 if ($count != $#{$args{'parameterlist'}}) {
1619 $count++;
1620 print ",";
1621 }
1622 print "\n";
1623 }
1624 print "};\n\n";
1625
1626 print "Constants:\n\n";
1627 foreach $parameter (@{$args{'parameterlist'}}) {
1628 print "$parameter\n\t";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001629 print $args{'parameterdescs'}{$parameter} . "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 }
1631
1632 output_section_text(@_);
1633}
1634
1635# output typedef in text
1636sub output_typedef_text(%) {
1637 my %args = %{$_[0]};
1638 my ($parameter);
1639 my $count;
1640 print "Typedef:\n\n";
1641
Randy Dunlapb9d973282009-06-09 08:50:38 -07001642 print "typedef " . $args{'typedef'} . " - " . $args{'purpose'} . "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 output_section_text(@_);
1644}
1645
1646# output struct as text
1647sub output_struct_text(%) {
1648 my %args = %{$_[0]};
1649 my ($parameter);
1650
Randy Dunlapb9d973282009-06-09 08:50:38 -07001651 print $args{'type'} . " " . $args{'struct'} . " - " . $args{'purpose'} . "\n\n";
1652 print $args{'type'} . " " . $args{'struct'} . " {\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 foreach $parameter (@{$args{'parameterlist'}}) {
1654 if ($parameter =~ /^#/) {
1655 print "$parameter\n";
1656 next;
1657 }
1658
1659 my $parameter_name = $parameter;
1660 $parameter_name =~ s/\[.*//;
1661
Randy Dunlap3c308792007-05-08 00:24:39 -07001662 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 $type = $args{'parametertypes'}{$parameter};
1664 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1665 # pointer-to-function
1666 print "\t$1 $parameter) ($2);\n";
1667 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
Randy Dunlap51f5a0c2007-07-19 01:48:24 -07001668 # bitfield
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 print "\t$1 $parameter$2;\n";
1670 } else {
Randy Dunlapb9d973282009-06-09 08:50:38 -07001671 print "\t" . $type . " " . $parameter . ";\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 }
1673 }
1674 print "};\n\n";
1675
1676 print "Members:\n\n";
1677 foreach $parameter (@{$args{'parameterlist'}}) {
1678 ($parameter =~ /^#/) && next;
1679
1680 my $parameter_name = $parameter;
1681 $parameter_name =~ s/\[.*//;
1682
Randy Dunlap3c308792007-05-08 00:24:39 -07001683 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 print "$parameter\n\t";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001685 print $args{'parameterdescs'}{$parameter_name} . "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 }
1687 print "\n";
1688 output_section_text(@_);
1689}
1690
Johannes Bergb112e0f2007-10-24 15:08:48 -07001691sub output_blockhead_text(%) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 my %args = %{$_[0]};
1693 my ($parameter, $section);
1694
1695 foreach $section (@{$args{'sectionlist'}}) {
1696 print " $section:\n";
1697 print " -> ";
1698 output_highlight($args{'sections'}{$section});
1699 }
1700}
1701
Johannes Bergeda603f2010-09-11 15:55:22 -07001702## list mode output functions
1703
1704sub output_function_list(%) {
1705 my %args = %{$_[0]};
1706
1707 print $args{'function'} . "\n";
1708}
1709
1710# output enum in list
1711sub output_enum_list(%) {
1712 my %args = %{$_[0]};
1713 print $args{'enum'} . "\n";
1714}
1715
1716# output typedef in list
1717sub output_typedef_list(%) {
1718 my %args = %{$_[0]};
1719 print $args{'typedef'} . "\n";
1720}
1721
1722# output struct as list
1723sub output_struct_list(%) {
1724 my %args = %{$_[0]};
1725
1726 print $args{'struct'} . "\n";
1727}
1728
1729sub output_blockhead_list(%) {
1730 my %args = %{$_[0]};
1731 my ($parameter, $section);
1732
1733 foreach $section (@{$args{'sectionlist'}}) {
1734 print "DOC: $section\n";
1735 }
1736}
1737
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738##
Randy Dunlap27205742006-10-11 01:22:12 -07001739# generic output function for all types (function, struct/union, typedef, enum);
1740# calls the generated, variable output_ function name based on
1741# functype and output_mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742sub output_declaration {
1743 no strict 'refs';
1744 my $name = shift;
1745 my $functype = shift;
1746 my $func = "output_${functype}_$output_mode";
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001747 if (($function_only==0) ||
1748 ( $function_only == 1 && defined($function_table{$name})) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 ( $function_only == 2 && !defined($function_table{$name})))
1750 {
Randy Dunlap3c308792007-05-08 00:24:39 -07001751 &$func(@_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 $section_counter++;
1753 }
1754}
1755
1756##
Randy Dunlap27205742006-10-11 01:22:12 -07001757# generic output function - calls the right one based on current output mode.
Johannes Bergb112e0f2007-10-24 15:08:48 -07001758sub output_blockhead {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 no strict 'refs';
Randy Dunlapb9d973282009-06-09 08:50:38 -07001760 my $func = "output_blockhead_" . $output_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 &$func(@_);
1762 $section_counter++;
1763}
1764
1765##
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001766# takes a declaration (struct, union, enum, typedef) and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767# invokes the right handler. NOT called for functions.
1768sub dump_declaration($$) {
1769 no strict 'refs';
1770 my ($prototype, $file) = @_;
Randy Dunlapb9d973282009-06-09 08:50:38 -07001771 my $func = "dump_" . $decl_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 &$func(@_);
1773}
1774
1775sub dump_union($$) {
1776 dump_struct(@_);
1777}
1778
1779sub dump_struct($$) {
1780 my $x = shift;
1781 my $file = shift;
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001782 my $nested;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
Randy Dunlap52dc5ae2009-04-30 15:08:53 -07001784 if ($x =~ /(struct|union)\s+(\w+)\s*{(.*)}/) {
1785 #my $decl_type = $1;
Randy Dunlap3c308792007-05-08 00:24:39 -07001786 $declaration_name = $2;
1787 my $members = $3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
1789 # ignore embedded structs or unions
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001790 $members =~ s/({.*})//g;
1791 $nested = $1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Martin Waitzaeec46b2005-11-13 16:08:13 -08001793 # ignore members marked private:
Randy Dunlap52dc5ae2009-04-30 15:08:53 -07001794 $members =~ s/\/\*\s*private:.*?\/\*\s*public:.*?\*\///gos;
1795 $members =~ s/\/\*\s*private:.*//gos;
Martin Waitzaeec46b2005-11-13 16:08:13 -08001796 # strip comments:
1797 $members =~ s/\/\*.*?\*\///gos;
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001798 $nested =~ s/\/\*.*?\*\///gos;
Randy Dunlapd960eea2009-06-29 14:54:11 -07001799 # strip kmemcheck_bitfield_{begin,end}.*;
1800 $members =~ s/kmemcheck_bitfield_.*?;//gos;
Randy Dunlapef5da592010-03-23 13:35:14 -07001801 # strip attributes
Jonathan Corbetf0074922015-08-23 13:35:23 -06001802 $members =~ s/__attribute__\s*\(\([a-z,_\*\s\(\)]*\)\)//i;
Johannes Berg7b990782014-12-10 15:41:28 -08001803 $members =~ s/__aligned\s*\([^;]*\)//gos;
Jonathan Corbetf0074922015-08-23 13:35:23 -06001804 $members =~ s/\s*CRYPTO_MINALIGN_ATTR//gos;
Martin Waitzaeec46b2005-11-13 16:08:13 -08001805
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 create_parameterlist($members, ';', $file);
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001807 check_sections($file, $declaration_name, "struct", $sectcheck, $struct_actual, $nested);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
1809 output_declaration($declaration_name,
1810 'struct',
1811 {'struct' => $declaration_name,
1812 'module' => $modulename,
1813 'parameterlist' => \@parameterlist,
1814 'parameterdescs' => \%parameterdescs,
1815 'parametertypes' => \%parametertypes,
1816 'sectionlist' => \@sectionlist,
1817 'sections' => \%sections,
1818 'purpose' => $declaration_purpose,
1819 'type' => $decl_type
1820 });
1821 }
1822 else {
Bart Van Assched40e1e62015-09-04 15:43:21 -07001823 print STDERR "${file}:$.: error: Cannot parse struct or union!\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 ++$errors;
1825 }
1826}
1827
1828sub dump_enum($$) {
1829 my $x = shift;
1830 my $file = shift;
1831
Martin Waitzaeec46b2005-11-13 16:08:13 -08001832 $x =~ s@/\*.*?\*/@@gos; # strip comments.
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001833 $x =~ s/^#\s*define\s+.*$//; # strip #define macros inside enums
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 if ($x =~ /enum\s+(\w+)\s*{(.*)}/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001836 $declaration_name = $1;
1837 my $members = $2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
1839 foreach my $arg (split ',', $members) {
1840 $arg =~ s/^\s*(\w+).*/$1/;
1841 push @parameterlist, $arg;
1842 if (!$parameterdescs{$arg}) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001843 $parameterdescs{$arg} = $undescribed;
Bart Van Assched40e1e62015-09-04 15:43:21 -07001844 print STDERR "${file}:$.: warning: Enum value '$arg' ".
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 "not described in enum '$declaration_name'\n";
1846 }
1847
1848 }
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001849
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 output_declaration($declaration_name,
1851 'enum',
1852 {'enum' => $declaration_name,
1853 'module' => $modulename,
1854 'parameterlist' => \@parameterlist,
1855 'parameterdescs' => \%parameterdescs,
1856 'sectionlist' => \@sectionlist,
1857 'sections' => \%sections,
1858 'purpose' => $declaration_purpose
1859 });
1860 }
1861 else {
Bart Van Assched40e1e62015-09-04 15:43:21 -07001862 print STDERR "${file}:$.: error: Cannot parse enum!\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 ++$errors;
1864 }
1865}
1866
1867sub dump_typedef($$) {
1868 my $x = shift;
1869 my $file = shift;
1870
Martin Waitzaeec46b2005-11-13 16:08:13 -08001871 $x =~ s@/\*.*?\*/@@gos; # strip comments.
Mauro Carvalho Chehab83766452015-10-08 16:14:45 -03001872
1873 # Parse function prototypes
1874 if ($x =~ /typedef\s+(\w+)\s*\(\*\s*(\w\S+)\s*\)\s*\((.*)\);/) {
1875 # Function typedefs
1876 $return_type = $1;
1877 $declaration_name = $2;
1878 my $args = $3;
1879
1880 create_parameterlist($args, ',', $file);
1881
1882 output_declaration($declaration_name,
1883 'function',
1884 {'function' => $declaration_name,
1885 'module' => $modulename,
1886 'functiontype' => $return_type,
1887 'parameterlist' => \@parameterlist,
1888 'parameterdescs' => \%parameterdescs,
1889 'parametertypes' => \%parametertypes,
1890 'sectionlist' => \@sectionlist,
1891 'sections' => \%sections,
1892 'purpose' => $declaration_purpose
1893 });
1894 return;
1895 }
1896
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 while (($x =~ /\(*.\)\s*;$/) || ($x =~ /\[*.\]\s*;$/)) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001898 $x =~ s/\(*.\)\s*;$/;/;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 $x =~ s/\[*.\]\s*;$/;/;
1900 }
1901
1902 if ($x =~ /typedef.*\s+(\w+)\s*;/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001903 $declaration_name = $1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
1905 output_declaration($declaration_name,
1906 'typedef',
1907 {'typedef' => $declaration_name,
1908 'module' => $modulename,
1909 'sectionlist' => \@sectionlist,
1910 'sections' => \%sections,
1911 'purpose' => $declaration_purpose
1912 });
1913 }
1914 else {
Bart Van Assched40e1e62015-09-04 15:43:21 -07001915 print STDERR "${file}:$.: error: Cannot parse typedef!\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 ++$errors;
1917 }
1918}
1919
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001920sub save_struct_actual($) {
1921 my $actual = shift;
1922
1923 # strip all spaces from the actual param so that it looks like one string item
1924 $actual =~ s/\s*//g;
1925 $struct_actual = $struct_actual . $actual . " ";
1926}
1927
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928sub create_parameterlist($$$) {
1929 my $args = shift;
1930 my $splitter = shift;
1931 my $file = shift;
1932 my $type;
1933 my $param;
1934
Martin Waitza6d3fe72006-01-09 20:53:55 -08001935 # temporarily replace commas inside function pointer definition
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 while ($args =~ /(\([^\),]+),/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001937 $args =~ s/(\([^\),]+),/$1#/g;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 }
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 foreach my $arg (split($splitter, $args)) {
1941 # strip comments
1942 $arg =~ s/\/\*.*\*\///;
Randy Dunlap3c308792007-05-08 00:24:39 -07001943 # strip leading/trailing spaces
1944 $arg =~ s/^\s*//;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 $arg =~ s/\s*$//;
1946 $arg =~ s/\s+/ /;
1947
1948 if ($arg =~ /^#/) {
1949 # Treat preprocessor directive as a typeless variable just to fill
1950 # corresponding data structures "correctly". Catch it later in
1951 # output_* subs.
1952 push_parameter($arg, "", $file);
Richard Kennedy00d62962008-02-23 15:24:01 -08001953 } elsif ($arg =~ m/\(.+\)\s*\(/) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 # pointer-to-function
1955 $arg =~ tr/#/,/;
Richard Kennedy00d62962008-02-23 15:24:01 -08001956 $arg =~ m/[^\(]+\(\*?\s*(\w*)\s*\)/;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 $param = $1;
1958 $type = $arg;
Richard Kennedy00d62962008-02-23 15:24:01 -08001959 $type =~ s/([^\(]+\(\*?)\s*$param/$1/;
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001960 save_struct_actual($param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 push_parameter($param, $type, $file);
Martin Waitzaeec46b2005-11-13 16:08:13 -08001962 } elsif ($arg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 $arg =~ s/\s*:\s*/:/g;
1964 $arg =~ s/\s*\[/\[/g;
1965
1966 my @args = split('\s*,\s*', $arg);
1967 if ($args[0] =~ m/\*/) {
1968 $args[0] =~ s/(\*+)\s*/ $1/;
1969 }
Borislav Petkov884f2812007-05-08 00:29:05 -07001970
1971 my @first_arg;
1972 if ($args[0] =~ /^(.*\s+)(.*?\[.*\].*)$/) {
1973 shift @args;
1974 push(@first_arg, split('\s+', $1));
1975 push(@first_arg, $2);
1976 } else {
1977 @first_arg = split('\s+', shift @args);
1978 }
1979
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 unshift(@args, pop @first_arg);
1981 $type = join " ", @first_arg;
1982
1983 foreach $param (@args) {
1984 if ($param =~ m/^(\*+)\s*(.*)/) {
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001985 save_struct_actual($2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 push_parameter($2, "$type $1", $file);
1987 }
1988 elsif ($param =~ m/(.*?):(\d+)/) {
Randy Dunlap7b978872008-05-16 15:45:52 -07001989 if ($type ne "") { # skip unnamed bit-fields
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001990 save_struct_actual($1);
Randy Dunlap7b978872008-05-16 15:45:52 -07001991 push_parameter($1, "$type:$2", $file)
1992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 }
1994 else {
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001995 save_struct_actual($param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 push_parameter($param, $type, $file);
1997 }
1998 }
1999 }
2000 }
2001}
2002
2003sub push_parameter($$$) {
2004 my $param = shift;
2005 my $type = shift;
2006 my $file = shift;
2007
Randy Dunlap5f8c7c92007-07-19 01:48:24 -07002008 if (($anon_struct_union == 1) && ($type eq "") &&
2009 ($param eq "}")) {
2010 return; # ignore the ending }; from anon. struct/union
2011 }
2012
2013 $anon_struct_union = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 my $param_name = $param;
2015 $param_name =~ s/\[.*//;
2016
Martin Waitza6d3fe72006-01-09 20:53:55 -08002017 if ($type eq "" && $param =~ /\.\.\.$/)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 {
Randy Dunlapced69092008-12-01 13:14:03 -08002019 if (!defined $parameterdescs{$param} || $parameterdescs{$param} eq "") {
2020 $parameterdescs{$param} = "variable arguments";
2021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 }
2023 elsif ($type eq "" && ($param eq "" or $param eq "void"))
2024 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 $param="void";
2026 $parameterdescs{void} = "no arguments";
2027 }
Randy Dunlap134fe012006-12-22 01:10:50 -08002028 elsif ($type eq "" && ($param eq "struct" or $param eq "union"))
2029 # handle unnamed (anonymous) union or struct:
2030 {
2031 $type = $param;
Randy Dunlap5f8c7c92007-07-19 01:48:24 -07002032 $param = "{unnamed_" . $param . "}";
Randy Dunlap134fe012006-12-22 01:10:50 -08002033 $parameterdescs{$param} = "anonymous\n";
Randy Dunlap5f8c7c92007-07-19 01:48:24 -07002034 $anon_struct_union = 1;
Randy Dunlap134fe012006-12-22 01:10:50 -08002035 }
2036
Martin Waitza6d3fe72006-01-09 20:53:55 -08002037 # warn if parameter has no description
Randy Dunlap134fe012006-12-22 01:10:50 -08002038 # (but ignore ones starting with # as these are not parameters
2039 # but inline preprocessor statements);
2040 # also ignore unnamed structs/unions;
Randy Dunlap5f8c7c92007-07-19 01:48:24 -07002041 if (!$anon_struct_union) {
Martin Waitza6d3fe72006-01-09 20:53:55 -08002042 if (!defined $parameterdescs{$param_name} && $param_name !~ /^#/) {
2043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 $parameterdescs{$param_name} = $undescribed;
2045
2046 if (($type eq 'function') || ($type eq 'enum')) {
Bart Van Assched40e1e62015-09-04 15:43:21 -07002047 print STDERR "${file}:$.: warning: Function parameter ".
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 "or member '$param' not " .
2049 "described in '$declaration_name'\n";
2050 }
Bart Van Assched40e1e62015-09-04 15:43:21 -07002051 print STDERR "${file}:$.: warning:" .
Randy Dunlap3c308792007-05-08 00:24:39 -07002052 " No description found for parameter '$param'\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 ++$warnings;
Randy Dunlap3c308792007-05-08 00:24:39 -07002054 }
2055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
Randy Dunlap2b35f4d2010-11-18 12:27:31 -08002057 $param = xml_escape($param);
2058
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002059 # strip spaces from $param so that it is one continuous string
Randy Dunlape34e7db2009-06-17 17:37:47 -07002060 # on @parameterlist;
2061 # this fixes a problem where check_sections() cannot find
2062 # a parameter like "addr[6 + 2]" because it actually appears
2063 # as "addr[6", "+", "2]" on the parameter list;
2064 # but it's better to maintain the param string unchanged for output,
2065 # so just weaken the string compare in check_sections() to ignore
2066 # "[blah" in a parameter string;
2067 ###$param =~ s/\s*//g;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 push @parameterlist, $param;
2069 $parametertypes{$param} = $type;
2070}
2071
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08002072sub check_sections($$$$$$) {
2073 my ($file, $decl_name, $decl_type, $sectcheck, $prmscheck, $nested) = @_;
2074 my @sects = split ' ', $sectcheck;
2075 my @prms = split ' ', $prmscheck;
2076 my $err;
2077 my ($px, $sx);
2078 my $prm_clean; # strip trailing "[array size]" and/or beginning "*"
2079
2080 foreach $sx (0 .. $#sects) {
2081 $err = 1;
2082 foreach $px (0 .. $#prms) {
2083 $prm_clean = $prms[$px];
2084 $prm_clean =~ s/\[.*\]//;
Johannes Berg1f3a6682010-09-11 15:55:12 -07002085 $prm_clean =~ s/__attribute__\s*\(\([a-z,_\*\s\(\)]*\)\)//i;
Randy Dunlape34e7db2009-06-17 17:37:47 -07002086 # ignore array size in a parameter string;
2087 # however, the original param string may contain
2088 # spaces, e.g.: addr[6 + 2]
2089 # and this appears in @prms as "addr[6" since the
2090 # parameter list is split at spaces;
2091 # hence just ignore "[..." for the sections check;
2092 $prm_clean =~ s/\[.*//;
2093
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08002094 ##$prm_clean =~ s/^\**//;
2095 if ($prm_clean eq $sects[$sx]) {
2096 $err = 0;
2097 last;
2098 }
2099 }
2100 if ($err) {
2101 if ($decl_type eq "function") {
Bart Van Assched40e1e62015-09-04 15:43:21 -07002102 print STDERR "${file}:$.: warning: " .
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08002103 "Excess function parameter " .
2104 "'$sects[$sx]' " .
2105 "description in '$decl_name'\n";
2106 ++$warnings;
2107 } else {
2108 if ($nested !~ m/\Q$sects[$sx]\E/) {
Bart Van Assched40e1e62015-09-04 15:43:21 -07002109 print STDERR "${file}:$.: warning: " .
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08002110 "Excess struct/union/enum/typedef member " .
2111 "'$sects[$sx]' " .
2112 "description in '$decl_name'\n";
2113 ++$warnings;
2114 }
2115 }
2116 }
2117 }
2118}
2119
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120##
Yacine Belkadi4092bac2012-11-26 22:22:27 +01002121# Checks the section describing the return value of a function.
2122sub check_return_section {
2123 my $file = shift;
2124 my $declaration_name = shift;
2125 my $return_type = shift;
2126
2127 # Ignore an empty return type (It's a macro)
2128 # Ignore functions with a "void" return type. (But don't ignore "void *")
2129 if (($return_type eq "") || ($return_type =~ /void\s*\w*\s*$/)) {
2130 return;
2131 }
2132
2133 if (!defined($sections{$section_return}) ||
2134 $sections{$section_return} eq "") {
Bart Van Assched40e1e62015-09-04 15:43:21 -07002135 print STDERR "${file}:$.: warning: " .
Yacine Belkadi4092bac2012-11-26 22:22:27 +01002136 "No description found for return value of " .
2137 "'$declaration_name'\n";
2138 ++$warnings;
2139 }
2140}
2141
2142##
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143# takes a function prototype and the name of the current file being
2144# processed and spits out all the details stored in the global
2145# arrays/hashes.
2146sub dump_function($$) {
2147 my $prototype = shift;
2148 my $file = shift;
Horia Geantacbb4d3e2014-07-12 09:55:03 -07002149 my $noret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
2151 $prototype =~ s/^static +//;
2152 $prototype =~ s/^extern +//;
Pavel Pisa4dc3b162005-05-01 08:59:25 -07002153 $prototype =~ s/^asmlinkage +//;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 $prototype =~ s/^inline +//;
2155 $prototype =~ s/^__inline__ +//;
Randy Dunlap32e79402006-10-11 01:22:10 -07002156 $prototype =~ s/^__inline +//;
2157 $prototype =~ s/^__always_inline +//;
2158 $prototype =~ s/^noinline +//;
Randy Dunlap74fc5c62008-06-19 16:03:29 -07002159 $prototype =~ s/__init +//;
Randy Dunlap20072202010-03-23 13:35:24 -07002160 $prototype =~ s/__init_or_module +//;
Randy Dunlap270a0092014-08-24 18:17:17 -07002161 $prototype =~ s/__meminit +//;
Randy Dunlap70c95b02012-01-21 10:31:54 -08002162 $prototype =~ s/__must_check +//;
Randy Dunlap0df7c0e2012-08-16 16:23:20 -07002163 $prototype =~ s/__weak +//;
Horia Geantacbb4d3e2014-07-12 09:55:03 -07002164 my $define = $prototype =~ s/^#\s*define\s+//; #ak added
Randy Dunlap328d2442007-02-28 20:12:10 -08002165 $prototype =~ s/__attribute__\s*\(\([a-z,]*\)\)//;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
2167 # Yes, this truly is vile. We are looking for:
2168 # 1. Return type (may be nothing if we're looking at a macro)
2169 # 2. Function name
2170 # 3. Function parameters.
2171 #
2172 # All the while we have to watch out for function pointer parameters
2173 # (which IIRC is what the two sections are for), C types (these
2174 # regexps don't even start to express all the possibilities), and
2175 # so on.
2176 #
2177 # If you mess with these regexps, it's a good idea to check that
2178 # the following functions' documentation still comes out right:
2179 # - parport_register_device (function pointer parameters)
2180 # - atomic_set (macro)
Martin Waitz9598f912006-02-01 03:06:55 -08002181 # - pci_match_device, __copy_to_user (long return type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
Horia Geantacbb4d3e2014-07-12 09:55:03 -07002183 if ($define && $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s+/) {
2184 # This is an object-like macro, it has no return type and no parameter
2185 # list.
2186 # Function-like macros are not allowed to have spaces between
2187 # declaration_name and opening parenthesis (notice the \s+).
2188 $return_type = $1;
2189 $declaration_name = $2;
2190 $noret = 1;
2191 } elsif ($prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
2193 $prototype =~ m/^(\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
2194 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
Randy Dunlap94b3e032008-02-07 00:13:26 -08002195 $prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
2197 $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
2198 $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
2199 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
2200 $prototype =~ m/^(\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
2201 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
2202 $prototype =~ m/^(\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
2203 $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
Martin Waitz9598f912006-02-01 03:06:55 -08002204 $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
2205 $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
Randy Dunlap412ecd772007-02-10 22:47:33 -08002206 $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
2207 $prototype =~ m/^(\w+\s+\w+\s*\*\s*\w+\s*\*\s*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 $return_type = $1;
2209 $declaration_name = $2;
2210 my $args = $3;
2211
2212 create_parameterlist($args, ',', $file);
2213 } else {
Bart Van Assched40e1e62015-09-04 15:43:21 -07002214 print STDERR "${file}:$.: warning: cannot understand function prototype: '$prototype'\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 return;
2216 }
2217
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08002218 my $prms = join " ", @parameterlist;
2219 check_sections($file, $declaration_name, "function", $sectcheck, $prms, "");
2220
Yacine Belkadi4092bac2012-11-26 22:22:27 +01002221 # This check emits a lot of warnings at the moment, because many
2222 # functions don't have a 'Return' doc section. So until the number
2223 # of warnings goes sufficiently down, the check is only performed in
2224 # verbose mode.
2225 # TODO: always perform the check.
Horia Geantacbb4d3e2014-07-12 09:55:03 -07002226 if ($verbose && !$noret) {
Yacine Belkadi4092bac2012-11-26 22:22:27 +01002227 check_return_section($file, $declaration_name, $return_type);
2228 }
2229
Randy Dunlap3c3b8092006-02-01 03:06:58 -08002230 output_declaration($declaration_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 'function',
2232 {'function' => $declaration_name,
2233 'module' => $modulename,
2234 'functiontype' => $return_type,
2235 'parameterlist' => \@parameterlist,
2236 'parameterdescs' => \%parameterdescs,
2237 'parametertypes' => \%parametertypes,
2238 'sectionlist' => \@sectionlist,
2239 'sections' => \%sections,
2240 'purpose' => $declaration_purpose
2241 });
2242}
2243
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244sub reset_state {
2245 $function = "";
2246 %constants = ();
2247 %parameterdescs = ();
2248 %parametertypes = ();
2249 @parameterlist = ();
2250 %sections = ();
2251 @sectionlist = ();
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08002252 $sectcheck = "";
2253 $struct_actual = "";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 $prototype = "";
Randy Dunlap3c3b8092006-02-01 03:06:58 -08002255
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 $state = 0;
Danilo Cesar Lemes de Paulaa4c6ebe2015-08-04 09:04:08 -03002257 $split_doc_state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258}
2259
Jason Baron56afb0f2009-04-30 13:29:36 -04002260sub tracepoint_munge($) {
2261 my $file = shift;
2262 my $tracepointname = 0;
2263 my $tracepointargs = 0;
2264
Jason Baron3a9089f2009-12-01 12:18:49 -05002265 if ($prototype =~ m/TRACE_EVENT\((.*?),/) {
Jason Baron56afb0f2009-04-30 13:29:36 -04002266 $tracepointname = $1;
2267 }
Jason Baron3a9089f2009-12-01 12:18:49 -05002268 if ($prototype =~ m/DEFINE_SINGLE_EVENT\((.*?),/) {
2269 $tracepointname = $1;
2270 }
2271 if ($prototype =~ m/DEFINE_EVENT\((.*?),(.*?),/) {
2272 $tracepointname = $2;
2273 }
2274 $tracepointname =~ s/^\s+//; #strip leading whitespace
2275 if ($prototype =~ m/TP_PROTO\((.*?)\)/) {
Jason Baron56afb0f2009-04-30 13:29:36 -04002276 $tracepointargs = $1;
2277 }
2278 if (($tracepointname eq 0) || ($tracepointargs eq 0)) {
Bart Van Assched40e1e62015-09-04 15:43:21 -07002279 print STDERR "${file}:$.: warning: Unrecognized tracepoint format: \n".
Jason Baron56afb0f2009-04-30 13:29:36 -04002280 "$prototype\n";
2281 } else {
2282 $prototype = "static inline void trace_$tracepointname($tracepointargs)";
2283 }
2284}
2285
Randy Dunlapb4870bc2009-02-11 13:04:33 -08002286sub syscall_munge() {
2287 my $void = 0;
2288
2289 $prototype =~ s@[\r\n\t]+@ @gos; # strip newlines/CR's/tabs
2290## if ($prototype =~ m/SYSCALL_DEFINE0\s*\(\s*(a-zA-Z0-9_)*\s*\)/) {
2291 if ($prototype =~ m/SYSCALL_DEFINE0/) {
2292 $void = 1;
2293## $prototype = "long sys_$1(void)";
2294 }
2295
2296 $prototype =~ s/SYSCALL_DEFINE.*\(/long sys_/; # fix return type & func name
2297 if ($prototype =~ m/long (sys_.*?),/) {
2298 $prototype =~ s/,/\(/;
2299 } elsif ($void) {
2300 $prototype =~ s/\)/\(void\)/;
2301 }
2302
2303 # now delete all of the odd-number commas in $prototype
2304 # so that arg types & arg names don't have a comma between them
2305 my $count = 0;
2306 my $len = length($prototype);
2307 if ($void) {
2308 $len = 0; # skip the for-loop
2309 }
2310 for (my $ix = 0; $ix < $len; $ix++) {
2311 if (substr($prototype, $ix, 1) eq ',') {
2312 $count++;
2313 if ($count % 2 == 1) {
2314 substr($prototype, $ix, 1) = ' ';
2315 }
2316 }
2317 }
2318}
2319
Randy Dunlap3c3b8092006-02-01 03:06:58 -08002320sub process_state3_function($$) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 my $x = shift;
2322 my $file = shift;
2323
Randy Dunlap51f5a0c2007-07-19 01:48:24 -07002324 $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
2325
Randy Dunlap890c78c2008-10-25 17:06:43 -07002326 if ($x =~ m#\s*/\*\s+MACDOC\s*#io || ($x =~ /^#/ && $x !~ /^#\s*define/)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 # do nothing
2328 }
2329 elsif ($x =~ /([^\{]*)/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07002330 $prototype .= $1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 }
Randy Dunlapb4870bc2009-02-11 13:04:33 -08002332
Randy Dunlap890c78c2008-10-25 17:06:43 -07002333 if (($x =~ /\{/) || ($x =~ /\#\s*define/) || ($x =~ /;/)) {
Randy Dunlap3c308792007-05-08 00:24:39 -07002334 $prototype =~ s@/\*.*?\*/@@gos; # strip comments.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 $prototype =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
2336 $prototype =~ s@^\s+@@gos; # strip leading spaces
Randy Dunlapb4870bc2009-02-11 13:04:33 -08002337 if ($prototype =~ /SYSCALL_DEFINE/) {
2338 syscall_munge();
2339 }
Jason Baron3a9089f2009-12-01 12:18:49 -05002340 if ($prototype =~ /TRACE_EVENT/ || $prototype =~ /DEFINE_EVENT/ ||
2341 $prototype =~ /DEFINE_SINGLE_EVENT/)
2342 {
Jason Baron56afb0f2009-04-30 13:29:36 -04002343 tracepoint_munge($file);
2344 }
Randy Dunlapb4870bc2009-02-11 13:04:33 -08002345 dump_function($prototype, $file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 reset_state();
2347 }
2348}
2349
Randy Dunlap3c3b8092006-02-01 03:06:58 -08002350sub process_state3_type($$) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 my $x = shift;
2352 my $file = shift;
2353
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 $x =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
2355 $x =~ s@^\s+@@gos; # strip leading spaces
2356 $x =~ s@\s+$@@gos; # strip trailing spaces
Randy Dunlap51f5a0c2007-07-19 01:48:24 -07002357 $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
2358
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 if ($x =~ /^#/) {
2360 # To distinguish preprocessor directive from regular declaration later.
2361 $x .= ";";
2362 }
2363
2364 while (1) {
Randy Dunlap3c308792007-05-08 00:24:39 -07002365 if ( $x =~ /([^{};]*)([{};])(.*)/ ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 $prototype .= $1 . $2;
2367 ($2 eq '{') && $brcount++;
2368 ($2 eq '}') && $brcount--;
2369 if (($2 eq ';') && ($brcount == 0)) {
Randy Dunlapb9d973282009-06-09 08:50:38 -07002370 dump_declaration($prototype, $file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 reset_state();
Randy Dunlap3c308792007-05-08 00:24:39 -07002372 last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 }
2374 $x = $3;
Randy Dunlap3c308792007-05-08 00:24:39 -07002375 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 $prototype .= $x;
2377 last;
2378 }
2379 }
2380}
2381
Randy Dunlap6b5b55f2007-10-16 23:31:20 -07002382# xml_escape: replace <, >, and & in the text stream;
2383#
2384# however, formatting controls that are generated internally/locally in the
2385# kernel-doc script are not escaped here; instead, they begin life like
2386# $blankline_html (4 of '\' followed by a mnemonic + ':'), then these strings
2387# are converted to their mnemonic-expected output, without the 4 * '\' & ':',
2388# just before actual output; (this is done by local_unescape())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389sub xml_escape($) {
2390 my $text = shift;
Randy Dunlapecfb2512006-06-25 05:49:13 -07002391 if (($output_mode eq "text") || ($output_mode eq "man")) {
2392 return $text;
2393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 $text =~ s/\&/\\\\\\amp;/g;
2395 $text =~ s/\</\\\\\\lt;/g;
2396 $text =~ s/\>/\\\\\\gt;/g;
2397 return $text;
2398}
2399
Randy Dunlap6b5b55f2007-10-16 23:31:20 -07002400# convert local escape strings to html
2401# local escape strings look like: '\\\\menmonic:' (that's 4 backslashes)
2402sub local_unescape($) {
2403 my $text = shift;
2404 if (($output_mode eq "text") || ($output_mode eq "man")) {
2405 return $text;
2406 }
2407 $text =~ s/\\\\\\\\lt:/</g;
2408 $text =~ s/\\\\\\\\gt:/>/g;
2409 return $text;
2410}
2411
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412sub process_file($) {
Randy Dunlap2283a112005-07-07 15:39:26 -07002413 my $file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 my $identifier;
2415 my $func;
Randy Dunlapa21217d2007-02-10 01:46:04 -08002416 my $descr;
Johannes Weiner64231332009-09-17 19:26:53 -07002417 my $in_purpose = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 my $initial_section_counter = $section_counter;
2419
Randy Dunlap2283a112005-07-07 15:39:26 -07002420 if (defined($ENV{'SRCTREE'})) {
2421 $file = "$ENV{'SRCTREE'}" . "/" . "@_";
2422 }
2423 else {
2424 $file = "@_";
2425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 if (defined($source_map{$file})) {
2427 $file = $source_map{$file};
2428 }
2429
2430 if (!open(IN,"<$file")) {
2431 print STDERR "Error: Cannot open file $file\n";
2432 ++$errors;
2433 return;
2434 }
2435
Ilya Dryomova9e73142010-02-26 13:05:47 -08002436 $. = 1;
2437
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 $section_counter = 0;
2439 while (<IN>) {
Daniel Santos65478422012-10-04 17:15:05 -07002440 while (s/\\\s*$//) {
2441 $_ .= <IN>;
2442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 if ($state == 0) {
2444 if (/$doc_start/o) {
2445 $state = 1; # next line is always the function name
Randy Dunlap850622d2006-06-25 05:48:55 -07002446 $in_doc_sect = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 }
2448 } elsif ($state == 1) { # this line is the function name (always)
2449 if (/$doc_block/o) {
2450 $state = 4;
2451 $contents = "";
2452 if ( $1 eq "" ) {
2453 $section = $section_intro;
2454 } else {
2455 $section = $1;
2456 }
Randy Dunlap3c308792007-05-08 00:24:39 -07002457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 elsif (/$doc_decl/o) {
2459 $identifier = $1;
2460 if (/\s*([\w\s]+?)\s*-/) {
2461 $identifier = $1;
2462 }
2463
2464 $state = 2;
2465 if (/-(.*)/) {
Randy Dunlap51f5a0c2007-07-19 01:48:24 -07002466 # strip leading/trailing/multiple spaces
Randy Dunlapa21217d2007-02-10 01:46:04 -08002467 $descr= $1;
2468 $descr =~ s/^\s*//;
2469 $descr =~ s/\s*$//;
Daniel Santos12ae6772012-10-04 17:15:10 -07002470 $descr =~ s/\s+/ /g;
Randy Dunlapa21217d2007-02-10 01:46:04 -08002471 $declaration_purpose = xml_escape($descr);
Johannes Weiner64231332009-09-17 19:26:53 -07002472 $in_purpose = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 } else {
2474 $declaration_purpose = "";
2475 }
Randy Dunlap77cc23b2008-02-07 00:13:42 -08002476
2477 if (($declaration_purpose eq "") && $verbose) {
Bart Van Assched40e1e62015-09-04 15:43:21 -07002478 print STDERR "${file}:$.: warning: missing initial short description on line:\n";
Randy Dunlap77cc23b2008-02-07 00:13:42 -08002479 print STDERR $_;
2480 ++$warnings;
2481 }
2482
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 if ($identifier =~ m/^struct/) {
2484 $decl_type = 'struct';
2485 } elsif ($identifier =~ m/^union/) {
2486 $decl_type = 'union';
2487 } elsif ($identifier =~ m/^enum/) {
2488 $decl_type = 'enum';
2489 } elsif ($identifier =~ m/^typedef/) {
2490 $decl_type = 'typedef';
2491 } else {
2492 $decl_type = 'function';
2493 }
2494
2495 if ($verbose) {
Bart Van Assched40e1e62015-09-04 15:43:21 -07002496 print STDERR "${file}:$.: info: Scanning doc for $identifier\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 }
2498 } else {
Bart Van Assched40e1e62015-09-04 15:43:21 -07002499 print STDERR "${file}:$.: warning: Cannot understand $_ on line $.",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 " - I thought it was a doc line\n";
2501 ++$warnings;
2502 $state = 0;
2503 }
2504 } elsif ($state == 2) { # look for head: lines, and include content
2505 if (/$doc_sect/o) {
2506 $newsection = $1;
2507 $newcontents = $2;
2508
Randy Dunlap792aa2f2008-02-07 00:13:41 -08002509 if (($contents ne "") && ($contents ne "\n")) {
Randy Dunlap850622d2006-06-25 05:48:55 -07002510 if (!$in_doc_sect && $verbose) {
Bart Van Assched40e1e62015-09-04 15:43:21 -07002511 print STDERR "${file}:$.: warning: contents before sections\n";
Randy Dunlap850622d2006-06-25 05:48:55 -07002512 ++$warnings;
2513 }
Randy Dunlap94dc7ad2008-04-28 02:16:34 -07002514 dump_section($file, $section, xml_escape($contents));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 $section = $section_default;
2516 }
2517
Randy Dunlap850622d2006-06-25 05:48:55 -07002518 $in_doc_sect = 1;
Johannes Weiner64231332009-09-17 19:26:53 -07002519 $in_purpose = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 $contents = $newcontents;
2521 if ($contents ne "") {
Randy Dunlap27205742006-10-11 01:22:12 -07002522 while ((substr($contents, 0, 1) eq " ") ||
2523 substr($contents, 0, 1) eq "\t") {
2524 $contents = substr($contents, 1);
Randy Dunlap05189492006-06-25 05:47:47 -07002525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 $contents .= "\n";
2527 }
2528 $section = $newsection;
2529 } elsif (/$doc_end/) {
Randy Dunlap4c98eca2010-03-10 15:22:02 -08002530 if (($contents ne "") && ($contents ne "\n")) {
Randy Dunlap94dc7ad2008-04-28 02:16:34 -07002531 dump_section($file, $section, xml_escape($contents));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 $section = $section_default;
2533 $contents = "";
2534 }
Randy Dunlap46b958e2008-04-28 02:16:35 -07002535 # look for doc_com + <text> + doc_end:
2536 if ($_ =~ m'\s*\*\s*[a-zA-Z_0-9:\.]+\*/') {
Bart Van Assched40e1e62015-09-04 15:43:21 -07002537 print STDERR "${file}:$.: warning: suspicious ending line: $_";
Randy Dunlap46b958e2008-04-28 02:16:35 -07002538 ++$warnings;
2539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
2541 $prototype = "";
2542 $state = 3;
2543 $brcount = 0;
Randy Dunlap232acbc2006-06-25 05:47:48 -07002544# print STDERR "end of doc comment, looking for prototype\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 } elsif (/$doc_content/) {
2546 # miguel-style comment kludge, look for blank lines after
2547 # @parameter line to signify start of description
Johannes Weiner64231332009-09-17 19:26:53 -07002548 if ($1 eq "") {
2549 if ($section =~ m/^@/ || $section eq $section_context) {
2550 dump_section($file, $section, xml_escape($contents));
2551 $section = $section_default;
2552 $contents = "";
2553 } else {
2554 $contents .= "\n";
2555 }
2556 $in_purpose = 0;
2557 } elsif ($in_purpose == 1) {
2558 # Continued declaration purpose
2559 chomp($declaration_purpose);
2560 $declaration_purpose .= " " . xml_escape($1);
Daniel Santos12ae6772012-10-04 17:15:10 -07002561 $declaration_purpose =~ s/\s+/ /g;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 } else {
Randy Dunlapb9d973282009-06-09 08:50:38 -07002563 $contents .= $1 . "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 }
2565 } else {
2566 # i dont know - bad line? ignore.
Bart Van Assched40e1e62015-09-04 15:43:21 -07002567 print STDERR "${file}:$.: warning: bad line: $_";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 ++$warnings;
2569 }
Danilo Cesar Lemes de Paulaa4c6ebe2015-08-04 09:04:08 -03002570 } elsif ($state == 5) { # scanning for split parameters
2571 # First line (state 1) needs to be a @parameter
2572 if ($split_doc_state == 1 && /$doc_split_sect/o) {
2573 $section = $1;
2574 $contents = $2;
2575 if ($contents ne "") {
2576 while ((substr($contents, 0, 1) eq " ") ||
2577 substr($contents, 0, 1) eq "\t") {
2578 $contents = substr($contents, 1);
2579 }
2580 $contents .= "\n";
2581 }
2582 $split_doc_state = 2;
2583 # Documentation block end */
2584 } elsif (/$doc_split_end/) {
2585 if (($contents ne "") && ($contents ne "\n")) {
2586 dump_section($file, $section, xml_escape($contents));
2587 $section = $section_default;
2588 $contents = "";
2589 }
2590 $state = 3;
2591 $split_doc_state = 0;
2592 # Regular text
2593 } elsif (/$doc_content/) {
2594 if ($split_doc_state == 2) {
2595 $contents .= $1 . "\n";
2596 } elsif ($split_doc_state == 1) {
2597 $split_doc_state = 4;
2598 print STDERR "Warning(${file}:$.): ";
2599 print STDERR "Incorrect use of kernel-doc format: $_";
2600 ++$warnings;
2601 }
2602 }
Randy Dunlap232acbc2006-06-25 05:47:48 -07002603 } elsif ($state == 3) { # scanning for function '{' (end of prototype)
Danilo Cesar Lemes de Paulaa4c6ebe2015-08-04 09:04:08 -03002604 if (/$doc_split_start/) {
2605 $state = 5;
2606 $split_doc_state = 1;
2607 } elsif ($decl_type eq 'function') {
Randy Dunlap3c308792007-05-08 00:24:39 -07002608 process_state3_function($_, $file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 } else {
Randy Dunlap3c308792007-05-08 00:24:39 -07002610 process_state3_type($_, $file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 }
2612 } elsif ($state == 4) {
2613 # Documentation block
Randy Dunlap3c308792007-05-08 00:24:39 -07002614 if (/$doc_block/) {
Randy Dunlap94dc7ad2008-04-28 02:16:34 -07002615 dump_doc_section($file, $section, xml_escape($contents));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 $contents = "";
2617 $function = "";
2618 %constants = ();
2619 %parameterdescs = ();
2620 %parametertypes = ();
2621 @parameterlist = ();
2622 %sections = ();
2623 @sectionlist = ();
2624 $prototype = "";
2625 if ( $1 eq "" ) {
2626 $section = $section_intro;
2627 } else {
2628 $section = $1;
2629 }
Randy Dunlap3c308792007-05-08 00:24:39 -07002630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 elsif (/$doc_end/)
2632 {
Randy Dunlap94dc7ad2008-04-28 02:16:34 -07002633 dump_doc_section($file, $section, xml_escape($contents));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 $contents = "";
2635 $function = "";
2636 %constants = ();
2637 %parameterdescs = ();
2638 %parametertypes = ();
2639 @parameterlist = ();
2640 %sections = ();
2641 @sectionlist = ();
2642 $prototype = "";
2643 $state = 0;
2644 }
2645 elsif (/$doc_content/)
2646 {
2647 if ( $1 eq "" )
2648 {
2649 $contents .= $blankline;
2650 }
2651 else
2652 {
2653 $contents .= $1 . "\n";
Randy Dunlap3c3b8092006-02-01 03:06:58 -08002654 }
Randy Dunlap3c308792007-05-08 00:24:39 -07002655 }
2656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 }
2658 if ($initial_section_counter == $section_counter) {
Bart Van Assched40e1e62015-09-04 15:43:21 -07002659 print STDERR "${file}:1: warning: no structured comments found\n";
Johannes Berge946c43a2013-11-12 15:11:12 -08002660 if (($function_only == 1) && ($show_not_found == 1)) {
2661 print STDERR " Was looking for '$_'.\n" for keys %function_table;
2662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 if ($output_mode eq "xml") {
2664 # The template wants at least one RefEntry here; make one.
2665 print "<refentry>\n";
2666 print " <refnamediv>\n";
2667 print " <refname>\n";
2668 print " ${file}\n";
2669 print " </refname>\n";
2670 print " <refpurpose>\n";
2671 print " Document generation inconsistency\n";
2672 print " </refpurpose>\n";
2673 print " </refnamediv>\n";
2674 print " <refsect1>\n";
2675 print " <title>\n";
2676 print " Oops\n";
2677 print " </title>\n";
2678 print " <warning>\n";
2679 print " <para>\n";
2680 print " The template for this document tried to insert\n";
2681 print " the structured comment from the file\n";
2682 print " <filename>${file}</filename> at this point,\n";
2683 print " but none was found.\n";
2684 print " This dummy section is inserted to allow\n";
2685 print " generation to continue.\n";
2686 print " </para>\n";
2687 print " </warning>\n";
2688 print " </refsect1>\n";
2689 print "</refentry>\n";
2690 }
2691 }
2692}
Randy Dunlap8484baa2011-01-05 16:28:43 -08002693
2694
2695$kernelversion = get_kernel_version();
2696
2697# generate a sequence of code that will splice in highlighting information
2698# using the s// operator.
Jérémy Bobbio2e106e42015-07-08 20:06:33 +01002699foreach my $pattern (sort keys %highlights) {
Randy Dunlap8484baa2011-01-05 16:28:43 -08002700# print STDERR "scanning pattern:$pattern, highlight:($highlights{$pattern})\n";
2701 $dohighlight .= "\$contents =~ s:$pattern:$highlights{$pattern}:gs;\n";
2702}
2703
2704# Read the file that maps relative names to absolute names for
2705# separate source and object directories and for shadow trees.
2706if (open(SOURCE_MAP, "<.tmp_filelist.txt")) {
2707 my ($relname, $absname);
2708 while(<SOURCE_MAP>) {
2709 chop();
2710 ($relname, $absname) = (split())[0..1];
2711 $relname =~ s:^/+::;
2712 $source_map{$relname} = $absname;
2713 }
2714 close(SOURCE_MAP);
2715}
2716
2717foreach (@ARGV) {
2718 chomp;
2719 process_file($_);
2720}
2721if ($verbose && $errors) {
2722 print STDERR "$errors errors\n";
2723}
2724if ($verbose && $warnings) {
2725 print STDERR "$warnings warnings\n";
2726}
2727
2728exit($errors);