blob: 102e1235fd5ced3f83f18a9a5ecd2306b31e087b [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 Dunlapb9d973282009-06-09 08:50:38 -07008## Copyright (C) 2005-2009 Randy Dunlap ##
Linus Torvalds1da177e2005-04-16 15:20:36 -07009## ##
10## #define enhancements by Armin Kuster <akuster@mvista.com> ##
11## Copyright (c) 2000 MontaVista Software, Inc. ##
12## ##
13## This software falls under the GNU General Public License. ##
14## Please read the COPYING file for more information ##
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016# 18/01/2001 - Cleanups
17# Functions prototyped as foo(void) same as foo()
18# Stop eval'ing where we don't need to.
19# -- huggie@earth.li
20
21# 27/06/2001 - Allowed whitespace after initial "/**" and
22# allowed comments before function declarations.
23# -- Christian Kreibich <ck@whoop.org>
24
25# Still to do:
26# - add perldoc documentation
27# - Look more closely at some of the scarier bits :)
28
29# 26/05/2001 - Support for separate source and object trees.
30# Return error code.
31# Keith Owens <kaos@ocs.com.au>
32
33# 23/09/2001 - Added support for typedefs, structs, enums and unions
34# Support for Context section; can be terminated using empty line
35# Small fixes (like spaces vs. \s in regex)
36# -- Tim Jansen <tim@tjansen.de>
37
38
39#
40# This will read a 'c' file and scan for embedded comments in the
41# style of gnome comments (+minor extensions - see below).
42#
43
44# Note: This only supports 'c'.
45
46# usage:
Johannes Berg4b445952007-10-24 15:08:48 -070047# kernel-doc [ -docbook | -html | -text | -man ] [ -no-doc-sections ]
Linus Torvalds1da177e2005-04-16 15:20:36 -070048# [ -function funcname [ -function funcname ...] ] c file(s)s > outputfile
49# or
50# [ -nofunction funcname [ -function funcname ...] ] c file(s)s > outputfile
51#
52# Set output format using one of -docbook -html -text or -man. Default is man.
53#
Johannes Berg4b445952007-10-24 15:08:48 -070054# -no-doc-sections
55# Do not output DOC: sections
56#
Linus Torvalds1da177e2005-04-16 15:20:36 -070057# -function funcname
Johannes Bergb112e0f2007-10-24 15:08:48 -070058# If set, then only generate documentation for the given function(s) or
59# DOC: section titles. All other functions and DOC: sections are ignored.
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#
61# -nofunction funcname
Johannes Bergb112e0f2007-10-24 15:08:48 -070062# If set, then only generate documentation for the other function(s)/DOC:
63# sections. Cannot be used together with -function (yes, that's a bug --
64# perl hackers can fix it 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#
66# c files - list of 'c' files to process
67#
68# All output goes to stdout, with errors to stderr.
69
70#
71# format of comments.
72# In the following table, (...)? signifies optional structure.
73# (...)* signifies 0 or more structure elements
74# /**
75# * function_name(:)? (- short description)?
76# (* @parameterx: (description of parameter x)?)*
77# (* a blank line)?
78# * (Description:)? (Description of function)?
79# * (section header: (section description)? )*
80# (*)?*/
81#
82# So .. the trivial example would be:
83#
84# /**
85# * my_function
Randy Dunlapb9d973282009-06-09 08:50:38 -070086# */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#
Randy Dunlap891dcd22007-02-10 01:45:53 -080088# If the Description: header tag is omitted, then there must be a blank line
Linus Torvalds1da177e2005-04-16 15:20:36 -070089# after the last parameter specification.
90# e.g.
91# /**
92# * my_function - does my stuff
93# * @my_arg: its mine damnit
94# *
Randy Dunlap3c3b8092006-02-01 03:06:58 -080095# * Does my stuff explained.
Linus Torvalds1da177e2005-04-16 15:20:36 -070096# */
97#
98# or, could also use:
99# /**
100# * my_function - does my stuff
101# * @my_arg: its mine damnit
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800102# * Description: Does my stuff explained.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103# */
104# etc.
105#
Randy Dunlapb9d973282009-06-09 08:50:38 -0700106# Besides functions you can also write documentation for structs, unions,
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800107# enums and typedefs. Instead of the function name you must write the name
108# of the declaration; the struct/union/enum/typedef must always precede
109# the name. Nesting of declarations is not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110# Use the argument mechanism to document members or constants.
111# e.g.
112# /**
113# * struct my_struct - short description
114# * @a: first member
115# * @b: second member
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800116# *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117# * Longer description
118# */
119# struct my_struct {
120# int a;
121# int b;
Martin Waitzaeec46b2005-11-13 16:08:13 -0800122# /* private: */
123# int c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124# };
125#
126# All descriptions can be multiline, except the short function description.
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800127#
128# You can also add additional sections. When documenting kernel functions you
129# should document the "Context:" of the function, e.g. whether the functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130# can be called form interrupts. Unlike other sections you can end it with an
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800131# empty line.
132# Example-sections should contain the string EXAMPLE so that they are marked
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133# appropriately in DocBook.
134#
135# Example:
136# /**
137# * user_function - function that can only be called in user context
138# * @a: some argument
139# * Context: !in_interrupt()
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800140# *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141# * Some description
142# * Example:
143# * user_function(22);
144# */
145# ...
146#
147#
148# All descriptive text is further processed, scanning for the following special
149# patterns, which are highlighted appropriately.
150#
151# 'funcname()' - function
152# '$ENVVAR' - environmental variable
153# '&struct_name' - name of a structure (up to two words including 'struct')
154# '@parameter' - name of a parameter
155# '%CONST' - name of a constant.
156
157my $errors = 0;
158my $warnings = 0;
Randy Dunlap5f8c7c92007-07-19 01:48:24 -0700159my $anon_struct_union = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161# match expressions used to find embedded type information
162my $type_constant = '\%([-_\w]+)';
163my $type_func = '(\w+)\(\)';
164my $type_param = '\@(\w+)';
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700165my $type_struct = '\&((struct\s*)*[_\w]+)';
Randy Dunlap6b5b55f2007-10-16 23:31:20 -0700166my $type_struct_xml = '\\&amp;((struct\s*)*[_\w]+)';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167my $type_env = '(\$\w+)';
168
169# Output conversion substitutions.
170# One for each output format
171
172# these work fairly well
173my %highlights_html = ( $type_constant, "<i>\$1</i>",
174 $type_func, "<b>\$1</b>",
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700175 $type_struct_xml, "<i>\$1</i>",
176 $type_env, "<b><i>\$1</i></b>",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 $type_param, "<tt><b>\$1</b></tt>" );
Randy Dunlap6b5b55f2007-10-16 23:31:20 -0700178my $local_lt = "\\\\\\\\lt:";
179my $local_gt = "\\\\\\\\gt:";
180my $blankline_html = $local_lt . "p" . $local_gt; # was "<p>"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182# XML, docbook format
183my %highlights_xml = ( "([^=])\\\"([^\\\"<]+)\\\"", "\$1<quote>\$2</quote>",
184 $type_constant, "<constant>\$1</constant>",
185 $type_func, "<function>\$1</function>",
Johannes Berg5c98fc02007-10-24 15:08:48 -0700186 $type_struct_xml, "<structname>\$1</structname>",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 $type_env, "<envar>\$1</envar>",
188 $type_param, "<parameter>\$1</parameter>" );
Johannes Berg5c98fc02007-10-24 15:08:48 -0700189my $blankline_xml = $local_lt . "/para" . $local_gt . $local_lt . "para" . $local_gt . "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191# gnome, docbook format
192my %highlights_gnome = ( $type_constant, "<replaceable class=\"option\">\$1</replaceable>",
193 $type_func, "<function>\$1</function>",
194 $type_struct, "<structname>\$1</structname>",
195 $type_env, "<envar>\$1</envar>",
196 $type_param, "<parameter>\$1</parameter>" );
197my $blankline_gnome = "</para><para>\n";
198
199# these are pretty rough
200my %highlights_man = ( $type_constant, "\$1",
201 $type_func, "\\\\fB\$1\\\\fP",
202 $type_struct, "\\\\fI\$1\\\\fP",
203 $type_param, "\\\\fI\$1\\\\fP" );
204my $blankline_man = "";
205
206# text-mode
207my %highlights_text = ( $type_constant, "\$1",
208 $type_func, "\$1",
209 $type_struct, "\$1",
210 $type_param, "\$1" );
211my $blankline_text = "";
212
213
214sub usage {
Johannes Berg4b445952007-10-24 15:08:48 -0700215 print "Usage: $0 [ -v ] [ -docbook | -html | -text | -man ] [ -no-doc-sections ]\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 print " [ -function funcname [ -function funcname ...] ]\n";
217 print " [ -nofunction funcname [ -nofunction funcname ...] ]\n";
218 print " c source file(s) > outputfile\n";
Randy Dunlap77cc23b2008-02-07 00:13:42 -0800219 print " -v : verbose output, more warnings & other info listed\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 exit 1;
221}
222
223# read arguments
Randy Dunlapb9d973282009-06-09 08:50:38 -0700224if ($#ARGV == -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 usage();
226}
227
228my $verbose = 0;
229my $output_mode = "man";
Johannes Berg4b445952007-10-24 15:08:48 -0700230my $no_doc_sections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231my %highlights = %highlights_man;
232my $blankline = $blankline_man;
233my $modulename = "Kernel API";
234my $function_only = 0;
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800235my $man_date = ('January', 'February', 'March', 'April', 'May', 'June',
236 'July', 'August', 'September', 'October',
237 'November', 'December')[(localtime)[4]] .
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 " " . ((localtime)[5]+1900);
239
240# Essentially these are globals
Randy Dunlapb9d973282009-06-09 08:50:38 -0700241# They probably want to be tidied up, made more localised or something.
242# CAVEAT EMPTOR! Some of the others I localised may not want to be, which
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243# could cause "use of undefined value" or other bugs.
Randy Dunlapb9d973282009-06-09 08:50:38 -0700244my ($function, %function_table, %parametertypes, $declaration_purpose);
245my ($type, $declaration_name, $return_type);
Ilya Dryomov1c32fd02010-02-26 13:06:03 -0800246my ($newsection, $newcontents, $prototype, $brcount, %source_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Randy Dunlapbd0e88e2008-03-13 12:32:43 -0700248if (defined($ENV{'KBUILD_VERBOSE'})) {
249 $verbose = "$ENV{'KBUILD_VERBOSE'}";
250}
251
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800252# Generated docbook code is inserted in a template at a point where
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253# docbook v3.1 requires a non-zero sequence of RefEntry's; see:
254# http://www.oasis-open.org/docbook/documentation/reference/html/refentry.html
255# We keep track of number of generated entries and generate a dummy
256# if needs be to ensure the expanded template can be postprocessed
257# into html.
258my $section_counter = 0;
259
260my $lineprefix="";
261
262# states
263# 0 - normal code
264# 1 - looking for function name
265# 2 - scanning field start.
266# 3 - scanning prototype.
267# 4 - documentation block
268my $state;
Randy Dunlap850622d2006-06-25 05:48:55 -0700269my $in_doc_sect;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271#declaration types: can be
272# 'function', 'struct', 'union', 'enum', 'typedef'
273my $decl_type;
274
275my $doc_special = "\@\%\$\&";
276
277my $doc_start = '^/\*\*\s*$'; # Allow whitespace at end of comment start.
278my $doc_end = '\*/';
279my $doc_com = '\s*\*\s*';
Randy Dunlapb9d973282009-06-09 08:50:38 -0700280my $doc_decl = $doc_com . '(\w+)';
281my $doc_sect = $doc_com . '([' . $doc_special . ']?[\w\s]+):(.*)';
282my $doc_content = $doc_com . '(.*)';
283my $doc_block = $doc_com . 'DOC:\s*(.*)?';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285my %constants;
286my %parameterdescs;
287my @parameterlist;
288my %sections;
289my @sectionlist;
Randy Dunlapa1d94aa2008-12-19 08:49:30 -0800290my $sectcheck;
291my $struct_actual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293my $contents = "";
294my $section_default = "Description"; # default section
295my $section_intro = "Introduction";
296my $section = $section_default;
297my $section_context = "Context";
298
299my $undescribed = "-- undescribed --";
300
301reset_state();
302
303while ($ARGV[0] =~ m/^-(.*)/) {
304 my $cmd = shift @ARGV;
305 if ($cmd eq "-html") {
306 $output_mode = "html";
307 %highlights = %highlights_html;
308 $blankline = $blankline_html;
309 } elsif ($cmd eq "-man") {
310 $output_mode = "man";
311 %highlights = %highlights_man;
312 $blankline = $blankline_man;
313 } elsif ($cmd eq "-text") {
314 $output_mode = "text";
315 %highlights = %highlights_text;
316 $blankline = $blankline_text;
317 } elsif ($cmd eq "-docbook") {
318 $output_mode = "xml";
319 %highlights = %highlights_xml;
320 $blankline = $blankline_xml;
321 } elsif ($cmd eq "-gnome") {
322 $output_mode = "gnome";
323 %highlights = %highlights_gnome;
324 $blankline = $blankline_gnome;
325 } elsif ($cmd eq "-module") { # not needed for XML, inherits from calling document
326 $modulename = shift @ARGV;
327 } elsif ($cmd eq "-function") { # to only output specific functions
328 $function_only = 1;
329 $function = shift @ARGV;
330 $function_table{$function} = 1;
331 } elsif ($cmd eq "-nofunction") { # to only output specific functions
332 $function_only = 2;
333 $function = shift @ARGV;
334 $function_table{$function} = 1;
335 } elsif ($cmd eq "-v") {
336 $verbose = 1;
337 } elsif (($cmd eq "-h") || ($cmd eq "--help")) {
338 usage();
Johannes Berg4b445952007-10-24 15:08:48 -0700339 } elsif ($cmd eq '-no-doc-sections') {
340 $no_doc_sections = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342}
343
Borislav Petkov53f049f2007-05-08 00:30:54 -0700344# get kernel version from env
345sub get_kernel_version() {
Johannes Berg1b9bc222007-10-24 15:08:48 -0700346 my $version = 'unknown kernel version';
Borislav Petkov53f049f2007-05-08 00:30:54 -0700347
348 if (defined($ENV{'KERNELVERSION'})) {
349 $version = $ENV{'KERNELVERSION'};
350 }
351 return $version;
352}
Borislav Petkov03662992007-05-09 02:33:43 -0700353my $kernelversion = get_kernel_version();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355# generate a sequence of code that will splice in highlighting information
356# using the s// operator.
357my $dohighlight = "";
358foreach my $pattern (keys %highlights) {
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700359# print STDERR "scanning pattern:$pattern, highlight:($highlights{$pattern})\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 $dohighlight .= "\$contents =~ s:$pattern:$highlights{$pattern}:gs;\n";
361}
362
363##
364# dumps section contents to arrays/hashes intended for that purpose.
365#
366sub dump_section {
Randy Dunlap94dc7ad2008-04-28 02:16:34 -0700367 my $file = shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 my $name = shift;
369 my $contents = join "\n", @_;
370
371 if ($name =~ m/$type_constant/) {
372 $name = $1;
373# print STDERR "constant section '$1' = '$contents'\n";
374 $constants{$name} = $contents;
375 } elsif ($name =~ m/$type_param/) {
376# print STDERR "parameter def '$1' = '$contents'\n";
377 $name = $1;
378 $parameterdescs{$name} = $contents;
Randy Dunlapa1d94aa2008-12-19 08:49:30 -0800379 $sectcheck = $sectcheck . $name . " ";
Randy Dunlapced69092008-12-01 13:14:03 -0800380 } elsif ($name eq "@\.\.\.") {
381# print STDERR "parameter def '...' = '$contents'\n";
382 $name = "...";
383 $parameterdescs{$name} = $contents;
Randy Dunlapa1d94aa2008-12-19 08:49:30 -0800384 $sectcheck = $sectcheck . $name . " ";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 } else {
386# print STDERR "other section '$name' = '$contents'\n";
Randy Dunlap94dc7ad2008-04-28 02:16:34 -0700387 if (defined($sections{$name}) && ($sections{$name} ne "")) {
388 print STDERR "Error(${file}:$.): duplicate section name '$name'\n";
389 ++$errors;
390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 $sections{$name} = $contents;
392 push @sectionlist, $name;
393 }
394}
395
396##
Johannes Bergb112e0f2007-10-24 15:08:48 -0700397# dump DOC: section after checking that it should go out
398#
399sub dump_doc_section {
Randy Dunlap94dc7ad2008-04-28 02:16:34 -0700400 my $file = shift;
Johannes Bergb112e0f2007-10-24 15:08:48 -0700401 my $name = shift;
402 my $contents = join "\n", @_;
403
Johannes Berg4b445952007-10-24 15:08:48 -0700404 if ($no_doc_sections) {
405 return;
406 }
407
Johannes Bergb112e0f2007-10-24 15:08:48 -0700408 if (($function_only == 0) ||
409 ( $function_only == 1 && defined($function_table{$name})) ||
410 ( $function_only == 2 && !defined($function_table{$name})))
411 {
Randy Dunlap94dc7ad2008-04-28 02:16:34 -0700412 dump_section($file, $name, $contents);
Johannes Bergb112e0f2007-10-24 15:08:48 -0700413 output_blockhead({'sectionlist' => \@sectionlist,
414 'sections' => \%sections,
415 'module' => $modulename,
416 'content-only' => ($function_only != 0), });
417 }
418}
419
420##
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421# output function
422#
423# parameterdescs, a hash.
424# function => "function name"
425# parameterlist => @list of parameters
426# parameterdescs => %parameter descriptions
427# sectionlist => @list of sections
Randy Dunlapa21217d2007-02-10 01:46:04 -0800428# sections => %section descriptions
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800429#
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431sub output_highlight {
432 my $contents = join "\n",@_;
433 my $line;
434
435# DEBUG
436# if (!defined $contents) {
437# use Carp;
438# confess "output_highlight got called with no args?\n";
439# }
440
Johannes Berg5c98fc02007-10-24 15:08:48 -0700441 if ($output_mode eq "html" || $output_mode eq "xml") {
Randy Dunlap6b5b55f2007-10-16 23:31:20 -0700442 $contents = local_unescape($contents);
443 # convert data read & converted thru xml_escape() into &xyz; format:
444 $contents =~ s/\\\\\\/&/g;
445 }
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700446# print STDERR "contents b4:$contents\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 eval $dohighlight;
448 die $@ if $@;
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700449# print STDERR "contents af:$contents\n";
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 foreach $line (split "\n", $contents) {
Randy Dunlap3c308792007-05-08 00:24:39 -0700452 if ($line eq ""){
Randy Dunlap6b5b55f2007-10-16 23:31:20 -0700453 print $lineprefix, local_unescape($blankline);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 } else {
Randy Dunlap3c308792007-05-08 00:24:39 -0700455 $line =~ s/\\\\\\/\&/g;
Randy Dunlapcdccb312007-07-19 01:48:25 -0700456 if ($output_mode eq "man" && substr($line, 0, 1) eq ".") {
457 print "\\&$line";
458 } else {
459 print $lineprefix, $line;
460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 }
462 print "\n";
463 }
464}
465
466#output sections in html
467sub output_section_html(%) {
468 my %args = %{$_[0]};
469 my $section;
470
471 foreach $section (@{$args{'sectionlist'}}) {
472 print "<h3>$section</h3>\n";
473 print "<blockquote>\n";
474 output_highlight($args{'sections'}{$section});
475 print "</blockquote>\n";
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
479# output enum in html
480sub output_enum_html(%) {
481 my %args = %{$_[0]};
482 my ($parameter);
483 my $count;
Randy Dunlapb9d973282009-06-09 08:50:38 -0700484 print "<h2>enum " . $args{'enum'} . "</h2>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Randy Dunlapb9d973282009-06-09 08:50:38 -0700486 print "<b>enum " . $args{'enum'} . "</b> {<br>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 $count = 0;
488 foreach $parameter (@{$args{'parameterlist'}}) {
Randy Dunlapb9d973282009-06-09 08:50:38 -0700489 print " <b>" . $parameter . "</b>";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 if ($count != $#{$args{'parameterlist'}}) {
491 $count++;
492 print ",\n";
493 }
494 print "<br>";
495 }
496 print "};<br>\n";
497
498 print "<h3>Constants</h3>\n";
499 print "<dl>\n";
500 foreach $parameter (@{$args{'parameterlist'}}) {
Randy Dunlapb9d973282009-06-09 08:50:38 -0700501 print "<dt><b>" . $parameter . "</b>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 print "<dd>";
503 output_highlight($args{'parameterdescs'}{$parameter});
504 }
505 print "</dl>\n";
506 output_section_html(@_);
507 print "<hr>\n";
508}
509
Randy Dunlapd28bee02006-02-01 03:06:57 -0800510# output typedef in html
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511sub output_typedef_html(%) {
512 my %args = %{$_[0]};
513 my ($parameter);
514 my $count;
Randy Dunlapb9d973282009-06-09 08:50:38 -0700515 print "<h2>typedef " . $args{'typedef'} . "</h2>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Randy Dunlapb9d973282009-06-09 08:50:38 -0700517 print "<b>typedef " . $args{'typedef'} . "</b>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 output_section_html(@_);
519 print "<hr>\n";
520}
521
522# output struct in html
523sub output_struct_html(%) {
524 my %args = %{$_[0]};
525 my ($parameter);
526
Randy Dunlapb9d973282009-06-09 08:50:38 -0700527 print "<h2>" . $args{'type'} . " " . $args{'struct'} . " - " . $args{'purpose'} . "</h2>\n";
528 print "<b>" . $args{'type'} . " " . $args{'struct'} . "</b> {<br>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 foreach $parameter (@{$args{'parameterlist'}}) {
530 if ($parameter =~ /^#/) {
531 print "$parameter<br>\n";
532 next;
533 }
534 my $parameter_name = $parameter;
535 $parameter_name =~ s/\[.*//;
536
Randy Dunlap3c308792007-05-08 00:24:39 -0700537 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 $type = $args{'parametertypes'}{$parameter};
539 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
540 # pointer-to-function
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700541 print "&nbsp; &nbsp; <i>$1</i><b>$parameter</b>) <i>($2)</i>;<br>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700543 # bitfield
544 print "&nbsp; &nbsp; <i>$1</i> <b>$parameter</b>$2;<br>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 } else {
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700546 print "&nbsp; &nbsp; <i>$type</i> <b>$parameter</b>;<br>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
548 }
549 print "};<br>\n";
550
551 print "<h3>Members</h3>\n";
552 print "<dl>\n";
553 foreach $parameter (@{$args{'parameterlist'}}) {
554 ($parameter =~ /^#/) && next;
555
556 my $parameter_name = $parameter;
557 $parameter_name =~ s/\[.*//;
558
Randy Dunlap3c308792007-05-08 00:24:39 -0700559 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Randy Dunlapb9d973282009-06-09 08:50:38 -0700560 print "<dt><b>" . $parameter . "</b>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 print "<dd>";
562 output_highlight($args{'parameterdescs'}{$parameter_name});
563 }
564 print "</dl>\n";
565 output_section_html(@_);
566 print "<hr>\n";
567}
568
569# output function in html
570sub output_function_html(%) {
571 my %args = %{$_[0]};
572 my ($parameter, $section);
573 my $count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Randy Dunlapb9d973282009-06-09 08:50:38 -0700575 print "<h2>" . $args{'function'} . " - " . $args{'purpose'} . "</h2>\n";
576 print "<i>" . $args{'functiontype'} . "</i>\n";
577 print "<b>" . $args{'function'} . "</b>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 print "(";
579 $count = 0;
580 foreach $parameter (@{$args{'parameterlist'}}) {
581 $type = $args{'parametertypes'}{$parameter};
582 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
583 # pointer-to-function
584 print "<i>$1</i><b>$parameter</b>) <i>($2)</i>";
585 } else {
Randy Dunlapb9d973282009-06-09 08:50:38 -0700586 print "<i>" . $type . "</i> <b>" . $parameter . "</b>";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
588 if ($count != $#{$args{'parameterlist'}}) {
589 $count++;
590 print ",\n";
591 }
592 }
593 print ")\n";
594
595 print "<h3>Arguments</h3>\n";
596 print "<dl>\n";
597 foreach $parameter (@{$args{'parameterlist'}}) {
598 my $parameter_name = $parameter;
599 $parameter_name =~ s/\[.*//;
600
Randy Dunlap3c308792007-05-08 00:24:39 -0700601 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Randy Dunlapb9d973282009-06-09 08:50:38 -0700602 print "<dt><b>" . $parameter . "</b>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 print "<dd>";
604 output_highlight($args{'parameterdescs'}{$parameter_name});
605 }
606 print "</dl>\n";
607 output_section_html(@_);
608 print "<hr>\n";
609}
610
Johannes Bergb112e0f2007-10-24 15:08:48 -0700611# output DOC: block header in html
612sub output_blockhead_html(%) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 my %args = %{$_[0]};
614 my ($parameter, $section);
615 my $count;
616
617 foreach $section (@{$args{'sectionlist'}}) {
618 print "<h3>$section</h3>\n";
619 print "<ul>\n";
620 output_highlight($args{'sections'}{$section});
621 print "</ul>\n";
622 }
623 print "<hr>\n";
624}
625
626sub output_section_xml(%) {
627 my %args = %{$_[0]};
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800628 my $section;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 # print out each section
630 $lineprefix=" ";
631 foreach $section (@{$args{'sectionlist'}}) {
Rich Walkerc73894c2005-05-01 08:59:26 -0700632 print "<refsect1>\n";
633 print "<title>$section</title>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 if ($section =~ m/EXAMPLE/i) {
Rich Walkerc73894c2005-05-01 08:59:26 -0700635 print "<informalexample><programlisting>\n";
636 } else {
637 print "<para>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
639 output_highlight($args{'sections'}{$section});
640 if ($section =~ m/EXAMPLE/i) {
Rich Walkerc73894c2005-05-01 08:59:26 -0700641 print "</programlisting></informalexample>\n";
642 } else {
643 print "</para>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
Rich Walkerc73894c2005-05-01 08:59:26 -0700645 print "</refsect1>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
647}
648
649# output function in XML DocBook
650sub output_function_xml(%) {
651 my %args = %{$_[0]};
652 my ($parameter, $section);
653 my $count;
654 my $id;
655
Randy Dunlapb9d973282009-06-09 08:50:38 -0700656 $id = "API-" . $args{'function'};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 $id =~ s/[^A-Za-z0-9]/-/g;
658
Pavel Pisa5449bc92007-02-10 01:45:37 -0800659 print "<refentry id=\"$id\">\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700660 print "<refentryinfo>\n";
661 print " <title>LINUX</title>\n";
662 print " <productname>Kernel Hackers Manual</productname>\n";
663 print " <date>$man_date</date>\n";
664 print "</refentryinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 print "<refmeta>\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -0700666 print " <refentrytitle><phrase>" . $args{'function'} . "</phrase></refentrytitle>\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700667 print " <manvolnum>9</manvolnum>\n";
Borislav Petkov03662992007-05-09 02:33:43 -0700668 print " <refmiscinfo class=\"version\">" . $kernelversion . "</refmiscinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 print "</refmeta>\n";
670 print "<refnamediv>\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -0700671 print " <refname>" . $args{'function'} . "</refname>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 print " <refpurpose>\n";
673 print " ";
674 output_highlight ($args{'purpose'});
675 print " </refpurpose>\n";
676 print "</refnamediv>\n";
677
678 print "<refsynopsisdiv>\n";
679 print " <title>Synopsis</title>\n";
680 print " <funcsynopsis><funcprototype>\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -0700681 print " <funcdef>" . $args{'functiontype'} . " ";
682 print "<function>" . $args{'function'} . " </function></funcdef>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684 $count = 0;
685 if ($#{$args{'parameterlist'}} >= 0) {
686 foreach $parameter (@{$args{'parameterlist'}}) {
687 $type = $args{'parametertypes'}{$parameter};
688 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
689 # pointer-to-function
690 print " <paramdef>$1<parameter>$parameter</parameter>)\n";
691 print " <funcparams>$2</funcparams></paramdef>\n";
692 } else {
Randy Dunlapb9d973282009-06-09 08:50:38 -0700693 print " <paramdef>" . $type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 print " <parameter>$parameter</parameter></paramdef>\n";
695 }
696 }
697 } else {
Martin Waitz6013d542005-05-01 08:59:25 -0700698 print " <void/>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 }
700 print " </funcprototype></funcsynopsis>\n";
701 print "</refsynopsisdiv>\n";
702
703 # print parameters
704 print "<refsect1>\n <title>Arguments</title>\n";
705 if ($#{$args{'parameterlist'}} >= 0) {
706 print " <variablelist>\n";
707 foreach $parameter (@{$args{'parameterlist'}}) {
708 my $parameter_name = $parameter;
709 $parameter_name =~ s/\[.*//;
710
711 print " <varlistentry>\n <term><parameter>$parameter</parameter></term>\n";
712 print " <listitem>\n <para>\n";
713 $lineprefix=" ";
714 output_highlight($args{'parameterdescs'}{$parameter_name});
715 print " </para>\n </listitem>\n </varlistentry>\n";
716 }
717 print " </variablelist>\n";
718 } else {
719 print " <para>\n None\n </para>\n";
720 }
721 print "</refsect1>\n";
722
723 output_section_xml(@_);
724 print "</refentry>\n\n";
725}
726
727# output struct in XML DocBook
728sub output_struct_xml(%) {
729 my %args = %{$_[0]};
730 my ($parameter, $section);
731 my $id;
732
Randy Dunlapb9d973282009-06-09 08:50:38 -0700733 $id = "API-struct-" . $args{'struct'};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 $id =~ s/[^A-Za-z0-9]/-/g;
735
Pavel Pisa5449bc92007-02-10 01:45:37 -0800736 print "<refentry id=\"$id\">\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700737 print "<refentryinfo>\n";
738 print " <title>LINUX</title>\n";
739 print " <productname>Kernel Hackers Manual</productname>\n";
740 print " <date>$man_date</date>\n";
741 print "</refentryinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 print "<refmeta>\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -0700743 print " <refentrytitle><phrase>" . $args{'type'} . " " . $args{'struct'} . "</phrase></refentrytitle>\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700744 print " <manvolnum>9</manvolnum>\n";
Borislav Petkov03662992007-05-09 02:33:43 -0700745 print " <refmiscinfo class=\"version\">" . $kernelversion . "</refmiscinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 print "</refmeta>\n";
747 print "<refnamediv>\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -0700748 print " <refname>" . $args{'type'} . " " . $args{'struct'} . "</refname>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 print " <refpurpose>\n";
750 print " ";
751 output_highlight ($args{'purpose'});
752 print " </refpurpose>\n";
753 print "</refnamediv>\n";
754
755 print "<refsynopsisdiv>\n";
756 print " <title>Synopsis</title>\n";
757 print " <programlisting>\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -0700758 print $args{'type'} . " " . $args{'struct'} . " {\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 foreach $parameter (@{$args{'parameterlist'}}) {
760 if ($parameter =~ /^#/) {
761 print "$parameter\n";
762 next;
763 }
764
765 my $parameter_name = $parameter;
766 $parameter_name =~ s/\[.*//;
767
768 defined($args{'parameterdescs'}{$parameter_name}) || next;
Randy Dunlap3c308792007-05-08 00:24:39 -0700769 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 $type = $args{'parametertypes'}{$parameter};
771 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
772 # pointer-to-function
773 print " $1 $parameter) ($2);\n";
774 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
Randy Dunlap51f5a0c2007-07-19 01:48:24 -0700775 # bitfield
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 print " $1 $parameter$2;\n";
777 } else {
Randy Dunlapb9d973282009-06-09 08:50:38 -0700778 print " " . $type . " " . $parameter . ";\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
780 }
781 print "};";
782 print " </programlisting>\n";
783 print "</refsynopsisdiv>\n";
784
785 print " <refsect1>\n";
786 print " <title>Members</title>\n";
787
Randy Dunlap39f00c02008-09-22 13:57:44 -0700788 if ($#{$args{'parameterlist'}} >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 print " <variablelist>\n";
790 foreach $parameter (@{$args{'parameterlist'}}) {
791 ($parameter =~ /^#/) && next;
792
793 my $parameter_name = $parameter;
794 $parameter_name =~ s/\[.*//;
795
796 defined($args{'parameterdescs'}{$parameter_name}) || next;
797 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
798 print " <varlistentry>";
799 print " <term>$parameter</term>\n";
800 print " <listitem><para>\n";
801 output_highlight($args{'parameterdescs'}{$parameter_name});
802 print " </para></listitem>\n";
803 print " </varlistentry>\n";
804 }
805 print " </variablelist>\n";
Randy Dunlap39f00c02008-09-22 13:57:44 -0700806 } else {
807 print " <para>\n None\n </para>\n";
808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 print " </refsect1>\n";
810
811 output_section_xml(@_);
812
813 print "</refentry>\n\n";
814}
815
816# output enum in XML DocBook
817sub output_enum_xml(%) {
818 my %args = %{$_[0]};
819 my ($parameter, $section);
820 my $count;
821 my $id;
822
Randy Dunlapb9d973282009-06-09 08:50:38 -0700823 $id = "API-enum-" . $args{'enum'};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 $id =~ s/[^A-Za-z0-9]/-/g;
825
Pavel Pisa5449bc92007-02-10 01:45:37 -0800826 print "<refentry id=\"$id\">\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700827 print "<refentryinfo>\n";
828 print " <title>LINUX</title>\n";
829 print " <productname>Kernel Hackers Manual</productname>\n";
830 print " <date>$man_date</date>\n";
831 print "</refentryinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 print "<refmeta>\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -0700833 print " <refentrytitle><phrase>enum " . $args{'enum'} . "</phrase></refentrytitle>\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700834 print " <manvolnum>9</manvolnum>\n";
Borislav Petkov03662992007-05-09 02:33:43 -0700835 print " <refmiscinfo class=\"version\">" . $kernelversion . "</refmiscinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 print "</refmeta>\n";
837 print "<refnamediv>\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -0700838 print " <refname>enum " . $args{'enum'} . "</refname>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 print " <refpurpose>\n";
840 print " ";
841 output_highlight ($args{'purpose'});
842 print " </refpurpose>\n";
843 print "</refnamediv>\n";
844
845 print "<refsynopsisdiv>\n";
846 print " <title>Synopsis</title>\n";
847 print " <programlisting>\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -0700848 print "enum " . $args{'enum'} . " {\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 $count = 0;
850 foreach $parameter (@{$args{'parameterlist'}}) {
Randy Dunlap3c308792007-05-08 00:24:39 -0700851 print " $parameter";
852 if ($count != $#{$args{'parameterlist'}}) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 $count++;
854 print ",";
Randy Dunlap3c308792007-05-08 00:24:39 -0700855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 print "\n";
857 }
858 print "};";
859 print " </programlisting>\n";
860 print "</refsynopsisdiv>\n";
861
862 print "<refsect1>\n";
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800863 print " <title>Constants</title>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 print " <variablelist>\n";
865 foreach $parameter (@{$args{'parameterlist'}}) {
866 my $parameter_name = $parameter;
867 $parameter_name =~ s/\[.*//;
868
869 print " <varlistentry>";
870 print " <term>$parameter</term>\n";
871 print " <listitem><para>\n";
872 output_highlight($args{'parameterdescs'}{$parameter_name});
873 print " </para></listitem>\n";
874 print " </varlistentry>\n";
875 }
876 print " </variablelist>\n";
877 print "</refsect1>\n";
878
879 output_section_xml(@_);
880
881 print "</refentry>\n\n";
882}
883
884# output typedef in XML DocBook
885sub output_typedef_xml(%) {
886 my %args = %{$_[0]};
887 my ($parameter, $section);
888 my $id;
889
Randy Dunlapb9d973282009-06-09 08:50:38 -0700890 $id = "API-typedef-" . $args{'typedef'};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 $id =~ s/[^A-Za-z0-9]/-/g;
892
Pavel Pisa5449bc92007-02-10 01:45:37 -0800893 print "<refentry id=\"$id\">\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700894 print "<refentryinfo>\n";
895 print " <title>LINUX</title>\n";
896 print " <productname>Kernel Hackers Manual</productname>\n";
897 print " <date>$man_date</date>\n";
898 print "</refentryinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 print "<refmeta>\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -0700900 print " <refentrytitle><phrase>typedef " . $args{'typedef'} . "</phrase></refentrytitle>\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700901 print " <manvolnum>9</manvolnum>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 print "</refmeta>\n";
903 print "<refnamediv>\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -0700904 print " <refname>typedef " . $args{'typedef'} . "</refname>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 print " <refpurpose>\n";
906 print " ";
907 output_highlight ($args{'purpose'});
908 print " </refpurpose>\n";
909 print "</refnamediv>\n";
910
911 print "<refsynopsisdiv>\n";
912 print " <title>Synopsis</title>\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -0700913 print " <synopsis>typedef " . $args{'typedef'} . ";</synopsis>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 print "</refsynopsisdiv>\n";
915
916 output_section_xml(@_);
917
918 print "</refentry>\n\n";
919}
920
921# output in XML DocBook
Johannes Bergb112e0f2007-10-24 15:08:48 -0700922sub output_blockhead_xml(%) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 my %args = %{$_[0]};
924 my ($parameter, $section);
925 my $count;
926
927 my $id = $args{'module'};
928 $id =~ s/[^A-Za-z0-9]/-/g;
929
930 # print out each section
931 $lineprefix=" ";
932 foreach $section (@{$args{'sectionlist'}}) {
Johannes Bergb112e0f2007-10-24 15:08:48 -0700933 if (!$args{'content-only'}) {
934 print "<refsect1>\n <title>$section</title>\n";
935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 if ($section =~ m/EXAMPLE/i) {
937 print "<example><para>\n";
Johannes Bergb112e0f2007-10-24 15:08:48 -0700938 } else {
939 print "<para>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
941 output_highlight($args{'sections'}{$section});
942 if ($section =~ m/EXAMPLE/i) {
943 print "</para></example>\n";
Johannes Bergb112e0f2007-10-24 15:08:48 -0700944 } else {
945 print "</para>";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
Johannes Bergb112e0f2007-10-24 15:08:48 -0700947 if (!$args{'content-only'}) {
948 print "\n</refsect1>\n";
949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 }
951
952 print "\n\n";
953}
954
955# output in XML DocBook
956sub output_function_gnome {
957 my %args = %{$_[0]};
958 my ($parameter, $section);
959 my $count;
960 my $id;
961
Randy Dunlapb9d973282009-06-09 08:50:38 -0700962 $id = $args{'module'} . "-" . $args{'function'};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 $id =~ s/[^A-Za-z0-9]/-/g;
964
965 print "<sect2>\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -0700966 print " <title id=\"$id\">" . $args{'function'} . "</title>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 print " <funcsynopsis>\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -0700969 print " <funcdef>" . $args{'functiontype'} . " ";
970 print "<function>" . $args{'function'} . " ";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 print "</function></funcdef>\n";
972
973 $count = 0;
974 if ($#{$args{'parameterlist'}} >= 0) {
975 foreach $parameter (@{$args{'parameterlist'}}) {
976 $type = $args{'parametertypes'}{$parameter};
977 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
978 # pointer-to-function
979 print " <paramdef>$1 <parameter>$parameter</parameter>)\n";
980 print " <funcparams>$2</funcparams></paramdef>\n";
981 } else {
Randy Dunlapb9d973282009-06-09 08:50:38 -0700982 print " <paramdef>" . $type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 print " <parameter>$parameter</parameter></paramdef>\n";
984 }
985 }
986 } else {
987 print " <void>\n";
988 }
989 print " </funcsynopsis>\n";
990 if ($#{$args{'parameterlist'}} >= 0) {
991 print " <informaltable pgwide=\"1\" frame=\"none\" role=\"params\">\n";
992 print "<tgroup cols=\"2\">\n";
993 print "<colspec colwidth=\"2*\">\n";
994 print "<colspec colwidth=\"8*\">\n";
995 print "<tbody>\n";
996 foreach $parameter (@{$args{'parameterlist'}}) {
997 my $parameter_name = $parameter;
998 $parameter_name =~ s/\[.*//;
999
1000 print " <row><entry align=\"right\"><parameter>$parameter</parameter></entry>\n";
1001 print " <entry>\n";
1002 $lineprefix=" ";
1003 output_highlight($args{'parameterdescs'}{$parameter_name});
1004 print " </entry></row>\n";
1005 }
1006 print " </tbody></tgroup></informaltable>\n";
1007 } else {
1008 print " <para>\n None\n </para>\n";
1009 }
1010
1011 # print out each section
1012 $lineprefix=" ";
1013 foreach $section (@{$args{'sectionlist'}}) {
1014 print "<simplesect>\n <title>$section</title>\n";
1015 if ($section =~ m/EXAMPLE/i) {
1016 print "<example><programlisting>\n";
1017 } else {
1018 }
1019 print "<para>\n";
1020 output_highlight($args{'sections'}{$section});
1021 print "</para>\n";
1022 if ($section =~ m/EXAMPLE/i) {
1023 print "</programlisting></example>\n";
1024 } else {
1025 }
1026 print " </simplesect>\n";
1027 }
1028
1029 print "</sect2>\n\n";
1030}
1031
1032##
1033# output function in man
1034sub output_function_man(%) {
1035 my %args = %{$_[0]};
1036 my ($parameter, $section);
1037 my $count;
1038
1039 print ".TH \"$args{'function'}\" 9 \"$args{'function'}\" \"$man_date\" \"Kernel Hacker's Manual\" LINUX\n";
1040
1041 print ".SH NAME\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001042 print $args{'function'} . " \\- " . $args{'purpose'} . "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 print ".SH SYNOPSIS\n";
Randy Dunlapa21217d2007-02-10 01:46:04 -08001045 if ($args{'functiontype'} ne "") {
Randy Dunlapb9d973282009-06-09 08:50:38 -07001046 print ".B \"" . $args{'functiontype'} . "\" " . $args{'function'} . "\n";
Randy Dunlapa21217d2007-02-10 01:46:04 -08001047 } else {
Randy Dunlapb9d973282009-06-09 08:50:38 -07001048 print ".B \"" . $args{'function'} . "\n";
Randy Dunlapa21217d2007-02-10 01:46:04 -08001049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 $count = 0;
1051 my $parenth = "(";
1052 my $post = ",";
1053 foreach my $parameter (@{$args{'parameterlist'}}) {
1054 if ($count == $#{$args{'parameterlist'}}) {
1055 $post = ");";
1056 }
1057 $type = $args{'parametertypes'}{$parameter};
1058 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1059 # pointer-to-function
Randy Dunlapb9d973282009-06-09 08:50:38 -07001060 print ".BI \"" . $parenth . $1 . "\" " . $parameter . " \") (" . $2 . ")" . $post . "\"\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 } else {
1062 $type =~ s/([^\*])$/$1 /;
Randy Dunlapb9d973282009-06-09 08:50:38 -07001063 print ".BI \"" . $parenth . $type . "\" " . $parameter . " \"" . $post . "\"\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 }
1065 $count++;
1066 $parenth = "";
1067 }
1068
1069 print ".SH ARGUMENTS\n";
1070 foreach $parameter (@{$args{'parameterlist'}}) {
1071 my $parameter_name = $parameter;
1072 $parameter_name =~ s/\[.*//;
1073
Randy Dunlapb9d973282009-06-09 08:50:38 -07001074 print ".IP \"" . $parameter . "\" 12\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 output_highlight($args{'parameterdescs'}{$parameter_name});
1076 }
1077 foreach $section (@{$args{'sectionlist'}}) {
1078 print ".SH \"", uc $section, "\"\n";
1079 output_highlight($args{'sections'}{$section});
1080 }
1081}
1082
1083##
1084# output enum in man
1085sub output_enum_man(%) {
1086 my %args = %{$_[0]};
1087 my ($parameter, $section);
1088 my $count;
1089
1090 print ".TH \"$args{'module'}\" 9 \"enum $args{'enum'}\" \"$man_date\" \"API Manual\" LINUX\n";
1091
1092 print ".SH NAME\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001093 print "enum " . $args{'enum'} . " \\- " . $args{'purpose'} . "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
1095 print ".SH SYNOPSIS\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001096 print "enum " . $args{'enum'} . " {\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 $count = 0;
1098 foreach my $parameter (@{$args{'parameterlist'}}) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001099 print ".br\n.BI \" $parameter\"\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 if ($count == $#{$args{'parameterlist'}}) {
1101 print "\n};\n";
1102 last;
1103 }
1104 else {
1105 print ", \n.br\n";
1106 }
1107 $count++;
1108 }
1109
1110 print ".SH Constants\n";
1111 foreach $parameter (@{$args{'parameterlist'}}) {
1112 my $parameter_name = $parameter;
1113 $parameter_name =~ s/\[.*//;
1114
Randy Dunlapb9d973282009-06-09 08:50:38 -07001115 print ".IP \"" . $parameter . "\" 12\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 output_highlight($args{'parameterdescs'}{$parameter_name});
1117 }
1118 foreach $section (@{$args{'sectionlist'}}) {
1119 print ".SH \"$section\"\n";
1120 output_highlight($args{'sections'}{$section});
1121 }
1122}
1123
1124##
1125# output struct in man
1126sub output_struct_man(%) {
1127 my %args = %{$_[0]};
1128 my ($parameter, $section);
1129
Randy Dunlapb9d973282009-06-09 08:50:38 -07001130 print ".TH \"$args{'module'}\" 9 \"" . $args{'type'} . " " . $args{'struct'} . "\" \"$man_date\" \"API Manual\" LINUX\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 print ".SH NAME\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001133 print $args{'type'} . " " . $args{'struct'} . " \\- " . $args{'purpose'} . "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135 print ".SH SYNOPSIS\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001136 print $args{'type'} . " " . $args{'struct'} . " {\n.br\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 foreach my $parameter (@{$args{'parameterlist'}}) {
1139 if ($parameter =~ /^#/) {
1140 print ".BI \"$parameter\"\n.br\n";
1141 next;
1142 }
1143 my $parameter_name = $parameter;
1144 $parameter_name =~ s/\[.*//;
1145
Randy Dunlap3c308792007-05-08 00:24:39 -07001146 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 $type = $args{'parametertypes'}{$parameter};
1148 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1149 # pointer-to-function
Randy Dunlapb9d973282009-06-09 08:50:38 -07001150 print ".BI \" " . $1 . "\" " . $parameter . " \") (" . $2 . ")" . "\"\n;\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
Randy.Dunlap1d7e1d42006-07-01 04:36:34 -07001152 # bitfield
Randy Dunlapb9d973282009-06-09 08:50:38 -07001153 print ".BI \" " . $1 . "\ \" " . $parameter . $2 . " \"" . "\"\n;\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 } else {
1155 $type =~ s/([^\*])$/$1 /;
Randy Dunlapb9d973282009-06-09 08:50:38 -07001156 print ".BI \" " . $type . "\" " . $parameter . " \"" . "\"\n;\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 }
1158 print "\n.br\n";
1159 }
1160 print "};\n.br\n";
1161
Randy Dunlapc51d3da2006-06-25 05:49:14 -07001162 print ".SH Members\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 foreach $parameter (@{$args{'parameterlist'}}) {
1164 ($parameter =~ /^#/) && next;
1165
1166 my $parameter_name = $parameter;
1167 $parameter_name =~ s/\[.*//;
1168
Randy Dunlap3c308792007-05-08 00:24:39 -07001169 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Randy Dunlapb9d973282009-06-09 08:50:38 -07001170 print ".IP \"" . $parameter . "\" 12\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 output_highlight($args{'parameterdescs'}{$parameter_name});
1172 }
1173 foreach $section (@{$args{'sectionlist'}}) {
1174 print ".SH \"$section\"\n";
1175 output_highlight($args{'sections'}{$section});
1176 }
1177}
1178
1179##
1180# output typedef in man
1181sub output_typedef_man(%) {
1182 my %args = %{$_[0]};
1183 my ($parameter, $section);
1184
1185 print ".TH \"$args{'module'}\" 9 \"$args{'typedef'}\" \"$man_date\" \"API Manual\" LINUX\n";
1186
1187 print ".SH NAME\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001188 print "typedef " . $args{'typedef'} . " \\- " . $args{'purpose'} . "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 foreach $section (@{$args{'sectionlist'}}) {
1191 print ".SH \"$section\"\n";
1192 output_highlight($args{'sections'}{$section});
1193 }
1194}
1195
Johannes Bergb112e0f2007-10-24 15:08:48 -07001196sub output_blockhead_man(%) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 my %args = %{$_[0]};
1198 my ($parameter, $section);
1199 my $count;
1200
1201 print ".TH \"$args{'module'}\" 9 \"$args{'module'}\" \"$man_date\" \"API Manual\" LINUX\n";
1202
1203 foreach $section (@{$args{'sectionlist'}}) {
1204 print ".SH \"$section\"\n";
1205 output_highlight($args{'sections'}{$section});
1206 }
1207}
1208
1209##
1210# output in text
1211sub output_function_text(%) {
1212 my %args = %{$_[0]};
1213 my ($parameter, $section);
Randy Dunlapa21217d2007-02-10 01:46:04 -08001214 my $start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Randy Dunlapf47634b2006-07-01 04:36:36 -07001216 print "Name:\n\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001217 print $args{'function'} . " - " . $args{'purpose'} . "\n";
Randy Dunlapf47634b2006-07-01 04:36:36 -07001218
1219 print "\nSynopsis:\n\n";
Randy Dunlapa21217d2007-02-10 01:46:04 -08001220 if ($args{'functiontype'} ne "") {
Randy Dunlapb9d973282009-06-09 08:50:38 -07001221 $start = $args{'functiontype'} . " " . $args{'function'} . " (";
Randy Dunlapa21217d2007-02-10 01:46:04 -08001222 } else {
Randy Dunlapb9d973282009-06-09 08:50:38 -07001223 $start = $args{'function'} . " (";
Randy Dunlapa21217d2007-02-10 01:46:04 -08001224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 print $start;
Randy Dunlapa21217d2007-02-10 01:46:04 -08001226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 my $count = 0;
1228 foreach my $parameter (@{$args{'parameterlist'}}) {
1229 $type = $args{'parametertypes'}{$parameter};
1230 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1231 # pointer-to-function
Randy Dunlapb9d973282009-06-09 08:50:38 -07001232 print $1 . $parameter . ") (" . $2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 } else {
Randy Dunlapb9d973282009-06-09 08:50:38 -07001234 print $type . " " . $parameter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 }
1236 if ($count != $#{$args{'parameterlist'}}) {
1237 $count++;
1238 print ",\n";
1239 print " " x length($start);
1240 } else {
1241 print ");\n\n";
1242 }
1243 }
1244
1245 print "Arguments:\n\n";
1246 foreach $parameter (@{$args{'parameterlist'}}) {
1247 my $parameter_name = $parameter;
1248 $parameter_name =~ s/\[.*//;
1249
Randy Dunlapb9d973282009-06-09 08:50:38 -07001250 print $parameter . "\n\t" . $args{'parameterdescs'}{$parameter_name} . "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 }
1252 output_section_text(@_);
1253}
1254
1255#output sections in text
1256sub output_section_text(%) {
1257 my %args = %{$_[0]};
1258 my $section;
1259
1260 print "\n";
1261 foreach $section (@{$args{'sectionlist'}}) {
1262 print "$section:\n\n";
1263 output_highlight($args{'sections'}{$section});
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 print "\n\n";
1266}
1267
1268# output enum in text
1269sub output_enum_text(%) {
1270 my %args = %{$_[0]};
1271 my ($parameter);
1272 my $count;
1273 print "Enum:\n\n";
1274
Randy Dunlapb9d973282009-06-09 08:50:38 -07001275 print "enum " . $args{'enum'} . " - " . $args{'purpose'} . "\n\n";
1276 print "enum " . $args{'enum'} . " {\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 $count = 0;
1278 foreach $parameter (@{$args{'parameterlist'}}) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001279 print "\t$parameter";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 if ($count != $#{$args{'parameterlist'}}) {
1281 $count++;
1282 print ",";
1283 }
1284 print "\n";
1285 }
1286 print "};\n\n";
1287
1288 print "Constants:\n\n";
1289 foreach $parameter (@{$args{'parameterlist'}}) {
1290 print "$parameter\n\t";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001291 print $args{'parameterdescs'}{$parameter} . "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 }
1293
1294 output_section_text(@_);
1295}
1296
1297# output typedef in text
1298sub output_typedef_text(%) {
1299 my %args = %{$_[0]};
1300 my ($parameter);
1301 my $count;
1302 print "Typedef:\n\n";
1303
Randy Dunlapb9d973282009-06-09 08:50:38 -07001304 print "typedef " . $args{'typedef'} . " - " . $args{'purpose'} . "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 output_section_text(@_);
1306}
1307
1308# output struct as text
1309sub output_struct_text(%) {
1310 my %args = %{$_[0]};
1311 my ($parameter);
1312
Randy Dunlapb9d973282009-06-09 08:50:38 -07001313 print $args{'type'} . " " . $args{'struct'} . " - " . $args{'purpose'} . "\n\n";
1314 print $args{'type'} . " " . $args{'struct'} . " {\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 foreach $parameter (@{$args{'parameterlist'}}) {
1316 if ($parameter =~ /^#/) {
1317 print "$parameter\n";
1318 next;
1319 }
1320
1321 my $parameter_name = $parameter;
1322 $parameter_name =~ s/\[.*//;
1323
Randy Dunlap3c308792007-05-08 00:24:39 -07001324 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 $type = $args{'parametertypes'}{$parameter};
1326 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1327 # pointer-to-function
1328 print "\t$1 $parameter) ($2);\n";
1329 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
Randy Dunlap51f5a0c2007-07-19 01:48:24 -07001330 # bitfield
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 print "\t$1 $parameter$2;\n";
1332 } else {
Randy Dunlapb9d973282009-06-09 08:50:38 -07001333 print "\t" . $type . " " . $parameter . ";\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 }
1335 }
1336 print "};\n\n";
1337
1338 print "Members:\n\n";
1339 foreach $parameter (@{$args{'parameterlist'}}) {
1340 ($parameter =~ /^#/) && next;
1341
1342 my $parameter_name = $parameter;
1343 $parameter_name =~ s/\[.*//;
1344
Randy Dunlap3c308792007-05-08 00:24:39 -07001345 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 print "$parameter\n\t";
Randy Dunlapb9d973282009-06-09 08:50:38 -07001347 print $args{'parameterdescs'}{$parameter_name} . "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 }
1349 print "\n";
1350 output_section_text(@_);
1351}
1352
Johannes Bergb112e0f2007-10-24 15:08:48 -07001353sub output_blockhead_text(%) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 my %args = %{$_[0]};
1355 my ($parameter, $section);
1356
1357 foreach $section (@{$args{'sectionlist'}}) {
1358 print " $section:\n";
1359 print " -> ";
1360 output_highlight($args{'sections'}{$section});
1361 }
1362}
1363
1364##
Randy Dunlap27205742006-10-11 01:22:12 -07001365# generic output function for all types (function, struct/union, typedef, enum);
1366# calls the generated, variable output_ function name based on
1367# functype and output_mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368sub output_declaration {
1369 no strict 'refs';
1370 my $name = shift;
1371 my $functype = shift;
1372 my $func = "output_${functype}_$output_mode";
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001373 if (($function_only==0) ||
1374 ( $function_only == 1 && defined($function_table{$name})) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 ( $function_only == 2 && !defined($function_table{$name})))
1376 {
Randy Dunlap3c308792007-05-08 00:24:39 -07001377 &$func(@_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 $section_counter++;
1379 }
1380}
1381
1382##
Randy Dunlap27205742006-10-11 01:22:12 -07001383# generic output function - calls the right one based on current output mode.
Johannes Bergb112e0f2007-10-24 15:08:48 -07001384sub output_blockhead {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 no strict 'refs';
Randy Dunlapb9d973282009-06-09 08:50:38 -07001386 my $func = "output_blockhead_" . $output_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 &$func(@_);
1388 $section_counter++;
1389}
1390
1391##
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001392# takes a declaration (struct, union, enum, typedef) and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393# invokes the right handler. NOT called for functions.
1394sub dump_declaration($$) {
1395 no strict 'refs';
1396 my ($prototype, $file) = @_;
Randy Dunlapb9d973282009-06-09 08:50:38 -07001397 my $func = "dump_" . $decl_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 &$func(@_);
1399}
1400
1401sub dump_union($$) {
1402 dump_struct(@_);
1403}
1404
1405sub dump_struct($$) {
1406 my $x = shift;
1407 my $file = shift;
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001408 my $nested;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Randy Dunlap52dc5ae2009-04-30 15:08:53 -07001410 if ($x =~ /(struct|union)\s+(\w+)\s*{(.*)}/) {
1411 #my $decl_type = $1;
Randy Dunlap3c308792007-05-08 00:24:39 -07001412 $declaration_name = $2;
1413 my $members = $3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
1415 # ignore embedded structs or unions
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001416 $members =~ s/({.*})//g;
1417 $nested = $1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Martin Waitzaeec46b2005-11-13 16:08:13 -08001419 # ignore members marked private:
Randy Dunlap52dc5ae2009-04-30 15:08:53 -07001420 $members =~ s/\/\*\s*private:.*?\/\*\s*public:.*?\*\///gos;
1421 $members =~ s/\/\*\s*private:.*//gos;
Martin Waitzaeec46b2005-11-13 16:08:13 -08001422 # strip comments:
1423 $members =~ s/\/\*.*?\*\///gos;
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001424 $nested =~ s/\/\*.*?\*\///gos;
Randy Dunlapd960eea2009-06-29 14:54:11 -07001425 # strip kmemcheck_bitfield_{begin,end}.*;
1426 $members =~ s/kmemcheck_bitfield_.*?;//gos;
Randy Dunlapef5da592010-03-23 13:35:14 -07001427 # strip attributes
1428 $members =~ s/__aligned\s*\(\d+\)//gos;
Martin Waitzaeec46b2005-11-13 16:08:13 -08001429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 create_parameterlist($members, ';', $file);
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001431 check_sections($file, $declaration_name, "struct", $sectcheck, $struct_actual, $nested);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 output_declaration($declaration_name,
1434 'struct',
1435 {'struct' => $declaration_name,
1436 'module' => $modulename,
1437 'parameterlist' => \@parameterlist,
1438 'parameterdescs' => \%parameterdescs,
1439 'parametertypes' => \%parametertypes,
1440 'sectionlist' => \@sectionlist,
1441 'sections' => \%sections,
1442 'purpose' => $declaration_purpose,
1443 'type' => $decl_type
1444 });
1445 }
1446 else {
Randy Dunlap3c308792007-05-08 00:24:39 -07001447 print STDERR "Error(${file}:$.): Cannot parse struct or union!\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 ++$errors;
1449 }
1450}
1451
1452sub dump_enum($$) {
1453 my $x = shift;
1454 my $file = shift;
1455
Martin Waitzaeec46b2005-11-13 16:08:13 -08001456 $x =~ s@/\*.*?\*/@@gos; # strip comments.
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001457 $x =~ s/^#\s*define\s+.*$//; # strip #define macros inside enums
1458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 if ($x =~ /enum\s+(\w+)\s*{(.*)}/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001460 $declaration_name = $1;
1461 my $members = $2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463 foreach my $arg (split ',', $members) {
1464 $arg =~ s/^\s*(\w+).*/$1/;
1465 push @parameterlist, $arg;
1466 if (!$parameterdescs{$arg}) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001467 $parameterdescs{$arg} = $undescribed;
1468 print STDERR "Warning(${file}:$.): Enum value '$arg' ".
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 "not described in enum '$declaration_name'\n";
1470 }
1471
1472 }
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 output_declaration($declaration_name,
1475 'enum',
1476 {'enum' => $declaration_name,
1477 'module' => $modulename,
1478 'parameterlist' => \@parameterlist,
1479 'parameterdescs' => \%parameterdescs,
1480 'sectionlist' => \@sectionlist,
1481 'sections' => \%sections,
1482 'purpose' => $declaration_purpose
1483 });
1484 }
1485 else {
Randy Dunlap3c308792007-05-08 00:24:39 -07001486 print STDERR "Error(${file}:$.): Cannot parse enum!\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 ++$errors;
1488 }
1489}
1490
1491sub dump_typedef($$) {
1492 my $x = shift;
1493 my $file = shift;
1494
Martin Waitzaeec46b2005-11-13 16:08:13 -08001495 $x =~ s@/\*.*?\*/@@gos; # strip comments.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 while (($x =~ /\(*.\)\s*;$/) || ($x =~ /\[*.\]\s*;$/)) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001497 $x =~ s/\(*.\)\s*;$/;/;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 $x =~ s/\[*.\]\s*;$/;/;
1499 }
1500
1501 if ($x =~ /typedef.*\s+(\w+)\s*;/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001502 $declaration_name = $1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
1504 output_declaration($declaration_name,
1505 'typedef',
1506 {'typedef' => $declaration_name,
1507 'module' => $modulename,
1508 'sectionlist' => \@sectionlist,
1509 'sections' => \%sections,
1510 'purpose' => $declaration_purpose
1511 });
1512 }
1513 else {
Randy Dunlap3c308792007-05-08 00:24:39 -07001514 print STDERR "Error(${file}:$.): Cannot parse typedef!\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 ++$errors;
1516 }
1517}
1518
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001519sub save_struct_actual($) {
1520 my $actual = shift;
1521
1522 # strip all spaces from the actual param so that it looks like one string item
1523 $actual =~ s/\s*//g;
1524 $struct_actual = $struct_actual . $actual . " ";
1525}
1526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527sub create_parameterlist($$$) {
1528 my $args = shift;
1529 my $splitter = shift;
1530 my $file = shift;
1531 my $type;
1532 my $param;
1533
Martin Waitza6d3fe72006-01-09 20:53:55 -08001534 # temporarily replace commas inside function pointer definition
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 while ($args =~ /(\([^\),]+),/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001536 $args =~ s/(\([^\),]+),/$1#/g;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 }
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 foreach my $arg (split($splitter, $args)) {
1540 # strip comments
1541 $arg =~ s/\/\*.*\*\///;
Randy Dunlap3c308792007-05-08 00:24:39 -07001542 # strip leading/trailing spaces
1543 $arg =~ s/^\s*//;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 $arg =~ s/\s*$//;
1545 $arg =~ s/\s+/ /;
1546
1547 if ($arg =~ /^#/) {
1548 # Treat preprocessor directive as a typeless variable just to fill
1549 # corresponding data structures "correctly". Catch it later in
1550 # output_* subs.
1551 push_parameter($arg, "", $file);
Richard Kennedy00d62962008-02-23 15:24:01 -08001552 } elsif ($arg =~ m/\(.+\)\s*\(/) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 # pointer-to-function
1554 $arg =~ tr/#/,/;
Richard Kennedy00d62962008-02-23 15:24:01 -08001555 $arg =~ m/[^\(]+\(\*?\s*(\w*)\s*\)/;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 $param = $1;
1557 $type = $arg;
Richard Kennedy00d62962008-02-23 15:24:01 -08001558 $type =~ s/([^\(]+\(\*?)\s*$param/$1/;
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001559 save_struct_actual($param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 push_parameter($param, $type, $file);
Martin Waitzaeec46b2005-11-13 16:08:13 -08001561 } elsif ($arg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 $arg =~ s/\s*:\s*/:/g;
1563 $arg =~ s/\s*\[/\[/g;
1564
1565 my @args = split('\s*,\s*', $arg);
1566 if ($args[0] =~ m/\*/) {
1567 $args[0] =~ s/(\*+)\s*/ $1/;
1568 }
Borislav Petkov884f2812007-05-08 00:29:05 -07001569
1570 my @first_arg;
1571 if ($args[0] =~ /^(.*\s+)(.*?\[.*\].*)$/) {
1572 shift @args;
1573 push(@first_arg, split('\s+', $1));
1574 push(@first_arg, $2);
1575 } else {
1576 @first_arg = split('\s+', shift @args);
1577 }
1578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 unshift(@args, pop @first_arg);
1580 $type = join " ", @first_arg;
1581
1582 foreach $param (@args) {
1583 if ($param =~ m/^(\*+)\s*(.*)/) {
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001584 save_struct_actual($2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 push_parameter($2, "$type $1", $file);
1586 }
1587 elsif ($param =~ m/(.*?):(\d+)/) {
Randy Dunlap7b978872008-05-16 15:45:52 -07001588 if ($type ne "") { # skip unnamed bit-fields
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001589 save_struct_actual($1);
Randy Dunlap7b978872008-05-16 15:45:52 -07001590 push_parameter($1, "$type:$2", $file)
1591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 }
1593 else {
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001594 save_struct_actual($param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 push_parameter($param, $type, $file);
1596 }
1597 }
1598 }
1599 }
1600}
1601
1602sub push_parameter($$$) {
1603 my $param = shift;
1604 my $type = shift;
1605 my $file = shift;
1606
Randy Dunlap5f8c7c92007-07-19 01:48:24 -07001607 if (($anon_struct_union == 1) && ($type eq "") &&
1608 ($param eq "}")) {
1609 return; # ignore the ending }; from anon. struct/union
1610 }
1611
1612 $anon_struct_union = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 my $param_name = $param;
1614 $param_name =~ s/\[.*//;
1615
Martin Waitza6d3fe72006-01-09 20:53:55 -08001616 if ($type eq "" && $param =~ /\.\.\.$/)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 {
Randy Dunlapced69092008-12-01 13:14:03 -08001618 if (!defined $parameterdescs{$param} || $parameterdescs{$param} eq "") {
1619 $parameterdescs{$param} = "variable arguments";
1620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 }
1622 elsif ($type eq "" && ($param eq "" or $param eq "void"))
1623 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 $param="void";
1625 $parameterdescs{void} = "no arguments";
1626 }
Randy Dunlap134fe012006-12-22 01:10:50 -08001627 elsif ($type eq "" && ($param eq "struct" or $param eq "union"))
1628 # handle unnamed (anonymous) union or struct:
1629 {
1630 $type = $param;
Randy Dunlap5f8c7c92007-07-19 01:48:24 -07001631 $param = "{unnamed_" . $param . "}";
Randy Dunlap134fe012006-12-22 01:10:50 -08001632 $parameterdescs{$param} = "anonymous\n";
Randy Dunlap5f8c7c92007-07-19 01:48:24 -07001633 $anon_struct_union = 1;
Randy Dunlap134fe012006-12-22 01:10:50 -08001634 }
1635
Martin Waitza6d3fe72006-01-09 20:53:55 -08001636 # warn if parameter has no description
Randy Dunlap134fe012006-12-22 01:10:50 -08001637 # (but ignore ones starting with # as these are not parameters
1638 # but inline preprocessor statements);
1639 # also ignore unnamed structs/unions;
Randy Dunlap5f8c7c92007-07-19 01:48:24 -07001640 if (!$anon_struct_union) {
Martin Waitza6d3fe72006-01-09 20:53:55 -08001641 if (!defined $parameterdescs{$param_name} && $param_name !~ /^#/) {
1642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 $parameterdescs{$param_name} = $undescribed;
1644
1645 if (($type eq 'function') || ($type eq 'enum')) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001646 print STDERR "Warning(${file}:$.): Function parameter ".
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 "or member '$param' not " .
1648 "described in '$declaration_name'\n";
1649 }
Randy Dunlapb9d973282009-06-09 08:50:38 -07001650 print STDERR "Warning(${file}:$.):" .
Randy Dunlap3c308792007-05-08 00:24:39 -07001651 " No description found for parameter '$param'\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 ++$warnings;
Randy Dunlap3c308792007-05-08 00:24:39 -07001653 }
1654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Randy Dunlape34e7db2009-06-17 17:37:47 -07001656 # strip spaces from $param so that it is one continous string
1657 # on @parameterlist;
1658 # this fixes a problem where check_sections() cannot find
1659 # a parameter like "addr[6 + 2]" because it actually appears
1660 # as "addr[6", "+", "2]" on the parameter list;
1661 # but it's better to maintain the param string unchanged for output,
1662 # so just weaken the string compare in check_sections() to ignore
1663 # "[blah" in a parameter string;
1664 ###$param =~ s/\s*//g;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 push @parameterlist, $param;
1666 $parametertypes{$param} = $type;
1667}
1668
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001669sub check_sections($$$$$$) {
1670 my ($file, $decl_name, $decl_type, $sectcheck, $prmscheck, $nested) = @_;
1671 my @sects = split ' ', $sectcheck;
1672 my @prms = split ' ', $prmscheck;
1673 my $err;
1674 my ($px, $sx);
1675 my $prm_clean; # strip trailing "[array size]" and/or beginning "*"
1676
1677 foreach $sx (0 .. $#sects) {
1678 $err = 1;
1679 foreach $px (0 .. $#prms) {
1680 $prm_clean = $prms[$px];
1681 $prm_clean =~ s/\[.*\]//;
1682 $prm_clean =~ s/__attribute__\s*\(\([a-z,_\*\s\(\)]*\)\)//;
Randy Dunlape34e7db2009-06-17 17:37:47 -07001683 # ignore array size in a parameter string;
1684 # however, the original param string may contain
1685 # spaces, e.g.: addr[6 + 2]
1686 # and this appears in @prms as "addr[6" since the
1687 # parameter list is split at spaces;
1688 # hence just ignore "[..." for the sections check;
1689 $prm_clean =~ s/\[.*//;
1690
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001691 ##$prm_clean =~ s/^\**//;
1692 if ($prm_clean eq $sects[$sx]) {
1693 $err = 0;
1694 last;
1695 }
1696 }
1697 if ($err) {
1698 if ($decl_type eq "function") {
1699 print STDERR "Warning(${file}:$.): " .
1700 "Excess function parameter " .
1701 "'$sects[$sx]' " .
1702 "description in '$decl_name'\n";
1703 ++$warnings;
1704 } else {
1705 if ($nested !~ m/\Q$sects[$sx]\E/) {
1706 print STDERR "Warning(${file}:$.): " .
1707 "Excess struct/union/enum/typedef member " .
1708 "'$sects[$sx]' " .
1709 "description in '$decl_name'\n";
1710 ++$warnings;
1711 }
1712 }
1713 }
1714 }
1715}
1716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717##
1718# takes a function prototype and the name of the current file being
1719# processed and spits out all the details stored in the global
1720# arrays/hashes.
1721sub dump_function($$) {
1722 my $prototype = shift;
1723 my $file = shift;
1724
1725 $prototype =~ s/^static +//;
1726 $prototype =~ s/^extern +//;
Pavel Pisa4dc3b162005-05-01 08:59:25 -07001727 $prototype =~ s/^asmlinkage +//;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 $prototype =~ s/^inline +//;
1729 $prototype =~ s/^__inline__ +//;
Randy Dunlap32e79402006-10-11 01:22:10 -07001730 $prototype =~ s/^__inline +//;
1731 $prototype =~ s/^__always_inline +//;
1732 $prototype =~ s/^noinline +//;
Randy Dunlap0129a052006-07-30 03:03:41 -07001733 $prototype =~ s/__devinit +//;
Randy Dunlap74fc5c62008-06-19 16:03:29 -07001734 $prototype =~ s/__init +//;
Randy Dunlap20072202010-03-23 13:35:24 -07001735 $prototype =~ s/__init_or_module +//;
Randy Dunlap890c78c2008-10-25 17:06:43 -07001736 $prototype =~ s/^#\s*define\s+//; #ak added
Randy Dunlap328d2442007-02-28 20:12:10 -08001737 $prototype =~ s/__attribute__\s*\(\([a-z,]*\)\)//;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
1739 # Yes, this truly is vile. We are looking for:
1740 # 1. Return type (may be nothing if we're looking at a macro)
1741 # 2. Function name
1742 # 3. Function parameters.
1743 #
1744 # All the while we have to watch out for function pointer parameters
1745 # (which IIRC is what the two sections are for), C types (these
1746 # regexps don't even start to express all the possibilities), and
1747 # so on.
1748 #
1749 # If you mess with these regexps, it's a good idea to check that
1750 # the following functions' documentation still comes out right:
1751 # - parport_register_device (function pointer parameters)
1752 # - atomic_set (macro)
Martin Waitz9598f912006-02-01 03:06:55 -08001753 # - pci_match_device, __copy_to_user (long return type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
1755 if ($prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1756 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1757 $prototype =~ m/^(\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1758 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
Randy Dunlap94b3e032008-02-07 00:13:26 -08001759 $prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1761 $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1762 $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1763 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1764 $prototype =~ m/^(\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1765 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1766 $prototype =~ m/^(\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1767 $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
Martin Waitz9598f912006-02-01 03:06:55 -08001768 $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1769 $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
Randy Dunlap412ecd772007-02-10 22:47:33 -08001770 $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1771 $prototype =~ m/^(\w+\s+\w+\s*\*\s*\w+\s*\*\s*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 $return_type = $1;
1773 $declaration_name = $2;
1774 my $args = $3;
1775
1776 create_parameterlist($args, ',', $file);
1777 } else {
1778 print STDERR "Error(${file}:$.): cannot understand prototype: '$prototype'\n";
1779 ++$errors;
1780 return;
1781 }
1782
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001783 my $prms = join " ", @parameterlist;
1784 check_sections($file, $declaration_name, "function", $sectcheck, $prms, "");
1785
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001786 output_declaration($declaration_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 'function',
1788 {'function' => $declaration_name,
1789 'module' => $modulename,
1790 'functiontype' => $return_type,
1791 'parameterlist' => \@parameterlist,
1792 'parameterdescs' => \%parameterdescs,
1793 'parametertypes' => \%parametertypes,
1794 'sectionlist' => \@sectionlist,
1795 'sections' => \%sections,
1796 'purpose' => $declaration_purpose
1797 });
1798}
1799
1800sub process_file($);
1801
1802# Read the file that maps relative names to absolute names for
1803# separate source and object directories and for shadow trees.
1804if (open(SOURCE_MAP, "<.tmp_filelist.txt")) {
1805 my ($relname, $absname);
1806 while(<SOURCE_MAP>) {
1807 chop();
1808 ($relname, $absname) = (split())[0..1];
1809 $relname =~ s:^/+::;
1810 $source_map{$relname} = $absname;
1811 }
1812 close(SOURCE_MAP);
1813}
1814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815foreach (@ARGV) {
1816 chomp;
1817 process_file($_);
1818}
1819if ($verbose && $errors) {
1820 print STDERR "$errors errors\n";
1821}
1822if ($verbose && $warnings) {
1823 print STDERR "$warnings warnings\n";
1824}
1825
1826exit($errors);
1827
1828sub reset_state {
1829 $function = "";
1830 %constants = ();
1831 %parameterdescs = ();
1832 %parametertypes = ();
1833 @parameterlist = ();
1834 %sections = ();
1835 @sectionlist = ();
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001836 $sectcheck = "";
1837 $struct_actual = "";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 $prototype = "";
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001839
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 $state = 0;
1841}
1842
Jason Baron56afb0f2009-04-30 13:29:36 -04001843sub tracepoint_munge($) {
1844 my $file = shift;
1845 my $tracepointname = 0;
1846 my $tracepointargs = 0;
1847
Jason Baron3a9089f2009-12-01 12:18:49 -05001848 if ($prototype =~ m/TRACE_EVENT\((.*?),/) {
Jason Baron56afb0f2009-04-30 13:29:36 -04001849 $tracepointname = $1;
1850 }
Jason Baron3a9089f2009-12-01 12:18:49 -05001851 if ($prototype =~ m/DEFINE_SINGLE_EVENT\((.*?),/) {
1852 $tracepointname = $1;
1853 }
1854 if ($prototype =~ m/DEFINE_EVENT\((.*?),(.*?),/) {
1855 $tracepointname = $2;
1856 }
1857 $tracepointname =~ s/^\s+//; #strip leading whitespace
1858 if ($prototype =~ m/TP_PROTO\((.*?)\)/) {
Jason Baron56afb0f2009-04-30 13:29:36 -04001859 $tracepointargs = $1;
1860 }
1861 if (($tracepointname eq 0) || ($tracepointargs eq 0)) {
1862 print STDERR "Warning(${file}:$.): Unrecognized tracepoint format: \n".
1863 "$prototype\n";
1864 } else {
1865 $prototype = "static inline void trace_$tracepointname($tracepointargs)";
1866 }
1867}
1868
Randy Dunlapb4870bc2009-02-11 13:04:33 -08001869sub syscall_munge() {
1870 my $void = 0;
1871
1872 $prototype =~ s@[\r\n\t]+@ @gos; # strip newlines/CR's/tabs
1873## if ($prototype =~ m/SYSCALL_DEFINE0\s*\(\s*(a-zA-Z0-9_)*\s*\)/) {
1874 if ($prototype =~ m/SYSCALL_DEFINE0/) {
1875 $void = 1;
1876## $prototype = "long sys_$1(void)";
1877 }
1878
1879 $prototype =~ s/SYSCALL_DEFINE.*\(/long sys_/; # fix return type & func name
1880 if ($prototype =~ m/long (sys_.*?),/) {
1881 $prototype =~ s/,/\(/;
1882 } elsif ($void) {
1883 $prototype =~ s/\)/\(void\)/;
1884 }
1885
1886 # now delete all of the odd-number commas in $prototype
1887 # so that arg types & arg names don't have a comma between them
1888 my $count = 0;
1889 my $len = length($prototype);
1890 if ($void) {
1891 $len = 0; # skip the for-loop
1892 }
1893 for (my $ix = 0; $ix < $len; $ix++) {
1894 if (substr($prototype, $ix, 1) eq ',') {
1895 $count++;
1896 if ($count % 2 == 1) {
1897 substr($prototype, $ix, 1) = ' ';
1898 }
1899 }
1900 }
1901}
1902
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001903sub process_state3_function($$) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 my $x = shift;
1905 my $file = shift;
1906
Randy Dunlap51f5a0c2007-07-19 01:48:24 -07001907 $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
1908
Randy Dunlap890c78c2008-10-25 17:06:43 -07001909 if ($x =~ m#\s*/\*\s+MACDOC\s*#io || ($x =~ /^#/ && $x !~ /^#\s*define/)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 # do nothing
1911 }
1912 elsif ($x =~ /([^\{]*)/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001913 $prototype .= $1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 }
Randy Dunlapb4870bc2009-02-11 13:04:33 -08001915
Randy Dunlap890c78c2008-10-25 17:06:43 -07001916 if (($x =~ /\{/) || ($x =~ /\#\s*define/) || ($x =~ /;/)) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001917 $prototype =~ s@/\*.*?\*/@@gos; # strip comments.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 $prototype =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1919 $prototype =~ s@^\s+@@gos; # strip leading spaces
Randy Dunlapb4870bc2009-02-11 13:04:33 -08001920 if ($prototype =~ /SYSCALL_DEFINE/) {
1921 syscall_munge();
1922 }
Jason Baron3a9089f2009-12-01 12:18:49 -05001923 if ($prototype =~ /TRACE_EVENT/ || $prototype =~ /DEFINE_EVENT/ ||
1924 $prototype =~ /DEFINE_SINGLE_EVENT/)
1925 {
Jason Baron56afb0f2009-04-30 13:29:36 -04001926 tracepoint_munge($file);
1927 }
Randy Dunlapb4870bc2009-02-11 13:04:33 -08001928 dump_function($prototype, $file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 reset_state();
1930 }
1931}
1932
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001933sub process_state3_type($$) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 my $x = shift;
1935 my $file = shift;
1936
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 $x =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1938 $x =~ s@^\s+@@gos; # strip leading spaces
1939 $x =~ s@\s+$@@gos; # strip trailing spaces
Randy Dunlap51f5a0c2007-07-19 01:48:24 -07001940 $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
1941
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 if ($x =~ /^#/) {
1943 # To distinguish preprocessor directive from regular declaration later.
1944 $x .= ";";
1945 }
1946
1947 while (1) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001948 if ( $x =~ /([^{};]*)([{};])(.*)/ ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 $prototype .= $1 . $2;
1950 ($2 eq '{') && $brcount++;
1951 ($2 eq '}') && $brcount--;
1952 if (($2 eq ';') && ($brcount == 0)) {
Randy Dunlapb9d973282009-06-09 08:50:38 -07001953 dump_declaration($prototype, $file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 reset_state();
Randy Dunlap3c308792007-05-08 00:24:39 -07001955 last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 }
1957 $x = $3;
Randy Dunlap3c308792007-05-08 00:24:39 -07001958 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 $prototype .= $x;
1960 last;
1961 }
1962 }
1963}
1964
Randy Dunlap6b5b55f2007-10-16 23:31:20 -07001965# xml_escape: replace <, >, and & in the text stream;
1966#
1967# however, formatting controls that are generated internally/locally in the
1968# kernel-doc script are not escaped here; instead, they begin life like
1969# $blankline_html (4 of '\' followed by a mnemonic + ':'), then these strings
1970# are converted to their mnemonic-expected output, without the 4 * '\' & ':',
1971# just before actual output; (this is done by local_unescape())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972sub xml_escape($) {
1973 my $text = shift;
Randy Dunlapecfb2512006-06-25 05:49:13 -07001974 if (($output_mode eq "text") || ($output_mode eq "man")) {
1975 return $text;
1976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 $text =~ s/\&/\\\\\\amp;/g;
1978 $text =~ s/\</\\\\\\lt;/g;
1979 $text =~ s/\>/\\\\\\gt;/g;
1980 return $text;
1981}
1982
Randy Dunlap6b5b55f2007-10-16 23:31:20 -07001983# convert local escape strings to html
1984# local escape strings look like: '\\\\menmonic:' (that's 4 backslashes)
1985sub local_unescape($) {
1986 my $text = shift;
1987 if (($output_mode eq "text") || ($output_mode eq "man")) {
1988 return $text;
1989 }
1990 $text =~ s/\\\\\\\\lt:/</g;
1991 $text =~ s/\\\\\\\\gt:/>/g;
1992 return $text;
1993}
1994
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995sub process_file($) {
Randy Dunlap2283a112005-07-07 15:39:26 -07001996 my $file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 my $identifier;
1998 my $func;
Randy Dunlapa21217d2007-02-10 01:46:04 -08001999 my $descr;
Johannes Weiner64231332009-09-17 19:26:53 -07002000 my $in_purpose = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 my $initial_section_counter = $section_counter;
2002
Randy Dunlap2283a112005-07-07 15:39:26 -07002003 if (defined($ENV{'SRCTREE'})) {
2004 $file = "$ENV{'SRCTREE'}" . "/" . "@_";
2005 }
2006 else {
2007 $file = "@_";
2008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 if (defined($source_map{$file})) {
2010 $file = $source_map{$file};
2011 }
2012
2013 if (!open(IN,"<$file")) {
2014 print STDERR "Error: Cannot open file $file\n";
2015 ++$errors;
2016 return;
2017 }
2018
Ilya Dryomova9e73142010-02-26 13:05:47 -08002019 $. = 1;
2020
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 $section_counter = 0;
2022 while (<IN>) {
2023 if ($state == 0) {
2024 if (/$doc_start/o) {
2025 $state = 1; # next line is always the function name
Randy Dunlap850622d2006-06-25 05:48:55 -07002026 $in_doc_sect = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 }
2028 } elsif ($state == 1) { # this line is the function name (always)
2029 if (/$doc_block/o) {
2030 $state = 4;
2031 $contents = "";
2032 if ( $1 eq "" ) {
2033 $section = $section_intro;
2034 } else {
2035 $section = $1;
2036 }
Randy Dunlap3c308792007-05-08 00:24:39 -07002037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 elsif (/$doc_decl/o) {
2039 $identifier = $1;
2040 if (/\s*([\w\s]+?)\s*-/) {
2041 $identifier = $1;
2042 }
2043
2044 $state = 2;
2045 if (/-(.*)/) {
Randy Dunlap51f5a0c2007-07-19 01:48:24 -07002046 # strip leading/trailing/multiple spaces
Randy Dunlapa21217d2007-02-10 01:46:04 -08002047 $descr= $1;
2048 $descr =~ s/^\s*//;
2049 $descr =~ s/\s*$//;
2050 $descr =~ s/\s+/ /;
2051 $declaration_purpose = xml_escape($descr);
Johannes Weiner64231332009-09-17 19:26:53 -07002052 $in_purpose = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 } else {
2054 $declaration_purpose = "";
2055 }
Randy Dunlap77cc23b2008-02-07 00:13:42 -08002056
2057 if (($declaration_purpose eq "") && $verbose) {
2058 print STDERR "Warning(${file}:$.): missing initial short description on line:\n";
2059 print STDERR $_;
2060 ++$warnings;
2061 }
2062
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 if ($identifier =~ m/^struct/) {
2064 $decl_type = 'struct';
2065 } elsif ($identifier =~ m/^union/) {
2066 $decl_type = 'union';
2067 } elsif ($identifier =~ m/^enum/) {
2068 $decl_type = 'enum';
2069 } elsif ($identifier =~ m/^typedef/) {
2070 $decl_type = 'typedef';
2071 } else {
2072 $decl_type = 'function';
2073 }
2074
2075 if ($verbose) {
2076 print STDERR "Info(${file}:$.): Scanning doc for $identifier\n";
2077 }
2078 } else {
2079 print STDERR "Warning(${file}:$.): Cannot understand $_ on line $.",
2080 " - I thought it was a doc line\n";
2081 ++$warnings;
2082 $state = 0;
2083 }
2084 } elsif ($state == 2) { # look for head: lines, and include content
2085 if (/$doc_sect/o) {
2086 $newsection = $1;
2087 $newcontents = $2;
2088
Randy Dunlap792aa2f2008-02-07 00:13:41 -08002089 if (($contents ne "") && ($contents ne "\n")) {
Randy Dunlap850622d2006-06-25 05:48:55 -07002090 if (!$in_doc_sect && $verbose) {
2091 print STDERR "Warning(${file}:$.): contents before sections\n";
2092 ++$warnings;
2093 }
Randy Dunlap94dc7ad2008-04-28 02:16:34 -07002094 dump_section($file, $section, xml_escape($contents));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 $section = $section_default;
2096 }
2097
Randy Dunlap850622d2006-06-25 05:48:55 -07002098 $in_doc_sect = 1;
Johannes Weiner64231332009-09-17 19:26:53 -07002099 $in_purpose = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 $contents = $newcontents;
2101 if ($contents ne "") {
Randy Dunlap27205742006-10-11 01:22:12 -07002102 while ((substr($contents, 0, 1) eq " ") ||
2103 substr($contents, 0, 1) eq "\t") {
2104 $contents = substr($contents, 1);
Randy Dunlap05189492006-06-25 05:47:47 -07002105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 $contents .= "\n";
2107 }
2108 $section = $newsection;
2109 } elsif (/$doc_end/) {
2110
Randy Dunlap4c98eca2010-03-10 15:22:02 -08002111 if (($contents ne "") && ($contents ne "\n")) {
Randy Dunlap94dc7ad2008-04-28 02:16:34 -07002112 dump_section($file, $section, xml_escape($contents));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 $section = $section_default;
2114 $contents = "";
2115 }
Randy Dunlap46b958e2008-04-28 02:16:35 -07002116 # look for doc_com + <text> + doc_end:
2117 if ($_ =~ m'\s*\*\s*[a-zA-Z_0-9:\.]+\*/') {
2118 print STDERR "Warning(${file}:$.): suspicious ending line: $_";
2119 ++$warnings;
2120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
2122 $prototype = "";
2123 $state = 3;
2124 $brcount = 0;
Randy Dunlap232acbc2006-06-25 05:47:48 -07002125# print STDERR "end of doc comment, looking for prototype\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 } elsif (/$doc_content/) {
2127 # miguel-style comment kludge, look for blank lines after
2128 # @parameter line to signify start of description
Johannes Weiner64231332009-09-17 19:26:53 -07002129 if ($1 eq "") {
2130 if ($section =~ m/^@/ || $section eq $section_context) {
2131 dump_section($file, $section, xml_escape($contents));
2132 $section = $section_default;
2133 $contents = "";
2134 } else {
2135 $contents .= "\n";
2136 }
2137 $in_purpose = 0;
2138 } elsif ($in_purpose == 1) {
2139 # Continued declaration purpose
2140 chomp($declaration_purpose);
2141 $declaration_purpose .= " " . xml_escape($1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 } else {
Randy Dunlapb9d973282009-06-09 08:50:38 -07002143 $contents .= $1 . "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 }
2145 } else {
2146 # i dont know - bad line? ignore.
Randy Dunlap3c3b8092006-02-01 03:06:58 -08002147 print STDERR "Warning(${file}:$.): bad line: $_";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 ++$warnings;
2149 }
Randy Dunlap232acbc2006-06-25 05:47:48 -07002150 } elsif ($state == 3) { # scanning for function '{' (end of prototype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 if ($decl_type eq 'function') {
Randy Dunlap3c308792007-05-08 00:24:39 -07002152 process_state3_function($_, $file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 } else {
Randy Dunlap3c308792007-05-08 00:24:39 -07002154 process_state3_type($_, $file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 }
2156 } elsif ($state == 4) {
2157 # Documentation block
Randy Dunlap3c308792007-05-08 00:24:39 -07002158 if (/$doc_block/) {
Randy Dunlap94dc7ad2008-04-28 02:16:34 -07002159 dump_doc_section($file, $section, xml_escape($contents));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 $contents = "";
2161 $function = "";
2162 %constants = ();
2163 %parameterdescs = ();
2164 %parametertypes = ();
2165 @parameterlist = ();
2166 %sections = ();
2167 @sectionlist = ();
2168 $prototype = "";
2169 if ( $1 eq "" ) {
2170 $section = $section_intro;
2171 } else {
2172 $section = $1;
2173 }
Randy Dunlap3c308792007-05-08 00:24:39 -07002174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 elsif (/$doc_end/)
2176 {
Randy Dunlap94dc7ad2008-04-28 02:16:34 -07002177 dump_doc_section($file, $section, xml_escape($contents));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 $contents = "";
2179 $function = "";
2180 %constants = ();
2181 %parameterdescs = ();
2182 %parametertypes = ();
2183 @parameterlist = ();
2184 %sections = ();
2185 @sectionlist = ();
2186 $prototype = "";
2187 $state = 0;
2188 }
2189 elsif (/$doc_content/)
2190 {
2191 if ( $1 eq "" )
2192 {
2193 $contents .= $blankline;
2194 }
2195 else
2196 {
2197 $contents .= $1 . "\n";
Randy Dunlap3c3b8092006-02-01 03:06:58 -08002198 }
Randy Dunlap3c308792007-05-08 00:24:39 -07002199 }
2200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 }
2202 if ($initial_section_counter == $section_counter) {
2203 print STDERR "Warning(${file}): no structured comments found\n";
2204 if ($output_mode eq "xml") {
2205 # The template wants at least one RefEntry here; make one.
2206 print "<refentry>\n";
2207 print " <refnamediv>\n";
2208 print " <refname>\n";
2209 print " ${file}\n";
2210 print " </refname>\n";
2211 print " <refpurpose>\n";
2212 print " Document generation inconsistency\n";
2213 print " </refpurpose>\n";
2214 print " </refnamediv>\n";
2215 print " <refsect1>\n";
2216 print " <title>\n";
2217 print " Oops\n";
2218 print " </title>\n";
2219 print " <warning>\n";
2220 print " <para>\n";
2221 print " The template for this document tried to insert\n";
2222 print " the structured comment from the file\n";
2223 print " <filename>${file}</filename> at this point,\n";
2224 print " but none was found.\n";
2225 print " This dummy section is inserted to allow\n";
2226 print " generation to continue.\n";
2227 print " </para>\n";
2228 print " </warning>\n";
2229 print " </refsect1>\n";
2230 print "</refentry>\n";
2231 }
2232 }
2233}