blob: 0ac1a07874cc7bfe5e6677f3976e608ff6195559 [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#
136# You can also add additional sections. When documenting kernel functions you
137# should document the "Context:" of the function, e.g. whether the functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138# can be called form interrupts. Unlike other sections you can end it with an
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800139# empty line.
Yacine Belkadi4092bac2012-11-26 22:22:27 +0100140# A non-void function should have a "Return:" section describing the return
141# value(s).
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800142# Example-sections should contain the string EXAMPLE so that they are marked
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143# appropriately in DocBook.
144#
145# Example:
146# /**
147# * user_function - function that can only be called in user context
148# * @a: some argument
149# * Context: !in_interrupt()
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800150# *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151# * Some description
152# * Example:
153# * user_function(22);
154# */
155# ...
156#
157#
158# All descriptive text is further processed, scanning for the following special
159# patterns, which are highlighted appropriately.
160#
161# 'funcname()' - function
162# '$ENVVAR' - environmental variable
163# '&struct_name' - name of a structure (up to two words including 'struct')
164# '@parameter' - name of a parameter
165# '%CONST' - name of a constant.
166
Randy Dunlap8484baa2011-01-05 16:28:43 -0800167## init lots of data
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169my $errors = 0;
170my $warnings = 0;
Randy Dunlap5f8c7c92007-07-19 01:48:24 -0700171my $anon_struct_union = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173# match expressions used to find embedded type information
174my $type_constant = '\%([-_\w]+)';
175my $type_func = '(\w+)\(\)';
176my $type_param = '\@(\w+)';
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700177my $type_struct = '\&((struct\s*)*[_\w]+)';
Randy Dunlap6b5b55f2007-10-16 23:31:20 -0700178my $type_struct_xml = '\\&amp;((struct\s*)*[_\w]+)';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179my $type_env = '(\$\w+)';
180
181# Output conversion substitutions.
182# One for each output format
183
184# these work fairly well
185my %highlights_html = ( $type_constant, "<i>\$1</i>",
186 $type_func, "<b>\$1</b>",
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700187 $type_struct_xml, "<i>\$1</i>",
188 $type_env, "<b><i>\$1</i></b>",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 $type_param, "<tt><b>\$1</b></tt>" );
Randy Dunlap6b5b55f2007-10-16 23:31:20 -0700190my $local_lt = "\\\\\\\\lt:";
191my $local_gt = "\\\\\\\\gt:";
192my $blankline_html = $local_lt . "p" . $local_gt; # was "<p>"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Dan Luedtke1b40c192012-08-12 10:46:15 +0200194# html version 5
195my %highlights_html5 = ( $type_constant, "<span class=\"const\">\$1</span>",
196 $type_func, "<span class=\"func\">\$1</span>",
197 $type_struct_xml, "<span class=\"struct\">\$1</span>",
198 $type_env, "<span class=\"env\">\$1</span>",
199 $type_param, "<span class=\"param\">\$1</span>" );
200my $blankline_html5 = $local_lt . "br /" . $local_gt;
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202# XML, docbook format
203my %highlights_xml = ( "([^=])\\\"([^\\\"<]+)\\\"", "\$1<quote>\$2</quote>",
204 $type_constant, "<constant>\$1</constant>",
205 $type_func, "<function>\$1</function>",
Johannes Berg5c98fc02007-10-24 15:08:48 -0700206 $type_struct_xml, "<structname>\$1</structname>",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 $type_env, "<envar>\$1</envar>",
208 $type_param, "<parameter>\$1</parameter>" );
Johannes Berg5c98fc02007-10-24 15:08:48 -0700209my $blankline_xml = $local_lt . "/para" . $local_gt . $local_lt . "para" . $local_gt . "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211# gnome, docbook format
212my %highlights_gnome = ( $type_constant, "<replaceable class=\"option\">\$1</replaceable>",
213 $type_func, "<function>\$1</function>",
214 $type_struct, "<structname>\$1</structname>",
215 $type_env, "<envar>\$1</envar>",
216 $type_param, "<parameter>\$1</parameter>" );
217my $blankline_gnome = "</para><para>\n";
218
219# these are pretty rough
220my %highlights_man = ( $type_constant, "\$1",
221 $type_func, "\\\\fB\$1\\\\fP",
222 $type_struct, "\\\\fI\$1\\\\fP",
223 $type_param, "\\\\fI\$1\\\\fP" );
224my $blankline_man = "";
225
226# text-mode
227my %highlights_text = ( $type_constant, "\$1",
228 $type_func, "\$1",
229 $type_struct, "\$1",
230 $type_param, "\$1" );
231my $blankline_text = "";
232
Johannes Bergeda603f2010-09-11 15:55:22 -0700233# list mode
234my %highlights_list = ( $type_constant, "\$1",
235 $type_func, "\$1",
236 $type_struct, "\$1",
237 $type_param, "\$1" );
238my $blankline_list = "";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240# read arguments
Randy Dunlapb9d973282009-06-09 08:50:38 -0700241if ($#ARGV == -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 usage();
243}
244
Randy Dunlap8484baa2011-01-05 16:28:43 -0800245my $kernelversion;
246my $dohighlight = "";
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248my $verbose = 0;
249my $output_mode = "man";
Daniel Santose314ba32012-10-04 17:15:08 -0700250my $output_preformatted = 0;
Johannes Berg4b445952007-10-24 15:08:48 -0700251my $no_doc_sections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252my %highlights = %highlights_man;
253my $blankline = $blankline_man;
254my $modulename = "Kernel API";
255my $function_only = 0;
Ben Hutchingsb2c41052015-07-08 20:07:16 +0100256my $show_not_found = 0;
257
258my @build_time;
259if (defined($ENV{'KBUILD_BUILD_TIMESTAMP'}) &&
260 (my $seconds = `date -d"${ENV{'KBUILD_BUILD_TIMESTAMP'}}" +%s`) ne '') {
261 @build_time = gmtime($seconds);
262} else {
263 @build_time = localtime;
264}
265
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800266my $man_date = ('January', 'February', 'March', 'April', 'May', 'June',
267 'July', 'August', 'September', 'October',
Ben Hutchingsb2c41052015-07-08 20:07:16 +0100268 'November', 'December')[$build_time[4]] .
269 " " . ($build_time[5]+1900);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Randy Dunlap8484baa2011-01-05 16:28:43 -0800271# Essentially these are globals.
Randy Dunlapb9d973282009-06-09 08:50:38 -0700272# They probably want to be tidied up, made more localised or something.
273# CAVEAT EMPTOR! Some of the others I localised may not want to be, which
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274# could cause "use of undefined value" or other bugs.
Randy Dunlapb9d973282009-06-09 08:50:38 -0700275my ($function, %function_table, %parametertypes, $declaration_purpose);
276my ($type, $declaration_name, $return_type);
Ilya Dryomov1c32fd02010-02-26 13:06:03 -0800277my ($newsection, $newcontents, $prototype, $brcount, %source_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Randy Dunlapbd0e88e2008-03-13 12:32:43 -0700279if (defined($ENV{'KBUILD_VERBOSE'})) {
280 $verbose = "$ENV{'KBUILD_VERBOSE'}";
281}
282
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800283# Generated docbook code is inserted in a template at a point where
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284# docbook v3.1 requires a non-zero sequence of RefEntry's; see:
285# http://www.oasis-open.org/docbook/documentation/reference/html/refentry.html
286# We keep track of number of generated entries and generate a dummy
287# if needs be to ensure the expanded template can be postprocessed
288# into html.
289my $section_counter = 0;
290
291my $lineprefix="";
292
293# states
294# 0 - normal code
295# 1 - looking for function name
296# 2 - scanning field start.
297# 3 - scanning prototype.
298# 4 - documentation block
299my $state;
Randy Dunlap850622d2006-06-25 05:48:55 -0700300my $in_doc_sect;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302#declaration types: can be
303# 'function', 'struct', 'union', 'enum', 'typedef'
304my $decl_type;
305
306my $doc_special = "\@\%\$\&";
307
308my $doc_start = '^/\*\*\s*$'; # Allow whitespace at end of comment start.
309my $doc_end = '\*/';
310my $doc_com = '\s*\*\s*';
Daniel Santos12ae6772012-10-04 17:15:10 -0700311my $doc_com_body = '\s*\* ?';
Randy Dunlapb9d973282009-06-09 08:50:38 -0700312my $doc_decl = $doc_com . '(\w+)';
313my $doc_sect = $doc_com . '([' . $doc_special . ']?[\w\s]+):(.*)';
Daniel Santos12ae6772012-10-04 17:15:10 -0700314my $doc_content = $doc_com_body . '(.*)';
Randy Dunlapb9d973282009-06-09 08:50:38 -0700315my $doc_block = $doc_com . 'DOC:\s*(.*)?';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317my %constants;
318my %parameterdescs;
319my @parameterlist;
320my %sections;
321my @sectionlist;
Randy Dunlapa1d94aa2008-12-19 08:49:30 -0800322my $sectcheck;
323my $struct_actual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325my $contents = "";
326my $section_default = "Description"; # default section
327my $section_intro = "Introduction";
328my $section = $section_default;
329my $section_context = "Context";
Yacine Belkadi4092bac2012-11-26 22:22:27 +0100330my $section_return = "Return";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332my $undescribed = "-- undescribed --";
333
334reset_state();
335
336while ($ARGV[0] =~ m/^-(.*)/) {
337 my $cmd = shift @ARGV;
338 if ($cmd eq "-html") {
339 $output_mode = "html";
340 %highlights = %highlights_html;
341 $blankline = $blankline_html;
Dan Luedtke1b40c192012-08-12 10:46:15 +0200342 } elsif ($cmd eq "-html5") {
343 $output_mode = "html5";
344 %highlights = %highlights_html5;
345 $blankline = $blankline_html5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 } elsif ($cmd eq "-man") {
347 $output_mode = "man";
348 %highlights = %highlights_man;
349 $blankline = $blankline_man;
350 } elsif ($cmd eq "-text") {
351 $output_mode = "text";
352 %highlights = %highlights_text;
353 $blankline = $blankline_text;
354 } elsif ($cmd eq "-docbook") {
355 $output_mode = "xml";
356 %highlights = %highlights_xml;
357 $blankline = $blankline_xml;
Johannes Bergeda603f2010-09-11 15:55:22 -0700358 } elsif ($cmd eq "-list") {
359 $output_mode = "list";
360 %highlights = %highlights_list;
361 $blankline = $blankline_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 } elsif ($cmd eq "-gnome") {
363 $output_mode = "gnome";
364 %highlights = %highlights_gnome;
365 $blankline = $blankline_gnome;
366 } elsif ($cmd eq "-module") { # not needed for XML, inherits from calling document
367 $modulename = shift @ARGV;
368 } elsif ($cmd eq "-function") { # to only output specific functions
369 $function_only = 1;
370 $function = shift @ARGV;
371 $function_table{$function} = 1;
372 } elsif ($cmd eq "-nofunction") { # to only output specific functions
373 $function_only = 2;
374 $function = shift @ARGV;
375 $function_table{$function} = 1;
376 } elsif ($cmd eq "-v") {
377 $verbose = 1;
378 } elsif (($cmd eq "-h") || ($cmd eq "--help")) {
379 usage();
Johannes Berg4b445952007-10-24 15:08:48 -0700380 } elsif ($cmd eq '-no-doc-sections') {
381 $no_doc_sections = 1;
Johannes Berge946c43a2013-11-12 15:11:12 -0800382 } elsif ($cmd eq '-show-not-found') {
383 $show_not_found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
385}
386
Randy Dunlap8484baa2011-01-05 16:28:43 -0800387# continue execution near EOF;
388
389sub usage {
Dan Luedtke1b40c192012-08-12 10:46:15 +0200390 print "Usage: $0 [ -docbook | -html | -html5 | -text | -man | -list ]\n";
Randy Dunlap8484baa2011-01-05 16:28:43 -0800391 print " [ -no-doc-sections ]\n";
392 print " [ -function funcname [ -function funcname ...] ]\n";
393 print " [ -nofunction funcname [ -nofunction funcname ...] ]\n";
Dan Luedtke1b40c192012-08-12 10:46:15 +0200394 print " [ -v ]\n";
Randy Dunlap8484baa2011-01-05 16:28:43 -0800395 print " c source file(s) > outputfile\n";
396 print " -v : verbose output, more warnings & other info listed\n";
397 exit 1;
398}
399
Borislav Petkov53f049f2007-05-08 00:30:54 -0700400# get kernel version from env
401sub get_kernel_version() {
Johannes Berg1b9bc222007-10-24 15:08:48 -0700402 my $version = 'unknown kernel version';
Borislav Petkov53f049f2007-05-08 00:30:54 -0700403
404 if (defined($ENV{'KERNELVERSION'})) {
405 $version = $ENV{'KERNELVERSION'};
406 }
407 return $version;
408}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410##
411# dumps section contents to arrays/hashes intended for that purpose.
412#
413sub dump_section {
Randy Dunlap94dc7ad2008-04-28 02:16:34 -0700414 my $file = shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 my $name = shift;
416 my $contents = join "\n", @_;
417
418 if ($name =~ m/$type_constant/) {
419 $name = $1;
420# print STDERR "constant section '$1' = '$contents'\n";
421 $constants{$name} = $contents;
422 } elsif ($name =~ m/$type_param/) {
423# print STDERR "parameter def '$1' = '$contents'\n";
424 $name = $1;
425 $parameterdescs{$name} = $contents;
Randy Dunlapa1d94aa2008-12-19 08:49:30 -0800426 $sectcheck = $sectcheck . $name . " ";
Randy Dunlapced69092008-12-01 13:14:03 -0800427 } elsif ($name eq "@\.\.\.") {
428# print STDERR "parameter def '...' = '$contents'\n";
429 $name = "...";
430 $parameterdescs{$name} = $contents;
Randy Dunlapa1d94aa2008-12-19 08:49:30 -0800431 $sectcheck = $sectcheck . $name . " ";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 } else {
433# print STDERR "other section '$name' = '$contents'\n";
Randy Dunlap94dc7ad2008-04-28 02:16:34 -0700434 if (defined($sections{$name}) && ($sections{$name} ne "")) {
435 print STDERR "Error(${file}:$.): duplicate section name '$name'\n";
436 ++$errors;
437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 $sections{$name} = $contents;
439 push @sectionlist, $name;
440 }
441}
442
443##
Johannes Bergb112e0f2007-10-24 15:08:48 -0700444# dump DOC: section after checking that it should go out
445#
446sub dump_doc_section {
Randy Dunlap94dc7ad2008-04-28 02:16:34 -0700447 my $file = shift;
Johannes Bergb112e0f2007-10-24 15:08:48 -0700448 my $name = shift;
449 my $contents = join "\n", @_;
450
Johannes Berg4b445952007-10-24 15:08:48 -0700451 if ($no_doc_sections) {
452 return;
453 }
454
Johannes Bergb112e0f2007-10-24 15:08:48 -0700455 if (($function_only == 0) ||
456 ( $function_only == 1 && defined($function_table{$name})) ||
457 ( $function_only == 2 && !defined($function_table{$name})))
458 {
Randy Dunlap94dc7ad2008-04-28 02:16:34 -0700459 dump_section($file, $name, $contents);
Johannes Bergb112e0f2007-10-24 15:08:48 -0700460 output_blockhead({'sectionlist' => \@sectionlist,
461 'sections' => \%sections,
462 'module' => $modulename,
463 'content-only' => ($function_only != 0), });
464 }
465}
466
467##
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468# output function
469#
470# parameterdescs, a hash.
471# function => "function name"
472# parameterlist => @list of parameters
473# parameterdescs => %parameter descriptions
474# sectionlist => @list of sections
Randy Dunlapa21217d2007-02-10 01:46:04 -0800475# sections => %section descriptions
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800476#
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478sub output_highlight {
479 my $contents = join "\n",@_;
480 my $line;
481
482# DEBUG
483# if (!defined $contents) {
484# use Carp;
485# confess "output_highlight got called with no args?\n";
486# }
487
Dan Luedtke1b40c192012-08-12 10:46:15 +0200488 if ($output_mode eq "html" || $output_mode eq "html5" ||
489 $output_mode eq "xml") {
Randy Dunlap6b5b55f2007-10-16 23:31:20 -0700490 $contents = local_unescape($contents);
491 # convert data read & converted thru xml_escape() into &xyz; format:
Randy Dunlap2b35f4d2010-11-18 12:27:31 -0800492 $contents =~ s/\\\\\\/\&/g;
Randy Dunlap6b5b55f2007-10-16 23:31:20 -0700493 }
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700494# print STDERR "contents b4:$contents\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 eval $dohighlight;
496 die $@ if $@;
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700497# print STDERR "contents af:$contents\n";
498
Dan Luedtke1b40c192012-08-12 10:46:15 +0200499# strip whitespaces when generating html5
500 if ($output_mode eq "html5") {
501 $contents =~ s/^\s+//;
502 $contents =~ s/\s+$//;
503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 foreach $line (split "\n", $contents) {
Daniel Santos12ae6772012-10-04 17:15:10 -0700505 if (! $output_preformatted) {
506 $line =~ s/^\s*//;
507 }
Randy Dunlap3c308792007-05-08 00:24:39 -0700508 if ($line eq ""){
Daniel Santose314ba32012-10-04 17:15:08 -0700509 if (! $output_preformatted) {
510 print $lineprefix, local_unescape($blankline);
511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 } else {
Randy Dunlap3c308792007-05-08 00:24:39 -0700513 $line =~ s/\\\\\\/\&/g;
Randy Dunlapcdccb312007-07-19 01:48:25 -0700514 if ($output_mode eq "man" && substr($line, 0, 1) eq ".") {
515 print "\\&$line";
516 } else {
517 print $lineprefix, $line;
518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520 print "\n";
521 }
522}
523
Dan Luedtke1b40c192012-08-12 10:46:15 +0200524# output sections in html
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525sub output_section_html(%) {
526 my %args = %{$_[0]};
527 my $section;
528
529 foreach $section (@{$args{'sectionlist'}}) {
530 print "<h3>$section</h3>\n";
531 print "<blockquote>\n";
532 output_highlight($args{'sections'}{$section});
533 print "</blockquote>\n";
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
537# output enum in html
538sub output_enum_html(%) {
539 my %args = %{$_[0]};
540 my ($parameter);
541 my $count;
Randy Dunlapb9d973282009-06-09 08:50:38 -0700542 print "<h2>enum " . $args{'enum'} . "</h2>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Randy Dunlapb9d973282009-06-09 08:50:38 -0700544 print "<b>enum " . $args{'enum'} . "</b> {<br>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 $count = 0;
546 foreach $parameter (@{$args{'parameterlist'}}) {
Randy Dunlapb9d973282009-06-09 08:50:38 -0700547 print " <b>" . $parameter . "</b>";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if ($count != $#{$args{'parameterlist'}}) {
549 $count++;
550 print ",\n";
551 }
552 print "<br>";
553 }
554 print "};<br>\n";
555
556 print "<h3>Constants</h3>\n";
557 print "<dl>\n";
558 foreach $parameter (@{$args{'parameterlist'}}) {
Randy Dunlapb9d973282009-06-09 08:50:38 -0700559 print "<dt><b>" . $parameter . "</b>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 print "<dd>";
561 output_highlight($args{'parameterdescs'}{$parameter});
562 }
563 print "</dl>\n";
564 output_section_html(@_);
565 print "<hr>\n";
566}
567
Randy Dunlapd28bee02006-02-01 03:06:57 -0800568# output typedef in html
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569sub output_typedef_html(%) {
570 my %args = %{$_[0]};
571 my ($parameter);
572 my $count;
Randy Dunlapb9d973282009-06-09 08:50:38 -0700573 print "<h2>typedef " . $args{'typedef'} . "</h2>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Randy Dunlapb9d973282009-06-09 08:50:38 -0700575 print "<b>typedef " . $args{'typedef'} . "</b>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 output_section_html(@_);
577 print "<hr>\n";
578}
579
580# output struct in html
581sub output_struct_html(%) {
582 my %args = %{$_[0]};
583 my ($parameter);
584
Randy Dunlapb9d973282009-06-09 08:50:38 -0700585 print "<h2>" . $args{'type'} . " " . $args{'struct'} . " - " . $args{'purpose'} . "</h2>\n";
586 print "<b>" . $args{'type'} . " " . $args{'struct'} . "</b> {<br>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 foreach $parameter (@{$args{'parameterlist'}}) {
588 if ($parameter =~ /^#/) {
589 print "$parameter<br>\n";
590 next;
591 }
592 my $parameter_name = $parameter;
593 $parameter_name =~ s/\[.*//;
594
Randy Dunlap3c308792007-05-08 00:24:39 -0700595 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 $type = $args{'parametertypes'}{$parameter};
597 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
598 # pointer-to-function
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700599 print "&nbsp; &nbsp; <i>$1</i><b>$parameter</b>) <i>($2)</i>;<br>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700601 # bitfield
602 print "&nbsp; &nbsp; <i>$1</i> <b>$parameter</b>$2;<br>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 } else {
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700604 print "&nbsp; &nbsp; <i>$type</i> <b>$parameter</b>;<br>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
606 }
607 print "};<br>\n";
608
609 print "<h3>Members</h3>\n";
610 print "<dl>\n";
611 foreach $parameter (@{$args{'parameterlist'}}) {
612 ($parameter =~ /^#/) && next;
613
614 my $parameter_name = $parameter;
615 $parameter_name =~ s/\[.*//;
616
Randy Dunlap3c308792007-05-08 00:24:39 -0700617 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Randy Dunlapb9d973282009-06-09 08:50:38 -0700618 print "<dt><b>" . $parameter . "</b>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 print "<dd>";
620 output_highlight($args{'parameterdescs'}{$parameter_name});
621 }
622 print "</dl>\n";
623 output_section_html(@_);
624 print "<hr>\n";
625}
626
627# output function in html
628sub output_function_html(%) {
629 my %args = %{$_[0]};
630 my ($parameter, $section);
631 my $count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Randy Dunlapb9d973282009-06-09 08:50:38 -0700633 print "<h2>" . $args{'function'} . " - " . $args{'purpose'} . "</h2>\n";
634 print "<i>" . $args{'functiontype'} . "</i>\n";
635 print "<b>" . $args{'function'} . "</b>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 print "(";
637 $count = 0;
638 foreach $parameter (@{$args{'parameterlist'}}) {
639 $type = $args{'parametertypes'}{$parameter};
640 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
641 # pointer-to-function
642 print "<i>$1</i><b>$parameter</b>) <i>($2)</i>";
643 } else {
Randy Dunlapb9d973282009-06-09 08:50:38 -0700644 print "<i>" . $type . "</i> <b>" . $parameter . "</b>";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
646 if ($count != $#{$args{'parameterlist'}}) {
647 $count++;
648 print ",\n";
649 }
650 }
651 print ")\n";
652
653 print "<h3>Arguments</h3>\n";
654 print "<dl>\n";
655 foreach $parameter (@{$args{'parameterlist'}}) {
656 my $parameter_name = $parameter;
657 $parameter_name =~ s/\[.*//;
658
Randy Dunlap3c308792007-05-08 00:24:39 -0700659 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Randy Dunlapb9d973282009-06-09 08:50:38 -0700660 print "<dt><b>" . $parameter . "</b>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 print "<dd>";
662 output_highlight($args{'parameterdescs'}{$parameter_name});
663 }
664 print "</dl>\n";
665 output_section_html(@_);
666 print "<hr>\n";
667}
668
Johannes Bergb112e0f2007-10-24 15:08:48 -0700669# output DOC: block header in html
670sub output_blockhead_html(%) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 my %args = %{$_[0]};
672 my ($parameter, $section);
673 my $count;
674
675 foreach $section (@{$args{'sectionlist'}}) {
676 print "<h3>$section</h3>\n";
677 print "<ul>\n";
678 output_highlight($args{'sections'}{$section});
679 print "</ul>\n";
680 }
681 print "<hr>\n";
682}
683
Dan Luedtke1b40c192012-08-12 10:46:15 +0200684# output sections in html5
685sub output_section_html5(%) {
686 my %args = %{$_[0]};
687 my $section;
688
689 foreach $section (@{$args{'sectionlist'}}) {
690 print "<section>\n";
691 print "<h1>$section</h1>\n";
692 print "<p>\n";
693 output_highlight($args{'sections'}{$section});
694 print "</p>\n";
695 print "</section>\n";
696 }
697}
698
699# output enum in html5
700sub output_enum_html5(%) {
701 my %args = %{$_[0]};
702 my ($parameter);
703 my $count;
704 my $html5id;
705
706 $html5id = $args{'enum'};
707 $html5id =~ s/[^a-zA-Z0-9\-]+/_/g;
708 print "<article class=\"enum\" id=\"enum:". $html5id . "\">";
709 print "<h1>enum " . $args{'enum'} . "</h1>\n";
710 print "<ol class=\"code\">\n";
711 print "<li>";
712 print "<span class=\"keyword\">enum</span> ";
713 print "<span class=\"identifier\">" . $args{'enum'} . "</span> {";
714 print "</li>\n";
715 $count = 0;
716 foreach $parameter (@{$args{'parameterlist'}}) {
717 print "<li class=\"indent\">";
718 print "<span class=\"param\">" . $parameter . "</span>";
719 if ($count != $#{$args{'parameterlist'}}) {
720 $count++;
721 print ",";
722 }
723 print "</li>\n";
724 }
725 print "<li>};</li>\n";
726 print "</ol>\n";
727
728 print "<section>\n";
729 print "<h1>Constants</h1>\n";
730 print "<dl>\n";
731 foreach $parameter (@{$args{'parameterlist'}}) {
732 print "<dt>" . $parameter . "</dt>\n";
733 print "<dd>";
734 output_highlight($args{'parameterdescs'}{$parameter});
735 print "</dd>\n";
736 }
737 print "</dl>\n";
738 print "</section>\n";
739 output_section_html5(@_);
740 print "</article>\n";
741}
742
743# output typedef in html5
744sub output_typedef_html5(%) {
745 my %args = %{$_[0]};
746 my ($parameter);
747 my $count;
748 my $html5id;
749
750 $html5id = $args{'typedef'};
751 $html5id =~ s/[^a-zA-Z0-9\-]+/_/g;
752 print "<article class=\"typedef\" id=\"typedef:" . $html5id . "\">\n";
753 print "<h1>typedef " . $args{'typedef'} . "</h1>\n";
754
755 print "<ol class=\"code\">\n";
756 print "<li>";
757 print "<span class=\"keyword\">typedef</span> ";
758 print "<span class=\"identifier\">" . $args{'typedef'} . "</span>";
759 print "</li>\n";
760 print "</ol>\n";
761 output_section_html5(@_);
762 print "</article>\n";
763}
764
765# output struct in html5
766sub output_struct_html5(%) {
767 my %args = %{$_[0]};
768 my ($parameter);
769 my $html5id;
770
771 $html5id = $args{'struct'};
772 $html5id =~ s/[^a-zA-Z0-9\-]+/_/g;
773 print "<article class=\"struct\" id=\"struct:" . $html5id . "\">\n";
774 print "<hgroup>\n";
775 print "<h1>" . $args{'type'} . " " . $args{'struct'} . "</h1>";
776 print "<h2>". $args{'purpose'} . "</h2>\n";
777 print "</hgroup>\n";
778 print "<ol class=\"code\">\n";
779 print "<li>";
780 print "<span class=\"type\">" . $args{'type'} . "</span> ";
781 print "<span class=\"identifier\">" . $args{'struct'} . "</span> {";
782 print "</li>\n";
783 foreach $parameter (@{$args{'parameterlist'}}) {
784 print "<li class=\"indent\">";
785 if ($parameter =~ /^#/) {
786 print "<span class=\"param\">" . $parameter ."</span>\n";
787 print "</li>\n";
788 next;
789 }
790 my $parameter_name = $parameter;
791 $parameter_name =~ s/\[.*//;
792
793 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
794 $type = $args{'parametertypes'}{$parameter};
795 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
796 # pointer-to-function
797 print "<span class=\"type\">$1</span> ";
798 print "<span class=\"param\">$parameter</span>";
799 print "<span class=\"type\">)</span> ";
800 print "(<span class=\"args\">$2</span>);";
801 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
802 # bitfield
803 print "<span class=\"type\">$1</span> ";
804 print "<span class=\"param\">$parameter</span>";
805 print "<span class=\"bits\">$2</span>;";
806 } else {
807 print "<span class=\"type\">$type</span> ";
808 print "<span class=\"param\">$parameter</span>;";
809 }
810 print "</li>\n";
811 }
812 print "<li>};</li>\n";
813 print "</ol>\n";
814
815 print "<section>\n";
816 print "<h1>Members</h1>\n";
817 print "<dl>\n";
818 foreach $parameter (@{$args{'parameterlist'}}) {
819 ($parameter =~ /^#/) && next;
820
821 my $parameter_name = $parameter;
822 $parameter_name =~ s/\[.*//;
823
824 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
825 print "<dt>" . $parameter . "</dt>\n";
826 print "<dd>";
827 output_highlight($args{'parameterdescs'}{$parameter_name});
828 print "</dd>\n";
829 }
830 print "</dl>\n";
831 print "</section>\n";
832 output_section_html5(@_);
833 print "</article>\n";
834}
835
836# output function in html5
837sub output_function_html5(%) {
838 my %args = %{$_[0]};
839 my ($parameter, $section);
840 my $count;
841 my $html5id;
842
843 $html5id = $args{'function'};
844 $html5id =~ s/[^a-zA-Z0-9\-]+/_/g;
845 print "<article class=\"function\" id=\"func:". $html5id . "\">\n";
846 print "<hgroup>\n";
847 print "<h1>" . $args{'function'} . "</h1>";
848 print "<h2>" . $args{'purpose'} . "</h2>\n";
849 print "</hgroup>\n";
850 print "<ol class=\"code\">\n";
851 print "<li>";
852 print "<span class=\"type\">" . $args{'functiontype'} . "</span> ";
853 print "<span class=\"identifier\">" . $args{'function'} . "</span> (";
854 print "</li>";
855 $count = 0;
856 foreach $parameter (@{$args{'parameterlist'}}) {
857 print "<li class=\"indent\">";
858 $type = $args{'parametertypes'}{$parameter};
859 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
860 # pointer-to-function
861 print "<span class=\"type\">$1</span> ";
862 print "<span class=\"param\">$parameter</span>";
863 print "<span class=\"type\">)</span> ";
864 print "(<span class=\"args\">$2</span>)";
865 } else {
866 print "<span class=\"type\">$type</span> ";
867 print "<span class=\"param\">$parameter</span>";
868 }
869 if ($count != $#{$args{'parameterlist'}}) {
870 $count++;
871 print ",";
872 }
873 print "</li>\n";
874 }
875 print "<li>)</li>\n";
876 print "</ol>\n";
877
878 print "<section>\n";
879 print "<h1>Arguments</h1>\n";
880 print "<p>\n";
881 print "<dl>\n";
882 foreach $parameter (@{$args{'parameterlist'}}) {
883 my $parameter_name = $parameter;
884 $parameter_name =~ s/\[.*//;
885
886 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
887 print "<dt>" . $parameter . "</dt>\n";
888 print "<dd>";
889 output_highlight($args{'parameterdescs'}{$parameter_name});
890 print "</dd>\n";
891 }
892 print "</dl>\n";
893 print "</section>\n";
894 output_section_html5(@_);
895 print "</article>\n";
896}
897
898# output DOC: block header in html5
899sub output_blockhead_html5(%) {
900 my %args = %{$_[0]};
901 my ($parameter, $section);
902 my $count;
903 my $html5id;
904
905 foreach $section (@{$args{'sectionlist'}}) {
906 $html5id = $section;
907 $html5id =~ s/[^a-zA-Z0-9\-]+/_/g;
908 print "<article class=\"doc\" id=\"doc:". $html5id . "\">\n";
909 print "<h1>$section</h1>\n";
910 print "<p>\n";
911 output_highlight($args{'sections'}{$section});
912 print "</p>\n";
913 }
914 print "</article>\n";
915}
916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917sub output_section_xml(%) {
918 my %args = %{$_[0]};
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800919 my $section;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 # print out each section
921 $lineprefix=" ";
922 foreach $section (@{$args{'sectionlist'}}) {
Rich Walkerc73894c2005-05-01 08:59:26 -0700923 print "<refsect1>\n";
924 print "<title>$section</title>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 if ($section =~ m/EXAMPLE/i) {
Rich Walkerc73894c2005-05-01 08:59:26 -0700926 print "<informalexample><programlisting>\n";
Daniel Santose314ba32012-10-04 17:15:08 -0700927 $output_preformatted = 1;
Rich Walkerc73894c2005-05-01 08:59:26 -0700928 } else {
929 print "<para>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 }
931 output_highlight($args{'sections'}{$section});
Daniel Santose314ba32012-10-04 17:15:08 -0700932 $output_preformatted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 if ($section =~ m/EXAMPLE/i) {
Rich Walkerc73894c2005-05-01 08:59:26 -0700934 print "</programlisting></informalexample>\n";
935 } else {
936 print "</para>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
Rich Walkerc73894c2005-05-01 08:59:26 -0700938 print "</refsect1>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
940}
941
942# output function in XML DocBook
943sub output_function_xml(%) {
944 my %args = %{$_[0]};
945 my ($parameter, $section);
946 my $count;
947 my $id;
948
Randy Dunlapb9d973282009-06-09 08:50:38 -0700949 $id = "API-" . $args{'function'};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 $id =~ s/[^A-Za-z0-9]/-/g;
951
Pavel Pisa5449bc92007-02-10 01:45:37 -0800952 print "<refentry id=\"$id\">\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700953 print "<refentryinfo>\n";
954 print " <title>LINUX</title>\n";
955 print " <productname>Kernel Hackers Manual</productname>\n";
956 print " <date>$man_date</date>\n";
957 print "</refentryinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 print "<refmeta>\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -0700959 print " <refentrytitle><phrase>" . $args{'function'} . "</phrase></refentrytitle>\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700960 print " <manvolnum>9</manvolnum>\n";
Borislav Petkov03662992007-05-09 02:33:43 -0700961 print " <refmiscinfo class=\"version\">" . $kernelversion . "</refmiscinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 print "</refmeta>\n";
963 print "<refnamediv>\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -0700964 print " <refname>" . $args{'function'} . "</refname>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 print " <refpurpose>\n";
966 print " ";
967 output_highlight ($args{'purpose'});
968 print " </refpurpose>\n";
969 print "</refnamediv>\n";
970
971 print "<refsynopsisdiv>\n";
972 print " <title>Synopsis</title>\n";
973 print " <funcsynopsis><funcprototype>\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -0700974 print " <funcdef>" . $args{'functiontype'} . " ";
975 print "<function>" . $args{'function'} . " </function></funcdef>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 $count = 0;
978 if ($#{$args{'parameterlist'}} >= 0) {
979 foreach $parameter (@{$args{'parameterlist'}}) {
980 $type = $args{'parametertypes'}{$parameter};
981 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
982 # pointer-to-function
983 print " <paramdef>$1<parameter>$parameter</parameter>)\n";
984 print " <funcparams>$2</funcparams></paramdef>\n";
985 } else {
Randy Dunlapb9d973282009-06-09 08:50:38 -0700986 print " <paramdef>" . $type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 print " <parameter>$parameter</parameter></paramdef>\n";
988 }
989 }
990 } else {
Martin Waitz6013d542005-05-01 08:59:25 -0700991 print " <void/>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
993 print " </funcprototype></funcsynopsis>\n";
994 print "</refsynopsisdiv>\n";
995
996 # print parameters
997 print "<refsect1>\n <title>Arguments</title>\n";
998 if ($#{$args{'parameterlist'}} >= 0) {
999 print " <variablelist>\n";
1000 foreach $parameter (@{$args{'parameterlist'}}) {
1001 my $parameter_name = $parameter;
1002 $parameter_name =~ s/\[.*//;
1003
1004 print " <varlistentry>\n <term><parameter>$parameter</parameter></term>\n";
1005 print " <listitem>\n <para>\n";
1006 $lineprefix=" ";
1007 output_highlight($args{'parameterdescs'}{$parameter_name});
1008 print " </para>\n </listitem>\n </varlistentry>\n";
1009 }
1010 print " </variablelist>\n";
1011 } else {
1012 print " <para>\n None\n </para>\n";
1013 }
1014 print "</refsect1>\n";
1015
1016 output_section_xml(@_);
1017 print "</refentry>\n\n";
1018}
1019
1020# output struct in XML DocBook
1021sub output_struct_xml(%) {
1022 my %args = %{$_[0]};
1023 my ($parameter, $section);
1024 my $id;
1025
Randy Dunlapb9d973282009-06-09 08:50:38 -07001026 $id = "API-struct-" . $args{'struct'};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 $id =~ s/[^A-Za-z0-9]/-/g;
1028
Pavel Pisa5449bc92007-02-10 01:45:37 -08001029 print "<refentry id=\"$id\">\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -07001030 print "<refentryinfo>\n";
1031 print " <title>LINUX</title>\n";
1032 print " <productname>Kernel Hackers Manual</productname>\n";
1033 print " <date>$man_date</date>\n";
1034 print "</refentryinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 print "<refmeta>\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001036 print " <refentrytitle><phrase>" . $args{'type'} . " " . $args{'struct'} . "</phrase></refentrytitle>\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -07001037 print " <manvolnum>9</manvolnum>\n";
Borislav Petkov03662992007-05-09 02:33:43 -07001038 print " <refmiscinfo class=\"version\">" . $kernelversion . "</refmiscinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 print "</refmeta>\n";
1040 print "<refnamediv>\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001041 print " <refname>" . $args{'type'} . " " . $args{'struct'} . "</refname>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 print " <refpurpose>\n";
1043 print " ";
1044 output_highlight ($args{'purpose'});
1045 print " </refpurpose>\n";
1046 print "</refnamediv>\n";
1047
1048 print "<refsynopsisdiv>\n";
1049 print " <title>Synopsis</title>\n";
1050 print " <programlisting>\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001051 print $args{'type'} . " " . $args{'struct'} . " {\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 foreach $parameter (@{$args{'parameterlist'}}) {
1053 if ($parameter =~ /^#/) {
Randy Dunlap2b35f4d2010-11-18 12:27:31 -08001054 my $prm = $parameter;
1055 # convert data read & converted thru xml_escape() into &xyz; format:
1056 # This allows us to have #define macros interspersed in a struct.
1057 $prm =~ s/\\\\\\/\&/g;
1058 print "$prm\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 next;
1060 }
1061
1062 my $parameter_name = $parameter;
1063 $parameter_name =~ s/\[.*//;
1064
1065 defined($args{'parameterdescs'}{$parameter_name}) || next;
Randy Dunlap3c308792007-05-08 00:24:39 -07001066 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 $type = $args{'parametertypes'}{$parameter};
1068 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1069 # pointer-to-function
1070 print " $1 $parameter) ($2);\n";
1071 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
Randy Dunlap51f5a0c2007-07-19 01:48:24 -07001072 # bitfield
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 print " $1 $parameter$2;\n";
1074 } else {
Randy Dunlapb9d973282009-06-09 08:50:38 -07001075 print " " . $type . " " . $parameter . ";\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 }
1077 }
1078 print "};";
1079 print " </programlisting>\n";
1080 print "</refsynopsisdiv>\n";
1081
1082 print " <refsect1>\n";
1083 print " <title>Members</title>\n";
1084
Randy Dunlap39f00c02008-09-22 13:57:44 -07001085 if ($#{$args{'parameterlist'}} >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 print " <variablelist>\n";
1087 foreach $parameter (@{$args{'parameterlist'}}) {
1088 ($parameter =~ /^#/) && next;
1089
1090 my $parameter_name = $parameter;
1091 $parameter_name =~ s/\[.*//;
1092
1093 defined($args{'parameterdescs'}{$parameter_name}) || next;
1094 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
1095 print " <varlistentry>";
1096 print " <term>$parameter</term>\n";
1097 print " <listitem><para>\n";
1098 output_highlight($args{'parameterdescs'}{$parameter_name});
1099 print " </para></listitem>\n";
1100 print " </varlistentry>\n";
1101 }
1102 print " </variablelist>\n";
Randy Dunlap39f00c02008-09-22 13:57:44 -07001103 } else {
1104 print " <para>\n None\n </para>\n";
1105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 print " </refsect1>\n";
1107
1108 output_section_xml(@_);
1109
1110 print "</refentry>\n\n";
1111}
1112
1113# output enum in XML DocBook
1114sub output_enum_xml(%) {
1115 my %args = %{$_[0]};
1116 my ($parameter, $section);
1117 my $count;
1118 my $id;
1119
Randy Dunlapb9d973282009-06-09 08:50:38 -07001120 $id = "API-enum-" . $args{'enum'};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 $id =~ s/[^A-Za-z0-9]/-/g;
1122
Pavel Pisa5449bc92007-02-10 01:45:37 -08001123 print "<refentry id=\"$id\">\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -07001124 print "<refentryinfo>\n";
1125 print " <title>LINUX</title>\n";
1126 print " <productname>Kernel Hackers Manual</productname>\n";
1127 print " <date>$man_date</date>\n";
1128 print "</refentryinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 print "<refmeta>\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001130 print " <refentrytitle><phrase>enum " . $args{'enum'} . "</phrase></refentrytitle>\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -07001131 print " <manvolnum>9</manvolnum>\n";
Borislav Petkov03662992007-05-09 02:33:43 -07001132 print " <refmiscinfo class=\"version\">" . $kernelversion . "</refmiscinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 print "</refmeta>\n";
1134 print "<refnamediv>\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001135 print " <refname>enum " . $args{'enum'} . "</refname>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 print " <refpurpose>\n";
1137 print " ";
1138 output_highlight ($args{'purpose'});
1139 print " </refpurpose>\n";
1140 print "</refnamediv>\n";
1141
1142 print "<refsynopsisdiv>\n";
1143 print " <title>Synopsis</title>\n";
1144 print " <programlisting>\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001145 print "enum " . $args{'enum'} . " {\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 $count = 0;
1147 foreach $parameter (@{$args{'parameterlist'}}) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001148 print " $parameter";
1149 if ($count != $#{$args{'parameterlist'}}) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 $count++;
1151 print ",";
Randy Dunlap3c308792007-05-08 00:24:39 -07001152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 print "\n";
1154 }
1155 print "};";
1156 print " </programlisting>\n";
1157 print "</refsynopsisdiv>\n";
1158
1159 print "<refsect1>\n";
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001160 print " <title>Constants</title>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 print " <variablelist>\n";
1162 foreach $parameter (@{$args{'parameterlist'}}) {
1163 my $parameter_name = $parameter;
1164 $parameter_name =~ s/\[.*//;
1165
1166 print " <varlistentry>";
1167 print " <term>$parameter</term>\n";
1168 print " <listitem><para>\n";
1169 output_highlight($args{'parameterdescs'}{$parameter_name});
1170 print " </para></listitem>\n";
1171 print " </varlistentry>\n";
1172 }
1173 print " </variablelist>\n";
1174 print "</refsect1>\n";
1175
1176 output_section_xml(@_);
1177
1178 print "</refentry>\n\n";
1179}
1180
1181# output typedef in XML DocBook
1182sub output_typedef_xml(%) {
1183 my %args = %{$_[0]};
1184 my ($parameter, $section);
1185 my $id;
1186
Randy Dunlapb9d973282009-06-09 08:50:38 -07001187 $id = "API-typedef-" . $args{'typedef'};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 $id =~ s/[^A-Za-z0-9]/-/g;
1189
Pavel Pisa5449bc92007-02-10 01:45:37 -08001190 print "<refentry id=\"$id\">\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -07001191 print "<refentryinfo>\n";
1192 print " <title>LINUX</title>\n";
1193 print " <productname>Kernel Hackers Manual</productname>\n";
1194 print " <date>$man_date</date>\n";
1195 print "</refentryinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 print "<refmeta>\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001197 print " <refentrytitle><phrase>typedef " . $args{'typedef'} . "</phrase></refentrytitle>\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -07001198 print " <manvolnum>9</manvolnum>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 print "</refmeta>\n";
1200 print "<refnamediv>\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001201 print " <refname>typedef " . $args{'typedef'} . "</refname>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 print " <refpurpose>\n";
1203 print " ";
1204 output_highlight ($args{'purpose'});
1205 print " </refpurpose>\n";
1206 print "</refnamediv>\n";
1207
1208 print "<refsynopsisdiv>\n";
1209 print " <title>Synopsis</title>\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001210 print " <synopsis>typedef " . $args{'typedef'} . ";</synopsis>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 print "</refsynopsisdiv>\n";
1212
1213 output_section_xml(@_);
1214
1215 print "</refentry>\n\n";
1216}
1217
1218# output in XML DocBook
Johannes Bergb112e0f2007-10-24 15:08:48 -07001219sub output_blockhead_xml(%) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 my %args = %{$_[0]};
1221 my ($parameter, $section);
1222 my $count;
1223
1224 my $id = $args{'module'};
1225 $id =~ s/[^A-Za-z0-9]/-/g;
1226
1227 # print out each section
1228 $lineprefix=" ";
1229 foreach $section (@{$args{'sectionlist'}}) {
Johannes Bergb112e0f2007-10-24 15:08:48 -07001230 if (!$args{'content-only'}) {
1231 print "<refsect1>\n <title>$section</title>\n";
1232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 if ($section =~ m/EXAMPLE/i) {
1234 print "<example><para>\n";
Daniel Santose314ba32012-10-04 17:15:08 -07001235 $output_preformatted = 1;
Johannes Bergb112e0f2007-10-24 15:08:48 -07001236 } else {
1237 print "<para>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 }
1239 output_highlight($args{'sections'}{$section});
Daniel Santose314ba32012-10-04 17:15:08 -07001240 $output_preformatted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 if ($section =~ m/EXAMPLE/i) {
1242 print "</para></example>\n";
Johannes Bergb112e0f2007-10-24 15:08:48 -07001243 } else {
1244 print "</para>";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 }
Johannes Bergb112e0f2007-10-24 15:08:48 -07001246 if (!$args{'content-only'}) {
1247 print "\n</refsect1>\n";
1248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 }
1250
1251 print "\n\n";
1252}
1253
1254# output in XML DocBook
1255sub output_function_gnome {
1256 my %args = %{$_[0]};
1257 my ($parameter, $section);
1258 my $count;
1259 my $id;
1260
Randy Dunlapb9d973282009-06-09 08:50:38 -07001261 $id = $args{'module'} . "-" . $args{'function'};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 $id =~ s/[^A-Za-z0-9]/-/g;
1263
1264 print "<sect2>\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001265 print " <title id=\"$id\">" . $args{'function'} . "</title>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 print " <funcsynopsis>\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001268 print " <funcdef>" . $args{'functiontype'} . " ";
1269 print "<function>" . $args{'function'} . " ";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 print "</function></funcdef>\n";
1271
1272 $count = 0;
1273 if ($#{$args{'parameterlist'}} >= 0) {
1274 foreach $parameter (@{$args{'parameterlist'}}) {
1275 $type = $args{'parametertypes'}{$parameter};
1276 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1277 # pointer-to-function
1278 print " <paramdef>$1 <parameter>$parameter</parameter>)\n";
1279 print " <funcparams>$2</funcparams></paramdef>\n";
1280 } else {
Randy Dunlapb9d973282009-06-09 08:50:38 -07001281 print " <paramdef>" . $type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 print " <parameter>$parameter</parameter></paramdef>\n";
1283 }
1284 }
1285 } else {
1286 print " <void>\n";
1287 }
1288 print " </funcsynopsis>\n";
1289 if ($#{$args{'parameterlist'}} >= 0) {
1290 print " <informaltable pgwide=\"1\" frame=\"none\" role=\"params\">\n";
1291 print "<tgroup cols=\"2\">\n";
1292 print "<colspec colwidth=\"2*\">\n";
1293 print "<colspec colwidth=\"8*\">\n";
1294 print "<tbody>\n";
1295 foreach $parameter (@{$args{'parameterlist'}}) {
1296 my $parameter_name = $parameter;
1297 $parameter_name =~ s/\[.*//;
1298
1299 print " <row><entry align=\"right\"><parameter>$parameter</parameter></entry>\n";
1300 print " <entry>\n";
1301 $lineprefix=" ";
1302 output_highlight($args{'parameterdescs'}{$parameter_name});
1303 print " </entry></row>\n";
1304 }
1305 print " </tbody></tgroup></informaltable>\n";
1306 } else {
1307 print " <para>\n None\n </para>\n";
1308 }
1309
1310 # print out each section
1311 $lineprefix=" ";
1312 foreach $section (@{$args{'sectionlist'}}) {
1313 print "<simplesect>\n <title>$section</title>\n";
1314 if ($section =~ m/EXAMPLE/i) {
1315 print "<example><programlisting>\n";
Daniel Santose314ba32012-10-04 17:15:08 -07001316 $output_preformatted = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 } else {
1318 }
1319 print "<para>\n";
1320 output_highlight($args{'sections'}{$section});
Daniel Santose314ba32012-10-04 17:15:08 -07001321 $output_preformatted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 print "</para>\n";
1323 if ($section =~ m/EXAMPLE/i) {
1324 print "</programlisting></example>\n";
1325 } else {
1326 }
1327 print " </simplesect>\n";
1328 }
1329
1330 print "</sect2>\n\n";
1331}
1332
1333##
1334# output function in man
1335sub output_function_man(%) {
1336 my %args = %{$_[0]};
1337 my ($parameter, $section);
1338 my $count;
1339
1340 print ".TH \"$args{'function'}\" 9 \"$args{'function'}\" \"$man_date\" \"Kernel Hacker's Manual\" LINUX\n";
1341
1342 print ".SH NAME\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001343 print $args{'function'} . " \\- " . $args{'purpose'} . "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 print ".SH SYNOPSIS\n";
Randy Dunlapa21217d2007-02-10 01:46:04 -08001346 if ($args{'functiontype'} ne "") {
Randy Dunlapb9d973282009-06-09 08:50:38 -07001347 print ".B \"" . $args{'functiontype'} . "\" " . $args{'function'} . "\n";
Randy Dunlapa21217d2007-02-10 01:46:04 -08001348 } else {
Randy Dunlapb9d973282009-06-09 08:50:38 -07001349 print ".B \"" . $args{'function'} . "\n";
Randy Dunlapa21217d2007-02-10 01:46:04 -08001350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 $count = 0;
1352 my $parenth = "(";
1353 my $post = ",";
1354 foreach my $parameter (@{$args{'parameterlist'}}) {
1355 if ($count == $#{$args{'parameterlist'}}) {
1356 $post = ");";
1357 }
1358 $type = $args{'parametertypes'}{$parameter};
1359 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1360 # pointer-to-function
Randy Dunlapb9d973282009-06-09 08:50:38 -07001361 print ".BI \"" . $parenth . $1 . "\" " . $parameter . " \") (" . $2 . ")" . $post . "\"\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 } else {
1363 $type =~ s/([^\*])$/$1 /;
Randy Dunlapb9d973282009-06-09 08:50:38 -07001364 print ".BI \"" . $parenth . $type . "\" " . $parameter . " \"" . $post . "\"\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 }
1366 $count++;
1367 $parenth = "";
1368 }
1369
1370 print ".SH ARGUMENTS\n";
1371 foreach $parameter (@{$args{'parameterlist'}}) {
1372 my $parameter_name = $parameter;
1373 $parameter_name =~ s/\[.*//;
1374
Randy Dunlapb9d973282009-06-09 08:50:38 -07001375 print ".IP \"" . $parameter . "\" 12\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 output_highlight($args{'parameterdescs'}{$parameter_name});
1377 }
1378 foreach $section (@{$args{'sectionlist'}}) {
1379 print ".SH \"", uc $section, "\"\n";
1380 output_highlight($args{'sections'}{$section});
1381 }
1382}
1383
1384##
1385# output enum in man
1386sub output_enum_man(%) {
1387 my %args = %{$_[0]};
1388 my ($parameter, $section);
1389 my $count;
1390
1391 print ".TH \"$args{'module'}\" 9 \"enum $args{'enum'}\" \"$man_date\" \"API Manual\" LINUX\n";
1392
1393 print ".SH NAME\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001394 print "enum " . $args{'enum'} . " \\- " . $args{'purpose'} . "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396 print ".SH SYNOPSIS\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001397 print "enum " . $args{'enum'} . " {\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 $count = 0;
1399 foreach my $parameter (@{$args{'parameterlist'}}) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001400 print ".br\n.BI \" $parameter\"\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 if ($count == $#{$args{'parameterlist'}}) {
1402 print "\n};\n";
1403 last;
1404 }
1405 else {
1406 print ", \n.br\n";
1407 }
1408 $count++;
1409 }
1410
1411 print ".SH Constants\n";
1412 foreach $parameter (@{$args{'parameterlist'}}) {
1413 my $parameter_name = $parameter;
1414 $parameter_name =~ s/\[.*//;
1415
Randy Dunlapb9d973282009-06-09 08:50:38 -07001416 print ".IP \"" . $parameter . "\" 12\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 output_highlight($args{'parameterdescs'}{$parameter_name});
1418 }
1419 foreach $section (@{$args{'sectionlist'}}) {
1420 print ".SH \"$section\"\n";
1421 output_highlight($args{'sections'}{$section});
1422 }
1423}
1424
1425##
1426# output struct in man
1427sub output_struct_man(%) {
1428 my %args = %{$_[0]};
1429 my ($parameter, $section);
1430
Randy Dunlapb9d973282009-06-09 08:50:38 -07001431 print ".TH \"$args{'module'}\" 9 \"" . $args{'type'} . " " . $args{'struct'} . "\" \"$man_date\" \"API Manual\" LINUX\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 print ".SH NAME\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001434 print $args{'type'} . " " . $args{'struct'} . " \\- " . $args{'purpose'} . "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
1436 print ".SH SYNOPSIS\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001437 print $args{'type'} . " " . $args{'struct'} . " {\n.br\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
1439 foreach my $parameter (@{$args{'parameterlist'}}) {
1440 if ($parameter =~ /^#/) {
1441 print ".BI \"$parameter\"\n.br\n";
1442 next;
1443 }
1444 my $parameter_name = $parameter;
1445 $parameter_name =~ s/\[.*//;
1446
Randy Dunlap3c308792007-05-08 00:24:39 -07001447 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 $type = $args{'parametertypes'}{$parameter};
1449 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1450 # pointer-to-function
Randy Dunlapb9d973282009-06-09 08:50:38 -07001451 print ".BI \" " . $1 . "\" " . $parameter . " \") (" . $2 . ")" . "\"\n;\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
Randy.Dunlap1d7e1d42006-07-01 04:36:34 -07001453 # bitfield
Randy Dunlapb9d973282009-06-09 08:50:38 -07001454 print ".BI \" " . $1 . "\ \" " . $parameter . $2 . " \"" . "\"\n;\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 } else {
1456 $type =~ s/([^\*])$/$1 /;
Randy Dunlapb9d973282009-06-09 08:50:38 -07001457 print ".BI \" " . $type . "\" " . $parameter . " \"" . "\"\n;\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 }
1459 print "\n.br\n";
1460 }
1461 print "};\n.br\n";
1462
Randy Dunlapc51d3da2006-06-25 05:49:14 -07001463 print ".SH Members\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 foreach $parameter (@{$args{'parameterlist'}}) {
1465 ($parameter =~ /^#/) && next;
1466
1467 my $parameter_name = $parameter;
1468 $parameter_name =~ s/\[.*//;
1469
Randy Dunlap3c308792007-05-08 00:24:39 -07001470 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Randy Dunlapb9d973282009-06-09 08:50:38 -07001471 print ".IP \"" . $parameter . "\" 12\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 output_highlight($args{'parameterdescs'}{$parameter_name});
1473 }
1474 foreach $section (@{$args{'sectionlist'}}) {
1475 print ".SH \"$section\"\n";
1476 output_highlight($args{'sections'}{$section});
1477 }
1478}
1479
1480##
1481# output typedef in man
1482sub output_typedef_man(%) {
1483 my %args = %{$_[0]};
1484 my ($parameter, $section);
1485
1486 print ".TH \"$args{'module'}\" 9 \"$args{'typedef'}\" \"$man_date\" \"API Manual\" LINUX\n";
1487
1488 print ".SH NAME\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001489 print "typedef " . $args{'typedef'} . " \\- " . $args{'purpose'} . "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
1491 foreach $section (@{$args{'sectionlist'}}) {
1492 print ".SH \"$section\"\n";
1493 output_highlight($args{'sections'}{$section});
1494 }
1495}
1496
Johannes Bergb112e0f2007-10-24 15:08:48 -07001497sub output_blockhead_man(%) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 my %args = %{$_[0]};
1499 my ($parameter, $section);
1500 my $count;
1501
1502 print ".TH \"$args{'module'}\" 9 \"$args{'module'}\" \"$man_date\" \"API Manual\" LINUX\n";
1503
1504 foreach $section (@{$args{'sectionlist'}}) {
1505 print ".SH \"$section\"\n";
1506 output_highlight($args{'sections'}{$section});
1507 }
1508}
1509
1510##
1511# output in text
1512sub output_function_text(%) {
1513 my %args = %{$_[0]};
1514 my ($parameter, $section);
Randy Dunlapa21217d2007-02-10 01:46:04 -08001515 my $start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Randy Dunlapf47634b2006-07-01 04:36:36 -07001517 print "Name:\n\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001518 print $args{'function'} . " - " . $args{'purpose'} . "\n";
Randy Dunlapf47634b2006-07-01 04:36:36 -07001519
1520 print "\nSynopsis:\n\n";
Randy Dunlapa21217d2007-02-10 01:46:04 -08001521 if ($args{'functiontype'} ne "") {
Randy Dunlapb9d973282009-06-09 08:50:38 -07001522 $start = $args{'functiontype'} . " " . $args{'function'} . " (";
Randy Dunlapa21217d2007-02-10 01:46:04 -08001523 } else {
Randy Dunlapb9d973282009-06-09 08:50:38 -07001524 $start = $args{'function'} . " (";
Randy Dunlapa21217d2007-02-10 01:46:04 -08001525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 print $start;
Randy Dunlapa21217d2007-02-10 01:46:04 -08001527
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 my $count = 0;
1529 foreach my $parameter (@{$args{'parameterlist'}}) {
1530 $type = $args{'parametertypes'}{$parameter};
1531 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1532 # pointer-to-function
Randy Dunlapb9d973282009-06-09 08:50:38 -07001533 print $1 . $parameter . ") (" . $2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 } else {
Randy Dunlapb9d973282009-06-09 08:50:38 -07001535 print $type . " " . $parameter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 }
1537 if ($count != $#{$args{'parameterlist'}}) {
1538 $count++;
1539 print ",\n";
1540 print " " x length($start);
1541 } else {
1542 print ");\n\n";
1543 }
1544 }
1545
1546 print "Arguments:\n\n";
1547 foreach $parameter (@{$args{'parameterlist'}}) {
1548 my $parameter_name = $parameter;
1549 $parameter_name =~ s/\[.*//;
1550
Randy Dunlapb9d973282009-06-09 08:50:38 -07001551 print $parameter . "\n\t" . $args{'parameterdescs'}{$parameter_name} . "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 }
1553 output_section_text(@_);
1554}
1555
1556#output sections in text
1557sub output_section_text(%) {
1558 my %args = %{$_[0]};
1559 my $section;
1560
1561 print "\n";
1562 foreach $section (@{$args{'sectionlist'}}) {
1563 print "$section:\n\n";
1564 output_highlight($args{'sections'}{$section});
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 print "\n\n";
1567}
1568
1569# output enum in text
1570sub output_enum_text(%) {
1571 my %args = %{$_[0]};
1572 my ($parameter);
1573 my $count;
1574 print "Enum:\n\n";
1575
Randy Dunlapb9d973282009-06-09 08:50:38 -07001576 print "enum " . $args{'enum'} . " - " . $args{'purpose'} . "\n\n";
1577 print "enum " . $args{'enum'} . " {\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 $count = 0;
1579 foreach $parameter (@{$args{'parameterlist'}}) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001580 print "\t$parameter";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 if ($count != $#{$args{'parameterlist'}}) {
1582 $count++;
1583 print ",";
1584 }
1585 print "\n";
1586 }
1587 print "};\n\n";
1588
1589 print "Constants:\n\n";
1590 foreach $parameter (@{$args{'parameterlist'}}) {
1591 print "$parameter\n\t";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001592 print $args{'parameterdescs'}{$parameter} . "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 }
1594
1595 output_section_text(@_);
1596}
1597
1598# output typedef in text
1599sub output_typedef_text(%) {
1600 my %args = %{$_[0]};
1601 my ($parameter);
1602 my $count;
1603 print "Typedef:\n\n";
1604
Randy Dunlapb9d973282009-06-09 08:50:38 -07001605 print "typedef " . $args{'typedef'} . " - " . $args{'purpose'} . "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 output_section_text(@_);
1607}
1608
1609# output struct as text
1610sub output_struct_text(%) {
1611 my %args = %{$_[0]};
1612 my ($parameter);
1613
Randy Dunlapb9d973282009-06-09 08:50:38 -07001614 print $args{'type'} . " " . $args{'struct'} . " - " . $args{'purpose'} . "\n\n";
1615 print $args{'type'} . " " . $args{'struct'} . " {\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 foreach $parameter (@{$args{'parameterlist'}}) {
1617 if ($parameter =~ /^#/) {
1618 print "$parameter\n";
1619 next;
1620 }
1621
1622 my $parameter_name = $parameter;
1623 $parameter_name =~ s/\[.*//;
1624
Randy Dunlap3c308792007-05-08 00:24:39 -07001625 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 $type = $args{'parametertypes'}{$parameter};
1627 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1628 # pointer-to-function
1629 print "\t$1 $parameter) ($2);\n";
1630 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
Randy Dunlap51f5a0c2007-07-19 01:48:24 -07001631 # bitfield
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 print "\t$1 $parameter$2;\n";
1633 } else {
Randy Dunlapb9d973282009-06-09 08:50:38 -07001634 print "\t" . $type . " " . $parameter . ";\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 }
1636 }
1637 print "};\n\n";
1638
1639 print "Members:\n\n";
1640 foreach $parameter (@{$args{'parameterlist'}}) {
1641 ($parameter =~ /^#/) && next;
1642
1643 my $parameter_name = $parameter;
1644 $parameter_name =~ s/\[.*//;
1645
Randy Dunlap3c308792007-05-08 00:24:39 -07001646 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 print "$parameter\n\t";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001648 print $args{'parameterdescs'}{$parameter_name} . "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 }
1650 print "\n";
1651 output_section_text(@_);
1652}
1653
Johannes Bergb112e0f2007-10-24 15:08:48 -07001654sub output_blockhead_text(%) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 my %args = %{$_[0]};
1656 my ($parameter, $section);
1657
1658 foreach $section (@{$args{'sectionlist'}}) {
1659 print " $section:\n";
1660 print " -> ";
1661 output_highlight($args{'sections'}{$section});
1662 }
1663}
1664
Johannes Bergeda603f2010-09-11 15:55:22 -07001665## list mode output functions
1666
1667sub output_function_list(%) {
1668 my %args = %{$_[0]};
1669
1670 print $args{'function'} . "\n";
1671}
1672
1673# output enum in list
1674sub output_enum_list(%) {
1675 my %args = %{$_[0]};
1676 print $args{'enum'} . "\n";
1677}
1678
1679# output typedef in list
1680sub output_typedef_list(%) {
1681 my %args = %{$_[0]};
1682 print $args{'typedef'} . "\n";
1683}
1684
1685# output struct as list
1686sub output_struct_list(%) {
1687 my %args = %{$_[0]};
1688
1689 print $args{'struct'} . "\n";
1690}
1691
1692sub output_blockhead_list(%) {
1693 my %args = %{$_[0]};
1694 my ($parameter, $section);
1695
1696 foreach $section (@{$args{'sectionlist'}}) {
1697 print "DOC: $section\n";
1698 }
1699}
1700
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701##
Randy Dunlap27205742006-10-11 01:22:12 -07001702# generic output function for all types (function, struct/union, typedef, enum);
1703# calls the generated, variable output_ function name based on
1704# functype and output_mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705sub output_declaration {
1706 no strict 'refs';
1707 my $name = shift;
1708 my $functype = shift;
1709 my $func = "output_${functype}_$output_mode";
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001710 if (($function_only==0) ||
1711 ( $function_only == 1 && defined($function_table{$name})) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 ( $function_only == 2 && !defined($function_table{$name})))
1713 {
Randy Dunlap3c308792007-05-08 00:24:39 -07001714 &$func(@_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 $section_counter++;
1716 }
1717}
1718
1719##
Randy Dunlap27205742006-10-11 01:22:12 -07001720# generic output function - calls the right one based on current output mode.
Johannes Bergb112e0f2007-10-24 15:08:48 -07001721sub output_blockhead {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 no strict 'refs';
Randy Dunlapb9d973282009-06-09 08:50:38 -07001723 my $func = "output_blockhead_" . $output_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 &$func(@_);
1725 $section_counter++;
1726}
1727
1728##
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001729# takes a declaration (struct, union, enum, typedef) and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730# invokes the right handler. NOT called for functions.
1731sub dump_declaration($$) {
1732 no strict 'refs';
1733 my ($prototype, $file) = @_;
Randy Dunlapb9d973282009-06-09 08:50:38 -07001734 my $func = "dump_" . $decl_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 &$func(@_);
1736}
1737
1738sub dump_union($$) {
1739 dump_struct(@_);
1740}
1741
1742sub dump_struct($$) {
1743 my $x = shift;
1744 my $file = shift;
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001745 my $nested;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Randy Dunlap52dc5ae2009-04-30 15:08:53 -07001747 if ($x =~ /(struct|union)\s+(\w+)\s*{(.*)}/) {
1748 #my $decl_type = $1;
Randy Dunlap3c308792007-05-08 00:24:39 -07001749 $declaration_name = $2;
1750 my $members = $3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
1752 # ignore embedded structs or unions
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001753 $members =~ s/({.*})//g;
1754 $nested = $1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
Martin Waitzaeec46b2005-11-13 16:08:13 -08001756 # ignore members marked private:
Randy Dunlap52dc5ae2009-04-30 15:08:53 -07001757 $members =~ s/\/\*\s*private:.*?\/\*\s*public:.*?\*\///gos;
1758 $members =~ s/\/\*\s*private:.*//gos;
Martin Waitzaeec46b2005-11-13 16:08:13 -08001759 # strip comments:
1760 $members =~ s/\/\*.*?\*\///gos;
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001761 $nested =~ s/\/\*.*?\*\///gos;
Randy Dunlapd960eea2009-06-29 14:54:11 -07001762 # strip kmemcheck_bitfield_{begin,end}.*;
1763 $members =~ s/kmemcheck_bitfield_.*?;//gos;
Randy Dunlapef5da592010-03-23 13:35:14 -07001764 # strip attributes
Johannes Berg7b990782014-12-10 15:41:28 -08001765 $members =~ s/__aligned\s*\([^;]*\)//gos;
Martin Waitzaeec46b2005-11-13 16:08:13 -08001766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 create_parameterlist($members, ';', $file);
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001768 check_sections($file, $declaration_name, "struct", $sectcheck, $struct_actual, $nested);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
1770 output_declaration($declaration_name,
1771 'struct',
1772 {'struct' => $declaration_name,
1773 'module' => $modulename,
1774 'parameterlist' => \@parameterlist,
1775 'parameterdescs' => \%parameterdescs,
1776 'parametertypes' => \%parametertypes,
1777 'sectionlist' => \@sectionlist,
1778 'sections' => \%sections,
1779 'purpose' => $declaration_purpose,
1780 'type' => $decl_type
1781 });
1782 }
1783 else {
Randy Dunlap3c308792007-05-08 00:24:39 -07001784 print STDERR "Error(${file}:$.): Cannot parse struct or union!\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 ++$errors;
1786 }
1787}
1788
1789sub dump_enum($$) {
1790 my $x = shift;
1791 my $file = shift;
1792
Martin Waitzaeec46b2005-11-13 16:08:13 -08001793 $x =~ s@/\*.*?\*/@@gos; # strip comments.
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001794 $x =~ s/^#\s*define\s+.*$//; # strip #define macros inside enums
1795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 if ($x =~ /enum\s+(\w+)\s*{(.*)}/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001797 $declaration_name = $1;
1798 my $members = $2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
1800 foreach my $arg (split ',', $members) {
1801 $arg =~ s/^\s*(\w+).*/$1/;
1802 push @parameterlist, $arg;
1803 if (!$parameterdescs{$arg}) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001804 $parameterdescs{$arg} = $undescribed;
1805 print STDERR "Warning(${file}:$.): Enum value '$arg' ".
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 "not described in enum '$declaration_name'\n";
1807 }
1808
1809 }
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 output_declaration($declaration_name,
1812 'enum',
1813 {'enum' => $declaration_name,
1814 'module' => $modulename,
1815 'parameterlist' => \@parameterlist,
1816 'parameterdescs' => \%parameterdescs,
1817 'sectionlist' => \@sectionlist,
1818 'sections' => \%sections,
1819 'purpose' => $declaration_purpose
1820 });
1821 }
1822 else {
Randy Dunlap3c308792007-05-08 00:24:39 -07001823 print STDERR "Error(${file}:$.): Cannot parse enum!\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 ++$errors;
1825 }
1826}
1827
1828sub dump_typedef($$) {
1829 my $x = shift;
1830 my $file = shift;
1831
Martin Waitzaeec46b2005-11-13 16:08:13 -08001832 $x =~ s@/\*.*?\*/@@gos; # strip comments.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 while (($x =~ /\(*.\)\s*;$/) || ($x =~ /\[*.\]\s*;$/)) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001834 $x =~ s/\(*.\)\s*;$/;/;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 $x =~ s/\[*.\]\s*;$/;/;
1836 }
1837
1838 if ($x =~ /typedef.*\s+(\w+)\s*;/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001839 $declaration_name = $1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
1841 output_declaration($declaration_name,
1842 'typedef',
1843 {'typedef' => $declaration_name,
1844 'module' => $modulename,
1845 'sectionlist' => \@sectionlist,
1846 'sections' => \%sections,
1847 'purpose' => $declaration_purpose
1848 });
1849 }
1850 else {
Randy Dunlap3c308792007-05-08 00:24:39 -07001851 print STDERR "Error(${file}:$.): Cannot parse typedef!\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 ++$errors;
1853 }
1854}
1855
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001856sub save_struct_actual($) {
1857 my $actual = shift;
1858
1859 # strip all spaces from the actual param so that it looks like one string item
1860 $actual =~ s/\s*//g;
1861 $struct_actual = $struct_actual . $actual . " ";
1862}
1863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864sub create_parameterlist($$$) {
1865 my $args = shift;
1866 my $splitter = shift;
1867 my $file = shift;
1868 my $type;
1869 my $param;
1870
Martin Waitza6d3fe72006-01-09 20:53:55 -08001871 # temporarily replace commas inside function pointer definition
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 while ($args =~ /(\([^\),]+),/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001873 $args =~ s/(\([^\),]+),/$1#/g;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 }
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001875
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 foreach my $arg (split($splitter, $args)) {
1877 # strip comments
1878 $arg =~ s/\/\*.*\*\///;
Randy Dunlap3c308792007-05-08 00:24:39 -07001879 # strip leading/trailing spaces
1880 $arg =~ s/^\s*//;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 $arg =~ s/\s*$//;
1882 $arg =~ s/\s+/ /;
1883
1884 if ($arg =~ /^#/) {
1885 # Treat preprocessor directive as a typeless variable just to fill
1886 # corresponding data structures "correctly". Catch it later in
1887 # output_* subs.
1888 push_parameter($arg, "", $file);
Richard Kennedy00d62962008-02-23 15:24:01 -08001889 } elsif ($arg =~ m/\(.+\)\s*\(/) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 # pointer-to-function
1891 $arg =~ tr/#/,/;
Richard Kennedy00d62962008-02-23 15:24:01 -08001892 $arg =~ m/[^\(]+\(\*?\s*(\w*)\s*\)/;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 $param = $1;
1894 $type = $arg;
Richard Kennedy00d62962008-02-23 15:24:01 -08001895 $type =~ s/([^\(]+\(\*?)\s*$param/$1/;
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001896 save_struct_actual($param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 push_parameter($param, $type, $file);
Martin Waitzaeec46b2005-11-13 16:08:13 -08001898 } elsif ($arg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 $arg =~ s/\s*:\s*/:/g;
1900 $arg =~ s/\s*\[/\[/g;
1901
1902 my @args = split('\s*,\s*', $arg);
1903 if ($args[0] =~ m/\*/) {
1904 $args[0] =~ s/(\*+)\s*/ $1/;
1905 }
Borislav Petkov884f2812007-05-08 00:29:05 -07001906
1907 my @first_arg;
1908 if ($args[0] =~ /^(.*\s+)(.*?\[.*\].*)$/) {
1909 shift @args;
1910 push(@first_arg, split('\s+', $1));
1911 push(@first_arg, $2);
1912 } else {
1913 @first_arg = split('\s+', shift @args);
1914 }
1915
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 unshift(@args, pop @first_arg);
1917 $type = join " ", @first_arg;
1918
1919 foreach $param (@args) {
1920 if ($param =~ m/^(\*+)\s*(.*)/) {
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001921 save_struct_actual($2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 push_parameter($2, "$type $1", $file);
1923 }
1924 elsif ($param =~ m/(.*?):(\d+)/) {
Randy Dunlap7b978872008-05-16 15:45:52 -07001925 if ($type ne "") { # skip unnamed bit-fields
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001926 save_struct_actual($1);
Randy Dunlap7b978872008-05-16 15:45:52 -07001927 push_parameter($1, "$type:$2", $file)
1928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 }
1930 else {
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001931 save_struct_actual($param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 push_parameter($param, $type, $file);
1933 }
1934 }
1935 }
1936 }
1937}
1938
1939sub push_parameter($$$) {
1940 my $param = shift;
1941 my $type = shift;
1942 my $file = shift;
1943
Randy Dunlap5f8c7c92007-07-19 01:48:24 -07001944 if (($anon_struct_union == 1) && ($type eq "") &&
1945 ($param eq "}")) {
1946 return; # ignore the ending }; from anon. struct/union
1947 }
1948
1949 $anon_struct_union = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 my $param_name = $param;
1951 $param_name =~ s/\[.*//;
1952
Martin Waitza6d3fe72006-01-09 20:53:55 -08001953 if ($type eq "" && $param =~ /\.\.\.$/)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 {
Randy Dunlapced69092008-12-01 13:14:03 -08001955 if (!defined $parameterdescs{$param} || $parameterdescs{$param} eq "") {
1956 $parameterdescs{$param} = "variable arguments";
1957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 }
1959 elsif ($type eq "" && ($param eq "" or $param eq "void"))
1960 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 $param="void";
1962 $parameterdescs{void} = "no arguments";
1963 }
Randy Dunlap134fe012006-12-22 01:10:50 -08001964 elsif ($type eq "" && ($param eq "struct" or $param eq "union"))
1965 # handle unnamed (anonymous) union or struct:
1966 {
1967 $type = $param;
Randy Dunlap5f8c7c92007-07-19 01:48:24 -07001968 $param = "{unnamed_" . $param . "}";
Randy Dunlap134fe012006-12-22 01:10:50 -08001969 $parameterdescs{$param} = "anonymous\n";
Randy Dunlap5f8c7c92007-07-19 01:48:24 -07001970 $anon_struct_union = 1;
Randy Dunlap134fe012006-12-22 01:10:50 -08001971 }
1972
Martin Waitza6d3fe72006-01-09 20:53:55 -08001973 # warn if parameter has no description
Randy Dunlap134fe012006-12-22 01:10:50 -08001974 # (but ignore ones starting with # as these are not parameters
1975 # but inline preprocessor statements);
1976 # also ignore unnamed structs/unions;
Randy Dunlap5f8c7c92007-07-19 01:48:24 -07001977 if (!$anon_struct_union) {
Martin Waitza6d3fe72006-01-09 20:53:55 -08001978 if (!defined $parameterdescs{$param_name} && $param_name !~ /^#/) {
1979
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 $parameterdescs{$param_name} = $undescribed;
1981
1982 if (($type eq 'function') || ($type eq 'enum')) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001983 print STDERR "Warning(${file}:$.): Function parameter ".
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 "or member '$param' not " .
1985 "described in '$declaration_name'\n";
1986 }
Randy Dunlapb9d973282009-06-09 08:50:38 -07001987 print STDERR "Warning(${file}:$.):" .
Randy Dunlap3c308792007-05-08 00:24:39 -07001988 " No description found for parameter '$param'\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 ++$warnings;
Randy Dunlap3c308792007-05-08 00:24:39 -07001990 }
1991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Randy Dunlap2b35f4d2010-11-18 12:27:31 -08001993 $param = xml_escape($param);
1994
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001995 # strip spaces from $param so that it is one continuous string
Randy Dunlape34e7db2009-06-17 17:37:47 -07001996 # on @parameterlist;
1997 # this fixes a problem where check_sections() cannot find
1998 # a parameter like "addr[6 + 2]" because it actually appears
1999 # as "addr[6", "+", "2]" on the parameter list;
2000 # but it's better to maintain the param string unchanged for output,
2001 # so just weaken the string compare in check_sections() to ignore
2002 # "[blah" in a parameter string;
2003 ###$param =~ s/\s*//g;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 push @parameterlist, $param;
2005 $parametertypes{$param} = $type;
2006}
2007
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08002008sub check_sections($$$$$$) {
2009 my ($file, $decl_name, $decl_type, $sectcheck, $prmscheck, $nested) = @_;
2010 my @sects = split ' ', $sectcheck;
2011 my @prms = split ' ', $prmscheck;
2012 my $err;
2013 my ($px, $sx);
2014 my $prm_clean; # strip trailing "[array size]" and/or beginning "*"
2015
2016 foreach $sx (0 .. $#sects) {
2017 $err = 1;
2018 foreach $px (0 .. $#prms) {
2019 $prm_clean = $prms[$px];
2020 $prm_clean =~ s/\[.*\]//;
Johannes Berg1f3a6682010-09-11 15:55:12 -07002021 $prm_clean =~ s/__attribute__\s*\(\([a-z,_\*\s\(\)]*\)\)//i;
Randy Dunlape34e7db2009-06-17 17:37:47 -07002022 # ignore array size in a parameter string;
2023 # however, the original param string may contain
2024 # spaces, e.g.: addr[6 + 2]
2025 # and this appears in @prms as "addr[6" since the
2026 # parameter list is split at spaces;
2027 # hence just ignore "[..." for the sections check;
2028 $prm_clean =~ s/\[.*//;
2029
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08002030 ##$prm_clean =~ s/^\**//;
2031 if ($prm_clean eq $sects[$sx]) {
2032 $err = 0;
2033 last;
2034 }
2035 }
2036 if ($err) {
2037 if ($decl_type eq "function") {
2038 print STDERR "Warning(${file}:$.): " .
2039 "Excess function parameter " .
2040 "'$sects[$sx]' " .
2041 "description in '$decl_name'\n";
2042 ++$warnings;
2043 } else {
2044 if ($nested !~ m/\Q$sects[$sx]\E/) {
2045 print STDERR "Warning(${file}:$.): " .
2046 "Excess struct/union/enum/typedef member " .
2047 "'$sects[$sx]' " .
2048 "description in '$decl_name'\n";
2049 ++$warnings;
2050 }
2051 }
2052 }
2053 }
2054}
2055
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056##
Yacine Belkadi4092bac2012-11-26 22:22:27 +01002057# Checks the section describing the return value of a function.
2058sub check_return_section {
2059 my $file = shift;
2060 my $declaration_name = shift;
2061 my $return_type = shift;
2062
2063 # Ignore an empty return type (It's a macro)
2064 # Ignore functions with a "void" return type. (But don't ignore "void *")
2065 if (($return_type eq "") || ($return_type =~ /void\s*\w*\s*$/)) {
2066 return;
2067 }
2068
2069 if (!defined($sections{$section_return}) ||
2070 $sections{$section_return} eq "") {
2071 print STDERR "Warning(${file}:$.): " .
2072 "No description found for return value of " .
2073 "'$declaration_name'\n";
2074 ++$warnings;
2075 }
2076}
2077
2078##
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079# takes a function prototype and the name of the current file being
2080# processed and spits out all the details stored in the global
2081# arrays/hashes.
2082sub dump_function($$) {
2083 my $prototype = shift;
2084 my $file = shift;
Horia Geantacbb4d3e2014-07-12 09:55:03 -07002085 my $noret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
2087 $prototype =~ s/^static +//;
2088 $prototype =~ s/^extern +//;
Pavel Pisa4dc3b162005-05-01 08:59:25 -07002089 $prototype =~ s/^asmlinkage +//;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 $prototype =~ s/^inline +//;
2091 $prototype =~ s/^__inline__ +//;
Randy Dunlap32e79402006-10-11 01:22:10 -07002092 $prototype =~ s/^__inline +//;
2093 $prototype =~ s/^__always_inline +//;
2094 $prototype =~ s/^noinline +//;
Randy Dunlap74fc5c62008-06-19 16:03:29 -07002095 $prototype =~ s/__init +//;
Randy Dunlap20072202010-03-23 13:35:24 -07002096 $prototype =~ s/__init_or_module +//;
Randy Dunlap270a0092014-08-24 18:17:17 -07002097 $prototype =~ s/__meminit +//;
Randy Dunlap70c95b02012-01-21 10:31:54 -08002098 $prototype =~ s/__must_check +//;
Randy Dunlap0df7c0e2012-08-16 16:23:20 -07002099 $prototype =~ s/__weak +//;
Horia Geantacbb4d3e2014-07-12 09:55:03 -07002100 my $define = $prototype =~ s/^#\s*define\s+//; #ak added
Randy Dunlap328d2442007-02-28 20:12:10 -08002101 $prototype =~ s/__attribute__\s*\(\([a-z,]*\)\)//;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
2103 # Yes, this truly is vile. We are looking for:
2104 # 1. Return type (may be nothing if we're looking at a macro)
2105 # 2. Function name
2106 # 3. Function parameters.
2107 #
2108 # All the while we have to watch out for function pointer parameters
2109 # (which IIRC is what the two sections are for), C types (these
2110 # regexps don't even start to express all the possibilities), and
2111 # so on.
2112 #
2113 # If you mess with these regexps, it's a good idea to check that
2114 # the following functions' documentation still comes out right:
2115 # - parport_register_device (function pointer parameters)
2116 # - atomic_set (macro)
Martin Waitz9598f912006-02-01 03:06:55 -08002117 # - pci_match_device, __copy_to_user (long return type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
Horia Geantacbb4d3e2014-07-12 09:55:03 -07002119 if ($define && $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s+/) {
2120 # This is an object-like macro, it has no return type and no parameter
2121 # list.
2122 # Function-like macros are not allowed to have spaces between
2123 # declaration_name and opening parenthesis (notice the \s+).
2124 $return_type = $1;
2125 $declaration_name = $2;
2126 $noret = 1;
2127 } elsif ($prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
2129 $prototype =~ m/^(\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
2130 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
Randy Dunlap94b3e032008-02-07 00:13:26 -08002131 $prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
2133 $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
2134 $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
2135 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
2136 $prototype =~ m/^(\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
2137 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
2138 $prototype =~ m/^(\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
2139 $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
Martin Waitz9598f912006-02-01 03:06:55 -08002140 $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
2141 $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
Randy Dunlap412ecd772007-02-10 22:47:33 -08002142 $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
2143 $prototype =~ m/^(\w+\s+\w+\s*\*\s*\w+\s*\*\s*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 $return_type = $1;
2145 $declaration_name = $2;
2146 my $args = $3;
2147
2148 create_parameterlist($args, ',', $file);
2149 } else {
Randy Dunlap9645ae82013-10-17 16:32:01 -07002150 print STDERR "Warning(${file}:$.): cannot understand function prototype: '$prototype'\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 return;
2152 }
2153
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08002154 my $prms = join " ", @parameterlist;
2155 check_sections($file, $declaration_name, "function", $sectcheck, $prms, "");
2156
Yacine Belkadi4092bac2012-11-26 22:22:27 +01002157 # This check emits a lot of warnings at the moment, because many
2158 # functions don't have a 'Return' doc section. So until the number
2159 # of warnings goes sufficiently down, the check is only performed in
2160 # verbose mode.
2161 # TODO: always perform the check.
Horia Geantacbb4d3e2014-07-12 09:55:03 -07002162 if ($verbose && !$noret) {
Yacine Belkadi4092bac2012-11-26 22:22:27 +01002163 check_return_section($file, $declaration_name, $return_type);
2164 }
2165
Randy Dunlap3c3b8092006-02-01 03:06:58 -08002166 output_declaration($declaration_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 'function',
2168 {'function' => $declaration_name,
2169 'module' => $modulename,
2170 'functiontype' => $return_type,
2171 'parameterlist' => \@parameterlist,
2172 'parameterdescs' => \%parameterdescs,
2173 'parametertypes' => \%parametertypes,
2174 'sectionlist' => \@sectionlist,
2175 'sections' => \%sections,
2176 'purpose' => $declaration_purpose
2177 });
2178}
2179
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180sub reset_state {
2181 $function = "";
2182 %constants = ();
2183 %parameterdescs = ();
2184 %parametertypes = ();
2185 @parameterlist = ();
2186 %sections = ();
2187 @sectionlist = ();
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08002188 $sectcheck = "";
2189 $struct_actual = "";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 $prototype = "";
Randy Dunlap3c3b8092006-02-01 03:06:58 -08002191
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 $state = 0;
2193}
2194
Jason Baron56afb0f2009-04-30 13:29:36 -04002195sub tracepoint_munge($) {
2196 my $file = shift;
2197 my $tracepointname = 0;
2198 my $tracepointargs = 0;
2199
Jason Baron3a9089f2009-12-01 12:18:49 -05002200 if ($prototype =~ m/TRACE_EVENT\((.*?),/) {
Jason Baron56afb0f2009-04-30 13:29:36 -04002201 $tracepointname = $1;
2202 }
Jason Baron3a9089f2009-12-01 12:18:49 -05002203 if ($prototype =~ m/DEFINE_SINGLE_EVENT\((.*?),/) {
2204 $tracepointname = $1;
2205 }
2206 if ($prototype =~ m/DEFINE_EVENT\((.*?),(.*?),/) {
2207 $tracepointname = $2;
2208 }
2209 $tracepointname =~ s/^\s+//; #strip leading whitespace
2210 if ($prototype =~ m/TP_PROTO\((.*?)\)/) {
Jason Baron56afb0f2009-04-30 13:29:36 -04002211 $tracepointargs = $1;
2212 }
2213 if (($tracepointname eq 0) || ($tracepointargs eq 0)) {
2214 print STDERR "Warning(${file}:$.): Unrecognized tracepoint format: \n".
2215 "$prototype\n";
2216 } else {
2217 $prototype = "static inline void trace_$tracepointname($tracepointargs)";
2218 }
2219}
2220
Randy Dunlapb4870bc2009-02-11 13:04:33 -08002221sub syscall_munge() {
2222 my $void = 0;
2223
2224 $prototype =~ s@[\r\n\t]+@ @gos; # strip newlines/CR's/tabs
2225## if ($prototype =~ m/SYSCALL_DEFINE0\s*\(\s*(a-zA-Z0-9_)*\s*\)/) {
2226 if ($prototype =~ m/SYSCALL_DEFINE0/) {
2227 $void = 1;
2228## $prototype = "long sys_$1(void)";
2229 }
2230
2231 $prototype =~ s/SYSCALL_DEFINE.*\(/long sys_/; # fix return type & func name
2232 if ($prototype =~ m/long (sys_.*?),/) {
2233 $prototype =~ s/,/\(/;
2234 } elsif ($void) {
2235 $prototype =~ s/\)/\(void\)/;
2236 }
2237
2238 # now delete all of the odd-number commas in $prototype
2239 # so that arg types & arg names don't have a comma between them
2240 my $count = 0;
2241 my $len = length($prototype);
2242 if ($void) {
2243 $len = 0; # skip the for-loop
2244 }
2245 for (my $ix = 0; $ix < $len; $ix++) {
2246 if (substr($prototype, $ix, 1) eq ',') {
2247 $count++;
2248 if ($count % 2 == 1) {
2249 substr($prototype, $ix, 1) = ' ';
2250 }
2251 }
2252 }
2253}
2254
Randy Dunlap3c3b8092006-02-01 03:06:58 -08002255sub process_state3_function($$) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 my $x = shift;
2257 my $file = shift;
2258
Randy Dunlap51f5a0c2007-07-19 01:48:24 -07002259 $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
2260
Randy Dunlap890c78c2008-10-25 17:06:43 -07002261 if ($x =~ m#\s*/\*\s+MACDOC\s*#io || ($x =~ /^#/ && $x !~ /^#\s*define/)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 # do nothing
2263 }
2264 elsif ($x =~ /([^\{]*)/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07002265 $prototype .= $1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 }
Randy Dunlapb4870bc2009-02-11 13:04:33 -08002267
Randy Dunlap890c78c2008-10-25 17:06:43 -07002268 if (($x =~ /\{/) || ($x =~ /\#\s*define/) || ($x =~ /;/)) {
Randy Dunlap3c308792007-05-08 00:24:39 -07002269 $prototype =~ s@/\*.*?\*/@@gos; # strip comments.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 $prototype =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
2271 $prototype =~ s@^\s+@@gos; # strip leading spaces
Randy Dunlapb4870bc2009-02-11 13:04:33 -08002272 if ($prototype =~ /SYSCALL_DEFINE/) {
2273 syscall_munge();
2274 }
Jason Baron3a9089f2009-12-01 12:18:49 -05002275 if ($prototype =~ /TRACE_EVENT/ || $prototype =~ /DEFINE_EVENT/ ||
2276 $prototype =~ /DEFINE_SINGLE_EVENT/)
2277 {
Jason Baron56afb0f2009-04-30 13:29:36 -04002278 tracepoint_munge($file);
2279 }
Randy Dunlapb4870bc2009-02-11 13:04:33 -08002280 dump_function($prototype, $file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 reset_state();
2282 }
2283}
2284
Randy Dunlap3c3b8092006-02-01 03:06:58 -08002285sub process_state3_type($$) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 my $x = shift;
2287 my $file = shift;
2288
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 $x =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
2290 $x =~ s@^\s+@@gos; # strip leading spaces
2291 $x =~ s@\s+$@@gos; # strip trailing spaces
Randy Dunlap51f5a0c2007-07-19 01:48:24 -07002292 $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
2293
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 if ($x =~ /^#/) {
2295 # To distinguish preprocessor directive from regular declaration later.
2296 $x .= ";";
2297 }
2298
2299 while (1) {
Randy Dunlap3c308792007-05-08 00:24:39 -07002300 if ( $x =~ /([^{};]*)([{};])(.*)/ ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 $prototype .= $1 . $2;
2302 ($2 eq '{') && $brcount++;
2303 ($2 eq '}') && $brcount--;
2304 if (($2 eq ';') && ($brcount == 0)) {
Randy Dunlapb9d973282009-06-09 08:50:38 -07002305 dump_declaration($prototype, $file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 reset_state();
Randy Dunlap3c308792007-05-08 00:24:39 -07002307 last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 }
2309 $x = $3;
Randy Dunlap3c308792007-05-08 00:24:39 -07002310 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 $prototype .= $x;
2312 last;
2313 }
2314 }
2315}
2316
Randy Dunlap6b5b55f2007-10-16 23:31:20 -07002317# xml_escape: replace <, >, and & in the text stream;
2318#
2319# however, formatting controls that are generated internally/locally in the
2320# kernel-doc script are not escaped here; instead, they begin life like
2321# $blankline_html (4 of '\' followed by a mnemonic + ':'), then these strings
2322# are converted to their mnemonic-expected output, without the 4 * '\' & ':',
2323# just before actual output; (this is done by local_unescape())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324sub xml_escape($) {
2325 my $text = shift;
Randy Dunlapecfb2512006-06-25 05:49:13 -07002326 if (($output_mode eq "text") || ($output_mode eq "man")) {
2327 return $text;
2328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 $text =~ s/\&/\\\\\\amp;/g;
2330 $text =~ s/\</\\\\\\lt;/g;
2331 $text =~ s/\>/\\\\\\gt;/g;
2332 return $text;
2333}
2334
Randy Dunlap6b5b55f2007-10-16 23:31:20 -07002335# convert local escape strings to html
2336# local escape strings look like: '\\\\menmonic:' (that's 4 backslashes)
2337sub local_unescape($) {
2338 my $text = shift;
2339 if (($output_mode eq "text") || ($output_mode eq "man")) {
2340 return $text;
2341 }
2342 $text =~ s/\\\\\\\\lt:/</g;
2343 $text =~ s/\\\\\\\\gt:/>/g;
2344 return $text;
2345}
2346
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347sub process_file($) {
Randy Dunlap2283a112005-07-07 15:39:26 -07002348 my $file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 my $identifier;
2350 my $func;
Randy Dunlapa21217d2007-02-10 01:46:04 -08002351 my $descr;
Johannes Weiner64231332009-09-17 19:26:53 -07002352 my $in_purpose = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 my $initial_section_counter = $section_counter;
2354
Randy Dunlap2283a112005-07-07 15:39:26 -07002355 if (defined($ENV{'SRCTREE'})) {
2356 $file = "$ENV{'SRCTREE'}" . "/" . "@_";
2357 }
2358 else {
2359 $file = "@_";
2360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 if (defined($source_map{$file})) {
2362 $file = $source_map{$file};
2363 }
2364
2365 if (!open(IN,"<$file")) {
2366 print STDERR "Error: Cannot open file $file\n";
2367 ++$errors;
2368 return;
2369 }
2370
Ilya Dryomova9e73142010-02-26 13:05:47 -08002371 $. = 1;
2372
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 $section_counter = 0;
2374 while (<IN>) {
Daniel Santos65478422012-10-04 17:15:05 -07002375 while (s/\\\s*$//) {
2376 $_ .= <IN>;
2377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 if ($state == 0) {
2379 if (/$doc_start/o) {
2380 $state = 1; # next line is always the function name
Randy Dunlap850622d2006-06-25 05:48:55 -07002381 $in_doc_sect = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 }
2383 } elsif ($state == 1) { # this line is the function name (always)
2384 if (/$doc_block/o) {
2385 $state = 4;
2386 $contents = "";
2387 if ( $1 eq "" ) {
2388 $section = $section_intro;
2389 } else {
2390 $section = $1;
2391 }
Randy Dunlap3c308792007-05-08 00:24:39 -07002392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 elsif (/$doc_decl/o) {
2394 $identifier = $1;
2395 if (/\s*([\w\s]+?)\s*-/) {
2396 $identifier = $1;
2397 }
2398
2399 $state = 2;
2400 if (/-(.*)/) {
Randy Dunlap51f5a0c2007-07-19 01:48:24 -07002401 # strip leading/trailing/multiple spaces
Randy Dunlapa21217d2007-02-10 01:46:04 -08002402 $descr= $1;
2403 $descr =~ s/^\s*//;
2404 $descr =~ s/\s*$//;
Daniel Santos12ae6772012-10-04 17:15:10 -07002405 $descr =~ s/\s+/ /g;
Randy Dunlapa21217d2007-02-10 01:46:04 -08002406 $declaration_purpose = xml_escape($descr);
Johannes Weiner64231332009-09-17 19:26:53 -07002407 $in_purpose = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 } else {
2409 $declaration_purpose = "";
2410 }
Randy Dunlap77cc23b2008-02-07 00:13:42 -08002411
2412 if (($declaration_purpose eq "") && $verbose) {
2413 print STDERR "Warning(${file}:$.): missing initial short description on line:\n";
2414 print STDERR $_;
2415 ++$warnings;
2416 }
2417
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 if ($identifier =~ m/^struct/) {
2419 $decl_type = 'struct';
2420 } elsif ($identifier =~ m/^union/) {
2421 $decl_type = 'union';
2422 } elsif ($identifier =~ m/^enum/) {
2423 $decl_type = 'enum';
2424 } elsif ($identifier =~ m/^typedef/) {
2425 $decl_type = 'typedef';
2426 } else {
2427 $decl_type = 'function';
2428 }
2429
2430 if ($verbose) {
2431 print STDERR "Info(${file}:$.): Scanning doc for $identifier\n";
2432 }
2433 } else {
2434 print STDERR "Warning(${file}:$.): Cannot understand $_ on line $.",
2435 " - I thought it was a doc line\n";
2436 ++$warnings;
2437 $state = 0;
2438 }
2439 } elsif ($state == 2) { # look for head: lines, and include content
2440 if (/$doc_sect/o) {
2441 $newsection = $1;
2442 $newcontents = $2;
2443
Randy Dunlap792aa2f2008-02-07 00:13:41 -08002444 if (($contents ne "") && ($contents ne "\n")) {
Randy Dunlap850622d2006-06-25 05:48:55 -07002445 if (!$in_doc_sect && $verbose) {
2446 print STDERR "Warning(${file}:$.): contents before sections\n";
2447 ++$warnings;
2448 }
Randy Dunlap94dc7ad2008-04-28 02:16:34 -07002449 dump_section($file, $section, xml_escape($contents));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 $section = $section_default;
2451 }
2452
Randy Dunlap850622d2006-06-25 05:48:55 -07002453 $in_doc_sect = 1;
Johannes Weiner64231332009-09-17 19:26:53 -07002454 $in_purpose = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 $contents = $newcontents;
2456 if ($contents ne "") {
Randy Dunlap27205742006-10-11 01:22:12 -07002457 while ((substr($contents, 0, 1) eq " ") ||
2458 substr($contents, 0, 1) eq "\t") {
2459 $contents = substr($contents, 1);
Randy Dunlap05189492006-06-25 05:47:47 -07002460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 $contents .= "\n";
2462 }
2463 $section = $newsection;
2464 } elsif (/$doc_end/) {
2465
Randy Dunlap4c98eca2010-03-10 15:22:02 -08002466 if (($contents ne "") && ($contents ne "\n")) {
Randy Dunlap94dc7ad2008-04-28 02:16:34 -07002467 dump_section($file, $section, xml_escape($contents));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 $section = $section_default;
2469 $contents = "";
2470 }
Randy Dunlap46b958e2008-04-28 02:16:35 -07002471 # look for doc_com + <text> + doc_end:
2472 if ($_ =~ m'\s*\*\s*[a-zA-Z_0-9:\.]+\*/') {
2473 print STDERR "Warning(${file}:$.): suspicious ending line: $_";
2474 ++$warnings;
2475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
2477 $prototype = "";
2478 $state = 3;
2479 $brcount = 0;
Randy Dunlap232acbc2006-06-25 05:47:48 -07002480# print STDERR "end of doc comment, looking for prototype\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 } elsif (/$doc_content/) {
2482 # miguel-style comment kludge, look for blank lines after
2483 # @parameter line to signify start of description
Johannes Weiner64231332009-09-17 19:26:53 -07002484 if ($1 eq "") {
2485 if ($section =~ m/^@/ || $section eq $section_context) {
2486 dump_section($file, $section, xml_escape($contents));
2487 $section = $section_default;
2488 $contents = "";
2489 } else {
2490 $contents .= "\n";
2491 }
2492 $in_purpose = 0;
2493 } elsif ($in_purpose == 1) {
2494 # Continued declaration purpose
2495 chomp($declaration_purpose);
2496 $declaration_purpose .= " " . xml_escape($1);
Daniel Santos12ae6772012-10-04 17:15:10 -07002497 $declaration_purpose =~ s/\s+/ /g;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 } else {
Randy Dunlapb9d973282009-06-09 08:50:38 -07002499 $contents .= $1 . "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 }
2501 } else {
2502 # i dont know - bad line? ignore.
Randy Dunlap3c3b8092006-02-01 03:06:58 -08002503 print STDERR "Warning(${file}:$.): bad line: $_";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 ++$warnings;
2505 }
Randy Dunlap232acbc2006-06-25 05:47:48 -07002506 } elsif ($state == 3) { # scanning for function '{' (end of prototype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 if ($decl_type eq 'function') {
Randy Dunlap3c308792007-05-08 00:24:39 -07002508 process_state3_function($_, $file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 } else {
Randy Dunlap3c308792007-05-08 00:24:39 -07002510 process_state3_type($_, $file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 }
2512 } elsif ($state == 4) {
2513 # Documentation block
Randy Dunlap3c308792007-05-08 00:24:39 -07002514 if (/$doc_block/) {
Randy Dunlap94dc7ad2008-04-28 02:16:34 -07002515 dump_doc_section($file, $section, xml_escape($contents));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 $contents = "";
2517 $function = "";
2518 %constants = ();
2519 %parameterdescs = ();
2520 %parametertypes = ();
2521 @parameterlist = ();
2522 %sections = ();
2523 @sectionlist = ();
2524 $prototype = "";
2525 if ( $1 eq "" ) {
2526 $section = $section_intro;
2527 } else {
2528 $section = $1;
2529 }
Randy Dunlap3c308792007-05-08 00:24:39 -07002530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 elsif (/$doc_end/)
2532 {
Randy Dunlap94dc7ad2008-04-28 02:16:34 -07002533 dump_doc_section($file, $section, xml_escape($contents));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 $contents = "";
2535 $function = "";
2536 %constants = ();
2537 %parameterdescs = ();
2538 %parametertypes = ();
2539 @parameterlist = ();
2540 %sections = ();
2541 @sectionlist = ();
2542 $prototype = "";
2543 $state = 0;
2544 }
2545 elsif (/$doc_content/)
2546 {
2547 if ( $1 eq "" )
2548 {
2549 $contents .= $blankline;
2550 }
2551 else
2552 {
2553 $contents .= $1 . "\n";
Randy Dunlap3c3b8092006-02-01 03:06:58 -08002554 }
Randy Dunlap3c308792007-05-08 00:24:39 -07002555 }
2556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 }
2558 if ($initial_section_counter == $section_counter) {
2559 print STDERR "Warning(${file}): no structured comments found\n";
Johannes Berge946c43a2013-11-12 15:11:12 -08002560 if (($function_only == 1) && ($show_not_found == 1)) {
2561 print STDERR " Was looking for '$_'.\n" for keys %function_table;
2562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 if ($output_mode eq "xml") {
2564 # The template wants at least one RefEntry here; make one.
2565 print "<refentry>\n";
2566 print " <refnamediv>\n";
2567 print " <refname>\n";
2568 print " ${file}\n";
2569 print " </refname>\n";
2570 print " <refpurpose>\n";
2571 print " Document generation inconsistency\n";
2572 print " </refpurpose>\n";
2573 print " </refnamediv>\n";
2574 print " <refsect1>\n";
2575 print " <title>\n";
2576 print " Oops\n";
2577 print " </title>\n";
2578 print " <warning>\n";
2579 print " <para>\n";
2580 print " The template for this document tried to insert\n";
2581 print " the structured comment from the file\n";
2582 print " <filename>${file}</filename> at this point,\n";
2583 print " but none was found.\n";
2584 print " This dummy section is inserted to allow\n";
2585 print " generation to continue.\n";
2586 print " </para>\n";
2587 print " </warning>\n";
2588 print " </refsect1>\n";
2589 print "</refentry>\n";
2590 }
2591 }
2592}
Randy Dunlap8484baa2011-01-05 16:28:43 -08002593
2594
2595$kernelversion = get_kernel_version();
2596
2597# generate a sequence of code that will splice in highlighting information
2598# using the s// operator.
Jérémy Bobbio2e106e42015-07-08 20:06:33 +01002599foreach my $pattern (sort keys %highlights) {
Randy Dunlap8484baa2011-01-05 16:28:43 -08002600# print STDERR "scanning pattern:$pattern, highlight:($highlights{$pattern})\n";
2601 $dohighlight .= "\$contents =~ s:$pattern:$highlights{$pattern}:gs;\n";
2602}
2603
2604# Read the file that maps relative names to absolute names for
2605# separate source and object directories and for shadow trees.
2606if (open(SOURCE_MAP, "<.tmp_filelist.txt")) {
2607 my ($relname, $absname);
2608 while(<SOURCE_MAP>) {
2609 chop();
2610 ($relname, $absname) = (split())[0..1];
2611 $relname =~ s:^/+::;
2612 $source_map{$relname} = $absname;
2613 }
2614 close(SOURCE_MAP);
2615}
2616
2617foreach (@ARGV) {
2618 chomp;
2619 process_file($_);
2620}
2621if ($verbose && $errors) {
2622 print STDERR "$errors errors\n";
2623}
2624if ($verbose && $warnings) {
2625 print STDERR "$warnings warnings\n";
2626}
2627
2628exit($errors);