kernel-doc 76.1 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7
#!/usr/bin/perl -w

use strict;

## Copyright (c) 1998 Michael Zucchi, All Rights Reserved        ##
## Copyright (C) 2000, 1  Tim Waugh <twaugh@redhat.com>          ##
## Copyright (C) 2001  Simon Huggins                             ##
8
## Copyright (C) 2005-2012  Randy Dunlap                         ##
9
## Copyright (C) 2012  Dan Luedtke                               ##
Linus Torvalds's avatar
Linus Torvalds committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
## 								 ##
## #define enhancements by Armin Kuster <akuster@mvista.com>	 ##
## Copyright (c) 2000 MontaVista Software, Inc.			 ##
## 								 ##
## This software falls under the GNU General Public License.     ##
## Please read the COPYING file for more information             ##

# 18/01/2001 - 	Cleanups
# 		Functions prototyped as foo(void) same as foo()
# 		Stop eval'ing where we don't need to.
# -- huggie@earth.li

# 27/06/2001 -  Allowed whitespace after initial "/**" and
#               allowed comments before function declarations.
# -- Christian Kreibich <ck@whoop.org>

# Still to do:
# 	- add perldoc documentation
# 	- Look more closely at some of the scarier bits :)

# 26/05/2001 - 	Support for separate source and object trees.
#		Return error code.
# 		Keith Owens <kaos@ocs.com.au>

# 23/09/2001 - Added support for typedefs, structs, enums and unions
#              Support for Context section; can be terminated using empty line
#              Small fixes (like spaces vs. \s in regex)
# -- Tim Jansen <tim@tjansen.de>

39 40
# 25/07/2012 - Added support for HTML5
# -- Dan Luedtke <mail@danrl.de>
Linus Torvalds's avatar
Linus Torvalds committed
41 42 43 44 45 46 47 48 49

#
# This will read a 'c' file and scan for embedded comments in the
# style of gnome comments (+minor extensions - see below).
#

# Note: This only supports 'c'.

# usage:
50 51 52 53
# kernel-doc [ -docbook | -html | -html5 | -text | -man | -list ]
#            [ -no-doc-sections ]
#            [ -function funcname [ -function funcname ...] ]
#            c file(s)s > outputfile
Linus Torvalds's avatar
Linus Torvalds committed
54
# or
55 56
#            [ -nofunction funcname [ -function funcname ...] ]
#            c file(s)s > outputfile
Linus Torvalds's avatar
Linus Torvalds committed
57
#
58 59
#  Set output format using one of -docbook -html -html5 -text or -man.
#  Default is man.
60
#  The -list format is for internal use by docproc.
Linus Torvalds's avatar
Linus Torvalds committed
61
#
62 63 64
#  -no-doc-sections
#	Do not output DOC: sections
#
Linus Torvalds's avatar
Linus Torvalds committed
65
#  -function funcname
66 67
#	If set, then only generate documentation for the given function(s) or
#	DOC: section titles.  All other functions and DOC: sections are ignored.
Linus Torvalds's avatar
Linus Torvalds committed
68 69
#
#  -nofunction funcname
70 71 72
#	If set, then only generate documentation for the other function(s)/DOC:
#	sections. Cannot be used together with -function (yes, that's a bug --
#	perl hackers can fix it 8))
Linus Torvalds's avatar
Linus Torvalds committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
#
#  c files - list of 'c' files to process
#
#  All output goes to stdout, with errors to stderr.

#
# format of comments.
# In the following table, (...)? signifies optional structure.
#                         (...)* signifies 0 or more structure elements
# /**
#  * function_name(:)? (- short description)?
# (* @parameterx: (description of parameter x)?)*
# (* a blank line)?
#  * (Description:)? (Description of function)?
#  * (section header: (section description)? )*
#  (*)?*/
#
# So .. the trivial example would be:
#
# /**
#  * my_function
Randy Dunlap's avatar
Randy Dunlap committed
94
#  */
Linus Torvalds's avatar
Linus Torvalds committed
95
#
96
# If the Description: header tag is omitted, then there must be a blank line
Linus Torvalds's avatar
Linus Torvalds committed
97 98 99 100 101 102
# after the last parameter specification.
# e.g.
# /**
#  * my_function - does my stuff
#  * @my_arg: its mine damnit
#  *
103
#  * Does my stuff explained.
Linus Torvalds's avatar
Linus Torvalds committed
104 105 106 107 108 109
#  */
#
#  or, could also use:
# /**
#  * my_function - does my stuff
#  * @my_arg: its mine damnit
110
#  * Description: Does my stuff explained.
Linus Torvalds's avatar
Linus Torvalds committed
111 112 113
#  */
# etc.
#
Randy Dunlap's avatar
Randy Dunlap committed
114
# Besides functions you can also write documentation for structs, unions,
115 116 117
# enums and typedefs. Instead of the function name you must write the name
# of the declaration;  the struct/union/enum/typedef must always precede
# the name. Nesting of declarations is not supported.
Linus Torvalds's avatar
Linus Torvalds committed
118 119 120 121 122 123
# Use the argument mechanism to document members or constants.
# e.g.
# /**
#  * struct my_struct - short description
#  * @a: first member
#  * @b: second member
124
#  *
Linus Torvalds's avatar
Linus Torvalds committed
125 126 127 128 129
#  * Longer description
#  */
# struct my_struct {
#     int a;
#     int b;
130 131
# /* private: */
#     int c;
Linus Torvalds's avatar
Linus Torvalds committed
132 133 134
# };
#
# All descriptions can be multiline, except the short function description.
135
#
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
# For really longs structs, you can also describe arguments inside the
# body of the struct.
# eg.
# /**
#  * struct my_struct - short description
#  * @a: first member
#  * @b: second member
#  *
#  * Longer description
#  */
# struct my_struct {
#     int a;
#     int b;
#     /**
#      * @c: This is longer description of C
#      *
#      * You can use paragraphs to describe arguments
#      * using this method.
#      */
#     int c;
# };
#
# This should be use only for struct/enum members.
#
160 161
# You can also add additional sections. When documenting kernel functions you
# should document the "Context:" of the function, e.g. whether the functions
Linus Torvalds's avatar
Linus Torvalds committed
162
# can be called form interrupts. Unlike other sections you can end it with an
163
# empty line.
164 165
# A non-void function should have a "Return:" section describing the return
# value(s).
166
# Example-sections should contain the string EXAMPLE so that they are marked
Linus Torvalds's avatar
Linus Torvalds committed
167 168 169 170 171 172 173
# appropriately in DocBook.
#
# Example:
# /**
#  * user_function - function that can only be called in user context
#  * @a: some argument
#  * Context: !in_interrupt()
174
#  *
Linus Torvalds's avatar
Linus Torvalds committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
#  * Some description
#  * Example:
#  *    user_function(22);
#  */
# ...
#
#
# All descriptive text is further processed, scanning for the following special
# patterns, which are highlighted appropriately.
#
# 'funcname()' - function
# '$ENVVAR' - environmental variable
# '&struct_name' - name of a structure (up to two words including 'struct')
# '@parameter' - name of a parameter
# '%CONST' - name of a constant.

Randy Dunlap's avatar
Randy Dunlap committed
191 192
## init lots of data

Linus Torvalds's avatar
Linus Torvalds committed
193 194
my $errors = 0;
my $warnings = 0;
195
my $anon_struct_union = 0;
Linus Torvalds's avatar
Linus Torvalds committed
196 197 198 199 200

# match expressions used to find embedded type information
my $type_constant = '\%([-_\w]+)';
my $type_func = '(\w+)\(\)';
my $type_param = '\@(\w+)';
201
my $type_struct = '\&((struct\s*)*[_\w]+)';
202
my $type_struct_xml = '\\&amp;((struct\s*)*[_\w]+)';
Linus Torvalds's avatar
Linus Torvalds committed
203 204 205 206 207 208
my $type_env = '(\$\w+)';

# Output conversion substitutions.
#  One for each output format

# these work fairly well
209 210 211 212 213 214 215
my @highlights_html = (
                       [$type_constant, "<i>\$1</i>"],
                       [$type_func, "<b>\$1</b>"],
                       [$type_struct_xml, "<i>\$1</i>"],
                       [$type_env, "<b><i>\$1</i></b>"],
                       [$type_param, "<tt><b>\$1</b></tt>"]
                      );
216 217 218
my $local_lt = "\\\\\\\\lt:";
my $local_gt = "\\\\\\\\gt:";
my $blankline_html = $local_lt . "p" . $local_gt;	# was "<p>"
Linus Torvalds's avatar
Linus Torvalds committed
219

220
# html version 5
221 222 223 224 225 226 227
my @highlights_html5 = (
                        [$type_constant, "<span class=\"const\">\$1</span>"],
                        [$type_func, "<span class=\"func\">\$1</span>"],
                        [$type_struct_xml, "<span class=\"struct\">\$1</span>"],
                        [$type_env, "<span class=\"env\">\$1</span>"],
                        [$type_param, "<span class=\"param\">\$1</span>]"]
		       );
228 229
my $blankline_html5 = $local_lt . "br /" . $local_gt;

Linus Torvalds's avatar
Linus Torvalds committed
230
# XML, docbook format
231 232 233 234 235 236 237 238
my @highlights_xml = (
                      ["([^=])\\\"([^\\\"<]+)\\\"", "\$1<quote>\$2</quote>"],
                      [$type_constant, "<constant>\$1</constant>"],
                      [$type_struct_xml, "<structname>\$1</structname>"],
                      [$type_param, "<parameter>\$1</parameter>"],
                      [$type_func, "<function>\$1</function>"],
                      [$type_env, "<envar>\$1</envar>"]
		     );
239
my $blankline_xml = $local_lt . "/para" . $local_gt . $local_lt . "para" . $local_gt . "\n";
Linus Torvalds's avatar
Linus Torvalds committed
240 241

# gnome, docbook format
242 243 244 245 246 247 248
my @highlights_gnome = (
                        [$type_constant, "<replaceable class=\"option\">\$1</replaceable>"],
                        [$type_func, "<function>\$1</function>"],
                        [$type_struct, "<structname>\$1</structname>"],
                        [$type_env, "<envar>\$1</envar>"],
                        [$type_param, "<parameter>\$1</parameter>" ]
		       );
Linus Torvalds's avatar
Linus Torvalds committed
249 250 251
my $blankline_gnome = "</para><para>\n";

# these are pretty rough
252 253 254 255 256 257
my @highlights_man = (
                      [$type_constant, "\$1"],
                      [$type_func, "\\\\fB\$1\\\\fP"],
                      [$type_struct, "\\\\fI\$1\\\\fP"],
                      [$type_param, "\\\\fI\$1\\\\fP"]
		     );
Linus Torvalds's avatar
Linus Torvalds committed
258 259 260
my $blankline_man = "";

# text-mode
261 262 263 264 265 266
my @highlights_text = (
                       [$type_constant, "\$1"],
                       [$type_func, "\$1"],
                       [$type_struct, "\$1"],
                       [$type_param, "\$1"]
		      );
Linus Torvalds's avatar
Linus Torvalds committed
267 268
my $blankline_text = "";

269
# list mode
270 271 272 273 274 275
my @highlights_list = (
                       [$type_constant, "\$1"],
                       [$type_func, "\$1"],
                       [$type_struct, "\$1"],
                       [$type_param, "\$1"]
		      );
276
my $blankline_list = "";
Linus Torvalds's avatar
Linus Torvalds committed
277 278

# read arguments
Randy Dunlap's avatar
Randy Dunlap committed
279
if ($#ARGV == -1) {
Linus Torvalds's avatar
Linus Torvalds committed
280 281 282
    usage();
}

Randy Dunlap's avatar
Randy Dunlap committed
283 284 285
my $kernelversion;
my $dohighlight = "";

Linus Torvalds's avatar
Linus Torvalds committed
286 287
my $verbose = 0;
my $output_mode = "man";
288
my $output_preformatted = 0;
289
my $no_doc_sections = 0;
290
my @highlights = @highlights_man;
Linus Torvalds's avatar
Linus Torvalds committed
291 292 293
my $blankline = $blankline_man;
my $modulename = "Kernel API";
my $function_only = 0;
294 295 296 297 298 299 300 301 302 303
my $show_not_found = 0;

my @build_time;
if (defined($ENV{'KBUILD_BUILD_TIMESTAMP'}) &&
    (my $seconds = `date -d"${ENV{'KBUILD_BUILD_TIMESTAMP'}}" +%s`) ne '') {
    @build_time = gmtime($seconds);
} else {
    @build_time = localtime;
}

304 305
my $man_date = ('January', 'February', 'March', 'April', 'May', 'June',
		'July', 'August', 'September', 'October',
306 307
		'November', 'December')[$build_time[4]] .
  " " . ($build_time[5]+1900);
Linus Torvalds's avatar
Linus Torvalds committed
308

Randy Dunlap's avatar
Randy Dunlap committed
309
# Essentially these are globals.
Randy Dunlap's avatar
Randy Dunlap committed
310 311
# They probably want to be tidied up, made more localised or something.
# CAVEAT EMPTOR!  Some of the others I localised may not want to be, which
Linus Torvalds's avatar
Linus Torvalds committed
312
# could cause "use of undefined value" or other bugs.
Randy Dunlap's avatar
Randy Dunlap committed
313 314
my ($function, %function_table, %parametertypes, $declaration_purpose);
my ($type, $declaration_name, $return_type);
315
my ($newsection, $newcontents, $prototype, $brcount, %source_map);
Linus Torvalds's avatar
Linus Torvalds committed
316

317 318 319 320
if (defined($ENV{'KBUILD_VERBOSE'})) {
	$verbose = "$ENV{'KBUILD_VERBOSE'}";
}

321
# Generated docbook code is inserted in a template at a point where
Linus Torvalds's avatar
Linus Torvalds committed
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
# docbook v3.1 requires a non-zero sequence of RefEntry's; see:
# http://www.oasis-open.org/docbook/documentation/reference/html/refentry.html
# We keep track of number of generated entries and generate a dummy
# if needs be to ensure the expanded template can be postprocessed
# into html.
my $section_counter = 0;

my $lineprefix="";

# states
# 0 - normal code
# 1 - looking for function name
# 2 - scanning field start.
# 3 - scanning prototype.
# 4 - documentation block
337
# 5 - gathering documentation outside main block
Linus Torvalds's avatar
Linus Torvalds committed
338
my $state;
339
my $in_doc_sect;
Linus Torvalds's avatar
Linus Torvalds committed
340

341 342 343 344 345 346 347 348 349
# Split Doc State
# 0 - Invalid (Before start or after finish)
# 1 - Is started (the /** was found inside a struct)
# 2 - The @parameter header was found, start accepting multi paragraph text.
# 3 - Finished (the */ was found)
# 4 - Error - Comment without header was found. Spit a warning as it's not
#     proper kernel-doc and ignore the rest.
my $split_doc_state;

Linus Torvalds's avatar
Linus Torvalds committed
350 351 352 353 354 355 356 357 358
#declaration types: can be
# 'function', 'struct', 'union', 'enum', 'typedef'
my $decl_type;

my $doc_special = "\@\%\$\&";

my $doc_start = '^/\*\*\s*$'; # Allow whitespace at end of comment start.
my $doc_end = '\*/';
my $doc_com = '\s*\*\s*';
359
my $doc_com_body = '\s*\* ?';
Randy Dunlap's avatar
Randy Dunlap committed
360 361
my $doc_decl = $doc_com . '(\w+)';
my $doc_sect = $doc_com . '([' . $doc_special . ']?[\w\s]+):(.*)';
362
my $doc_content = $doc_com_body . '(.*)';
Randy Dunlap's avatar
Randy Dunlap committed
363
my $doc_block = $doc_com . 'DOC:\s*(.*)?';
364 365 366
my $doc_split_start = '^\s*/\*\*\s*$';
my $doc_split_sect = '\s*\*\s*(@[\w\s]+):(.*)';
my $doc_split_end = '^\s*\*/\s*$';
Linus Torvalds's avatar
Linus Torvalds committed
367 368 369 370 371 372

my %constants;
my %parameterdescs;
my @parameterlist;
my %sections;
my @sectionlist;
373 374
my $sectcheck;
my $struct_actual;
Linus Torvalds's avatar
Linus Torvalds committed
375 376 377 378 379 380

my $contents = "";
my $section_default = "Description";	# default section
my $section_intro = "Introduction";
my $section = $section_default;
my $section_context = "Context";
381
my $section_return = "Return";
Linus Torvalds's avatar
Linus Torvalds committed
382 383 384 385 386 387 388 389 390

my $undescribed = "-- undescribed --";

reset_state();

while ($ARGV[0] =~ m/^-(.*)/) {
    my $cmd = shift @ARGV;
    if ($cmd eq "-html") {
	$output_mode = "html";
391
	@highlights = @highlights_html;
Linus Torvalds's avatar
Linus Torvalds committed
392
	$blankline = $blankline_html;
393 394
    } elsif ($cmd eq "-html5") {
	$output_mode = "html5";
395
	@highlights = @highlights_html5;
396
	$blankline = $blankline_html5;
Linus Torvalds's avatar
Linus Torvalds committed
397 398
    } elsif ($cmd eq "-man") {
	$output_mode = "man";
399
	@highlights = @highlights_man;
Linus Torvalds's avatar
Linus Torvalds committed
400 401 402
	$blankline = $blankline_man;
    } elsif ($cmd eq "-text") {
	$output_mode = "text";
403
	@highlights = @highlights_text;
Linus Torvalds's avatar
Linus Torvalds committed
404 405 406
	$blankline = $blankline_text;
    } elsif ($cmd eq "-docbook") {
	$output_mode = "xml";
407
	@highlights = @highlights_xml;
Linus Torvalds's avatar
Linus Torvalds committed
408
	$blankline = $blankline_xml;
409 410
    } elsif ($cmd eq "-list") {
	$output_mode = "list";
411
	@highlights = @highlights_list;
412
	$blankline = $blankline_list;
Linus Torvalds's avatar
Linus Torvalds committed
413 414
    } elsif ($cmd eq "-gnome") {
	$output_mode = "gnome";
415
	@highlights = @highlights_gnome;
Linus Torvalds's avatar
Linus Torvalds committed
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
	$blankline = $blankline_gnome;
    } elsif ($cmd eq "-module") { # not needed for XML, inherits from calling document
	$modulename = shift @ARGV;
    } elsif ($cmd eq "-function") { # to only output specific functions
	$function_only = 1;
	$function = shift @ARGV;
	$function_table{$function} = 1;
    } elsif ($cmd eq "-nofunction") { # to only output specific functions
	$function_only = 2;
	$function = shift @ARGV;
	$function_table{$function} = 1;
    } elsif ($cmd eq "-v") {
	$verbose = 1;
    } elsif (($cmd eq "-h") || ($cmd eq "--help")) {
	usage();
431 432
    } elsif ($cmd eq '-no-doc-sections') {
	    $no_doc_sections = 1;
433 434
    } elsif ($cmd eq '-show-not-found') {
	$show_not_found = 1;
Linus Torvalds's avatar
Linus Torvalds committed
435 436 437
    }
}

Randy Dunlap's avatar
Randy Dunlap committed
438 439 440
# continue execution near EOF;

sub usage {
441
    print "Usage: $0 [ -docbook | -html | -html5 | -text | -man | -list ]\n";
Randy Dunlap's avatar
Randy Dunlap committed
442 443 444
    print "         [ -no-doc-sections ]\n";
    print "         [ -function funcname [ -function funcname ...] ]\n";
    print "         [ -nofunction funcname [ -nofunction funcname ...] ]\n";
445
    print "         [ -v ]\n";
Randy Dunlap's avatar
Randy Dunlap committed
446 447 448 449 450
    print "         c source file(s) > outputfile\n";
    print "         -v : verbose output, more warnings & other info listed\n";
    exit 1;
}

451 452
# get kernel version from env
sub get_kernel_version() {
453
    my $version = 'unknown kernel version';
454 455 456 457 458 459

    if (defined($ENV{'KERNELVERSION'})) {
	$version = $ENV{'KERNELVERSION'};
    }
    return $version;
}
Linus Torvalds's avatar
Linus Torvalds committed
460 461 462 463 464

##
# dumps section contents to arrays/hashes intended for that purpose.
#
sub dump_section {
465
    my $file = shift;
Linus Torvalds's avatar
Linus Torvalds committed
466 467 468 469 470 471 472 473 474 475 476
    my $name = shift;
    my $contents = join "\n", @_;

    if ($name =~ m/$type_constant/) {
	$name = $1;
#	print STDERR "constant section '$1' = '$contents'\n";
	$constants{$name} = $contents;
    } elsif ($name =~ m/$type_param/) {
#	print STDERR "parameter def '$1' = '$contents'\n";
	$name = $1;
	$parameterdescs{$name} = $contents;
477
	$sectcheck = $sectcheck . $name . " ";
478 479 480 481
    } elsif ($name eq "@\.\.\.") {
#	print STDERR "parameter def '...' = '$contents'\n";
	$name = "...";
	$parameterdescs{$name} = $contents;
482
	$sectcheck = $sectcheck . $name . " ";
Linus Torvalds's avatar
Linus Torvalds committed
483 484
    } else {
#	print STDERR "other section '$name' = '$contents'\n";
485
	if (defined($sections{$name}) && ($sections{$name} ne "")) {
486
		print STDERR "${file}:$.: error: duplicate section name '$name'\n";
487 488
		++$errors;
	}
Linus Torvalds's avatar
Linus Torvalds committed
489 490 491 492 493
	$sections{$name} = $contents;
	push @sectionlist, $name;
    }
}

494 495 496 497
##
# dump DOC: section after checking that it should go out
#
sub dump_doc_section {
498
    my $file = shift;
499 500 501
    my $name = shift;
    my $contents = join "\n", @_;

502 503 504 505
    if ($no_doc_sections) {
        return;
    }

506 507 508 509
    if (($function_only == 0) ||
	( $function_only == 1 && defined($function_table{$name})) ||
	( $function_only == 2 && !defined($function_table{$name})))
    {
510
	dump_section($file, $name, $contents);
511 512 513 514 515 516 517
	output_blockhead({'sectionlist' => \@sectionlist,
			  'sections' => \%sections,
			  'module' => $modulename,
			  'content-only' => ($function_only != 0), });
    }
}

Linus Torvalds's avatar
Linus Torvalds committed
518 519 520 521 522 523 524 525
##
# output function
#
# parameterdescs, a hash.
#  function => "function name"
#  parameterlist => @list of parameters
#  parameterdescs => %parameter descriptions
#  sectionlist => @list of sections
526
#  sections => %section descriptions
527
#
Linus Torvalds's avatar
Linus Torvalds committed
528 529 530 531 532 533 534 535 536 537 538

sub output_highlight {
    my $contents = join "\n",@_;
    my $line;

#   DEBUG
#   if (!defined $contents) {
#	use Carp;
#	confess "output_highlight got called with no args?\n";
#   }

539 540
    if ($output_mode eq "html" || $output_mode eq "html5" ||
	$output_mode eq "xml") {
541 542
	$contents = local_unescape($contents);
	# convert data read & converted thru xml_escape() into &xyz; format:
543
	$contents =~ s/\\\\\\/\&/g;
544
    }
545
#   print STDERR "contents b4:$contents\n";
Linus Torvalds's avatar
Linus Torvalds committed
546 547
    eval $dohighlight;
    die $@ if $@;
548 549
#   print STDERR "contents af:$contents\n";

550 551 552 553 554
#   strip whitespaces when generating html5
    if ($output_mode eq "html5") {
	$contents =~ s/^\s+//;
	$contents =~ s/\s+$//;
    }
Linus Torvalds's avatar
Linus Torvalds committed
555
    foreach $line (split "\n", $contents) {
556 557 558
	if (! $output_preformatted) {
	    $line =~ s/^\s*//;
	}
559
	if ($line eq ""){
560 561 562
	    if (! $output_preformatted) {
		print $lineprefix, local_unescape($blankline);
	    }
Linus Torvalds's avatar
Linus Torvalds committed
563
	} else {
564
	    $line =~ s/\\\\\\/\&/g;
565 566 567 568 569
	    if ($output_mode eq "man" && substr($line, 0, 1) eq ".") {
		print "\\&$line";
	    } else {
		print $lineprefix, $line;
	    }
Linus Torvalds's avatar
Linus Torvalds committed
570 571 572 573 574
	}
	print "\n";
    }
}

575
# output sections in html
Linus Torvalds's avatar
Linus Torvalds committed
576 577 578 579 580 581 582 583 584
sub output_section_html(%) {
    my %args = %{$_[0]};
    my $section;

    foreach $section (@{$args{'sectionlist'}}) {
	print "<h3>$section</h3>\n";
	print "<blockquote>\n";
	output_highlight($args{'sections'}{$section});
	print "</blockquote>\n";
585
    }
Linus Torvalds's avatar
Linus Torvalds committed
586 587 588 589 590 591 592
}

# output enum in html
sub output_enum_html(%) {
    my %args = %{$_[0]};
    my ($parameter);
    my $count;
Randy Dunlap's avatar
Randy Dunlap committed
593
    print "<h2>enum " . $args{'enum'} . "</h2>\n";
Linus Torvalds's avatar
Linus Torvalds committed
594

Randy Dunlap's avatar
Randy Dunlap committed
595
    print "<b>enum " . $args{'enum'} . "</b> {<br>\n";
Linus Torvalds's avatar
Linus Torvalds committed
596 597
    $count = 0;
    foreach $parameter (@{$args{'parameterlist'}}) {
Randy Dunlap's avatar
Randy Dunlap committed
598
	print " <b>" . $parameter . "</b>";
Linus Torvalds's avatar
Linus Torvalds committed
599 600 601 602 603 604 605 606 607 608 609
	if ($count != $#{$args{'parameterlist'}}) {
	    $count++;
	    print ",\n";
	}
	print "<br>";
    }
    print "};<br>\n";

    print "<h3>Constants</h3>\n";
    print "<dl>\n";
    foreach $parameter (@{$args{'parameterlist'}}) {
Randy Dunlap's avatar
Randy Dunlap committed
610
	print "<dt><b>" . $parameter . "</b>\n";
Linus Torvalds's avatar
Linus Torvalds committed
611 612 613 614 615 616 617 618
	print "<dd>";
	output_highlight($args{'parameterdescs'}{$parameter});
    }
    print "</dl>\n";
    output_section_html(@_);
    print "<hr>\n";
}

619
# output typedef in html
Linus Torvalds's avatar
Linus Torvalds committed
620 621 622 623
sub output_typedef_html(%) {
    my %args = %{$_[0]};
    my ($parameter);
    my $count;
Randy Dunlap's avatar
Randy Dunlap committed
624
    print "<h2>typedef " . $args{'typedef'} . "</h2>\n";
Linus Torvalds's avatar
Linus Torvalds committed
625

Randy Dunlap's avatar
Randy Dunlap committed
626
    print "<b>typedef " . $args{'typedef'} . "</b>\n";
Linus Torvalds's avatar
Linus Torvalds committed
627 628 629 630 631 632 633 634 635
    output_section_html(@_);
    print "<hr>\n";
}

# output struct in html
sub output_struct_html(%) {
    my %args = %{$_[0]};
    my ($parameter);

Randy Dunlap's avatar
Randy Dunlap committed
636 637
    print "<h2>" . $args{'type'} . " " . $args{'struct'} . " - " . $args{'purpose'} . "</h2>\n";
    print "<b>" . $args{'type'} . " " . $args{'struct'} . "</b> {<br>\n";
Linus Torvalds's avatar
Linus Torvalds committed
638 639 640 641 642 643 644 645
    foreach $parameter (@{$args{'parameterlist'}}) {
	if ($parameter =~ /^#/) {
		print "$parameter<br>\n";
		next;
	}
	my $parameter_name = $parameter;
	$parameter_name =~ s/\[.*//;

646
	($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds's avatar
Linus Torvalds committed
647 648 649
	$type = $args{'parametertypes'}{$parameter};
	if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
	    # pointer-to-function
650
	    print "&nbsp; &nbsp; <i>$1</i><b>$parameter</b>) <i>($2)</i>;<br>\n";
Linus Torvalds's avatar
Linus Torvalds committed
651
	} elsif ($type =~ m/^(.*?)\s*(:.*)/) {
652 653
	    # bitfield
	    print "&nbsp; &nbsp; <i>$1</i> <b>$parameter</b>$2;<br>\n";
Linus Torvalds's avatar
Linus Torvalds committed
654
	} else {
655
	    print "&nbsp; &nbsp; <i>$type</i> <b>$parameter</b>;<br>\n";
Linus Torvalds's avatar
Linus Torvalds committed
656 657 658 659 660 661 662 663 664 665 666 667
	}
    }
    print "};<br>\n";

    print "<h3>Members</h3>\n";
    print "<dl>\n";
    foreach $parameter (@{$args{'parameterlist'}}) {
	($parameter =~ /^#/) && next;

	my $parameter_name = $parameter;
	$parameter_name =~ s/\[.*//;

668
	($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Randy Dunlap's avatar
Randy Dunlap committed
669
	print "<dt><b>" . $parameter . "</b>\n";
Linus Torvalds's avatar
Linus Torvalds committed
670 671 672 673 674 675 676 677 678 679 680 681 682 683
	print "<dd>";
	output_highlight($args{'parameterdescs'}{$parameter_name});
    }
    print "</dl>\n";
    output_section_html(@_);
    print "<hr>\n";
}

# output function in html
sub output_function_html(%) {
    my %args = %{$_[0]};
    my ($parameter, $section);
    my $count;

Randy Dunlap's avatar
Randy Dunlap committed
684 685 686
    print "<h2>" . $args{'function'} . " - " . $args{'purpose'} . "</h2>\n";
    print "<i>" . $args{'functiontype'} . "</i>\n";
    print "<b>" . $args{'function'} . "</b>\n";
Linus Torvalds's avatar
Linus Torvalds committed
687 688 689 690 691 692 693 694
    print "(";
    $count = 0;
    foreach $parameter (@{$args{'parameterlist'}}) {
	$type = $args{'parametertypes'}{$parameter};
	if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
	    # pointer-to-function
	    print "<i>$1</i><b>$parameter</b>) <i>($2)</i>";
	} else {
Randy Dunlap's avatar
Randy Dunlap committed
695
	    print "<i>" . $type . "</i> <b>" . $parameter . "</b>";
Linus Torvalds's avatar
Linus Torvalds committed
696 697 698 699 700 701 702 703 704 705 706 707 708 709
	}
	if ($count != $#{$args{'parameterlist'}}) {
	    $count++;
	    print ",\n";
	}
    }
    print ")\n";

    print "<h3>Arguments</h3>\n";
    print "<dl>\n";
    foreach $parameter (@{$args{'parameterlist'}}) {
	my $parameter_name = $parameter;
	$parameter_name =~ s/\[.*//;

710
	($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Randy Dunlap's avatar
Randy Dunlap committed
711
	print "<dt><b>" . $parameter . "</b>\n";
Linus Torvalds's avatar
Linus Torvalds committed
712 713 714 715 716 717 718 719
	print "<dd>";
	output_highlight($args{'parameterdescs'}{$parameter_name});
    }
    print "</dl>\n";
    output_section_html(@_);
    print "<hr>\n";
}

720 721
# output DOC: block header in html
sub output_blockhead_html(%) {
Linus Torvalds's avatar
Linus Torvalds committed
722 723 724 725 726 727 728 729 730 731 732 733 734
    my %args = %{$_[0]};
    my ($parameter, $section);
    my $count;

    foreach $section (@{$args{'sectionlist'}}) {
	print "<h3>$section</h3>\n";
	print "<ul>\n";
	output_highlight($args{'sections'}{$section});
	print "</ul>\n";
    }
    print "<hr>\n";
}

735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
# output sections in html5
sub output_section_html5(%) {
    my %args = %{$_[0]};
    my $section;

    foreach $section (@{$args{'sectionlist'}}) {
	print "<section>\n";
	print "<h1>$section</h1>\n";
	print "<p>\n";
	output_highlight($args{'sections'}{$section});
	print "</p>\n";
	print "</section>\n";
    }
}

# output enum in html5
sub output_enum_html5(%) {
    my %args = %{$_[0]};
    my ($parameter);
    my $count;
    my $html5id;

    $html5id = $args{'enum'};
    $html5id =~ s/[^a-zA-Z0-9\-]+/_/g;
    print "<article class=\"enum\" id=\"enum:". $html5id . "\">";
    print "<h1>enum " . $args{'enum'} . "</h1>\n";
    print "<ol class=\"code\">\n";
    print "<li>";
    print "<span class=\"keyword\">enum</span> ";
    print "<span class=\"identifier\">" . $args{'enum'} . "</span> {";
    print "</li>\n";
    $count = 0;
    foreach $parameter (@{$args{'parameterlist'}}) {
	print "<li class=\"indent\">";
	print "<span class=\"param\">" . $parameter . "</span>";
	if ($count != $#{$args{'parameterlist'}}) {
	    $count++;
	    print ",";
	}
	print "</li>\n";
    }
    print "<li>};</li>\n";
    print "</ol>\n";

    print "<section>\n";
    print "<h1>Constants</h1>\n";
    print "<dl>\n";
    foreach $parameter (@{$args{'parameterlist'}}) {
	print "<dt>" . $parameter . "</dt>\n";
	print "<dd>";
	output_highlight($args{'parameterdescs'}{$parameter});
	print "</dd>\n";
    }
    print "</dl>\n";
    print "</section>\n";
    output_section_html5(@_);
    print "</article>\n";
}

# output typedef in html5
sub output_typedef_html5(%) {
    my %args = %{$_[0]};
    my ($parameter);
    my $count;
    my $html5id;

    $html5id = $args{'typedef'};
    $html5id =~ s/[^a-zA-Z0-9\-]+/_/g;
    print "<article class=\"typedef\" id=\"typedef:" . $html5id . "\">\n";
    print "<h1>typedef " . $args{'typedef'} . "</h1>\n";

    print "<ol class=\"code\">\n";
    print "<li>";
    print "<span class=\"keyword\">typedef</span> ";
    print "<span class=\"identifier\">" . $args{'typedef'} . "</span>";
    print "</li>\n";
    print "</ol>\n";
    output_section_html5(@_);
    print "</article>\n";
}

# output struct in html5
sub output_struct_html5(%) {
    my %args = %{$_[0]};
    my ($parameter);
    my $html5id;

    $html5id = $args{'struct'};
    $html5id =~ s/[^a-zA-Z0-9\-]+/_/g;
    print "<article class=\"struct\" id=\"struct:" . $html5id . "\">\n";
    print "<hgroup>\n";
    print "<h1>" . $args{'type'} . " " . $args{'struct'} . "</h1>";
    print "<h2>". $args{'purpose'} . "</h2>\n";
    print "</hgroup>\n";
    print "<ol class=\"code\">\n";
    print "<li>";
    print "<span class=\"type\">" . $args{'type'} . "</span> ";
    print "<span class=\"identifier\">" . $args{'struct'} . "</span> {";
    print "</li>\n";
    foreach $parameter (@{$args{'parameterlist'}}) {
	print "<li class=\"indent\">";
	if ($parameter =~ /^#/) {
		print "<span class=\"param\">" . $parameter ."</span>\n";
		print "</li>\n";
		next;
	}
	my $parameter_name = $parameter;
	$parameter_name =~ s/\[.*//;

	($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
	$type = $args{'parametertypes'}{$parameter};
	if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
	    # pointer-to-function
	    print "<span class=\"type\">$1</span> ";
	    print "<span class=\"param\">$parameter</span>";
	    print "<span class=\"type\">)</span> ";
	    print "(<span class=\"args\">$2</span>);";
	} elsif ($type =~ m/^(.*?)\s*(:.*)/) {
	    # bitfield
	    print "<span class=\"type\">$1</span> ";
	    print "<span class=\"param\">$parameter</span>";
	    print "<span class=\"bits\">$2</span>;";
	} else {
	    print "<span class=\"type\">$type</span> ";
	    print "<span class=\"param\">$parameter</span>;";
	}
	print "</li>\n";
    }
    print "<li>};</li>\n";
    print "</ol>\n";

    print "<section>\n";
    print "<h1>Members</h1>\n";
    print "<dl>\n";
    foreach $parameter (@{$args{'parameterlist'}}) {
	($parameter =~ /^#/) && next;

	my $parameter_name = $parameter;
	$parameter_name =~ s/\[.*//;

	($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
	print "<dt>" . $parameter . "</dt>\n";
	print "<dd>";
	output_highlight($args{'parameterdescs'}{$parameter_name});
	print "</dd>\n";
    }
    print "</dl>\n";
    print "</section>\n";
    output_section_html5(@_);
    print "</article>\n";
}

# output function in html5
sub output_function_html5(%) {
    my %args = %{$_[0]};
    my ($parameter, $section);
    my $count;
    my $html5id;

    $html5id = $args{'function'};
    $html5id =~ s/[^a-zA-Z0-9\-]+/_/g;
    print "<article class=\"function\" id=\"func:". $html5id . "\">\n";
    print "<hgroup>\n";
    print "<h1>" . $args{'function'} . "</h1>";
    print "<h2>" . $args{'purpose'} . "</h2>\n";
    print "</hgroup>\n";
    print "<ol class=\"code\">\n";
    print "<li>";
    print "<span class=\"type\">" . $args{'functiontype'} . "</span> ";
    print "<span class=\"identifier\">" . $args{'function'} . "</span> (";
    print "</li>";
    $count = 0;
    foreach $parameter (@{$args{'parameterlist'}}) {
	print "<li class=\"indent\">";
	$type = $args{'parametertypes'}{$parameter};
	if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
	    # pointer-to-function
	    print "<span class=\"type\">$1</span> ";
	    print "<span class=\"param\">$parameter</span>";
	    print "<span class=\"type\">)</span> ";
	    print "(<span class=\"args\">$2</span>)";
	} else {
	    print "<span class=\"type\">$type</span> ";
	    print "<span class=\"param\">$parameter</span>";
	}
	if ($count != $#{$args{'parameterlist'}}) {
	    $count++;
	    print ",";
	}
	print "</li>\n";
    }
    print "<li>)</li>\n";
    print "</ol>\n";

    print "<section>\n";
    print "<h1>Arguments</h1>\n";
    print "<p>\n";
    print "<dl>\n";
    foreach $parameter (@{$args{'parameterlist'}}) {
	my $parameter_name = $parameter;
	$parameter_name =~ s/\[.*//;

	($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
	print "<dt>" . $parameter . "</dt>\n";
	print "<dd>";
	output_highlight($args{'parameterdescs'}{$parameter_name});
	print "</dd>\n";
    }
    print "</dl>\n";
    print "</section>\n";
    output_section_html5(@_);
    print "</article>\n";
}

# output DOC: block header in html5
sub output_blockhead_html5(%) {
    my %args = %{$_[0]};
    my ($parameter, $section);
    my $count;
    my $html5id;

    foreach $section (@{$args{'sectionlist'}}) {
	$html5id = $section;
	$html5id =~ s/[^a-zA-Z0-9\-]+/_/g;
	print "<article class=\"doc\" id=\"doc:". $html5id . "\">\n";
	print "<h1>$section</h1>\n";
	print "<p>\n";
	output_highlight($args{'sections'}{$section});
	print "</p>\n";
    }
    print "</article>\n";
}

Linus Torvalds's avatar
Linus Torvalds committed
968 969
sub output_section_xml(%) {
    my %args = %{$_[0]};
970
    my $section;
Linus Torvalds's avatar
Linus Torvalds committed
971 972 973
    # print out each section
    $lineprefix="   ";
    foreach $section (@{$args{'sectionlist'}}) {
974 975
	print "<refsect1>\n";
	print "<title>$section</title>\n";
Linus Torvalds's avatar
Linus Torvalds committed
976
	if ($section =~ m/EXAMPLE/i) {
977
	    print "<informalexample><programlisting>\n";
978
	    $output_preformatted = 1;
979 980
	} else {
	    print "<para>\n";
Linus Torvalds's avatar
Linus Torvalds committed
981 982
	}
	output_highlight($args{'sections'}{$section});
983
	$output_preformatted = 0;
Linus Torvalds's avatar
Linus Torvalds committed
984
	if ($section =~ m/EXAMPLE/i) {
985 986 987
	    print "</programlisting></informalexample>\n";
	} else {
	    print "</para>\n";
Linus Torvalds's avatar
Linus Torvalds committed
988
	}
989
	print "</refsect1>\n";
Linus Torvalds's avatar
Linus Torvalds committed
990 991 992 993 994 995 996 997 998 999
    }
}

# output function in XML DocBook
sub output_function_xml(%) {
    my %args = %{$_[0]};
    my ($parameter, $section);
    my $count;
    my $id;

Randy Dunlap's avatar
Randy Dunlap committed
1000
    $id = "API-" . $args{'function'};
Linus Torvalds's avatar
Linus Torvalds committed
1001 1002
    $id =~ s/[^A-Za-z0-9]/-/g;

1003
    print "<refentry id=\"$id\">\n";
1004 1005 1006 1007 1008
    print "<refentryinfo>\n";
    print " <title>LINUX</title>\n";
    print " <productname>Kernel Hackers Manual</productname>\n";
    print " <date>$man_date</date>\n";
    print "</refentryinfo>\n";
Linus Torvalds's avatar
Linus Torvalds committed
1009
    print "<refmeta>\n";
Randy Dunlap's avatar
Randy Dunlap committed
1010
    print " <refentrytitle><phrase>" . $args{'function'} . "</phrase></refentrytitle>\n";
1011
    print " <manvolnum>9</manvolnum>\n";
1012
    print " <refmiscinfo class=\"version\">" . $kernelversion . "</refmiscinfo>\n";
Linus Torvalds's avatar
Linus Torvalds committed
1013 1014
    print "</refmeta>\n";
    print "<refnamediv>\n";
Randy Dunlap's avatar
Randy Dunlap committed
1015
    print " <refname>" . $args{'function'} . "</refname>\n";
Linus Torvalds's avatar
Linus Torvalds committed
1016 1017 1018 1019 1020 1021 1022 1023 1024
    print " <refpurpose>\n";
    print "  ";
    output_highlight ($args{'purpose'});
    print " </refpurpose>\n";
    print "</refnamediv>\n";

    print "<refsynopsisdiv>\n";
    print " <title>Synopsis</title>\n";
    print "  <funcsynopsis><funcprototype>\n";
Randy Dunlap's avatar
Randy Dunlap committed
1025 1026
    print "   <funcdef>" . $args{'functiontype'} . " ";
    print "<function>" . $args{'function'} . " </function></funcdef>\n";
Linus Torvalds's avatar
Linus Torvalds committed
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036

    $count = 0;
    if ($#{$args{'parameterlist'}} >= 0) {
	foreach $parameter (@{$args{'parameterlist'}}) {
	    $type = $args{'parametertypes'}{$parameter};
	    if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
		# pointer-to-function
		print "   <paramdef>$1<parameter>$parameter</parameter>)\n";
		print "     <funcparams>$2</funcparams></paramdef>\n";
	    } else {
Randy Dunlap's avatar
Randy Dunlap committed
1037
		print "   <paramdef>" . $type;
Linus Torvalds's avatar
Linus Torvalds committed
1038 1039 1040 1041
		print " <parameter>$parameter</parameter></paramdef>\n";
	    }
	}
    } else {
1042
	print "  <void/>\n";
Linus Torvalds's avatar
Linus Torvalds committed
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
    }
    print "  </funcprototype></funcsynopsis>\n";
    print "</refsynopsisdiv>\n";

    # print parameters
    print "<refsect1>\n <title>Arguments</title>\n";
    if ($#{$args{'parameterlist'}} >= 0) {
	print " <variablelist>\n";
	foreach $parameter (@{$args{'parameterlist'}}) {
	    my $parameter_name = $parameter;
	    $parameter_name =~ s/\[.*//;

	    print "  <varlistentry>\n   <term><parameter>$parameter</parameter></term>\n";
	    print "   <listitem>\n    <para>\n";
	    $lineprefix="     ";
	    output_highlight($args{'parameterdescs'}{$parameter_name});
	    print "    </para>\n   </listitem>\n  </varlistentry>\n";
	}
	print " </variablelist>\n";
    } else {
	print " <para>\n  None\n </para>\n";
    }
    print "</refsect1>\n";

    output_section_xml(@_);
    print "</refentry>\n\n";
}

# output struct in XML DocBook
sub output_struct_xml(%) {
    my %args = %{$_[0]};
    my ($parameter, $section);
    my $id;

Randy Dunlap's avatar
Randy Dunlap committed
1077
    $id = "API-struct-" . $args{'struct'};
Linus Torvalds's avatar
Linus Torvalds committed
1078 1079
    $id =~ s/[^A-Za-z0-9]/-/g;

1080
    print "<refentry id=\"$id\">\n";
1081 1082 1083 1084 1085
    print "<refentryinfo>\n";
    print " <title>LINUX</title>\n";
    print " <productname>Kernel Hackers Manual</productname>\n";
    print " <date>$man_date</date>\n";
    print "</refentryinfo>\n";
Linus Torvalds's avatar
Linus Torvalds committed
1086
    print "<refmeta>\n";
Randy Dunlap's avatar
Randy Dunlap committed
1087
    print " <refentrytitle><phrase>" . $args{'type'} . " " . $args{'struct'} . "</phrase></refentrytitle>\n";
1088
    print " <manvolnum>9</manvolnum>\n";
1089
    print " <refmiscinfo class=\"version\">" . $kernelversion . "</refmiscinfo>\n";
Linus Torvalds's avatar
Linus Torvalds committed
1090 1091
    print "</refmeta>\n";
    print "<refnamediv>\n";
Randy Dunlap's avatar
Randy Dunlap committed
1092
    print " <refname>" . $args{'type'} . " " . $args{'struct'} . "</refname>\n";
Linus Torvalds's avatar
Linus Torvalds committed
1093 1094 1095 1096 1097 1098 1099 1100 1101
    print " <refpurpose>\n";
    print "  ";
    output_highlight ($args{'purpose'});
    print " </refpurpose>\n";
    print "</refnamediv>\n";

    print "<refsynopsisdiv>\n";
    print " <title>Synopsis</title>\n";
    print "  <programlisting>\n";
Randy Dunlap's avatar
Randy Dunlap committed
1102
    print $args{'type'} . " " . $args{'struct'} . " {\n";
Linus Torvalds's avatar
Linus Torvalds committed
1103 1104
    foreach $parameter (@{$args{'parameterlist'}}) {
	if ($parameter =~ /^#/) {
1105 1106 1107 1108 1109
	    my $prm = $parameter;
	    # convert data read & converted thru xml_escape() into &xyz; format:
	    # This allows us to have #define macros interspersed in a struct.
	    $prm =~ s/\\\\\\/\&/g;
	    print "$prm\n";
Linus Torvalds's avatar
Linus Torvalds committed
1110 1111 1112 1113 1114 1115 1116
	    next;
	}

	my $parameter_name = $parameter;
	$parameter_name =~ s/\[.*//;

	defined($args{'parameterdescs'}{$parameter_name}) || next;
1117
	($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds's avatar
Linus Torvalds committed
1118 1119 1120 1121 1122
	$type = $args{'parametertypes'}{$parameter};
	if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
	    # pointer-to-function
	    print "  $1 $parameter) ($2);\n";
	} elsif ($type =~ m/^(.*?)\s*(:.*)/) {
Randy Dunlap's avatar
Randy Dunlap committed
1123
	    # bitfield
Linus Torvalds's avatar
Linus Torvalds committed
1124 1125
	    print "  $1 $parameter$2;\n";
	} else {
Randy Dunlap's avatar
Randy Dunlap committed
1126
	    print "  " . $type . " " . $parameter . ";\n";
Linus Torvalds's avatar
Linus Torvalds committed
1127 1128 1129 1130 1131 1132 1133 1134 1135
	}
    }
    print "};";
    print "  </programlisting>\n";
    print "</refsynopsisdiv>\n";

    print " <refsect1>\n";
    print "  <title>Members</title>\n";

1136
    if ($#{$args{'parameterlist'}} >= 0) {
Linus Torvalds's avatar
Linus Torvalds committed
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
    print "  <variablelist>\n";
    foreach $parameter (@{$args{'parameterlist'}}) {
      ($parameter =~ /^#/) && next;

      my $parameter_name = $parameter;
      $parameter_name =~ s/\[.*//;

      defined($args{'parameterdescs'}{$parameter_name}) || next;
      ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
      print "    <varlistentry>";
      print "      <term>$parameter</term>\n";
      print "      <listitem><para>\n";
      output_highlight($args{'parameterdescs'}{$parameter_name});
      print "      </para></listitem>\n";
      print "    </varlistentry>\n";
    }
    print "  </variablelist>\n";
1154 1155 1156
    } else {
	print " <para>\n  None\n </para>\n";
    }
Linus Torvalds's avatar
Linus Torvalds committed
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
    print " </refsect1>\n";

    output_section_xml(@_);

    print "</refentry>\n\n";
}

# output enum in XML DocBook
sub output_enum_xml(%) {
    my %args = %{$_[0]};
    my ($parameter, $section);
    my $count;
    my $id;

Randy Dunlap's avatar
Randy Dunlap committed
1171
    $id = "API-enum-" . $args{'enum'};
Linus Torvalds's avatar
Linus Torvalds committed
1172 1173
    $id =~ s/[^A-Za-z0-9]/-/g;

1174
    print "<refentry id=\"$id\">\n";
1175 1176 1177 1178 1179
    print "<refentryinfo>\n";
    print " <title>LINUX</title>\n";
    print " <productname>Kernel Hackers Manual</productname>\n";
    print " <date>$man_date</date>\n";
    print "</refentryinfo>\n";
Linus Torvalds's avatar
Linus Torvalds committed
1180
    print "<refmeta>\n";
Randy Dunlap's avatar
Randy Dunlap committed
1181
    print " <refentrytitle><phrase>enum " . $args{'enum'} . "</phrase></refentrytitle>\n";
1182
    print " <manvolnum>9</manvolnum>\n";
1183
    print " <refmiscinfo class=\"version\">" . $kernelversion . "</refmiscinfo>\n";
Linus Torvalds's avatar
Linus Torvalds committed
1184 1185
    print "</refmeta>\n";
    print "<refnamediv>\n";
Randy Dunlap's avatar
Randy Dunlap committed
1186
    print " <refname>enum " . $args{'enum'} . "</refname>\n";
Linus Torvalds's avatar
Linus Torvalds committed
1187 1188 1189 1190 1191 1192 1193 1194 1195
    print " <refpurpose>\n";
    print "  ";
    output_highlight ($args{'purpose'});
    print " </refpurpose>\n";
    print "</refnamediv>\n";

    print "<refsynopsisdiv>\n";
    print " <title>Synopsis</title>\n";
    print "  <programlisting>\n";
Randy Dunlap's avatar
Randy Dunlap committed
1196
    print "enum " . $args{'enum'} . " {\n";
Linus Torvalds's avatar
Linus Torvalds committed
1197 1198
    $count = 0;
    foreach $parameter (@{$args{'parameterlist'}}) {
1199 1200
	print "  $parameter";
	if ($count != $#{$args{'parameterlist'}}) {
Linus Torvalds's avatar
Linus Torvalds committed
1201 1202
	    $count++;
	    print ",";
1203
	}
Linus Torvalds's avatar
Linus Torvalds committed
1204 1205 1206 1207 1208 1209 1210
	print "\n";
    }
    print "};";
    print "  </programlisting>\n";
    print "</refsynopsisdiv>\n";

    print "<refsect1>\n";
1211
    print " <title>Constants</title>\n";
Linus Torvalds's avatar
Linus Torvalds committed
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
    print "  <variablelist>\n";
    foreach $parameter (@{$args{'parameterlist'}}) {
      my $parameter_name = $parameter;
      $parameter_name =~ s/\[.*//;

      print "    <varlistentry>";
      print "      <term>$parameter</term>\n";
      print "      <listitem><para>\n";
      output_highlight($args{'parameterdescs'}{$parameter_name});
      print "      </para></listitem>\n";
      print "    </varlistentry>\n";
    }
    print "  </variablelist>\n";
    print "</refsect1>\n";

    output_section_xml(@_);

    print "</refentry>\n\n";
}

# output typedef in XML DocBook
sub output_typedef_xml(%) {
    my %args = %{$_[0]};
    my ($parameter, $section);
    my $id;

Randy Dunlap's avatar
Randy Dunlap committed
1238
    $id = "API-typedef-" . $args{'typedef'};
Linus Torvalds's avatar
Linus Torvalds committed
1239 1240
    $id =~ s/[^A-Za-z0-9]/-/g;

1241
    print "<refentry id=\"$id\">\n";
1242 1243 1244 1245 1246
    print "<refentryinfo>\n";
    print " <title>LINUX</title>\n";
    print " <productname>Kernel Hackers Manual</productname>\n";
    print " <date>$man_date</date>\n";
    print "</refentryinfo>\n";
Linus Torvalds's avatar
Linus Torvalds committed
1247
    print "<refmeta>\n";
Randy Dunlap's avatar
Randy Dunlap committed
1248
    print " <refentrytitle><phrase>typedef " . $args{'typedef'} . "</phrase></refentrytitle>\n";
1249
    print " <manvolnum>9</manvolnum>\n";
Linus Torvalds's avatar
Linus Torvalds committed
1250 1251
    print "</refmeta>\n";
    print "<refnamediv>\n";
Randy Dunlap's avatar
Randy Dunlap committed
1252
    print " <refname>typedef " . $args{'typedef'} . "</refname>\n";
Linus Torvalds's avatar
Linus Torvalds committed
1253 1254 1255 1256 1257 1258 1259 1260
    print " <refpurpose>\n";
    print "  ";
    output_highlight ($args{'purpose'});
    print " </refpurpose>\n";
    print "</refnamediv>\n";

    print "<refsynopsisdiv>\n";
    print " <title>Synopsis</title>\n";
Randy Dunlap's avatar
Randy Dunlap committed
1261
    print "  <synopsis>typedef " . $args{'typedef'} . ";</synopsis>\n";
Linus Torvalds's avatar
Linus Torvalds committed
1262 1263 1264 1265 1266 1267 1268 1269
    print "</refsynopsisdiv>\n";

    output_section_xml(@_);

    print "</refentry>\n\n";
}

# output in XML DocBook
1270
sub output_blockhead_xml(%) {
Linus Torvalds's avatar
Linus Torvalds committed
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
    my %args = %{$_[0]};
    my ($parameter, $section);
    my $count;

    my $id = $args{'module'};
    $id =~ s/[^A-Za-z0-9]/-/g;

    # print out each section
    $lineprefix="   ";
    foreach $section (@{$args{'sectionlist'}}) {
1281 1282 1283
	if (!$args{'content-only'}) {
		print "<refsect1>\n <title>$section</title>\n";
	}
Linus Torvalds's avatar
Linus Torvalds committed
1284 1285
	if ($section =~ m/EXAMPLE/i) {
	    print "<example><para>\n";
1286
	    $output_preformatted = 1;
1287 1288
	} else {
	    print "<para>\n";
Linus Torvalds's avatar
Linus Torvalds committed
1289 1290
	}
	output_highlight($args{'sections'}{$section});
1291
	$output_preformatted = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1292 1293
	if ($section =~ m/EXAMPLE/i) {
	    print "</para></example>\n";
1294 1295 1296 1297 1298
	} else {
	    print "</para>";
	}
	if (!$args{'content-only'}) {
		print "\n</refsect1>\n";
Linus Torvalds's avatar
Linus Torvalds committed
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
	}
    }

    print "\n\n";
}

# output in XML DocBook
sub output_function_gnome {
    my %args = %{$_[0]};
    my ($parameter, $section);
    my $count;
    my $id;

Randy Dunlap's avatar
Randy Dunlap committed
1312
    $id = $args{'module'} . "-" . $args{'function'};
Linus Torvalds's avatar
Linus Torvalds committed
1313 1314 1315
    $id =~ s/[^A-Za-z0-9]/-/g;

    print "<sect2>\n";
Randy Dunlap's avatar
Randy Dunlap committed
1316
    print " <title id=\"$id\">" . $args{'function'} . "</title>\n";
Linus Torvalds's avatar
Linus Torvalds committed
1317 1318

    print "  <funcsynopsis>\n";
Randy Dunlap's avatar
Randy Dunlap committed
1319 1320
    print "   <funcdef>" . $args{'functiontype'} . " ";
    print "<function>" . $args{'function'} . " ";
Linus Torvalds's avatar
Linus Torvalds committed
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
    print "</function></funcdef>\n";

    $count = 0;
    if ($#{$args{'parameterlist'}} >= 0) {
	foreach $parameter (@{$args{'parameterlist'}}) {
	    $type = $args{'parametertypes'}{$parameter};
	    if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
		# pointer-to-function
		print "   <paramdef>$1 <parameter>$parameter</parameter>)\n";
		print "     <funcparams>$2</funcparams></paramdef>\n";
	    } else {
Randy Dunlap's avatar
Randy Dunlap committed
1332
		print "   <paramdef>" . $type;
Linus Torvalds's avatar
Linus Torvalds committed
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
		print " <parameter>$parameter</parameter></paramdef>\n";
	    }
	}
    } else {
	print "  <void>\n";
    }
    print "  </funcsynopsis>\n";
    if ($#{$args{'parameterlist'}} >= 0) {
	print " <informaltable pgwide=\"1\" frame=\"none\" role=\"params\">\n";
	print "<tgroup cols=\"2\">\n";
	print "<colspec colwidth=\"2*\">\n";
	print "<colspec colwidth=\"8*\">\n";
	print "<tbody>\n";
	foreach $parameter (@{$args{'parameterlist'}}) {
	    my $parameter_name = $parameter;
	    $parameter_name =~ s/\[.*//;

	    print "  <row><entry align=\"right\"><parameter>$parameter</parameter></entry>\n";
	    print "   <entry>\n";
	    $lineprefix="     ";
	    output_highlight($args{'parameterdescs'}{$parameter_name});
	    print "    </entry></row>\n";
	}
	print " </tbody></tgroup></informaltable>\n";
    } else {
	print " <para>\n  None\n </para>\n";
    }

    # print out each section
    $lineprefix="   ";
    foreach $section (@{$args{'sectionlist'}}) {
	print "<simplesect>\n <title>$section</title>\n";
	if ($section =~ m/EXAMPLE/i) {
	    print "<example><programlisting>\n";
1367
	    $output_preformatted = 1;
Linus Torvalds's avatar
Linus Torvalds committed
1368 1369 1370 1371
	} else {
	}
	print "<para>\n";
	output_highlight($args{'sections'}{$section});
1372
	$output_preformatted = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
	print "</para>\n";
	if ($section =~ m/EXAMPLE/i) {
	    print "</programlisting></example>\n";
	} else {
	}
	print " </simplesect>\n";
    }

    print "</sect2>\n\n";
}

##
# output function in man
sub output_function_man(%) {
    my %args = %{$_[0]};
    my ($parameter, $section);
    my $count;

    print ".TH \"$args{'function'}\" 9 \"$args{'function'}\" \"$man_date\" \"Kernel Hacker's Manual\" LINUX\n";

    print ".SH NAME\n";
Randy Dunlap's avatar
Randy Dunlap committed
1394
    print $args{'function'} . " \\- " . $args{'purpose'} . "\n";
Linus Torvalds's avatar
Linus Torvalds committed
1395 1396

    print ".SH SYNOPSIS\n";
1397
    if ($args{'functiontype'} ne "") {
Randy Dunlap's avatar
Randy Dunlap committed
1398
	print ".B \"" . $args{'functiontype'} . "\" " . $args{'function'} . "\n";
1399
    } else {
Randy Dunlap's avatar
Randy Dunlap committed
1400
	print ".B \"" . $args{'function'} . "\n";
1401
    }
Linus Torvalds's avatar
Linus Torvalds committed
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411
    $count = 0;
    my $parenth = "(";
    my $post = ",";
    foreach my $parameter (@{$args{'parameterlist'}}) {
	if ($count == $#{$args{'parameterlist'}}) {
	    $post = ");";
	}
	$type = $args{'parametertypes'}{$parameter};
	if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
	    # pointer-to-function
Randy Dunlap's avatar
Randy Dunlap committed
1412
	    print ".BI \"" . $parenth . $1 . "\" " . $parameter . " \") (" . $2 . ")" . $post . "\"\n";
Linus Torvalds's avatar
Linus Torvalds committed
1413 1414
	} else {
	    $type =~ s/([^\*])$/$1 /;
Randy Dunlap's avatar
Randy Dunlap committed
1415
	    print ".BI \"" . $parenth . $type . "\" " . $parameter . " \"" . $post . "\"\n";
Linus Torvalds's avatar
Linus Torvalds committed
1416 1417 1418 1419 1420 1421 1422 1423 1424 1425
	}
	$count++;
	$parenth = "";
    }

    print ".SH ARGUMENTS\n";
    foreach $parameter (@{$args{'parameterlist'}}) {
	my $parameter_name = $parameter;
	$parameter_name =~ s/\[.*//;

Randy Dunlap's avatar
Randy Dunlap committed
1426
	print ".IP \"" . $parameter . "\" 12\n";
Linus Torvalds's avatar
Linus Torvalds committed
1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
	output_highlight($args{'parameterdescs'}{$parameter_name});
    }
    foreach $section (@{$args{'sectionlist'}}) {
	print ".SH \"", uc $section, "\"\n";
	output_highlight($args{'sections'}{$section});
    }
}

##
# output enum in man
sub output_enum_man(%) {
    my %args = %{$_[0]};
    my ($parameter, $section);
    my $count;

    print ".TH \"$args{'module'}\" 9 \"enum $args{'enum'}\" \"$man_date\" \"API Manual\" LINUX\n";

    print ".SH NAME\n";
Randy Dunlap's avatar
Randy Dunlap committed
1445
    print "enum " . $args{'enum'} . " \\- " . $args{'purpose'} . "\n";
Linus Torvalds's avatar
Linus Torvalds committed
1446 1447

    print ".SH SYNOPSIS\n";
Randy Dunlap's avatar
Randy Dunlap committed
1448
    print "enum " . $args{'enum'} . " {\n";
Linus Torvalds's avatar
Linus Torvalds committed
1449 1450
    $count = 0;
    foreach my $parameter (@{$args{'parameterlist'}}) {
1451
	print ".br\n.BI \"    $parameter\"\n";
Linus Torvalds's avatar
Linus Torvalds committed
1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466
	if ($count == $#{$args{'parameterlist'}}) {
	    print "\n};\n";
	    last;
	}
	else {
	    print ", \n.br\n";
	}
	$count++;
    }

    print ".SH Constants\n";
    foreach $parameter (@{$args{'parameterlist'}}) {
	my $parameter_name = $parameter;
	$parameter_name =~ s/\[.*//;

Randy Dunlap's avatar
Randy Dunlap committed
1467
	print ".IP \"" . $parameter . "\" 12\n";
Linus Torvalds's avatar
Linus Torvalds committed
1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
	output_highlight($args{'parameterdescs'}{$parameter_name});
    }
    foreach $section (@{$args{'sectionlist'}}) {
	print ".SH \"$section\"\n";
	output_highlight($args{'sections'}{$section});
    }
}

##
# output struct in man
sub output_struct_man(%) {
    my %args = %{$_[0]};
    my ($parameter, $section);

Randy Dunlap's avatar
Randy Dunlap committed
1482
    print ".TH \"$args{'module'}\" 9 \"" . $args{'type'} . " " . $args{'struct'} . "\" \"$man_date\" \"API Manual\" LINUX\n";
Linus Torvalds's avatar
Linus Torvalds committed
1483 1484

    print ".SH NAME\n";
Randy Dunlap's avatar
Randy Dunlap committed
1485
    print $args{'type'} . " " . $args{'struct'} . " \\- " . $args{'purpose'} . "\n";
Linus Torvalds's avatar
Linus Torvalds committed
1486 1487

    print ".SH SYNOPSIS\n";
Randy Dunlap's avatar
Randy Dunlap committed
1488
    print $args{'type'} . " " . $args{'struct'} . " {\n.br\n";
Linus Torvalds's avatar
Linus Torvalds committed
1489 1490 1491 1492 1493 1494 1495 1496 1497

    foreach my $parameter (@{$args{'parameterlist'}}) {
	if ($parameter =~ /^#/) {
	    print ".BI \"$parameter\"\n.br\n";
	    next;
	}
	my $parameter_name = $parameter;
	$parameter_name =~ s/\[.*//;

1498
	($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Linus Torvalds's avatar
Linus Torvalds committed
1499 1500 1501
	$type = $args{'parametertypes'}{$parameter};
	if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
	    # pointer-to-function
Randy Dunlap's avatar
Randy Dunlap committed
1502
	    print ".BI \"    " . $1 . "\" " . $parameter . " \") (" . $2 . ")" . "\"\n;\n";
Linus Torvalds's avatar
Linus Torvalds committed
1503
	} elsif ($type =~ m/^(.*?)\s*(:.*)/) {
1504
	    # bitfield
Randy Dunlap's avatar
Randy Dunlap committed
1505
	    print ".BI \"    " . $1 . "\ \" " . $parameter . $2 . " \"" . "\"\n;\n";
Linus Torvalds's avatar
Linus Torvalds committed
1506 1507
	} else {
	    $type =~ s/([^\*])$/$1 /;
Randy Dunlap's avatar
Randy Dunlap committed
1508
	    print ".BI \"    " . $type . "\" " . $parameter . " \"" . "\"\n;\n";
Linus Torvalds's avatar
Linus Torvalds committed
1509 1510 1511 1512 1513
	}
	print "\n.br\n";
    }
    print "};\n.br\n";

1514
    print ".SH Members\n";
Linus Torvalds's avatar
Linus Torvalds committed
1515 1516 1517 1518 1519 1520
    foreach $parameter (@{$args{'parameterlist'}}) {
	($parameter =~ /^#/) && next;

	my $parameter_name = $parameter;
	$parameter_name =~ s/\[.*//;

1521
	($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Randy Dunlap's avatar
Randy Dunlap committed
1522
	print ".IP \"" . $parameter . "\" 12\n";
Linus Torvalds's avatar
Linus Torvalds committed
1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539
	output_highlight($args{'parameterdescs'}{$parameter_name});
    }
    foreach $section (@{$args{'sectionlist'}}) {
	print ".SH \"$section\"\n";
	output_highlight($args{'sections'}{$section});
    }
}

##
# output typedef in man
sub output_typedef_man(%) {
    my %args = %{$_[0]};
    my ($parameter, $section);

    print ".TH \"$args{'module'}\" 9 \"$args{'typedef'}\" \"$man_date\" \"API Manual\" LINUX\n";

    print ".SH NAME\n";
Randy Dunlap's avatar
Randy Dunlap committed
1540
    print "typedef " . $args{'typedef'} . " \\- " . $args{'purpose'} . "\n";
Linus Torvalds's avatar
Linus Torvalds committed
1541 1542 1543 1544 1545 1546 1547

    foreach $section (@{$args{'sectionlist'}}) {
	print ".SH \"$section\"\n";
	output_highlight($args{'sections'}{$section});
    }
}

1548
sub output_blockhead_man(%) {
Linus Torvalds's avatar
Linus Torvalds committed
1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565
    my %args = %{$_[0]};
    my ($parameter, $section);
    my $count;

    print ".TH \"$args{'module'}\" 9 \"$args{'module'}\" \"$man_date\" \"API Manual\" LINUX\n";

    foreach $section (@{$args{'sectionlist'}}) {
	print ".SH \"$section\"\n";
	output_highlight($args{'sections'}{$section});
    }
}

##
# output in text
sub output_function_text(%) {
    my %args = %{$_[0]};
    my ($parameter, $section);
1566
    my $start;
Linus Torvalds's avatar
Linus Torvalds committed
1567

1568
    print "Name:\n\n";
Randy Dunlap's avatar
Randy Dunlap committed
1569
    print $args{'function'} . " - " . $args{'purpose'} . "\n";
1570 1571

    print "\nSynopsis:\n\n";
1572
    if ($args{'functiontype'} ne "") {
Randy Dunlap's avatar
Randy Dunlap committed
1573
	$start = $args{'functiontype'} . " " . $args{'function'} . " (";