blob: 8cf528b5e65a5fa9fc2918388431f19949a1c11e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#!/usr/bin/perl -w
2
3use strict;
4
5## Copyright (c) 1998 Michael Zucchi, All Rights Reserved ##
6## Copyright (C) 2000, 1 Tim Waugh <twaugh@redhat.com> ##
7## Copyright (C) 2001 Simon Huggins ##
Randy Dunlap6b5b55f2007-10-16 23:31:20 -07008## Copyright (C) 2005-2007 Randy Dunlap ##
Linus Torvalds1da177e2005-04-16 15:20:36 -07009## ##
10## #define enhancements by Armin Kuster <akuster@mvista.com> ##
11## Copyright (c) 2000 MontaVista Software, Inc. ##
12## ##
13## This software falls under the GNU General Public License. ##
14## Please read the COPYING file for more information ##
15
16# w.o. 03-11-2000: added the '-filelist' option.
17
18# 18/01/2001 - Cleanups
19# Functions prototyped as foo(void) same as foo()
20# Stop eval'ing where we don't need to.
21# -- huggie@earth.li
22
23# 27/06/2001 - Allowed whitespace after initial "/**" and
24# allowed comments before function declarations.
25# -- Christian Kreibich <ck@whoop.org>
26
27# Still to do:
28# - add perldoc documentation
29# - Look more closely at some of the scarier bits :)
30
31# 26/05/2001 - Support for separate source and object trees.
32# Return error code.
33# Keith Owens <kaos@ocs.com.au>
34
35# 23/09/2001 - Added support for typedefs, structs, enums and unions
36# Support for Context section; can be terminated using empty line
37# Small fixes (like spaces vs. \s in regex)
38# -- Tim Jansen <tim@tjansen.de>
39
40
41#
42# This will read a 'c' file and scan for embedded comments in the
43# style of gnome comments (+minor extensions - see below).
44#
45
46# Note: This only supports 'c'.
47
48# usage:
Randy Dunlapd28bee02006-02-01 03:06:57 -080049# kernel-doc [ -docbook | -html | -text | -man ]
Linus Torvalds1da177e2005-04-16 15:20:36 -070050# [ -function funcname [ -function funcname ...] ] c file(s)s > outputfile
51# or
52# [ -nofunction funcname [ -function funcname ...] ] c file(s)s > outputfile
53#
54# Set output format using one of -docbook -html -text or -man. Default is man.
55#
56# -function funcname
57# If set, then only generate documentation for the given function(s). All
58# other functions are ignored.
59#
60# -nofunction funcname
Rolf Eike Beera073a8b2006-10-03 23:13:02 +020061# If set, then only generate documentation for the other function(s).
62# Cannot be used together with -function
Randy Dunlapd28bee02006-02-01 03:06:57 -080063# (yes, that's a bug -- perl hackers can fix it 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#
65# c files - list of 'c' files to process
66#
67# All output goes to stdout, with errors to stderr.
68
69#
70# format of comments.
71# In the following table, (...)? signifies optional structure.
72# (...)* signifies 0 or more structure elements
73# /**
74# * function_name(:)? (- short description)?
75# (* @parameterx: (description of parameter x)?)*
76# (* a blank line)?
77# * (Description:)? (Description of function)?
78# * (section header: (section description)? )*
79# (*)?*/
80#
81# So .. the trivial example would be:
82#
83# /**
84# * my_function
85# **/
86#
Randy Dunlap891dcd22007-02-10 01:45:53 -080087# If the Description: header tag is omitted, then there must be a blank line
Linus Torvalds1da177e2005-04-16 15:20:36 -070088# after the last parameter specification.
89# e.g.
90# /**
91# * my_function - does my stuff
92# * @my_arg: its mine damnit
93# *
Randy Dunlap3c3b8092006-02-01 03:06:58 -080094# * Does my stuff explained.
Linus Torvalds1da177e2005-04-16 15:20:36 -070095# */
96#
97# or, could also use:
98# /**
99# * my_function - does my stuff
100# * @my_arg: its mine damnit
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800101# * Description: Does my stuff explained.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102# */
103# etc.
104#
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800105# Beside functions you can also write documentation for structs, unions,
106# enums and typedefs. Instead of the function name you must write the name
107# of the declaration; the struct/union/enum/typedef must always precede
108# the name. Nesting of declarations is not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109# Use the argument mechanism to document members or constants.
110# e.g.
111# /**
112# * struct my_struct - short description
113# * @a: first member
114# * @b: second member
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800115# *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116# * Longer description
117# */
118# struct my_struct {
119# int a;
120# int b;
Martin Waitzaeec46b2005-11-13 16:08:13 -0800121# /* private: */
122# int c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123# };
124#
125# All descriptions can be multiline, except the short function description.
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800126#
127# You can also add additional sections. When documenting kernel functions you
128# should document the "Context:" of the function, e.g. whether the functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129# can be called form interrupts. Unlike other sections you can end it with an
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800130# empty line.
131# Example-sections should contain the string EXAMPLE so that they are marked
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132# appropriately in DocBook.
133#
134# Example:
135# /**
136# * user_function - function that can only be called in user context
137# * @a: some argument
138# * Context: !in_interrupt()
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800139# *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140# * Some description
141# * Example:
142# * user_function(22);
143# */
144# ...
145#
146#
147# All descriptive text is further processed, scanning for the following special
148# patterns, which are highlighted appropriately.
149#
150# 'funcname()' - function
151# '$ENVVAR' - environmental variable
152# '&struct_name' - name of a structure (up to two words including 'struct')
153# '@parameter' - name of a parameter
154# '%CONST' - name of a constant.
155
156my $errors = 0;
157my $warnings = 0;
Randy Dunlap5f8c7c92007-07-19 01:48:24 -0700158my $anon_struct_union = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160# match expressions used to find embedded type information
161my $type_constant = '\%([-_\w]+)';
162my $type_func = '(\w+)\(\)';
163my $type_param = '\@(\w+)';
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700164my $type_struct = '\&((struct\s*)*[_\w]+)';
Randy Dunlap6b5b55f2007-10-16 23:31:20 -0700165my $type_struct_xml = '\\&amp;((struct\s*)*[_\w]+)';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166my $type_env = '(\$\w+)';
167
168# Output conversion substitutions.
169# One for each output format
170
171# these work fairly well
172my %highlights_html = ( $type_constant, "<i>\$1</i>",
173 $type_func, "<b>\$1</b>",
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700174 $type_struct_xml, "<i>\$1</i>",
175 $type_env, "<b><i>\$1</i></b>",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 $type_param, "<tt><b>\$1</b></tt>" );
Randy Dunlap6b5b55f2007-10-16 23:31:20 -0700177my $local_lt = "\\\\\\\\lt:";
178my $local_gt = "\\\\\\\\gt:";
179my $blankline_html = $local_lt . "p" . $local_gt; # was "<p>"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181# XML, docbook format
182my %highlights_xml = ( "([^=])\\\"([^\\\"<]+)\\\"", "\$1<quote>\$2</quote>",
183 $type_constant, "<constant>\$1</constant>",
184 $type_func, "<function>\$1</function>",
Johannes Berg5c98fc02007-10-24 15:08:48 -0700185 $type_struct_xml, "<structname>\$1</structname>",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 $type_env, "<envar>\$1</envar>",
187 $type_param, "<parameter>\$1</parameter>" );
Johannes Berg5c98fc02007-10-24 15:08:48 -0700188my $blankline_xml = $local_lt . "/para" . $local_gt . $local_lt . "para" . $local_gt . "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190# gnome, docbook format
191my %highlights_gnome = ( $type_constant, "<replaceable class=\"option\">\$1</replaceable>",
192 $type_func, "<function>\$1</function>",
193 $type_struct, "<structname>\$1</structname>",
194 $type_env, "<envar>\$1</envar>",
195 $type_param, "<parameter>\$1</parameter>" );
196my $blankline_gnome = "</para><para>\n";
197
198# these are pretty rough
199my %highlights_man = ( $type_constant, "\$1",
200 $type_func, "\\\\fB\$1\\\\fP",
201 $type_struct, "\\\\fI\$1\\\\fP",
202 $type_param, "\\\\fI\$1\\\\fP" );
203my $blankline_man = "";
204
205# text-mode
206my %highlights_text = ( $type_constant, "\$1",
207 $type_func, "\$1",
208 $type_struct, "\$1",
209 $type_param, "\$1" );
210my $blankline_text = "";
211
212
213sub usage {
214 print "Usage: $0 [ -v ] [ -docbook | -html | -text | -man ]\n";
215 print " [ -function funcname [ -function funcname ...] ]\n";
216 print " [ -nofunction funcname [ -nofunction funcname ...] ]\n";
217 print " c source file(s) > outputfile\n";
218 exit 1;
219}
220
221# read arguments
222if ($#ARGV==-1) {
223 usage();
224}
225
226my $verbose = 0;
227my $output_mode = "man";
228my %highlights = %highlights_man;
229my $blankline = $blankline_man;
230my $modulename = "Kernel API";
231my $function_only = 0;
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800232my $man_date = ('January', 'February', 'March', 'April', 'May', 'June',
233 'July', 'August', 'September', 'October',
234 'November', 'December')[(localtime)[4]] .
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 " " . ((localtime)[5]+1900);
236
237# Essentially these are globals
238# They probably want to be tidied up made more localised or summat.
239# CAVEAT EMPTOR! Some of the others I localised may not want to be which
240# could cause "use of undefined value" or other bugs.
241my ($function, %function_table,%parametertypes,$declaration_purpose);
242my ($type,$declaration_name,$return_type);
243my ($newsection,$newcontents,$prototype,$filelist, $brcount, %source_map);
244
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800245# Generated docbook code is inserted in a template at a point where
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246# docbook v3.1 requires a non-zero sequence of RefEntry's; see:
247# http://www.oasis-open.org/docbook/documentation/reference/html/refentry.html
248# We keep track of number of generated entries and generate a dummy
249# if needs be to ensure the expanded template can be postprocessed
250# into html.
251my $section_counter = 0;
252
253my $lineprefix="";
254
255# states
256# 0 - normal code
257# 1 - looking for function name
258# 2 - scanning field start.
259# 3 - scanning prototype.
260# 4 - documentation block
261my $state;
Randy Dunlap850622d2006-06-25 05:48:55 -0700262my $in_doc_sect;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264#declaration types: can be
265# 'function', 'struct', 'union', 'enum', 'typedef'
266my $decl_type;
267
268my $doc_special = "\@\%\$\&";
269
270my $doc_start = '^/\*\*\s*$'; # Allow whitespace at end of comment start.
271my $doc_end = '\*/';
272my $doc_com = '\s*\*\s*';
273my $doc_decl = $doc_com.'(\w+)';
Randy Dunlap891dcd22007-02-10 01:45:53 -0800274my $doc_sect = $doc_com.'(['.$doc_special.']?[\w\s]+):(.*)';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275my $doc_content = $doc_com.'(.*)';
276my $doc_block = $doc_com.'DOC:\s*(.*)?';
277
278my %constants;
279my %parameterdescs;
280my @parameterlist;
281my %sections;
282my @sectionlist;
283
284my $contents = "";
285my $section_default = "Description"; # default section
286my $section_intro = "Introduction";
287my $section = $section_default;
288my $section_context = "Context";
289
290my $undescribed = "-- undescribed --";
291
292reset_state();
293
294while ($ARGV[0] =~ m/^-(.*)/) {
295 my $cmd = shift @ARGV;
296 if ($cmd eq "-html") {
297 $output_mode = "html";
298 %highlights = %highlights_html;
299 $blankline = $blankline_html;
300 } elsif ($cmd eq "-man") {
301 $output_mode = "man";
302 %highlights = %highlights_man;
303 $blankline = $blankline_man;
304 } elsif ($cmd eq "-text") {
305 $output_mode = "text";
306 %highlights = %highlights_text;
307 $blankline = $blankline_text;
308 } elsif ($cmd eq "-docbook") {
309 $output_mode = "xml";
310 %highlights = %highlights_xml;
311 $blankline = $blankline_xml;
312 } elsif ($cmd eq "-gnome") {
313 $output_mode = "gnome";
314 %highlights = %highlights_gnome;
315 $blankline = $blankline_gnome;
316 } elsif ($cmd eq "-module") { # not needed for XML, inherits from calling document
317 $modulename = shift @ARGV;
318 } elsif ($cmd eq "-function") { # to only output specific functions
319 $function_only = 1;
320 $function = shift @ARGV;
321 $function_table{$function} = 1;
322 } elsif ($cmd eq "-nofunction") { # to only output specific functions
323 $function_only = 2;
324 $function = shift @ARGV;
325 $function_table{$function} = 1;
326 } elsif ($cmd eq "-v") {
327 $verbose = 1;
328 } elsif (($cmd eq "-h") || ($cmd eq "--help")) {
329 usage();
330 } elsif ($cmd eq '-filelist') {
331 $filelist = shift @ARGV;
332 }
333}
334
Borislav Petkov53f049f2007-05-08 00:30:54 -0700335# get kernel version from env
336sub get_kernel_version() {
Johannes Berg1b9bc222007-10-24 15:08:48 -0700337 my $version = 'unknown kernel version';
Borislav Petkov53f049f2007-05-08 00:30:54 -0700338
339 if (defined($ENV{'KERNELVERSION'})) {
340 $version = $ENV{'KERNELVERSION'};
341 }
342 return $version;
343}
Borislav Petkov03662992007-05-09 02:33:43 -0700344my $kernelversion = get_kernel_version();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346# generate a sequence of code that will splice in highlighting information
347# using the s// operator.
348my $dohighlight = "";
349foreach my $pattern (keys %highlights) {
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700350# print STDERR "scanning pattern:$pattern, highlight:($highlights{$pattern})\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 $dohighlight .= "\$contents =~ s:$pattern:$highlights{$pattern}:gs;\n";
352}
353
354##
355# dumps section contents to arrays/hashes intended for that purpose.
356#
357sub dump_section {
358 my $name = shift;
359 my $contents = join "\n", @_;
360
361 if ($name =~ m/$type_constant/) {
362 $name = $1;
363# print STDERR "constant section '$1' = '$contents'\n";
364 $constants{$name} = $contents;
365 } elsif ($name =~ m/$type_param/) {
366# print STDERR "parameter def '$1' = '$contents'\n";
367 $name = $1;
368 $parameterdescs{$name} = $contents;
369 } else {
370# print STDERR "other section '$name' = '$contents'\n";
371 $sections{$name} = $contents;
372 push @sectionlist, $name;
373 }
374}
375
376##
377# output function
378#
379# parameterdescs, a hash.
380# function => "function name"
381# parameterlist => @list of parameters
382# parameterdescs => %parameter descriptions
383# sectionlist => @list of sections
Randy Dunlapa21217d2007-02-10 01:46:04 -0800384# sections => %section descriptions
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800385#
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387sub output_highlight {
388 my $contents = join "\n",@_;
389 my $line;
390
391# DEBUG
392# if (!defined $contents) {
393# use Carp;
394# confess "output_highlight got called with no args?\n";
395# }
396
Johannes Berg5c98fc02007-10-24 15:08:48 -0700397 if ($output_mode eq "html" || $output_mode eq "xml") {
Randy Dunlap6b5b55f2007-10-16 23:31:20 -0700398 $contents = local_unescape($contents);
399 # convert data read & converted thru xml_escape() into &xyz; format:
400 $contents =~ s/\\\\\\/&/g;
401 }
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700402# print STDERR "contents b4:$contents\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 eval $dohighlight;
404 die $@ if $@;
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700405# print STDERR "contents af:$contents\n";
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 foreach $line (split "\n", $contents) {
Randy Dunlap3c308792007-05-08 00:24:39 -0700408 if ($line eq ""){
Randy Dunlap6b5b55f2007-10-16 23:31:20 -0700409 print $lineprefix, local_unescape($blankline);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 } else {
Randy Dunlap3c308792007-05-08 00:24:39 -0700411 $line =~ s/\\\\\\/\&/g;
Randy Dunlapcdccb312007-07-19 01:48:25 -0700412 if ($output_mode eq "man" && substr($line, 0, 1) eq ".") {
413 print "\\&$line";
414 } else {
415 print $lineprefix, $line;
416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418 print "\n";
419 }
420}
421
422#output sections in html
423sub output_section_html(%) {
424 my %args = %{$_[0]};
425 my $section;
426
427 foreach $section (@{$args{'sectionlist'}}) {
428 print "<h3>$section</h3>\n";
429 print "<blockquote>\n";
430 output_highlight($args{'sections'}{$section});
431 print "</blockquote>\n";
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
435# output enum in html
436sub output_enum_html(%) {
437 my %args = %{$_[0]};
438 my ($parameter);
439 my $count;
440 print "<h2>enum ".$args{'enum'}."</h2>\n";
441
442 print "<b>enum ".$args{'enum'}."</b> {<br>\n";
443 $count = 0;
444 foreach $parameter (@{$args{'parameterlist'}}) {
Randy Dunlap3c308792007-05-08 00:24:39 -0700445 print " <b>".$parameter."</b>";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if ($count != $#{$args{'parameterlist'}}) {
447 $count++;
448 print ",\n";
449 }
450 print "<br>";
451 }
452 print "};<br>\n";
453
454 print "<h3>Constants</h3>\n";
455 print "<dl>\n";
456 foreach $parameter (@{$args{'parameterlist'}}) {
457 print "<dt><b>".$parameter."</b>\n";
458 print "<dd>";
459 output_highlight($args{'parameterdescs'}{$parameter});
460 }
461 print "</dl>\n";
462 output_section_html(@_);
463 print "<hr>\n";
464}
465
Randy Dunlapd28bee02006-02-01 03:06:57 -0800466# output typedef in html
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467sub output_typedef_html(%) {
468 my %args = %{$_[0]};
469 my ($parameter);
470 my $count;
471 print "<h2>typedef ".$args{'typedef'}."</h2>\n";
472
473 print "<b>typedef ".$args{'typedef'}."</b>\n";
474 output_section_html(@_);
475 print "<hr>\n";
476}
477
478# output struct in html
479sub output_struct_html(%) {
480 my %args = %{$_[0]};
481 my ($parameter);
482
Randy Dunlap262d9b02007-02-20 13:58:04 -0800483 print "<h2>".$args{'type'}." ".$args{'struct'}. " - " .$args{'purpose'}."</h2>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 print "<b>".$args{'type'}." ".$args{'struct'}."</b> {<br>\n";
485 foreach $parameter (@{$args{'parameterlist'}}) {
486 if ($parameter =~ /^#/) {
487 print "$parameter<br>\n";
488 next;
489 }
490 my $parameter_name = $parameter;
491 $parameter_name =~ s/\[.*//;
492
Randy Dunlap3c308792007-05-08 00:24:39 -0700493 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 $type = $args{'parametertypes'}{$parameter};
495 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
496 # pointer-to-function
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700497 print "&nbsp; &nbsp; <i>$1</i><b>$parameter</b>) <i>($2)</i>;<br>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700499 # bitfield
500 print "&nbsp; &nbsp; <i>$1</i> <b>$parameter</b>$2;<br>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 } else {
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700502 print "&nbsp; &nbsp; <i>$type</i> <b>$parameter</b>;<br>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
504 }
505 print "};<br>\n";
506
507 print "<h3>Members</h3>\n";
508 print "<dl>\n";
509 foreach $parameter (@{$args{'parameterlist'}}) {
510 ($parameter =~ /^#/) && next;
511
512 my $parameter_name = $parameter;
513 $parameter_name =~ s/\[.*//;
514
Randy Dunlap3c308792007-05-08 00:24:39 -0700515 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 print "<dt><b>".$parameter."</b>\n";
517 print "<dd>";
518 output_highlight($args{'parameterdescs'}{$parameter_name});
519 }
520 print "</dl>\n";
521 output_section_html(@_);
522 print "<hr>\n";
523}
524
525# output function in html
526sub output_function_html(%) {
527 my %args = %{$_[0]};
528 my ($parameter, $section);
529 my $count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Randy Dunlap262d9b02007-02-20 13:58:04 -0800531 print "<h2>" .$args{'function'}." - ".$args{'purpose'}."</h2>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 print "<i>".$args{'functiontype'}."</i>\n";
533 print "<b>".$args{'function'}."</b>\n";
534 print "(";
535 $count = 0;
536 foreach $parameter (@{$args{'parameterlist'}}) {
537 $type = $args{'parametertypes'}{$parameter};
538 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
539 # pointer-to-function
540 print "<i>$1</i><b>$parameter</b>) <i>($2)</i>";
541 } else {
542 print "<i>".$type."</i> <b>".$parameter."</b>";
543 }
544 if ($count != $#{$args{'parameterlist'}}) {
545 $count++;
546 print ",\n";
547 }
548 }
549 print ")\n";
550
551 print "<h3>Arguments</h3>\n";
552 print "<dl>\n";
553 foreach $parameter (@{$args{'parameterlist'}}) {
554 my $parameter_name = $parameter;
555 $parameter_name =~ s/\[.*//;
556
Randy Dunlap3c308792007-05-08 00:24:39 -0700557 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 print "<dt><b>".$parameter."</b>\n";
559 print "<dd>";
560 output_highlight($args{'parameterdescs'}{$parameter_name});
561 }
562 print "</dl>\n";
563 output_section_html(@_);
564 print "<hr>\n";
565}
566
567# output intro in html
568sub output_intro_html(%) {
569 my %args = %{$_[0]};
570 my ($parameter, $section);
571 my $count;
572
573 foreach $section (@{$args{'sectionlist'}}) {
574 print "<h3>$section</h3>\n";
575 print "<ul>\n";
576 output_highlight($args{'sections'}{$section});
577 print "</ul>\n";
578 }
579 print "<hr>\n";
580}
581
582sub output_section_xml(%) {
583 my %args = %{$_[0]};
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800584 my $section;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 # print out each section
586 $lineprefix=" ";
587 foreach $section (@{$args{'sectionlist'}}) {
Rich Walkerc73894c2005-05-01 08:59:26 -0700588 print "<refsect1>\n";
589 print "<title>$section</title>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 if ($section =~ m/EXAMPLE/i) {
Rich Walkerc73894c2005-05-01 08:59:26 -0700591 print "<informalexample><programlisting>\n";
592 } else {
593 print "<para>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
595 output_highlight($args{'sections'}{$section});
596 if ($section =~ m/EXAMPLE/i) {
Rich Walkerc73894c2005-05-01 08:59:26 -0700597 print "</programlisting></informalexample>\n";
598 } else {
599 print "</para>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
Rich Walkerc73894c2005-05-01 08:59:26 -0700601 print "</refsect1>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
603}
604
605# output function in XML DocBook
606sub output_function_xml(%) {
607 my %args = %{$_[0]};
608 my ($parameter, $section);
609 my $count;
610 my $id;
611
612 $id = "API-".$args{'function'};
613 $id =~ s/[^A-Za-z0-9]/-/g;
614
Pavel Pisa5449bc92007-02-10 01:45:37 -0800615 print "<refentry id=\"$id\">\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700616 print "<refentryinfo>\n";
617 print " <title>LINUX</title>\n";
618 print " <productname>Kernel Hackers Manual</productname>\n";
619 print " <date>$man_date</date>\n";
620 print "</refentryinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 print "<refmeta>\n";
Pavel Pisa5449bc92007-02-10 01:45:37 -0800622 print " <refentrytitle><phrase>".$args{'function'}."</phrase></refentrytitle>\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700623 print " <manvolnum>9</manvolnum>\n";
Borislav Petkov03662992007-05-09 02:33:43 -0700624 print " <refmiscinfo class=\"version\">" . $kernelversion . "</refmiscinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 print "</refmeta>\n";
626 print "<refnamediv>\n";
627 print " <refname>".$args{'function'}."</refname>\n";
628 print " <refpurpose>\n";
629 print " ";
630 output_highlight ($args{'purpose'});
631 print " </refpurpose>\n";
632 print "</refnamediv>\n";
633
634 print "<refsynopsisdiv>\n";
635 print " <title>Synopsis</title>\n";
636 print " <funcsynopsis><funcprototype>\n";
637 print " <funcdef>".$args{'functiontype'}." ";
638 print "<function>".$args{'function'}." </function></funcdef>\n";
639
640 $count = 0;
641 if ($#{$args{'parameterlist'}} >= 0) {
642 foreach $parameter (@{$args{'parameterlist'}}) {
643 $type = $args{'parametertypes'}{$parameter};
644 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
645 # pointer-to-function
646 print " <paramdef>$1<parameter>$parameter</parameter>)\n";
647 print " <funcparams>$2</funcparams></paramdef>\n";
648 } else {
649 print " <paramdef>".$type;
650 print " <parameter>$parameter</parameter></paramdef>\n";
651 }
652 }
653 } else {
Martin Waitz6013d542005-05-01 08:59:25 -0700654 print " <void/>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
656 print " </funcprototype></funcsynopsis>\n";
657 print "</refsynopsisdiv>\n";
658
659 # print parameters
660 print "<refsect1>\n <title>Arguments</title>\n";
661 if ($#{$args{'parameterlist'}} >= 0) {
662 print " <variablelist>\n";
663 foreach $parameter (@{$args{'parameterlist'}}) {
664 my $parameter_name = $parameter;
665 $parameter_name =~ s/\[.*//;
666
667 print " <varlistentry>\n <term><parameter>$parameter</parameter></term>\n";
668 print " <listitem>\n <para>\n";
669 $lineprefix=" ";
670 output_highlight($args{'parameterdescs'}{$parameter_name});
671 print " </para>\n </listitem>\n </varlistentry>\n";
672 }
673 print " </variablelist>\n";
674 } else {
675 print " <para>\n None\n </para>\n";
676 }
677 print "</refsect1>\n";
678
679 output_section_xml(@_);
680 print "</refentry>\n\n";
681}
682
683# output struct in XML DocBook
684sub output_struct_xml(%) {
685 my %args = %{$_[0]};
686 my ($parameter, $section);
687 my $id;
688
689 $id = "API-struct-".$args{'struct'};
690 $id =~ s/[^A-Za-z0-9]/-/g;
691
Pavel Pisa5449bc92007-02-10 01:45:37 -0800692 print "<refentry id=\"$id\">\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700693 print "<refentryinfo>\n";
694 print " <title>LINUX</title>\n";
695 print " <productname>Kernel Hackers Manual</productname>\n";
696 print " <date>$man_date</date>\n";
697 print "</refentryinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 print "<refmeta>\n";
Pavel Pisa5449bc92007-02-10 01:45:37 -0800699 print " <refentrytitle><phrase>".$args{'type'}." ".$args{'struct'}."</phrase></refentrytitle>\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700700 print " <manvolnum>9</manvolnum>\n";
Borislav Petkov03662992007-05-09 02:33:43 -0700701 print " <refmiscinfo class=\"version\">" . $kernelversion . "</refmiscinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 print "</refmeta>\n";
703 print "<refnamediv>\n";
704 print " <refname>".$args{'type'}." ".$args{'struct'}."</refname>\n";
705 print " <refpurpose>\n";
706 print " ";
707 output_highlight ($args{'purpose'});
708 print " </refpurpose>\n";
709 print "</refnamediv>\n";
710
711 print "<refsynopsisdiv>\n";
712 print " <title>Synopsis</title>\n";
713 print " <programlisting>\n";
714 print $args{'type'}." ".$args{'struct'}." {\n";
715 foreach $parameter (@{$args{'parameterlist'}}) {
716 if ($parameter =~ /^#/) {
717 print "$parameter\n";
718 next;
719 }
720
721 my $parameter_name = $parameter;
722 $parameter_name =~ s/\[.*//;
723
724 defined($args{'parameterdescs'}{$parameter_name}) || next;
Randy Dunlap3c308792007-05-08 00:24:39 -0700725 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 $type = $args{'parametertypes'}{$parameter};
727 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
728 # pointer-to-function
729 print " $1 $parameter) ($2);\n";
730 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
Randy Dunlap51f5a0c2007-07-19 01:48:24 -0700731 # bitfield
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 print " $1 $parameter$2;\n";
733 } else {
734 print " ".$type." ".$parameter.";\n";
735 }
736 }
737 print "};";
738 print " </programlisting>\n";
739 print "</refsynopsisdiv>\n";
740
741 print " <refsect1>\n";
742 print " <title>Members</title>\n";
743
744 print " <variablelist>\n";
745 foreach $parameter (@{$args{'parameterlist'}}) {
746 ($parameter =~ /^#/) && next;
747
748 my $parameter_name = $parameter;
749 $parameter_name =~ s/\[.*//;
750
751 defined($args{'parameterdescs'}{$parameter_name}) || next;
752 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
753 print " <varlistentry>";
754 print " <term>$parameter</term>\n";
755 print " <listitem><para>\n";
756 output_highlight($args{'parameterdescs'}{$parameter_name});
757 print " </para></listitem>\n";
758 print " </varlistentry>\n";
759 }
760 print " </variablelist>\n";
761 print " </refsect1>\n";
762
763 output_section_xml(@_);
764
765 print "</refentry>\n\n";
766}
767
768# output enum in XML DocBook
769sub output_enum_xml(%) {
770 my %args = %{$_[0]};
771 my ($parameter, $section);
772 my $count;
773 my $id;
774
775 $id = "API-enum-".$args{'enum'};
776 $id =~ s/[^A-Za-z0-9]/-/g;
777
Pavel Pisa5449bc92007-02-10 01:45:37 -0800778 print "<refentry id=\"$id\">\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700779 print "<refentryinfo>\n";
780 print " <title>LINUX</title>\n";
781 print " <productname>Kernel Hackers Manual</productname>\n";
782 print " <date>$man_date</date>\n";
783 print "</refentryinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 print "<refmeta>\n";
Pavel Pisa5449bc92007-02-10 01:45:37 -0800785 print " <refentrytitle><phrase>enum ".$args{'enum'}."</phrase></refentrytitle>\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700786 print " <manvolnum>9</manvolnum>\n";
Borislav Petkov03662992007-05-09 02:33:43 -0700787 print " <refmiscinfo class=\"version\">" . $kernelversion . "</refmiscinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 print "</refmeta>\n";
789 print "<refnamediv>\n";
790 print " <refname>enum ".$args{'enum'}."</refname>\n";
791 print " <refpurpose>\n";
792 print " ";
793 output_highlight ($args{'purpose'});
794 print " </refpurpose>\n";
795 print "</refnamediv>\n";
796
797 print "<refsynopsisdiv>\n";
798 print " <title>Synopsis</title>\n";
799 print " <programlisting>\n";
800 print "enum ".$args{'enum'}." {\n";
801 $count = 0;
802 foreach $parameter (@{$args{'parameterlist'}}) {
Randy Dunlap3c308792007-05-08 00:24:39 -0700803 print " $parameter";
804 if ($count != $#{$args{'parameterlist'}}) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 $count++;
806 print ",";
Randy Dunlap3c308792007-05-08 00:24:39 -0700807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 print "\n";
809 }
810 print "};";
811 print " </programlisting>\n";
812 print "</refsynopsisdiv>\n";
813
814 print "<refsect1>\n";
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800815 print " <title>Constants</title>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 print " <variablelist>\n";
817 foreach $parameter (@{$args{'parameterlist'}}) {
818 my $parameter_name = $parameter;
819 $parameter_name =~ s/\[.*//;
820
821 print " <varlistentry>";
822 print " <term>$parameter</term>\n";
823 print " <listitem><para>\n";
824 output_highlight($args{'parameterdescs'}{$parameter_name});
825 print " </para></listitem>\n";
826 print " </varlistentry>\n";
827 }
828 print " </variablelist>\n";
829 print "</refsect1>\n";
830
831 output_section_xml(@_);
832
833 print "</refentry>\n\n";
834}
835
836# output typedef in XML DocBook
837sub output_typedef_xml(%) {
838 my %args = %{$_[0]};
839 my ($parameter, $section);
840 my $id;
841
842 $id = "API-typedef-".$args{'typedef'};
843 $id =~ s/[^A-Za-z0-9]/-/g;
844
Pavel Pisa5449bc92007-02-10 01:45:37 -0800845 print "<refentry id=\"$id\">\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700846 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";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 print "<refmeta>\n";
Pavel Pisa5449bc92007-02-10 01:45:37 -0800852 print " <refentrytitle><phrase>typedef ".$args{'typedef'}."</phrase></refentrytitle>\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700853 print " <manvolnum>9</manvolnum>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 print "</refmeta>\n";
855 print "<refnamediv>\n";
856 print " <refname>typedef ".$args{'typedef'}."</refname>\n";
857 print " <refpurpose>\n";
858 print " ";
859 output_highlight ($args{'purpose'});
860 print " </refpurpose>\n";
861 print "</refnamediv>\n";
862
863 print "<refsynopsisdiv>\n";
864 print " <title>Synopsis</title>\n";
865 print " <synopsis>typedef ".$args{'typedef'}.";</synopsis>\n";
866 print "</refsynopsisdiv>\n";
867
868 output_section_xml(@_);
869
870 print "</refentry>\n\n";
871}
872
873# output in XML DocBook
874sub output_intro_xml(%) {
875 my %args = %{$_[0]};
876 my ($parameter, $section);
877 my $count;
878
879 my $id = $args{'module'};
880 $id =~ s/[^A-Za-z0-9]/-/g;
881
882 # print out each section
883 $lineprefix=" ";
884 foreach $section (@{$args{'sectionlist'}}) {
885 print "<refsect1>\n <title>$section</title>\n <para>\n";
886 if ($section =~ m/EXAMPLE/i) {
887 print "<example><para>\n";
888 }
889 output_highlight($args{'sections'}{$section});
890 if ($section =~ m/EXAMPLE/i) {
891 print "</para></example>\n";
892 }
893 print " </para>\n</refsect1>\n";
894 }
895
896 print "\n\n";
897}
898
899# output in XML DocBook
900sub output_function_gnome {
901 my %args = %{$_[0]};
902 my ($parameter, $section);
903 my $count;
904 my $id;
905
906 $id = $args{'module'}."-".$args{'function'};
907 $id =~ s/[^A-Za-z0-9]/-/g;
908
909 print "<sect2>\n";
910 print " <title id=\"$id\">".$args{'function'}."</title>\n";
911
912 print " <funcsynopsis>\n";
913 print " <funcdef>".$args{'functiontype'}." ";
914 print "<function>".$args{'function'}." ";
915 print "</function></funcdef>\n";
916
917 $count = 0;
918 if ($#{$args{'parameterlist'}} >= 0) {
919 foreach $parameter (@{$args{'parameterlist'}}) {
920 $type = $args{'parametertypes'}{$parameter};
921 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
922 # pointer-to-function
923 print " <paramdef>$1 <parameter>$parameter</parameter>)\n";
924 print " <funcparams>$2</funcparams></paramdef>\n";
925 } else {
926 print " <paramdef>".$type;
927 print " <parameter>$parameter</parameter></paramdef>\n";
928 }
929 }
930 } else {
931 print " <void>\n";
932 }
933 print " </funcsynopsis>\n";
934 if ($#{$args{'parameterlist'}} >= 0) {
935 print " <informaltable pgwide=\"1\" frame=\"none\" role=\"params\">\n";
936 print "<tgroup cols=\"2\">\n";
937 print "<colspec colwidth=\"2*\">\n";
938 print "<colspec colwidth=\"8*\">\n";
939 print "<tbody>\n";
940 foreach $parameter (@{$args{'parameterlist'}}) {
941 my $parameter_name = $parameter;
942 $parameter_name =~ s/\[.*//;
943
944 print " <row><entry align=\"right\"><parameter>$parameter</parameter></entry>\n";
945 print " <entry>\n";
946 $lineprefix=" ";
947 output_highlight($args{'parameterdescs'}{$parameter_name});
948 print " </entry></row>\n";
949 }
950 print " </tbody></tgroup></informaltable>\n";
951 } else {
952 print " <para>\n None\n </para>\n";
953 }
954
955 # print out each section
956 $lineprefix=" ";
957 foreach $section (@{$args{'sectionlist'}}) {
958 print "<simplesect>\n <title>$section</title>\n";
959 if ($section =~ m/EXAMPLE/i) {
960 print "<example><programlisting>\n";
961 } else {
962 }
963 print "<para>\n";
964 output_highlight($args{'sections'}{$section});
965 print "</para>\n";
966 if ($section =~ m/EXAMPLE/i) {
967 print "</programlisting></example>\n";
968 } else {
969 }
970 print " </simplesect>\n";
971 }
972
973 print "</sect2>\n\n";
974}
975
976##
977# output function in man
978sub output_function_man(%) {
979 my %args = %{$_[0]};
980 my ($parameter, $section);
981 my $count;
982
983 print ".TH \"$args{'function'}\" 9 \"$args{'function'}\" \"$man_date\" \"Kernel Hacker's Manual\" LINUX\n";
984
985 print ".SH NAME\n";
986 print $args{'function'}." \\- ".$args{'purpose'}."\n";
987
988 print ".SH SYNOPSIS\n";
Randy Dunlapa21217d2007-02-10 01:46:04 -0800989 if ($args{'functiontype'} ne "") {
990 print ".B \"".$args{'functiontype'}."\" ".$args{'function'}."\n";
991 } else {
992 print ".B \"".$args{'function'}."\n";
993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 $count = 0;
995 my $parenth = "(";
996 my $post = ",";
997 foreach my $parameter (@{$args{'parameterlist'}}) {
998 if ($count == $#{$args{'parameterlist'}}) {
999 $post = ");";
1000 }
1001 $type = $args{'parametertypes'}{$parameter};
1002 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1003 # pointer-to-function
1004 print ".BI \"".$parenth.$1."\" ".$parameter." \") (".$2.")".$post."\"\n";
1005 } else {
1006 $type =~ s/([^\*])$/$1 /;
1007 print ".BI \"".$parenth.$type."\" ".$parameter." \"".$post."\"\n";
1008 }
1009 $count++;
1010 $parenth = "";
1011 }
1012
1013 print ".SH ARGUMENTS\n";
1014 foreach $parameter (@{$args{'parameterlist'}}) {
1015 my $parameter_name = $parameter;
1016 $parameter_name =~ s/\[.*//;
1017
1018 print ".IP \"".$parameter."\" 12\n";
1019 output_highlight($args{'parameterdescs'}{$parameter_name});
1020 }
1021 foreach $section (@{$args{'sectionlist'}}) {
1022 print ".SH \"", uc $section, "\"\n";
1023 output_highlight($args{'sections'}{$section});
1024 }
1025}
1026
1027##
1028# output enum in man
1029sub output_enum_man(%) {
1030 my %args = %{$_[0]};
1031 my ($parameter, $section);
1032 my $count;
1033
1034 print ".TH \"$args{'module'}\" 9 \"enum $args{'enum'}\" \"$man_date\" \"API Manual\" LINUX\n";
1035
1036 print ".SH NAME\n";
1037 print "enum ".$args{'enum'}." \\- ".$args{'purpose'}."\n";
1038
1039 print ".SH SYNOPSIS\n";
1040 print "enum ".$args{'enum'}." {\n";
1041 $count = 0;
1042 foreach my $parameter (@{$args{'parameterlist'}}) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001043 print ".br\n.BI \" $parameter\"\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 if ($count == $#{$args{'parameterlist'}}) {
1045 print "\n};\n";
1046 last;
1047 }
1048 else {
1049 print ", \n.br\n";
1050 }
1051 $count++;
1052 }
1053
1054 print ".SH Constants\n";
1055 foreach $parameter (@{$args{'parameterlist'}}) {
1056 my $parameter_name = $parameter;
1057 $parameter_name =~ s/\[.*//;
1058
1059 print ".IP \"".$parameter."\" 12\n";
1060 output_highlight($args{'parameterdescs'}{$parameter_name});
1061 }
1062 foreach $section (@{$args{'sectionlist'}}) {
1063 print ".SH \"$section\"\n";
1064 output_highlight($args{'sections'}{$section});
1065 }
1066}
1067
1068##
1069# output struct in man
1070sub output_struct_man(%) {
1071 my %args = %{$_[0]};
1072 my ($parameter, $section);
1073
1074 print ".TH \"$args{'module'}\" 9 \"".$args{'type'}." ".$args{'struct'}."\" \"$man_date\" \"API Manual\" LINUX\n";
1075
1076 print ".SH NAME\n";
1077 print $args{'type'}." ".$args{'struct'}." \\- ".$args{'purpose'}."\n";
1078
1079 print ".SH SYNOPSIS\n";
1080 print $args{'type'}." ".$args{'struct'}." {\n.br\n";
1081
1082 foreach my $parameter (@{$args{'parameterlist'}}) {
1083 if ($parameter =~ /^#/) {
1084 print ".BI \"$parameter\"\n.br\n";
1085 next;
1086 }
1087 my $parameter_name = $parameter;
1088 $parameter_name =~ s/\[.*//;
1089
Randy Dunlap3c308792007-05-08 00:24:39 -07001090 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 $type = $args{'parametertypes'}{$parameter};
1092 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1093 # pointer-to-function
1094 print ".BI \" ".$1."\" ".$parameter." \") (".$2.")"."\"\n;\n";
1095 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
Randy.Dunlap1d7e1d42006-07-01 04:36:34 -07001096 # bitfield
1097 print ".BI \" ".$1."\ \" ".$parameter.$2." \""."\"\n;\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 } else {
1099 $type =~ s/([^\*])$/$1 /;
1100 print ".BI \" ".$type."\" ".$parameter." \""."\"\n;\n";
1101 }
1102 print "\n.br\n";
1103 }
1104 print "};\n.br\n";
1105
Randy Dunlapc51d3da2006-06-25 05:49:14 -07001106 print ".SH Members\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 foreach $parameter (@{$args{'parameterlist'}}) {
1108 ($parameter =~ /^#/) && next;
1109
1110 my $parameter_name = $parameter;
1111 $parameter_name =~ s/\[.*//;
1112
Randy Dunlap3c308792007-05-08 00:24:39 -07001113 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 print ".IP \"".$parameter."\" 12\n";
1115 output_highlight($args{'parameterdescs'}{$parameter_name});
1116 }
1117 foreach $section (@{$args{'sectionlist'}}) {
1118 print ".SH \"$section\"\n";
1119 output_highlight($args{'sections'}{$section});
1120 }
1121}
1122
1123##
1124# output typedef in man
1125sub output_typedef_man(%) {
1126 my %args = %{$_[0]};
1127 my ($parameter, $section);
1128
1129 print ".TH \"$args{'module'}\" 9 \"$args{'typedef'}\" \"$man_date\" \"API Manual\" LINUX\n";
1130
1131 print ".SH NAME\n";
1132 print "typedef ".$args{'typedef'}." \\- ".$args{'purpose'}."\n";
1133
1134 foreach $section (@{$args{'sectionlist'}}) {
1135 print ".SH \"$section\"\n";
1136 output_highlight($args{'sections'}{$section});
1137 }
1138}
1139
1140sub output_intro_man(%) {
1141 my %args = %{$_[0]};
1142 my ($parameter, $section);
1143 my $count;
1144
1145 print ".TH \"$args{'module'}\" 9 \"$args{'module'}\" \"$man_date\" \"API Manual\" LINUX\n";
1146
1147 foreach $section (@{$args{'sectionlist'}}) {
1148 print ".SH \"$section\"\n";
1149 output_highlight($args{'sections'}{$section});
1150 }
1151}
1152
1153##
1154# output in text
1155sub output_function_text(%) {
1156 my %args = %{$_[0]};
1157 my ($parameter, $section);
Randy Dunlapa21217d2007-02-10 01:46:04 -08001158 my $start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Randy Dunlapf47634b2006-07-01 04:36:36 -07001160 print "Name:\n\n";
1161 print $args{'function'}." - ".$args{'purpose'}."\n";
1162
1163 print "\nSynopsis:\n\n";
Randy Dunlapa21217d2007-02-10 01:46:04 -08001164 if ($args{'functiontype'} ne "") {
1165 $start = $args{'functiontype'}." ".$args{'function'}." (";
1166 } else {
1167 $start = $args{'function'}." (";
1168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 print $start;
Randy Dunlapa21217d2007-02-10 01:46:04 -08001170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 my $count = 0;
1172 foreach my $parameter (@{$args{'parameterlist'}}) {
1173 $type = $args{'parametertypes'}{$parameter};
1174 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1175 # pointer-to-function
1176 print $1.$parameter.") (".$2;
1177 } else {
1178 print $type." ".$parameter;
1179 }
1180 if ($count != $#{$args{'parameterlist'}}) {
1181 $count++;
1182 print ",\n";
1183 print " " x length($start);
1184 } else {
1185 print ");\n\n";
1186 }
1187 }
1188
1189 print "Arguments:\n\n";
1190 foreach $parameter (@{$args{'parameterlist'}}) {
1191 my $parameter_name = $parameter;
1192 $parameter_name =~ s/\[.*//;
1193
1194 print $parameter."\n\t".$args{'parameterdescs'}{$parameter_name}."\n";
1195 }
1196 output_section_text(@_);
1197}
1198
1199#output sections in text
1200sub output_section_text(%) {
1201 my %args = %{$_[0]};
1202 my $section;
1203
1204 print "\n";
1205 foreach $section (@{$args{'sectionlist'}}) {
1206 print "$section:\n\n";
1207 output_highlight($args{'sections'}{$section});
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 print "\n\n";
1210}
1211
1212# output enum in text
1213sub output_enum_text(%) {
1214 my %args = %{$_[0]};
1215 my ($parameter);
1216 my $count;
1217 print "Enum:\n\n";
1218
Randy.Dunlap1d7e1d42006-07-01 04:36:34 -07001219 print "enum ".$args{'enum'}." - ".$args{'purpose'}."\n\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 print "enum ".$args{'enum'}." {\n";
1221 $count = 0;
1222 foreach $parameter (@{$args{'parameterlist'}}) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001223 print "\t$parameter";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 if ($count != $#{$args{'parameterlist'}}) {
1225 $count++;
1226 print ",";
1227 }
1228 print "\n";
1229 }
1230 print "};\n\n";
1231
1232 print "Constants:\n\n";
1233 foreach $parameter (@{$args{'parameterlist'}}) {
1234 print "$parameter\n\t";
1235 print $args{'parameterdescs'}{$parameter}."\n";
1236 }
1237
1238 output_section_text(@_);
1239}
1240
1241# output typedef in text
1242sub output_typedef_text(%) {
1243 my %args = %{$_[0]};
1244 my ($parameter);
1245 my $count;
1246 print "Typedef:\n\n";
1247
Randy.Dunlap1d7e1d42006-07-01 04:36:34 -07001248 print "typedef ".$args{'typedef'}." - ".$args{'purpose'}."\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 output_section_text(@_);
1250}
1251
1252# output struct as text
1253sub output_struct_text(%) {
1254 my %args = %{$_[0]};
1255 my ($parameter);
1256
Randy.Dunlap1d7e1d42006-07-01 04:36:34 -07001257 print $args{'type'}." ".$args{'struct'}." - ".$args{'purpose'}."\n\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 print $args{'type'}." ".$args{'struct'}." {\n";
1259 foreach $parameter (@{$args{'parameterlist'}}) {
1260 if ($parameter =~ /^#/) {
1261 print "$parameter\n";
1262 next;
1263 }
1264
1265 my $parameter_name = $parameter;
1266 $parameter_name =~ s/\[.*//;
1267
Randy Dunlap3c308792007-05-08 00:24:39 -07001268 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 $type = $args{'parametertypes'}{$parameter};
1270 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1271 # pointer-to-function
1272 print "\t$1 $parameter) ($2);\n";
1273 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
Randy Dunlap51f5a0c2007-07-19 01:48:24 -07001274 # bitfield
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 print "\t$1 $parameter$2;\n";
1276 } else {
1277 print "\t".$type." ".$parameter.";\n";
1278 }
1279 }
1280 print "};\n\n";
1281
1282 print "Members:\n\n";
1283 foreach $parameter (@{$args{'parameterlist'}}) {
1284 ($parameter =~ /^#/) && next;
1285
1286 my $parameter_name = $parameter;
1287 $parameter_name =~ s/\[.*//;
1288
Randy Dunlap3c308792007-05-08 00:24:39 -07001289 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 print "$parameter\n\t";
1291 print $args{'parameterdescs'}{$parameter_name}."\n";
1292 }
1293 print "\n";
1294 output_section_text(@_);
1295}
1296
1297sub output_intro_text(%) {
1298 my %args = %{$_[0]};
1299 my ($parameter, $section);
1300
1301 foreach $section (@{$args{'sectionlist'}}) {
1302 print " $section:\n";
1303 print " -> ";
1304 output_highlight($args{'sections'}{$section});
1305 }
1306}
1307
1308##
Randy Dunlap27205742006-10-11 01:22:12 -07001309# generic output function for all types (function, struct/union, typedef, enum);
1310# calls the generated, variable output_ function name based on
1311# functype and output_mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312sub output_declaration {
1313 no strict 'refs';
1314 my $name = shift;
1315 my $functype = shift;
1316 my $func = "output_${functype}_$output_mode";
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001317 if (($function_only==0) ||
1318 ( $function_only == 1 && defined($function_table{$name})) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 ( $function_only == 2 && !defined($function_table{$name})))
1320 {
Randy Dunlap3c308792007-05-08 00:24:39 -07001321 &$func(@_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 $section_counter++;
1323 }
1324}
1325
1326##
Randy Dunlap27205742006-10-11 01:22:12 -07001327# generic output function - calls the right one based on current output mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328sub output_intro {
1329 no strict 'refs';
1330 my $func = "output_intro_".$output_mode;
1331 &$func(@_);
1332 $section_counter++;
1333}
1334
1335##
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001336# takes a declaration (struct, union, enum, typedef) and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337# invokes the right handler. NOT called for functions.
1338sub dump_declaration($$) {
1339 no strict 'refs';
1340 my ($prototype, $file) = @_;
1341 my $func = "dump_".$decl_type;
1342 &$func(@_);
1343}
1344
1345sub dump_union($$) {
1346 dump_struct(@_);
1347}
1348
1349sub dump_struct($$) {
1350 my $x = shift;
1351 my $file = shift;
1352
1353 if ($x =~/(struct|union)\s+(\w+)\s*{(.*)}/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001354 $declaration_name = $2;
1355 my $members = $3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357 # ignore embedded structs or unions
1358 $members =~ s/{.*?}//g;
1359
Martin Waitzaeec46b2005-11-13 16:08:13 -08001360 # ignore members marked private:
1361 $members =~ s/\/\*.*?private:.*?public:.*?\*\///gos;
1362 $members =~ s/\/\*.*?private:.*//gos;
1363 # strip comments:
1364 $members =~ s/\/\*.*?\*\///gos;
1365
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 create_parameterlist($members, ';', $file);
1367
1368 output_declaration($declaration_name,
1369 'struct',
1370 {'struct' => $declaration_name,
1371 'module' => $modulename,
1372 'parameterlist' => \@parameterlist,
1373 'parameterdescs' => \%parameterdescs,
1374 'parametertypes' => \%parametertypes,
1375 'sectionlist' => \@sectionlist,
1376 'sections' => \%sections,
1377 'purpose' => $declaration_purpose,
1378 'type' => $decl_type
1379 });
1380 }
1381 else {
Randy Dunlap3c308792007-05-08 00:24:39 -07001382 print STDERR "Error(${file}:$.): Cannot parse struct or union!\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 ++$errors;
1384 }
1385}
1386
1387sub dump_enum($$) {
1388 my $x = shift;
1389 my $file = shift;
1390
Martin Waitzaeec46b2005-11-13 16:08:13 -08001391 $x =~ s@/\*.*?\*/@@gos; # strip comments.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 if ($x =~ /enum\s+(\w+)\s*{(.*)}/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001393 $declaration_name = $1;
1394 my $members = $2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396 foreach my $arg (split ',', $members) {
1397 $arg =~ s/^\s*(\w+).*/$1/;
1398 push @parameterlist, $arg;
1399 if (!$parameterdescs{$arg}) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001400 $parameterdescs{$arg} = $undescribed;
1401 print STDERR "Warning(${file}:$.): Enum value '$arg' ".
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 "not described in enum '$declaration_name'\n";
1403 }
1404
1405 }
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 output_declaration($declaration_name,
1408 'enum',
1409 {'enum' => $declaration_name,
1410 'module' => $modulename,
1411 'parameterlist' => \@parameterlist,
1412 'parameterdescs' => \%parameterdescs,
1413 'sectionlist' => \@sectionlist,
1414 'sections' => \%sections,
1415 'purpose' => $declaration_purpose
1416 });
1417 }
1418 else {
Randy Dunlap3c308792007-05-08 00:24:39 -07001419 print STDERR "Error(${file}:$.): Cannot parse enum!\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 ++$errors;
1421 }
1422}
1423
1424sub dump_typedef($$) {
1425 my $x = shift;
1426 my $file = shift;
1427
Martin Waitzaeec46b2005-11-13 16:08:13 -08001428 $x =~ s@/\*.*?\*/@@gos; # strip comments.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 while (($x =~ /\(*.\)\s*;$/) || ($x =~ /\[*.\]\s*;$/)) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001430 $x =~ s/\(*.\)\s*;$/;/;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 $x =~ s/\[*.\]\s*;$/;/;
1432 }
1433
1434 if ($x =~ /typedef.*\s+(\w+)\s*;/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001435 $declaration_name = $1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
1437 output_declaration($declaration_name,
1438 'typedef',
1439 {'typedef' => $declaration_name,
1440 'module' => $modulename,
1441 'sectionlist' => \@sectionlist,
1442 'sections' => \%sections,
1443 'purpose' => $declaration_purpose
1444 });
1445 }
1446 else {
Randy Dunlap3c308792007-05-08 00:24:39 -07001447 print STDERR "Error(${file}:$.): Cannot parse typedef!\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 ++$errors;
1449 }
1450}
1451
1452sub create_parameterlist($$$) {
1453 my $args = shift;
1454 my $splitter = shift;
1455 my $file = shift;
1456 my $type;
1457 my $param;
1458
Martin Waitza6d3fe72006-01-09 20:53:55 -08001459 # temporarily replace commas inside function pointer definition
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 while ($args =~ /(\([^\),]+),/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001461 $args =~ s/(\([^\),]+),/$1#/g;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 }
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 foreach my $arg (split($splitter, $args)) {
1465 # strip comments
1466 $arg =~ s/\/\*.*\*\///;
Randy Dunlap3c308792007-05-08 00:24:39 -07001467 # strip leading/trailing spaces
1468 $arg =~ s/^\s*//;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 $arg =~ s/\s*$//;
1470 $arg =~ s/\s+/ /;
1471
1472 if ($arg =~ /^#/) {
1473 # Treat preprocessor directive as a typeless variable just to fill
1474 # corresponding data structures "correctly". Catch it later in
1475 # output_* subs.
1476 push_parameter($arg, "", $file);
Randy Dunlapea82c742006-12-06 20:38:52 -08001477 } elsif ($arg =~ m/\(.*\*/) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 # pointer-to-function
1479 $arg =~ tr/#/,/;
Randy Dunlap996a07b2007-02-10 01:45:56 -08001480 $arg =~ m/[^\(]+\(\*\s*([^\)]+)\)/;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 $param = $1;
1482 $type = $arg;
1483 $type =~ s/([^\(]+\(\*)$param/$1/;
1484 push_parameter($param, $type, $file);
Martin Waitzaeec46b2005-11-13 16:08:13 -08001485 } elsif ($arg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 $arg =~ s/\s*:\s*/:/g;
1487 $arg =~ s/\s*\[/\[/g;
1488
1489 my @args = split('\s*,\s*', $arg);
1490 if ($args[0] =~ m/\*/) {
1491 $args[0] =~ s/(\*+)\s*/ $1/;
1492 }
Borislav Petkov884f2812007-05-08 00:29:05 -07001493
1494 my @first_arg;
1495 if ($args[0] =~ /^(.*\s+)(.*?\[.*\].*)$/) {
1496 shift @args;
1497 push(@first_arg, split('\s+', $1));
1498 push(@first_arg, $2);
1499 } else {
1500 @first_arg = split('\s+', shift @args);
1501 }
1502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 unshift(@args, pop @first_arg);
1504 $type = join " ", @first_arg;
1505
1506 foreach $param (@args) {
1507 if ($param =~ m/^(\*+)\s*(.*)/) {
1508 push_parameter($2, "$type $1", $file);
1509 }
1510 elsif ($param =~ m/(.*?):(\d+)/) {
1511 push_parameter($1, "$type:$2", $file)
1512 }
1513 else {
1514 push_parameter($param, $type, $file);
1515 }
1516 }
1517 }
1518 }
1519}
1520
1521sub push_parameter($$$) {
1522 my $param = shift;
1523 my $type = shift;
1524 my $file = shift;
1525
Randy Dunlap5f8c7c92007-07-19 01:48:24 -07001526 if (($anon_struct_union == 1) && ($type eq "") &&
1527 ($param eq "}")) {
1528 return; # ignore the ending }; from anon. struct/union
1529 }
1530
1531 $anon_struct_union = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 my $param_name = $param;
1533 $param_name =~ s/\[.*//;
1534
Martin Waitza6d3fe72006-01-09 20:53:55 -08001535 if ($type eq "" && $param =~ /\.\.\.$/)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 {
1537 $type="";
Martin Waitza6d3fe72006-01-09 20:53:55 -08001538 $parameterdescs{$param} = "variable arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 }
1540 elsif ($type eq "" && ($param eq "" or $param eq "void"))
1541 {
1542 $type="";
1543 $param="void";
1544 $parameterdescs{void} = "no arguments";
1545 }
Randy Dunlap134fe012006-12-22 01:10:50 -08001546 elsif ($type eq "" && ($param eq "struct" or $param eq "union"))
1547 # handle unnamed (anonymous) union or struct:
1548 {
1549 $type = $param;
Randy Dunlap5f8c7c92007-07-19 01:48:24 -07001550 $param = "{unnamed_" . $param . "}";
Randy Dunlap134fe012006-12-22 01:10:50 -08001551 $parameterdescs{$param} = "anonymous\n";
Randy Dunlap5f8c7c92007-07-19 01:48:24 -07001552 $anon_struct_union = 1;
Randy Dunlap134fe012006-12-22 01:10:50 -08001553 }
1554
Martin Waitza6d3fe72006-01-09 20:53:55 -08001555 # warn if parameter has no description
Randy Dunlap134fe012006-12-22 01:10:50 -08001556 # (but ignore ones starting with # as these are not parameters
1557 # but inline preprocessor statements);
1558 # also ignore unnamed structs/unions;
Randy Dunlap5f8c7c92007-07-19 01:48:24 -07001559 if (!$anon_struct_union) {
Martin Waitza6d3fe72006-01-09 20:53:55 -08001560 if (!defined $parameterdescs{$param_name} && $param_name !~ /^#/) {
1561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 $parameterdescs{$param_name} = $undescribed;
1563
1564 if (($type eq 'function') || ($type eq 'enum')) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001565 print STDERR "Warning(${file}:$.): Function parameter ".
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 "or member '$param' not " .
1567 "described in '$declaration_name'\n";
1568 }
1569 print STDERR "Warning(${file}:$.):".
Randy Dunlap3c308792007-05-08 00:24:39 -07001570 " No description found for parameter '$param'\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 ++$warnings;
Randy Dunlap3c308792007-05-08 00:24:39 -07001572 }
1573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
1575 push @parameterlist, $param;
1576 $parametertypes{$param} = $type;
1577}
1578
1579##
1580# takes a function prototype and the name of the current file being
1581# processed and spits out all the details stored in the global
1582# arrays/hashes.
1583sub dump_function($$) {
1584 my $prototype = shift;
1585 my $file = shift;
1586
1587 $prototype =~ s/^static +//;
1588 $prototype =~ s/^extern +//;
Pavel Pisa4dc3b162005-05-01 08:59:25 -07001589 $prototype =~ s/^fastcall +//;
1590 $prototype =~ s/^asmlinkage +//;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 $prototype =~ s/^inline +//;
1592 $prototype =~ s/^__inline__ +//;
Randy Dunlap32e79402006-10-11 01:22:10 -07001593 $prototype =~ s/^__inline +//;
1594 $prototype =~ s/^__always_inline +//;
1595 $prototype =~ s/^noinline +//;
Randy Dunlap0129a052006-07-30 03:03:41 -07001596 $prototype =~ s/__devinit +//;
Randy Dunlap996a07b2007-02-10 01:45:56 -08001597 $prototype =~ s/^#define\s+//; #ak added
Randy Dunlap328d2442007-02-28 20:12:10 -08001598 $prototype =~ s/__attribute__\s*\(\([a-z,]*\)\)//;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
1600 # Yes, this truly is vile. We are looking for:
1601 # 1. Return type (may be nothing if we're looking at a macro)
1602 # 2. Function name
1603 # 3. Function parameters.
1604 #
1605 # All the while we have to watch out for function pointer parameters
1606 # (which IIRC is what the two sections are for), C types (these
1607 # regexps don't even start to express all the possibilities), and
1608 # so on.
1609 #
1610 # If you mess with these regexps, it's a good idea to check that
1611 # the following functions' documentation still comes out right:
1612 # - parport_register_device (function pointer parameters)
1613 # - atomic_set (macro)
Martin Waitz9598f912006-02-01 03:06:55 -08001614 # - pci_match_device, __copy_to_user (long return type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
1616 if ($prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1617 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1618 $prototype =~ m/^(\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1619 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1620 $prototype =~ m/^(\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1621 $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1622 $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1623 $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1624 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1625 $prototype =~ m/^(\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1626 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1627 $prototype =~ m/^(\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1628 $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
Martin Waitz9598f912006-02-01 03:06:55 -08001629 $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1630 $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
Randy Dunlap412ecd772007-02-10 22:47:33 -08001631 $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1632 $prototype =~ m/^(\w+\s+\w+\s*\*\s*\w+\s*\*\s*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 $return_type = $1;
1634 $declaration_name = $2;
1635 my $args = $3;
1636
1637 create_parameterlist($args, ',', $file);
1638 } else {
1639 print STDERR "Error(${file}:$.): cannot understand prototype: '$prototype'\n";
1640 ++$errors;
1641 return;
1642 }
1643
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001644 output_declaration($declaration_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 'function',
1646 {'function' => $declaration_name,
1647 'module' => $modulename,
1648 'functiontype' => $return_type,
1649 'parameterlist' => \@parameterlist,
1650 'parameterdescs' => \%parameterdescs,
1651 'parametertypes' => \%parametertypes,
1652 'sectionlist' => \@sectionlist,
1653 'sections' => \%sections,
1654 'purpose' => $declaration_purpose
1655 });
1656}
1657
1658sub process_file($);
1659
1660# Read the file that maps relative names to absolute names for
1661# separate source and object directories and for shadow trees.
1662if (open(SOURCE_MAP, "<.tmp_filelist.txt")) {
1663 my ($relname, $absname);
1664 while(<SOURCE_MAP>) {
1665 chop();
1666 ($relname, $absname) = (split())[0..1];
1667 $relname =~ s:^/+::;
1668 $source_map{$relname} = $absname;
1669 }
1670 close(SOURCE_MAP);
1671}
1672
1673if ($filelist) {
1674 open(FLIST,"<$filelist") or die "Can't open file list $filelist";
1675 while(<FLIST>) {
1676 chop;
1677 process_file($_);
1678 }
1679}
1680
1681foreach (@ARGV) {
1682 chomp;
1683 process_file($_);
1684}
1685if ($verbose && $errors) {
1686 print STDERR "$errors errors\n";
1687}
1688if ($verbose && $warnings) {
1689 print STDERR "$warnings warnings\n";
1690}
1691
1692exit($errors);
1693
1694sub reset_state {
1695 $function = "";
1696 %constants = ();
1697 %parameterdescs = ();
1698 %parametertypes = ();
1699 @parameterlist = ();
1700 %sections = ();
1701 @sectionlist = ();
1702 $prototype = "";
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001703
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 $state = 0;
1705}
1706
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001707sub process_state3_function($$) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 my $x = shift;
1709 my $file = shift;
1710
Randy Dunlap51f5a0c2007-07-19 01:48:24 -07001711 $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
1712
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 if ($x =~ m#\s*/\*\s+MACDOC\s*#io || ($x =~ /^#/ && $x !~ /^#define/)) {
1714 # do nothing
1715 }
1716 elsif ($x =~ /([^\{]*)/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001717 $prototype .= $1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 }
1719 if (($x =~ /\{/) || ($x =~ /\#define/) || ($x =~ /;/)) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001720 $prototype =~ s@/\*.*?\*/@@gos; # strip comments.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 $prototype =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1722 $prototype =~ s@^\s+@@gos; # strip leading spaces
1723 dump_function($prototype,$file);
1724 reset_state();
1725 }
1726}
1727
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001728sub process_state3_type($$) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 my $x = shift;
1730 my $file = shift;
1731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 $x =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1733 $x =~ s@^\s+@@gos; # strip leading spaces
1734 $x =~ s@\s+$@@gos; # strip trailing spaces
Randy Dunlap51f5a0c2007-07-19 01:48:24 -07001735 $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
1736
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 if ($x =~ /^#/) {
1738 # To distinguish preprocessor directive from regular declaration later.
1739 $x .= ";";
1740 }
1741
1742 while (1) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001743 if ( $x =~ /([^{};]*)([{};])(.*)/ ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 $prototype .= $1 . $2;
1745 ($2 eq '{') && $brcount++;
1746 ($2 eq '}') && $brcount--;
1747 if (($2 eq ';') && ($brcount == 0)) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001748 dump_declaration($prototype,$file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 reset_state();
Randy Dunlap3c308792007-05-08 00:24:39 -07001750 last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 }
1752 $x = $3;
Randy Dunlap3c308792007-05-08 00:24:39 -07001753 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 $prototype .= $x;
1755 last;
1756 }
1757 }
1758}
1759
Randy Dunlap6b5b55f2007-10-16 23:31:20 -07001760# xml_escape: replace <, >, and & in the text stream;
1761#
1762# however, formatting controls that are generated internally/locally in the
1763# kernel-doc script are not escaped here; instead, they begin life like
1764# $blankline_html (4 of '\' followed by a mnemonic + ':'), then these strings
1765# are converted to their mnemonic-expected output, without the 4 * '\' & ':',
1766# just before actual output; (this is done by local_unescape())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767sub xml_escape($) {
1768 my $text = shift;
Randy Dunlapecfb2512006-06-25 05:49:13 -07001769 if (($output_mode eq "text") || ($output_mode eq "man")) {
1770 return $text;
1771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 $text =~ s/\&/\\\\\\amp;/g;
1773 $text =~ s/\</\\\\\\lt;/g;
1774 $text =~ s/\>/\\\\\\gt;/g;
1775 return $text;
1776}
1777
Randy Dunlap6b5b55f2007-10-16 23:31:20 -07001778# convert local escape strings to html
1779# local escape strings look like: '\\\\menmonic:' (that's 4 backslashes)
1780sub local_unescape($) {
1781 my $text = shift;
1782 if (($output_mode eq "text") || ($output_mode eq "man")) {
1783 return $text;
1784 }
1785 $text =~ s/\\\\\\\\lt:/</g;
1786 $text =~ s/\\\\\\\\gt:/>/g;
1787 return $text;
1788}
1789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790sub process_file($) {
Randy Dunlap2283a112005-07-07 15:39:26 -07001791 my $file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 my $identifier;
1793 my $func;
Randy Dunlapa21217d2007-02-10 01:46:04 -08001794 my $descr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 my $initial_section_counter = $section_counter;
1796
Randy Dunlap2283a112005-07-07 15:39:26 -07001797 if (defined($ENV{'SRCTREE'})) {
1798 $file = "$ENV{'SRCTREE'}" . "/" . "@_";
1799 }
1800 else {
1801 $file = "@_";
1802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 if (defined($source_map{$file})) {
1804 $file = $source_map{$file};
1805 }
1806
1807 if (!open(IN,"<$file")) {
1808 print STDERR "Error: Cannot open file $file\n";
1809 ++$errors;
1810 return;
1811 }
1812
1813 $section_counter = 0;
1814 while (<IN>) {
1815 if ($state == 0) {
1816 if (/$doc_start/o) {
1817 $state = 1; # next line is always the function name
Randy Dunlap850622d2006-06-25 05:48:55 -07001818 $in_doc_sect = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 }
1820 } elsif ($state == 1) { # this line is the function name (always)
1821 if (/$doc_block/o) {
1822 $state = 4;
1823 $contents = "";
1824 if ( $1 eq "" ) {
1825 $section = $section_intro;
1826 } else {
1827 $section = $1;
1828 }
Randy Dunlap3c308792007-05-08 00:24:39 -07001829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 elsif (/$doc_decl/o) {
1831 $identifier = $1;
1832 if (/\s*([\w\s]+?)\s*-/) {
1833 $identifier = $1;
1834 }
1835
1836 $state = 2;
1837 if (/-(.*)/) {
Randy Dunlap51f5a0c2007-07-19 01:48:24 -07001838 # strip leading/trailing/multiple spaces
Randy Dunlapa21217d2007-02-10 01:46:04 -08001839 $descr= $1;
1840 $descr =~ s/^\s*//;
1841 $descr =~ s/\s*$//;
1842 $descr =~ s/\s+/ /;
1843 $declaration_purpose = xml_escape($descr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 } else {
1845 $declaration_purpose = "";
1846 }
1847 if ($identifier =~ m/^struct/) {
1848 $decl_type = 'struct';
1849 } elsif ($identifier =~ m/^union/) {
1850 $decl_type = 'union';
1851 } elsif ($identifier =~ m/^enum/) {
1852 $decl_type = 'enum';
1853 } elsif ($identifier =~ m/^typedef/) {
1854 $decl_type = 'typedef';
1855 } else {
1856 $decl_type = 'function';
1857 }
1858
1859 if ($verbose) {
1860 print STDERR "Info(${file}:$.): Scanning doc for $identifier\n";
1861 }
1862 } else {
1863 print STDERR "Warning(${file}:$.): Cannot understand $_ on line $.",
1864 " - I thought it was a doc line\n";
1865 ++$warnings;
1866 $state = 0;
1867 }
1868 } elsif ($state == 2) { # look for head: lines, and include content
1869 if (/$doc_sect/o) {
1870 $newsection = $1;
1871 $newcontents = $2;
1872
1873 if ($contents ne "") {
Randy Dunlap850622d2006-06-25 05:48:55 -07001874 if (!$in_doc_sect && $verbose) {
1875 print STDERR "Warning(${file}:$.): contents before sections\n";
1876 ++$warnings;
1877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 dump_section($section, xml_escape($contents));
1879 $section = $section_default;
1880 }
1881
Randy Dunlap850622d2006-06-25 05:48:55 -07001882 $in_doc_sect = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 $contents = $newcontents;
1884 if ($contents ne "") {
Randy Dunlap27205742006-10-11 01:22:12 -07001885 while ((substr($contents, 0, 1) eq " ") ||
1886 substr($contents, 0, 1) eq "\t") {
1887 $contents = substr($contents, 1);
Randy Dunlap05189492006-06-25 05:47:47 -07001888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 $contents .= "\n";
1890 }
1891 $section = $newsection;
1892 } elsif (/$doc_end/) {
1893
1894 if ($contents ne "") {
1895 dump_section($section, xml_escape($contents));
1896 $section = $section_default;
1897 $contents = "";
1898 }
1899
1900 $prototype = "";
1901 $state = 3;
1902 $brcount = 0;
Randy Dunlap232acbc2006-06-25 05:47:48 -07001903# print STDERR "end of doc comment, looking for prototype\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 } elsif (/$doc_content/) {
1905 # miguel-style comment kludge, look for blank lines after
1906 # @parameter line to signify start of description
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001907 if ($1 eq "" &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 ($section =~ m/^@/ || $section eq $section_context)) {
1909 dump_section($section, xml_escape($contents));
1910 $section = $section_default;
1911 $contents = "";
1912 } else {
1913 $contents .= $1."\n";
1914 }
1915 } else {
1916 # i dont know - bad line? ignore.
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001917 print STDERR "Warning(${file}:$.): bad line: $_";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 ++$warnings;
1919 }
Randy Dunlap232acbc2006-06-25 05:47:48 -07001920 } elsif ($state == 3) { # scanning for function '{' (end of prototype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 if ($decl_type eq 'function') {
Randy Dunlap3c308792007-05-08 00:24:39 -07001922 process_state3_function($_, $file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 } else {
Randy Dunlap3c308792007-05-08 00:24:39 -07001924 process_state3_type($_, $file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 }
1926 } elsif ($state == 4) {
1927 # Documentation block
Randy Dunlap3c308792007-05-08 00:24:39 -07001928 if (/$doc_block/) {
Randy Dunlap6b5b55f2007-10-16 23:31:20 -07001929 dump_section($section, xml_escape($contents));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 output_intro({'sectionlist' => \@sectionlist,
1931 'sections' => \%sections });
1932 $contents = "";
1933 $function = "";
1934 %constants = ();
1935 %parameterdescs = ();
1936 %parametertypes = ();
1937 @parameterlist = ();
1938 %sections = ();
1939 @sectionlist = ();
1940 $prototype = "";
1941 if ( $1 eq "" ) {
1942 $section = $section_intro;
1943 } else {
1944 $section = $1;
1945 }
Randy Dunlap3c308792007-05-08 00:24:39 -07001946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 elsif (/$doc_end/)
1948 {
Randy Dunlap6b5b55f2007-10-16 23:31:20 -07001949 dump_section($section, xml_escape($contents));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 output_intro({'sectionlist' => \@sectionlist,
1951 'sections' => \%sections });
1952 $contents = "";
1953 $function = "";
1954 %constants = ();
1955 %parameterdescs = ();
1956 %parametertypes = ();
1957 @parameterlist = ();
1958 %sections = ();
1959 @sectionlist = ();
1960 $prototype = "";
1961 $state = 0;
1962 }
1963 elsif (/$doc_content/)
1964 {
1965 if ( $1 eq "" )
1966 {
1967 $contents .= $blankline;
1968 }
1969 else
1970 {
1971 $contents .= $1 . "\n";
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001972 }
Randy Dunlap3c308792007-05-08 00:24:39 -07001973 }
1974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 }
1976 if ($initial_section_counter == $section_counter) {
1977 print STDERR "Warning(${file}): no structured comments found\n";
1978 if ($output_mode eq "xml") {
1979 # The template wants at least one RefEntry here; make one.
1980 print "<refentry>\n";
1981 print " <refnamediv>\n";
1982 print " <refname>\n";
1983 print " ${file}\n";
1984 print " </refname>\n";
1985 print " <refpurpose>\n";
1986 print " Document generation inconsistency\n";
1987 print " </refpurpose>\n";
1988 print " </refnamediv>\n";
1989 print " <refsect1>\n";
1990 print " <title>\n";
1991 print " Oops\n";
1992 print " </title>\n";
1993 print " <warning>\n";
1994 print " <para>\n";
1995 print " The template for this document tried to insert\n";
1996 print " the structured comment from the file\n";
1997 print " <filename>${file}</filename> at this point,\n";
1998 print " but none was found.\n";
1999 print " This dummy section is inserted to allow\n";
2000 print " generation to continue.\n";
2001 print " </para>\n";
2002 print " </warning>\n";
2003 print " </refsect1>\n";
2004 print "</refentry>\n";
2005 }
2006 }
2007}