blob: c47eb31d604c4469a355cb10c121849581b6ded3 [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+)';
162my $type_struct = '\&((struct\s*)?[_\w]+)';
163my $type_env = '(\$\w+)';
164
165# Output conversion substitutions.
166# One for each output format
167
168# these work fairly well
169my %highlights_html = ( $type_constant, "<i>\$1</i>",
170 $type_func, "<b>\$1</b>",
171 $type_struct, "<i>\$1</i>",
172 $type_param, "<tt><b>\$1</b></tt>" );
173my $blankline_html = "<p>";
174
175# XML, docbook format
176my %highlights_xml = ( "([^=])\\\"([^\\\"<]+)\\\"", "\$1<quote>\$2</quote>",
177 $type_constant, "<constant>\$1</constant>",
178 $type_func, "<function>\$1</function>",
179 $type_struct, "<structname>\$1</structname>",
180 $type_env, "<envar>\$1</envar>",
181 $type_param, "<parameter>\$1</parameter>" );
182my $blankline_xml = "</para><para>\n";
183
184# gnome, docbook format
185my %highlights_gnome = ( $type_constant, "<replaceable class=\"option\">\$1</replaceable>",
186 $type_func, "<function>\$1</function>",
187 $type_struct, "<structname>\$1</structname>",
188 $type_env, "<envar>\$1</envar>",
189 $type_param, "<parameter>\$1</parameter>" );
190my $blankline_gnome = "</para><para>\n";
191
192# these are pretty rough
193my %highlights_man = ( $type_constant, "\$1",
194 $type_func, "\\\\fB\$1\\\\fP",
195 $type_struct, "\\\\fI\$1\\\\fP",
196 $type_param, "\\\\fI\$1\\\\fP" );
197my $blankline_man = "";
198
199# text-mode
200my %highlights_text = ( $type_constant, "\$1",
201 $type_func, "\$1",
202 $type_struct, "\$1",
203 $type_param, "\$1" );
204my $blankline_text = "";
205
206
207sub usage {
208 print "Usage: $0 [ -v ] [ -docbook | -html | -text | -man ]\n";
209 print " [ -function funcname [ -function funcname ...] ]\n";
210 print " [ -nofunction funcname [ -nofunction funcname ...] ]\n";
211 print " c source file(s) > outputfile\n";
212 exit 1;
213}
214
215# read arguments
216if ($#ARGV==-1) {
217 usage();
218}
219
220my $verbose = 0;
221my $output_mode = "man";
222my %highlights = %highlights_man;
223my $blankline = $blankline_man;
224my $modulename = "Kernel API";
225my $function_only = 0;
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800226my $man_date = ('January', 'February', 'March', 'April', 'May', 'June',
227 'July', 'August', 'September', 'October',
228 'November', 'December')[(localtime)[4]] .
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 " " . ((localtime)[5]+1900);
230
231# Essentially these are globals
232# They probably want to be tidied up made more localised or summat.
233# CAVEAT EMPTOR! Some of the others I localised may not want to be which
234# could cause "use of undefined value" or other bugs.
235my ($function, %function_table,%parametertypes,$declaration_purpose);
236my ($type,$declaration_name,$return_type);
237my ($newsection,$newcontents,$prototype,$filelist, $brcount, %source_map);
238
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800239# Generated docbook code is inserted in a template at a point where
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240# docbook v3.1 requires a non-zero sequence of RefEntry's; see:
241# http://www.oasis-open.org/docbook/documentation/reference/html/refentry.html
242# We keep track of number of generated entries and generate a dummy
243# if needs be to ensure the expanded template can be postprocessed
244# into html.
245my $section_counter = 0;
246
247my $lineprefix="";
248
249# states
250# 0 - normal code
251# 1 - looking for function name
252# 2 - scanning field start.
253# 3 - scanning prototype.
254# 4 - documentation block
255my $state;
Randy Dunlap850622d2006-06-25 05:48:55 -0700256my $in_doc_sect;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258#declaration types: can be
259# 'function', 'struct', 'union', 'enum', 'typedef'
260my $decl_type;
261
262my $doc_special = "\@\%\$\&";
263
264my $doc_start = '^/\*\*\s*$'; # Allow whitespace at end of comment start.
265my $doc_end = '\*/';
266my $doc_com = '\s*\*\s*';
267my $doc_decl = $doc_com.'(\w+)';
Randy Dunlap891dcd22007-02-10 01:45:53 -0800268my $doc_sect = $doc_com.'(['.$doc_special.']?[\w\s]+):(.*)';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269my $doc_content = $doc_com.'(.*)';
270my $doc_block = $doc_com.'DOC:\s*(.*)?';
271
272my %constants;
273my %parameterdescs;
274my @parameterlist;
275my %sections;
276my @sectionlist;
277
278my $contents = "";
279my $section_default = "Description"; # default section
280my $section_intro = "Introduction";
281my $section = $section_default;
282my $section_context = "Context";
283
284my $undescribed = "-- undescribed --";
285
286reset_state();
287
288while ($ARGV[0] =~ m/^-(.*)/) {
289 my $cmd = shift @ARGV;
290 if ($cmd eq "-html") {
291 $output_mode = "html";
292 %highlights = %highlights_html;
293 $blankline = $blankline_html;
294 } elsif ($cmd eq "-man") {
295 $output_mode = "man";
296 %highlights = %highlights_man;
297 $blankline = $blankline_man;
298 } elsif ($cmd eq "-text") {
299 $output_mode = "text";
300 %highlights = %highlights_text;
301 $blankline = $blankline_text;
302 } elsif ($cmd eq "-docbook") {
303 $output_mode = "xml";
304 %highlights = %highlights_xml;
305 $blankline = $blankline_xml;
306 } elsif ($cmd eq "-gnome") {
307 $output_mode = "gnome";
308 %highlights = %highlights_gnome;
309 $blankline = $blankline_gnome;
310 } elsif ($cmd eq "-module") { # not needed for XML, inherits from calling document
311 $modulename = shift @ARGV;
312 } elsif ($cmd eq "-function") { # to only output specific functions
313 $function_only = 1;
314 $function = shift @ARGV;
315 $function_table{$function} = 1;
316 } elsif ($cmd eq "-nofunction") { # to only output specific functions
317 $function_only = 2;
318 $function = shift @ARGV;
319 $function_table{$function} = 1;
320 } elsif ($cmd eq "-v") {
321 $verbose = 1;
322 } elsif (($cmd eq "-h") || ($cmd eq "--help")) {
323 usage();
324 } elsif ($cmd eq '-filelist') {
325 $filelist = shift @ARGV;
326 }
327}
328
329
330# generate a sequence of code that will splice in highlighting information
331# using the s// operator.
332my $dohighlight = "";
333foreach my $pattern (keys %highlights) {
334# print "scanning pattern $pattern ($highlights{$pattern})\n";
335 $dohighlight .= "\$contents =~ s:$pattern:$highlights{$pattern}:gs;\n";
336}
337
338##
339# dumps section contents to arrays/hashes intended for that purpose.
340#
341sub dump_section {
342 my $name = shift;
343 my $contents = join "\n", @_;
344
345 if ($name =~ m/$type_constant/) {
346 $name = $1;
347# print STDERR "constant section '$1' = '$contents'\n";
348 $constants{$name} = $contents;
349 } elsif ($name =~ m/$type_param/) {
350# print STDERR "parameter def '$1' = '$contents'\n";
351 $name = $1;
352 $parameterdescs{$name} = $contents;
353 } else {
354# print STDERR "other section '$name' = '$contents'\n";
355 $sections{$name} = $contents;
356 push @sectionlist, $name;
357 }
358}
359
360##
361# output function
362#
363# parameterdescs, a hash.
364# function => "function name"
365# parameterlist => @list of parameters
366# parameterdescs => %parameter descriptions
367# sectionlist => @list of sections
Randy Dunlapa21217d2007-02-10 01:46:04 -0800368# sections => %section descriptions
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800369#
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371sub output_highlight {
372 my $contents = join "\n",@_;
373 my $line;
374
375# DEBUG
376# if (!defined $contents) {
377# use Carp;
378# confess "output_highlight got called with no args?\n";
379# }
380
381 eval $dohighlight;
382 die $@ if $@;
383 foreach $line (split "\n", $contents) {
Randy Dunlap3c308792007-05-08 00:24:39 -0700384 if ($line eq ""){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 print $lineprefix, $blankline;
386 } else {
Randy Dunlap3c308792007-05-08 00:24:39 -0700387 $line =~ s/\\\\\\/\&/g;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 print $lineprefix, $line;
389 }
390 print "\n";
391 }
392}
393
394#output sections in html
395sub output_section_html(%) {
396 my %args = %{$_[0]};
397 my $section;
398
399 foreach $section (@{$args{'sectionlist'}}) {
400 print "<h3>$section</h3>\n";
401 print "<blockquote>\n";
402 output_highlight($args{'sections'}{$section});
403 print "</blockquote>\n";
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
407# output enum in html
408sub output_enum_html(%) {
409 my %args = %{$_[0]};
410 my ($parameter);
411 my $count;
412 print "<h2>enum ".$args{'enum'}."</h2>\n";
413
414 print "<b>enum ".$args{'enum'}."</b> {<br>\n";
415 $count = 0;
416 foreach $parameter (@{$args{'parameterlist'}}) {
Randy Dunlap3c308792007-05-08 00:24:39 -0700417 print " <b>".$parameter."</b>";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if ($count != $#{$args{'parameterlist'}}) {
419 $count++;
420 print ",\n";
421 }
422 print "<br>";
423 }
424 print "};<br>\n";
425
426 print "<h3>Constants</h3>\n";
427 print "<dl>\n";
428 foreach $parameter (@{$args{'parameterlist'}}) {
429 print "<dt><b>".$parameter."</b>\n";
430 print "<dd>";
431 output_highlight($args{'parameterdescs'}{$parameter});
432 }
433 print "</dl>\n";
434 output_section_html(@_);
435 print "<hr>\n";
436}
437
Randy Dunlapd28bee02006-02-01 03:06:57 -0800438# output typedef in html
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439sub output_typedef_html(%) {
440 my %args = %{$_[0]};
441 my ($parameter);
442 my $count;
443 print "<h2>typedef ".$args{'typedef'}."</h2>\n";
444
445 print "<b>typedef ".$args{'typedef'}."</b>\n";
446 output_section_html(@_);
447 print "<hr>\n";
448}
449
450# output struct in html
451sub output_struct_html(%) {
452 my %args = %{$_[0]};
453 my ($parameter);
454
Randy Dunlap262d9b02007-02-20 13:58:04 -0800455 print "<h2>".$args{'type'}." ".$args{'struct'}. " - " .$args{'purpose'}."</h2>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 print "<b>".$args{'type'}." ".$args{'struct'}."</b> {<br>\n";
457 foreach $parameter (@{$args{'parameterlist'}}) {
458 if ($parameter =~ /^#/) {
459 print "$parameter<br>\n";
460 next;
461 }
462 my $parameter_name = $parameter;
463 $parameter_name =~ s/\[.*//;
464
Randy Dunlap3c308792007-05-08 00:24:39 -0700465 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 $type = $args{'parametertypes'}{$parameter};
467 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
468 # pointer-to-function
469 print " <i>$1</i><b>$parameter</b>) <i>($2)</i>;<br>\n";
470 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
471 print " <i>$1</i> <b>$parameter</b>$2;<br>\n";
472 } else {
473 print " <i>$type</i> <b>$parameter</b>;<br>\n";
474 }
475 }
476 print "};<br>\n";
477
478 print "<h3>Members</h3>\n";
479 print "<dl>\n";
480 foreach $parameter (@{$args{'parameterlist'}}) {
481 ($parameter =~ /^#/) && next;
482
483 my $parameter_name = $parameter;
484 $parameter_name =~ s/\[.*//;
485
Randy Dunlap3c308792007-05-08 00:24:39 -0700486 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 print "<dt><b>".$parameter."</b>\n";
488 print "<dd>";
489 output_highlight($args{'parameterdescs'}{$parameter_name});
490 }
491 print "</dl>\n";
492 output_section_html(@_);
493 print "<hr>\n";
494}
495
496# output function in html
497sub output_function_html(%) {
498 my %args = %{$_[0]};
499 my ($parameter, $section);
500 my $count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Randy Dunlap262d9b02007-02-20 13:58:04 -0800502 print "<h2>" .$args{'function'}." - ".$args{'purpose'}."</h2>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 print "<i>".$args{'functiontype'}."</i>\n";
504 print "<b>".$args{'function'}."</b>\n";
505 print "(";
506 $count = 0;
507 foreach $parameter (@{$args{'parameterlist'}}) {
508 $type = $args{'parametertypes'}{$parameter};
509 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
510 # pointer-to-function
511 print "<i>$1</i><b>$parameter</b>) <i>($2)</i>";
512 } else {
513 print "<i>".$type."</i> <b>".$parameter."</b>";
514 }
515 if ($count != $#{$args{'parameterlist'}}) {
516 $count++;
517 print ",\n";
518 }
519 }
520 print ")\n";
521
522 print "<h3>Arguments</h3>\n";
523 print "<dl>\n";
524 foreach $parameter (@{$args{'parameterlist'}}) {
525 my $parameter_name = $parameter;
526 $parameter_name =~ s/\[.*//;
527
Randy Dunlap3c308792007-05-08 00:24:39 -0700528 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 print "<dt><b>".$parameter."</b>\n";
530 print "<dd>";
531 output_highlight($args{'parameterdescs'}{$parameter_name});
532 }
533 print "</dl>\n";
534 output_section_html(@_);
535 print "<hr>\n";
536}
537
538# output intro in html
539sub output_intro_html(%) {
540 my %args = %{$_[0]};
541 my ($parameter, $section);
542 my $count;
543
544 foreach $section (@{$args{'sectionlist'}}) {
545 print "<h3>$section</h3>\n";
546 print "<ul>\n";
547 output_highlight($args{'sections'}{$section});
548 print "</ul>\n";
549 }
550 print "<hr>\n";
551}
552
553sub output_section_xml(%) {
554 my %args = %{$_[0]};
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800555 my $section;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 # print out each section
557 $lineprefix=" ";
558 foreach $section (@{$args{'sectionlist'}}) {
Rich Walkerc73894c2005-05-01 08:59:26 -0700559 print "<refsect1>\n";
560 print "<title>$section</title>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 if ($section =~ m/EXAMPLE/i) {
Rich Walkerc73894c2005-05-01 08:59:26 -0700562 print "<informalexample><programlisting>\n";
563 } else {
564 print "<para>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
566 output_highlight($args{'sections'}{$section});
567 if ($section =~ m/EXAMPLE/i) {
Rich Walkerc73894c2005-05-01 08:59:26 -0700568 print "</programlisting></informalexample>\n";
569 } else {
570 print "</para>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
Rich Walkerc73894c2005-05-01 08:59:26 -0700572 print "</refsect1>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
574}
575
576# output function in XML DocBook
577sub output_function_xml(%) {
578 my %args = %{$_[0]};
579 my ($parameter, $section);
580 my $count;
581 my $id;
582
583 $id = "API-".$args{'function'};
584 $id =~ s/[^A-Za-z0-9]/-/g;
585
Pavel Pisa5449bc92007-02-10 01:45:37 -0800586 print "<refentry id=\"$id\">\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700587 print "<refentryinfo>\n";
588 print " <title>LINUX</title>\n";
589 print " <productname>Kernel Hackers Manual</productname>\n";
590 print " <date>$man_date</date>\n";
591 print "</refentryinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 print "<refmeta>\n";
Pavel Pisa5449bc92007-02-10 01:45:37 -0800593 print " <refentrytitle><phrase>".$args{'function'}."</phrase></refentrytitle>\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700594 print " <manvolnum>9</manvolnum>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 print "</refmeta>\n";
596 print "<refnamediv>\n";
597 print " <refname>".$args{'function'}."</refname>\n";
598 print " <refpurpose>\n";
599 print " ";
600 output_highlight ($args{'purpose'});
601 print " </refpurpose>\n";
602 print "</refnamediv>\n";
603
604 print "<refsynopsisdiv>\n";
605 print " <title>Synopsis</title>\n";
606 print " <funcsynopsis><funcprototype>\n";
607 print " <funcdef>".$args{'functiontype'}." ";
608 print "<function>".$args{'function'}." </function></funcdef>\n";
609
610 $count = 0;
611 if ($#{$args{'parameterlist'}} >= 0) {
612 foreach $parameter (@{$args{'parameterlist'}}) {
613 $type = $args{'parametertypes'}{$parameter};
614 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
615 # pointer-to-function
616 print " <paramdef>$1<parameter>$parameter</parameter>)\n";
617 print " <funcparams>$2</funcparams></paramdef>\n";
618 } else {
619 print " <paramdef>".$type;
620 print " <parameter>$parameter</parameter></paramdef>\n";
621 }
622 }
623 } else {
Martin Waitz6013d542005-05-01 08:59:25 -0700624 print " <void/>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
626 print " </funcprototype></funcsynopsis>\n";
627 print "</refsynopsisdiv>\n";
628
629 # print parameters
630 print "<refsect1>\n <title>Arguments</title>\n";
631 if ($#{$args{'parameterlist'}} >= 0) {
632 print " <variablelist>\n";
633 foreach $parameter (@{$args{'parameterlist'}}) {
634 my $parameter_name = $parameter;
635 $parameter_name =~ s/\[.*//;
636
637 print " <varlistentry>\n <term><parameter>$parameter</parameter></term>\n";
638 print " <listitem>\n <para>\n";
639 $lineprefix=" ";
640 output_highlight($args{'parameterdescs'}{$parameter_name});
641 print " </para>\n </listitem>\n </varlistentry>\n";
642 }
643 print " </variablelist>\n";
644 } else {
645 print " <para>\n None\n </para>\n";
646 }
647 print "</refsect1>\n";
648
649 output_section_xml(@_);
650 print "</refentry>\n\n";
651}
652
653# output struct in XML DocBook
654sub output_struct_xml(%) {
655 my %args = %{$_[0]};
656 my ($parameter, $section);
657 my $id;
658
659 $id = "API-struct-".$args{'struct'};
660 $id =~ s/[^A-Za-z0-9]/-/g;
661
Pavel Pisa5449bc92007-02-10 01:45:37 -0800662 print "<refentry id=\"$id\">\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700663 print "<refentryinfo>\n";
664 print " <title>LINUX</title>\n";
665 print " <productname>Kernel Hackers Manual</productname>\n";
666 print " <date>$man_date</date>\n";
667 print "</refentryinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 print "<refmeta>\n";
Pavel Pisa5449bc92007-02-10 01:45:37 -0800669 print " <refentrytitle><phrase>".$args{'type'}." ".$args{'struct'}."</phrase></refentrytitle>\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700670 print " <manvolnum>9</manvolnum>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 print "</refmeta>\n";
672 print "<refnamediv>\n";
673 print " <refname>".$args{'type'}." ".$args{'struct'}."</refname>\n";
674 print " <refpurpose>\n";
675 print " ";
676 output_highlight ($args{'purpose'});
677 print " </refpurpose>\n";
678 print "</refnamediv>\n";
679
680 print "<refsynopsisdiv>\n";
681 print " <title>Synopsis</title>\n";
682 print " <programlisting>\n";
683 print $args{'type'}." ".$args{'struct'}." {\n";
684 foreach $parameter (@{$args{'parameterlist'}}) {
685 if ($parameter =~ /^#/) {
686 print "$parameter\n";
687 next;
688 }
689
690 my $parameter_name = $parameter;
691 $parameter_name =~ s/\[.*//;
692
693 defined($args{'parameterdescs'}{$parameter_name}) || next;
Randy Dunlap3c308792007-05-08 00:24:39 -0700694 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 $type = $args{'parametertypes'}{$parameter};
696 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
697 # pointer-to-function
698 print " $1 $parameter) ($2);\n";
699 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
700 print " $1 $parameter$2;\n";
701 } else {
702 print " ".$type." ".$parameter.";\n";
703 }
704 }
705 print "};";
706 print " </programlisting>\n";
707 print "</refsynopsisdiv>\n";
708
709 print " <refsect1>\n";
710 print " <title>Members</title>\n";
711
712 print " <variablelist>\n";
713 foreach $parameter (@{$args{'parameterlist'}}) {
714 ($parameter =~ /^#/) && next;
715
716 my $parameter_name = $parameter;
717 $parameter_name =~ s/\[.*//;
718
719 defined($args{'parameterdescs'}{$parameter_name}) || next;
720 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
721 print " <varlistentry>";
722 print " <term>$parameter</term>\n";
723 print " <listitem><para>\n";
724 output_highlight($args{'parameterdescs'}{$parameter_name});
725 print " </para></listitem>\n";
726 print " </varlistentry>\n";
727 }
728 print " </variablelist>\n";
729 print " </refsect1>\n";
730
731 output_section_xml(@_);
732
733 print "</refentry>\n\n";
734}
735
736# output enum in XML DocBook
737sub output_enum_xml(%) {
738 my %args = %{$_[0]};
739 my ($parameter, $section);
740 my $count;
741 my $id;
742
743 $id = "API-enum-".$args{'enum'};
744 $id =~ s/[^A-Za-z0-9]/-/g;
745
Pavel Pisa5449bc92007-02-10 01:45:37 -0800746 print "<refentry id=\"$id\">\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700747 print "<refentryinfo>\n";
748 print " <title>LINUX</title>\n";
749 print " <productname>Kernel Hackers Manual</productname>\n";
750 print " <date>$man_date</date>\n";
751 print "</refentryinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 print "<refmeta>\n";
Pavel Pisa5449bc92007-02-10 01:45:37 -0800753 print " <refentrytitle><phrase>enum ".$args{'enum'}."</phrase></refentrytitle>\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700754 print " <manvolnum>9</manvolnum>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 print "</refmeta>\n";
756 print "<refnamediv>\n";
757 print " <refname>enum ".$args{'enum'}."</refname>\n";
758 print " <refpurpose>\n";
759 print " ";
760 output_highlight ($args{'purpose'});
761 print " </refpurpose>\n";
762 print "</refnamediv>\n";
763
764 print "<refsynopsisdiv>\n";
765 print " <title>Synopsis</title>\n";
766 print " <programlisting>\n";
767 print "enum ".$args{'enum'}." {\n";
768 $count = 0;
769 foreach $parameter (@{$args{'parameterlist'}}) {
Randy Dunlap3c308792007-05-08 00:24:39 -0700770 print " $parameter";
771 if ($count != $#{$args{'parameterlist'}}) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 $count++;
773 print ",";
Randy Dunlap3c308792007-05-08 00:24:39 -0700774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 print "\n";
776 }
777 print "};";
778 print " </programlisting>\n";
779 print "</refsynopsisdiv>\n";
780
781 print "<refsect1>\n";
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800782 print " <title>Constants</title>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 print " <variablelist>\n";
784 foreach $parameter (@{$args{'parameterlist'}}) {
785 my $parameter_name = $parameter;
786 $parameter_name =~ s/\[.*//;
787
788 print " <varlistentry>";
789 print " <term>$parameter</term>\n";
790 print " <listitem><para>\n";
791 output_highlight($args{'parameterdescs'}{$parameter_name});
792 print " </para></listitem>\n";
793 print " </varlistentry>\n";
794 }
795 print " </variablelist>\n";
796 print "</refsect1>\n";
797
798 output_section_xml(@_);
799
800 print "</refentry>\n\n";
801}
802
803# output typedef in XML DocBook
804sub output_typedef_xml(%) {
805 my %args = %{$_[0]};
806 my ($parameter, $section);
807 my $id;
808
809 $id = "API-typedef-".$args{'typedef'};
810 $id =~ s/[^A-Za-z0-9]/-/g;
811
Pavel Pisa5449bc92007-02-10 01:45:37 -0800812 print "<refentry id=\"$id\">\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700813 print "<refentryinfo>\n";
814 print " <title>LINUX</title>\n";
815 print " <productname>Kernel Hackers Manual</productname>\n";
816 print " <date>$man_date</date>\n";
817 print "</refentryinfo>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 print "<refmeta>\n";
Pavel Pisa5449bc92007-02-10 01:45:37 -0800819 print " <refentrytitle><phrase>typedef ".$args{'typedef'}."</phrase></refentrytitle>\n";
Martin Waitz8b0c2d92005-05-01 08:59:27 -0700820 print " <manvolnum>9</manvolnum>\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 print "</refmeta>\n";
822 print "<refnamediv>\n";
823 print " <refname>typedef ".$args{'typedef'}."</refname>\n";
824 print " <refpurpose>\n";
825 print " ";
826 output_highlight ($args{'purpose'});
827 print " </refpurpose>\n";
828 print "</refnamediv>\n";
829
830 print "<refsynopsisdiv>\n";
831 print " <title>Synopsis</title>\n";
832 print " <synopsis>typedef ".$args{'typedef'}.";</synopsis>\n";
833 print "</refsynopsisdiv>\n";
834
835 output_section_xml(@_);
836
837 print "</refentry>\n\n";
838}
839
840# output in XML DocBook
841sub output_intro_xml(%) {
842 my %args = %{$_[0]};
843 my ($parameter, $section);
844 my $count;
845
846 my $id = $args{'module'};
847 $id =~ s/[^A-Za-z0-9]/-/g;
848
849 # print out each section
850 $lineprefix=" ";
851 foreach $section (@{$args{'sectionlist'}}) {
852 print "<refsect1>\n <title>$section</title>\n <para>\n";
853 if ($section =~ m/EXAMPLE/i) {
854 print "<example><para>\n";
855 }
856 output_highlight($args{'sections'}{$section});
857 if ($section =~ m/EXAMPLE/i) {
858 print "</para></example>\n";
859 }
860 print " </para>\n</refsect1>\n";
861 }
862
863 print "\n\n";
864}
865
866# output in XML DocBook
867sub output_function_gnome {
868 my %args = %{$_[0]};
869 my ($parameter, $section);
870 my $count;
871 my $id;
872
873 $id = $args{'module'}."-".$args{'function'};
874 $id =~ s/[^A-Za-z0-9]/-/g;
875
876 print "<sect2>\n";
877 print " <title id=\"$id\">".$args{'function'}."</title>\n";
878
879 print " <funcsynopsis>\n";
880 print " <funcdef>".$args{'functiontype'}." ";
881 print "<function>".$args{'function'}." ";
882 print "</function></funcdef>\n";
883
884 $count = 0;
885 if ($#{$args{'parameterlist'}} >= 0) {
886 foreach $parameter (@{$args{'parameterlist'}}) {
887 $type = $args{'parametertypes'}{$parameter};
888 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
889 # pointer-to-function
890 print " <paramdef>$1 <parameter>$parameter</parameter>)\n";
891 print " <funcparams>$2</funcparams></paramdef>\n";
892 } else {
893 print " <paramdef>".$type;
894 print " <parameter>$parameter</parameter></paramdef>\n";
895 }
896 }
897 } else {
898 print " <void>\n";
899 }
900 print " </funcsynopsis>\n";
901 if ($#{$args{'parameterlist'}} >= 0) {
902 print " <informaltable pgwide=\"1\" frame=\"none\" role=\"params\">\n";
903 print "<tgroup cols=\"2\">\n";
904 print "<colspec colwidth=\"2*\">\n";
905 print "<colspec colwidth=\"8*\">\n";
906 print "<tbody>\n";
907 foreach $parameter (@{$args{'parameterlist'}}) {
908 my $parameter_name = $parameter;
909 $parameter_name =~ s/\[.*//;
910
911 print " <row><entry align=\"right\"><parameter>$parameter</parameter></entry>\n";
912 print " <entry>\n";
913 $lineprefix=" ";
914 output_highlight($args{'parameterdescs'}{$parameter_name});
915 print " </entry></row>\n";
916 }
917 print " </tbody></tgroup></informaltable>\n";
918 } else {
919 print " <para>\n None\n </para>\n";
920 }
921
922 # print out each section
923 $lineprefix=" ";
924 foreach $section (@{$args{'sectionlist'}}) {
925 print "<simplesect>\n <title>$section</title>\n";
926 if ($section =~ m/EXAMPLE/i) {
927 print "<example><programlisting>\n";
928 } else {
929 }
930 print "<para>\n";
931 output_highlight($args{'sections'}{$section});
932 print "</para>\n";
933 if ($section =~ m/EXAMPLE/i) {
934 print "</programlisting></example>\n";
935 } else {
936 }
937 print " </simplesect>\n";
938 }
939
940 print "</sect2>\n\n";
941}
942
943##
944# output function in man
945sub output_function_man(%) {
946 my %args = %{$_[0]};
947 my ($parameter, $section);
948 my $count;
949
950 print ".TH \"$args{'function'}\" 9 \"$args{'function'}\" \"$man_date\" \"Kernel Hacker's Manual\" LINUX\n";
951
952 print ".SH NAME\n";
953 print $args{'function'}." \\- ".$args{'purpose'}."\n";
954
955 print ".SH SYNOPSIS\n";
Randy Dunlapa21217d2007-02-10 01:46:04 -0800956 if ($args{'functiontype'} ne "") {
957 print ".B \"".$args{'functiontype'}."\" ".$args{'function'}."\n";
958 } else {
959 print ".B \"".$args{'function'}."\n";
960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 $count = 0;
962 my $parenth = "(";
963 my $post = ",";
964 foreach my $parameter (@{$args{'parameterlist'}}) {
965 if ($count == $#{$args{'parameterlist'}}) {
966 $post = ");";
967 }
968 $type = $args{'parametertypes'}{$parameter};
969 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
970 # pointer-to-function
971 print ".BI \"".$parenth.$1."\" ".$parameter." \") (".$2.")".$post."\"\n";
972 } else {
973 $type =~ s/([^\*])$/$1 /;
974 print ".BI \"".$parenth.$type."\" ".$parameter." \"".$post."\"\n";
975 }
976 $count++;
977 $parenth = "";
978 }
979
980 print ".SH ARGUMENTS\n";
981 foreach $parameter (@{$args{'parameterlist'}}) {
982 my $parameter_name = $parameter;
983 $parameter_name =~ s/\[.*//;
984
985 print ".IP \"".$parameter."\" 12\n";
986 output_highlight($args{'parameterdescs'}{$parameter_name});
987 }
988 foreach $section (@{$args{'sectionlist'}}) {
989 print ".SH \"", uc $section, "\"\n";
990 output_highlight($args{'sections'}{$section});
991 }
992}
993
994##
995# output enum in man
996sub output_enum_man(%) {
997 my %args = %{$_[0]};
998 my ($parameter, $section);
999 my $count;
1000
1001 print ".TH \"$args{'module'}\" 9 \"enum $args{'enum'}\" \"$man_date\" \"API Manual\" LINUX\n";
1002
1003 print ".SH NAME\n";
1004 print "enum ".$args{'enum'}." \\- ".$args{'purpose'}."\n";
1005
1006 print ".SH SYNOPSIS\n";
1007 print "enum ".$args{'enum'}." {\n";
1008 $count = 0;
1009 foreach my $parameter (@{$args{'parameterlist'}}) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001010 print ".br\n.BI \" $parameter\"\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 if ($count == $#{$args{'parameterlist'}}) {
1012 print "\n};\n";
1013 last;
1014 }
1015 else {
1016 print ", \n.br\n";
1017 }
1018 $count++;
1019 }
1020
1021 print ".SH Constants\n";
1022 foreach $parameter (@{$args{'parameterlist'}}) {
1023 my $parameter_name = $parameter;
1024 $parameter_name =~ s/\[.*//;
1025
1026 print ".IP \"".$parameter."\" 12\n";
1027 output_highlight($args{'parameterdescs'}{$parameter_name});
1028 }
1029 foreach $section (@{$args{'sectionlist'}}) {
1030 print ".SH \"$section\"\n";
1031 output_highlight($args{'sections'}{$section});
1032 }
1033}
1034
1035##
1036# output struct in man
1037sub output_struct_man(%) {
1038 my %args = %{$_[0]};
1039 my ($parameter, $section);
1040
1041 print ".TH \"$args{'module'}\" 9 \"".$args{'type'}." ".$args{'struct'}."\" \"$man_date\" \"API Manual\" LINUX\n";
1042
1043 print ".SH NAME\n";
1044 print $args{'type'}." ".$args{'struct'}." \\- ".$args{'purpose'}."\n";
1045
1046 print ".SH SYNOPSIS\n";
1047 print $args{'type'}." ".$args{'struct'}." {\n.br\n";
1048
1049 foreach my $parameter (@{$args{'parameterlist'}}) {
1050 if ($parameter =~ /^#/) {
1051 print ".BI \"$parameter\"\n.br\n";
1052 next;
1053 }
1054 my $parameter_name = $parameter;
1055 $parameter_name =~ s/\[.*//;
1056
Randy Dunlap3c308792007-05-08 00:24:39 -07001057 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 $type = $args{'parametertypes'}{$parameter};
1059 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1060 # pointer-to-function
1061 print ".BI \" ".$1."\" ".$parameter." \") (".$2.")"."\"\n;\n";
1062 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
Randy.Dunlap1d7e1d42006-07-01 04:36:34 -07001063 # bitfield
1064 print ".BI \" ".$1."\ \" ".$parameter.$2." \""."\"\n;\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 } else {
1066 $type =~ s/([^\*])$/$1 /;
1067 print ".BI \" ".$type."\" ".$parameter." \""."\"\n;\n";
1068 }
1069 print "\n.br\n";
1070 }
1071 print "};\n.br\n";
1072
Randy Dunlapc51d3da2006-06-25 05:49:14 -07001073 print ".SH Members\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 foreach $parameter (@{$args{'parameterlist'}}) {
1075 ($parameter =~ /^#/) && next;
1076
1077 my $parameter_name = $parameter;
1078 $parameter_name =~ s/\[.*//;
1079
Randy Dunlap3c308792007-05-08 00:24:39 -07001080 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 print ".IP \"".$parameter."\" 12\n";
1082 output_highlight($args{'parameterdescs'}{$parameter_name});
1083 }
1084 foreach $section (@{$args{'sectionlist'}}) {
1085 print ".SH \"$section\"\n";
1086 output_highlight($args{'sections'}{$section});
1087 }
1088}
1089
1090##
1091# output typedef in man
1092sub output_typedef_man(%) {
1093 my %args = %{$_[0]};
1094 my ($parameter, $section);
1095
1096 print ".TH \"$args{'module'}\" 9 \"$args{'typedef'}\" \"$man_date\" \"API Manual\" LINUX\n";
1097
1098 print ".SH NAME\n";
1099 print "typedef ".$args{'typedef'}." \\- ".$args{'purpose'}."\n";
1100
1101 foreach $section (@{$args{'sectionlist'}}) {
1102 print ".SH \"$section\"\n";
1103 output_highlight($args{'sections'}{$section});
1104 }
1105}
1106
1107sub output_intro_man(%) {
1108 my %args = %{$_[0]};
1109 my ($parameter, $section);
1110 my $count;
1111
1112 print ".TH \"$args{'module'}\" 9 \"$args{'module'}\" \"$man_date\" \"API Manual\" LINUX\n";
1113
1114 foreach $section (@{$args{'sectionlist'}}) {
1115 print ".SH \"$section\"\n";
1116 output_highlight($args{'sections'}{$section});
1117 }
1118}
1119
1120##
1121# output in text
1122sub output_function_text(%) {
1123 my %args = %{$_[0]};
1124 my ($parameter, $section);
Randy Dunlapa21217d2007-02-10 01:46:04 -08001125 my $start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Randy Dunlapf47634b2006-07-01 04:36:36 -07001127 print "Name:\n\n";
1128 print $args{'function'}." - ".$args{'purpose'}."\n";
1129
1130 print "\nSynopsis:\n\n";
Randy Dunlapa21217d2007-02-10 01:46:04 -08001131 if ($args{'functiontype'} ne "") {
1132 $start = $args{'functiontype'}." ".$args{'function'}." (";
1133 } else {
1134 $start = $args{'function'}." (";
1135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 print $start;
Randy Dunlapa21217d2007-02-10 01:46:04 -08001137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 my $count = 0;
1139 foreach my $parameter (@{$args{'parameterlist'}}) {
1140 $type = $args{'parametertypes'}{$parameter};
1141 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1142 # pointer-to-function
1143 print $1.$parameter.") (".$2;
1144 } else {
1145 print $type." ".$parameter;
1146 }
1147 if ($count != $#{$args{'parameterlist'}}) {
1148 $count++;
1149 print ",\n";
1150 print " " x length($start);
1151 } else {
1152 print ");\n\n";
1153 }
1154 }
1155
1156 print "Arguments:\n\n";
1157 foreach $parameter (@{$args{'parameterlist'}}) {
1158 my $parameter_name = $parameter;
1159 $parameter_name =~ s/\[.*//;
1160
1161 print $parameter."\n\t".$args{'parameterdescs'}{$parameter_name}."\n";
1162 }
1163 output_section_text(@_);
1164}
1165
1166#output sections in text
1167sub output_section_text(%) {
1168 my %args = %{$_[0]};
1169 my $section;
1170
1171 print "\n";
1172 foreach $section (@{$args{'sectionlist'}}) {
1173 print "$section:\n\n";
1174 output_highlight($args{'sections'}{$section});
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 print "\n\n";
1177}
1178
1179# output enum in text
1180sub output_enum_text(%) {
1181 my %args = %{$_[0]};
1182 my ($parameter);
1183 my $count;
1184 print "Enum:\n\n";
1185
Randy.Dunlap1d7e1d42006-07-01 04:36:34 -07001186 print "enum ".$args{'enum'}." - ".$args{'purpose'}."\n\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 print "enum ".$args{'enum'}." {\n";
1188 $count = 0;
1189 foreach $parameter (@{$args{'parameterlist'}}) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001190 print "\t$parameter";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 if ($count != $#{$args{'parameterlist'}}) {
1192 $count++;
1193 print ",";
1194 }
1195 print "\n";
1196 }
1197 print "};\n\n";
1198
1199 print "Constants:\n\n";
1200 foreach $parameter (@{$args{'parameterlist'}}) {
1201 print "$parameter\n\t";
1202 print $args{'parameterdescs'}{$parameter}."\n";
1203 }
1204
1205 output_section_text(@_);
1206}
1207
1208# output typedef in text
1209sub output_typedef_text(%) {
1210 my %args = %{$_[0]};
1211 my ($parameter);
1212 my $count;
1213 print "Typedef:\n\n";
1214
Randy.Dunlap1d7e1d42006-07-01 04:36:34 -07001215 print "typedef ".$args{'typedef'}." - ".$args{'purpose'}."\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 output_section_text(@_);
1217}
1218
1219# output struct as text
1220sub output_struct_text(%) {
1221 my %args = %{$_[0]};
1222 my ($parameter);
1223
Randy.Dunlap1d7e1d42006-07-01 04:36:34 -07001224 print $args{'type'}." ".$args{'struct'}." - ".$args{'purpose'}."\n\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 print $args{'type'}." ".$args{'struct'}." {\n";
1226 foreach $parameter (@{$args{'parameterlist'}}) {
1227 if ($parameter =~ /^#/) {
1228 print "$parameter\n";
1229 next;
1230 }
1231
1232 my $parameter_name = $parameter;
1233 $parameter_name =~ s/\[.*//;
1234
Randy Dunlap3c308792007-05-08 00:24:39 -07001235 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 $type = $args{'parametertypes'}{$parameter};
1237 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
1238 # pointer-to-function
1239 print "\t$1 $parameter) ($2);\n";
1240 } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
1241 print "\t$1 $parameter$2;\n";
1242 } else {
1243 print "\t".$type." ".$parameter.";\n";
1244 }
1245 }
1246 print "};\n\n";
1247
1248 print "Members:\n\n";
1249 foreach $parameter (@{$args{'parameterlist'}}) {
1250 ($parameter =~ /^#/) && next;
1251
1252 my $parameter_name = $parameter;
1253 $parameter_name =~ s/\[.*//;
1254
Randy Dunlap3c308792007-05-08 00:24:39 -07001255 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 print "$parameter\n\t";
1257 print $args{'parameterdescs'}{$parameter_name}."\n";
1258 }
1259 print "\n";
1260 output_section_text(@_);
1261}
1262
1263sub output_intro_text(%) {
1264 my %args = %{$_[0]};
1265 my ($parameter, $section);
1266
1267 foreach $section (@{$args{'sectionlist'}}) {
1268 print " $section:\n";
1269 print " -> ";
1270 output_highlight($args{'sections'}{$section});
1271 }
1272}
1273
1274##
Randy Dunlap27205742006-10-11 01:22:12 -07001275# generic output function for all types (function, struct/union, typedef, enum);
1276# calls the generated, variable output_ function name based on
1277# functype and output_mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278sub output_declaration {
1279 no strict 'refs';
1280 my $name = shift;
1281 my $functype = shift;
1282 my $func = "output_${functype}_$output_mode";
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001283 if (($function_only==0) ||
1284 ( $function_only == 1 && defined($function_table{$name})) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 ( $function_only == 2 && !defined($function_table{$name})))
1286 {
Randy Dunlap3c308792007-05-08 00:24:39 -07001287 &$func(@_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 $section_counter++;
1289 }
1290}
1291
1292##
Randy Dunlap27205742006-10-11 01:22:12 -07001293# generic output function - calls the right one based on current output mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294sub output_intro {
1295 no strict 'refs';
1296 my $func = "output_intro_".$output_mode;
1297 &$func(@_);
1298 $section_counter++;
1299}
1300
1301##
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001302# takes a declaration (struct, union, enum, typedef) and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303# invokes the right handler. NOT called for functions.
1304sub dump_declaration($$) {
1305 no strict 'refs';
1306 my ($prototype, $file) = @_;
1307 my $func = "dump_".$decl_type;
1308 &$func(@_);
1309}
1310
1311sub dump_union($$) {
1312 dump_struct(@_);
1313}
1314
1315sub dump_struct($$) {
1316 my $x = shift;
1317 my $file = shift;
1318
1319 if ($x =~/(struct|union)\s+(\w+)\s*{(.*)}/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001320 $declaration_name = $2;
1321 my $members = $3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 # ignore embedded structs or unions
1324 $members =~ s/{.*?}//g;
1325
Martin Waitzaeec46b2005-11-13 16:08:13 -08001326 # ignore members marked private:
1327 $members =~ s/\/\*.*?private:.*?public:.*?\*\///gos;
1328 $members =~ s/\/\*.*?private:.*//gos;
1329 # strip comments:
1330 $members =~ s/\/\*.*?\*\///gos;
1331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 create_parameterlist($members, ';', $file);
1333
1334 output_declaration($declaration_name,
1335 'struct',
1336 {'struct' => $declaration_name,
1337 'module' => $modulename,
1338 'parameterlist' => \@parameterlist,
1339 'parameterdescs' => \%parameterdescs,
1340 'parametertypes' => \%parametertypes,
1341 'sectionlist' => \@sectionlist,
1342 'sections' => \%sections,
1343 'purpose' => $declaration_purpose,
1344 'type' => $decl_type
1345 });
1346 }
1347 else {
Randy Dunlap3c308792007-05-08 00:24:39 -07001348 print STDERR "Error(${file}:$.): Cannot parse struct or union!\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 ++$errors;
1350 }
1351}
1352
1353sub dump_enum($$) {
1354 my $x = shift;
1355 my $file = shift;
1356
Martin Waitzaeec46b2005-11-13 16:08:13 -08001357 $x =~ s@/\*.*?\*/@@gos; # strip comments.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 if ($x =~ /enum\s+(\w+)\s*{(.*)}/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001359 $declaration_name = $1;
1360 my $members = $2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362 foreach my $arg (split ',', $members) {
1363 $arg =~ s/^\s*(\w+).*/$1/;
1364 push @parameterlist, $arg;
1365 if (!$parameterdescs{$arg}) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001366 $parameterdescs{$arg} = $undescribed;
1367 print STDERR "Warning(${file}:$.): Enum value '$arg' ".
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 "not described in enum '$declaration_name'\n";
1369 }
1370
1371 }
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001372
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 output_declaration($declaration_name,
1374 'enum',
1375 {'enum' => $declaration_name,
1376 'module' => $modulename,
1377 'parameterlist' => \@parameterlist,
1378 'parameterdescs' => \%parameterdescs,
1379 'sectionlist' => \@sectionlist,
1380 'sections' => \%sections,
1381 'purpose' => $declaration_purpose
1382 });
1383 }
1384 else {
Randy Dunlap3c308792007-05-08 00:24:39 -07001385 print STDERR "Error(${file}:$.): Cannot parse enum!\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 ++$errors;
1387 }
1388}
1389
1390sub dump_typedef($$) {
1391 my $x = shift;
1392 my $file = shift;
1393
Martin Waitzaeec46b2005-11-13 16:08:13 -08001394 $x =~ s@/\*.*?\*/@@gos; # strip comments.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 while (($x =~ /\(*.\)\s*;$/) || ($x =~ /\[*.\]\s*;$/)) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001396 $x =~ s/\(*.\)\s*;$/;/;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 $x =~ s/\[*.\]\s*;$/;/;
1398 }
1399
1400 if ($x =~ /typedef.*\s+(\w+)\s*;/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001401 $declaration_name = $1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403 output_declaration($declaration_name,
1404 'typedef',
1405 {'typedef' => $declaration_name,
1406 'module' => $modulename,
1407 'sectionlist' => \@sectionlist,
1408 'sections' => \%sections,
1409 'purpose' => $declaration_purpose
1410 });
1411 }
1412 else {
Randy Dunlap3c308792007-05-08 00:24:39 -07001413 print STDERR "Error(${file}:$.): Cannot parse typedef!\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 ++$errors;
1415 }
1416}
1417
1418sub create_parameterlist($$$) {
1419 my $args = shift;
1420 my $splitter = shift;
1421 my $file = shift;
1422 my $type;
1423 my $param;
1424
Martin Waitza6d3fe72006-01-09 20:53:55 -08001425 # temporarily replace commas inside function pointer definition
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 while ($args =~ /(\([^\),]+),/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001427 $args =~ s/(\([^\),]+),/$1#/g;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 }
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 foreach my $arg (split($splitter, $args)) {
1431 # strip comments
1432 $arg =~ s/\/\*.*\*\///;
Randy Dunlap3c308792007-05-08 00:24:39 -07001433 # strip leading/trailing spaces
1434 $arg =~ s/^\s*//;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 $arg =~ s/\s*$//;
1436 $arg =~ s/\s+/ /;
1437
1438 if ($arg =~ /^#/) {
1439 # Treat preprocessor directive as a typeless variable just to fill
1440 # corresponding data structures "correctly". Catch it later in
1441 # output_* subs.
1442 push_parameter($arg, "", $file);
Randy Dunlapea82c742006-12-06 20:38:52 -08001443 } elsif ($arg =~ m/\(.*\*/) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 # pointer-to-function
1445 $arg =~ tr/#/,/;
Randy Dunlap996a07b2007-02-10 01:45:56 -08001446 $arg =~ m/[^\(]+\(\*\s*([^\)]+)\)/;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 $param = $1;
1448 $type = $arg;
1449 $type =~ s/([^\(]+\(\*)$param/$1/;
1450 push_parameter($param, $type, $file);
Martin Waitzaeec46b2005-11-13 16:08:13 -08001451 } elsif ($arg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 $arg =~ s/\s*:\s*/:/g;
1453 $arg =~ s/\s*\[/\[/g;
1454
1455 my @args = split('\s*,\s*', $arg);
1456 if ($args[0] =~ m/\*/) {
1457 $args[0] =~ s/(\*+)\s*/ $1/;
1458 }
Borislav Petkov884f2812007-05-08 00:29:05 -07001459
1460 my @first_arg;
1461 if ($args[0] =~ /^(.*\s+)(.*?\[.*\].*)$/) {
1462 shift @args;
1463 push(@first_arg, split('\s+', $1));
1464 push(@first_arg, $2);
1465 } else {
1466 @first_arg = split('\s+', shift @args);
1467 }
1468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 unshift(@args, pop @first_arg);
1470 $type = join " ", @first_arg;
1471
1472 foreach $param (@args) {
1473 if ($param =~ m/^(\*+)\s*(.*)/) {
1474 push_parameter($2, "$type $1", $file);
1475 }
1476 elsif ($param =~ m/(.*?):(\d+)/) {
1477 push_parameter($1, "$type:$2", $file)
1478 }
1479 else {
1480 push_parameter($param, $type, $file);
1481 }
1482 }
1483 }
1484 }
1485}
1486
1487sub push_parameter($$$) {
1488 my $param = shift;
1489 my $type = shift;
1490 my $file = shift;
Randy Dunlap134fe012006-12-22 01:10:50 -08001491 my $anon = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
1493 my $param_name = $param;
1494 $param_name =~ s/\[.*//;
1495
Martin Waitza6d3fe72006-01-09 20:53:55 -08001496 if ($type eq "" && $param =~ /\.\.\.$/)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 {
1498 $type="";
Martin Waitza6d3fe72006-01-09 20:53:55 -08001499 $parameterdescs{$param} = "variable arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 }
1501 elsif ($type eq "" && ($param eq "" or $param eq "void"))
1502 {
1503 $type="";
1504 $param="void";
1505 $parameterdescs{void} = "no arguments";
1506 }
Randy Dunlap134fe012006-12-22 01:10:50 -08001507 elsif ($type eq "" && ($param eq "struct" or $param eq "union"))
1508 # handle unnamed (anonymous) union or struct:
1509 {
1510 $type = $param;
1511 $param = "{unnamed_" . $param. "}";
1512 $parameterdescs{$param} = "anonymous\n";
1513 $anon = 1;
1514 }
1515
Martin Waitza6d3fe72006-01-09 20:53:55 -08001516 # warn if parameter has no description
Randy Dunlap134fe012006-12-22 01:10:50 -08001517 # (but ignore ones starting with # as these are not parameters
1518 # but inline preprocessor statements);
1519 # also ignore unnamed structs/unions;
1520 if (!$anon) {
Martin Waitza6d3fe72006-01-09 20:53:55 -08001521 if (!defined $parameterdescs{$param_name} && $param_name !~ /^#/) {
1522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 $parameterdescs{$param_name} = $undescribed;
1524
1525 if (($type eq 'function') || ($type eq 'enum')) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001526 print STDERR "Warning(${file}:$.): Function parameter ".
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 "or member '$param' not " .
1528 "described in '$declaration_name'\n";
1529 }
1530 print STDERR "Warning(${file}:$.):".
Randy Dunlap3c308792007-05-08 00:24:39 -07001531 " No description found for parameter '$param'\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 ++$warnings;
Randy Dunlap3c308792007-05-08 00:24:39 -07001533 }
1534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
1536 push @parameterlist, $param;
1537 $parametertypes{$param} = $type;
1538}
1539
1540##
1541# takes a function prototype and the name of the current file being
1542# processed and spits out all the details stored in the global
1543# arrays/hashes.
1544sub dump_function($$) {
1545 my $prototype = shift;
1546 my $file = shift;
1547
1548 $prototype =~ s/^static +//;
1549 $prototype =~ s/^extern +//;
Pavel Pisa4dc3b162005-05-01 08:59:25 -07001550 $prototype =~ s/^fastcall +//;
1551 $prototype =~ s/^asmlinkage +//;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 $prototype =~ s/^inline +//;
1553 $prototype =~ s/^__inline__ +//;
Randy Dunlap32e79402006-10-11 01:22:10 -07001554 $prototype =~ s/^__inline +//;
1555 $prototype =~ s/^__always_inline +//;
1556 $prototype =~ s/^noinline +//;
Randy Dunlap0129a052006-07-30 03:03:41 -07001557 $prototype =~ s/__devinit +//;
Randy Dunlap996a07b2007-02-10 01:45:56 -08001558 $prototype =~ s/^#define\s+//; #ak added
Randy Dunlap328d2442007-02-28 20:12:10 -08001559 $prototype =~ s/__attribute__\s*\(\([a-z,]*\)\)//;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
1561 # Yes, this truly is vile. We are looking for:
1562 # 1. Return type (may be nothing if we're looking at a macro)
1563 # 2. Function name
1564 # 3. Function parameters.
1565 #
1566 # All the while we have to watch out for function pointer parameters
1567 # (which IIRC is what the two sections are for), C types (these
1568 # regexps don't even start to express all the possibilities), and
1569 # so on.
1570 #
1571 # If you mess with these regexps, it's a good idea to check that
1572 # the following functions' documentation still comes out right:
1573 # - parport_register_device (function pointer parameters)
1574 # - atomic_set (macro)
Martin Waitz9598f912006-02-01 03:06:55 -08001575 # - pci_match_device, __copy_to_user (long return type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
1577 if ($prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1578 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1579 $prototype =~ m/^(\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1580 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1581 $prototype =~ m/^(\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1582 $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1583 $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1584 $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1585 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1586 $prototype =~ m/^(\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1587 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1588 $prototype =~ m/^(\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1589 $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
Martin Waitz9598f912006-02-01 03:06:55 -08001590 $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1591 $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
Randy Dunlap412ecd772007-02-10 22:47:33 -08001592 $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1593 $prototype =~ m/^(\w+\s+\w+\s*\*\s*\w+\s*\*\s*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 $return_type = $1;
1595 $declaration_name = $2;
1596 my $args = $3;
1597
1598 create_parameterlist($args, ',', $file);
1599 } else {
1600 print STDERR "Error(${file}:$.): cannot understand prototype: '$prototype'\n";
1601 ++$errors;
1602 return;
1603 }
1604
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001605 output_declaration($declaration_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 'function',
1607 {'function' => $declaration_name,
1608 'module' => $modulename,
1609 'functiontype' => $return_type,
1610 'parameterlist' => \@parameterlist,
1611 'parameterdescs' => \%parameterdescs,
1612 'parametertypes' => \%parametertypes,
1613 'sectionlist' => \@sectionlist,
1614 'sections' => \%sections,
1615 'purpose' => $declaration_purpose
1616 });
1617}
1618
1619sub process_file($);
1620
1621# Read the file that maps relative names to absolute names for
1622# separate source and object directories and for shadow trees.
1623if (open(SOURCE_MAP, "<.tmp_filelist.txt")) {
1624 my ($relname, $absname);
1625 while(<SOURCE_MAP>) {
1626 chop();
1627 ($relname, $absname) = (split())[0..1];
1628 $relname =~ s:^/+::;
1629 $source_map{$relname} = $absname;
1630 }
1631 close(SOURCE_MAP);
1632}
1633
1634if ($filelist) {
1635 open(FLIST,"<$filelist") or die "Can't open file list $filelist";
1636 while(<FLIST>) {
1637 chop;
1638 process_file($_);
1639 }
1640}
1641
1642foreach (@ARGV) {
1643 chomp;
1644 process_file($_);
1645}
1646if ($verbose && $errors) {
1647 print STDERR "$errors errors\n";
1648}
1649if ($verbose && $warnings) {
1650 print STDERR "$warnings warnings\n";
1651}
1652
1653exit($errors);
1654
1655sub reset_state {
1656 $function = "";
1657 %constants = ();
1658 %parameterdescs = ();
1659 %parametertypes = ();
1660 @parameterlist = ();
1661 %sections = ();
1662 @sectionlist = ();
1663 $prototype = "";
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 $state = 0;
1666}
1667
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001668sub process_state3_function($$) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 my $x = shift;
1670 my $file = shift;
1671
1672 if ($x =~ m#\s*/\*\s+MACDOC\s*#io || ($x =~ /^#/ && $x !~ /^#define/)) {
1673 # do nothing
1674 }
1675 elsif ($x =~ /([^\{]*)/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001676 $prototype .= $1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 }
1678 if (($x =~ /\{/) || ($x =~ /\#define/) || ($x =~ /;/)) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001679 $prototype =~ s@/\*.*?\*/@@gos; # strip comments.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 $prototype =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1681 $prototype =~ s@^\s+@@gos; # strip leading spaces
1682 dump_function($prototype,$file);
1683 reset_state();
1684 }
1685}
1686
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001687sub process_state3_type($$) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 my $x = shift;
1689 my $file = shift;
1690
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 $x =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1692 $x =~ s@^\s+@@gos; # strip leading spaces
1693 $x =~ s@\s+$@@gos; # strip trailing spaces
1694 if ($x =~ /^#/) {
1695 # To distinguish preprocessor directive from regular declaration later.
1696 $x .= ";";
1697 }
1698
1699 while (1) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001700 if ( $x =~ /([^{};]*)([{};])(.*)/ ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 $prototype .= $1 . $2;
1702 ($2 eq '{') && $brcount++;
1703 ($2 eq '}') && $brcount--;
1704 if (($2 eq ';') && ($brcount == 0)) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001705 dump_declaration($prototype,$file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 reset_state();
Randy Dunlap3c308792007-05-08 00:24:39 -07001707 last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 }
1709 $x = $3;
Randy Dunlap3c308792007-05-08 00:24:39 -07001710 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 $prototype .= $x;
1712 last;
1713 }
1714 }
1715}
1716
1717# replace <, >, and &
1718sub xml_escape($) {
1719 my $text = shift;
Randy Dunlapecfb2512006-06-25 05:49:13 -07001720 if (($output_mode eq "text") || ($output_mode eq "man")) {
1721 return $text;
1722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 $text =~ s/\&/\\\\\\amp;/g;
1724 $text =~ s/\</\\\\\\lt;/g;
1725 $text =~ s/\>/\\\\\\gt;/g;
1726 return $text;
1727}
1728
1729sub process_file($) {
Randy Dunlap2283a112005-07-07 15:39:26 -07001730 my $file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 my $identifier;
1732 my $func;
Randy Dunlapa21217d2007-02-10 01:46:04 -08001733 my $descr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 my $initial_section_counter = $section_counter;
1735
Randy Dunlap2283a112005-07-07 15:39:26 -07001736 if (defined($ENV{'SRCTREE'})) {
1737 $file = "$ENV{'SRCTREE'}" . "/" . "@_";
1738 }
1739 else {
1740 $file = "@_";
1741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 if (defined($source_map{$file})) {
1743 $file = $source_map{$file};
1744 }
1745
1746 if (!open(IN,"<$file")) {
1747 print STDERR "Error: Cannot open file $file\n";
1748 ++$errors;
1749 return;
1750 }
1751
1752 $section_counter = 0;
1753 while (<IN>) {
1754 if ($state == 0) {
1755 if (/$doc_start/o) {
1756 $state = 1; # next line is always the function name
Randy Dunlap850622d2006-06-25 05:48:55 -07001757 $in_doc_sect = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 }
1759 } elsif ($state == 1) { # this line is the function name (always)
1760 if (/$doc_block/o) {
1761 $state = 4;
1762 $contents = "";
1763 if ( $1 eq "" ) {
1764 $section = $section_intro;
1765 } else {
1766 $section = $1;
1767 }
Randy Dunlap3c308792007-05-08 00:24:39 -07001768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 elsif (/$doc_decl/o) {
1770 $identifier = $1;
1771 if (/\s*([\w\s]+?)\s*-/) {
1772 $identifier = $1;
1773 }
1774
1775 $state = 2;
1776 if (/-(.*)/) {
Randy Dunlapa21217d2007-02-10 01:46:04 -08001777 # strip leading/trailing/multiple spaces #RDD:T:
1778 $descr= $1;
1779 $descr =~ s/^\s*//;
1780 $descr =~ s/\s*$//;
1781 $descr =~ s/\s+/ /;
1782 $declaration_purpose = xml_escape($descr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 } else {
1784 $declaration_purpose = "";
1785 }
1786 if ($identifier =~ m/^struct/) {
1787 $decl_type = 'struct';
1788 } elsif ($identifier =~ m/^union/) {
1789 $decl_type = 'union';
1790 } elsif ($identifier =~ m/^enum/) {
1791 $decl_type = 'enum';
1792 } elsif ($identifier =~ m/^typedef/) {
1793 $decl_type = 'typedef';
1794 } else {
1795 $decl_type = 'function';
1796 }
1797
1798 if ($verbose) {
1799 print STDERR "Info(${file}:$.): Scanning doc for $identifier\n";
1800 }
1801 } else {
1802 print STDERR "Warning(${file}:$.): Cannot understand $_ on line $.",
1803 " - I thought it was a doc line\n";
1804 ++$warnings;
1805 $state = 0;
1806 }
1807 } elsif ($state == 2) { # look for head: lines, and include content
1808 if (/$doc_sect/o) {
1809 $newsection = $1;
1810 $newcontents = $2;
1811
1812 if ($contents ne "") {
Randy Dunlap850622d2006-06-25 05:48:55 -07001813 if (!$in_doc_sect && $verbose) {
1814 print STDERR "Warning(${file}:$.): contents before sections\n";
1815 ++$warnings;
1816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 dump_section($section, xml_escape($contents));
1818 $section = $section_default;
1819 }
1820
Randy Dunlap850622d2006-06-25 05:48:55 -07001821 $in_doc_sect = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 $contents = $newcontents;
1823 if ($contents ne "") {
Randy Dunlap27205742006-10-11 01:22:12 -07001824 while ((substr($contents, 0, 1) eq " ") ||
1825 substr($contents, 0, 1) eq "\t") {
1826 $contents = substr($contents, 1);
Randy Dunlap05189492006-06-25 05:47:47 -07001827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 $contents .= "\n";
1829 }
1830 $section = $newsection;
1831 } elsif (/$doc_end/) {
1832
1833 if ($contents ne "") {
1834 dump_section($section, xml_escape($contents));
1835 $section = $section_default;
1836 $contents = "";
1837 }
1838
1839 $prototype = "";
1840 $state = 3;
1841 $brcount = 0;
Randy Dunlap232acbc2006-06-25 05:47:48 -07001842# print STDERR "end of doc comment, looking for prototype\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 } elsif (/$doc_content/) {
1844 # miguel-style comment kludge, look for blank lines after
1845 # @parameter line to signify start of description
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001846 if ($1 eq "" &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 ($section =~ m/^@/ || $section eq $section_context)) {
1848 dump_section($section, xml_escape($contents));
1849 $section = $section_default;
1850 $contents = "";
1851 } else {
1852 $contents .= $1."\n";
1853 }
1854 } else {
1855 # i dont know - bad line? ignore.
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001856 print STDERR "Warning(${file}:$.): bad line: $_";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 ++$warnings;
1858 }
Randy Dunlap232acbc2006-06-25 05:47:48 -07001859 } elsif ($state == 3) { # scanning for function '{' (end of prototype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 if ($decl_type eq 'function') {
Randy Dunlap3c308792007-05-08 00:24:39 -07001861 process_state3_function($_, $file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 } else {
Randy Dunlap3c308792007-05-08 00:24:39 -07001863 process_state3_type($_, $file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 }
1865 } elsif ($state == 4) {
1866 # Documentation block
Randy Dunlap3c308792007-05-08 00:24:39 -07001867 if (/$doc_block/) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 dump_section($section, $contents);
1869 output_intro({'sectionlist' => \@sectionlist,
1870 'sections' => \%sections });
1871 $contents = "";
1872 $function = "";
1873 %constants = ();
1874 %parameterdescs = ();
1875 %parametertypes = ();
1876 @parameterlist = ();
1877 %sections = ();
1878 @sectionlist = ();
1879 $prototype = "";
1880 if ( $1 eq "" ) {
1881 $section = $section_intro;
1882 } else {
1883 $section = $1;
1884 }
Randy Dunlap3c308792007-05-08 00:24:39 -07001885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 elsif (/$doc_end/)
1887 {
1888 dump_section($section, $contents);
1889 output_intro({'sectionlist' => \@sectionlist,
1890 'sections' => \%sections });
1891 $contents = "";
1892 $function = "";
1893 %constants = ();
1894 %parameterdescs = ();
1895 %parametertypes = ();
1896 @parameterlist = ();
1897 %sections = ();
1898 @sectionlist = ();
1899 $prototype = "";
1900 $state = 0;
1901 }
1902 elsif (/$doc_content/)
1903 {
1904 if ( $1 eq "" )
1905 {
1906 $contents .= $blankline;
1907 }
1908 else
1909 {
1910 $contents .= $1 . "\n";
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001911 }
Randy Dunlap3c308792007-05-08 00:24:39 -07001912 }
1913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 }
1915 if ($initial_section_counter == $section_counter) {
1916 print STDERR "Warning(${file}): no structured comments found\n";
1917 if ($output_mode eq "xml") {
1918 # The template wants at least one RefEntry here; make one.
1919 print "<refentry>\n";
1920 print " <refnamediv>\n";
1921 print " <refname>\n";
1922 print " ${file}\n";
1923 print " </refname>\n";
1924 print " <refpurpose>\n";
1925 print " Document generation inconsistency\n";
1926 print " </refpurpose>\n";
1927 print " </refnamediv>\n";
1928 print " <refsect1>\n";
1929 print " <title>\n";
1930 print " Oops\n";
1931 print " </title>\n";
1932 print " <warning>\n";
1933 print " <para>\n";
1934 print " The template for this document tried to insert\n";
1935 print " the structured comment from the file\n";
1936 print " <filename>${file}</filename> at this point,\n";
1937 print " but none was found.\n";
1938 print " This dummy section is inserted to allow\n";
1939 print " generation to continue.\n";
1940 print " </para>\n";
1941 print " </warning>\n";
1942 print " </refsect1>\n";
1943 print "</refentry>\n";
1944 }
1945 }
1946}