blob: 263d04ab2d947398ee443f3b4ed1401700e0c307 [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 Dunlap6b5b55f2007-10-16 23:31:20 -07008## Copyright (C) 2005-2007 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
16# w.o. 03-11-2000: added the '-filelist' option.
17
18# 18/01/2001 - Cleanups
19# Functions prototyped as foo(void) same as foo()
20# Stop eval'ing where we don't need to.
21# -- huggie@earth.li
22
23# 27/06/2001 - Allowed whitespace after initial "/**" and
24# allowed comments before function declarations.
25# -- Christian Kreibich <ck@whoop.org>
26
27# Still to do:
28# - add perldoc documentation
29# - Look more closely at some of the scarier bits :)
30
31# 26/05/2001 - Support for separate source and object trees.
32# Return error code.
33# Keith Owens <kaos@ocs.com.au>
34
35# 23/09/2001 - Added support for typedefs, structs, enums and unions
36# Support for Context section; can be terminated using empty line
37# Small fixes (like spaces vs. \s in regex)
38# -- Tim Jansen <tim@tjansen.de>
39
40
41#
42# This will read a 'c' file and scan for embedded comments in the
43# style of gnome comments (+minor extensions - see below).
44#
45
46# Note: This only supports 'c'.
47
48# usage:
Johannes Berg4b445952007-10-24 15:08:48 -070049# kernel-doc [ -docbook | -html | -text | -man ] [ -no-doc-sections ]
Linus Torvalds1da177e2005-04-16 15:20:36 -070050# [ -function funcname [ -function funcname ...] ] c file(s)s > outputfile
51# or
52# [ -nofunction funcname [ -function funcname ...] ] c file(s)s > outputfile
53#
54# Set output format using one of -docbook -html -text or -man. Default is man.
55#
Johannes Berg4b445952007-10-24 15:08:48 -070056# -no-doc-sections
57# Do not output DOC: sections
58#
Linus Torvalds1da177e2005-04-16 15:20:36 -070059# -function funcname
Johannes Bergb112e0f2007-10-24 15:08:48 -070060# If set, then only generate documentation for the given function(s) or
61# DOC: section titles. All other functions and DOC: sections are ignored.
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#
63# -nofunction funcname
Johannes Bergb112e0f2007-10-24 15:08:48 -070064# If set, then only generate documentation for the other function(s)/DOC:
65# sections. Cannot be used together with -function (yes, that's a bug --
66# perl hackers can fix it 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#
68# c files - list of 'c' files to process
69#
70# All output goes to stdout, with errors to stderr.
71
72#
73# format of comments.
74# In the following table, (...)? signifies optional structure.
75# (...)* signifies 0 or more structure elements
76# /**
77# * function_name(:)? (- short description)?
78# (* @parameterx: (description of parameter x)?)*
79# (* a blank line)?
80# * (Description:)? (Description of function)?
81# * (section header: (section description)? )*
82# (*)?*/
83#
84# So .. the trivial example would be:
85#
86# /**
87# * my_function
88# **/
89#
Randy Dunlap891dcd22007-02-10 01:45:53 -080090# If the Description: header tag is omitted, then there must be a blank line
Linus Torvalds1da177e2005-04-16 15:20:36 -070091# after the last parameter specification.
92# e.g.
93# /**
94# * my_function - does my stuff
95# * @my_arg: its mine damnit
96# *
Randy Dunlap3c3b8092006-02-01 03:06:58 -080097# * Does my stuff explained.
Linus Torvalds1da177e2005-04-16 15:20:36 -070098# */
99#
100# or, could also use:
101# /**
102# * my_function - does my stuff
103# * @my_arg: its mine damnit
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800104# * Description: Does my stuff explained.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105# */
106# etc.
107#
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800108# Beside functions you can also write documentation for structs, unions,
109# enums and typedefs. Instead of the function name you must write the name
110# of the declaration; the struct/union/enum/typedef must always precede
111# the name. Nesting of declarations is not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112# Use the argument mechanism to document members or constants.
113# e.g.
114# /**
115# * struct my_struct - short description
116# * @a: first member
117# * @b: second member
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800118# *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119# * Longer description
120# */
121# struct my_struct {
122# int a;
123# int b;
Martin Waitzaeec46b2005-11-13 16:08:13 -0800124# /* private: */
125# int c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126# };
127#
128# All descriptions can be multiline, except the short function description.
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800129#
130# You can also add additional sections. When documenting kernel functions you
131# should document the "Context:" of the function, e.g. whether the functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132# can be called form interrupts. Unlike other sections you can end it with an
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800133# empty line.
134# Example-sections should contain the string EXAMPLE so that they are marked
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135# appropriately in DocBook.
136#
137# Example:
138# /**
139# * user_function - function that can only be called in user context
140# * @a: some argument
141# * Context: !in_interrupt()
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800142# *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143# * Some description
144# * Example:
145# * user_function(22);
146# */
147# ...
148#
149#
150# All descriptive text is further processed, scanning for the following special
151# patterns, which are highlighted appropriately.
152#
153# 'funcname()' - function
154# '$ENVVAR' - environmental variable
155# '&struct_name' - name of a structure (up to two words including 'struct')
156# '@parameter' - name of a parameter
157# '%CONST' - name of a constant.
158
159my $errors = 0;
160my $warnings = 0;
Randy Dunlap5f8c7c92007-07-19 01:48:24 -0700161my $anon_struct_union = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163# match expressions used to find embedded type information
164my $type_constant = '\%([-_\w]+)';
165my $type_func = '(\w+)\(\)';
166my $type_param = '\@(\w+)';
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700167my $type_struct = '\&((struct\s*)*[_\w]+)';
Randy Dunlap6b5b55f2007-10-16 23:31:20 -0700168my $type_struct_xml = '\\&amp;((struct\s*)*[_\w]+)';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169my $type_env = '(\$\w+)';
170
171# Output conversion substitutions.
172# One for each output format
173
174# these work fairly well
175my %highlights_html = ( $type_constant, "<i>\$1</i>",
176 $type_func, "<b>\$1</b>",
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700177 $type_struct_xml, "<i>\$1</i>",
178 $type_env, "<b><i>\$1</i></b>",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 $type_param, "<tt><b>\$1</b></tt>" );
Randy Dunlap6b5b55f2007-10-16 23:31:20 -0700180my $local_lt = "\\\\\\\\lt:";
181my $local_gt = "\\\\\\\\gt:";
182my $blankline_html = $local_lt . "p" . $local_gt; # was "<p>"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184# XML, docbook format
185my %highlights_xml = ( "([^=])\\\"([^\\\"<]+)\\\"", "\$1<quote>\$2</quote>",
186 $type_constant, "<constant>\$1</constant>",
187 $type_func, "<function>\$1</function>",
Johannes Berg5c98fc02007-10-24 15:08:48 -0700188 $type_struct_xml, "<structname>\$1</structname>",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 $type_env, "<envar>\$1</envar>",
190 $type_param, "<parameter>\$1</parameter>" );
Johannes Berg5c98fc02007-10-24 15:08:48 -0700191my $blankline_xml = $local_lt . "/para" . $local_gt . $local_lt . "para" . $local_gt . "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193# gnome, docbook format
194my %highlights_gnome = ( $type_constant, "<replaceable class=\"option\">\$1</replaceable>",
195 $type_func, "<function>\$1</function>",
196 $type_struct, "<structname>\$1</structname>",
197 $type_env, "<envar>\$1</envar>",
198 $type_param, "<parameter>\$1</parameter>" );
199my $blankline_gnome = "</para><para>\n";
200
201# these are pretty rough
202my %highlights_man = ( $type_constant, "\$1",
203 $type_func, "\\\\fB\$1\\\\fP",
204 $type_struct, "\\\\fI\$1\\\\fP",
205 $type_param, "\\\\fI\$1\\\\fP" );
206my $blankline_man = "";
207
208# text-mode
209my %highlights_text = ( $type_constant, "\$1",
210 $type_func, "\$1",
211 $type_struct, "\$1",
212 $type_param, "\$1" );
213my $blankline_text = "";
214
215
216sub usage {
Johannes Berg4b445952007-10-24 15:08:48 -0700217 print "Usage: $0 [ -v ] [ -docbook | -html | -text | -man ] [ -no-doc-sections ]\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 print " [ -function funcname [ -function funcname ...] ]\n";
219 print " [ -nofunction funcname [ -nofunction funcname ...] ]\n";
220 print " c source file(s) > outputfile\n";
Randy Dunlap77cc23b2008-02-07 00:13:42 -0800221 print " -v : verbose output, more warnings & other info listed\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 exit 1;
223}
224
225# read arguments
226if ($#ARGV==-1) {
227 usage();
228}
229
230my $verbose = 0;
231my $output_mode = "man";
Johannes Berg4b445952007-10-24 15:08:48 -0700232my $no_doc_sections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233my %highlights = %highlights_man;
234my $blankline = $blankline_man;
235my $modulename = "Kernel API";
236my $function_only = 0;
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800237my $man_date = ('January', 'February', 'March', 'April', 'May', 'June',
238 'July', 'August', 'September', 'October',
239 'November', 'December')[(localtime)[4]] .
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 " " . ((localtime)[5]+1900);
241
242# Essentially these are globals
243# They probably want to be tidied up made more localised or summat.
244# CAVEAT EMPTOR! Some of the others I localised may not want to be which
245# could cause "use of undefined value" or other bugs.
246my ($function, %function_table,%parametertypes,$declaration_purpose);
247my ($type,$declaration_name,$return_type);
248my ($newsection,$newcontents,$prototype,$filelist, $brcount, %source_map);
249
Randy Dunlapbd0e88e2008-03-13 12:32:43 -0700250if (defined($ENV{'KBUILD_VERBOSE'})) {
251 $verbose = "$ENV{'KBUILD_VERBOSE'}";
252}
253
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800254# Generated docbook code is inserted in a template at a point where
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255# docbook v3.1 requires a non-zero sequence of RefEntry's; see:
256# http://www.oasis-open.org/docbook/documentation/reference/html/refentry.html
257# We keep track of number of generated entries and generate a dummy
258# if needs be to ensure the expanded template can be postprocessed
259# into html.
260my $section_counter = 0;
261
262my $lineprefix="";
263
264# states
265# 0 - normal code
266# 1 - looking for function name
267# 2 - scanning field start.
268# 3 - scanning prototype.
269# 4 - documentation block
270my $state;
Randy Dunlap850622d2006-06-25 05:48:55 -0700271my $in_doc_sect;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273#declaration types: can be
274# 'function', 'struct', 'union', 'enum', 'typedef'
275my $decl_type;
276
277my $doc_special = "\@\%\$\&";
278
279my $doc_start = '^/\*\*\s*$'; # Allow whitespace at end of comment start.
280my $doc_end = '\*/';
281my $doc_com = '\s*\*\s*';
282my $doc_decl = $doc_com.'(\w+)';
Randy Dunlap891dcd22007-02-10 01:45:53 -0800283my $doc_sect = $doc_com.'(['.$doc_special.']?[\w\s]+):(.*)';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284my $doc_content = $doc_com.'(.*)';
285my $doc_block = $doc_com.'DOC:\s*(.*)?';
286
287my %constants;
288my %parameterdescs;
289my @parameterlist;
290my %sections;
291my @sectionlist;
292
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();
339 } elsif ($cmd eq '-filelist') {
340 $filelist = shift @ARGV;
Johannes Berg4b445952007-10-24 15:08:48 -0700341 } elsif ($cmd eq '-no-doc-sections') {
342 $no_doc_sections = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344}
345
Borislav Petkov53f049f2007-05-08 00:30:54 -0700346# get kernel version from env
347sub get_kernel_version() {
Johannes Berg1b9bc222007-10-24 15:08:48 -0700348 my $version = 'unknown kernel version';
Borislav Petkov53f049f2007-05-08 00:30:54 -0700349
350 if (defined($ENV{'KERNELVERSION'})) {
351 $version = $ENV{'KERNELVERSION'};
352 }
353 return $version;
354}
Borislav Petkov03662992007-05-09 02:33:43 -0700355my $kernelversion = get_kernel_version();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357# generate a sequence of code that will splice in highlighting information
358# using the s// operator.
359my $dohighlight = "";
360foreach my $pattern (keys %highlights) {
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700361# print STDERR "scanning pattern:$pattern, highlight:($highlights{$pattern})\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 $dohighlight .= "\$contents =~ s:$pattern:$highlights{$pattern}:gs;\n";
363}
364
365##
366# dumps section contents to arrays/hashes intended for that purpose.
367#
368sub dump_section {
369 my $name = shift;
370 my $contents = join "\n", @_;
371
372 if ($name =~ m/$type_constant/) {
373 $name = $1;
374# print STDERR "constant section '$1' = '$contents'\n";
375 $constants{$name} = $contents;
376 } elsif ($name =~ m/$type_param/) {
377# print STDERR "parameter def '$1' = '$contents'\n";
378 $name = $1;
379 $parameterdescs{$name} = $contents;
380 } else {
381# print STDERR "other section '$name' = '$contents'\n";
382 $sections{$name} = $contents;
383 push @sectionlist, $name;
384 }
385}
386
387##
Johannes Bergb112e0f2007-10-24 15:08:48 -0700388# dump DOC: section after checking that it should go out
389#
390sub dump_doc_section {
391 my $name = shift;
392 my $contents = join "\n", @_;
393
Johannes Berg4b445952007-10-24 15:08:48 -0700394 if ($no_doc_sections) {
395 return;
396 }
397
Johannes Bergb112e0f2007-10-24 15:08:48 -0700398 if (($function_only == 0) ||
399 ( $function_only == 1 && defined($function_table{$name})) ||
400 ( $function_only == 2 && !defined($function_table{$name})))
401 {
402 dump_section $name, $contents;
403 output_blockhead({'sectionlist' => \@sectionlist,
404 'sections' => \%sections,
405 'module' => $modulename,
406 'content-only' => ($function_only != 0), });
407 }
408}
409
410##
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411# output function
412#
413# parameterdescs, a hash.
414# function => "function name"
415# parameterlist => @list of parameters
416# parameterdescs => %parameter descriptions
417# sectionlist => @list of sections
Randy Dunlapa21217d2007-02-10 01:46:04 -0800418# sections => %section descriptions
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800419#
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421sub output_highlight {
422 my $contents = join "\n",@_;
423 my $line;
424
425# DEBUG
426# if (!defined $contents) {
427# use Carp;
428# confess "output_highlight got called with no args?\n";
429# }
430
Johannes Berg5c98fc02007-10-24 15:08:48 -0700431 if ($output_mode eq "html" || $output_mode eq "xml") {
Randy Dunlap6b5b55f2007-10-16 23:31:20 -0700432 $contents = local_unescape($contents);
433 # convert data read & converted thru xml_escape() into &xyz; format:
434 $contents =~ s/\\\\\\/&/g;
435 }
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700436# print STDERR "contents b4:$contents\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 eval $dohighlight;
438 die $@ if $@;
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700439# print STDERR "contents af:$contents\n";
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 foreach $line (split "\n", $contents) {
Randy Dunlap3c308792007-05-08 00:24:39 -0700442 if ($line eq ""){
Randy Dunlap6b5b55f2007-10-16 23:31:20 -0700443 print $lineprefix, local_unescape($blankline);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 } else {
Randy Dunlap3c308792007-05-08 00:24:39 -0700445 $line =~ s/\\\\\\/\&/g;
Randy Dunlapcdccb312007-07-19 01:48:25 -0700446 if ($output_mode eq "man" && substr($line, 0, 1) eq ".") {
447 print "\\&$line";
448 } else {
449 print $lineprefix, $line;
450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
452 print "\n";
453 }
454}
455
456#output sections in html
457sub output_section_html(%) {
458 my %args = %{$_[0]};
459 my $section;
460
461 foreach $section (@{$args{'sectionlist'}}) {
462 print "<h3>$section</h3>\n";
463 print "<blockquote>\n";
464 output_highlight($args{'sections'}{$section});
465 print "</blockquote>\n";
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
469# output enum in html
470sub output_enum_html(%) {
471 my %args = %{$_[0]};
472 my ($parameter);
473 my $count;
474 print "<h2>enum ".$args{'enum'}."</h2>\n";
475
476 print "<b>enum ".$args{'enum'}."</b> {<br>\n";
477 $count = 0;
478 foreach $parameter (@{$args{'parameterlist'}}) {
Randy Dunlap3c308792007-05-08 00:24:39 -0700479 print " <b>".$parameter."</b>";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if ($count != $#{$args{'parameterlist'}}) {
481 $count++;
482 print ",\n";
483 }
484 print "<br>";
485 }
486 print "};<br>\n";
487
488 print "<h3>Constants</h3>\n";
489 print "<dl>\n";
490 foreach $parameter (@{$args{'parameterlist'}}) {
491 print "<dt><b>".$parameter."</b>\n";
492 print "<dd>";
493 output_highlight($args{'parameterdescs'}{$parameter});
494 }
495 print "</dl>\n";
496 output_section_html(@_);
497 print "<hr>\n";
498}
499
Randy Dunlapd28bee02006-02-01 03:06:57 -0800500# output typedef in html
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501sub output_typedef_html(%) {
502 my %args = %{$_[0]};
503 my ($parameter);
504 my $count;
505 print "<h2>typedef ".$args{'typedef'}."</h2>\n";
506
507 print "<b>typedef ".$args{'typedef'}."</b>\n";
508 output_section_html(@_);
509 print "<hr>\n";
510}
511
512# output struct in html
513sub output_struct_html(%) {
514 my %args = %{$_[0]};
515 my ($parameter);
516
Randy Dunlap262d9b02007-02-20 13:58:04 -0800517 print "<h2>".$args{'type'}." ".$args{'struct'}. " - " .$args{'purpose'}."</h2>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 print "<b>".$args{'type'}." ".$args{'struct'}."</b> {<br>\n";
519 foreach $parameter (@{$args{'parameterlist'}}) {
520 if ($parameter =~ /^#/) {
521 print "$parameter<br>\n";
522 next;
523 }
524 my $parameter_name = $parameter;
525 $parameter_name =~ s/\[.*//;
526
Randy Dunlap3c308792007-05-08 00:24:39 -0700527 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 $type = $args{'parametertypes'}{$parameter};
529 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
530 # pointer-to-function
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700531 print "&nbsp; &nbsp; <i>$1</i><b>$parameter</b>) <i>($2)</i>;<br>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700533 # bitfield
534 print "&nbsp; &nbsp; <i>$1</i> <b>$parameter</b>$2;<br>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 } else {
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700536 print "&nbsp; &nbsp; <i>$type</i> <b>$parameter</b>;<br>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
538 }
539 print "};<br>\n";
540
541 print "<h3>Members</h3>\n";
542 print "<dl>\n";
543 foreach $parameter (@{$args{'parameterlist'}}) {
544 ($parameter =~ /^#/) && next;
545
546 my $parameter_name = $parameter;
547 $parameter_name =~ s/\[.*//;
548
Randy Dunlap3c308792007-05-08 00:24:39 -0700549 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 print "<dt><b>".$parameter."</b>\n";
551 print "<dd>";
552 output_highlight($args{'parameterdescs'}{$parameter_name});
553 }
554 print "</dl>\n";
555 output_section_html(@_);
556 print "<hr>\n";
557}
558
559# output function in html
560sub output_function_html(%) {
561 my %args = %{$_[0]};
562 my ($parameter, $section);
563 my $count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Randy Dunlap262d9b02007-02-20 13:58:04 -0800565 print "<h2>" .$args{'function'}." - ".$args{'purpose'}."</h2>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 print "<i>".$args{'functiontype'}."</i>\n";
567 print "<b>".$args{'function'}."</b>\n";
568 print "(";
569 $count = 0;
570 foreach $parameter (@{$args{'parameterlist'}}) {
571 $type = $args{'parametertypes'}{$parameter};
572 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
573 # pointer-to-function
574 print "<i>$1</i><b>$parameter</b>) <i>($2)</i>";
575 } else {
576 print "<i>".$type."</i> <b>".$parameter."</b>";
577 }
578 if ($count != $#{$args{'parameterlist'}}) {
579 $count++;
580 print ",\n";
581 }
582 }
583 print ")\n";
584
585 print "<h3>Arguments</h3>\n";
586 print "<dl>\n";
587 foreach $parameter (@{$args{'parameterlist'}}) {
588 my $parameter_name = $parameter;
589 $parameter_name =~ s/\[.*//;
590
Randy Dunlap3c308792007-05-08 00:24:39 -0700591 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 print "<dt><b>".$parameter."</b>\n";
593 print "<dd>";
594 output_highlight($args{'parameterdescs'}{$parameter_name});
595 }
596 print "</dl>\n";
597 output_section_html(@_);
598 print "<hr>\n";
599}
600
Johannes Bergb112e0f2007-10-24 15:08:48 -0700601# output DOC: block header in html
602sub output_blockhead_html(%) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 my %args = %{$_[0]};
604 my ($parameter, $section);
605 my $count;
606
607 foreach $section (@{$args{'sectionlist'}}) {
608 print "<h3>$section</h3>\n";
609 print "<ul>\n";
610 output_highlight($args{'sections'}{$section});
611 print "</ul>\n";
612 }
613 print "<hr>\n";
614}
615
616sub output_section_xml(%) {
617 my %args = %{$_[0]};
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800618 my $section;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 # print out each section
620 $lineprefix=" ";
621 foreach $section (@{$args{'sectionlist'}}) {
Rich Walkerc73894c2005-05-01 08:59:26 -0700622 print "<refsect1>\n";
623 print "<title>$section</title>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if ($section =~ m/EXAMPLE/i) {
Rich Walkerc73894c2005-05-01 08:59:26 -0700625 print "<informalexample><programlisting>\n";
626 } else {
627 print "<para>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
629 output_highlight($args{'sections'}{$section});
630 if ($section =~ m/EXAMPLE/i) {
Rich Walkerc73894c2005-05-01 08:59:26 -0700631 print "</programlisting></informalexample>\n";
632 } else {
633 print "</para>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
Rich Walkerc73894c2005-05-01 08:59:26 -0700635 print "</refsect1>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
637}
638
639# output function in XML DocBook
640sub output_function_xml(%) {
641 my %args = %{$_[0]};
642 my ($parameter, $section);
643 my $count;
644 my $id;
645
646 $id = "API-".$args{'function'};
647 $id =~ s/[^A-Za-z0-9]/-/g;
648
Pavel Pisa5449bc92007-02-10 01:45:37 -0800649 print "<refentry id=\"$id\">\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700650 print "<refentryinfo>\n";
651 print " <title>LINUX</title>\n";
652 print " <productname>Kernel Hackers Manual</productname>\n";
653 print " <date>$man_date</date>\n";
654 print "</refentryinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 print "<refmeta>\n";
Pavel Pisa5449bc92007-02-10 01:45:37 -0800656 print " <refentrytitle><phrase>".$args{'function'}."</phrase></refentrytitle>\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700657 print " <manvolnum>9</manvolnum>\n";
Borislav Petkov03662992007-05-09 02:33:43 -0700658 print " <refmiscinfo class=\"version\">" . $kernelversion . "</refmiscinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 print "</refmeta>\n";
660 print "<refnamediv>\n";
661 print " <refname>".$args{'function'}."</refname>\n";
662 print " <refpurpose>\n";
663 print " ";
664 output_highlight ($args{'purpose'});
665 print " </refpurpose>\n";
666 print "</refnamediv>\n";
667
668 print "<refsynopsisdiv>\n";
669 print " <title>Synopsis</title>\n";
670 print " <funcsynopsis><funcprototype>\n";
671 print " <funcdef>".$args{'functiontype'}." ";
672 print "<function>".$args{'function'}." </function></funcdef>\n";
673
674 $count = 0;
675 if ($#{$args{'parameterlist'}} >= 0) {
676 foreach $parameter (@{$args{'parameterlist'}}) {
677 $type = $args{'parametertypes'}{$parameter};
678 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
679 # pointer-to-function
680 print " <paramdef>$1<parameter>$parameter</parameter>)\n";
681 print " <funcparams>$2</funcparams></paramdef>\n";
682 } else {
683 print " <paramdef>".$type;
684 print " <parameter>$parameter</parameter></paramdef>\n";
685 }
686 }
687 } else {
Martin Waitz6013d542005-05-01 08:59:25 -0700688 print " <void/>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
690 print " </funcprototype></funcsynopsis>\n";
691 print "</refsynopsisdiv>\n";
692
693 # print parameters
694 print "<refsect1>\n <title>Arguments</title>\n";
695 if ($#{$args{'parameterlist'}} >= 0) {
696 print " <variablelist>\n";
697 foreach $parameter (@{$args{'parameterlist'}}) {
698 my $parameter_name = $parameter;
699 $parameter_name =~ s/\[.*//;
700
701 print " <varlistentry>\n <term><parameter>$parameter</parameter></term>\n";
702 print " <listitem>\n <para>\n";
703 $lineprefix=" ";
704 output_highlight($args{'parameterdescs'}{$parameter_name});
705 print " </para>\n </listitem>\n </varlistentry>\n";
706 }
707 print " </variablelist>\n";
708 } else {
709 print " <para>\n None\n </para>\n";
710 }
711 print "</refsect1>\n";
712
713 output_section_xml(@_);
714 print "</refentry>\n\n";
715}
716
717# output struct in XML DocBook
718sub output_struct_xml(%) {
719 my %args = %{$_[0]};
720 my ($parameter, $section);
721 my $id;
722
723 $id = "API-struct-".$args{'struct'};
724 $id =~ s/[^A-Za-z0-9]/-/g;
725
Pavel Pisa5449bc92007-02-10 01:45:37 -0800726 print "<refentry id=\"$id\">\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700727 print "<refentryinfo>\n";
728 print " <title>LINUX</title>\n";
729 print " <productname>Kernel Hackers Manual</productname>\n";
730 print " <date>$man_date</date>\n";
731 print "</refentryinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 print "<refmeta>\n";
Pavel Pisa5449bc92007-02-10 01:45:37 -0800733 print " <refentrytitle><phrase>".$args{'type'}." ".$args{'struct'}."</phrase></refentrytitle>\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700734 print " <manvolnum>9</manvolnum>\n";
Borislav Petkov03662992007-05-09 02:33:43 -0700735 print " <refmiscinfo class=\"version\">" . $kernelversion . "</refmiscinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 print "</refmeta>\n";
737 print "<refnamediv>\n";
738 print " <refname>".$args{'type'}." ".$args{'struct'}."</refname>\n";
739 print " <refpurpose>\n";
740 print " ";
741 output_highlight ($args{'purpose'});
742 print " </refpurpose>\n";
743 print "</refnamediv>\n";
744
745 print "<refsynopsisdiv>\n";
746 print " <title>Synopsis</title>\n";
747 print " <programlisting>\n";
748 print $args{'type'}." ".$args{'struct'}." {\n";
749 foreach $parameter (@{$args{'parameterlist'}}) {
750 if ($parameter =~ /^#/) {
751 print "$parameter\n";
752 next;
753 }
754
755 my $parameter_name = $parameter;
756 $parameter_name =~ s/\[.*//;
757
758 defined($args{'parameterdescs'}{$parameter_name}) || next;
Randy Dunlap3c308792007-05-08 00:24:39 -0700759 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 $type = $args{'parametertypes'}{$parameter};
761 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
762 # pointer-to-function
763 print " $1 $parameter) ($2);\n";
764 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
Randy Dunlap51f5a0c2007-07-19 01:48:24 -0700765 # bitfield
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 print " $1 $parameter$2;\n";
767 } else {
768 print " ".$type." ".$parameter.";\n";
769 }
770 }
771 print "};";
772 print " </programlisting>\n";
773 print "</refsynopsisdiv>\n";
774
775 print " <refsect1>\n";
776 print " <title>Members</title>\n";
777
778 print " <variablelist>\n";
779 foreach $parameter (@{$args{'parameterlist'}}) {
780 ($parameter =~ /^#/) && next;
781
782 my $parameter_name = $parameter;
783 $parameter_name =~ s/\[.*//;
784
785 defined($args{'parameterdescs'}{$parameter_name}) || next;
786 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
787 print " <varlistentry>";
788 print " <term>$parameter</term>\n";
789 print " <listitem><para>\n";
790 output_highlight($args{'parameterdescs'}{$parameter_name});
791 print " </para></listitem>\n";
792 print " </varlistentry>\n";
793 }
794 print " </variablelist>\n";
795 print " </refsect1>\n";
796
797 output_section_xml(@_);
798
799 print "</refentry>\n\n";
800}
801
802# output enum in XML DocBook
803sub output_enum_xml(%) {
804 my %args = %{$_[0]};
805 my ($parameter, $section);
806 my $count;
807 my $id;
808
809 $id = "API-enum-".$args{'enum'};
810 $id =~ s/[^A-Za-z0-9]/-/g;
811
Pavel Pisa5449bc92007-02-10 01:45:37 -0800812 print "<refentry id=\"$id\">\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700813 print "<refentryinfo>\n";
814 print " <title>LINUX</title>\n";
815 print " <productname>Kernel Hackers Manual</productname>\n";
816 print " <date>$man_date</date>\n";
817 print "</refentryinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 print "<refmeta>\n";
Pavel Pisa5449bc92007-02-10 01:45:37 -0800819 print " <refentrytitle><phrase>enum ".$args{'enum'}."</phrase></refentrytitle>\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700820 print " <manvolnum>9</manvolnum>\n";
Borislav Petkov03662992007-05-09 02:33:43 -0700821 print " <refmiscinfo class=\"version\">" . $kernelversion . "</refmiscinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 print "</refmeta>\n";
823 print "<refnamediv>\n";
824 print " <refname>enum ".$args{'enum'}."</refname>\n";
825 print " <refpurpose>\n";
826 print " ";
827 output_highlight ($args{'purpose'});
828 print " </refpurpose>\n";
829 print "</refnamediv>\n";
830
831 print "<refsynopsisdiv>\n";
832 print " <title>Synopsis</title>\n";
833 print " <programlisting>\n";
834 print "enum ".$args{'enum'}." {\n";
835 $count = 0;
836 foreach $parameter (@{$args{'parameterlist'}}) {
Randy Dunlap3c308792007-05-08 00:24:39 -0700837 print " $parameter";
838 if ($count != $#{$args{'parameterlist'}}) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 $count++;
840 print ",";
Randy Dunlap3c308792007-05-08 00:24:39 -0700841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 print "\n";
843 }
844 print "};";
845 print " </programlisting>\n";
846 print "</refsynopsisdiv>\n";
847
848 print "<refsect1>\n";
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800849 print " <title>Constants</title>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 print " <variablelist>\n";
851 foreach $parameter (@{$args{'parameterlist'}}) {
852 my $parameter_name = $parameter;
853 $parameter_name =~ s/\[.*//;
854
855 print " <varlistentry>";
856 print " <term>$parameter</term>\n";
857 print " <listitem><para>\n";
858 output_highlight($args{'parameterdescs'}{$parameter_name});
859 print " </para></listitem>\n";
860 print " </varlistentry>\n";
861 }
862 print " </variablelist>\n";
863 print "</refsect1>\n";
864
865 output_section_xml(@_);
866
867 print "</refentry>\n\n";
868}
869
870# output typedef in XML DocBook
871sub output_typedef_xml(%) {
872 my %args = %{$_[0]};
873 my ($parameter, $section);
874 my $id;
875
876 $id = "API-typedef-".$args{'typedef'};
877 $id =~ s/[^A-Za-z0-9]/-/g;
878
Pavel Pisa5449bc92007-02-10 01:45:37 -0800879 print "<refentry id=\"$id\">\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700880 print "<refentryinfo>\n";
881 print " <title>LINUX</title>\n";
882 print " <productname>Kernel Hackers Manual</productname>\n";
883 print " <date>$man_date</date>\n";
884 print "</refentryinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 print "<refmeta>\n";
Pavel Pisa5449bc92007-02-10 01:45:37 -0800886 print " <refentrytitle><phrase>typedef ".$args{'typedef'}."</phrase></refentrytitle>\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700887 print " <manvolnum>9</manvolnum>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 print "</refmeta>\n";
889 print "<refnamediv>\n";
890 print " <refname>typedef ".$args{'typedef'}."</refname>\n";
891 print " <refpurpose>\n";
892 print " ";
893 output_highlight ($args{'purpose'});
894 print " </refpurpose>\n";
895 print "</refnamediv>\n";
896
897 print "<refsynopsisdiv>\n";
898 print " <title>Synopsis</title>\n";
899 print " <synopsis>typedef ".$args{'typedef'}.";</synopsis>\n";
900 print "</refsynopsisdiv>\n";
901
902 output_section_xml(@_);
903
904 print "</refentry>\n\n";
905}
906
907# output in XML DocBook
Johannes Bergb112e0f2007-10-24 15:08:48 -0700908sub output_blockhead_xml(%) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 my %args = %{$_[0]};
910 my ($parameter, $section);
911 my $count;
912
913 my $id = $args{'module'};
914 $id =~ s/[^A-Za-z0-9]/-/g;
915
916 # print out each section
917 $lineprefix=" ";
918 foreach $section (@{$args{'sectionlist'}}) {
Johannes Bergb112e0f2007-10-24 15:08:48 -0700919 if (!$args{'content-only'}) {
920 print "<refsect1>\n <title>$section</title>\n";
921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 if ($section =~ m/EXAMPLE/i) {
923 print "<example><para>\n";
Johannes Bergb112e0f2007-10-24 15:08:48 -0700924 } else {
925 print "<para>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 }
927 output_highlight($args{'sections'}{$section});
928 if ($section =~ m/EXAMPLE/i) {
929 print "</para></example>\n";
Johannes Bergb112e0f2007-10-24 15:08:48 -0700930 } else {
931 print "</para>";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
Johannes Bergb112e0f2007-10-24 15:08:48 -0700933 if (!$args{'content-only'}) {
934 print "\n</refsect1>\n";
935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
937
938 print "\n\n";
939}
940
941# output in XML DocBook
942sub output_function_gnome {
943 my %args = %{$_[0]};
944 my ($parameter, $section);
945 my $count;
946 my $id;
947
948 $id = $args{'module'}."-".$args{'function'};
949 $id =~ s/[^A-Za-z0-9]/-/g;
950
951 print "<sect2>\n";
952 print " <title id=\"$id\">".$args{'function'}."</title>\n";
953
954 print " <funcsynopsis>\n";
955 print " <funcdef>".$args{'functiontype'}." ";
956 print "<function>".$args{'function'}." ";
957 print "</function></funcdef>\n";
958
959 $count = 0;
960 if ($#{$args{'parameterlist'}} >= 0) {
961 foreach $parameter (@{$args{'parameterlist'}}) {
962 $type = $args{'parametertypes'}{$parameter};
963 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
964 # pointer-to-function
965 print " <paramdef>$1 <parameter>$parameter</parameter>)\n";
966 print " <funcparams>$2</funcparams></paramdef>\n";
967 } else {
968 print " <paramdef>".$type;
969 print " <parameter>$parameter</parameter></paramdef>\n";
970 }
971 }
972 } else {
973 print " <void>\n";
974 }
975 print " </funcsynopsis>\n";
976 if ($#{$args{'parameterlist'}} >= 0) {
977 print " <informaltable pgwide=\"1\" frame=\"none\" role=\"params\">\n";
978 print "<tgroup cols=\"2\">\n";
979 print "<colspec colwidth=\"2*\">\n";
980 print "<colspec colwidth=\"8*\">\n";
981 print "<tbody>\n";
982 foreach $parameter (@{$args{'parameterlist'}}) {
983 my $parameter_name = $parameter;
984 $parameter_name =~ s/\[.*//;
985
986 print " <row><entry align=\"right\"><parameter>$parameter</parameter></entry>\n";
987 print " <entry>\n";
988 $lineprefix=" ";
989 output_highlight($args{'parameterdescs'}{$parameter_name});
990 print " </entry></row>\n";
991 }
992 print " </tbody></tgroup></informaltable>\n";
993 } else {
994 print " <para>\n None\n </para>\n";
995 }
996
997 # print out each section
998 $lineprefix=" ";
999 foreach $section (@{$args{'sectionlist'}}) {
1000 print "<simplesect>\n <title>$section</title>\n";
1001 if ($section =~ m/EXAMPLE/i) {
1002 print "<example><programlisting>\n";
1003 } else {
1004 }
1005 print "<para>\n";
1006 output_highlight($args{'sections'}{$section});
1007 print "</para>\n";
1008 if ($section =~ m/EXAMPLE/i) {
1009 print "</programlisting></example>\n";
1010 } else {
1011 }
1012 print " </simplesect>\n";
1013 }
1014
1015 print "</sect2>\n\n";
1016}
1017
1018##
1019# output function in man
1020sub output_function_man(%) {
1021 my %args = %{$_[0]};
1022 my ($parameter, $section);
1023 my $count;
1024
1025 print ".TH \"$args{'function'}\" 9 \"$args{'function'}\" \"$man_date\" \"Kernel Hacker's Manual\" LINUX\n";
1026
1027 print ".SH NAME\n";
1028 print $args{'function'}." \\- ".$args{'purpose'}."\n";
1029
1030 print ".SH SYNOPSIS\n";
Randy Dunlapa21217d2007-02-10 01:46:04 -08001031 if ($args{'functiontype'} ne "") {
1032 print ".B \"".$args{'functiontype'}."\" ".$args{'function'}."\n";
1033 } else {
1034 print ".B \"".$args{'function'}."\n";
1035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 $count = 0;
1037 my $parenth = "(";
1038 my $post = ",";
1039 foreach my $parameter (@{$args{'parameterlist'}}) {
1040 if ($count == $#{$args{'parameterlist'}}) {
1041 $post = ");";
1042 }
1043 $type = $args{'parametertypes'}{$parameter};
1044 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1045 # pointer-to-function
1046 print ".BI \"".$parenth.$1."\" ".$parameter." \") (".$2.")".$post."\"\n";
1047 } else {
1048 $type =~ s/([^\*])$/$1 /;
1049 print ".BI \"".$parenth.$type."\" ".$parameter." \"".$post."\"\n";
1050 }
1051 $count++;
1052 $parenth = "";
1053 }
1054
1055 print ".SH ARGUMENTS\n";
1056 foreach $parameter (@{$args{'parameterlist'}}) {
1057 my $parameter_name = $parameter;
1058 $parameter_name =~ s/\[.*//;
1059
1060 print ".IP \"".$parameter."\" 12\n";
1061 output_highlight($args{'parameterdescs'}{$parameter_name});
1062 }
1063 foreach $section (@{$args{'sectionlist'}}) {
1064 print ".SH \"", uc $section, "\"\n";
1065 output_highlight($args{'sections'}{$section});
1066 }
1067}
1068
1069##
1070# output enum in man
1071sub output_enum_man(%) {
1072 my %args = %{$_[0]};
1073 my ($parameter, $section);
1074 my $count;
1075
1076 print ".TH \"$args{'module'}\" 9 \"enum $args{'enum'}\" \"$man_date\" \"API Manual\" LINUX\n";
1077
1078 print ".SH NAME\n";
1079 print "enum ".$args{'enum'}." \\- ".$args{'purpose'}."\n";
1080
1081 print ".SH SYNOPSIS\n";
1082 print "enum ".$args{'enum'}." {\n";
1083 $count = 0;
1084 foreach my $parameter (@{$args{'parameterlist'}}) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001085 print ".br\n.BI \" $parameter\"\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 if ($count == $#{$args{'parameterlist'}}) {
1087 print "\n};\n";
1088 last;
1089 }
1090 else {
1091 print ", \n.br\n";
1092 }
1093 $count++;
1094 }
1095
1096 print ".SH Constants\n";
1097 foreach $parameter (@{$args{'parameterlist'}}) {
1098 my $parameter_name = $parameter;
1099 $parameter_name =~ s/\[.*//;
1100
1101 print ".IP \"".$parameter."\" 12\n";
1102 output_highlight($args{'parameterdescs'}{$parameter_name});
1103 }
1104 foreach $section (@{$args{'sectionlist'}}) {
1105 print ".SH \"$section\"\n";
1106 output_highlight($args{'sections'}{$section});
1107 }
1108}
1109
1110##
1111# output struct in man
1112sub output_struct_man(%) {
1113 my %args = %{$_[0]};
1114 my ($parameter, $section);
1115
1116 print ".TH \"$args{'module'}\" 9 \"".$args{'type'}." ".$args{'struct'}."\" \"$man_date\" \"API Manual\" LINUX\n";
1117
1118 print ".SH NAME\n";
1119 print $args{'type'}." ".$args{'struct'}." \\- ".$args{'purpose'}."\n";
1120
1121 print ".SH SYNOPSIS\n";
1122 print $args{'type'}." ".$args{'struct'}." {\n.br\n";
1123
1124 foreach my $parameter (@{$args{'parameterlist'}}) {
1125 if ($parameter =~ /^#/) {
1126 print ".BI \"$parameter\"\n.br\n";
1127 next;
1128 }
1129 my $parameter_name = $parameter;
1130 $parameter_name =~ s/\[.*//;
1131
Randy Dunlap3c308792007-05-08 00:24:39 -07001132 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 $type = $args{'parametertypes'}{$parameter};
1134 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1135 # pointer-to-function
1136 print ".BI \" ".$1."\" ".$parameter." \") (".$2.")"."\"\n;\n";
1137 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
Randy.Dunlap1d7e1d42006-07-01 04:36:34 -07001138 # bitfield
1139 print ".BI \" ".$1."\ \" ".$parameter.$2." \""."\"\n;\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 } else {
1141 $type =~ s/([^\*])$/$1 /;
1142 print ".BI \" ".$type."\" ".$parameter." \""."\"\n;\n";
1143 }
1144 print "\n.br\n";
1145 }
1146 print "};\n.br\n";
1147
Randy Dunlapc51d3da2006-06-25 05:49:14 -07001148 print ".SH Members\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 foreach $parameter (@{$args{'parameterlist'}}) {
1150 ($parameter =~ /^#/) && next;
1151
1152 my $parameter_name = $parameter;
1153 $parameter_name =~ s/\[.*//;
1154
Randy Dunlap3c308792007-05-08 00:24:39 -07001155 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 print ".IP \"".$parameter."\" 12\n";
1157 output_highlight($args{'parameterdescs'}{$parameter_name});
1158 }
1159 foreach $section (@{$args{'sectionlist'}}) {
1160 print ".SH \"$section\"\n";
1161 output_highlight($args{'sections'}{$section});
1162 }
1163}
1164
1165##
1166# output typedef in man
1167sub output_typedef_man(%) {
1168 my %args = %{$_[0]};
1169 my ($parameter, $section);
1170
1171 print ".TH \"$args{'module'}\" 9 \"$args{'typedef'}\" \"$man_date\" \"API Manual\" LINUX\n";
1172
1173 print ".SH NAME\n";
1174 print "typedef ".$args{'typedef'}." \\- ".$args{'purpose'}."\n";
1175
1176 foreach $section (@{$args{'sectionlist'}}) {
1177 print ".SH \"$section\"\n";
1178 output_highlight($args{'sections'}{$section});
1179 }
1180}
1181
Johannes Bergb112e0f2007-10-24 15:08:48 -07001182sub output_blockhead_man(%) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 my %args = %{$_[0]};
1184 my ($parameter, $section);
1185 my $count;
1186
1187 print ".TH \"$args{'module'}\" 9 \"$args{'module'}\" \"$man_date\" \"API Manual\" LINUX\n";
1188
1189 foreach $section (@{$args{'sectionlist'}}) {
1190 print ".SH \"$section\"\n";
1191 output_highlight($args{'sections'}{$section});
1192 }
1193}
1194
1195##
1196# output in text
1197sub output_function_text(%) {
1198 my %args = %{$_[0]};
1199 my ($parameter, $section);
Randy Dunlapa21217d2007-02-10 01:46:04 -08001200 my $start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Randy Dunlapf47634b2006-07-01 04:36:36 -07001202 print "Name:\n\n";
1203 print $args{'function'}." - ".$args{'purpose'}."\n";
1204
1205 print "\nSynopsis:\n\n";
Randy Dunlapa21217d2007-02-10 01:46:04 -08001206 if ($args{'functiontype'} ne "") {
1207 $start = $args{'functiontype'}." ".$args{'function'}." (";
1208 } else {
1209 $start = $args{'function'}." (";
1210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 print $start;
Randy Dunlapa21217d2007-02-10 01:46:04 -08001212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 my $count = 0;
1214 foreach my $parameter (@{$args{'parameterlist'}}) {
1215 $type = $args{'parametertypes'}{$parameter};
1216 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1217 # pointer-to-function
1218 print $1.$parameter.") (".$2;
1219 } else {
1220 print $type." ".$parameter;
1221 }
1222 if ($count != $#{$args{'parameterlist'}}) {
1223 $count++;
1224 print ",\n";
1225 print " " x length($start);
1226 } else {
1227 print ");\n\n";
1228 }
1229 }
1230
1231 print "Arguments:\n\n";
1232 foreach $parameter (@{$args{'parameterlist'}}) {
1233 my $parameter_name = $parameter;
1234 $parameter_name =~ s/\[.*//;
1235
1236 print $parameter."\n\t".$args{'parameterdescs'}{$parameter_name}."\n";
1237 }
1238 output_section_text(@_);
1239}
1240
1241#output sections in text
1242sub output_section_text(%) {
1243 my %args = %{$_[0]};
1244 my $section;
1245
1246 print "\n";
1247 foreach $section (@{$args{'sectionlist'}}) {
1248 print "$section:\n\n";
1249 output_highlight($args{'sections'}{$section});
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 print "\n\n";
1252}
1253
1254# output enum in text
1255sub output_enum_text(%) {
1256 my %args = %{$_[0]};
1257 my ($parameter);
1258 my $count;
1259 print "Enum:\n\n";
1260
Randy.Dunlap1d7e1d42006-07-01 04:36:34 -07001261 print "enum ".$args{'enum'}." - ".$args{'purpose'}."\n\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 print "enum ".$args{'enum'}." {\n";
1263 $count = 0;
1264 foreach $parameter (@{$args{'parameterlist'}}) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001265 print "\t$parameter";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 if ($count != $#{$args{'parameterlist'}}) {
1267 $count++;
1268 print ",";
1269 }
1270 print "\n";
1271 }
1272 print "};\n\n";
1273
1274 print "Constants:\n\n";
1275 foreach $parameter (@{$args{'parameterlist'}}) {
1276 print "$parameter\n\t";
1277 print $args{'parameterdescs'}{$parameter}."\n";
1278 }
1279
1280 output_section_text(@_);
1281}
1282
1283# output typedef in text
1284sub output_typedef_text(%) {
1285 my %args = %{$_[0]};
1286 my ($parameter);
1287 my $count;
1288 print "Typedef:\n\n";
1289
Randy.Dunlap1d7e1d42006-07-01 04:36:34 -07001290 print "typedef ".$args{'typedef'}." - ".$args{'purpose'}."\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 output_section_text(@_);
1292}
1293
1294# output struct as text
1295sub output_struct_text(%) {
1296 my %args = %{$_[0]};
1297 my ($parameter);
1298
Randy.Dunlap1d7e1d42006-07-01 04:36:34 -07001299 print $args{'type'}." ".$args{'struct'}." - ".$args{'purpose'}."\n\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 print $args{'type'}." ".$args{'struct'}." {\n";
1301 foreach $parameter (@{$args{'parameterlist'}}) {
1302 if ($parameter =~ /^#/) {
1303 print "$parameter\n";
1304 next;
1305 }
1306
1307 my $parameter_name = $parameter;
1308 $parameter_name =~ s/\[.*//;
1309
Randy Dunlap3c308792007-05-08 00:24:39 -07001310 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 $type = $args{'parametertypes'}{$parameter};
1312 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1313 # pointer-to-function
1314 print "\t$1 $parameter) ($2);\n";
1315 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
Randy Dunlap51f5a0c2007-07-19 01:48:24 -07001316 # bitfield
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 print "\t$1 $parameter$2;\n";
1318 } else {
1319 print "\t".$type." ".$parameter.";\n";
1320 }
1321 }
1322 print "};\n\n";
1323
1324 print "Members:\n\n";
1325 foreach $parameter (@{$args{'parameterlist'}}) {
1326 ($parameter =~ /^#/) && next;
1327
1328 my $parameter_name = $parameter;
1329 $parameter_name =~ s/\[.*//;
1330
Randy Dunlap3c308792007-05-08 00:24:39 -07001331 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 print "$parameter\n\t";
1333 print $args{'parameterdescs'}{$parameter_name}."\n";
1334 }
1335 print "\n";
1336 output_section_text(@_);
1337}
1338
Johannes Bergb112e0f2007-10-24 15:08:48 -07001339sub output_blockhead_text(%) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 my %args = %{$_[0]};
1341 my ($parameter, $section);
1342
1343 foreach $section (@{$args{'sectionlist'}}) {
1344 print " $section:\n";
1345 print " -> ";
1346 output_highlight($args{'sections'}{$section});
1347 }
1348}
1349
1350##
Randy Dunlap27205742006-10-11 01:22:12 -07001351# generic output function for all types (function, struct/union, typedef, enum);
1352# calls the generated, variable output_ function name based on
1353# functype and output_mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354sub output_declaration {
1355 no strict 'refs';
1356 my $name = shift;
1357 my $functype = shift;
1358 my $func = "output_${functype}_$output_mode";
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001359 if (($function_only==0) ||
1360 ( $function_only == 1 && defined($function_table{$name})) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 ( $function_only == 2 && !defined($function_table{$name})))
1362 {
Randy Dunlap3c308792007-05-08 00:24:39 -07001363 &$func(@_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 $section_counter++;
1365 }
1366}
1367
1368##
Randy Dunlap27205742006-10-11 01:22:12 -07001369# generic output function - calls the right one based on current output mode.
Johannes Bergb112e0f2007-10-24 15:08:48 -07001370sub output_blockhead {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 no strict 'refs';
Johannes Bergb112e0f2007-10-24 15:08:48 -07001372 my $func = "output_blockhead_".$output_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 &$func(@_);
1374 $section_counter++;
1375}
1376
1377##
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001378# takes a declaration (struct, union, enum, typedef) and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379# invokes the right handler. NOT called for functions.
1380sub dump_declaration($$) {
1381 no strict 'refs';
1382 my ($prototype, $file) = @_;
1383 my $func = "dump_".$decl_type;
1384 &$func(@_);
1385}
1386
1387sub dump_union($$) {
1388 dump_struct(@_);
1389}
1390
1391sub dump_struct($$) {
1392 my $x = shift;
1393 my $file = shift;
1394
1395 if ($x =~/(struct|union)\s+(\w+)\s*{(.*)}/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001396 $declaration_name = $2;
1397 my $members = $3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 # ignore embedded structs or unions
1400 $members =~ s/{.*?}//g;
1401
Martin Waitzaeec46b2005-11-13 16:08:13 -08001402 # ignore members marked private:
1403 $members =~ s/\/\*.*?private:.*?public:.*?\*\///gos;
1404 $members =~ s/\/\*.*?private:.*//gos;
1405 # strip comments:
1406 $members =~ s/\/\*.*?\*\///gos;
1407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 create_parameterlist($members, ';', $file);
1409
1410 output_declaration($declaration_name,
1411 'struct',
1412 {'struct' => $declaration_name,
1413 'module' => $modulename,
1414 'parameterlist' => \@parameterlist,
1415 'parameterdescs' => \%parameterdescs,
1416 'parametertypes' => \%parametertypes,
1417 'sectionlist' => \@sectionlist,
1418 'sections' => \%sections,
1419 'purpose' => $declaration_purpose,
1420 'type' => $decl_type
1421 });
1422 }
1423 else {
Randy Dunlap3c308792007-05-08 00:24:39 -07001424 print STDERR "Error(${file}:$.): Cannot parse struct or union!\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 ++$errors;
1426 }
1427}
1428
1429sub dump_enum($$) {
1430 my $x = shift;
1431 my $file = shift;
1432
Martin Waitzaeec46b2005-11-13 16:08:13 -08001433 $x =~ s@/\*.*?\*/@@gos; # strip comments.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 if ($x =~ /enum\s+(\w+)\s*{(.*)}/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001435 $declaration_name = $1;
1436 my $members = $2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
1438 foreach my $arg (split ',', $members) {
1439 $arg =~ s/^\s*(\w+).*/$1/;
1440 push @parameterlist, $arg;
1441 if (!$parameterdescs{$arg}) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001442 $parameterdescs{$arg} = $undescribed;
1443 print STDERR "Warning(${file}:$.): Enum value '$arg' ".
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 "not described in enum '$declaration_name'\n";
1445 }
1446
1447 }
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 output_declaration($declaration_name,
1450 'enum',
1451 {'enum' => $declaration_name,
1452 'module' => $modulename,
1453 'parameterlist' => \@parameterlist,
1454 'parameterdescs' => \%parameterdescs,
1455 'sectionlist' => \@sectionlist,
1456 'sections' => \%sections,
1457 'purpose' => $declaration_purpose
1458 });
1459 }
1460 else {
Randy Dunlap3c308792007-05-08 00:24:39 -07001461 print STDERR "Error(${file}:$.): Cannot parse enum!\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 ++$errors;
1463 }
1464}
1465
1466sub dump_typedef($$) {
1467 my $x = shift;
1468 my $file = shift;
1469
Martin Waitzaeec46b2005-11-13 16:08:13 -08001470 $x =~ s@/\*.*?\*/@@gos; # strip comments.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 while (($x =~ /\(*.\)\s*;$/) || ($x =~ /\[*.\]\s*;$/)) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001472 $x =~ s/\(*.\)\s*;$/;/;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 $x =~ s/\[*.\]\s*;$/;/;
1474 }
1475
1476 if ($x =~ /typedef.*\s+(\w+)\s*;/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001477 $declaration_name = $1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
1479 output_declaration($declaration_name,
1480 'typedef',
1481 {'typedef' => $declaration_name,
1482 'module' => $modulename,
1483 'sectionlist' => \@sectionlist,
1484 'sections' => \%sections,
1485 'purpose' => $declaration_purpose
1486 });
1487 }
1488 else {
Randy Dunlap3c308792007-05-08 00:24:39 -07001489 print STDERR "Error(${file}:$.): Cannot parse typedef!\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 ++$errors;
1491 }
1492}
1493
1494sub create_parameterlist($$$) {
1495 my $args = shift;
1496 my $splitter = shift;
1497 my $file = shift;
1498 my $type;
1499 my $param;
1500
Martin Waitza6d3fe72006-01-09 20:53:55 -08001501 # temporarily replace commas inside function pointer definition
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 while ($args =~ /(\([^\),]+),/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001503 $args =~ s/(\([^\),]+),/$1#/g;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 }
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 foreach my $arg (split($splitter, $args)) {
1507 # strip comments
1508 $arg =~ s/\/\*.*\*\///;
Randy Dunlap3c308792007-05-08 00:24:39 -07001509 # strip leading/trailing spaces
1510 $arg =~ s/^\s*//;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 $arg =~ s/\s*$//;
1512 $arg =~ s/\s+/ /;
1513
1514 if ($arg =~ /^#/) {
1515 # Treat preprocessor directive as a typeless variable just to fill
1516 # corresponding data structures "correctly". Catch it later in
1517 # output_* subs.
1518 push_parameter($arg, "", $file);
Richard Kennedy00d62962008-02-23 15:24:01 -08001519 } elsif ($arg =~ m/\(.+\)\s*\(/) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 # pointer-to-function
1521 $arg =~ tr/#/,/;
Richard Kennedy00d62962008-02-23 15:24:01 -08001522 $arg =~ m/[^\(]+\(\*?\s*(\w*)\s*\)/;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 $param = $1;
1524 $type = $arg;
Richard Kennedy00d62962008-02-23 15:24:01 -08001525 $type =~ s/([^\(]+\(\*?)\s*$param/$1/;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 push_parameter($param, $type, $file);
Martin Waitzaeec46b2005-11-13 16:08:13 -08001527 } elsif ($arg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 $arg =~ s/\s*:\s*/:/g;
1529 $arg =~ s/\s*\[/\[/g;
1530
1531 my @args = split('\s*,\s*', $arg);
1532 if ($args[0] =~ m/\*/) {
1533 $args[0] =~ s/(\*+)\s*/ $1/;
1534 }
Borislav Petkov884f2812007-05-08 00:29:05 -07001535
1536 my @first_arg;
1537 if ($args[0] =~ /^(.*\s+)(.*?\[.*\].*)$/) {
1538 shift @args;
1539 push(@first_arg, split('\s+', $1));
1540 push(@first_arg, $2);
1541 } else {
1542 @first_arg = split('\s+', shift @args);
1543 }
1544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 unshift(@args, pop @first_arg);
1546 $type = join " ", @first_arg;
1547
1548 foreach $param (@args) {
1549 if ($param =~ m/^(\*+)\s*(.*)/) {
1550 push_parameter($2, "$type $1", $file);
1551 }
1552 elsif ($param =~ m/(.*?):(\d+)/) {
1553 push_parameter($1, "$type:$2", $file)
1554 }
1555 else {
1556 push_parameter($param, $type, $file);
1557 }
1558 }
1559 }
1560 }
1561}
1562
1563sub push_parameter($$$) {
1564 my $param = shift;
1565 my $type = shift;
1566 my $file = shift;
1567
Randy Dunlap5f8c7c92007-07-19 01:48:24 -07001568 if (($anon_struct_union == 1) && ($type eq "") &&
1569 ($param eq "}")) {
1570 return; # ignore the ending }; from anon. struct/union
1571 }
1572
1573 $anon_struct_union = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 my $param_name = $param;
1575 $param_name =~ s/\[.*//;
1576
Martin Waitza6d3fe72006-01-09 20:53:55 -08001577 if ($type eq "" && $param =~ /\.\.\.$/)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 {
1579 $type="";
Martin Waitza6d3fe72006-01-09 20:53:55 -08001580 $parameterdescs{$param} = "variable arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 }
1582 elsif ($type eq "" && ($param eq "" or $param eq "void"))
1583 {
1584 $type="";
1585 $param="void";
1586 $parameterdescs{void} = "no arguments";
1587 }
Randy Dunlap134fe012006-12-22 01:10:50 -08001588 elsif ($type eq "" && ($param eq "struct" or $param eq "union"))
1589 # handle unnamed (anonymous) union or struct:
1590 {
1591 $type = $param;
Randy Dunlap5f8c7c92007-07-19 01:48:24 -07001592 $param = "{unnamed_" . $param . "}";
Randy Dunlap134fe012006-12-22 01:10:50 -08001593 $parameterdescs{$param} = "anonymous\n";
Randy Dunlap5f8c7c92007-07-19 01:48:24 -07001594 $anon_struct_union = 1;
Randy Dunlap134fe012006-12-22 01:10:50 -08001595 }
1596
Martin Waitza6d3fe72006-01-09 20:53:55 -08001597 # warn if parameter has no description
Randy Dunlap134fe012006-12-22 01:10:50 -08001598 # (but ignore ones starting with # as these are not parameters
1599 # but inline preprocessor statements);
1600 # also ignore unnamed structs/unions;
Randy Dunlap5f8c7c92007-07-19 01:48:24 -07001601 if (!$anon_struct_union) {
Martin Waitza6d3fe72006-01-09 20:53:55 -08001602 if (!defined $parameterdescs{$param_name} && $param_name !~ /^#/) {
1603
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 $parameterdescs{$param_name} = $undescribed;
1605
1606 if (($type eq 'function') || ($type eq 'enum')) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001607 print STDERR "Warning(${file}:$.): Function parameter ".
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 "or member '$param' not " .
1609 "described in '$declaration_name'\n";
1610 }
1611 print STDERR "Warning(${file}:$.):".
Randy Dunlap3c308792007-05-08 00:24:39 -07001612 " No description found for parameter '$param'\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 ++$warnings;
Randy Dunlap3c308792007-05-08 00:24:39 -07001614 }
1615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
1617 push @parameterlist, $param;
1618 $parametertypes{$param} = $type;
1619}
1620
1621##
1622# takes a function prototype and the name of the current file being
1623# processed and spits out all the details stored in the global
1624# arrays/hashes.
1625sub dump_function($$) {
1626 my $prototype = shift;
1627 my $file = shift;
1628
1629 $prototype =~ s/^static +//;
1630 $prototype =~ s/^extern +//;
Pavel Pisa4dc3b162005-05-01 08:59:25 -07001631 $prototype =~ s/^asmlinkage +//;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 $prototype =~ s/^inline +//;
1633 $prototype =~ s/^__inline__ +//;
Randy Dunlap32e79402006-10-11 01:22:10 -07001634 $prototype =~ s/^__inline +//;
1635 $prototype =~ s/^__always_inline +//;
1636 $prototype =~ s/^noinline +//;
Randy Dunlap0129a052006-07-30 03:03:41 -07001637 $prototype =~ s/__devinit +//;
Randy Dunlap996a07b2007-02-10 01:45:56 -08001638 $prototype =~ s/^#define\s+//; #ak added
Randy Dunlap328d2442007-02-28 20:12:10 -08001639 $prototype =~ s/__attribute__\s*\(\([a-z,]*\)\)//;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
1641 # Yes, this truly is vile. We are looking for:
1642 # 1. Return type (may be nothing if we're looking at a macro)
1643 # 2. Function name
1644 # 3. Function parameters.
1645 #
1646 # All the while we have to watch out for function pointer parameters
1647 # (which IIRC is what the two sections are for), C types (these
1648 # regexps don't even start to express all the possibilities), and
1649 # so on.
1650 #
1651 # If you mess with these regexps, it's a good idea to check that
1652 # the following functions' documentation still comes out right:
1653 # - parport_register_device (function pointer parameters)
1654 # - atomic_set (macro)
Martin Waitz9598f912006-02-01 03:06:55 -08001655 # - pci_match_device, __copy_to_user (long return type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
1657 if ($prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1658 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1659 $prototype =~ m/^(\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1660 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
Randy Dunlap94b3e032008-02-07 00:13:26 -08001661 $prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1663 $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1664 $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1665 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1666 $prototype =~ m/^(\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1667 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1668 $prototype =~ m/^(\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1669 $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
Martin Waitz9598f912006-02-01 03:06:55 -08001670 $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1671 $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
Randy Dunlap412ecd72007-02-10 22:47:33 -08001672 $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1673 $prototype =~ m/^(\w+\s+\w+\s*\*\s*\w+\s*\*\s*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 $return_type = $1;
1675 $declaration_name = $2;
1676 my $args = $3;
1677
1678 create_parameterlist($args, ',', $file);
1679 } else {
1680 print STDERR "Error(${file}:$.): cannot understand prototype: '$prototype'\n";
1681 ++$errors;
1682 return;
1683 }
1684
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001685 output_declaration($declaration_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 'function',
1687 {'function' => $declaration_name,
1688 'module' => $modulename,
1689 'functiontype' => $return_type,
1690 'parameterlist' => \@parameterlist,
1691 'parameterdescs' => \%parameterdescs,
1692 'parametertypes' => \%parametertypes,
1693 'sectionlist' => \@sectionlist,
1694 'sections' => \%sections,
1695 'purpose' => $declaration_purpose
1696 });
1697}
1698
1699sub process_file($);
1700
1701# Read the file that maps relative names to absolute names for
1702# separate source and object directories and for shadow trees.
1703if (open(SOURCE_MAP, "<.tmp_filelist.txt")) {
1704 my ($relname, $absname);
1705 while(<SOURCE_MAP>) {
1706 chop();
1707 ($relname, $absname) = (split())[0..1];
1708 $relname =~ s:^/+::;
1709 $source_map{$relname} = $absname;
1710 }
1711 close(SOURCE_MAP);
1712}
1713
1714if ($filelist) {
1715 open(FLIST,"<$filelist") or die "Can't open file list $filelist";
1716 while(<FLIST>) {
1717 chop;
1718 process_file($_);
1719 }
1720}
1721
1722foreach (@ARGV) {
1723 chomp;
1724 process_file($_);
1725}
1726if ($verbose && $errors) {
1727 print STDERR "$errors errors\n";
1728}
1729if ($verbose && $warnings) {
1730 print STDERR "$warnings warnings\n";
1731}
1732
1733exit($errors);
1734
1735sub reset_state {
1736 $function = "";
1737 %constants = ();
1738 %parameterdescs = ();
1739 %parametertypes = ();
1740 @parameterlist = ();
1741 %sections = ();
1742 @sectionlist = ();
1743 $prototype = "";
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001744
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 $state = 0;
1746}
1747
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001748sub process_state3_function($$) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 my $x = shift;
1750 my $file = shift;
1751
Randy Dunlap51f5a0c2007-07-19 01:48:24 -07001752 $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
1753
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 if ($x =~ m#\s*/\*\s+MACDOC\s*#io || ($x =~ /^#/ && $x !~ /^#define/)) {
1755 # do nothing
1756 }
1757 elsif ($x =~ /([^\{]*)/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001758 $prototype .= $1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 }
1760 if (($x =~ /\{/) || ($x =~ /\#define/) || ($x =~ /;/)) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001761 $prototype =~ s@/\*.*?\*/@@gos; # strip comments.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 $prototype =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1763 $prototype =~ s@^\s+@@gos; # strip leading spaces
1764 dump_function($prototype,$file);
1765 reset_state();
1766 }
1767}
1768
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001769sub process_state3_type($$) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 my $x = shift;
1771 my $file = shift;
1772
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 $x =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1774 $x =~ s@^\s+@@gos; # strip leading spaces
1775 $x =~ s@\s+$@@gos; # strip trailing spaces
Randy Dunlap51f5a0c2007-07-19 01:48:24 -07001776 $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
1777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 if ($x =~ /^#/) {
1779 # To distinguish preprocessor directive from regular declaration later.
1780 $x .= ";";
1781 }
1782
1783 while (1) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001784 if ( $x =~ /([^{};]*)([{};])(.*)/ ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 $prototype .= $1 . $2;
1786 ($2 eq '{') && $brcount++;
1787 ($2 eq '}') && $brcount--;
1788 if (($2 eq ';') && ($brcount == 0)) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001789 dump_declaration($prototype,$file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 reset_state();
Randy Dunlap3c308792007-05-08 00:24:39 -07001791 last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 }
1793 $x = $3;
Randy Dunlap3c308792007-05-08 00:24:39 -07001794 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 $prototype .= $x;
1796 last;
1797 }
1798 }
1799}
1800
Randy Dunlap6b5b55f2007-10-16 23:31:20 -07001801# xml_escape: replace <, >, and & in the text stream;
1802#
1803# however, formatting controls that are generated internally/locally in the
1804# kernel-doc script are not escaped here; instead, they begin life like
1805# $blankline_html (4 of '\' followed by a mnemonic + ':'), then these strings
1806# are converted to their mnemonic-expected output, without the 4 * '\' & ':',
1807# just before actual output; (this is done by local_unescape())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808sub xml_escape($) {
1809 my $text = shift;
Randy Dunlapecfb2512006-06-25 05:49:13 -07001810 if (($output_mode eq "text") || ($output_mode eq "man")) {
1811 return $text;
1812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 $text =~ s/\&/\\\\\\amp;/g;
1814 $text =~ s/\</\\\\\\lt;/g;
1815 $text =~ s/\>/\\\\\\gt;/g;
1816 return $text;
1817}
1818
Randy Dunlap6b5b55f2007-10-16 23:31:20 -07001819# convert local escape strings to html
1820# local escape strings look like: '\\\\menmonic:' (that's 4 backslashes)
1821sub local_unescape($) {
1822 my $text = shift;
1823 if (($output_mode eq "text") || ($output_mode eq "man")) {
1824 return $text;
1825 }
1826 $text =~ s/\\\\\\\\lt:/</g;
1827 $text =~ s/\\\\\\\\gt:/>/g;
1828 return $text;
1829}
1830
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831sub process_file($) {
Randy Dunlap2283a112005-07-07 15:39:26 -07001832 my $file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 my $identifier;
1834 my $func;
Randy Dunlapa21217d2007-02-10 01:46:04 -08001835 my $descr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 my $initial_section_counter = $section_counter;
1837
Randy Dunlap2283a112005-07-07 15:39:26 -07001838 if (defined($ENV{'SRCTREE'})) {
1839 $file = "$ENV{'SRCTREE'}" . "/" . "@_";
1840 }
1841 else {
1842 $file = "@_";
1843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 if (defined($source_map{$file})) {
1845 $file = $source_map{$file};
1846 }
1847
1848 if (!open(IN,"<$file")) {
1849 print STDERR "Error: Cannot open file $file\n";
1850 ++$errors;
1851 return;
1852 }
1853
1854 $section_counter = 0;
1855 while (<IN>) {
1856 if ($state == 0) {
1857 if (/$doc_start/o) {
1858 $state = 1; # next line is always the function name
Randy Dunlap850622d2006-06-25 05:48:55 -07001859 $in_doc_sect = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 }
1861 } elsif ($state == 1) { # this line is the function name (always)
1862 if (/$doc_block/o) {
1863 $state = 4;
1864 $contents = "";
1865 if ( $1 eq "" ) {
1866 $section = $section_intro;
1867 } else {
1868 $section = $1;
1869 }
Randy Dunlap3c308792007-05-08 00:24:39 -07001870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 elsif (/$doc_decl/o) {
1872 $identifier = $1;
1873 if (/\s*([\w\s]+?)\s*-/) {
1874 $identifier = $1;
1875 }
1876
1877 $state = 2;
1878 if (/-(.*)/) {
Randy Dunlap51f5a0c2007-07-19 01:48:24 -07001879 # strip leading/trailing/multiple spaces
Randy Dunlapa21217d2007-02-10 01:46:04 -08001880 $descr= $1;
1881 $descr =~ s/^\s*//;
1882 $descr =~ s/\s*$//;
1883 $descr =~ s/\s+/ /;
1884 $declaration_purpose = xml_escape($descr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 } else {
1886 $declaration_purpose = "";
1887 }
Randy Dunlap77cc23b2008-02-07 00:13:42 -08001888
1889 if (($declaration_purpose eq "") && $verbose) {
1890 print STDERR "Warning(${file}:$.): missing initial short description on line:\n";
1891 print STDERR $_;
1892 ++$warnings;
1893 }
1894
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 if ($identifier =~ m/^struct/) {
1896 $decl_type = 'struct';
1897 } elsif ($identifier =~ m/^union/) {
1898 $decl_type = 'union';
1899 } elsif ($identifier =~ m/^enum/) {
1900 $decl_type = 'enum';
1901 } elsif ($identifier =~ m/^typedef/) {
1902 $decl_type = 'typedef';
1903 } else {
1904 $decl_type = 'function';
1905 }
1906
1907 if ($verbose) {
1908 print STDERR "Info(${file}:$.): Scanning doc for $identifier\n";
1909 }
1910 } else {
1911 print STDERR "Warning(${file}:$.): Cannot understand $_ on line $.",
1912 " - I thought it was a doc line\n";
1913 ++$warnings;
1914 $state = 0;
1915 }
1916 } elsif ($state == 2) { # look for head: lines, and include content
1917 if (/$doc_sect/o) {
1918 $newsection = $1;
1919 $newcontents = $2;
1920
Randy Dunlap792aa2f2008-02-07 00:13:41 -08001921 if (($contents ne "") && ($contents ne "\n")) {
Randy Dunlap850622d2006-06-25 05:48:55 -07001922 if (!$in_doc_sect && $verbose) {
1923 print STDERR "Warning(${file}:$.): contents before sections\n";
1924 ++$warnings;
1925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 dump_section($section, xml_escape($contents));
1927 $section = $section_default;
1928 }
1929
Randy Dunlap850622d2006-06-25 05:48:55 -07001930 $in_doc_sect = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 $contents = $newcontents;
1932 if ($contents ne "") {
Randy Dunlap27205742006-10-11 01:22:12 -07001933 while ((substr($contents, 0, 1) eq " ") ||
1934 substr($contents, 0, 1) eq "\t") {
1935 $contents = substr($contents, 1);
Randy Dunlap05189492006-06-25 05:47:47 -07001936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 $contents .= "\n";
1938 }
1939 $section = $newsection;
1940 } elsif (/$doc_end/) {
1941
1942 if ($contents ne "") {
1943 dump_section($section, xml_escape($contents));
1944 $section = $section_default;
1945 $contents = "";
1946 }
1947
1948 $prototype = "";
1949 $state = 3;
1950 $brcount = 0;
Randy Dunlap232acbc2006-06-25 05:47:48 -07001951# print STDERR "end of doc comment, looking for prototype\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 } elsif (/$doc_content/) {
1953 # miguel-style comment kludge, look for blank lines after
1954 # @parameter line to signify start of description
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001955 if ($1 eq "" &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 ($section =~ m/^@/ || $section eq $section_context)) {
1957 dump_section($section, xml_escape($contents));
1958 $section = $section_default;
1959 $contents = "";
1960 } else {
1961 $contents .= $1."\n";
1962 }
1963 } else {
1964 # i dont know - bad line? ignore.
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001965 print STDERR "Warning(${file}:$.): bad line: $_";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 ++$warnings;
1967 }
Randy Dunlap232acbc2006-06-25 05:47:48 -07001968 } elsif ($state == 3) { # scanning for function '{' (end of prototype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 if ($decl_type eq 'function') {
Randy Dunlap3c308792007-05-08 00:24:39 -07001970 process_state3_function($_, $file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 } else {
Randy Dunlap3c308792007-05-08 00:24:39 -07001972 process_state3_type($_, $file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 }
1974 } elsif ($state == 4) {
1975 # Documentation block
Randy Dunlap3c308792007-05-08 00:24:39 -07001976 if (/$doc_block/) {
Johannes Bergb112e0f2007-10-24 15:08:48 -07001977 dump_doc_section($section, xml_escape($contents));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 $contents = "";
1979 $function = "";
1980 %constants = ();
1981 %parameterdescs = ();
1982 %parametertypes = ();
1983 @parameterlist = ();
1984 %sections = ();
1985 @sectionlist = ();
1986 $prototype = "";
1987 if ( $1 eq "" ) {
1988 $section = $section_intro;
1989 } else {
1990 $section = $1;
1991 }
Randy Dunlap3c308792007-05-08 00:24:39 -07001992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 elsif (/$doc_end/)
1994 {
Johannes Bergb112e0f2007-10-24 15:08:48 -07001995 dump_doc_section($section, xml_escape($contents));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 $contents = "";
1997 $function = "";
1998 %constants = ();
1999 %parameterdescs = ();
2000 %parametertypes = ();
2001 @parameterlist = ();
2002 %sections = ();
2003 @sectionlist = ();
2004 $prototype = "";
2005 $state = 0;
2006 }
2007 elsif (/$doc_content/)
2008 {
2009 if ( $1 eq "" )
2010 {
2011 $contents .= $blankline;
2012 }
2013 else
2014 {
2015 $contents .= $1 . "\n";
Randy Dunlap3c3b8092006-02-01 03:06:58 -08002016 }
Randy Dunlap3c308792007-05-08 00:24:39 -07002017 }
2018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 }
2020 if ($initial_section_counter == $section_counter) {
2021 print STDERR "Warning(${file}): no structured comments found\n";
2022 if ($output_mode eq "xml") {
2023 # The template wants at least one RefEntry here; make one.
2024 print "<refentry>\n";
2025 print " <refnamediv>\n";
2026 print " <refname>\n";
2027 print " ${file}\n";
2028 print " </refname>\n";
2029 print " <refpurpose>\n";
2030 print " Document generation inconsistency\n";
2031 print " </refpurpose>\n";
2032 print " </refnamediv>\n";
2033 print " <refsect1>\n";
2034 print " <title>\n";
2035 print " Oops\n";
2036 print " </title>\n";
2037 print " <warning>\n";
2038 print " <para>\n";
2039 print " The template for this document tried to insert\n";
2040 print " the structured comment from the file\n";
2041 print " <filename>${file}</filename> at this point,\n";
2042 print " but none was found.\n";
2043 print " This dummy section is inserted to allow\n";
2044 print " generation to continue.\n";
2045 print " </para>\n";
2046 print " </warning>\n";
2047 print " </refsect1>\n";
2048 print "</refentry>\n";
2049 }
2050 }
2051}