]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - scripts/kernel-doc
kernel-doc: bugfix - empty line in Example section
[mirror_ubuntu-bionic-kernel.git] / scripts / kernel-doc
CommitLineData
1da177e4
LT
1#!/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 ##
70c95b00 8## Copyright (C) 2005-2012 Randy Dunlap ##
1da177e4
LT
9## ##
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
1da177e4
LT
16# 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:
eda603f6 47# kernel-doc [ -docbook | -html | -text | -man | -list ] [ -no-doc-sections ]
1da177e4
LT
48# [ -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.
eda603f6 53# The -list format is for internal use by docproc.
1da177e4 54#
4b44595a
JB
55# -no-doc-sections
56# Do not output DOC: sections
57#
1da177e4 58# -function funcname
b112e0f7
JB
59# If set, then only generate documentation for the given function(s) or
60# DOC: section titles. All other functions and DOC: sections are ignored.
1da177e4
LT
61#
62# -nofunction funcname
b112e0f7
JB
63# If set, then only generate documentation for the other function(s)/DOC:
64# sections. Cannot be used together with -function (yes, that's a bug --
65# perl hackers can fix it 8))
1da177e4
LT
66#
67# c files - list of 'c' files to process
68#
69# All output goes to stdout, with errors to stderr.
70
71#
72# format of comments.
73# In the following table, (...)? signifies optional structure.
74# (...)* signifies 0 or more structure elements
75# /**
76# * function_name(:)? (- short description)?
77# (* @parameterx: (description of parameter x)?)*
78# (* a blank line)?
79# * (Description:)? (Description of function)?
80# * (section header: (section description)? )*
81# (*)?*/
82#
83# So .. the trivial example would be:
84#
85# /**
86# * my_function
b9d97328 87# */
1da177e4 88#
891dcd2f 89# If the Description: header tag is omitted, then there must be a blank line
1da177e4
LT
90# after the last parameter specification.
91# e.g.
92# /**
93# * my_function - does my stuff
94# * @my_arg: its mine damnit
95# *
3c3b809e 96# * Does my stuff explained.
1da177e4
LT
97# */
98#
99# or, could also use:
100# /**
101# * my_function - does my stuff
102# * @my_arg: its mine damnit
3c3b809e 103# * Description: Does my stuff explained.
1da177e4
LT
104# */
105# etc.
106#
b9d97328 107# Besides functions you can also write documentation for structs, unions,
3c3b809e
RD
108# enums and typedefs. Instead of the function name you must write the name
109# of the declaration; the struct/union/enum/typedef must always precede
110# the name. Nesting of declarations is not supported.
1da177e4
LT
111# Use the argument mechanism to document members or constants.
112# e.g.
113# /**
114# * struct my_struct - short description
115# * @a: first member
116# * @b: second member
3c3b809e 117# *
1da177e4
LT
118# * Longer description
119# */
120# struct my_struct {
121# int a;
122# int b;
aeec46b9
MW
123# /* private: */
124# int c;
1da177e4
LT
125# };
126#
127# All descriptions can be multiline, except the short function description.
3c3b809e
RD
128#
129# You can also add additional sections. When documenting kernel functions you
130# should document the "Context:" of the function, e.g. whether the functions
1da177e4 131# can be called form interrupts. Unlike other sections you can end it with an
3c3b809e
RD
132# empty line.
133# Example-sections should contain the string EXAMPLE so that they are marked
1da177e4
LT
134# appropriately in DocBook.
135#
136# Example:
137# /**
138# * user_function - function that can only be called in user context
139# * @a: some argument
140# * Context: !in_interrupt()
3c3b809e 141# *
1da177e4
LT
142# * Some description
143# * Example:
144# * user_function(22);
145# */
146# ...
147#
148#
149# All descriptive text is further processed, scanning for the following special
150# patterns, which are highlighted appropriately.
151#
152# 'funcname()' - function
153# '$ENVVAR' - environmental variable
154# '&struct_name' - name of a structure (up to two words including 'struct')
155# '@parameter' - name of a parameter
156# '%CONST' - name of a constant.
157
8484baaa
RD
158## init lots of data
159
1da177e4
LT
160my $errors = 0;
161my $warnings = 0;
5f8c7c98 162my $anon_struct_union = 0;
1da177e4
LT
163
164# match expressions used to find embedded type information
165my $type_constant = '\%([-_\w]+)';
166my $type_func = '(\w+)\(\)';
167my $type_param = '\@(\w+)';
3eb014a1 168my $type_struct = '\&((struct\s*)*[_\w]+)';
6b5b55f6 169my $type_struct_xml = '\\&amp;((struct\s*)*[_\w]+)';
1da177e4
LT
170my $type_env = '(\$\w+)';
171
172# Output conversion substitutions.
173# One for each output format
174
175# these work fairly well
176my %highlights_html = ( $type_constant, "<i>\$1</i>",
177 $type_func, "<b>\$1</b>",
3eb014a1
RD
178 $type_struct_xml, "<i>\$1</i>",
179 $type_env, "<b><i>\$1</i></b>",
1da177e4 180 $type_param, "<tt><b>\$1</b></tt>" );
6b5b55f6
RD
181my $local_lt = "\\\\\\\\lt:";
182my $local_gt = "\\\\\\\\gt:";
183my $blankline_html = $local_lt . "p" . $local_gt; # was "<p>"
1da177e4
LT
184
185# XML, docbook format
186my %highlights_xml = ( "([^=])\\\"([^\\\"<]+)\\\"", "\$1<quote>\$2</quote>",
187 $type_constant, "<constant>\$1</constant>",
188 $type_func, "<function>\$1</function>",
5c98fc03 189 $type_struct_xml, "<structname>\$1</structname>",
1da177e4
LT
190 $type_env, "<envar>\$1</envar>",
191 $type_param, "<parameter>\$1</parameter>" );
5c98fc03 192my $blankline_xml = $local_lt . "/para" . $local_gt . $local_lt . "para" . $local_gt . "\n";
1da177e4
LT
193
194# gnome, docbook format
195my %highlights_gnome = ( $type_constant, "<replaceable class=\"option\">\$1</replaceable>",
196 $type_func, "<function>\$1</function>",
197 $type_struct, "<structname>\$1</structname>",
198 $type_env, "<envar>\$1</envar>",
199 $type_param, "<parameter>\$1</parameter>" );
200my $blankline_gnome = "</para><para>\n";
201
202# these are pretty rough
203my %highlights_man = ( $type_constant, "\$1",
204 $type_func, "\\\\fB\$1\\\\fP",
205 $type_struct, "\\\\fI\$1\\\\fP",
206 $type_param, "\\\\fI\$1\\\\fP" );
207my $blankline_man = "";
208
209# text-mode
210my %highlights_text = ( $type_constant, "\$1",
211 $type_func, "\$1",
212 $type_struct, "\$1",
213 $type_param, "\$1" );
214my $blankline_text = "";
215
eda603f6
JB
216# list mode
217my %highlights_list = ( $type_constant, "\$1",
218 $type_func, "\$1",
219 $type_struct, "\$1",
220 $type_param, "\$1" );
221my $blankline_list = "";
1da177e4 222
1da177e4 223# read arguments
b9d97328 224if ($#ARGV == -1) {
1da177e4
LT
225 usage();
226}
227
8484baaa
RD
228my $kernelversion;
229my $dohighlight = "";
230
1da177e4
LT
231my $verbose = 0;
232my $output_mode = "man";
e314ba31 233my $output_preformatted = 0;
4b44595a 234my $no_doc_sections = 0;
1da177e4
LT
235my %highlights = %highlights_man;
236my $blankline = $blankline_man;
237my $modulename = "Kernel API";
238my $function_only = 0;
3c3b809e
RD
239my $man_date = ('January', 'February', 'March', 'April', 'May', 'June',
240 'July', 'August', 'September', 'October',
241 'November', 'December')[(localtime)[4]] .
1da177e4
LT
242 " " . ((localtime)[5]+1900);
243
8484baaa 244# Essentially these are globals.
b9d97328
RD
245# They probably want to be tidied up, made more localised or something.
246# CAVEAT EMPTOR! Some of the others I localised may not want to be, which
1da177e4 247# could cause "use of undefined value" or other bugs.
b9d97328
RD
248my ($function, %function_table, %parametertypes, $declaration_purpose);
249my ($type, $declaration_name, $return_type);
1c32fd0c 250my ($newsection, $newcontents, $prototype, $brcount, %source_map);
1da177e4 251
bd0e88e5
RD
252if (defined($ENV{'KBUILD_VERBOSE'})) {
253 $verbose = "$ENV{'KBUILD_VERBOSE'}";
254}
255
3c3b809e 256# Generated docbook code is inserted in a template at a point where
1da177e4
LT
257# docbook v3.1 requires a non-zero sequence of RefEntry's; see:
258# http://www.oasis-open.org/docbook/documentation/reference/html/refentry.html
259# We keep track of number of generated entries and generate a dummy
260# if needs be to ensure the expanded template can be postprocessed
261# into html.
262my $section_counter = 0;
263
264my $lineprefix="";
265
266# states
267# 0 - normal code
268# 1 - looking for function name
269# 2 - scanning field start.
270# 3 - scanning prototype.
271# 4 - documentation block
272my $state;
850622df 273my $in_doc_sect;
1da177e4
LT
274
275#declaration types: can be
276# 'function', 'struct', 'union', 'enum', 'typedef'
277my $decl_type;
278
279my $doc_special = "\@\%\$\&";
280
281my $doc_start = '^/\*\*\s*$'; # Allow whitespace at end of comment start.
282my $doc_end = '\*/';
283my $doc_com = '\s*\*\s*';
b9d97328
RD
284my $doc_decl = $doc_com . '(\w+)';
285my $doc_sect = $doc_com . '([' . $doc_special . ']?[\w\s]+):(.*)';
286my $doc_content = $doc_com . '(.*)';
287my $doc_block = $doc_com . 'DOC:\s*(.*)?';
1da177e4
LT
288
289my %constants;
290my %parameterdescs;
291my @parameterlist;
292my %sections;
293my @sectionlist;
a1d94aa5
RD
294my $sectcheck;
295my $struct_actual;
1da177e4
LT
296
297my $contents = "";
298my $section_default = "Description"; # default section
299my $section_intro = "Introduction";
300my $section = $section_default;
301my $section_context = "Context";
302
303my $undescribed = "-- undescribed --";
304
305reset_state();
306
307while ($ARGV[0] =~ m/^-(.*)/) {
308 my $cmd = shift @ARGV;
309 if ($cmd eq "-html") {
310 $output_mode = "html";
311 %highlights = %highlights_html;
312 $blankline = $blankline_html;
313 } elsif ($cmd eq "-man") {
314 $output_mode = "man";
315 %highlights = %highlights_man;
316 $blankline = $blankline_man;
317 } elsif ($cmd eq "-text") {
318 $output_mode = "text";
319 %highlights = %highlights_text;
320 $blankline = $blankline_text;
321 } elsif ($cmd eq "-docbook") {
322 $output_mode = "xml";
323 %highlights = %highlights_xml;
324 $blankline = $blankline_xml;
eda603f6
JB
325 } elsif ($cmd eq "-list") {
326 $output_mode = "list";
327 %highlights = %highlights_list;
328 $blankline = $blankline_list;
1da177e4
LT
329 } elsif ($cmd eq "-gnome") {
330 $output_mode = "gnome";
331 %highlights = %highlights_gnome;
332 $blankline = $blankline_gnome;
333 } elsif ($cmd eq "-module") { # not needed for XML, inherits from calling document
334 $modulename = shift @ARGV;
335 } elsif ($cmd eq "-function") { # to only output specific functions
336 $function_only = 1;
337 $function = shift @ARGV;
338 $function_table{$function} = 1;
339 } elsif ($cmd eq "-nofunction") { # to only output specific functions
340 $function_only = 2;
341 $function = shift @ARGV;
342 $function_table{$function} = 1;
343 } elsif ($cmd eq "-v") {
344 $verbose = 1;
345 } elsif (($cmd eq "-h") || ($cmd eq "--help")) {
346 usage();
4b44595a
JB
347 } elsif ($cmd eq '-no-doc-sections') {
348 $no_doc_sections = 1;
1da177e4
LT
349 }
350}
351
8484baaa
RD
352# continue execution near EOF;
353
354sub usage {
355 print "Usage: $0 [ -v ] [ -docbook | -html | -text | -man | -list ]\n";
356 print " [ -no-doc-sections ]\n";
357 print " [ -function funcname [ -function funcname ...] ]\n";
358 print " [ -nofunction funcname [ -nofunction funcname ...] ]\n";
359 print " c source file(s) > outputfile\n";
360 print " -v : verbose output, more warnings & other info listed\n";
361 exit 1;
362}
363
53f049fa
BP
364# get kernel version from env
365sub get_kernel_version() {
1b9bc22d 366 my $version = 'unknown kernel version';
53f049fa
BP
367
368 if (defined($ENV{'KERNELVERSION'})) {
369 $version = $ENV{'KERNELVERSION'};
370 }
371 return $version;
372}
1da177e4
LT
373
374##
375# dumps section contents to arrays/hashes intended for that purpose.
376#
377sub dump_section {
94dc7ad5 378 my $file = shift;
1da177e4
LT
379 my $name = shift;
380 my $contents = join "\n", @_;
381
382 if ($name =~ m/$type_constant/) {
383 $name = $1;
384# print STDERR "constant section '$1' = '$contents'\n";
385 $constants{$name} = $contents;
386 } elsif ($name =~ m/$type_param/) {
387# print STDERR "parameter def '$1' = '$contents'\n";
388 $name = $1;
389 $parameterdescs{$name} = $contents;
a1d94aa5 390 $sectcheck = $sectcheck . $name . " ";
ced69090
RD
391 } elsif ($name eq "@\.\.\.") {
392# print STDERR "parameter def '...' = '$contents'\n";
393 $name = "...";
394 $parameterdescs{$name} = $contents;
a1d94aa5 395 $sectcheck = $sectcheck . $name . " ";
1da177e4
LT
396 } else {
397# print STDERR "other section '$name' = '$contents'\n";
94dc7ad5
RD
398 if (defined($sections{$name}) && ($sections{$name} ne "")) {
399 print STDERR "Error(${file}:$.): duplicate section name '$name'\n";
400 ++$errors;
401 }
1da177e4
LT
402 $sections{$name} = $contents;
403 push @sectionlist, $name;
404 }
405}
406
b112e0f7
JB
407##
408# dump DOC: section after checking that it should go out
409#
410sub dump_doc_section {
94dc7ad5 411 my $file = shift;
b112e0f7
JB
412 my $name = shift;
413 my $contents = join "\n", @_;
414
4b44595a
JB
415 if ($no_doc_sections) {
416 return;
417 }
418
b112e0f7
JB
419 if (($function_only == 0) ||
420 ( $function_only == 1 && defined($function_table{$name})) ||
421 ( $function_only == 2 && !defined($function_table{$name})))
422 {
94dc7ad5 423 dump_section($file, $name, $contents);
b112e0f7
JB
424 output_blockhead({'sectionlist' => \@sectionlist,
425 'sections' => \%sections,
426 'module' => $modulename,
427 'content-only' => ($function_only != 0), });
428 }
429}
430
1da177e4
LT
431##
432# output function
433#
434# parameterdescs, a hash.
435# function => "function name"
436# parameterlist => @list of parameters
437# parameterdescs => %parameter descriptions
438# sectionlist => @list of sections
a21217da 439# sections => %section descriptions
3c3b809e 440#
1da177e4
LT
441
442sub output_highlight {
443 my $contents = join "\n",@_;
444 my $line;
445
446# DEBUG
447# if (!defined $contents) {
448# use Carp;
449# confess "output_highlight got called with no args?\n";
450# }
451
5c98fc03 452 if ($output_mode eq "html" || $output_mode eq "xml") {
6b5b55f6
RD
453 $contents = local_unescape($contents);
454 # convert data read & converted thru xml_escape() into &xyz; format:
2b35f4d9 455 $contents =~ s/\\\\\\/\&/g;
6b5b55f6 456 }
3eb014a1 457# print STDERR "contents b4:$contents\n";
1da177e4
LT
458 eval $dohighlight;
459 die $@ if $@;
3eb014a1
RD
460# print STDERR "contents af:$contents\n";
461
1da177e4 462 foreach $line (split "\n", $contents) {
3c308798 463 if ($line eq ""){
e314ba31
DS
464 if (! $output_preformatted) {
465 print $lineprefix, local_unescape($blankline);
466 }
1da177e4 467 } else {
3c308798 468 $line =~ s/\\\\\\/\&/g;
cdccb316
RD
469 if ($output_mode eq "man" && substr($line, 0, 1) eq ".") {
470 print "\\&$line";
471 } else {
472 print $lineprefix, $line;
473 }
1da177e4
LT
474 }
475 print "\n";
476 }
477}
478
479#output sections in html
480sub output_section_html(%) {
481 my %args = %{$_[0]};
482 my $section;
483
484 foreach $section (@{$args{'sectionlist'}}) {
485 print "<h3>$section</h3>\n";
486 print "<blockquote>\n";
487 output_highlight($args{'sections'}{$section});
488 print "</blockquote>\n";
3c3b809e 489 }
1da177e4
LT
490}
491
492# output enum in html
493sub output_enum_html(%) {
494 my %args = %{$_[0]};
495 my ($parameter);
496 my $count;
b9d97328 497 print "<h2>enum " . $args{'enum'} . "</h2>\n";
1da177e4 498
b9d97328 499 print "<b>enum " . $args{'enum'} . "</b> {<br>\n";
1da177e4
LT
500 $count = 0;
501 foreach $parameter (@{$args{'parameterlist'}}) {
b9d97328 502 print " <b>" . $parameter . "</b>";
1da177e4
LT
503 if ($count != $#{$args{'parameterlist'}}) {
504 $count++;
505 print ",\n";
506 }
507 print "<br>";
508 }
509 print "};<br>\n";
510
511 print "<h3>Constants</h3>\n";
512 print "<dl>\n";
513 foreach $parameter (@{$args{'parameterlist'}}) {
b9d97328 514 print "<dt><b>" . $parameter . "</b>\n";
1da177e4
LT
515 print "<dd>";
516 output_highlight($args{'parameterdescs'}{$parameter});
517 }
518 print "</dl>\n";
519 output_section_html(@_);
520 print "<hr>\n";
521}
522
d28bee0c 523# output typedef in html
1da177e4
LT
524sub output_typedef_html(%) {
525 my %args = %{$_[0]};
526 my ($parameter);
527 my $count;
b9d97328 528 print "<h2>typedef " . $args{'typedef'} . "</h2>\n";
1da177e4 529
b9d97328 530 print "<b>typedef " . $args{'typedef'} . "</b>\n";
1da177e4
LT
531 output_section_html(@_);
532 print "<hr>\n";
533}
534
535# output struct in html
536sub output_struct_html(%) {
537 my %args = %{$_[0]};
538 my ($parameter);
539
b9d97328
RD
540 print "<h2>" . $args{'type'} . " " . $args{'struct'} . " - " . $args{'purpose'} . "</h2>\n";
541 print "<b>" . $args{'type'} . " " . $args{'struct'} . "</b> {<br>\n";
1da177e4
LT
542 foreach $parameter (@{$args{'parameterlist'}}) {
543 if ($parameter =~ /^#/) {
544 print "$parameter<br>\n";
545 next;
546 }
547 my $parameter_name = $parameter;
548 $parameter_name =~ s/\[.*//;
549
3c308798 550 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
1da177e4
LT
551 $type = $args{'parametertypes'}{$parameter};
552 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
553 # pointer-to-function
3eb014a1 554 print "&nbsp; &nbsp; <i>$1</i><b>$parameter</b>) <i>($2)</i>;<br>\n";
1da177e4 555 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
3eb014a1
RD
556 # bitfield
557 print "&nbsp; &nbsp; <i>$1</i> <b>$parameter</b>$2;<br>\n";
1da177e4 558 } else {
3eb014a1 559 print "&nbsp; &nbsp; <i>$type</i> <b>$parameter</b>;<br>\n";
1da177e4
LT
560 }
561 }
562 print "};<br>\n";
563
564 print "<h3>Members</h3>\n";
565 print "<dl>\n";
566 foreach $parameter (@{$args{'parameterlist'}}) {
567 ($parameter =~ /^#/) && next;
568
569 my $parameter_name = $parameter;
570 $parameter_name =~ s/\[.*//;
571
3c308798 572 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
b9d97328 573 print "<dt><b>" . $parameter . "</b>\n";
1da177e4
LT
574 print "<dd>";
575 output_highlight($args{'parameterdescs'}{$parameter_name});
576 }
577 print "</dl>\n";
578 output_section_html(@_);
579 print "<hr>\n";
580}
581
582# output function in html
583sub output_function_html(%) {
584 my %args = %{$_[0]};
585 my ($parameter, $section);
586 my $count;
1da177e4 587
b9d97328
RD
588 print "<h2>" . $args{'function'} . " - " . $args{'purpose'} . "</h2>\n";
589 print "<i>" . $args{'functiontype'} . "</i>\n";
590 print "<b>" . $args{'function'} . "</b>\n";
1da177e4
LT
591 print "(";
592 $count = 0;
593 foreach $parameter (@{$args{'parameterlist'}}) {
594 $type = $args{'parametertypes'}{$parameter};
595 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
596 # pointer-to-function
597 print "<i>$1</i><b>$parameter</b>) <i>($2)</i>";
598 } else {
b9d97328 599 print "<i>" . $type . "</i> <b>" . $parameter . "</b>";
1da177e4
LT
600 }
601 if ($count != $#{$args{'parameterlist'}}) {
602 $count++;
603 print ",\n";
604 }
605 }
606 print ")\n";
607
608 print "<h3>Arguments</h3>\n";
609 print "<dl>\n";
610 foreach $parameter (@{$args{'parameterlist'}}) {
611 my $parameter_name = $parameter;
612 $parameter_name =~ s/\[.*//;
613
3c308798 614 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
b9d97328 615 print "<dt><b>" . $parameter . "</b>\n";
1da177e4
LT
616 print "<dd>";
617 output_highlight($args{'parameterdescs'}{$parameter_name});
618 }
619 print "</dl>\n";
620 output_section_html(@_);
621 print "<hr>\n";
622}
623
b112e0f7
JB
624# output DOC: block header in html
625sub output_blockhead_html(%) {
1da177e4
LT
626 my %args = %{$_[0]};
627 my ($parameter, $section);
628 my $count;
629
630 foreach $section (@{$args{'sectionlist'}}) {
631 print "<h3>$section</h3>\n";
632 print "<ul>\n";
633 output_highlight($args{'sections'}{$section});
634 print "</ul>\n";
635 }
636 print "<hr>\n";
637}
638
639sub output_section_xml(%) {
640 my %args = %{$_[0]};
3c3b809e 641 my $section;
1da177e4
LT
642 # print out each section
643 $lineprefix=" ";
644 foreach $section (@{$args{'sectionlist'}}) {
c73894c1
RW
645 print "<refsect1>\n";
646 print "<title>$section</title>\n";
1da177e4 647 if ($section =~ m/EXAMPLE/i) {
c73894c1 648 print "<informalexample><programlisting>\n";
e314ba31 649 $output_preformatted = 1;
c73894c1
RW
650 } else {
651 print "<para>\n";
1da177e4
LT
652 }
653 output_highlight($args{'sections'}{$section});
e314ba31 654 $output_preformatted = 0;
1da177e4 655 if ($section =~ m/EXAMPLE/i) {
c73894c1
RW
656 print "</programlisting></informalexample>\n";
657 } else {
658 print "</para>\n";
1da177e4 659 }
c73894c1 660 print "</refsect1>\n";
1da177e4
LT
661 }
662}
663
664# output function in XML DocBook
665sub output_function_xml(%) {
666 my %args = %{$_[0]};
667 my ($parameter, $section);
668 my $count;
669 my $id;
670
b9d97328 671 $id = "API-" . $args{'function'};
1da177e4
LT
672 $id =~ s/[^A-Za-z0-9]/-/g;
673
5449bc94 674 print "<refentry id=\"$id\">\n";
8b0c2d98
MW
675 print "<refentryinfo>\n";
676 print " <title>LINUX</title>\n";
677 print " <productname>Kernel Hackers Manual</productname>\n";
678 print " <date>$man_date</date>\n";
679 print "</refentryinfo>\n";
1da177e4 680 print "<refmeta>\n";
b9d97328 681 print " <refentrytitle><phrase>" . $args{'function'} . "</phrase></refentrytitle>\n";
8b0c2d98 682 print " <manvolnum>9</manvolnum>\n";
0366299b 683 print " <refmiscinfo class=\"version\">" . $kernelversion . "</refmiscinfo>\n";
1da177e4
LT
684 print "</refmeta>\n";
685 print "<refnamediv>\n";
b9d97328 686 print " <refname>" . $args{'function'} . "</refname>\n";
1da177e4
LT
687 print " <refpurpose>\n";
688 print " ";
689 output_highlight ($args{'purpose'});
690 print " </refpurpose>\n";
691 print "</refnamediv>\n";
692
693 print "<refsynopsisdiv>\n";
694 print " <title>Synopsis</title>\n";
695 print " <funcsynopsis><funcprototype>\n";
b9d97328
RD
696 print " <funcdef>" . $args{'functiontype'} . " ";
697 print "<function>" . $args{'function'} . " </function></funcdef>\n";
1da177e4
LT
698
699 $count = 0;
700 if ($#{$args{'parameterlist'}} >= 0) {
701 foreach $parameter (@{$args{'parameterlist'}}) {
702 $type = $args{'parametertypes'}{$parameter};
703 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
704 # pointer-to-function
705 print " <paramdef>$1<parameter>$parameter</parameter>)\n";
706 print " <funcparams>$2</funcparams></paramdef>\n";
707 } else {
b9d97328 708 print " <paramdef>" . $type;
1da177e4
LT
709 print " <parameter>$parameter</parameter></paramdef>\n";
710 }
711 }
712 } else {
6013d544 713 print " <void/>\n";
1da177e4
LT
714 }
715 print " </funcprototype></funcsynopsis>\n";
716 print "</refsynopsisdiv>\n";
717
718 # print parameters
719 print "<refsect1>\n <title>Arguments</title>\n";
720 if ($#{$args{'parameterlist'}} >= 0) {
721 print " <variablelist>\n";
722 foreach $parameter (@{$args{'parameterlist'}}) {
723 my $parameter_name = $parameter;
724 $parameter_name =~ s/\[.*//;
725
726 print " <varlistentry>\n <term><parameter>$parameter</parameter></term>\n";
727 print " <listitem>\n <para>\n";
728 $lineprefix=" ";
729 output_highlight($args{'parameterdescs'}{$parameter_name});
730 print " </para>\n </listitem>\n </varlistentry>\n";
731 }
732 print " </variablelist>\n";
733 } else {
734 print " <para>\n None\n </para>\n";
735 }
736 print "</refsect1>\n";
737
738 output_section_xml(@_);
739 print "</refentry>\n\n";
740}
741
742# output struct in XML DocBook
743sub output_struct_xml(%) {
744 my %args = %{$_[0]};
745 my ($parameter, $section);
746 my $id;
747
b9d97328 748 $id = "API-struct-" . $args{'struct'};
1da177e4
LT
749 $id =~ s/[^A-Za-z0-9]/-/g;
750
5449bc94 751 print "<refentry id=\"$id\">\n";
8b0c2d98
MW
752 print "<refentryinfo>\n";
753 print " <title>LINUX</title>\n";
754 print " <productname>Kernel Hackers Manual</productname>\n";
755 print " <date>$man_date</date>\n";
756 print "</refentryinfo>\n";
1da177e4 757 print "<refmeta>\n";
b9d97328 758 print " <refentrytitle><phrase>" . $args{'type'} . " " . $args{'struct'} . "</phrase></refentrytitle>\n";
8b0c2d98 759 print " <manvolnum>9</manvolnum>\n";
0366299b 760 print " <refmiscinfo class=\"version\">" . $kernelversion . "</refmiscinfo>\n";
1da177e4
LT
761 print "</refmeta>\n";
762 print "<refnamediv>\n";
b9d97328 763 print " <refname>" . $args{'type'} . " " . $args{'struct'} . "</refname>\n";
1da177e4
LT
764 print " <refpurpose>\n";
765 print " ";
766 output_highlight ($args{'purpose'});
767 print " </refpurpose>\n";
768 print "</refnamediv>\n";
769
770 print "<refsynopsisdiv>\n";
771 print " <title>Synopsis</title>\n";
772 print " <programlisting>\n";
b9d97328 773 print $args{'type'} . " " . $args{'struct'} . " {\n";
1da177e4
LT
774 foreach $parameter (@{$args{'parameterlist'}}) {
775 if ($parameter =~ /^#/) {
2b35f4d9
RD
776 my $prm = $parameter;
777 # convert data read & converted thru xml_escape() into &xyz; format:
778 # This allows us to have #define macros interspersed in a struct.
779 $prm =~ s/\\\\\\/\&/g;
780 print "$prm\n";
1da177e4
LT
781 next;
782 }
783
784 my $parameter_name = $parameter;
785 $parameter_name =~ s/\[.*//;
786
787 defined($args{'parameterdescs'}{$parameter_name}) || next;
3c308798 788 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
1da177e4
LT
789 $type = $args{'parametertypes'}{$parameter};
790 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
791 # pointer-to-function
792 print " $1 $parameter) ($2);\n";
793 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
51f5a0c8 794 # bitfield
1da177e4
LT
795 print " $1 $parameter$2;\n";
796 } else {
b9d97328 797 print " " . $type . " " . $parameter . ";\n";
1da177e4
LT
798 }
799 }
800 print "};";
801 print " </programlisting>\n";
802 print "</refsynopsisdiv>\n";
803
804 print " <refsect1>\n";
805 print " <title>Members</title>\n";
806
39f00c08 807 if ($#{$args{'parameterlist'}} >= 0) {
1da177e4
LT
808 print " <variablelist>\n";
809 foreach $parameter (@{$args{'parameterlist'}}) {
810 ($parameter =~ /^#/) && next;
811
812 my $parameter_name = $parameter;
813 $parameter_name =~ s/\[.*//;
814
815 defined($args{'parameterdescs'}{$parameter_name}) || next;
816 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
817 print " <varlistentry>";
818 print " <term>$parameter</term>\n";
819 print " <listitem><para>\n";
820 output_highlight($args{'parameterdescs'}{$parameter_name});
821 print " </para></listitem>\n";
822 print " </varlistentry>\n";
823 }
824 print " </variablelist>\n";
39f00c08
RD
825 } else {
826 print " <para>\n None\n </para>\n";
827 }
1da177e4
LT
828 print " </refsect1>\n";
829
830 output_section_xml(@_);
831
832 print "</refentry>\n\n";
833}
834
835# output enum in XML DocBook
836sub output_enum_xml(%) {
837 my %args = %{$_[0]};
838 my ($parameter, $section);
839 my $count;
840 my $id;
841
b9d97328 842 $id = "API-enum-" . $args{'enum'};
1da177e4
LT
843 $id =~ s/[^A-Za-z0-9]/-/g;
844
5449bc94 845 print "<refentry id=\"$id\">\n";
8b0c2d98
MW
846 print "<refentryinfo>\n";
847 print " <title>LINUX</title>\n";
848 print " <productname>Kernel Hackers Manual</productname>\n";
849 print " <date>$man_date</date>\n";
850 print "</refentryinfo>\n";
1da177e4 851 print "<refmeta>\n";
b9d97328 852 print " <refentrytitle><phrase>enum " . $args{'enum'} . "</phrase></refentrytitle>\n";
8b0c2d98 853 print " <manvolnum>9</manvolnum>\n";
0366299b 854 print " <refmiscinfo class=\"version\">" . $kernelversion . "</refmiscinfo>\n";
1da177e4
LT
855 print "</refmeta>\n";
856 print "<refnamediv>\n";
b9d97328 857 print " <refname>enum " . $args{'enum'} . "</refname>\n";
1da177e4
LT
858 print " <refpurpose>\n";
859 print " ";
860 output_highlight ($args{'purpose'});
861 print " </refpurpose>\n";
862 print "</refnamediv>\n";
863
864 print "<refsynopsisdiv>\n";
865 print " <title>Synopsis</title>\n";
866 print " <programlisting>\n";
b9d97328 867 print "enum " . $args{'enum'} . " {\n";
1da177e4
LT
868 $count = 0;
869 foreach $parameter (@{$args{'parameterlist'}}) {
3c308798
RD
870 print " $parameter";
871 if ($count != $#{$args{'parameterlist'}}) {
1da177e4
LT
872 $count++;
873 print ",";
3c308798 874 }
1da177e4
LT
875 print "\n";
876 }
877 print "};";
878 print " </programlisting>\n";
879 print "</refsynopsisdiv>\n";
880
881 print "<refsect1>\n";
3c3b809e 882 print " <title>Constants</title>\n";
1da177e4
LT
883 print " <variablelist>\n";
884 foreach $parameter (@{$args{'parameterlist'}}) {
885 my $parameter_name = $parameter;
886 $parameter_name =~ s/\[.*//;
887
888 print " <varlistentry>";
889 print " <term>$parameter</term>\n";
890 print " <listitem><para>\n";
891 output_highlight($args{'parameterdescs'}{$parameter_name});
892 print " </para></listitem>\n";
893 print " </varlistentry>\n";
894 }
895 print " </variablelist>\n";
896 print "</refsect1>\n";
897
898 output_section_xml(@_);
899
900 print "</refentry>\n\n";
901}
902
903# output typedef in XML DocBook
904sub output_typedef_xml(%) {
905 my %args = %{$_[0]};
906 my ($parameter, $section);
907 my $id;
908
b9d97328 909 $id = "API-typedef-" . $args{'typedef'};
1da177e4
LT
910 $id =~ s/[^A-Za-z0-9]/-/g;
911
5449bc94 912 print "<refentry id=\"$id\">\n";
8b0c2d98
MW
913 print "<refentryinfo>\n";
914 print " <title>LINUX</title>\n";
915 print " <productname>Kernel Hackers Manual</productname>\n";
916 print " <date>$man_date</date>\n";
917 print "</refentryinfo>\n";
1da177e4 918 print "<refmeta>\n";
b9d97328 919 print " <refentrytitle><phrase>typedef " . $args{'typedef'} . "</phrase></refentrytitle>\n";
8b0c2d98 920 print " <manvolnum>9</manvolnum>\n";
1da177e4
LT
921 print "</refmeta>\n";
922 print "<refnamediv>\n";
b9d97328 923 print " <refname>typedef " . $args{'typedef'} . "</refname>\n";
1da177e4
LT
924 print " <refpurpose>\n";
925 print " ";
926 output_highlight ($args{'purpose'});
927 print " </refpurpose>\n";
928 print "</refnamediv>\n";
929
930 print "<refsynopsisdiv>\n";
931 print " <title>Synopsis</title>\n";
b9d97328 932 print " <synopsis>typedef " . $args{'typedef'} . ";</synopsis>\n";
1da177e4
LT
933 print "</refsynopsisdiv>\n";
934
935 output_section_xml(@_);
936
937 print "</refentry>\n\n";
938}
939
940# output in XML DocBook
b112e0f7 941sub output_blockhead_xml(%) {
1da177e4
LT
942 my %args = %{$_[0]};
943 my ($parameter, $section);
944 my $count;
945
946 my $id = $args{'module'};
947 $id =~ s/[^A-Za-z0-9]/-/g;
948
949 # print out each section
950 $lineprefix=" ";
951 foreach $section (@{$args{'sectionlist'}}) {
b112e0f7
JB
952 if (!$args{'content-only'}) {
953 print "<refsect1>\n <title>$section</title>\n";
954 }
1da177e4
LT
955 if ($section =~ m/EXAMPLE/i) {
956 print "<example><para>\n";
e314ba31 957 $output_preformatted = 1;
b112e0f7
JB
958 } else {
959 print "<para>\n";
1da177e4
LT
960 }
961 output_highlight($args{'sections'}{$section});
e314ba31 962 $output_preformatted = 0;
1da177e4
LT
963 if ($section =~ m/EXAMPLE/i) {
964 print "</para></example>\n";
b112e0f7
JB
965 } else {
966 print "</para>";
967 }
968 if (!$args{'content-only'}) {
969 print "\n</refsect1>\n";
1da177e4 970 }
1da177e4
LT
971 }
972
973 print "\n\n";
974}
975
976# output in XML DocBook
977sub output_function_gnome {
978 my %args = %{$_[0]};
979 my ($parameter, $section);
980 my $count;
981 my $id;
982
b9d97328 983 $id = $args{'module'} . "-" . $args{'function'};
1da177e4
LT
984 $id =~ s/[^A-Za-z0-9]/-/g;
985
986 print "<sect2>\n";
b9d97328 987 print " <title id=\"$id\">" . $args{'function'} . "</title>\n";
1da177e4
LT
988
989 print " <funcsynopsis>\n";
b9d97328
RD
990 print " <funcdef>" . $args{'functiontype'} . " ";
991 print "<function>" . $args{'function'} . " ";
1da177e4
LT
992 print "</function></funcdef>\n";
993
994 $count = 0;
995 if ($#{$args{'parameterlist'}} >= 0) {
996 foreach $parameter (@{$args{'parameterlist'}}) {
997 $type = $args{'parametertypes'}{$parameter};
998 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
999 # pointer-to-function
1000 print " <paramdef>$1 <parameter>$parameter</parameter>)\n";
1001 print " <funcparams>$2</funcparams></paramdef>\n";
1002 } else {
b9d97328 1003 print " <paramdef>" . $type;
1da177e4
LT
1004 print " <parameter>$parameter</parameter></paramdef>\n";
1005 }
1006 }
1007 } else {
1008 print " <void>\n";
1009 }
1010 print " </funcsynopsis>\n";
1011 if ($#{$args{'parameterlist'}} >= 0) {
1012 print " <informaltable pgwide=\"1\" frame=\"none\" role=\"params\">\n";
1013 print "<tgroup cols=\"2\">\n";
1014 print "<colspec colwidth=\"2*\">\n";
1015 print "<colspec colwidth=\"8*\">\n";
1016 print "<tbody>\n";
1017 foreach $parameter (@{$args{'parameterlist'}}) {
1018 my $parameter_name = $parameter;
1019 $parameter_name =~ s/\[.*//;
1020
1021 print " <row><entry align=\"right\"><parameter>$parameter</parameter></entry>\n";
1022 print " <entry>\n";
1023 $lineprefix=" ";
1024 output_highlight($args{'parameterdescs'}{$parameter_name});
1025 print " </entry></row>\n";
1026 }
1027 print " </tbody></tgroup></informaltable>\n";
1028 } else {
1029 print " <para>\n None\n </para>\n";
1030 }
1031
1032 # print out each section
1033 $lineprefix=" ";
1034 foreach $section (@{$args{'sectionlist'}}) {
1035 print "<simplesect>\n <title>$section</title>\n";
1036 if ($section =~ m/EXAMPLE/i) {
1037 print "<example><programlisting>\n";
e314ba31 1038 $output_preformatted = 1;
1da177e4
LT
1039 } else {
1040 }
1041 print "<para>\n";
1042 output_highlight($args{'sections'}{$section});
e314ba31 1043 $output_preformatted = 0;
1da177e4
LT
1044 print "</para>\n";
1045 if ($section =~ m/EXAMPLE/i) {
1046 print "</programlisting></example>\n";
1047 } else {
1048 }
1049 print " </simplesect>\n";
1050 }
1051
1052 print "</sect2>\n\n";
1053}
1054
1055##
1056# output function in man
1057sub output_function_man(%) {
1058 my %args = %{$_[0]};
1059 my ($parameter, $section);
1060 my $count;
1061
1062 print ".TH \"$args{'function'}\" 9 \"$args{'function'}\" \"$man_date\" \"Kernel Hacker's Manual\" LINUX\n";
1063
1064 print ".SH NAME\n";
b9d97328 1065 print $args{'function'} . " \\- " . $args{'purpose'} . "\n";
1da177e4
LT
1066
1067 print ".SH SYNOPSIS\n";
a21217da 1068 if ($args{'functiontype'} ne "") {
b9d97328 1069 print ".B \"" . $args{'functiontype'} . "\" " . $args{'function'} . "\n";
a21217da 1070 } else {
b9d97328 1071 print ".B \"" . $args{'function'} . "\n";
a21217da 1072 }
1da177e4
LT
1073 $count = 0;
1074 my $parenth = "(";
1075 my $post = ",";
1076 foreach my $parameter (@{$args{'parameterlist'}}) {
1077 if ($count == $#{$args{'parameterlist'}}) {
1078 $post = ");";
1079 }
1080 $type = $args{'parametertypes'}{$parameter};
1081 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1082 # pointer-to-function
b9d97328 1083 print ".BI \"" . $parenth . $1 . "\" " . $parameter . " \") (" . $2 . ")" . $post . "\"\n";
1da177e4
LT
1084 } else {
1085 $type =~ s/([^\*])$/$1 /;
b9d97328 1086 print ".BI \"" . $parenth . $type . "\" " . $parameter . " \"" . $post . "\"\n";
1da177e4
LT
1087 }
1088 $count++;
1089 $parenth = "";
1090 }
1091
1092 print ".SH ARGUMENTS\n";
1093 foreach $parameter (@{$args{'parameterlist'}}) {
1094 my $parameter_name = $parameter;
1095 $parameter_name =~ s/\[.*//;
1096
b9d97328 1097 print ".IP \"" . $parameter . "\" 12\n";
1da177e4
LT
1098 output_highlight($args{'parameterdescs'}{$parameter_name});
1099 }
1100 foreach $section (@{$args{'sectionlist'}}) {
1101 print ".SH \"", uc $section, "\"\n";
1102 output_highlight($args{'sections'}{$section});
1103 }
1104}
1105
1106##
1107# output enum in man
1108sub output_enum_man(%) {
1109 my %args = %{$_[0]};
1110 my ($parameter, $section);
1111 my $count;
1112
1113 print ".TH \"$args{'module'}\" 9 \"enum $args{'enum'}\" \"$man_date\" \"API Manual\" LINUX\n";
1114
1115 print ".SH NAME\n";
b9d97328 1116 print "enum " . $args{'enum'} . " \\- " . $args{'purpose'} . "\n";
1da177e4
LT
1117
1118 print ".SH SYNOPSIS\n";
b9d97328 1119 print "enum " . $args{'enum'} . " {\n";
1da177e4
LT
1120 $count = 0;
1121 foreach my $parameter (@{$args{'parameterlist'}}) {
3c308798 1122 print ".br\n.BI \" $parameter\"\n";
1da177e4
LT
1123 if ($count == $#{$args{'parameterlist'}}) {
1124 print "\n};\n";
1125 last;
1126 }
1127 else {
1128 print ", \n.br\n";
1129 }
1130 $count++;
1131 }
1132
1133 print ".SH Constants\n";
1134 foreach $parameter (@{$args{'parameterlist'}}) {
1135 my $parameter_name = $parameter;
1136 $parameter_name =~ s/\[.*//;
1137
b9d97328 1138 print ".IP \"" . $parameter . "\" 12\n";
1da177e4
LT
1139 output_highlight($args{'parameterdescs'}{$parameter_name});
1140 }
1141 foreach $section (@{$args{'sectionlist'}}) {
1142 print ".SH \"$section\"\n";
1143 output_highlight($args{'sections'}{$section});
1144 }
1145}
1146
1147##
1148# output struct in man
1149sub output_struct_man(%) {
1150 my %args = %{$_[0]};
1151 my ($parameter, $section);
1152
b9d97328 1153 print ".TH \"$args{'module'}\" 9 \"" . $args{'type'} . " " . $args{'struct'} . "\" \"$man_date\" \"API Manual\" LINUX\n";
1da177e4
LT
1154
1155 print ".SH NAME\n";
b9d97328 1156 print $args{'type'} . " " . $args{'struct'} . " \\- " . $args{'purpose'} . "\n";
1da177e4
LT
1157
1158 print ".SH SYNOPSIS\n";
b9d97328 1159 print $args{'type'} . " " . $args{'struct'} . " {\n.br\n";
1da177e4
LT
1160
1161 foreach my $parameter (@{$args{'parameterlist'}}) {
1162 if ($parameter =~ /^#/) {
1163 print ".BI \"$parameter\"\n.br\n";
1164 next;
1165 }
1166 my $parameter_name = $parameter;
1167 $parameter_name =~ s/\[.*//;
1168
3c308798 1169 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
1da177e4
LT
1170 $type = $args{'parametertypes'}{$parameter};
1171 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1172 # pointer-to-function
b9d97328 1173 print ".BI \" " . $1 . "\" " . $parameter . " \") (" . $2 . ")" . "\"\n;\n";
1da177e4 1174 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
1d7e1d45 1175 # bitfield
b9d97328 1176 print ".BI \" " . $1 . "\ \" " . $parameter . $2 . " \"" . "\"\n;\n";
1da177e4
LT
1177 } else {
1178 $type =~ s/([^\*])$/$1 /;
b9d97328 1179 print ".BI \" " . $type . "\" " . $parameter . " \"" . "\"\n;\n";
1da177e4
LT
1180 }
1181 print "\n.br\n";
1182 }
1183 print "};\n.br\n";
1184
c51d3dac 1185 print ".SH Members\n";
1da177e4
LT
1186 foreach $parameter (@{$args{'parameterlist'}}) {
1187 ($parameter =~ /^#/) && next;
1188
1189 my $parameter_name = $parameter;
1190 $parameter_name =~ s/\[.*//;
1191
3c308798 1192 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
b9d97328 1193 print ".IP \"" . $parameter . "\" 12\n";
1da177e4
LT
1194 output_highlight($args{'parameterdescs'}{$parameter_name});
1195 }
1196 foreach $section (@{$args{'sectionlist'}}) {
1197 print ".SH \"$section\"\n";
1198 output_highlight($args{'sections'}{$section});
1199 }
1200}
1201
1202##
1203# output typedef in man
1204sub output_typedef_man(%) {
1205 my %args = %{$_[0]};
1206 my ($parameter, $section);
1207
1208 print ".TH \"$args{'module'}\" 9 \"$args{'typedef'}\" \"$man_date\" \"API Manual\" LINUX\n";
1209
1210 print ".SH NAME\n";
b9d97328 1211 print "typedef " . $args{'typedef'} . " \\- " . $args{'purpose'} . "\n";
1da177e4
LT
1212
1213 foreach $section (@{$args{'sectionlist'}}) {
1214 print ".SH \"$section\"\n";
1215 output_highlight($args{'sections'}{$section});
1216 }
1217}
1218
b112e0f7 1219sub output_blockhead_man(%) {
1da177e4
LT
1220 my %args = %{$_[0]};
1221 my ($parameter, $section);
1222 my $count;
1223
1224 print ".TH \"$args{'module'}\" 9 \"$args{'module'}\" \"$man_date\" \"API Manual\" LINUX\n";
1225
1226 foreach $section (@{$args{'sectionlist'}}) {
1227 print ".SH \"$section\"\n";
1228 output_highlight($args{'sections'}{$section});
1229 }
1230}
1231
1232##
1233# output in text
1234sub output_function_text(%) {
1235 my %args = %{$_[0]};
1236 my ($parameter, $section);
a21217da 1237 my $start;
1da177e4 1238
f47634b2 1239 print "Name:\n\n";
b9d97328 1240 print $args{'function'} . " - " . $args{'purpose'} . "\n";
f47634b2
RD
1241
1242 print "\nSynopsis:\n\n";
a21217da 1243 if ($args{'functiontype'} ne "") {
b9d97328 1244 $start = $args{'functiontype'} . " " . $args{'function'} . " (";
a21217da 1245 } else {
b9d97328 1246 $start = $args{'function'} . " (";
a21217da 1247 }
1da177e4 1248 print $start;
a21217da 1249
1da177e4
LT
1250 my $count = 0;
1251 foreach my $parameter (@{$args{'parameterlist'}}) {
1252 $type = $args{'parametertypes'}{$parameter};
1253 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1254 # pointer-to-function
b9d97328 1255 print $1 . $parameter . ") (" . $2;
1da177e4 1256 } else {
b9d97328 1257 print $type . " " . $parameter;
1da177e4
LT
1258 }
1259 if ($count != $#{$args{'parameterlist'}}) {
1260 $count++;
1261 print ",\n";
1262 print " " x length($start);
1263 } else {
1264 print ");\n\n";
1265 }
1266 }
1267
1268 print "Arguments:\n\n";
1269 foreach $parameter (@{$args{'parameterlist'}}) {
1270 my $parameter_name = $parameter;
1271 $parameter_name =~ s/\[.*//;
1272
b9d97328 1273 print $parameter . "\n\t" . $args{'parameterdescs'}{$parameter_name} . "\n";
1da177e4
LT
1274 }
1275 output_section_text(@_);
1276}
1277
1278#output sections in text
1279sub output_section_text(%) {
1280 my %args = %{$_[0]};
1281 my $section;
1282
1283 print "\n";
1284 foreach $section (@{$args{'sectionlist'}}) {
1285 print "$section:\n\n";
1286 output_highlight($args{'sections'}{$section});
3c3b809e 1287 }
1da177e4
LT
1288 print "\n\n";
1289}
1290
1291# output enum in text
1292sub output_enum_text(%) {
1293 my %args = %{$_[0]};
1294 my ($parameter);
1295 my $count;
1296 print "Enum:\n\n";
1297
b9d97328
RD
1298 print "enum " . $args{'enum'} . " - " . $args{'purpose'} . "\n\n";
1299 print "enum " . $args{'enum'} . " {\n";
1da177e4
LT
1300 $count = 0;
1301 foreach $parameter (@{$args{'parameterlist'}}) {
3c308798 1302 print "\t$parameter";
1da177e4
LT
1303 if ($count != $#{$args{'parameterlist'}}) {
1304 $count++;
1305 print ",";
1306 }
1307 print "\n";
1308 }
1309 print "};\n\n";
1310
1311 print "Constants:\n\n";
1312 foreach $parameter (@{$args{'parameterlist'}}) {
1313 print "$parameter\n\t";
b9d97328 1314 print $args{'parameterdescs'}{$parameter} . "\n";
1da177e4
LT
1315 }
1316
1317 output_section_text(@_);
1318}
1319
1320# output typedef in text
1321sub output_typedef_text(%) {
1322 my %args = %{$_[0]};
1323 my ($parameter);
1324 my $count;
1325 print "Typedef:\n\n";
1326
b9d97328 1327 print "typedef " . $args{'typedef'} . " - " . $args{'purpose'} . "\n";
1da177e4
LT
1328 output_section_text(@_);
1329}
1330
1331# output struct as text
1332sub output_struct_text(%) {
1333 my %args = %{$_[0]};
1334 my ($parameter);
1335
b9d97328
RD
1336 print $args{'type'} . " " . $args{'struct'} . " - " . $args{'purpose'} . "\n\n";
1337 print $args{'type'} . " " . $args{'struct'} . " {\n";
1da177e4
LT
1338 foreach $parameter (@{$args{'parameterlist'}}) {
1339 if ($parameter =~ /^#/) {
1340 print "$parameter\n";
1341 next;
1342 }
1343
1344 my $parameter_name = $parameter;
1345 $parameter_name =~ s/\[.*//;
1346
3c308798 1347 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
1da177e4
LT
1348 $type = $args{'parametertypes'}{$parameter};
1349 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1350 # pointer-to-function
1351 print "\t$1 $parameter) ($2);\n";
1352 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
51f5a0c8 1353 # bitfield
1da177e4
LT
1354 print "\t$1 $parameter$2;\n";
1355 } else {
b9d97328 1356 print "\t" . $type . " " . $parameter . ";\n";
1da177e4
LT
1357 }
1358 }
1359 print "};\n\n";
1360
1361 print "Members:\n\n";
1362 foreach $parameter (@{$args{'parameterlist'}}) {
1363 ($parameter =~ /^#/) && next;
1364
1365 my $parameter_name = $parameter;
1366 $parameter_name =~ s/\[.*//;
1367
3c308798 1368 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
1da177e4 1369 print "$parameter\n\t";
b9d97328 1370 print $args{'parameterdescs'}{$parameter_name} . "\n";
1da177e4
LT
1371 }
1372 print "\n";
1373 output_section_text(@_);
1374}
1375
b112e0f7 1376sub output_blockhead_text(%) {
1da177e4
LT
1377 my %args = %{$_[0]};
1378 my ($parameter, $section);
1379
1380 foreach $section (@{$args{'sectionlist'}}) {
1381 print " $section:\n";
1382 print " -> ";
1383 output_highlight($args{'sections'}{$section});
1384 }
1385}
1386
eda603f6
JB
1387## list mode output functions
1388
1389sub output_function_list(%) {
1390 my %args = %{$_[0]};
1391
1392 print $args{'function'} . "\n";
1393}
1394
1395# output enum in list
1396sub output_enum_list(%) {
1397 my %args = %{$_[0]};
1398 print $args{'enum'} . "\n";
1399}
1400
1401# output typedef in list
1402sub output_typedef_list(%) {
1403 my %args = %{$_[0]};
1404 print $args{'typedef'} . "\n";
1405}
1406
1407# output struct as list
1408sub output_struct_list(%) {
1409 my %args = %{$_[0]};
1410
1411 print $args{'struct'} . "\n";
1412}
1413
1414sub output_blockhead_list(%) {
1415 my %args = %{$_[0]};
1416 my ($parameter, $section);
1417
1418 foreach $section (@{$args{'sectionlist'}}) {
1419 print "DOC: $section\n";
1420 }
1421}
1422
1da177e4 1423##
27205744
RD
1424# generic output function for all types (function, struct/union, typedef, enum);
1425# calls the generated, variable output_ function name based on
1426# functype and output_mode
1da177e4
LT
1427sub output_declaration {
1428 no strict 'refs';
1429 my $name = shift;
1430 my $functype = shift;
1431 my $func = "output_${functype}_$output_mode";
3c3b809e
RD
1432 if (($function_only==0) ||
1433 ( $function_only == 1 && defined($function_table{$name})) ||
1da177e4
LT
1434 ( $function_only == 2 && !defined($function_table{$name})))
1435 {
3c308798 1436 &$func(@_);
1da177e4
LT
1437 $section_counter++;
1438 }
1439}
1440
1441##
27205744 1442# generic output function - calls the right one based on current output mode.
b112e0f7 1443sub output_blockhead {
1da177e4 1444 no strict 'refs';
b9d97328 1445 my $func = "output_blockhead_" . $output_mode;
1da177e4
LT
1446 &$func(@_);
1447 $section_counter++;
1448}
1449
1450##
3c3b809e 1451# takes a declaration (struct, union, enum, typedef) and
1da177e4
LT
1452# invokes the right handler. NOT called for functions.
1453sub dump_declaration($$) {
1454 no strict 'refs';
1455 my ($prototype, $file) = @_;
b9d97328 1456 my $func = "dump_" . $decl_type;
1da177e4
LT
1457 &$func(@_);
1458}
1459
1460sub dump_union($$) {
1461 dump_struct(@_);
1462}
1463
1464sub dump_struct($$) {
1465 my $x = shift;
1466 my $file = shift;
a1d94aa5 1467 my $nested;
1da177e4 1468
52dc5aec
RD
1469 if ($x =~ /(struct|union)\s+(\w+)\s*{(.*)}/) {
1470 #my $decl_type = $1;
3c308798
RD
1471 $declaration_name = $2;
1472 my $members = $3;
1da177e4
LT
1473
1474 # ignore embedded structs or unions
a1d94aa5
RD
1475 $members =~ s/({.*})//g;
1476 $nested = $1;
1da177e4 1477
aeec46b9 1478 # ignore members marked private:
52dc5aec
RD
1479 $members =~ s/\/\*\s*private:.*?\/\*\s*public:.*?\*\///gos;
1480 $members =~ s/\/\*\s*private:.*//gos;
aeec46b9
MW
1481 # strip comments:
1482 $members =~ s/\/\*.*?\*\///gos;
a1d94aa5 1483 $nested =~ s/\/\*.*?\*\///gos;
d960eea9
RD
1484 # strip kmemcheck_bitfield_{begin,end}.*;
1485 $members =~ s/kmemcheck_bitfield_.*?;//gos;
ef5da59f
RD
1486 # strip attributes
1487 $members =~ s/__aligned\s*\(\d+\)//gos;
aeec46b9 1488
1da177e4 1489 create_parameterlist($members, ';', $file);
a1d94aa5 1490 check_sections($file, $declaration_name, "struct", $sectcheck, $struct_actual, $nested);
1da177e4
LT
1491
1492 output_declaration($declaration_name,
1493 'struct',
1494 {'struct' => $declaration_name,
1495 'module' => $modulename,
1496 'parameterlist' => \@parameterlist,
1497 'parameterdescs' => \%parameterdescs,
1498 'parametertypes' => \%parametertypes,
1499 'sectionlist' => \@sectionlist,
1500 'sections' => \%sections,
1501 'purpose' => $declaration_purpose,
1502 'type' => $decl_type
1503 });
1504 }
1505 else {
3c308798 1506 print STDERR "Error(${file}:$.): Cannot parse struct or union!\n";
1da177e4
LT
1507 ++$errors;
1508 }
1509}
1510
1511sub dump_enum($$) {
1512 my $x = shift;
1513 my $file = shift;
1514
aeec46b9 1515 $x =~ s@/\*.*?\*/@@gos; # strip comments.
b6d676db
RD
1516 $x =~ s/^#\s*define\s+.*$//; # strip #define macros inside enums
1517
1da177e4 1518 if ($x =~ /enum\s+(\w+)\s*{(.*)}/) {
3c308798
RD
1519 $declaration_name = $1;
1520 my $members = $2;
1da177e4
LT
1521
1522 foreach my $arg (split ',', $members) {
1523 $arg =~ s/^\s*(\w+).*/$1/;
1524 push @parameterlist, $arg;
1525 if (!$parameterdescs{$arg}) {
3c308798
RD
1526 $parameterdescs{$arg} = $undescribed;
1527 print STDERR "Warning(${file}:$.): Enum value '$arg' ".
1da177e4
LT
1528 "not described in enum '$declaration_name'\n";
1529 }
1530
1531 }
3c3b809e 1532
1da177e4
LT
1533 output_declaration($declaration_name,
1534 'enum',
1535 {'enum' => $declaration_name,
1536 'module' => $modulename,
1537 'parameterlist' => \@parameterlist,
1538 'parameterdescs' => \%parameterdescs,
1539 'sectionlist' => \@sectionlist,
1540 'sections' => \%sections,
1541 'purpose' => $declaration_purpose
1542 });
1543 }
1544 else {
3c308798 1545 print STDERR "Error(${file}:$.): Cannot parse enum!\n";
1da177e4
LT
1546 ++$errors;
1547 }
1548}
1549
1550sub dump_typedef($$) {
1551 my $x = shift;
1552 my $file = shift;
1553
aeec46b9 1554 $x =~ s@/\*.*?\*/@@gos; # strip comments.
1da177e4 1555 while (($x =~ /\(*.\)\s*;$/) || ($x =~ /\[*.\]\s*;$/)) {
3c308798 1556 $x =~ s/\(*.\)\s*;$/;/;
1da177e4
LT
1557 $x =~ s/\[*.\]\s*;$/;/;
1558 }
1559
1560 if ($x =~ /typedef.*\s+(\w+)\s*;/) {
3c308798 1561 $declaration_name = $1;
1da177e4
LT
1562
1563 output_declaration($declaration_name,
1564 'typedef',
1565 {'typedef' => $declaration_name,
1566 'module' => $modulename,
1567 'sectionlist' => \@sectionlist,
1568 'sections' => \%sections,
1569 'purpose' => $declaration_purpose
1570 });
1571 }
1572 else {
3c308798 1573 print STDERR "Error(${file}:$.): Cannot parse typedef!\n";
1da177e4
LT
1574 ++$errors;
1575 }
1576}
1577
a1d94aa5
RD
1578sub save_struct_actual($) {
1579 my $actual = shift;
1580
1581 # strip all spaces from the actual param so that it looks like one string item
1582 $actual =~ s/\s*//g;
1583 $struct_actual = $struct_actual . $actual . " ";
1584}
1585
1da177e4
LT
1586sub create_parameterlist($$$) {
1587 my $args = shift;
1588 my $splitter = shift;
1589 my $file = shift;
1590 my $type;
1591 my $param;
1592
a6d3fe77 1593 # temporarily replace commas inside function pointer definition
1da177e4 1594 while ($args =~ /(\([^\),]+),/) {
3c308798 1595 $args =~ s/(\([^\),]+),/$1#/g;
1da177e4 1596 }
3c3b809e 1597
1da177e4
LT
1598 foreach my $arg (split($splitter, $args)) {
1599 # strip comments
1600 $arg =~ s/\/\*.*\*\///;
3c308798
RD
1601 # strip leading/trailing spaces
1602 $arg =~ s/^\s*//;
1da177e4
LT
1603 $arg =~ s/\s*$//;
1604 $arg =~ s/\s+/ /;
1605
1606 if ($arg =~ /^#/) {
1607 # Treat preprocessor directive as a typeless variable just to fill
1608 # corresponding data structures "correctly". Catch it later in
1609 # output_* subs.
1610 push_parameter($arg, "", $file);
00d62961 1611 } elsif ($arg =~ m/\(.+\)\s*\(/) {
1da177e4
LT
1612 # pointer-to-function
1613 $arg =~ tr/#/,/;
00d62961 1614 $arg =~ m/[^\(]+\(\*?\s*(\w*)\s*\)/;
1da177e4
LT
1615 $param = $1;
1616 $type = $arg;
00d62961 1617 $type =~ s/([^\(]+\(\*?)\s*$param/$1/;
a1d94aa5 1618 save_struct_actual($param);
1da177e4 1619 push_parameter($param, $type, $file);
aeec46b9 1620 } elsif ($arg) {
1da177e4
LT
1621 $arg =~ s/\s*:\s*/:/g;
1622 $arg =~ s/\s*\[/\[/g;
1623
1624 my @args = split('\s*,\s*', $arg);
1625 if ($args[0] =~ m/\*/) {
1626 $args[0] =~ s/(\*+)\s*/ $1/;
1627 }
884f2810
BP
1628
1629 my @first_arg;
1630 if ($args[0] =~ /^(.*\s+)(.*?\[.*\].*)$/) {
1631 shift @args;
1632 push(@first_arg, split('\s+', $1));
1633 push(@first_arg, $2);
1634 } else {
1635 @first_arg = split('\s+', shift @args);
1636 }
1637
1da177e4
LT
1638 unshift(@args, pop @first_arg);
1639 $type = join " ", @first_arg;
1640
1641 foreach $param (@args) {
1642 if ($param =~ m/^(\*+)\s*(.*)/) {
a1d94aa5 1643 save_struct_actual($2);
1da177e4
LT
1644 push_parameter($2, "$type $1", $file);
1645 }
1646 elsif ($param =~ m/(.*?):(\d+)/) {
7b97887e 1647 if ($type ne "") { # skip unnamed bit-fields
a1d94aa5 1648 save_struct_actual($1);
7b97887e
RD
1649 push_parameter($1, "$type:$2", $file)
1650 }
1da177e4
LT
1651 }
1652 else {
a1d94aa5 1653 save_struct_actual($param);
1da177e4
LT
1654 push_parameter($param, $type, $file);
1655 }
1656 }
1657 }
1658 }
1659}
1660
1661sub push_parameter($$$) {
1662 my $param = shift;
1663 my $type = shift;
1664 my $file = shift;
1665
5f8c7c98
RD
1666 if (($anon_struct_union == 1) && ($type eq "") &&
1667 ($param eq "}")) {
1668 return; # ignore the ending }; from anon. struct/union
1669 }
1670
1671 $anon_struct_union = 0;
1da177e4
LT
1672 my $param_name = $param;
1673 $param_name =~ s/\[.*//;
1674
a6d3fe77 1675 if ($type eq "" && $param =~ /\.\.\.$/)
1da177e4 1676 {
ced69090
RD
1677 if (!defined $parameterdescs{$param} || $parameterdescs{$param} eq "") {
1678 $parameterdescs{$param} = "variable arguments";
1679 }
1da177e4
LT
1680 }
1681 elsif ($type eq "" && ($param eq "" or $param eq "void"))
1682 {
1da177e4
LT
1683 $param="void";
1684 $parameterdescs{void} = "no arguments";
1685 }
134fe01b
RD
1686 elsif ($type eq "" && ($param eq "struct" or $param eq "union"))
1687 # handle unnamed (anonymous) union or struct:
1688 {
1689 $type = $param;
5f8c7c98 1690 $param = "{unnamed_" . $param . "}";
134fe01b 1691 $parameterdescs{$param} = "anonymous\n";
5f8c7c98 1692 $anon_struct_union = 1;
134fe01b
RD
1693 }
1694
a6d3fe77 1695 # warn if parameter has no description
134fe01b
RD
1696 # (but ignore ones starting with # as these are not parameters
1697 # but inline preprocessor statements);
1698 # also ignore unnamed structs/unions;
5f8c7c98 1699 if (!$anon_struct_union) {
a6d3fe77
MW
1700 if (!defined $parameterdescs{$param_name} && $param_name !~ /^#/) {
1701
1da177e4
LT
1702 $parameterdescs{$param_name} = $undescribed;
1703
1704 if (($type eq 'function') || ($type eq 'enum')) {
3c308798 1705 print STDERR "Warning(${file}:$.): Function parameter ".
1da177e4
LT
1706 "or member '$param' not " .
1707 "described in '$declaration_name'\n";
1708 }
b9d97328 1709 print STDERR "Warning(${file}:$.):" .
3c308798 1710 " No description found for parameter '$param'\n";
1da177e4 1711 ++$warnings;
3c308798
RD
1712 }
1713 }
1da177e4 1714
2b35f4d9
RD
1715 $param = xml_escape($param);
1716
25985edc 1717 # strip spaces from $param so that it is one continuous string
e34e7dbb
RD
1718 # on @parameterlist;
1719 # this fixes a problem where check_sections() cannot find
1720 # a parameter like "addr[6 + 2]" because it actually appears
1721 # as "addr[6", "+", "2]" on the parameter list;
1722 # but it's better to maintain the param string unchanged for output,
1723 # so just weaken the string compare in check_sections() to ignore
1724 # "[blah" in a parameter string;
1725 ###$param =~ s/\s*//g;
1da177e4
LT
1726 push @parameterlist, $param;
1727 $parametertypes{$param} = $type;
1728}
1729
a1d94aa5
RD
1730sub check_sections($$$$$$) {
1731 my ($file, $decl_name, $decl_type, $sectcheck, $prmscheck, $nested) = @_;
1732 my @sects = split ' ', $sectcheck;
1733 my @prms = split ' ', $prmscheck;
1734 my $err;
1735 my ($px, $sx);
1736 my $prm_clean; # strip trailing "[array size]" and/or beginning "*"
1737
1738 foreach $sx (0 .. $#sects) {
1739 $err = 1;
1740 foreach $px (0 .. $#prms) {
1741 $prm_clean = $prms[$px];
1742 $prm_clean =~ s/\[.*\]//;
1f3a6688 1743 $prm_clean =~ s/__attribute__\s*\(\([a-z,_\*\s\(\)]*\)\)//i;
e34e7dbb
RD
1744 # ignore array size in a parameter string;
1745 # however, the original param string may contain
1746 # spaces, e.g.: addr[6 + 2]
1747 # and this appears in @prms as "addr[6" since the
1748 # parameter list is split at spaces;
1749 # hence just ignore "[..." for the sections check;
1750 $prm_clean =~ s/\[.*//;
1751
a1d94aa5
RD
1752 ##$prm_clean =~ s/^\**//;
1753 if ($prm_clean eq $sects[$sx]) {
1754 $err = 0;
1755 last;
1756 }
1757 }
1758 if ($err) {
1759 if ($decl_type eq "function") {
1760 print STDERR "Warning(${file}:$.): " .
1761 "Excess function parameter " .
1762 "'$sects[$sx]' " .
1763 "description in '$decl_name'\n";
1764 ++$warnings;
1765 } else {
1766 if ($nested !~ m/\Q$sects[$sx]\E/) {
1767 print STDERR "Warning(${file}:$.): " .
1768 "Excess struct/union/enum/typedef member " .
1769 "'$sects[$sx]' " .
1770 "description in '$decl_name'\n";
1771 ++$warnings;
1772 }
1773 }
1774 }
1775 }
1776}
1777
1da177e4
LT
1778##
1779# takes a function prototype and the name of the current file being
1780# processed and spits out all the details stored in the global
1781# arrays/hashes.
1782sub dump_function($$) {
1783 my $prototype = shift;
1784 my $file = shift;
1785
1786 $prototype =~ s/^static +//;
1787 $prototype =~ s/^extern +//;
4dc3b16b 1788 $prototype =~ s/^asmlinkage +//;
1da177e4
LT
1789 $prototype =~ s/^inline +//;
1790 $prototype =~ s/^__inline__ +//;
32e79401
RD
1791 $prototype =~ s/^__inline +//;
1792 $prototype =~ s/^__always_inline +//;
1793 $prototype =~ s/^noinline +//;
0129a057 1794 $prototype =~ s/__devinit +//;
74fc5c65 1795 $prototype =~ s/__init +//;
20072205 1796 $prototype =~ s/__init_or_module +//;
70c95b00 1797 $prototype =~ s/__must_check +//;
0df7c0e3 1798 $prototype =~ s/__weak +//;
890c78c2 1799 $prototype =~ s/^#\s*define\s+//; #ak added
328d2440 1800 $prototype =~ s/__attribute__\s*\(\([a-z,]*\)\)//;
1da177e4
LT
1801
1802 # Yes, this truly is vile. We are looking for:
1803 # 1. Return type (may be nothing if we're looking at a macro)
1804 # 2. Function name
1805 # 3. Function parameters.
1806 #
1807 # All the while we have to watch out for function pointer parameters
1808 # (which IIRC is what the two sections are for), C types (these
1809 # regexps don't even start to express all the possibilities), and
1810 # so on.
1811 #
1812 # If you mess with these regexps, it's a good idea to check that
1813 # the following functions' documentation still comes out right:
1814 # - parport_register_device (function pointer parameters)
1815 # - atomic_set (macro)
9598f91f 1816 # - pci_match_device, __copy_to_user (long return type)
1da177e4
LT
1817
1818 if ($prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1819 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1820 $prototype =~ m/^(\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1821 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
94b3e03c 1822 $prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1da177e4
LT
1823 $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1824 $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1825 $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1826 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1827 $prototype =~ m/^(\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1828 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1829 $prototype =~ m/^(\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1830 $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
9598f91f
MW
1831 $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1832 $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
412ecd77
RD
1833 $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1834 $prototype =~ m/^(\w+\s+\w+\s*\*\s*\w+\s*\*\s*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/) {
1da177e4
LT
1835 $return_type = $1;
1836 $declaration_name = $2;
1837 my $args = $3;
1838
1839 create_parameterlist($args, ',', $file);
1840 } else {
1841 print STDERR "Error(${file}:$.): cannot understand prototype: '$prototype'\n";
1842 ++$errors;
1843 return;
1844 }
1845
a1d94aa5
RD
1846 my $prms = join " ", @parameterlist;
1847 check_sections($file, $declaration_name, "function", $sectcheck, $prms, "");
1848
3c3b809e 1849 output_declaration($declaration_name,
1da177e4
LT
1850 'function',
1851 {'function' => $declaration_name,
1852 'module' => $modulename,
1853 'functiontype' => $return_type,
1854 'parameterlist' => \@parameterlist,
1855 'parameterdescs' => \%parameterdescs,
1856 'parametertypes' => \%parametertypes,
1857 'sectionlist' => \@sectionlist,
1858 'sections' => \%sections,
1859 'purpose' => $declaration_purpose
1860 });
1861}
1862
1da177e4
LT
1863sub reset_state {
1864 $function = "";
1865 %constants = ();
1866 %parameterdescs = ();
1867 %parametertypes = ();
1868 @parameterlist = ();
1869 %sections = ();
1870 @sectionlist = ();
a1d94aa5
RD
1871 $sectcheck = "";
1872 $struct_actual = "";
1da177e4 1873 $prototype = "";
3c3b809e 1874
1da177e4
LT
1875 $state = 0;
1876}
1877
56afb0f8
JB
1878sub tracepoint_munge($) {
1879 my $file = shift;
1880 my $tracepointname = 0;
1881 my $tracepointargs = 0;
1882
3a9089fd 1883 if ($prototype =~ m/TRACE_EVENT\((.*?),/) {
56afb0f8
JB
1884 $tracepointname = $1;
1885 }
3a9089fd
JB
1886 if ($prototype =~ m/DEFINE_SINGLE_EVENT\((.*?),/) {
1887 $tracepointname = $1;
1888 }
1889 if ($prototype =~ m/DEFINE_EVENT\((.*?),(.*?),/) {
1890 $tracepointname = $2;
1891 }
1892 $tracepointname =~ s/^\s+//; #strip leading whitespace
1893 if ($prototype =~ m/TP_PROTO\((.*?)\)/) {
56afb0f8
JB
1894 $tracepointargs = $1;
1895 }
1896 if (($tracepointname eq 0) || ($tracepointargs eq 0)) {
1897 print STDERR "Warning(${file}:$.): Unrecognized tracepoint format: \n".
1898 "$prototype\n";
1899 } else {
1900 $prototype = "static inline void trace_$tracepointname($tracepointargs)";
1901 }
1902}
1903
b4870bc5
RD
1904sub syscall_munge() {
1905 my $void = 0;
1906
1907 $prototype =~ s@[\r\n\t]+@ @gos; # strip newlines/CR's/tabs
1908## if ($prototype =~ m/SYSCALL_DEFINE0\s*\(\s*(a-zA-Z0-9_)*\s*\)/) {
1909 if ($prototype =~ m/SYSCALL_DEFINE0/) {
1910 $void = 1;
1911## $prototype = "long sys_$1(void)";
1912 }
1913
1914 $prototype =~ s/SYSCALL_DEFINE.*\(/long sys_/; # fix return type & func name
1915 if ($prototype =~ m/long (sys_.*?),/) {
1916 $prototype =~ s/,/\(/;
1917 } elsif ($void) {
1918 $prototype =~ s/\)/\(void\)/;
1919 }
1920
1921 # now delete all of the odd-number commas in $prototype
1922 # so that arg types & arg names don't have a comma between them
1923 my $count = 0;
1924 my $len = length($prototype);
1925 if ($void) {
1926 $len = 0; # skip the for-loop
1927 }
1928 for (my $ix = 0; $ix < $len; $ix++) {
1929 if (substr($prototype, $ix, 1) eq ',') {
1930 $count++;
1931 if ($count % 2 == 1) {
1932 substr($prototype, $ix, 1) = ' ';
1933 }
1934 }
1935 }
1936}
1937
3c3b809e 1938sub process_state3_function($$) {
1da177e4
LT
1939 my $x = shift;
1940 my $file = shift;
1941
51f5a0c8
RD
1942 $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
1943
890c78c2 1944 if ($x =~ m#\s*/\*\s+MACDOC\s*#io || ($x =~ /^#/ && $x !~ /^#\s*define/)) {
1da177e4
LT
1945 # do nothing
1946 }
1947 elsif ($x =~ /([^\{]*)/) {
3c308798 1948 $prototype .= $1;
1da177e4 1949 }
b4870bc5 1950
890c78c2 1951 if (($x =~ /\{/) || ($x =~ /\#\s*define/) || ($x =~ /;/)) {
3c308798 1952 $prototype =~ s@/\*.*?\*/@@gos; # strip comments.
1da177e4
LT
1953 $prototype =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1954 $prototype =~ s@^\s+@@gos; # strip leading spaces
b4870bc5
RD
1955 if ($prototype =~ /SYSCALL_DEFINE/) {
1956 syscall_munge();
1957 }
3a9089fd
JB
1958 if ($prototype =~ /TRACE_EVENT/ || $prototype =~ /DEFINE_EVENT/ ||
1959 $prototype =~ /DEFINE_SINGLE_EVENT/)
1960 {
56afb0f8
JB
1961 tracepoint_munge($file);
1962 }
b4870bc5 1963 dump_function($prototype, $file);
1da177e4
LT
1964 reset_state();
1965 }
1966}
1967
3c3b809e 1968sub process_state3_type($$) {
1da177e4
LT
1969 my $x = shift;
1970 my $file = shift;
1971
1da177e4
LT
1972 $x =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1973 $x =~ s@^\s+@@gos; # strip leading spaces
1974 $x =~ s@\s+$@@gos; # strip trailing spaces
51f5a0c8
RD
1975 $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
1976
1da177e4
LT
1977 if ($x =~ /^#/) {
1978 # To distinguish preprocessor directive from regular declaration later.
1979 $x .= ";";
1980 }
1981
1982 while (1) {
3c308798 1983 if ( $x =~ /([^{};]*)([{};])(.*)/ ) {
1da177e4
LT
1984 $prototype .= $1 . $2;
1985 ($2 eq '{') && $brcount++;
1986 ($2 eq '}') && $brcount--;
1987 if (($2 eq ';') && ($brcount == 0)) {
b9d97328 1988 dump_declaration($prototype, $file);
1da177e4 1989 reset_state();
3c308798 1990 last;
1da177e4
LT
1991 }
1992 $x = $3;
3c308798 1993 } else {
1da177e4
LT
1994 $prototype .= $x;
1995 last;
1996 }
1997 }
1998}
1999
6b5b55f6
RD
2000# xml_escape: replace <, >, and & in the text stream;
2001#
2002# however, formatting controls that are generated internally/locally in the
2003# kernel-doc script are not escaped here; instead, they begin life like
2004# $blankline_html (4 of '\' followed by a mnemonic + ':'), then these strings
2005# are converted to their mnemonic-expected output, without the 4 * '\' & ':',
2006# just before actual output; (this is done by local_unescape())
1da177e4
LT
2007sub xml_escape($) {
2008 my $text = shift;
ecfb251a
RD
2009 if (($output_mode eq "text") || ($output_mode eq "man")) {
2010 return $text;
2011 }
1da177e4
LT
2012 $text =~ s/\&/\\\\\\amp;/g;
2013 $text =~ s/\</\\\\\\lt;/g;
2014 $text =~ s/\>/\\\\\\gt;/g;
2015 return $text;
2016}
2017
6b5b55f6
RD
2018# convert local escape strings to html
2019# local escape strings look like: '\\\\menmonic:' (that's 4 backslashes)
2020sub local_unescape($) {
2021 my $text = shift;
2022 if (($output_mode eq "text") || ($output_mode eq "man")) {
2023 return $text;
2024 }
2025 $text =~ s/\\\\\\\\lt:/</g;
2026 $text =~ s/\\\\\\\\gt:/>/g;
2027 return $text;
2028}
2029
1da177e4 2030sub process_file($) {
2283a117 2031 my $file;
1da177e4
LT
2032 my $identifier;
2033 my $func;
a21217da 2034 my $descr;
6423133b 2035 my $in_purpose = 0;
1da177e4
LT
2036 my $initial_section_counter = $section_counter;
2037
2283a117
RD
2038 if (defined($ENV{'SRCTREE'})) {
2039 $file = "$ENV{'SRCTREE'}" . "/" . "@_";
2040 }
2041 else {
2042 $file = "@_";
2043 }
1da177e4
LT
2044 if (defined($source_map{$file})) {
2045 $file = $source_map{$file};
2046 }
2047
2048 if (!open(IN,"<$file")) {
2049 print STDERR "Error: Cannot open file $file\n";
2050 ++$errors;
2051 return;
2052 }
2053
a9e7314b
ID
2054 $. = 1;
2055
1da177e4
LT
2056 $section_counter = 0;
2057 while (<IN>) {
65478428
DS
2058 while (s/\\\s*$//) {
2059 $_ .= <IN>;
2060 }
1da177e4
LT
2061 if ($state == 0) {
2062 if (/$doc_start/o) {
2063 $state = 1; # next line is always the function name
850622df 2064 $in_doc_sect = 0;
1da177e4
LT
2065 }
2066 } elsif ($state == 1) { # this line is the function name (always)
2067 if (/$doc_block/o) {
2068 $state = 4;
2069 $contents = "";
2070 if ( $1 eq "" ) {
2071 $section = $section_intro;
2072 } else {
2073 $section = $1;
2074 }
3c308798 2075 }
1da177e4
LT
2076 elsif (/$doc_decl/o) {
2077 $identifier = $1;
2078 if (/\s*([\w\s]+?)\s*-/) {
2079 $identifier = $1;
2080 }
2081
2082 $state = 2;
2083 if (/-(.*)/) {
51f5a0c8 2084 # strip leading/trailing/multiple spaces
a21217da
RD
2085 $descr= $1;
2086 $descr =~ s/^\s*//;
2087 $descr =~ s/\s*$//;
2088 $descr =~ s/\s+/ /;
2089 $declaration_purpose = xml_escape($descr);
6423133b 2090 $in_purpose = 1;
1da177e4
LT
2091 } else {
2092 $declaration_purpose = "";
2093 }
77cc23b8
RD
2094
2095 if (($declaration_purpose eq "") && $verbose) {
2096 print STDERR "Warning(${file}:$.): missing initial short description on line:\n";
2097 print STDERR $_;
2098 ++$warnings;
2099 }
2100
1da177e4
LT
2101 if ($identifier =~ m/^struct/) {
2102 $decl_type = 'struct';
2103 } elsif ($identifier =~ m/^union/) {
2104 $decl_type = 'union';
2105 } elsif ($identifier =~ m/^enum/) {
2106 $decl_type = 'enum';
2107 } elsif ($identifier =~ m/^typedef/) {
2108 $decl_type = 'typedef';
2109 } else {
2110 $decl_type = 'function';
2111 }
2112
2113 if ($verbose) {
2114 print STDERR "Info(${file}:$.): Scanning doc for $identifier\n";
2115 }
2116 } else {
2117 print STDERR "Warning(${file}:$.): Cannot understand $_ on line $.",
2118 " - I thought it was a doc line\n";
2119 ++$warnings;
2120 $state = 0;
2121 }
2122 } elsif ($state == 2) { # look for head: lines, and include content
2123 if (/$doc_sect/o) {
2124 $newsection = $1;
2125 $newcontents = $2;
2126
792aa2f2 2127 if (($contents ne "") && ($contents ne "\n")) {
850622df
RD
2128 if (!$in_doc_sect && $verbose) {
2129 print STDERR "Warning(${file}:$.): contents before sections\n";
2130 ++$warnings;
2131 }
94dc7ad5 2132 dump_section($file, $section, xml_escape($contents));
1da177e4
LT
2133 $section = $section_default;
2134 }
2135
850622df 2136 $in_doc_sect = 1;
6423133b 2137 $in_purpose = 0;
1da177e4
LT
2138 $contents = $newcontents;
2139 if ($contents ne "") {
27205744
RD
2140 while ((substr($contents, 0, 1) eq " ") ||
2141 substr($contents, 0, 1) eq "\t") {
2142 $contents = substr($contents, 1);
05189497 2143 }
1da177e4
LT
2144 $contents .= "\n";
2145 }
2146 $section = $newsection;
2147 } elsif (/$doc_end/) {
2148
4c98ecaf 2149 if (($contents ne "") && ($contents ne "\n")) {
94dc7ad5 2150 dump_section($file, $section, xml_escape($contents));
1da177e4
LT
2151 $section = $section_default;
2152 $contents = "";
2153 }
46b958eb
RD
2154 # look for doc_com + <text> + doc_end:
2155 if ($_ =~ m'\s*\*\s*[a-zA-Z_0-9:\.]+\*/') {
2156 print STDERR "Warning(${file}:$.): suspicious ending line: $_";
2157 ++$warnings;
2158 }
1da177e4
LT
2159
2160 $prototype = "";
2161 $state = 3;
2162 $brcount = 0;
232acbcf 2163# print STDERR "end of doc comment, looking for prototype\n";
1da177e4
LT
2164 } elsif (/$doc_content/) {
2165 # miguel-style comment kludge, look for blank lines after
2166 # @parameter line to signify start of description
6423133b
JW
2167 if ($1 eq "") {
2168 if ($section =~ m/^@/ || $section eq $section_context) {
2169 dump_section($file, $section, xml_escape($contents));
2170 $section = $section_default;
2171 $contents = "";
2172 } else {
2173 $contents .= "\n";
2174 }
2175 $in_purpose = 0;
2176 } elsif ($in_purpose == 1) {
2177 # Continued declaration purpose
2178 chomp($declaration_purpose);
2179 $declaration_purpose .= " " . xml_escape($1);
1da177e4 2180 } else {
b9d97328 2181 $contents .= $1 . "\n";
1da177e4
LT
2182 }
2183 } else {
2184 # i dont know - bad line? ignore.
3c3b809e 2185 print STDERR "Warning(${file}:$.): bad line: $_";
1da177e4
LT
2186 ++$warnings;
2187 }
232acbcf 2188 } elsif ($state == 3) { # scanning for function '{' (end of prototype)
1da177e4 2189 if ($decl_type eq 'function') {
3c308798 2190 process_state3_function($_, $file);
1da177e4 2191 } else {
3c308798 2192 process_state3_type($_, $file);
1da177e4
LT
2193 }
2194 } elsif ($state == 4) {
2195 # Documentation block
3c308798 2196 if (/$doc_block/) {
94dc7ad5 2197 dump_doc_section($file, $section, xml_escape($contents));
1da177e4
LT
2198 $contents = "";
2199 $function = "";
2200 %constants = ();
2201 %parameterdescs = ();
2202 %parametertypes = ();
2203 @parameterlist = ();
2204 %sections = ();
2205 @sectionlist = ();
2206 $prototype = "";
2207 if ( $1 eq "" ) {
2208 $section = $section_intro;
2209 } else {
2210 $section = $1;
2211 }
3c308798 2212 }
1da177e4
LT
2213 elsif (/$doc_end/)
2214 {
94dc7ad5 2215 dump_doc_section($file, $section, xml_escape($contents));
1da177e4
LT
2216 $contents = "";
2217 $function = "";
2218 %constants = ();
2219 %parameterdescs = ();
2220 %parametertypes = ();
2221 @parameterlist = ();
2222 %sections = ();
2223 @sectionlist = ();
2224 $prototype = "";
2225 $state = 0;
2226 }
2227 elsif (/$doc_content/)
2228 {
2229 if ( $1 eq "" )
2230 {
2231 $contents .= $blankline;
2232 }
2233 else
2234 {
2235 $contents .= $1 . "\n";
3c3b809e 2236 }
3c308798
RD
2237 }
2238 }
1da177e4
LT
2239 }
2240 if ($initial_section_counter == $section_counter) {
2241 print STDERR "Warning(${file}): no structured comments found\n";
2242 if ($output_mode eq "xml") {
2243 # The template wants at least one RefEntry here; make one.
2244 print "<refentry>\n";
2245 print " <refnamediv>\n";
2246 print " <refname>\n";
2247 print " ${file}\n";
2248 print " </refname>\n";
2249 print " <refpurpose>\n";
2250 print " Document generation inconsistency\n";
2251 print " </refpurpose>\n";
2252 print " </refnamediv>\n";
2253 print " <refsect1>\n";
2254 print " <title>\n";
2255 print " Oops\n";
2256 print " </title>\n";
2257 print " <warning>\n";
2258 print " <para>\n";
2259 print " The template for this document tried to insert\n";
2260 print " the structured comment from the file\n";
2261 print " <filename>${file}</filename> at this point,\n";
2262 print " but none was found.\n";
2263 print " This dummy section is inserted to allow\n";
2264 print " generation to continue.\n";
2265 print " </para>\n";
2266 print " </warning>\n";
2267 print " </refsect1>\n";
2268 print "</refentry>\n";
2269 }
2270 }
2271}
8484baaa
RD
2272
2273
2274$kernelversion = get_kernel_version();
2275
2276# generate a sequence of code that will splice in highlighting information
2277# using the s// operator.
2278foreach my $pattern (keys %highlights) {
2279# print STDERR "scanning pattern:$pattern, highlight:($highlights{$pattern})\n";
2280 $dohighlight .= "\$contents =~ s:$pattern:$highlights{$pattern}:gs;\n";
2281}
2282
2283# Read the file that maps relative names to absolute names for
2284# separate source and object directories and for shadow trees.
2285if (open(SOURCE_MAP, "<.tmp_filelist.txt")) {
2286 my ($relname, $absname);
2287 while(<SOURCE_MAP>) {
2288 chop();
2289 ($relname, $absname) = (split())[0..1];
2290 $relname =~ s:^/+::;
2291 $source_map{$relname} = $absname;
2292 }
2293 close(SOURCE_MAP);
2294}
2295
2296foreach (@ARGV) {
2297 chomp;
2298 process_file($_);
2299}
2300if ($verbose && $errors) {
2301 print STDERR "$errors errors\n";
2302}
2303if ($verbose && $warnings) {
2304 print STDERR "$warnings warnings\n";
2305}
2306
2307exit($errors);