blob: e5bf649e516a1d38bb9cc0ebf226b989246d09ac [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;
157
158# match expressions used to find embedded type information
159my $type_constant = '\%([-_\w]+)';
160my $type_func = '(\w+)\(\)';
161my $type_param = '\@(\w+)';
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700162my $type_struct = '\&((struct\s*)*[_\w]+)';
163my $type_struct_xml = '\\\amp;((struct\s*)*[_\w]+)';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164my $type_env = '(\$\w+)';
165
166# Output conversion substitutions.
167# One for each output format
168
169# these work fairly well
170my %highlights_html = ( $type_constant, "<i>\$1</i>",
171 $type_func, "<b>\$1</b>",
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700172 $type_struct_xml, "<i>\$1</i>",
173 $type_env, "<b><i>\$1</i></b>",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 $type_param, "<tt><b>\$1</b></tt>" );
175my $blankline_html = "<p>";
176
177# XML, docbook format
178my %highlights_xml = ( "([^=])\\\"([^\\\"<]+)\\\"", "\$1<quote>\$2</quote>",
179 $type_constant, "<constant>\$1</constant>",
180 $type_func, "<function>\$1</function>",
181 $type_struct, "<structname>\$1</structname>",
182 $type_env, "<envar>\$1</envar>",
183 $type_param, "<parameter>\$1</parameter>" );
184my $blankline_xml = "</para><para>\n";
185
186# gnome, docbook format
187my %highlights_gnome = ( $type_constant, "<replaceable class=\"option\">\$1</replaceable>",
188 $type_func, "<function>\$1</function>",
189 $type_struct, "<structname>\$1</structname>",
190 $type_env, "<envar>\$1</envar>",
191 $type_param, "<parameter>\$1</parameter>" );
192my $blankline_gnome = "</para><para>\n";
193
194# these are pretty rough
195my %highlights_man = ( $type_constant, "\$1",
196 $type_func, "\\\\fB\$1\\\\fP",
197 $type_struct, "\\\\fI\$1\\\\fP",
198 $type_param, "\\\\fI\$1\\\\fP" );
199my $blankline_man = "";
200
201# text-mode
202my %highlights_text = ( $type_constant, "\$1",
203 $type_func, "\$1",
204 $type_struct, "\$1",
205 $type_param, "\$1" );
206my $blankline_text = "";
207
208
209sub usage {
210 print "Usage: $0 [ -v ] [ -docbook | -html | -text | -man ]\n";
211 print " [ -function funcname [ -function funcname ...] ]\n";
212 print " [ -nofunction funcname [ -nofunction funcname ...] ]\n";
213 print " c source file(s) > outputfile\n";
214 exit 1;
215}
216
217# read arguments
218if ($#ARGV==-1) {
219 usage();
220}
221
222my $verbose = 0;
223my $output_mode = "man";
224my %highlights = %highlights_man;
225my $blankline = $blankline_man;
226my $modulename = "Kernel API";
227my $function_only = 0;
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800228my $man_date = ('January', 'February', 'March', 'April', 'May', 'June',
229 'July', 'August', 'September', 'October',
230 'November', 'December')[(localtime)[4]] .
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 " " . ((localtime)[5]+1900);
232
233# Essentially these are globals
234# They probably want to be tidied up made more localised or summat.
235# CAVEAT EMPTOR! Some of the others I localised may not want to be which
236# could cause "use of undefined value" or other bugs.
237my ($function, %function_table,%parametertypes,$declaration_purpose);
238my ($type,$declaration_name,$return_type);
239my ($newsection,$newcontents,$prototype,$filelist, $brcount, %source_map);
240
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800241# Generated docbook code is inserted in a template at a point where
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242# docbook v3.1 requires a non-zero sequence of RefEntry's; see:
243# http://www.oasis-open.org/docbook/documentation/reference/html/refentry.html
244# We keep track of number of generated entries and generate a dummy
245# if needs be to ensure the expanded template can be postprocessed
246# into html.
247my $section_counter = 0;
248
249my $lineprefix="";
250
251# states
252# 0 - normal code
253# 1 - looking for function name
254# 2 - scanning field start.
255# 3 - scanning prototype.
256# 4 - documentation block
257my $state;
Randy Dunlap850622d2006-06-25 05:48:55 -0700258my $in_doc_sect;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260#declaration types: can be
261# 'function', 'struct', 'union', 'enum', 'typedef'
262my $decl_type;
263
264my $doc_special = "\@\%\$\&";
265
266my $doc_start = '^/\*\*\s*$'; # Allow whitespace at end of comment start.
267my $doc_end = '\*/';
268my $doc_com = '\s*\*\s*';
269my $doc_decl = $doc_com.'(\w+)';
Randy Dunlap891dcd22007-02-10 01:45:53 -0800270my $doc_sect = $doc_com.'(['.$doc_special.']?[\w\s]+):(.*)';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271my $doc_content = $doc_com.'(.*)';
272my $doc_block = $doc_com.'DOC:\s*(.*)?';
273
274my %constants;
275my %parameterdescs;
276my @parameterlist;
277my %sections;
278my @sectionlist;
279
280my $contents = "";
281my $section_default = "Description"; # default section
282my $section_intro = "Introduction";
283my $section = $section_default;
284my $section_context = "Context";
285
286my $undescribed = "-- undescribed --";
287
288reset_state();
289
290while ($ARGV[0] =~ m/^-(.*)/) {
291 my $cmd = shift @ARGV;
292 if ($cmd eq "-html") {
293 $output_mode = "html";
294 %highlights = %highlights_html;
295 $blankline = $blankline_html;
296 } elsif ($cmd eq "-man") {
297 $output_mode = "man";
298 %highlights = %highlights_man;
299 $blankline = $blankline_man;
300 } elsif ($cmd eq "-text") {
301 $output_mode = "text";
302 %highlights = %highlights_text;
303 $blankline = $blankline_text;
304 } elsif ($cmd eq "-docbook") {
305 $output_mode = "xml";
306 %highlights = %highlights_xml;
307 $blankline = $blankline_xml;
308 } elsif ($cmd eq "-gnome") {
309 $output_mode = "gnome";
310 %highlights = %highlights_gnome;
311 $blankline = $blankline_gnome;
312 } elsif ($cmd eq "-module") { # not needed for XML, inherits from calling document
313 $modulename = shift @ARGV;
314 } elsif ($cmd eq "-function") { # to only output specific functions
315 $function_only = 1;
316 $function = shift @ARGV;
317 $function_table{$function} = 1;
318 } elsif ($cmd eq "-nofunction") { # to only output specific functions
319 $function_only = 2;
320 $function = shift @ARGV;
321 $function_table{$function} = 1;
322 } elsif ($cmd eq "-v") {
323 $verbose = 1;
324 } elsif (($cmd eq "-h") || ($cmd eq "--help")) {
325 usage();
326 } elsif ($cmd eq '-filelist') {
327 $filelist = shift @ARGV;
328 }
329}
330
Borislav Petkov53f049f2007-05-08 00:30:54 -0700331# get kernel version from env
332sub get_kernel_version() {
333 my $version;
334
335 if (defined($ENV{'KERNELVERSION'})) {
336 $version = $ENV{'KERNELVERSION'};
337 }
338 return $version;
339}
Borislav Petkov03662992007-05-09 02:33:43 -0700340my $kernelversion = get_kernel_version();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342# generate a sequence of code that will splice in highlighting information
343# using the s// operator.
344my $dohighlight = "";
345foreach my $pattern (keys %highlights) {
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700346# print STDERR "scanning pattern:$pattern, highlight:($highlights{$pattern})\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 $dohighlight .= "\$contents =~ s:$pattern:$highlights{$pattern}:gs;\n";
348}
349
350##
351# dumps section contents to arrays/hashes intended for that purpose.
352#
353sub dump_section {
354 my $name = shift;
355 my $contents = join "\n", @_;
356
357 if ($name =~ m/$type_constant/) {
358 $name = $1;
359# print STDERR "constant section '$1' = '$contents'\n";
360 $constants{$name} = $contents;
361 } elsif ($name =~ m/$type_param/) {
362# print STDERR "parameter def '$1' = '$contents'\n";
363 $name = $1;
364 $parameterdescs{$name} = $contents;
365 } else {
366# print STDERR "other section '$name' = '$contents'\n";
367 $sections{$name} = $contents;
368 push @sectionlist, $name;
369 }
370}
371
372##
373# output function
374#
375# parameterdescs, a hash.
376# function => "function name"
377# parameterlist => @list of parameters
378# parameterdescs => %parameter descriptions
379# sectionlist => @list of sections
Randy Dunlapa21217d2007-02-10 01:46:04 -0800380# sections => %section descriptions
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800381#
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383sub output_highlight {
384 my $contents = join "\n",@_;
385 my $line;
386
387# DEBUG
388# if (!defined $contents) {
389# use Carp;
390# confess "output_highlight got called with no args?\n";
391# }
392
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700393# print STDERR "contents b4:$contents\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 eval $dohighlight;
395 die $@ if $@;
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700396 if ($output_mode eq "html") {
397 $contents =~ s/\\\\//;
398 }
399# print STDERR "contents af:$contents\n";
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 foreach $line (split "\n", $contents) {
Randy Dunlap3c308792007-05-08 00:24:39 -0700402 if ($line eq ""){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 print $lineprefix, $blankline;
404 } else {
Randy Dunlap3c308792007-05-08 00:24:39 -0700405 $line =~ s/\\\\\\/\&/g;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 print $lineprefix, $line;
407 }
408 print "\n";
409 }
410}
411
412#output sections in html
413sub output_section_html(%) {
414 my %args = %{$_[0]};
415 my $section;
416
417 foreach $section (@{$args{'sectionlist'}}) {
418 print "<h3>$section</h3>\n";
419 print "<blockquote>\n";
420 output_highlight($args{'sections'}{$section});
421 print "</blockquote>\n";
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
425# output enum in html
426sub output_enum_html(%) {
427 my %args = %{$_[0]};
428 my ($parameter);
429 my $count;
430 print "<h2>enum ".$args{'enum'}."</h2>\n";
431
432 print "<b>enum ".$args{'enum'}."</b> {<br>\n";
433 $count = 0;
434 foreach $parameter (@{$args{'parameterlist'}}) {
Randy Dunlap3c308792007-05-08 00:24:39 -0700435 print " <b>".$parameter."</b>";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if ($count != $#{$args{'parameterlist'}}) {
437 $count++;
438 print ",\n";
439 }
440 print "<br>";
441 }
442 print "};<br>\n";
443
444 print "<h3>Constants</h3>\n";
445 print "<dl>\n";
446 foreach $parameter (@{$args{'parameterlist'}}) {
447 print "<dt><b>".$parameter."</b>\n";
448 print "<dd>";
449 output_highlight($args{'parameterdescs'}{$parameter});
450 }
451 print "</dl>\n";
452 output_section_html(@_);
453 print "<hr>\n";
454}
455
Randy Dunlapd28bee02006-02-01 03:06:57 -0800456# output typedef in html
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457sub output_typedef_html(%) {
458 my %args = %{$_[0]};
459 my ($parameter);
460 my $count;
461 print "<h2>typedef ".$args{'typedef'}."</h2>\n";
462
463 print "<b>typedef ".$args{'typedef'}."</b>\n";
464 output_section_html(@_);
465 print "<hr>\n";
466}
467
468# output struct in html
469sub output_struct_html(%) {
470 my %args = %{$_[0]};
471 my ($parameter);
472
Randy Dunlap262d9b02007-02-20 13:58:04 -0800473 print "<h2>".$args{'type'}." ".$args{'struct'}. " - " .$args{'purpose'}."</h2>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 print "<b>".$args{'type'}." ".$args{'struct'}."</b> {<br>\n";
475 foreach $parameter (@{$args{'parameterlist'}}) {
476 if ($parameter =~ /^#/) {
477 print "$parameter<br>\n";
478 next;
479 }
480 my $parameter_name = $parameter;
481 $parameter_name =~ s/\[.*//;
482
Randy Dunlap3c308792007-05-08 00:24:39 -0700483 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 $type = $args{'parametertypes'}{$parameter};
485 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
486 # pointer-to-function
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700487 print "&nbsp; &nbsp; <i>$1</i><b>$parameter</b>) <i>($2)</i>;<br>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700489 # bitfield
490 print "&nbsp; &nbsp; <i>$1</i> <b>$parameter</b>$2;<br>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 } else {
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700492 print "&nbsp; &nbsp; <i>$type</i> <b>$parameter</b>;<br>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
494 }
495 print "};<br>\n";
496
497 print "<h3>Members</h3>\n";
498 print "<dl>\n";
499 foreach $parameter (@{$args{'parameterlist'}}) {
500 ($parameter =~ /^#/) && next;
501
502 my $parameter_name = $parameter;
503 $parameter_name =~ s/\[.*//;
504
Randy Dunlap3c308792007-05-08 00:24:39 -0700505 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 print "<dt><b>".$parameter."</b>\n";
507 print "<dd>";
508 output_highlight($args{'parameterdescs'}{$parameter_name});
509 }
510 print "</dl>\n";
511 output_section_html(@_);
512 print "<hr>\n";
513}
514
515# output function in html
516sub output_function_html(%) {
517 my %args = %{$_[0]};
518 my ($parameter, $section);
519 my $count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Randy Dunlap262d9b02007-02-20 13:58:04 -0800521 print "<h2>" .$args{'function'}." - ".$args{'purpose'}."</h2>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 print "<i>".$args{'functiontype'}."</i>\n";
523 print "<b>".$args{'function'}."</b>\n";
524 print "(";
525 $count = 0;
526 foreach $parameter (@{$args{'parameterlist'}}) {
527 $type = $args{'parametertypes'}{$parameter};
528 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
529 # pointer-to-function
530 print "<i>$1</i><b>$parameter</b>) <i>($2)</i>";
531 } else {
532 print "<i>".$type."</i> <b>".$parameter."</b>";
533 }
534 if ($count != $#{$args{'parameterlist'}}) {
535 $count++;
536 print ",\n";
537 }
538 }
539 print ")\n";
540
541 print "<h3>Arguments</h3>\n";
542 print "<dl>\n";
543 foreach $parameter (@{$args{'parameterlist'}}) {
544 my $parameter_name = $parameter;
545 $parameter_name =~ s/\[.*//;
546
Randy Dunlap3c308792007-05-08 00:24:39 -0700547 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 print "<dt><b>".$parameter."</b>\n";
549 print "<dd>";
550 output_highlight($args{'parameterdescs'}{$parameter_name});
551 }
552 print "</dl>\n";
553 output_section_html(@_);
554 print "<hr>\n";
555}
556
557# output intro in html
558sub output_intro_html(%) {
559 my %args = %{$_[0]};
560 my ($parameter, $section);
561 my $count;
562
563 foreach $section (@{$args{'sectionlist'}}) {
564 print "<h3>$section</h3>\n";
565 print "<ul>\n";
566 output_highlight($args{'sections'}{$section});
567 print "</ul>\n";
568 }
569 print "<hr>\n";
570}
571
572sub output_section_xml(%) {
573 my %args = %{$_[0]};
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800574 my $section;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 # print out each section
576 $lineprefix=" ";
577 foreach $section (@{$args{'sectionlist'}}) {
Rich Walkerc73894c2005-05-01 08:59:26 -0700578 print "<refsect1>\n";
579 print "<title>$section</title>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if ($section =~ m/EXAMPLE/i) {
Rich Walkerc73894c2005-05-01 08:59:26 -0700581 print "<informalexample><programlisting>\n";
582 } else {
583 print "<para>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
585 output_highlight($args{'sections'}{$section});
586 if ($section =~ m/EXAMPLE/i) {
Rich Walkerc73894c2005-05-01 08:59:26 -0700587 print "</programlisting></informalexample>\n";
588 } else {
589 print "</para>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
Rich Walkerc73894c2005-05-01 08:59:26 -0700591 print "</refsect1>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
593}
594
595# output function in XML DocBook
596sub output_function_xml(%) {
597 my %args = %{$_[0]};
598 my ($parameter, $section);
599 my $count;
600 my $id;
601
602 $id = "API-".$args{'function'};
603 $id =~ s/[^A-Za-z0-9]/-/g;
604
Pavel Pisa5449bc92007-02-10 01:45:37 -0800605 print "<refentry id=\"$id\">\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700606 print "<refentryinfo>\n";
607 print " <title>LINUX</title>\n";
608 print " <productname>Kernel Hackers Manual</productname>\n";
609 print " <date>$man_date</date>\n";
610 print "</refentryinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 print "<refmeta>\n";
Pavel Pisa5449bc92007-02-10 01:45:37 -0800612 print " <refentrytitle><phrase>".$args{'function'}."</phrase></refentrytitle>\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700613 print " <manvolnum>9</manvolnum>\n";
Borislav Petkov03662992007-05-09 02:33:43 -0700614 print " <refmiscinfo class=\"version\">" . $kernelversion . "</refmiscinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 print "</refmeta>\n";
616 print "<refnamediv>\n";
617 print " <refname>".$args{'function'}."</refname>\n";
618 print " <refpurpose>\n";
619 print " ";
620 output_highlight ($args{'purpose'});
621 print " </refpurpose>\n";
622 print "</refnamediv>\n";
623
624 print "<refsynopsisdiv>\n";
625 print " <title>Synopsis</title>\n";
626 print " <funcsynopsis><funcprototype>\n";
627 print " <funcdef>".$args{'functiontype'}." ";
628 print "<function>".$args{'function'}." </function></funcdef>\n";
629
630 $count = 0;
631 if ($#{$args{'parameterlist'}} >= 0) {
632 foreach $parameter (@{$args{'parameterlist'}}) {
633 $type = $args{'parametertypes'}{$parameter};
634 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
635 # pointer-to-function
636 print " <paramdef>$1<parameter>$parameter</parameter>)\n";
637 print " <funcparams>$2</funcparams></paramdef>\n";
638 } else {
639 print " <paramdef>".$type;
640 print " <parameter>$parameter</parameter></paramdef>\n";
641 }
642 }
643 } else {
Martin Waitz6013d542005-05-01 08:59:25 -0700644 print " <void/>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
646 print " </funcprototype></funcsynopsis>\n";
647 print "</refsynopsisdiv>\n";
648
649 # print parameters
650 print "<refsect1>\n <title>Arguments</title>\n";
651 if ($#{$args{'parameterlist'}} >= 0) {
652 print " <variablelist>\n";
653 foreach $parameter (@{$args{'parameterlist'}}) {
654 my $parameter_name = $parameter;
655 $parameter_name =~ s/\[.*//;
656
657 print " <varlistentry>\n <term><parameter>$parameter</parameter></term>\n";
658 print " <listitem>\n <para>\n";
659 $lineprefix=" ";
660 output_highlight($args{'parameterdescs'}{$parameter_name});
661 print " </para>\n </listitem>\n </varlistentry>\n";
662 }
663 print " </variablelist>\n";
664 } else {
665 print " <para>\n None\n </para>\n";
666 }
667 print "</refsect1>\n";
668
669 output_section_xml(@_);
670 print "</refentry>\n\n";
671}
672
673# output struct in XML DocBook
674sub output_struct_xml(%) {
675 my %args = %{$_[0]};
676 my ($parameter, $section);
677 my $id;
678
679 $id = "API-struct-".$args{'struct'};
680 $id =~ s/[^A-Za-z0-9]/-/g;
681
Pavel Pisa5449bc92007-02-10 01:45:37 -0800682 print "<refentry id=\"$id\">\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700683 print "<refentryinfo>\n";
684 print " <title>LINUX</title>\n";
685 print " <productname>Kernel Hackers Manual</productname>\n";
686 print " <date>$man_date</date>\n";
687 print "</refentryinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 print "<refmeta>\n";
Pavel Pisa5449bc92007-02-10 01:45:37 -0800689 print " <refentrytitle><phrase>".$args{'type'}." ".$args{'struct'}."</phrase></refentrytitle>\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700690 print " <manvolnum>9</manvolnum>\n";
Borislav Petkov03662992007-05-09 02:33:43 -0700691 print " <refmiscinfo class=\"version\">" . $kernelversion . "</refmiscinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 print "</refmeta>\n";
693 print "<refnamediv>\n";
694 print " <refname>".$args{'type'}." ".$args{'struct'}."</refname>\n";
695 print " <refpurpose>\n";
696 print " ";
697 output_highlight ($args{'purpose'});
698 print " </refpurpose>\n";
699 print "</refnamediv>\n";
700
701 print "<refsynopsisdiv>\n";
702 print " <title>Synopsis</title>\n";
703 print " <programlisting>\n";
704 print $args{'type'}." ".$args{'struct'}." {\n";
705 foreach $parameter (@{$args{'parameterlist'}}) {
706 if ($parameter =~ /^#/) {
707 print "$parameter\n";
708 next;
709 }
710
711 my $parameter_name = $parameter;
712 $parameter_name =~ s/\[.*//;
713
714 defined($args{'parameterdescs'}{$parameter_name}) || next;
Randy Dunlap3c308792007-05-08 00:24:39 -0700715 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 $type = $args{'parametertypes'}{$parameter};
717 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
718 # pointer-to-function
719 print " $1 $parameter) ($2);\n";
720 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
721 print " $1 $parameter$2;\n";
722 } else {
723 print " ".$type." ".$parameter.";\n";
724 }
725 }
726 print "};";
727 print " </programlisting>\n";
728 print "</refsynopsisdiv>\n";
729
730 print " <refsect1>\n";
731 print " <title>Members</title>\n";
732
733 print " <variablelist>\n";
734 foreach $parameter (@{$args{'parameterlist'}}) {
735 ($parameter =~ /^#/) && next;
736
737 my $parameter_name = $parameter;
738 $parameter_name =~ s/\[.*//;
739
740 defined($args{'parameterdescs'}{$parameter_name}) || next;
741 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
742 print " <varlistentry>";
743 print " <term>$parameter</term>\n";
744 print " <listitem><para>\n";
745 output_highlight($args{'parameterdescs'}{$parameter_name});
746 print " </para></listitem>\n";
747 print " </varlistentry>\n";
748 }
749 print " </variablelist>\n";
750 print " </refsect1>\n";
751
752 output_section_xml(@_);
753
754 print "</refentry>\n\n";
755}
756
757# output enum in XML DocBook
758sub output_enum_xml(%) {
759 my %args = %{$_[0]};
760 my ($parameter, $section);
761 my $count;
762 my $id;
763
764 $id = "API-enum-".$args{'enum'};
765 $id =~ s/[^A-Za-z0-9]/-/g;
766
Pavel Pisa5449bc92007-02-10 01:45:37 -0800767 print "<refentry id=\"$id\">\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700768 print "<refentryinfo>\n";
769 print " <title>LINUX</title>\n";
770 print " <productname>Kernel Hackers Manual</productname>\n";
771 print " <date>$man_date</date>\n";
772 print "</refentryinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 print "<refmeta>\n";
Pavel Pisa5449bc92007-02-10 01:45:37 -0800774 print " <refentrytitle><phrase>enum ".$args{'enum'}."</phrase></refentrytitle>\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700775 print " <manvolnum>9</manvolnum>\n";
Borislav Petkov03662992007-05-09 02:33:43 -0700776 print " <refmiscinfo class=\"version\">" . $kernelversion . "</refmiscinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 print "</refmeta>\n";
778 print "<refnamediv>\n";
779 print " <refname>enum ".$args{'enum'}."</refname>\n";
780 print " <refpurpose>\n";
781 print " ";
782 output_highlight ($args{'purpose'});
783 print " </refpurpose>\n";
784 print "</refnamediv>\n";
785
786 print "<refsynopsisdiv>\n";
787 print " <title>Synopsis</title>\n";
788 print " <programlisting>\n";
789 print "enum ".$args{'enum'}." {\n";
790 $count = 0;
791 foreach $parameter (@{$args{'parameterlist'}}) {
Randy Dunlap3c308792007-05-08 00:24:39 -0700792 print " $parameter";
793 if ($count != $#{$args{'parameterlist'}}) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 $count++;
795 print ",";
Randy Dunlap3c308792007-05-08 00:24:39 -0700796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 print "\n";
798 }
799 print "};";
800 print " </programlisting>\n";
801 print "</refsynopsisdiv>\n";
802
803 print "<refsect1>\n";
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800804 print " <title>Constants</title>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 print " <variablelist>\n";
806 foreach $parameter (@{$args{'parameterlist'}}) {
807 my $parameter_name = $parameter;
808 $parameter_name =~ s/\[.*//;
809
810 print " <varlistentry>";
811 print " <term>$parameter</term>\n";
812 print " <listitem><para>\n";
813 output_highlight($args{'parameterdescs'}{$parameter_name});
814 print " </para></listitem>\n";
815 print " </varlistentry>\n";
816 }
817 print " </variablelist>\n";
818 print "</refsect1>\n";
819
820 output_section_xml(@_);
821
822 print "</refentry>\n\n";
823}
824
825# output typedef in XML DocBook
826sub output_typedef_xml(%) {
827 my %args = %{$_[0]};
828 my ($parameter, $section);
829 my $id;
830
831 $id = "API-typedef-".$args{'typedef'};
832 $id =~ s/[^A-Za-z0-9]/-/g;
833
Pavel Pisa5449bc92007-02-10 01:45:37 -0800834 print "<refentry id=\"$id\">\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700835 print "<refentryinfo>\n";
836 print " <title>LINUX</title>\n";
837 print " <productname>Kernel Hackers Manual</productname>\n";
838 print " <date>$man_date</date>\n";
839 print "</refentryinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 print "<refmeta>\n";
Pavel Pisa5449bc92007-02-10 01:45:37 -0800841 print " <refentrytitle><phrase>typedef ".$args{'typedef'}."</phrase></refentrytitle>\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700842 print " <manvolnum>9</manvolnum>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 print "</refmeta>\n";
844 print "<refnamediv>\n";
845 print " <refname>typedef ".$args{'typedef'}."</refname>\n";
846 print " <refpurpose>\n";
847 print " ";
848 output_highlight ($args{'purpose'});
849 print " </refpurpose>\n";
850 print "</refnamediv>\n";
851
852 print "<refsynopsisdiv>\n";
853 print " <title>Synopsis</title>\n";
854 print " <synopsis>typedef ".$args{'typedef'}.";</synopsis>\n";
855 print "</refsynopsisdiv>\n";
856
857 output_section_xml(@_);
858
859 print "</refentry>\n\n";
860}
861
862# output in XML DocBook
863sub output_intro_xml(%) {
864 my %args = %{$_[0]};
865 my ($parameter, $section);
866 my $count;
867
868 my $id = $args{'module'};
869 $id =~ s/[^A-Za-z0-9]/-/g;
870
871 # print out each section
872 $lineprefix=" ";
873 foreach $section (@{$args{'sectionlist'}}) {
874 print "<refsect1>\n <title>$section</title>\n <para>\n";
875 if ($section =~ m/EXAMPLE/i) {
876 print "<example><para>\n";
877 }
878 output_highlight($args{'sections'}{$section});
879 if ($section =~ m/EXAMPLE/i) {
880 print "</para></example>\n";
881 }
882 print " </para>\n</refsect1>\n";
883 }
884
885 print "\n\n";
886}
887
888# output in XML DocBook
889sub output_function_gnome {
890 my %args = %{$_[0]};
891 my ($parameter, $section);
892 my $count;
893 my $id;
894
895 $id = $args{'module'}."-".$args{'function'};
896 $id =~ s/[^A-Za-z0-9]/-/g;
897
898 print "<sect2>\n";
899 print " <title id=\"$id\">".$args{'function'}."</title>\n";
900
901 print " <funcsynopsis>\n";
902 print " <funcdef>".$args{'functiontype'}." ";
903 print "<function>".$args{'function'}." ";
904 print "</function></funcdef>\n";
905
906 $count = 0;
907 if ($#{$args{'parameterlist'}} >= 0) {
908 foreach $parameter (@{$args{'parameterlist'}}) {
909 $type = $args{'parametertypes'}{$parameter};
910 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
911 # pointer-to-function
912 print " <paramdef>$1 <parameter>$parameter</parameter>)\n";
913 print " <funcparams>$2</funcparams></paramdef>\n";
914 } else {
915 print " <paramdef>".$type;
916 print " <parameter>$parameter</parameter></paramdef>\n";
917 }
918 }
919 } else {
920 print " <void>\n";
921 }
922 print " </funcsynopsis>\n";
923 if ($#{$args{'parameterlist'}} >= 0) {
924 print " <informaltable pgwide=\"1\" frame=\"none\" role=\"params\">\n";
925 print "<tgroup cols=\"2\">\n";
926 print "<colspec colwidth=\"2*\">\n";
927 print "<colspec colwidth=\"8*\">\n";
928 print "<tbody>\n";
929 foreach $parameter (@{$args{'parameterlist'}}) {
930 my $parameter_name = $parameter;
931 $parameter_name =~ s/\[.*//;
932
933 print " <row><entry align=\"right\"><parameter>$parameter</parameter></entry>\n";
934 print " <entry>\n";
935 $lineprefix=" ";
936 output_highlight($args{'parameterdescs'}{$parameter_name});
937 print " </entry></row>\n";
938 }
939 print " </tbody></tgroup></informaltable>\n";
940 } else {
941 print " <para>\n None\n </para>\n";
942 }
943
944 # print out each section
945 $lineprefix=" ";
946 foreach $section (@{$args{'sectionlist'}}) {
947 print "<simplesect>\n <title>$section</title>\n";
948 if ($section =~ m/EXAMPLE/i) {
949 print "<example><programlisting>\n";
950 } else {
951 }
952 print "<para>\n";
953 output_highlight($args{'sections'}{$section});
954 print "</para>\n";
955 if ($section =~ m/EXAMPLE/i) {
956 print "</programlisting></example>\n";
957 } else {
958 }
959 print " </simplesect>\n";
960 }
961
962 print "</sect2>\n\n";
963}
964
965##
966# output function in man
967sub output_function_man(%) {
968 my %args = %{$_[0]};
969 my ($parameter, $section);
970 my $count;
971
972 print ".TH \"$args{'function'}\" 9 \"$args{'function'}\" \"$man_date\" \"Kernel Hacker's Manual\" LINUX\n";
973
974 print ".SH NAME\n";
975 print $args{'function'}." \\- ".$args{'purpose'}."\n";
976
977 print ".SH SYNOPSIS\n";
Randy Dunlapa21217d2007-02-10 01:46:04 -0800978 if ($args{'functiontype'} ne "") {
979 print ".B \"".$args{'functiontype'}."\" ".$args{'function'}."\n";
980 } else {
981 print ".B \"".$args{'function'}."\n";
982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 $count = 0;
984 my $parenth = "(";
985 my $post = ",";
986 foreach my $parameter (@{$args{'parameterlist'}}) {
987 if ($count == $#{$args{'parameterlist'}}) {
988 $post = ");";
989 }
990 $type = $args{'parametertypes'}{$parameter};
991 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
992 # pointer-to-function
993 print ".BI \"".$parenth.$1."\" ".$parameter." \") (".$2.")".$post."\"\n";
994 } else {
995 $type =~ s/([^\*])$/$1 /;
996 print ".BI \"".$parenth.$type."\" ".$parameter." \"".$post."\"\n";
997 }
998 $count++;
999 $parenth = "";
1000 }
1001
1002 print ".SH ARGUMENTS\n";
1003 foreach $parameter (@{$args{'parameterlist'}}) {
1004 my $parameter_name = $parameter;
1005 $parameter_name =~ s/\[.*//;
1006
1007 print ".IP \"".$parameter."\" 12\n";
1008 output_highlight($args{'parameterdescs'}{$parameter_name});
1009 }
1010 foreach $section (@{$args{'sectionlist'}}) {
1011 print ".SH \"", uc $section, "\"\n";
1012 output_highlight($args{'sections'}{$section});
1013 }
1014}
1015
1016##
1017# output enum in man
1018sub output_enum_man(%) {
1019 my %args = %{$_[0]};
1020 my ($parameter, $section);
1021 my $count;
1022
1023 print ".TH \"$args{'module'}\" 9 \"enum $args{'enum'}\" \"$man_date\" \"API Manual\" LINUX\n";
1024
1025 print ".SH NAME\n";
1026 print "enum ".$args{'enum'}." \\- ".$args{'purpose'}."\n";
1027
1028 print ".SH SYNOPSIS\n";
1029 print "enum ".$args{'enum'}." {\n";
1030 $count = 0;
1031 foreach my $parameter (@{$args{'parameterlist'}}) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001032 print ".br\n.BI \" $parameter\"\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if ($count == $#{$args{'parameterlist'}}) {
1034 print "\n};\n";
1035 last;
1036 }
1037 else {
1038 print ", \n.br\n";
1039 }
1040 $count++;
1041 }
1042
1043 print ".SH Constants\n";
1044 foreach $parameter (@{$args{'parameterlist'}}) {
1045 my $parameter_name = $parameter;
1046 $parameter_name =~ s/\[.*//;
1047
1048 print ".IP \"".$parameter."\" 12\n";
1049 output_highlight($args{'parameterdescs'}{$parameter_name});
1050 }
1051 foreach $section (@{$args{'sectionlist'}}) {
1052 print ".SH \"$section\"\n";
1053 output_highlight($args{'sections'}{$section});
1054 }
1055}
1056
1057##
1058# output struct in man
1059sub output_struct_man(%) {
1060 my %args = %{$_[0]};
1061 my ($parameter, $section);
1062
1063 print ".TH \"$args{'module'}\" 9 \"".$args{'type'}." ".$args{'struct'}."\" \"$man_date\" \"API Manual\" LINUX\n";
1064
1065 print ".SH NAME\n";
1066 print $args{'type'}." ".$args{'struct'}." \\- ".$args{'purpose'}."\n";
1067
1068 print ".SH SYNOPSIS\n";
1069 print $args{'type'}." ".$args{'struct'}." {\n.br\n";
1070
1071 foreach my $parameter (@{$args{'parameterlist'}}) {
1072 if ($parameter =~ /^#/) {
1073 print ".BI \"$parameter\"\n.br\n";
1074 next;
1075 }
1076 my $parameter_name = $parameter;
1077 $parameter_name =~ s/\[.*//;
1078
Randy Dunlap3c308792007-05-08 00:24:39 -07001079 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 $type = $args{'parametertypes'}{$parameter};
1081 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1082 # pointer-to-function
1083 print ".BI \" ".$1."\" ".$parameter." \") (".$2.")"."\"\n;\n";
1084 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
Randy.Dunlap1d7e1d42006-07-01 04:36:34 -07001085 # bitfield
1086 print ".BI \" ".$1."\ \" ".$parameter.$2." \""."\"\n;\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 } else {
1088 $type =~ s/([^\*])$/$1 /;
1089 print ".BI \" ".$type."\" ".$parameter." \""."\"\n;\n";
1090 }
1091 print "\n.br\n";
1092 }
1093 print "};\n.br\n";
1094
Randy Dunlapc51d3da2006-06-25 05:49:14 -07001095 print ".SH Members\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 foreach $parameter (@{$args{'parameterlist'}}) {
1097 ($parameter =~ /^#/) && next;
1098
1099 my $parameter_name = $parameter;
1100 $parameter_name =~ s/\[.*//;
1101
Randy Dunlap3c308792007-05-08 00:24:39 -07001102 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 print ".IP \"".$parameter."\" 12\n";
1104 output_highlight($args{'parameterdescs'}{$parameter_name});
1105 }
1106 foreach $section (@{$args{'sectionlist'}}) {
1107 print ".SH \"$section\"\n";
1108 output_highlight($args{'sections'}{$section});
1109 }
1110}
1111
1112##
1113# output typedef in man
1114sub output_typedef_man(%) {
1115 my %args = %{$_[0]};
1116 my ($parameter, $section);
1117
1118 print ".TH \"$args{'module'}\" 9 \"$args{'typedef'}\" \"$man_date\" \"API Manual\" LINUX\n";
1119
1120 print ".SH NAME\n";
1121 print "typedef ".$args{'typedef'}." \\- ".$args{'purpose'}."\n";
1122
1123 foreach $section (@{$args{'sectionlist'}}) {
1124 print ".SH \"$section\"\n";
1125 output_highlight($args{'sections'}{$section});
1126 }
1127}
1128
1129sub output_intro_man(%) {
1130 my %args = %{$_[0]};
1131 my ($parameter, $section);
1132 my $count;
1133
1134 print ".TH \"$args{'module'}\" 9 \"$args{'module'}\" \"$man_date\" \"API Manual\" LINUX\n";
1135
1136 foreach $section (@{$args{'sectionlist'}}) {
1137 print ".SH \"$section\"\n";
1138 output_highlight($args{'sections'}{$section});
1139 }
1140}
1141
1142##
1143# output in text
1144sub output_function_text(%) {
1145 my %args = %{$_[0]};
1146 my ($parameter, $section);
Randy Dunlapa21217d2007-02-10 01:46:04 -08001147 my $start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Randy Dunlapf47634b2006-07-01 04:36:36 -07001149 print "Name:\n\n";
1150 print $args{'function'}." - ".$args{'purpose'}."\n";
1151
1152 print "\nSynopsis:\n\n";
Randy Dunlapa21217d2007-02-10 01:46:04 -08001153 if ($args{'functiontype'} ne "") {
1154 $start = $args{'functiontype'}." ".$args{'function'}." (";
1155 } else {
1156 $start = $args{'function'}." (";
1157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 print $start;
Randy Dunlapa21217d2007-02-10 01:46:04 -08001159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 my $count = 0;
1161 foreach my $parameter (@{$args{'parameterlist'}}) {
1162 $type = $args{'parametertypes'}{$parameter};
1163 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1164 # pointer-to-function
1165 print $1.$parameter.") (".$2;
1166 } else {
1167 print $type." ".$parameter;
1168 }
1169 if ($count != $#{$args{'parameterlist'}}) {
1170 $count++;
1171 print ",\n";
1172 print " " x length($start);
1173 } else {
1174 print ");\n\n";
1175 }
1176 }
1177
1178 print "Arguments:\n\n";
1179 foreach $parameter (@{$args{'parameterlist'}}) {
1180 my $parameter_name = $parameter;
1181 $parameter_name =~ s/\[.*//;
1182
1183 print $parameter."\n\t".$args{'parameterdescs'}{$parameter_name}."\n";
1184 }
1185 output_section_text(@_);
1186}
1187
1188#output sections in text
1189sub output_section_text(%) {
1190 my %args = %{$_[0]};
1191 my $section;
1192
1193 print "\n";
1194 foreach $section (@{$args{'sectionlist'}}) {
1195 print "$section:\n\n";
1196 output_highlight($args{'sections'}{$section});
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 print "\n\n";
1199}
1200
1201# output enum in text
1202sub output_enum_text(%) {
1203 my %args = %{$_[0]};
1204 my ($parameter);
1205 my $count;
1206 print "Enum:\n\n";
1207
Randy.Dunlap1d7e1d42006-07-01 04:36:34 -07001208 print "enum ".$args{'enum'}." - ".$args{'purpose'}."\n\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 print "enum ".$args{'enum'}." {\n";
1210 $count = 0;
1211 foreach $parameter (@{$args{'parameterlist'}}) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001212 print "\t$parameter";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 if ($count != $#{$args{'parameterlist'}}) {
1214 $count++;
1215 print ",";
1216 }
1217 print "\n";
1218 }
1219 print "};\n\n";
1220
1221 print "Constants:\n\n";
1222 foreach $parameter (@{$args{'parameterlist'}}) {
1223 print "$parameter\n\t";
1224 print $args{'parameterdescs'}{$parameter}."\n";
1225 }
1226
1227 output_section_text(@_);
1228}
1229
1230# output typedef in text
1231sub output_typedef_text(%) {
1232 my %args = %{$_[0]};
1233 my ($parameter);
1234 my $count;
1235 print "Typedef:\n\n";
1236
Randy.Dunlap1d7e1d42006-07-01 04:36:34 -07001237 print "typedef ".$args{'typedef'}." - ".$args{'purpose'}."\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 output_section_text(@_);
1239}
1240
1241# output struct as text
1242sub output_struct_text(%) {
1243 my %args = %{$_[0]};
1244 my ($parameter);
1245
Randy.Dunlap1d7e1d42006-07-01 04:36:34 -07001246 print $args{'type'}." ".$args{'struct'}." - ".$args{'purpose'}."\n\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 print $args{'type'}." ".$args{'struct'}." {\n";
1248 foreach $parameter (@{$args{'parameterlist'}}) {
1249 if ($parameter =~ /^#/) {
1250 print "$parameter\n";
1251 next;
1252 }
1253
1254 my $parameter_name = $parameter;
1255 $parameter_name =~ s/\[.*//;
1256
Randy Dunlap3c308792007-05-08 00:24:39 -07001257 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 $type = $args{'parametertypes'}{$parameter};
1259 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1260 # pointer-to-function
1261 print "\t$1 $parameter) ($2);\n";
1262 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
1263 print "\t$1 $parameter$2;\n";
1264 } else {
1265 print "\t".$type." ".$parameter.";\n";
1266 }
1267 }
1268 print "};\n\n";
1269
1270 print "Members:\n\n";
1271 foreach $parameter (@{$args{'parameterlist'}}) {
1272 ($parameter =~ /^#/) && next;
1273
1274 my $parameter_name = $parameter;
1275 $parameter_name =~ s/\[.*//;
1276
Randy Dunlap3c308792007-05-08 00:24:39 -07001277 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 print "$parameter\n\t";
1279 print $args{'parameterdescs'}{$parameter_name}."\n";
1280 }
1281 print "\n";
1282 output_section_text(@_);
1283}
1284
1285sub output_intro_text(%) {
1286 my %args = %{$_[0]};
1287 my ($parameter, $section);
1288
1289 foreach $section (@{$args{'sectionlist'}}) {
1290 print " $section:\n";
1291 print " -> ";
1292 output_highlight($args{'sections'}{$section});
1293 }
1294}
1295
1296##
Randy Dunlap27205742006-10-11 01:22:12 -07001297# generic output function for all types (function, struct/union, typedef, enum);
1298# calls the generated, variable output_ function name based on
1299# functype and output_mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300sub output_declaration {
1301 no strict 'refs';
1302 my $name = shift;
1303 my $functype = shift;
1304 my $func = "output_${functype}_$output_mode";
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001305 if (($function_only==0) ||
1306 ( $function_only == 1 && defined($function_table{$name})) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 ( $function_only == 2 && !defined($function_table{$name})))
1308 {
Randy Dunlap3c308792007-05-08 00:24:39 -07001309 &$func(@_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 $section_counter++;
1311 }
1312}
1313
1314##
Randy Dunlap27205742006-10-11 01:22:12 -07001315# generic output function - calls the right one based on current output mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316sub output_intro {
1317 no strict 'refs';
1318 my $func = "output_intro_".$output_mode;
1319 &$func(@_);
1320 $section_counter++;
1321}
1322
1323##
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001324# takes a declaration (struct, union, enum, typedef) and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325# invokes the right handler. NOT called for functions.
1326sub dump_declaration($$) {
1327 no strict 'refs';
1328 my ($prototype, $file) = @_;
1329 my $func = "dump_".$decl_type;
1330 &$func(@_);
1331}
1332
1333sub dump_union($$) {
1334 dump_struct(@_);
1335}
1336
1337sub dump_struct($$) {
1338 my $x = shift;
1339 my $file = shift;
1340
1341 if ($x =~/(struct|union)\s+(\w+)\s*{(.*)}/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001342 $declaration_name = $2;
1343 my $members = $3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 # ignore embedded structs or unions
1346 $members =~ s/{.*?}//g;
1347
Martin Waitzaeec46b2005-11-13 16:08:13 -08001348 # ignore members marked private:
1349 $members =~ s/\/\*.*?private:.*?public:.*?\*\///gos;
1350 $members =~ s/\/\*.*?private:.*//gos;
1351 # strip comments:
1352 $members =~ s/\/\*.*?\*\///gos;
1353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 create_parameterlist($members, ';', $file);
1355
1356 output_declaration($declaration_name,
1357 'struct',
1358 {'struct' => $declaration_name,
1359 'module' => $modulename,
1360 'parameterlist' => \@parameterlist,
1361 'parameterdescs' => \%parameterdescs,
1362 'parametertypes' => \%parametertypes,
1363 'sectionlist' => \@sectionlist,
1364 'sections' => \%sections,
1365 'purpose' => $declaration_purpose,
1366 'type' => $decl_type
1367 });
1368 }
1369 else {
Randy Dunlap3c308792007-05-08 00:24:39 -07001370 print STDERR "Error(${file}:$.): Cannot parse struct or union!\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 ++$errors;
1372 }
1373}
1374
1375sub dump_enum($$) {
1376 my $x = shift;
1377 my $file = shift;
1378
Martin Waitzaeec46b2005-11-13 16:08:13 -08001379 $x =~ s@/\*.*?\*/@@gos; # strip comments.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 if ($x =~ /enum\s+(\w+)\s*{(.*)}/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001381 $declaration_name = $1;
1382 my $members = $2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384 foreach my $arg (split ',', $members) {
1385 $arg =~ s/^\s*(\w+).*/$1/;
1386 push @parameterlist, $arg;
1387 if (!$parameterdescs{$arg}) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001388 $parameterdescs{$arg} = $undescribed;
1389 print STDERR "Warning(${file}:$.): Enum value '$arg' ".
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 "not described in enum '$declaration_name'\n";
1391 }
1392
1393 }
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 output_declaration($declaration_name,
1396 'enum',
1397 {'enum' => $declaration_name,
1398 'module' => $modulename,
1399 'parameterlist' => \@parameterlist,
1400 'parameterdescs' => \%parameterdescs,
1401 'sectionlist' => \@sectionlist,
1402 'sections' => \%sections,
1403 'purpose' => $declaration_purpose
1404 });
1405 }
1406 else {
Randy Dunlap3c308792007-05-08 00:24:39 -07001407 print STDERR "Error(${file}:$.): Cannot parse enum!\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 ++$errors;
1409 }
1410}
1411
1412sub dump_typedef($$) {
1413 my $x = shift;
1414 my $file = shift;
1415
Martin Waitzaeec46b2005-11-13 16:08:13 -08001416 $x =~ s@/\*.*?\*/@@gos; # strip comments.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 while (($x =~ /\(*.\)\s*;$/) || ($x =~ /\[*.\]\s*;$/)) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001418 $x =~ s/\(*.\)\s*;$/;/;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 $x =~ s/\[*.\]\s*;$/;/;
1420 }
1421
1422 if ($x =~ /typedef.*\s+(\w+)\s*;/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001423 $declaration_name = $1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425 output_declaration($declaration_name,
1426 'typedef',
1427 {'typedef' => $declaration_name,
1428 'module' => $modulename,
1429 'sectionlist' => \@sectionlist,
1430 'sections' => \%sections,
1431 'purpose' => $declaration_purpose
1432 });
1433 }
1434 else {
Randy Dunlap3c308792007-05-08 00:24:39 -07001435 print STDERR "Error(${file}:$.): Cannot parse typedef!\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 ++$errors;
1437 }
1438}
1439
1440sub create_parameterlist($$$) {
1441 my $args = shift;
1442 my $splitter = shift;
1443 my $file = shift;
1444 my $type;
1445 my $param;
1446
Martin Waitza6d3fe72006-01-09 20:53:55 -08001447 # temporarily replace commas inside function pointer definition
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 while ($args =~ /(\([^\),]+),/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001449 $args =~ s/(\([^\),]+),/$1#/g;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 }
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001451
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 foreach my $arg (split($splitter, $args)) {
1453 # strip comments
1454 $arg =~ s/\/\*.*\*\///;
Randy Dunlap3c308792007-05-08 00:24:39 -07001455 # strip leading/trailing spaces
1456 $arg =~ s/^\s*//;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 $arg =~ s/\s*$//;
1458 $arg =~ s/\s+/ /;
1459
1460 if ($arg =~ /^#/) {
1461 # Treat preprocessor directive as a typeless variable just to fill
1462 # corresponding data structures "correctly". Catch it later in
1463 # output_* subs.
1464 push_parameter($arg, "", $file);
Randy Dunlapea82c742006-12-06 20:38:52 -08001465 } elsif ($arg =~ m/\(.*\*/) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 # pointer-to-function
1467 $arg =~ tr/#/,/;
Randy Dunlap996a07b2007-02-10 01:45:56 -08001468 $arg =~ m/[^\(]+\(\*\s*([^\)]+)\)/;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 $param = $1;
1470 $type = $arg;
1471 $type =~ s/([^\(]+\(\*)$param/$1/;
1472 push_parameter($param, $type, $file);
Martin Waitzaeec46b2005-11-13 16:08:13 -08001473 } elsif ($arg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 $arg =~ s/\s*:\s*/:/g;
1475 $arg =~ s/\s*\[/\[/g;
1476
1477 my @args = split('\s*,\s*', $arg);
1478 if ($args[0] =~ m/\*/) {
1479 $args[0] =~ s/(\*+)\s*/ $1/;
1480 }
Borislav Petkov884f2812007-05-08 00:29:05 -07001481
1482 my @first_arg;
1483 if ($args[0] =~ /^(.*\s+)(.*?\[.*\].*)$/) {
1484 shift @args;
1485 push(@first_arg, split('\s+', $1));
1486 push(@first_arg, $2);
1487 } else {
1488 @first_arg = split('\s+', shift @args);
1489 }
1490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 unshift(@args, pop @first_arg);
1492 $type = join " ", @first_arg;
1493
1494 foreach $param (@args) {
1495 if ($param =~ m/^(\*+)\s*(.*)/) {
1496 push_parameter($2, "$type $1", $file);
1497 }
1498 elsif ($param =~ m/(.*?):(\d+)/) {
1499 push_parameter($1, "$type:$2", $file)
1500 }
1501 else {
1502 push_parameter($param, $type, $file);
1503 }
1504 }
1505 }
1506 }
1507}
1508
1509sub push_parameter($$$) {
1510 my $param = shift;
1511 my $type = shift;
1512 my $file = shift;
Randy Dunlap134fe012006-12-22 01:10:50 -08001513 my $anon = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
1515 my $param_name = $param;
1516 $param_name =~ s/\[.*//;
1517
Martin Waitza6d3fe72006-01-09 20:53:55 -08001518 if ($type eq "" && $param =~ /\.\.\.$/)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 {
1520 $type="";
Martin Waitza6d3fe72006-01-09 20:53:55 -08001521 $parameterdescs{$param} = "variable arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 }
1523 elsif ($type eq "" && ($param eq "" or $param eq "void"))
1524 {
1525 $type="";
1526 $param="void";
1527 $parameterdescs{void} = "no arguments";
1528 }
Randy Dunlap134fe012006-12-22 01:10:50 -08001529 elsif ($type eq "" && ($param eq "struct" or $param eq "union"))
1530 # handle unnamed (anonymous) union or struct:
1531 {
1532 $type = $param;
1533 $param = "{unnamed_" . $param. "}";
1534 $parameterdescs{$param} = "anonymous\n";
1535 $anon = 1;
1536 }
1537
Martin Waitza6d3fe72006-01-09 20:53:55 -08001538 # warn if parameter has no description
Randy Dunlap134fe012006-12-22 01:10:50 -08001539 # (but ignore ones starting with # as these are not parameters
1540 # but inline preprocessor statements);
1541 # also ignore unnamed structs/unions;
1542 if (!$anon) {
Martin Waitza6d3fe72006-01-09 20:53:55 -08001543 if (!defined $parameterdescs{$param_name} && $param_name !~ /^#/) {
1544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 $parameterdescs{$param_name} = $undescribed;
1546
1547 if (($type eq 'function') || ($type eq 'enum')) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001548 print STDERR "Warning(${file}:$.): Function parameter ".
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 "or member '$param' not " .
1550 "described in '$declaration_name'\n";
1551 }
1552 print STDERR "Warning(${file}:$.):".
Randy Dunlap3c308792007-05-08 00:24:39 -07001553 " No description found for parameter '$param'\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 ++$warnings;
Randy Dunlap3c308792007-05-08 00:24:39 -07001555 }
1556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558 push @parameterlist, $param;
1559 $parametertypes{$param} = $type;
1560}
1561
1562##
1563# takes a function prototype and the name of the current file being
1564# processed and spits out all the details stored in the global
1565# arrays/hashes.
1566sub dump_function($$) {
1567 my $prototype = shift;
1568 my $file = shift;
1569
1570 $prototype =~ s/^static +//;
1571 $prototype =~ s/^extern +//;
Pavel Pisa4dc3b162005-05-01 08:59:25 -07001572 $prototype =~ s/^fastcall +//;
1573 $prototype =~ s/^asmlinkage +//;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 $prototype =~ s/^inline +//;
1575 $prototype =~ s/^__inline__ +//;
Randy Dunlap32e79402006-10-11 01:22:10 -07001576 $prototype =~ s/^__inline +//;
1577 $prototype =~ s/^__always_inline +//;
1578 $prototype =~ s/^noinline +//;
Randy Dunlap0129a052006-07-30 03:03:41 -07001579 $prototype =~ s/__devinit +//;
Randy Dunlap996a07b2007-02-10 01:45:56 -08001580 $prototype =~ s/^#define\s+//; #ak added
Randy Dunlap328d2442007-02-28 20:12:10 -08001581 $prototype =~ s/__attribute__\s*\(\([a-z,]*\)\)//;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
1583 # Yes, this truly is vile. We are looking for:
1584 # 1. Return type (may be nothing if we're looking at a macro)
1585 # 2. Function name
1586 # 3. Function parameters.
1587 #
1588 # All the while we have to watch out for function pointer parameters
1589 # (which IIRC is what the two sections are for), C types (these
1590 # regexps don't even start to express all the possibilities), and
1591 # so on.
1592 #
1593 # If you mess with these regexps, it's a good idea to check that
1594 # the following functions' documentation still comes out right:
1595 # - parport_register_device (function pointer parameters)
1596 # - atomic_set (macro)
Martin Waitz9598f912006-02-01 03:06:55 -08001597 # - pci_match_device, __copy_to_user (long return type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
1599 if ($prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1600 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1601 $prototype =~ m/^(\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1602 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1603 $prototype =~ m/^(\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1604 $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1605 $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1606 $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1607 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1608 $prototype =~ m/^(\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1609 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1610 $prototype =~ m/^(\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1611 $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
Martin Waitz9598f912006-02-01 03:06:55 -08001612 $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1613 $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
Randy Dunlap412ecd72007-02-10 22:47:33 -08001614 $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1615 $prototype =~ m/^(\w+\s+\w+\s*\*\s*\w+\s*\*\s*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 $return_type = $1;
1617 $declaration_name = $2;
1618 my $args = $3;
1619
1620 create_parameterlist($args, ',', $file);
1621 } else {
1622 print STDERR "Error(${file}:$.): cannot understand prototype: '$prototype'\n";
1623 ++$errors;
1624 return;
1625 }
1626
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001627 output_declaration($declaration_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 'function',
1629 {'function' => $declaration_name,
1630 'module' => $modulename,
1631 'functiontype' => $return_type,
1632 'parameterlist' => \@parameterlist,
1633 'parameterdescs' => \%parameterdescs,
1634 'parametertypes' => \%parametertypes,
1635 'sectionlist' => \@sectionlist,
1636 'sections' => \%sections,
1637 'purpose' => $declaration_purpose
1638 });
1639}
1640
1641sub process_file($);
1642
1643# Read the file that maps relative names to absolute names for
1644# separate source and object directories and for shadow trees.
1645if (open(SOURCE_MAP, "<.tmp_filelist.txt")) {
1646 my ($relname, $absname);
1647 while(<SOURCE_MAP>) {
1648 chop();
1649 ($relname, $absname) = (split())[0..1];
1650 $relname =~ s:^/+::;
1651 $source_map{$relname} = $absname;
1652 }
1653 close(SOURCE_MAP);
1654}
1655
1656if ($filelist) {
1657 open(FLIST,"<$filelist") or die "Can't open file list $filelist";
1658 while(<FLIST>) {
1659 chop;
1660 process_file($_);
1661 }
1662}
1663
1664foreach (@ARGV) {
1665 chomp;
1666 process_file($_);
1667}
1668if ($verbose && $errors) {
1669 print STDERR "$errors errors\n";
1670}
1671if ($verbose && $warnings) {
1672 print STDERR "$warnings warnings\n";
1673}
1674
1675exit($errors);
1676
1677sub reset_state {
1678 $function = "";
1679 %constants = ();
1680 %parameterdescs = ();
1681 %parametertypes = ();
1682 @parameterlist = ();
1683 %sections = ();
1684 @sectionlist = ();
1685 $prototype = "";
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 $state = 0;
1688}
1689
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001690sub process_state3_function($$) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 my $x = shift;
1692 my $file = shift;
1693
1694 if ($x =~ m#\s*/\*\s+MACDOC\s*#io || ($x =~ /^#/ && $x !~ /^#define/)) {
1695 # do nothing
1696 }
1697 elsif ($x =~ /([^\{]*)/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001698 $prototype .= $1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 }
1700 if (($x =~ /\{/) || ($x =~ /\#define/) || ($x =~ /;/)) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001701 $prototype =~ s@/\*.*?\*/@@gos; # strip comments.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 $prototype =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1703 $prototype =~ s@^\s+@@gos; # strip leading spaces
1704 dump_function($prototype,$file);
1705 reset_state();
1706 }
1707}
1708
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001709sub process_state3_type($$) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 my $x = shift;
1711 my $file = shift;
1712
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 $x =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1714 $x =~ s@^\s+@@gos; # strip leading spaces
1715 $x =~ s@\s+$@@gos; # strip trailing spaces
1716 if ($x =~ /^#/) {
1717 # To distinguish preprocessor directive from regular declaration later.
1718 $x .= ";";
1719 }
1720
1721 while (1) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001722 if ( $x =~ /([^{};]*)([{};])(.*)/ ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 $prototype .= $1 . $2;
1724 ($2 eq '{') && $brcount++;
1725 ($2 eq '}') && $brcount--;
1726 if (($2 eq ';') && ($brcount == 0)) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001727 dump_declaration($prototype,$file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 reset_state();
Randy Dunlap3c308792007-05-08 00:24:39 -07001729 last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 }
1731 $x = $3;
Randy Dunlap3c308792007-05-08 00:24:39 -07001732 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 $prototype .= $x;
1734 last;
1735 }
1736 }
1737}
1738
1739# replace <, >, and &
1740sub xml_escape($) {
1741 my $text = shift;
Randy Dunlapecfb2512006-06-25 05:49:13 -07001742 if (($output_mode eq "text") || ($output_mode eq "man")) {
1743 return $text;
1744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 $text =~ s/\&/\\\\\\amp;/g;
1746 $text =~ s/\</\\\\\\lt;/g;
1747 $text =~ s/\>/\\\\\\gt;/g;
1748 return $text;
1749}
1750
1751sub process_file($) {
Randy Dunlap2283a112005-07-07 15:39:26 -07001752 my $file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 my $identifier;
1754 my $func;
Randy Dunlapa21217d2007-02-10 01:46:04 -08001755 my $descr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 my $initial_section_counter = $section_counter;
1757
Randy Dunlap2283a112005-07-07 15:39:26 -07001758 if (defined($ENV{'SRCTREE'})) {
1759 $file = "$ENV{'SRCTREE'}" . "/" . "@_";
1760 }
1761 else {
1762 $file = "@_";
1763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 if (defined($source_map{$file})) {
1765 $file = $source_map{$file};
1766 }
1767
1768 if (!open(IN,"<$file")) {
1769 print STDERR "Error: Cannot open file $file\n";
1770 ++$errors;
1771 return;
1772 }
1773
1774 $section_counter = 0;
1775 while (<IN>) {
1776 if ($state == 0) {
1777 if (/$doc_start/o) {
1778 $state = 1; # next line is always the function name
Randy Dunlap850622d2006-06-25 05:48:55 -07001779 $in_doc_sect = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 }
1781 } elsif ($state == 1) { # this line is the function name (always)
1782 if (/$doc_block/o) {
1783 $state = 4;
1784 $contents = "";
1785 if ( $1 eq "" ) {
1786 $section = $section_intro;
1787 } else {
1788 $section = $1;
1789 }
Randy Dunlap3c308792007-05-08 00:24:39 -07001790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 elsif (/$doc_decl/o) {
1792 $identifier = $1;
1793 if (/\s*([\w\s]+?)\s*-/) {
1794 $identifier = $1;
1795 }
1796
1797 $state = 2;
1798 if (/-(.*)/) {
Randy Dunlapa21217d2007-02-10 01:46:04 -08001799 # strip leading/trailing/multiple spaces #RDD:T:
1800 $descr= $1;
1801 $descr =~ s/^\s*//;
1802 $descr =~ s/\s*$//;
1803 $descr =~ s/\s+/ /;
1804 $declaration_purpose = xml_escape($descr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 } else {
1806 $declaration_purpose = "";
1807 }
1808 if ($identifier =~ m/^struct/) {
1809 $decl_type = 'struct';
1810 } elsif ($identifier =~ m/^union/) {
1811 $decl_type = 'union';
1812 } elsif ($identifier =~ m/^enum/) {
1813 $decl_type = 'enum';
1814 } elsif ($identifier =~ m/^typedef/) {
1815 $decl_type = 'typedef';
1816 } else {
1817 $decl_type = 'function';
1818 }
1819
1820 if ($verbose) {
1821 print STDERR "Info(${file}:$.): Scanning doc for $identifier\n";
1822 }
1823 } else {
1824 print STDERR "Warning(${file}:$.): Cannot understand $_ on line $.",
1825 " - I thought it was a doc line\n";
1826 ++$warnings;
1827 $state = 0;
1828 }
1829 } elsif ($state == 2) { # look for head: lines, and include content
1830 if (/$doc_sect/o) {
1831 $newsection = $1;
1832 $newcontents = $2;
1833
1834 if ($contents ne "") {
Randy Dunlap850622d2006-06-25 05:48:55 -07001835 if (!$in_doc_sect && $verbose) {
1836 print STDERR "Warning(${file}:$.): contents before sections\n";
1837 ++$warnings;
1838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 dump_section($section, xml_escape($contents));
1840 $section = $section_default;
1841 }
1842
Randy Dunlap850622d2006-06-25 05:48:55 -07001843 $in_doc_sect = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 $contents = $newcontents;
1845 if ($contents ne "") {
Randy Dunlap27205742006-10-11 01:22:12 -07001846 while ((substr($contents, 0, 1) eq " ") ||
1847 substr($contents, 0, 1) eq "\t") {
1848 $contents = substr($contents, 1);
Randy Dunlap05189492006-06-25 05:47:47 -07001849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 $contents .= "\n";
1851 }
1852 $section = $newsection;
1853 } elsif (/$doc_end/) {
1854
1855 if ($contents ne "") {
1856 dump_section($section, xml_escape($contents));
1857 $section = $section_default;
1858 $contents = "";
1859 }
1860
1861 $prototype = "";
1862 $state = 3;
1863 $brcount = 0;
Randy Dunlap232acbc2006-06-25 05:47:48 -07001864# print STDERR "end of doc comment, looking for prototype\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 } elsif (/$doc_content/) {
1866 # miguel-style comment kludge, look for blank lines after
1867 # @parameter line to signify start of description
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001868 if ($1 eq "" &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 ($section =~ m/^@/ || $section eq $section_context)) {
1870 dump_section($section, xml_escape($contents));
1871 $section = $section_default;
1872 $contents = "";
1873 } else {
1874 $contents .= $1."\n";
1875 }
1876 } else {
1877 # i dont know - bad line? ignore.
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001878 print STDERR "Warning(${file}:$.): bad line: $_";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 ++$warnings;
1880 }
Randy Dunlap232acbc2006-06-25 05:47:48 -07001881 } elsif ($state == 3) { # scanning for function '{' (end of prototype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 if ($decl_type eq 'function') {
Randy Dunlap3c308792007-05-08 00:24:39 -07001883 process_state3_function($_, $file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 } else {
Randy Dunlap3c308792007-05-08 00:24:39 -07001885 process_state3_type($_, $file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 }
1887 } elsif ($state == 4) {
1888 # Documentation block
Randy Dunlap3c308792007-05-08 00:24:39 -07001889 if (/$doc_block/) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 dump_section($section, $contents);
1891 output_intro({'sectionlist' => \@sectionlist,
1892 'sections' => \%sections });
1893 $contents = "";
1894 $function = "";
1895 %constants = ();
1896 %parameterdescs = ();
1897 %parametertypes = ();
1898 @parameterlist = ();
1899 %sections = ();
1900 @sectionlist = ();
1901 $prototype = "";
1902 if ( $1 eq "" ) {
1903 $section = $section_intro;
1904 } else {
1905 $section = $1;
1906 }
Randy Dunlap3c308792007-05-08 00:24:39 -07001907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 elsif (/$doc_end/)
1909 {
1910 dump_section($section, $contents);
1911 output_intro({'sectionlist' => \@sectionlist,
1912 'sections' => \%sections });
1913 $contents = "";
1914 $function = "";
1915 %constants = ();
1916 %parameterdescs = ();
1917 %parametertypes = ();
1918 @parameterlist = ();
1919 %sections = ();
1920 @sectionlist = ();
1921 $prototype = "";
1922 $state = 0;
1923 }
1924 elsif (/$doc_content/)
1925 {
1926 if ( $1 eq "" )
1927 {
1928 $contents .= $blankline;
1929 }
1930 else
1931 {
1932 $contents .= $1 . "\n";
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001933 }
Randy Dunlap3c308792007-05-08 00:24:39 -07001934 }
1935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 }
1937 if ($initial_section_counter == $section_counter) {
1938 print STDERR "Warning(${file}): no structured comments found\n";
1939 if ($output_mode eq "xml") {
1940 # The template wants at least one RefEntry here; make one.
1941 print "<refentry>\n";
1942 print " <refnamediv>\n";
1943 print " <refname>\n";
1944 print " ${file}\n";
1945 print " </refname>\n";
1946 print " <refpurpose>\n";
1947 print " Document generation inconsistency\n";
1948 print " </refpurpose>\n";
1949 print " </refnamediv>\n";
1950 print " <refsect1>\n";
1951 print " <title>\n";
1952 print " Oops\n";
1953 print " </title>\n";
1954 print " <warning>\n";
1955 print " <para>\n";
1956 print " The template for this document tried to insert\n";
1957 print " the structured comment from the file\n";
1958 print " <filename>${file}</filename> at this point,\n";
1959 print " but none was found.\n";
1960 print " This dummy section is inserted to allow\n";
1961 print " generation to continue.\n";
1962 print " </para>\n";
1963 print " </warning>\n";
1964 print " </refsect1>\n";
1965 print "</refentry>\n";
1966 }
1967 }
1968}