blob: 08894f4aab2c6c97aa47a2f3155d7abe77146547 [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 ##
8## ##
9## #define enhancements by Armin Kuster <akuster@mvista.com> ##
10## Copyright (c) 2000 MontaVista Software, Inc. ##
11## ##
12## This software falls under the GNU General Public License. ##
13## Please read the COPYING file for more information ##
14
15# w.o. 03-11-2000: added the '-filelist' option.
16
17# 18/01/2001 - Cleanups
18# Functions prototyped as foo(void) same as foo()
19# Stop eval'ing where we don't need to.
20# -- huggie@earth.li
21
22# 27/06/2001 - Allowed whitespace after initial "/**" and
23# allowed comments before function declarations.
24# -- Christian Kreibich <ck@whoop.org>
25
26# Still to do:
27# - add perldoc documentation
28# - Look more closely at some of the scarier bits :)
29
30# 26/05/2001 - Support for separate source and object trees.
31# Return error code.
32# Keith Owens <kaos@ocs.com.au>
33
34# 23/09/2001 - Added support for typedefs, structs, enums and unions
35# Support for Context section; can be terminated using empty line
36# Small fixes (like spaces vs. \s in regex)
37# -- Tim Jansen <tim@tjansen.de>
38
39
40#
41# This will read a 'c' file and scan for embedded comments in the
42# style of gnome comments (+minor extensions - see below).
43#
44
45# Note: This only supports 'c'.
46
47# usage:
Randy Dunlapd28bee02006-02-01 03:06:57 -080048# kernel-doc [ -docbook | -html | -text | -man ]
Linus Torvalds1da177e2005-04-16 15:20:36 -070049# [ -function funcname [ -function funcname ...] ] c file(s)s > outputfile
50# or
51# [ -nofunction funcname [ -function funcname ...] ] c file(s)s > outputfile
52#
53# Set output format using one of -docbook -html -text or -man. Default is man.
54#
55# -function funcname
56# If set, then only generate documentation for the given function(s). All
57# other functions are ignored.
58#
59# -nofunction funcname
Rolf Eike Beera073a8b2006-10-03 23:13:02 +020060# If set, then only generate documentation for the other function(s).
61# Cannot be used together with -function
Randy Dunlapd28bee02006-02-01 03:06:57 -080062# (yes, that's a bug -- perl hackers can fix it 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#
64# c files - list of 'c' files to process
65#
66# All output goes to stdout, with errors to stderr.
67
68#
69# format of comments.
70# In the following table, (...)? signifies optional structure.
71# (...)* signifies 0 or more structure elements
72# /**
73# * function_name(:)? (- short description)?
74# (* @parameterx: (description of parameter x)?)*
75# (* a blank line)?
76# * (Description:)? (Description of function)?
77# * (section header: (section description)? )*
78# (*)?*/
79#
80# So .. the trivial example would be:
81#
82# /**
83# * my_function
84# **/
85#
Randy Dunlap891dcd22007-02-10 01:45:53 -080086# If the Description: header tag is omitted, then there must be a blank line
Linus Torvalds1da177e2005-04-16 15:20:36 -070087# after the last parameter specification.
88# e.g.
89# /**
90# * my_function - does my stuff
91# * @my_arg: its mine damnit
92# *
Randy Dunlap3c3b8092006-02-01 03:06:58 -080093# * Does my stuff explained.
Linus Torvalds1da177e2005-04-16 15:20:36 -070094# */
95#
96# or, could also use:
97# /**
98# * my_function - does my stuff
99# * @my_arg: its mine damnit
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800100# * Description: Does my stuff explained.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101# */
102# etc.
103#
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800104# Beside functions you can also write documentation for structs, unions,
105# enums and typedefs. Instead of the function name you must write the name
106# of the declaration; the struct/union/enum/typedef must always precede
107# the name. Nesting of declarations is not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108# Use the argument mechanism to document members or constants.
109# e.g.
110# /**
111# * struct my_struct - short description
112# * @a: first member
113# * @b: second member
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800114# *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115# * Longer description
116# */
117# struct my_struct {
118# int a;
119# int b;
Martin Waitzaeec46b2005-11-13 16:08:13 -0800120# /* private: */
121# int c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122# };
123#
124# All descriptions can be multiline, except the short function description.
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800125#
126# You can also add additional sections. When documenting kernel functions you
127# should document the "Context:" of the function, e.g. whether the functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128# can be called form interrupts. Unlike other sections you can end it with an
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800129# empty line.
130# Example-sections should contain the string EXAMPLE so that they are marked
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131# appropriately in DocBook.
132#
133# Example:
134# /**
135# * user_function - function that can only be called in user context
136# * @a: some argument
137# * Context: !in_interrupt()
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800138# *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139# * Some description
140# * Example:
141# * user_function(22);
142# */
143# ...
144#
145#
146# All descriptive text is further processed, scanning for the following special
147# patterns, which are highlighted appropriately.
148#
149# 'funcname()' - function
150# '$ENVVAR' - environmental variable
151# '&struct_name' - name of a structure (up to two words including 'struct')
152# '@parameter' - name of a parameter
153# '%CONST' - name of a constant.
154
155my $errors = 0;
156my $warnings = 0;
Randy Dunlap5f8c7c92007-07-19 01:48:24 -0700157my $anon_struct_union = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159# match expressions used to find embedded type information
160my $type_constant = '\%([-_\w]+)';
161my $type_func = '(\w+)\(\)';
162my $type_param = '\@(\w+)';
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700163my $type_struct = '\&((struct\s*)*[_\w]+)';
164my $type_struct_xml = '\\\amp;((struct\s*)*[_\w]+)';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165my $type_env = '(\$\w+)';
166
167# Output conversion substitutions.
168# One for each output format
169
170# these work fairly well
171my %highlights_html = ( $type_constant, "<i>\$1</i>",
172 $type_func, "<b>\$1</b>",
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700173 $type_struct_xml, "<i>\$1</i>",
174 $type_env, "<b><i>\$1</i></b>",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 $type_param, "<tt><b>\$1</b></tt>" );
176my $blankline_html = "<p>";
177
178# XML, docbook format
179my %highlights_xml = ( "([^=])\\\"([^\\\"<]+)\\\"", "\$1<quote>\$2</quote>",
180 $type_constant, "<constant>\$1</constant>",
181 $type_func, "<function>\$1</function>",
182 $type_struct, "<structname>\$1</structname>",
183 $type_env, "<envar>\$1</envar>",
184 $type_param, "<parameter>\$1</parameter>" );
185my $blankline_xml = "</para><para>\n";
186
187# gnome, docbook format
188my %highlights_gnome = ( $type_constant, "<replaceable class=\"option\">\$1</replaceable>",
189 $type_func, "<function>\$1</function>",
190 $type_struct, "<structname>\$1</structname>",
191 $type_env, "<envar>\$1</envar>",
192 $type_param, "<parameter>\$1</parameter>" );
193my $blankline_gnome = "</para><para>\n";
194
195# these are pretty rough
196my %highlights_man = ( $type_constant, "\$1",
197 $type_func, "\\\\fB\$1\\\\fP",
198 $type_struct, "\\\\fI\$1\\\\fP",
199 $type_param, "\\\\fI\$1\\\\fP" );
200my $blankline_man = "";
201
202# text-mode
203my %highlights_text = ( $type_constant, "\$1",
204 $type_func, "\$1",
205 $type_struct, "\$1",
206 $type_param, "\$1" );
207my $blankline_text = "";
208
209
210sub usage {
211 print "Usage: $0 [ -v ] [ -docbook | -html | -text | -man ]\n";
212 print " [ -function funcname [ -function funcname ...] ]\n";
213 print " [ -nofunction funcname [ -nofunction funcname ...] ]\n";
214 print " c source file(s) > outputfile\n";
215 exit 1;
216}
217
218# read arguments
219if ($#ARGV==-1) {
220 usage();
221}
222
223my $verbose = 0;
224my $output_mode = "man";
225my %highlights = %highlights_man;
226my $blankline = $blankline_man;
227my $modulename = "Kernel API";
228my $function_only = 0;
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800229my $man_date = ('January', 'February', 'March', 'April', 'May', 'June',
230 'July', 'August', 'September', 'October',
231 'November', 'December')[(localtime)[4]] .
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 " " . ((localtime)[5]+1900);
233
234# Essentially these are globals
235# They probably want to be tidied up made more localised or summat.
236# CAVEAT EMPTOR! Some of the others I localised may not want to be which
237# could cause "use of undefined value" or other bugs.
238my ($function, %function_table,%parametertypes,$declaration_purpose);
239my ($type,$declaration_name,$return_type);
240my ($newsection,$newcontents,$prototype,$filelist, $brcount, %source_map);
241
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800242# Generated docbook code is inserted in a template at a point where
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243# docbook v3.1 requires a non-zero sequence of RefEntry's; see:
244# http://www.oasis-open.org/docbook/documentation/reference/html/refentry.html
245# We keep track of number of generated entries and generate a dummy
246# if needs be to ensure the expanded template can be postprocessed
247# into html.
248my $section_counter = 0;
249
250my $lineprefix="";
251
252# states
253# 0 - normal code
254# 1 - looking for function name
255# 2 - scanning field start.
256# 3 - scanning prototype.
257# 4 - documentation block
258my $state;
Randy Dunlap850622d2006-06-25 05:48:55 -0700259my $in_doc_sect;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261#declaration types: can be
262# 'function', 'struct', 'union', 'enum', 'typedef'
263my $decl_type;
264
265my $doc_special = "\@\%\$\&";
266
267my $doc_start = '^/\*\*\s*$'; # Allow whitespace at end of comment start.
268my $doc_end = '\*/';
269my $doc_com = '\s*\*\s*';
270my $doc_decl = $doc_com.'(\w+)';
Randy Dunlap891dcd22007-02-10 01:45:53 -0800271my $doc_sect = $doc_com.'(['.$doc_special.']?[\w\s]+):(.*)';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272my $doc_content = $doc_com.'(.*)';
273my $doc_block = $doc_com.'DOC:\s*(.*)?';
274
275my %constants;
276my %parameterdescs;
277my @parameterlist;
278my %sections;
279my @sectionlist;
280
281my $contents = "";
282my $section_default = "Description"; # default section
283my $section_intro = "Introduction";
284my $section = $section_default;
285my $section_context = "Context";
286
287my $undescribed = "-- undescribed --";
288
289reset_state();
290
291while ($ARGV[0] =~ m/^-(.*)/) {
292 my $cmd = shift @ARGV;
293 if ($cmd eq "-html") {
294 $output_mode = "html";
295 %highlights = %highlights_html;
296 $blankline = $blankline_html;
297 } elsif ($cmd eq "-man") {
298 $output_mode = "man";
299 %highlights = %highlights_man;
300 $blankline = $blankline_man;
301 } elsif ($cmd eq "-text") {
302 $output_mode = "text";
303 %highlights = %highlights_text;
304 $blankline = $blankline_text;
305 } elsif ($cmd eq "-docbook") {
306 $output_mode = "xml";
307 %highlights = %highlights_xml;
308 $blankline = $blankline_xml;
309 } elsif ($cmd eq "-gnome") {
310 $output_mode = "gnome";
311 %highlights = %highlights_gnome;
312 $blankline = $blankline_gnome;
313 } elsif ($cmd eq "-module") { # not needed for XML, inherits from calling document
314 $modulename = shift @ARGV;
315 } elsif ($cmd eq "-function") { # to only output specific functions
316 $function_only = 1;
317 $function = shift @ARGV;
318 $function_table{$function} = 1;
319 } elsif ($cmd eq "-nofunction") { # to only output specific functions
320 $function_only = 2;
321 $function = shift @ARGV;
322 $function_table{$function} = 1;
323 } elsif ($cmd eq "-v") {
324 $verbose = 1;
325 } elsif (($cmd eq "-h") || ($cmd eq "--help")) {
326 usage();
327 } elsif ($cmd eq '-filelist') {
328 $filelist = shift @ARGV;
329 }
330}
331
Borislav Petkov53f049f2007-05-08 00:30:54 -0700332# get kernel version from env
333sub get_kernel_version() {
334 my $version;
335
336 if (defined($ENV{'KERNELVERSION'})) {
337 $version = $ENV{'KERNELVERSION'};
338 }
339 return $version;
340}
Borislav Petkov03662992007-05-09 02:33:43 -0700341my $kernelversion = get_kernel_version();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343# generate a sequence of code that will splice in highlighting information
344# using the s// operator.
345my $dohighlight = "";
346foreach my $pattern (keys %highlights) {
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700347# print STDERR "scanning pattern:$pattern, highlight:($highlights{$pattern})\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 $dohighlight .= "\$contents =~ s:$pattern:$highlights{$pattern}:gs;\n";
349}
350
351##
352# dumps section contents to arrays/hashes intended for that purpose.
353#
354sub dump_section {
355 my $name = shift;
356 my $contents = join "\n", @_;
357
358 if ($name =~ m/$type_constant/) {
359 $name = $1;
360# print STDERR "constant section '$1' = '$contents'\n";
361 $constants{$name} = $contents;
362 } elsif ($name =~ m/$type_param/) {
363# print STDERR "parameter def '$1' = '$contents'\n";
364 $name = $1;
365 $parameterdescs{$name} = $contents;
366 } else {
367# print STDERR "other section '$name' = '$contents'\n";
368 $sections{$name} = $contents;
369 push @sectionlist, $name;
370 }
371}
372
373##
374# output function
375#
376# parameterdescs, a hash.
377# function => "function name"
378# parameterlist => @list of parameters
379# parameterdescs => %parameter descriptions
380# sectionlist => @list of sections
Randy Dunlapa21217d2007-02-10 01:46:04 -0800381# sections => %section descriptions
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800382#
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384sub output_highlight {
385 my $contents = join "\n",@_;
386 my $line;
387
388# DEBUG
389# if (!defined $contents) {
390# use Carp;
391# confess "output_highlight got called with no args?\n";
392# }
393
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700394# print STDERR "contents b4:$contents\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 eval $dohighlight;
396 die $@ if $@;
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700397 if ($output_mode eq "html") {
398 $contents =~ s/\\\\//;
399 }
400# print STDERR "contents af:$contents\n";
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 foreach $line (split "\n", $contents) {
Randy Dunlap3c308792007-05-08 00:24:39 -0700403 if ($line eq ""){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 print $lineprefix, $blankline;
405 } else {
Randy Dunlap3c308792007-05-08 00:24:39 -0700406 $line =~ s/\\\\\\/\&/g;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 print $lineprefix, $line;
408 }
409 print "\n";
410 }
411}
412
413#output sections in html
414sub output_section_html(%) {
415 my %args = %{$_[0]};
416 my $section;
417
418 foreach $section (@{$args{'sectionlist'}}) {
419 print "<h3>$section</h3>\n";
420 print "<blockquote>\n";
421 output_highlight($args{'sections'}{$section});
422 print "</blockquote>\n";
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
426# output enum in html
427sub output_enum_html(%) {
428 my %args = %{$_[0]};
429 my ($parameter);
430 my $count;
431 print "<h2>enum ".$args{'enum'}."</h2>\n";
432
433 print "<b>enum ".$args{'enum'}."</b> {<br>\n";
434 $count = 0;
435 foreach $parameter (@{$args{'parameterlist'}}) {
Randy Dunlap3c308792007-05-08 00:24:39 -0700436 print " <b>".$parameter."</b>";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if ($count != $#{$args{'parameterlist'}}) {
438 $count++;
439 print ",\n";
440 }
441 print "<br>";
442 }
443 print "};<br>\n";
444
445 print "<h3>Constants</h3>\n";
446 print "<dl>\n";
447 foreach $parameter (@{$args{'parameterlist'}}) {
448 print "<dt><b>".$parameter."</b>\n";
449 print "<dd>";
450 output_highlight($args{'parameterdescs'}{$parameter});
451 }
452 print "</dl>\n";
453 output_section_html(@_);
454 print "<hr>\n";
455}
456
Randy Dunlapd28bee02006-02-01 03:06:57 -0800457# output typedef in html
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458sub output_typedef_html(%) {
459 my %args = %{$_[0]};
460 my ($parameter);
461 my $count;
462 print "<h2>typedef ".$args{'typedef'}."</h2>\n";
463
464 print "<b>typedef ".$args{'typedef'}."</b>\n";
465 output_section_html(@_);
466 print "<hr>\n";
467}
468
469# output struct in html
470sub output_struct_html(%) {
471 my %args = %{$_[0]};
472 my ($parameter);
473
Randy Dunlap262d9b02007-02-20 13:58:04 -0800474 print "<h2>".$args{'type'}." ".$args{'struct'}. " - " .$args{'purpose'}."</h2>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 print "<b>".$args{'type'}." ".$args{'struct'}."</b> {<br>\n";
476 foreach $parameter (@{$args{'parameterlist'}}) {
477 if ($parameter =~ /^#/) {
478 print "$parameter<br>\n";
479 next;
480 }
481 my $parameter_name = $parameter;
482 $parameter_name =~ s/\[.*//;
483
Randy Dunlap3c308792007-05-08 00:24:39 -0700484 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 $type = $args{'parametertypes'}{$parameter};
486 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
487 # pointer-to-function
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700488 print "&nbsp; &nbsp; <i>$1</i><b>$parameter</b>) <i>($2)</i>;<br>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700490 # bitfield
491 print "&nbsp; &nbsp; <i>$1</i> <b>$parameter</b>$2;<br>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 } else {
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700493 print "&nbsp; &nbsp; <i>$type</i> <b>$parameter</b>;<br>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
495 }
496 print "};<br>\n";
497
498 print "<h3>Members</h3>\n";
499 print "<dl>\n";
500 foreach $parameter (@{$args{'parameterlist'}}) {
501 ($parameter =~ /^#/) && next;
502
503 my $parameter_name = $parameter;
504 $parameter_name =~ s/\[.*//;
505
Randy Dunlap3c308792007-05-08 00:24:39 -0700506 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 print "<dt><b>".$parameter."</b>\n";
508 print "<dd>";
509 output_highlight($args{'parameterdescs'}{$parameter_name});
510 }
511 print "</dl>\n";
512 output_section_html(@_);
513 print "<hr>\n";
514}
515
516# output function in html
517sub output_function_html(%) {
518 my %args = %{$_[0]};
519 my ($parameter, $section);
520 my $count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Randy Dunlap262d9b02007-02-20 13:58:04 -0800522 print "<h2>" .$args{'function'}." - ".$args{'purpose'}."</h2>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 print "<i>".$args{'functiontype'}."</i>\n";
524 print "<b>".$args{'function'}."</b>\n";
525 print "(";
526 $count = 0;
527 foreach $parameter (@{$args{'parameterlist'}}) {
528 $type = $args{'parametertypes'}{$parameter};
529 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
530 # pointer-to-function
531 print "<i>$1</i><b>$parameter</b>) <i>($2)</i>";
532 } else {
533 print "<i>".$type."</i> <b>".$parameter."</b>";
534 }
535 if ($count != $#{$args{'parameterlist'}}) {
536 $count++;
537 print ",\n";
538 }
539 }
540 print ")\n";
541
542 print "<h3>Arguments</h3>\n";
543 print "<dl>\n";
544 foreach $parameter (@{$args{'parameterlist'}}) {
545 my $parameter_name = $parameter;
546 $parameter_name =~ s/\[.*//;
547
Randy Dunlap3c308792007-05-08 00:24:39 -0700548 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 print "<dt><b>".$parameter."</b>\n";
550 print "<dd>";
551 output_highlight($args{'parameterdescs'}{$parameter_name});
552 }
553 print "</dl>\n";
554 output_section_html(@_);
555 print "<hr>\n";
556}
557
558# output intro in html
559sub output_intro_html(%) {
560 my %args = %{$_[0]};
561 my ($parameter, $section);
562 my $count;
563
564 foreach $section (@{$args{'sectionlist'}}) {
565 print "<h3>$section</h3>\n";
566 print "<ul>\n";
567 output_highlight($args{'sections'}{$section});
568 print "</ul>\n";
569 }
570 print "<hr>\n";
571}
572
573sub output_section_xml(%) {
574 my %args = %{$_[0]};
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800575 my $section;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 # print out each section
577 $lineprefix=" ";
578 foreach $section (@{$args{'sectionlist'}}) {
Rich Walkerc73894c2005-05-01 08:59:26 -0700579 print "<refsect1>\n";
580 print "<title>$section</title>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if ($section =~ m/EXAMPLE/i) {
Rich Walkerc73894c2005-05-01 08:59:26 -0700582 print "<informalexample><programlisting>\n";
583 } else {
584 print "<para>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
586 output_highlight($args{'sections'}{$section});
587 if ($section =~ m/EXAMPLE/i) {
Rich Walkerc73894c2005-05-01 08:59:26 -0700588 print "</programlisting></informalexample>\n";
589 } else {
590 print "</para>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
Rich Walkerc73894c2005-05-01 08:59:26 -0700592 print "</refsect1>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594}
595
596# output function in XML DocBook
597sub output_function_xml(%) {
598 my %args = %{$_[0]};
599 my ($parameter, $section);
600 my $count;
601 my $id;
602
603 $id = "API-".$args{'function'};
604 $id =~ s/[^A-Za-z0-9]/-/g;
605
Pavel Pisa5449bc92007-02-10 01:45:37 -0800606 print "<refentry id=\"$id\">\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700607 print "<refentryinfo>\n";
608 print " <title>LINUX</title>\n";
609 print " <productname>Kernel Hackers Manual</productname>\n";
610 print " <date>$man_date</date>\n";
611 print "</refentryinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 print "<refmeta>\n";
Pavel Pisa5449bc92007-02-10 01:45:37 -0800613 print " <refentrytitle><phrase>".$args{'function'}."</phrase></refentrytitle>\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700614 print " <manvolnum>9</manvolnum>\n";
Borislav Petkov03662992007-05-09 02:33:43 -0700615 print " <refmiscinfo class=\"version\">" . $kernelversion . "</refmiscinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 print "</refmeta>\n";
617 print "<refnamediv>\n";
618 print " <refname>".$args{'function'}."</refname>\n";
619 print " <refpurpose>\n";
620 print " ";
621 output_highlight ($args{'purpose'});
622 print " </refpurpose>\n";
623 print "</refnamediv>\n";
624
625 print "<refsynopsisdiv>\n";
626 print " <title>Synopsis</title>\n";
627 print " <funcsynopsis><funcprototype>\n";
628 print " <funcdef>".$args{'functiontype'}." ";
629 print "<function>".$args{'function'}." </function></funcdef>\n";
630
631 $count = 0;
632 if ($#{$args{'parameterlist'}} >= 0) {
633 foreach $parameter (@{$args{'parameterlist'}}) {
634 $type = $args{'parametertypes'}{$parameter};
635 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
636 # pointer-to-function
637 print " <paramdef>$1<parameter>$parameter</parameter>)\n";
638 print " <funcparams>$2</funcparams></paramdef>\n";
639 } else {
640 print " <paramdef>".$type;
641 print " <parameter>$parameter</parameter></paramdef>\n";
642 }
643 }
644 } else {
Martin Waitz6013d542005-05-01 08:59:25 -0700645 print " <void/>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
647 print " </funcprototype></funcsynopsis>\n";
648 print "</refsynopsisdiv>\n";
649
650 # print parameters
651 print "<refsect1>\n <title>Arguments</title>\n";
652 if ($#{$args{'parameterlist'}} >= 0) {
653 print " <variablelist>\n";
654 foreach $parameter (@{$args{'parameterlist'}}) {
655 my $parameter_name = $parameter;
656 $parameter_name =~ s/\[.*//;
657
658 print " <varlistentry>\n <term><parameter>$parameter</parameter></term>\n";
659 print " <listitem>\n <para>\n";
660 $lineprefix=" ";
661 output_highlight($args{'parameterdescs'}{$parameter_name});
662 print " </para>\n </listitem>\n </varlistentry>\n";
663 }
664 print " </variablelist>\n";
665 } else {
666 print " <para>\n None\n </para>\n";
667 }
668 print "</refsect1>\n";
669
670 output_section_xml(@_);
671 print "</refentry>\n\n";
672}
673
674# output struct in XML DocBook
675sub output_struct_xml(%) {
676 my %args = %{$_[0]};
677 my ($parameter, $section);
678 my $id;
679
680 $id = "API-struct-".$args{'struct'};
681 $id =~ s/[^A-Za-z0-9]/-/g;
682
Pavel Pisa5449bc92007-02-10 01:45:37 -0800683 print "<refentry id=\"$id\">\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700684 print "<refentryinfo>\n";
685 print " <title>LINUX</title>\n";
686 print " <productname>Kernel Hackers Manual</productname>\n";
687 print " <date>$man_date</date>\n";
688 print "</refentryinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 print "<refmeta>\n";
Pavel Pisa5449bc92007-02-10 01:45:37 -0800690 print " <refentrytitle><phrase>".$args{'type'}." ".$args{'struct'}."</phrase></refentrytitle>\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700691 print " <manvolnum>9</manvolnum>\n";
Borislav Petkov03662992007-05-09 02:33:43 -0700692 print " <refmiscinfo class=\"version\">" . $kernelversion . "</refmiscinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 print "</refmeta>\n";
694 print "<refnamediv>\n";
695 print " <refname>".$args{'type'}." ".$args{'struct'}."</refname>\n";
696 print " <refpurpose>\n";
697 print " ";
698 output_highlight ($args{'purpose'});
699 print " </refpurpose>\n";
700 print "</refnamediv>\n";
701
702 print "<refsynopsisdiv>\n";
703 print " <title>Synopsis</title>\n";
704 print " <programlisting>\n";
705 print $args{'type'}." ".$args{'struct'}." {\n";
706 foreach $parameter (@{$args{'parameterlist'}}) {
707 if ($parameter =~ /^#/) {
708 print "$parameter\n";
709 next;
710 }
711
712 my $parameter_name = $parameter;
713 $parameter_name =~ s/\[.*//;
714
715 defined($args{'parameterdescs'}{$parameter_name}) || next;
Randy Dunlap3c308792007-05-08 00:24:39 -0700716 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 $type = $args{'parametertypes'}{$parameter};
718 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
719 # pointer-to-function
720 print " $1 $parameter) ($2);\n";
721 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
722 print " $1 $parameter$2;\n";
723 } else {
724 print " ".$type." ".$parameter.";\n";
725 }
726 }
727 print "};";
728 print " </programlisting>\n";
729 print "</refsynopsisdiv>\n";
730
731 print " <refsect1>\n";
732 print " <title>Members</title>\n";
733
734 print " <variablelist>\n";
735 foreach $parameter (@{$args{'parameterlist'}}) {
736 ($parameter =~ /^#/) && next;
737
738 my $parameter_name = $parameter;
739 $parameter_name =~ s/\[.*//;
740
741 defined($args{'parameterdescs'}{$parameter_name}) || next;
742 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
743 print " <varlistentry>";
744 print " <term>$parameter</term>\n";
745 print " <listitem><para>\n";
746 output_highlight($args{'parameterdescs'}{$parameter_name});
747 print " </para></listitem>\n";
748 print " </varlistentry>\n";
749 }
750 print " </variablelist>\n";
751 print " </refsect1>\n";
752
753 output_section_xml(@_);
754
755 print "</refentry>\n\n";
756}
757
758# output enum in XML DocBook
759sub output_enum_xml(%) {
760 my %args = %{$_[0]};
761 my ($parameter, $section);
762 my $count;
763 my $id;
764
765 $id = "API-enum-".$args{'enum'};
766 $id =~ s/[^A-Za-z0-9]/-/g;
767
Pavel Pisa5449bc92007-02-10 01:45:37 -0800768 print "<refentry id=\"$id\">\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700769 print "<refentryinfo>\n";
770 print " <title>LINUX</title>\n";
771 print " <productname>Kernel Hackers Manual</productname>\n";
772 print " <date>$man_date</date>\n";
773 print "</refentryinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 print "<refmeta>\n";
Pavel Pisa5449bc92007-02-10 01:45:37 -0800775 print " <refentrytitle><phrase>enum ".$args{'enum'}."</phrase></refentrytitle>\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700776 print " <manvolnum>9</manvolnum>\n";
Borislav Petkov03662992007-05-09 02:33:43 -0700777 print " <refmiscinfo class=\"version\">" . $kernelversion . "</refmiscinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 print "</refmeta>\n";
779 print "<refnamediv>\n";
780 print " <refname>enum ".$args{'enum'}."</refname>\n";
781 print " <refpurpose>\n";
782 print " ";
783 output_highlight ($args{'purpose'});
784 print " </refpurpose>\n";
785 print "</refnamediv>\n";
786
787 print "<refsynopsisdiv>\n";
788 print " <title>Synopsis</title>\n";
789 print " <programlisting>\n";
790 print "enum ".$args{'enum'}." {\n";
791 $count = 0;
792 foreach $parameter (@{$args{'parameterlist'}}) {
Randy Dunlap3c308792007-05-08 00:24:39 -0700793 print " $parameter";
794 if ($count != $#{$args{'parameterlist'}}) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 $count++;
796 print ",";
Randy Dunlap3c308792007-05-08 00:24:39 -0700797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 print "\n";
799 }
800 print "};";
801 print " </programlisting>\n";
802 print "</refsynopsisdiv>\n";
803
804 print "<refsect1>\n";
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800805 print " <title>Constants</title>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 print " <variablelist>\n";
807 foreach $parameter (@{$args{'parameterlist'}}) {
808 my $parameter_name = $parameter;
809 $parameter_name =~ s/\[.*//;
810
811 print " <varlistentry>";
812 print " <term>$parameter</term>\n";
813 print " <listitem><para>\n";
814 output_highlight($args{'parameterdescs'}{$parameter_name});
815 print " </para></listitem>\n";
816 print " </varlistentry>\n";
817 }
818 print " </variablelist>\n";
819 print "</refsect1>\n";
820
821 output_section_xml(@_);
822
823 print "</refentry>\n\n";
824}
825
826# output typedef in XML DocBook
827sub output_typedef_xml(%) {
828 my %args = %{$_[0]};
829 my ($parameter, $section);
830 my $id;
831
832 $id = "API-typedef-".$args{'typedef'};
833 $id =~ s/[^A-Za-z0-9]/-/g;
834
Pavel Pisa5449bc92007-02-10 01:45:37 -0800835 print "<refentry id=\"$id\">\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700836 print "<refentryinfo>\n";
837 print " <title>LINUX</title>\n";
838 print " <productname>Kernel Hackers Manual</productname>\n";
839 print " <date>$man_date</date>\n";
840 print "</refentryinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 print "<refmeta>\n";
Pavel Pisa5449bc92007-02-10 01:45:37 -0800842 print " <refentrytitle><phrase>typedef ".$args{'typedef'}."</phrase></refentrytitle>\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700843 print " <manvolnum>9</manvolnum>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 print "</refmeta>\n";
845 print "<refnamediv>\n";
846 print " <refname>typedef ".$args{'typedef'}."</refname>\n";
847 print " <refpurpose>\n";
848 print " ";
849 output_highlight ($args{'purpose'});
850 print " </refpurpose>\n";
851 print "</refnamediv>\n";
852
853 print "<refsynopsisdiv>\n";
854 print " <title>Synopsis</title>\n";
855 print " <synopsis>typedef ".$args{'typedef'}.";</synopsis>\n";
856 print "</refsynopsisdiv>\n";
857
858 output_section_xml(@_);
859
860 print "</refentry>\n\n";
861}
862
863# output in XML DocBook
864sub output_intro_xml(%) {
865 my %args = %{$_[0]};
866 my ($parameter, $section);
867 my $count;
868
869 my $id = $args{'module'};
870 $id =~ s/[^A-Za-z0-9]/-/g;
871
872 # print out each section
873 $lineprefix=" ";
874 foreach $section (@{$args{'sectionlist'}}) {
875 print "<refsect1>\n <title>$section</title>\n <para>\n";
876 if ($section =~ m/EXAMPLE/i) {
877 print "<example><para>\n";
878 }
879 output_highlight($args{'sections'}{$section});
880 if ($section =~ m/EXAMPLE/i) {
881 print "</para></example>\n";
882 }
883 print " </para>\n</refsect1>\n";
884 }
885
886 print "\n\n";
887}
888
889# output in XML DocBook
890sub output_function_gnome {
891 my %args = %{$_[0]};
892 my ($parameter, $section);
893 my $count;
894 my $id;
895
896 $id = $args{'module'}."-".$args{'function'};
897 $id =~ s/[^A-Za-z0-9]/-/g;
898
899 print "<sect2>\n";
900 print " <title id=\"$id\">".$args{'function'}."</title>\n";
901
902 print " <funcsynopsis>\n";
903 print " <funcdef>".$args{'functiontype'}." ";
904 print "<function>".$args{'function'}." ";
905 print "</function></funcdef>\n";
906
907 $count = 0;
908 if ($#{$args{'parameterlist'}} >= 0) {
909 foreach $parameter (@{$args{'parameterlist'}}) {
910 $type = $args{'parametertypes'}{$parameter};
911 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
912 # pointer-to-function
913 print " <paramdef>$1 <parameter>$parameter</parameter>)\n";
914 print " <funcparams>$2</funcparams></paramdef>\n";
915 } else {
916 print " <paramdef>".$type;
917 print " <parameter>$parameter</parameter></paramdef>\n";
918 }
919 }
920 } else {
921 print " <void>\n";
922 }
923 print " </funcsynopsis>\n";
924 if ($#{$args{'parameterlist'}} >= 0) {
925 print " <informaltable pgwide=\"1\" frame=\"none\" role=\"params\">\n";
926 print "<tgroup cols=\"2\">\n";
927 print "<colspec colwidth=\"2*\">\n";
928 print "<colspec colwidth=\"8*\">\n";
929 print "<tbody>\n";
930 foreach $parameter (@{$args{'parameterlist'}}) {
931 my $parameter_name = $parameter;
932 $parameter_name =~ s/\[.*//;
933
934 print " <row><entry align=\"right\"><parameter>$parameter</parameter></entry>\n";
935 print " <entry>\n";
936 $lineprefix=" ";
937 output_highlight($args{'parameterdescs'}{$parameter_name});
938 print " </entry></row>\n";
939 }
940 print " </tbody></tgroup></informaltable>\n";
941 } else {
942 print " <para>\n None\n </para>\n";
943 }
944
945 # print out each section
946 $lineprefix=" ";
947 foreach $section (@{$args{'sectionlist'}}) {
948 print "<simplesect>\n <title>$section</title>\n";
949 if ($section =~ m/EXAMPLE/i) {
950 print "<example><programlisting>\n";
951 } else {
952 }
953 print "<para>\n";
954 output_highlight($args{'sections'}{$section});
955 print "</para>\n";
956 if ($section =~ m/EXAMPLE/i) {
957 print "</programlisting></example>\n";
958 } else {
959 }
960 print " </simplesect>\n";
961 }
962
963 print "</sect2>\n\n";
964}
965
966##
967# output function in man
968sub output_function_man(%) {
969 my %args = %{$_[0]};
970 my ($parameter, $section);
971 my $count;
972
973 print ".TH \"$args{'function'}\" 9 \"$args{'function'}\" \"$man_date\" \"Kernel Hacker's Manual\" LINUX\n";
974
975 print ".SH NAME\n";
976 print $args{'function'}." \\- ".$args{'purpose'}."\n";
977
978 print ".SH SYNOPSIS\n";
Randy Dunlapa21217d2007-02-10 01:46:04 -0800979 if ($args{'functiontype'} ne "") {
980 print ".B \"".$args{'functiontype'}."\" ".$args{'function'}."\n";
981 } else {
982 print ".B \"".$args{'function'}."\n";
983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 $count = 0;
985 my $parenth = "(";
986 my $post = ",";
987 foreach my $parameter (@{$args{'parameterlist'}}) {
988 if ($count == $#{$args{'parameterlist'}}) {
989 $post = ");";
990 }
991 $type = $args{'parametertypes'}{$parameter};
992 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
993 # pointer-to-function
994 print ".BI \"".$parenth.$1."\" ".$parameter." \") (".$2.")".$post."\"\n";
995 } else {
996 $type =~ s/([^\*])$/$1 /;
997 print ".BI \"".$parenth.$type."\" ".$parameter." \"".$post."\"\n";
998 }
999 $count++;
1000 $parenth = "";
1001 }
1002
1003 print ".SH ARGUMENTS\n";
1004 foreach $parameter (@{$args{'parameterlist'}}) {
1005 my $parameter_name = $parameter;
1006 $parameter_name =~ s/\[.*//;
1007
1008 print ".IP \"".$parameter."\" 12\n";
1009 output_highlight($args{'parameterdescs'}{$parameter_name});
1010 }
1011 foreach $section (@{$args{'sectionlist'}}) {
1012 print ".SH \"", uc $section, "\"\n";
1013 output_highlight($args{'sections'}{$section});
1014 }
1015}
1016
1017##
1018# output enum in man
1019sub output_enum_man(%) {
1020 my %args = %{$_[0]};
1021 my ($parameter, $section);
1022 my $count;
1023
1024 print ".TH \"$args{'module'}\" 9 \"enum $args{'enum'}\" \"$man_date\" \"API Manual\" LINUX\n";
1025
1026 print ".SH NAME\n";
1027 print "enum ".$args{'enum'}." \\- ".$args{'purpose'}."\n";
1028
1029 print ".SH SYNOPSIS\n";
1030 print "enum ".$args{'enum'}." {\n";
1031 $count = 0;
1032 foreach my $parameter (@{$args{'parameterlist'}}) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001033 print ".br\n.BI \" $parameter\"\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 if ($count == $#{$args{'parameterlist'}}) {
1035 print "\n};\n";
1036 last;
1037 }
1038 else {
1039 print ", \n.br\n";
1040 }
1041 $count++;
1042 }
1043
1044 print ".SH Constants\n";
1045 foreach $parameter (@{$args{'parameterlist'}}) {
1046 my $parameter_name = $parameter;
1047 $parameter_name =~ s/\[.*//;
1048
1049 print ".IP \"".$parameter."\" 12\n";
1050 output_highlight($args{'parameterdescs'}{$parameter_name});
1051 }
1052 foreach $section (@{$args{'sectionlist'}}) {
1053 print ".SH \"$section\"\n";
1054 output_highlight($args{'sections'}{$section});
1055 }
1056}
1057
1058##
1059# output struct in man
1060sub output_struct_man(%) {
1061 my %args = %{$_[0]};
1062 my ($parameter, $section);
1063
1064 print ".TH \"$args{'module'}\" 9 \"".$args{'type'}." ".$args{'struct'}."\" \"$man_date\" \"API Manual\" LINUX\n";
1065
1066 print ".SH NAME\n";
1067 print $args{'type'}." ".$args{'struct'}." \\- ".$args{'purpose'}."\n";
1068
1069 print ".SH SYNOPSIS\n";
1070 print $args{'type'}." ".$args{'struct'}." {\n.br\n";
1071
1072 foreach my $parameter (@{$args{'parameterlist'}}) {
1073 if ($parameter =~ /^#/) {
1074 print ".BI \"$parameter\"\n.br\n";
1075 next;
1076 }
1077 my $parameter_name = $parameter;
1078 $parameter_name =~ s/\[.*//;
1079
Randy Dunlap3c308792007-05-08 00:24:39 -07001080 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 $type = $args{'parametertypes'}{$parameter};
1082 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1083 # pointer-to-function
1084 print ".BI \" ".$1."\" ".$parameter." \") (".$2.")"."\"\n;\n";
1085 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
Randy.Dunlap1d7e1d42006-07-01 04:36:34 -07001086 # bitfield
1087 print ".BI \" ".$1."\ \" ".$parameter.$2." \""."\"\n;\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 } else {
1089 $type =~ s/([^\*])$/$1 /;
1090 print ".BI \" ".$type."\" ".$parameter." \""."\"\n;\n";
1091 }
1092 print "\n.br\n";
1093 }
1094 print "};\n.br\n";
1095
Randy Dunlapc51d3da2006-06-25 05:49:14 -07001096 print ".SH Members\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 foreach $parameter (@{$args{'parameterlist'}}) {
1098 ($parameter =~ /^#/) && next;
1099
1100 my $parameter_name = $parameter;
1101 $parameter_name =~ s/\[.*//;
1102
Randy Dunlap3c308792007-05-08 00:24:39 -07001103 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 print ".IP \"".$parameter."\" 12\n";
1105 output_highlight($args{'parameterdescs'}{$parameter_name});
1106 }
1107 foreach $section (@{$args{'sectionlist'}}) {
1108 print ".SH \"$section\"\n";
1109 output_highlight($args{'sections'}{$section});
1110 }
1111}
1112
1113##
1114# output typedef in man
1115sub output_typedef_man(%) {
1116 my %args = %{$_[0]};
1117 my ($parameter, $section);
1118
1119 print ".TH \"$args{'module'}\" 9 \"$args{'typedef'}\" \"$man_date\" \"API Manual\" LINUX\n";
1120
1121 print ".SH NAME\n";
1122 print "typedef ".$args{'typedef'}." \\- ".$args{'purpose'}."\n";
1123
1124 foreach $section (@{$args{'sectionlist'}}) {
1125 print ".SH \"$section\"\n";
1126 output_highlight($args{'sections'}{$section});
1127 }
1128}
1129
1130sub output_intro_man(%) {
1131 my %args = %{$_[0]};
1132 my ($parameter, $section);
1133 my $count;
1134
1135 print ".TH \"$args{'module'}\" 9 \"$args{'module'}\" \"$man_date\" \"API Manual\" LINUX\n";
1136
1137 foreach $section (@{$args{'sectionlist'}}) {
1138 print ".SH \"$section\"\n";
1139 output_highlight($args{'sections'}{$section});
1140 }
1141}
1142
1143##
1144# output in text
1145sub output_function_text(%) {
1146 my %args = %{$_[0]};
1147 my ($parameter, $section);
Randy Dunlapa21217d2007-02-10 01:46:04 -08001148 my $start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Randy Dunlapf47634b2006-07-01 04:36:36 -07001150 print "Name:\n\n";
1151 print $args{'function'}." - ".$args{'purpose'}."\n";
1152
1153 print "\nSynopsis:\n\n";
Randy Dunlapa21217d2007-02-10 01:46:04 -08001154 if ($args{'functiontype'} ne "") {
1155 $start = $args{'functiontype'}." ".$args{'function'}." (";
1156 } else {
1157 $start = $args{'function'}." (";
1158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 print $start;
Randy Dunlapa21217d2007-02-10 01:46:04 -08001160
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 my $count = 0;
1162 foreach my $parameter (@{$args{'parameterlist'}}) {
1163 $type = $args{'parametertypes'}{$parameter};
1164 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1165 # pointer-to-function
1166 print $1.$parameter.") (".$2;
1167 } else {
1168 print $type." ".$parameter;
1169 }
1170 if ($count != $#{$args{'parameterlist'}}) {
1171 $count++;
1172 print ",\n";
1173 print " " x length($start);
1174 } else {
1175 print ");\n\n";
1176 }
1177 }
1178
1179 print "Arguments:\n\n";
1180 foreach $parameter (@{$args{'parameterlist'}}) {
1181 my $parameter_name = $parameter;
1182 $parameter_name =~ s/\[.*//;
1183
1184 print $parameter."\n\t".$args{'parameterdescs'}{$parameter_name}."\n";
1185 }
1186 output_section_text(@_);
1187}
1188
1189#output sections in text
1190sub output_section_text(%) {
1191 my %args = %{$_[0]};
1192 my $section;
1193
1194 print "\n";
1195 foreach $section (@{$args{'sectionlist'}}) {
1196 print "$section:\n\n";
1197 output_highlight($args{'sections'}{$section});
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 print "\n\n";
1200}
1201
1202# output enum in text
1203sub output_enum_text(%) {
1204 my %args = %{$_[0]};
1205 my ($parameter);
1206 my $count;
1207 print "Enum:\n\n";
1208
Randy.Dunlap1d7e1d42006-07-01 04:36:34 -07001209 print "enum ".$args{'enum'}." - ".$args{'purpose'}."\n\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 print "enum ".$args{'enum'}." {\n";
1211 $count = 0;
1212 foreach $parameter (@{$args{'parameterlist'}}) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001213 print "\t$parameter";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 if ($count != $#{$args{'parameterlist'}}) {
1215 $count++;
1216 print ",";
1217 }
1218 print "\n";
1219 }
1220 print "};\n\n";
1221
1222 print "Constants:\n\n";
1223 foreach $parameter (@{$args{'parameterlist'}}) {
1224 print "$parameter\n\t";
1225 print $args{'parameterdescs'}{$parameter}."\n";
1226 }
1227
1228 output_section_text(@_);
1229}
1230
1231# output typedef in text
1232sub output_typedef_text(%) {
1233 my %args = %{$_[0]};
1234 my ($parameter);
1235 my $count;
1236 print "Typedef:\n\n";
1237
Randy.Dunlap1d7e1d42006-07-01 04:36:34 -07001238 print "typedef ".$args{'typedef'}." - ".$args{'purpose'}."\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 output_section_text(@_);
1240}
1241
1242# output struct as text
1243sub output_struct_text(%) {
1244 my %args = %{$_[0]};
1245 my ($parameter);
1246
Randy.Dunlap1d7e1d42006-07-01 04:36:34 -07001247 print $args{'type'}." ".$args{'struct'}." - ".$args{'purpose'}."\n\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 print $args{'type'}." ".$args{'struct'}." {\n";
1249 foreach $parameter (@{$args{'parameterlist'}}) {
1250 if ($parameter =~ /^#/) {
1251 print "$parameter\n";
1252 next;
1253 }
1254
1255 my $parameter_name = $parameter;
1256 $parameter_name =~ s/\[.*//;
1257
Randy Dunlap3c308792007-05-08 00:24:39 -07001258 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 $type = $args{'parametertypes'}{$parameter};
1260 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1261 # pointer-to-function
1262 print "\t$1 $parameter) ($2);\n";
1263 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
1264 print "\t$1 $parameter$2;\n";
1265 } else {
1266 print "\t".$type." ".$parameter.";\n";
1267 }
1268 }
1269 print "};\n\n";
1270
1271 print "Members:\n\n";
1272 foreach $parameter (@{$args{'parameterlist'}}) {
1273 ($parameter =~ /^#/) && next;
1274
1275 my $parameter_name = $parameter;
1276 $parameter_name =~ s/\[.*//;
1277
Randy Dunlap3c308792007-05-08 00:24:39 -07001278 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 print "$parameter\n\t";
1280 print $args{'parameterdescs'}{$parameter_name}."\n";
1281 }
1282 print "\n";
1283 output_section_text(@_);
1284}
1285
1286sub output_intro_text(%) {
1287 my %args = %{$_[0]};
1288 my ($parameter, $section);
1289
1290 foreach $section (@{$args{'sectionlist'}}) {
1291 print " $section:\n";
1292 print " -> ";
1293 output_highlight($args{'sections'}{$section});
1294 }
1295}
1296
1297##
Randy Dunlap27205742006-10-11 01:22:12 -07001298# generic output function for all types (function, struct/union, typedef, enum);
1299# calls the generated, variable output_ function name based on
1300# functype and output_mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301sub output_declaration {
1302 no strict 'refs';
1303 my $name = shift;
1304 my $functype = shift;
1305 my $func = "output_${functype}_$output_mode";
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001306 if (($function_only==0) ||
1307 ( $function_only == 1 && defined($function_table{$name})) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 ( $function_only == 2 && !defined($function_table{$name})))
1309 {
Randy Dunlap3c308792007-05-08 00:24:39 -07001310 &$func(@_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 $section_counter++;
1312 }
1313}
1314
1315##
Randy Dunlap27205742006-10-11 01:22:12 -07001316# generic output function - calls the right one based on current output mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317sub output_intro {
1318 no strict 'refs';
1319 my $func = "output_intro_".$output_mode;
1320 &$func(@_);
1321 $section_counter++;
1322}
1323
1324##
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001325# takes a declaration (struct, union, enum, typedef) and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326# invokes the right handler. NOT called for functions.
1327sub dump_declaration($$) {
1328 no strict 'refs';
1329 my ($prototype, $file) = @_;
1330 my $func = "dump_".$decl_type;
1331 &$func(@_);
1332}
1333
1334sub dump_union($$) {
1335 dump_struct(@_);
1336}
1337
1338sub dump_struct($$) {
1339 my $x = shift;
1340 my $file = shift;
1341
1342 if ($x =~/(struct|union)\s+(\w+)\s*{(.*)}/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001343 $declaration_name = $2;
1344 my $members = $3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 # ignore embedded structs or unions
1347 $members =~ s/{.*?}//g;
1348
Martin Waitzaeec46b2005-11-13 16:08:13 -08001349 # ignore members marked private:
1350 $members =~ s/\/\*.*?private:.*?public:.*?\*\///gos;
1351 $members =~ s/\/\*.*?private:.*//gos;
1352 # strip comments:
1353 $members =~ s/\/\*.*?\*\///gos;
1354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 create_parameterlist($members, ';', $file);
1356
1357 output_declaration($declaration_name,
1358 'struct',
1359 {'struct' => $declaration_name,
1360 'module' => $modulename,
1361 'parameterlist' => \@parameterlist,
1362 'parameterdescs' => \%parameterdescs,
1363 'parametertypes' => \%parametertypes,
1364 'sectionlist' => \@sectionlist,
1365 'sections' => \%sections,
1366 'purpose' => $declaration_purpose,
1367 'type' => $decl_type
1368 });
1369 }
1370 else {
Randy Dunlap3c308792007-05-08 00:24:39 -07001371 print STDERR "Error(${file}:$.): Cannot parse struct or union!\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 ++$errors;
1373 }
1374}
1375
1376sub dump_enum($$) {
1377 my $x = shift;
1378 my $file = shift;
1379
Martin Waitzaeec46b2005-11-13 16:08:13 -08001380 $x =~ s@/\*.*?\*/@@gos; # strip comments.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 if ($x =~ /enum\s+(\w+)\s*{(.*)}/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001382 $declaration_name = $1;
1383 my $members = $2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 foreach my $arg (split ',', $members) {
1386 $arg =~ s/^\s*(\w+).*/$1/;
1387 push @parameterlist, $arg;
1388 if (!$parameterdescs{$arg}) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001389 $parameterdescs{$arg} = $undescribed;
1390 print STDERR "Warning(${file}:$.): Enum value '$arg' ".
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 "not described in enum '$declaration_name'\n";
1392 }
1393
1394 }
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 output_declaration($declaration_name,
1397 'enum',
1398 {'enum' => $declaration_name,
1399 'module' => $modulename,
1400 'parameterlist' => \@parameterlist,
1401 'parameterdescs' => \%parameterdescs,
1402 'sectionlist' => \@sectionlist,
1403 'sections' => \%sections,
1404 'purpose' => $declaration_purpose
1405 });
1406 }
1407 else {
Randy Dunlap3c308792007-05-08 00:24:39 -07001408 print STDERR "Error(${file}:$.): Cannot parse enum!\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 ++$errors;
1410 }
1411}
1412
1413sub dump_typedef($$) {
1414 my $x = shift;
1415 my $file = shift;
1416
Martin Waitzaeec46b2005-11-13 16:08:13 -08001417 $x =~ s@/\*.*?\*/@@gos; # strip comments.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 while (($x =~ /\(*.\)\s*;$/) || ($x =~ /\[*.\]\s*;$/)) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001419 $x =~ s/\(*.\)\s*;$/;/;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 $x =~ s/\[*.\]\s*;$/;/;
1421 }
1422
1423 if ($x =~ /typedef.*\s+(\w+)\s*;/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001424 $declaration_name = $1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
1426 output_declaration($declaration_name,
1427 'typedef',
1428 {'typedef' => $declaration_name,
1429 'module' => $modulename,
1430 'sectionlist' => \@sectionlist,
1431 'sections' => \%sections,
1432 'purpose' => $declaration_purpose
1433 });
1434 }
1435 else {
Randy Dunlap3c308792007-05-08 00:24:39 -07001436 print STDERR "Error(${file}:$.): Cannot parse typedef!\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 ++$errors;
1438 }
1439}
1440
1441sub create_parameterlist($$$) {
1442 my $args = shift;
1443 my $splitter = shift;
1444 my $file = shift;
1445 my $type;
1446 my $param;
1447
Martin Waitza6d3fe72006-01-09 20:53:55 -08001448 # temporarily replace commas inside function pointer definition
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 while ($args =~ /(\([^\),]+),/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001450 $args =~ s/(\([^\),]+),/$1#/g;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 }
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001452
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 foreach my $arg (split($splitter, $args)) {
1454 # strip comments
1455 $arg =~ s/\/\*.*\*\///;
Randy Dunlap3c308792007-05-08 00:24:39 -07001456 # strip leading/trailing spaces
1457 $arg =~ s/^\s*//;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 $arg =~ s/\s*$//;
1459 $arg =~ s/\s+/ /;
1460
1461 if ($arg =~ /^#/) {
1462 # Treat preprocessor directive as a typeless variable just to fill
1463 # corresponding data structures "correctly". Catch it later in
1464 # output_* subs.
1465 push_parameter($arg, "", $file);
Randy Dunlapea82c742006-12-06 20:38:52 -08001466 } elsif ($arg =~ m/\(.*\*/) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 # pointer-to-function
1468 $arg =~ tr/#/,/;
Randy Dunlap996a07b2007-02-10 01:45:56 -08001469 $arg =~ m/[^\(]+\(\*\s*([^\)]+)\)/;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 $param = $1;
1471 $type = $arg;
1472 $type =~ s/([^\(]+\(\*)$param/$1/;
1473 push_parameter($param, $type, $file);
Martin Waitzaeec46b2005-11-13 16:08:13 -08001474 } elsif ($arg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 $arg =~ s/\s*:\s*/:/g;
1476 $arg =~ s/\s*\[/\[/g;
1477
1478 my @args = split('\s*,\s*', $arg);
1479 if ($args[0] =~ m/\*/) {
1480 $args[0] =~ s/(\*+)\s*/ $1/;
1481 }
Borislav Petkov884f2812007-05-08 00:29:05 -07001482
1483 my @first_arg;
1484 if ($args[0] =~ /^(.*\s+)(.*?\[.*\].*)$/) {
1485 shift @args;
1486 push(@first_arg, split('\s+', $1));
1487 push(@first_arg, $2);
1488 } else {
1489 @first_arg = split('\s+', shift @args);
1490 }
1491
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 unshift(@args, pop @first_arg);
1493 $type = join " ", @first_arg;
1494
1495 foreach $param (@args) {
1496 if ($param =~ m/^(\*+)\s*(.*)/) {
1497 push_parameter($2, "$type $1", $file);
1498 }
1499 elsif ($param =~ m/(.*?):(\d+)/) {
1500 push_parameter($1, "$type:$2", $file)
1501 }
1502 else {
1503 push_parameter($param, $type, $file);
1504 }
1505 }
1506 }
1507 }
1508}
1509
1510sub push_parameter($$$) {
1511 my $param = shift;
1512 my $type = shift;
1513 my $file = shift;
1514
Randy Dunlap5f8c7c92007-07-19 01:48:24 -07001515 if (($anon_struct_union == 1) && ($type eq "") &&
1516 ($param eq "}")) {
1517 return; # ignore the ending }; from anon. struct/union
1518 }
1519
1520 $anon_struct_union = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 my $param_name = $param;
1522 $param_name =~ s/\[.*//;
1523
Martin Waitza6d3fe72006-01-09 20:53:55 -08001524 if ($type eq "" && $param =~ /\.\.\.$/)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 {
1526 $type="";
Martin Waitza6d3fe72006-01-09 20:53:55 -08001527 $parameterdescs{$param} = "variable arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 }
1529 elsif ($type eq "" && ($param eq "" or $param eq "void"))
1530 {
1531 $type="";
1532 $param="void";
1533 $parameterdescs{void} = "no arguments";
1534 }
Randy Dunlap134fe012006-12-22 01:10:50 -08001535 elsif ($type eq "" && ($param eq "struct" or $param eq "union"))
1536 # handle unnamed (anonymous) union or struct:
1537 {
1538 $type = $param;
Randy Dunlap5f8c7c92007-07-19 01:48:24 -07001539 $param = "{unnamed_" . $param . "}";
Randy Dunlap134fe012006-12-22 01:10:50 -08001540 $parameterdescs{$param} = "anonymous\n";
Randy Dunlap5f8c7c92007-07-19 01:48:24 -07001541 $anon_struct_union = 1;
Randy Dunlap134fe012006-12-22 01:10:50 -08001542 }
1543
Martin Waitza6d3fe72006-01-09 20:53:55 -08001544 # warn if parameter has no description
Randy Dunlap134fe012006-12-22 01:10:50 -08001545 # (but ignore ones starting with # as these are not parameters
1546 # but inline preprocessor statements);
1547 # also ignore unnamed structs/unions;
Randy Dunlap5f8c7c92007-07-19 01:48:24 -07001548 if (!$anon_struct_union) {
Martin Waitza6d3fe72006-01-09 20:53:55 -08001549 if (!defined $parameterdescs{$param_name} && $param_name !~ /^#/) {
1550
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 $parameterdescs{$param_name} = $undescribed;
1552
1553 if (($type eq 'function') || ($type eq 'enum')) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001554 print STDERR "Warning(${file}:$.): Function parameter ".
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 "or member '$param' not " .
1556 "described in '$declaration_name'\n";
1557 }
1558 print STDERR "Warning(${file}:$.):".
Randy Dunlap3c308792007-05-08 00:24:39 -07001559 " No description found for parameter '$param'\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 ++$warnings;
Randy Dunlap3c308792007-05-08 00:24:39 -07001561 }
1562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
1564 push @parameterlist, $param;
1565 $parametertypes{$param} = $type;
1566}
1567
1568##
1569# takes a function prototype and the name of the current file being
1570# processed and spits out all the details stored in the global
1571# arrays/hashes.
1572sub dump_function($$) {
1573 my $prototype = shift;
1574 my $file = shift;
1575
1576 $prototype =~ s/^static +//;
1577 $prototype =~ s/^extern +//;
Pavel Pisa4dc3b162005-05-01 08:59:25 -07001578 $prototype =~ s/^fastcall +//;
1579 $prototype =~ s/^asmlinkage +//;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 $prototype =~ s/^inline +//;
1581 $prototype =~ s/^__inline__ +//;
Randy Dunlap32e79402006-10-11 01:22:10 -07001582 $prototype =~ s/^__inline +//;
1583 $prototype =~ s/^__always_inline +//;
1584 $prototype =~ s/^noinline +//;
Randy Dunlap0129a052006-07-30 03:03:41 -07001585 $prototype =~ s/__devinit +//;
Randy Dunlap996a07b2007-02-10 01:45:56 -08001586 $prototype =~ s/^#define\s+//; #ak added
Randy Dunlap328d2442007-02-28 20:12:10 -08001587 $prototype =~ s/__attribute__\s*\(\([a-z,]*\)\)//;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
1589 # Yes, this truly is vile. We are looking for:
1590 # 1. Return type (may be nothing if we're looking at a macro)
1591 # 2. Function name
1592 # 3. Function parameters.
1593 #
1594 # All the while we have to watch out for function pointer parameters
1595 # (which IIRC is what the two sections are for), C types (these
1596 # regexps don't even start to express all the possibilities), and
1597 # so on.
1598 #
1599 # If you mess with these regexps, it's a good idea to check that
1600 # the following functions' documentation still comes out right:
1601 # - parport_register_device (function pointer parameters)
1602 # - atomic_set (macro)
Martin Waitz9598f912006-02-01 03:06:55 -08001603 # - pci_match_device, __copy_to_user (long return type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
1605 if ($prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1606 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1607 $prototype =~ m/^(\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1608 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1609 $prototype =~ m/^(\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1610 $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1611 $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1612 $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1613 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1614 $prototype =~ m/^(\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1615 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1616 $prototype =~ m/^(\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1617 $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
Martin Waitz9598f912006-02-01 03:06:55 -08001618 $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1619 $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
Randy Dunlap412ecd772007-02-10 22:47:33 -08001620 $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1621 $prototype =~ m/^(\w+\s+\w+\s*\*\s*\w+\s*\*\s*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 $return_type = $1;
1623 $declaration_name = $2;
1624 my $args = $3;
1625
1626 create_parameterlist($args, ',', $file);
1627 } else {
1628 print STDERR "Error(${file}:$.): cannot understand prototype: '$prototype'\n";
1629 ++$errors;
1630 return;
1631 }
1632
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001633 output_declaration($declaration_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 'function',
1635 {'function' => $declaration_name,
1636 'module' => $modulename,
1637 'functiontype' => $return_type,
1638 'parameterlist' => \@parameterlist,
1639 'parameterdescs' => \%parameterdescs,
1640 'parametertypes' => \%parametertypes,
1641 'sectionlist' => \@sectionlist,
1642 'sections' => \%sections,
1643 'purpose' => $declaration_purpose
1644 });
1645}
1646
1647sub process_file($);
1648
1649# Read the file that maps relative names to absolute names for
1650# separate source and object directories and for shadow trees.
1651if (open(SOURCE_MAP, "<.tmp_filelist.txt")) {
1652 my ($relname, $absname);
1653 while(<SOURCE_MAP>) {
1654 chop();
1655 ($relname, $absname) = (split())[0..1];
1656 $relname =~ s:^/+::;
1657 $source_map{$relname} = $absname;
1658 }
1659 close(SOURCE_MAP);
1660}
1661
1662if ($filelist) {
1663 open(FLIST,"<$filelist") or die "Can't open file list $filelist";
1664 while(<FLIST>) {
1665 chop;
1666 process_file($_);
1667 }
1668}
1669
1670foreach (@ARGV) {
1671 chomp;
1672 process_file($_);
1673}
1674if ($verbose && $errors) {
1675 print STDERR "$errors errors\n";
1676}
1677if ($verbose && $warnings) {
1678 print STDERR "$warnings warnings\n";
1679}
1680
1681exit($errors);
1682
1683sub reset_state {
1684 $function = "";
1685 %constants = ();
1686 %parameterdescs = ();
1687 %parametertypes = ();
1688 @parameterlist = ();
1689 %sections = ();
1690 @sectionlist = ();
1691 $prototype = "";
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001692
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 $state = 0;
1694}
1695
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001696sub process_state3_function($$) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 my $x = shift;
1698 my $file = shift;
1699
1700 if ($x =~ m#\s*/\*\s+MACDOC\s*#io || ($x =~ /^#/ && $x !~ /^#define/)) {
1701 # do nothing
1702 }
1703 elsif ($x =~ /([^\{]*)/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001704 $prototype .= $1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 }
1706 if (($x =~ /\{/) || ($x =~ /\#define/) || ($x =~ /;/)) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001707 $prototype =~ s@/\*.*?\*/@@gos; # strip comments.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 $prototype =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1709 $prototype =~ s@^\s+@@gos; # strip leading spaces
1710 dump_function($prototype,$file);
1711 reset_state();
1712 }
1713}
1714
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001715sub process_state3_type($$) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 my $x = shift;
1717 my $file = shift;
1718
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 $x =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1720 $x =~ s@^\s+@@gos; # strip leading spaces
1721 $x =~ s@\s+$@@gos; # strip trailing spaces
1722 if ($x =~ /^#/) {
1723 # To distinguish preprocessor directive from regular declaration later.
1724 $x .= ";";
1725 }
1726
1727 while (1) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001728 if ( $x =~ /([^{};]*)([{};])(.*)/ ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 $prototype .= $1 . $2;
1730 ($2 eq '{') && $brcount++;
1731 ($2 eq '}') && $brcount--;
1732 if (($2 eq ';') && ($brcount == 0)) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001733 dump_declaration($prototype,$file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 reset_state();
Randy Dunlap3c308792007-05-08 00:24:39 -07001735 last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 }
1737 $x = $3;
Randy Dunlap3c308792007-05-08 00:24:39 -07001738 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 $prototype .= $x;
1740 last;
1741 }
1742 }
1743}
1744
1745# replace <, >, and &
1746sub xml_escape($) {
1747 my $text = shift;
Randy Dunlapecfb2512006-06-25 05:49:13 -07001748 if (($output_mode eq "text") || ($output_mode eq "man")) {
1749 return $text;
1750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 $text =~ s/\&/\\\\\\amp;/g;
1752 $text =~ s/\</\\\\\\lt;/g;
1753 $text =~ s/\>/\\\\\\gt;/g;
1754 return $text;
1755}
1756
1757sub process_file($) {
Randy Dunlap2283a112005-07-07 15:39:26 -07001758 my $file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 my $identifier;
1760 my $func;
Randy Dunlapa21217d2007-02-10 01:46:04 -08001761 my $descr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 my $initial_section_counter = $section_counter;
1763
Randy Dunlap2283a112005-07-07 15:39:26 -07001764 if (defined($ENV{'SRCTREE'})) {
1765 $file = "$ENV{'SRCTREE'}" . "/" . "@_";
1766 }
1767 else {
1768 $file = "@_";
1769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 if (defined($source_map{$file})) {
1771 $file = $source_map{$file};
1772 }
1773
1774 if (!open(IN,"<$file")) {
1775 print STDERR "Error: Cannot open file $file\n";
1776 ++$errors;
1777 return;
1778 }
1779
1780 $section_counter = 0;
1781 while (<IN>) {
1782 if ($state == 0) {
1783 if (/$doc_start/o) {
1784 $state = 1; # next line is always the function name
Randy Dunlap850622d2006-06-25 05:48:55 -07001785 $in_doc_sect = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 }
1787 } elsif ($state == 1) { # this line is the function name (always)
1788 if (/$doc_block/o) {
1789 $state = 4;
1790 $contents = "";
1791 if ( $1 eq "" ) {
1792 $section = $section_intro;
1793 } else {
1794 $section = $1;
1795 }
Randy Dunlap3c308792007-05-08 00:24:39 -07001796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 elsif (/$doc_decl/o) {
1798 $identifier = $1;
1799 if (/\s*([\w\s]+?)\s*-/) {
1800 $identifier = $1;
1801 }
1802
1803 $state = 2;
1804 if (/-(.*)/) {
Randy Dunlapa21217d2007-02-10 01:46:04 -08001805 # strip leading/trailing/multiple spaces #RDD:T:
1806 $descr= $1;
1807 $descr =~ s/^\s*//;
1808 $descr =~ s/\s*$//;
1809 $descr =~ s/\s+/ /;
1810 $declaration_purpose = xml_escape($descr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 } else {
1812 $declaration_purpose = "";
1813 }
1814 if ($identifier =~ m/^struct/) {
1815 $decl_type = 'struct';
1816 } elsif ($identifier =~ m/^union/) {
1817 $decl_type = 'union';
1818 } elsif ($identifier =~ m/^enum/) {
1819 $decl_type = 'enum';
1820 } elsif ($identifier =~ m/^typedef/) {
1821 $decl_type = 'typedef';
1822 } else {
1823 $decl_type = 'function';
1824 }
1825
1826 if ($verbose) {
1827 print STDERR "Info(${file}:$.): Scanning doc for $identifier\n";
1828 }
1829 } else {
1830 print STDERR "Warning(${file}:$.): Cannot understand $_ on line $.",
1831 " - I thought it was a doc line\n";
1832 ++$warnings;
1833 $state = 0;
1834 }
1835 } elsif ($state == 2) { # look for head: lines, and include content
1836 if (/$doc_sect/o) {
1837 $newsection = $1;
1838 $newcontents = $2;
1839
1840 if ($contents ne "") {
Randy Dunlap850622d2006-06-25 05:48:55 -07001841 if (!$in_doc_sect && $verbose) {
1842 print STDERR "Warning(${file}:$.): contents before sections\n";
1843 ++$warnings;
1844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 dump_section($section, xml_escape($contents));
1846 $section = $section_default;
1847 }
1848
Randy Dunlap850622d2006-06-25 05:48:55 -07001849 $in_doc_sect = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 $contents = $newcontents;
1851 if ($contents ne "") {
Randy Dunlap27205742006-10-11 01:22:12 -07001852 while ((substr($contents, 0, 1) eq " ") ||
1853 substr($contents, 0, 1) eq "\t") {
1854 $contents = substr($contents, 1);
Randy Dunlap05189492006-06-25 05:47:47 -07001855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 $contents .= "\n";
1857 }
1858 $section = $newsection;
1859 } elsif (/$doc_end/) {
1860
1861 if ($contents ne "") {
1862 dump_section($section, xml_escape($contents));
1863 $section = $section_default;
1864 $contents = "";
1865 }
1866
1867 $prototype = "";
1868 $state = 3;
1869 $brcount = 0;
Randy Dunlap232acbc2006-06-25 05:47:48 -07001870# print STDERR "end of doc comment, looking for prototype\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 } elsif (/$doc_content/) {
1872 # miguel-style comment kludge, look for blank lines after
1873 # @parameter line to signify start of description
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001874 if ($1 eq "" &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 ($section =~ m/^@/ || $section eq $section_context)) {
1876 dump_section($section, xml_escape($contents));
1877 $section = $section_default;
1878 $contents = "";
1879 } else {
1880 $contents .= $1."\n";
1881 }
1882 } else {
1883 # i dont know - bad line? ignore.
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001884 print STDERR "Warning(${file}:$.): bad line: $_";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 ++$warnings;
1886 }
Randy Dunlap232acbc2006-06-25 05:47:48 -07001887 } elsif ($state == 3) { # scanning for function '{' (end of prototype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 if ($decl_type eq 'function') {
Randy Dunlap3c308792007-05-08 00:24:39 -07001889 process_state3_function($_, $file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 } else {
Randy Dunlap3c308792007-05-08 00:24:39 -07001891 process_state3_type($_, $file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 }
1893 } elsif ($state == 4) {
1894 # Documentation block
Randy Dunlap3c308792007-05-08 00:24:39 -07001895 if (/$doc_block/) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 dump_section($section, $contents);
1897 output_intro({'sectionlist' => \@sectionlist,
1898 'sections' => \%sections });
1899 $contents = "";
1900 $function = "";
1901 %constants = ();
1902 %parameterdescs = ();
1903 %parametertypes = ();
1904 @parameterlist = ();
1905 %sections = ();
1906 @sectionlist = ();
1907 $prototype = "";
1908 if ( $1 eq "" ) {
1909 $section = $section_intro;
1910 } else {
1911 $section = $1;
1912 }
Randy Dunlap3c308792007-05-08 00:24:39 -07001913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 elsif (/$doc_end/)
1915 {
1916 dump_section($section, $contents);
1917 output_intro({'sectionlist' => \@sectionlist,
1918 'sections' => \%sections });
1919 $contents = "";
1920 $function = "";
1921 %constants = ();
1922 %parameterdescs = ();
1923 %parametertypes = ();
1924 @parameterlist = ();
1925 %sections = ();
1926 @sectionlist = ();
1927 $prototype = "";
1928 $state = 0;
1929 }
1930 elsif (/$doc_content/)
1931 {
1932 if ( $1 eq "" )
1933 {
1934 $contents .= $blankline;
1935 }
1936 else
1937 {
1938 $contents .= $1 . "\n";
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001939 }
Randy Dunlap3c308792007-05-08 00:24:39 -07001940 }
1941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 }
1943 if ($initial_section_counter == $section_counter) {
1944 print STDERR "Warning(${file}): no structured comments found\n";
1945 if ($output_mode eq "xml") {
1946 # The template wants at least one RefEntry here; make one.
1947 print "<refentry>\n";
1948 print " <refnamediv>\n";
1949 print " <refname>\n";
1950 print " ${file}\n";
1951 print " </refname>\n";
1952 print " <refpurpose>\n";
1953 print " Document generation inconsistency\n";
1954 print " </refpurpose>\n";
1955 print " </refnamediv>\n";
1956 print " <refsect1>\n";
1957 print " <title>\n";
1958 print " Oops\n";
1959 print " </title>\n";
1960 print " <warning>\n";
1961 print " <para>\n";
1962 print " The template for this document tried to insert\n";
1963 print " the structured comment from the file\n";
1964 print " <filename>${file}</filename> at this point,\n";
1965 print " but none was found.\n";
1966 print " This dummy section is inserted to allow\n";
1967 print " generation to continue.\n";
1968 print " </para>\n";
1969 print " </warning>\n";
1970 print " </refsect1>\n";
1971 print "</refentry>\n";
1972 }
1973 }
1974}