PageRenderTime 97ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/scripts/kernel-doc

https://bitbucket.org/evzijst/gittest
Perl | 1831 lines | 1348 code | 212 blank | 271 comment | 158 complexity | e732e89cc5343f24ce7b83e9a8b5d079 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0, LGPL-2.0
  1. #!/usr/bin/perl -w
  2. use strict;
  3. ## Copyright (c) 1998 Michael Zucchi, All Rights Reserved ##
  4. ## Copyright (C) 2000, 1 Tim Waugh <twaugh@redhat.com> ##
  5. ## Copyright (C) 2001 Simon Huggins ##
  6. ## ##
  7. ## #define enhancements by Armin Kuster <akuster@mvista.com> ##
  8. ## Copyright (c) 2000 MontaVista Software, Inc. ##
  9. ## ##
  10. ## This software falls under the GNU General Public License. ##
  11. ## Please read the COPYING file for more information ##
  12. # w.o. 03-11-2000: added the '-filelist' option.
  13. # 18/01/2001 - Cleanups
  14. # Functions prototyped as foo(void) same as foo()
  15. # Stop eval'ing where we don't need to.
  16. # -- huggie@earth.li
  17. # 27/06/2001 - Allowed whitespace after initial "/**" and
  18. # allowed comments before function declarations.
  19. # -- Christian Kreibich <ck@whoop.org>
  20. # Still to do:
  21. # - add perldoc documentation
  22. # - Look more closely at some of the scarier bits :)
  23. # 26/05/2001 - Support for separate source and object trees.
  24. # Return error code.
  25. # Keith Owens <kaos@ocs.com.au>
  26. # 23/09/2001 - Added support for typedefs, structs, enums and unions
  27. # Support for Context section; can be terminated using empty line
  28. # Small fixes (like spaces vs. \s in regex)
  29. # -- Tim Jansen <tim@tjansen.de>
  30. #
  31. # This will read a 'c' file and scan for embedded comments in the
  32. # style of gnome comments (+minor extensions - see below).
  33. #
  34. # Note: This only supports 'c'.
  35. # usage:
  36. # kerneldoc [ -docbook | -html | -text | -man ]
  37. # [ -function funcname [ -function funcname ...] ] c file(s)s > outputfile
  38. # or
  39. # [ -nofunction funcname [ -function funcname ...] ] c file(s)s > outputfile
  40. #
  41. # Set output format using one of -docbook -html -text or -man. Default is man.
  42. #
  43. # -function funcname
  44. # If set, then only generate documentation for the given function(s). All
  45. # other functions are ignored.
  46. #
  47. # -nofunction funcname
  48. # If set, then only generate documentation for the other function(s). All
  49. # other functions are ignored. Cannot be used with -function together
  50. # (yes thats a bug - perl hackers can fix it 8))
  51. #
  52. # c files - list of 'c' files to process
  53. #
  54. # All output goes to stdout, with errors to stderr.
  55. #
  56. # format of comments.
  57. # In the following table, (...)? signifies optional structure.
  58. # (...)* signifies 0 or more structure elements
  59. # /**
  60. # * function_name(:)? (- short description)?
  61. # (* @parameterx: (description of parameter x)?)*
  62. # (* a blank line)?
  63. # * (Description:)? (Description of function)?
  64. # * (section header: (section description)? )*
  65. # (*)?*/
  66. #
  67. # So .. the trivial example would be:
  68. #
  69. # /**
  70. # * my_function
  71. # **/
  72. #
  73. # If the Description: header tag is ommitted, then there must be a blank line
  74. # after the last parameter specification.
  75. # e.g.
  76. # /**
  77. # * my_function - does my stuff
  78. # * @my_arg: its mine damnit
  79. # *
  80. # * Does my stuff explained.
  81. # */
  82. #
  83. # or, could also use:
  84. # /**
  85. # * my_function - does my stuff
  86. # * @my_arg: its mine damnit
  87. # * Description: Does my stuff explained.
  88. # */
  89. # etc.
  90. #
  91. # Beside functions you can also write documentation for structs, unions,
  92. # enums and typedefs. Instead of the function name you must write the name
  93. # of the declaration; the struct/union/enum/typedef must always precede
  94. # the name. Nesting of declarations is not supported.
  95. # Use the argument mechanism to document members or constants.
  96. # e.g.
  97. # /**
  98. # * struct my_struct - short description
  99. # * @a: first member
  100. # * @b: second member
  101. # *
  102. # * Longer description
  103. # */
  104. # struct my_struct {
  105. # int a;
  106. # int b;
  107. # };
  108. #
  109. # All descriptions can be multiline, except the short function description.
  110. #
  111. # You can also add additional sections. When documenting kernel functions you
  112. # should document the "Context:" of the function, e.g. whether the functions
  113. # can be called form interrupts. Unlike other sections you can end it with an
  114. # empty line.
  115. # Example-sections should contain the string EXAMPLE so that they are marked
  116. # appropriately in DocBook.
  117. #
  118. # Example:
  119. # /**
  120. # * user_function - function that can only be called in user context
  121. # * @a: some argument
  122. # * Context: !in_interrupt()
  123. # *
  124. # * Some description
  125. # * Example:
  126. # * user_function(22);
  127. # */
  128. # ...
  129. #
  130. #
  131. # All descriptive text is further processed, scanning for the following special
  132. # patterns, which are highlighted appropriately.
  133. #
  134. # 'funcname()' - function
  135. # '$ENVVAR' - environmental variable
  136. # '&struct_name' - name of a structure (up to two words including 'struct')
  137. # '@parameter' - name of a parameter
  138. # '%CONST' - name of a constant.
  139. my $errors = 0;
  140. my $warnings = 0;
  141. # match expressions used to find embedded type information
  142. my $type_constant = '\%([-_\w]+)';
  143. my $type_func = '(\w+)\(\)';
  144. my $type_param = '\@(\w+)';
  145. my $type_struct = '\&((struct\s*)?[_\w]+)';
  146. my $type_env = '(\$\w+)';
  147. # Output conversion substitutions.
  148. # One for each output format
  149. # these work fairly well
  150. my %highlights_html = ( $type_constant, "<i>\$1</i>",
  151. $type_func, "<b>\$1</b>",
  152. $type_struct, "<i>\$1</i>",
  153. $type_param, "<tt><b>\$1</b></tt>" );
  154. my $blankline_html = "<p>";
  155. # XML, docbook format
  156. my %highlights_xml = ( "([^=])\\\"([^\\\"<]+)\\\"", "\$1<quote>\$2</quote>",
  157. $type_constant, "<constant>\$1</constant>",
  158. $type_func, "<function>\$1</function>",
  159. $type_struct, "<structname>\$1</structname>",
  160. $type_env, "<envar>\$1</envar>",
  161. $type_param, "<parameter>\$1</parameter>" );
  162. my $blankline_xml = "</para><para>\n";
  163. # gnome, docbook format
  164. my %highlights_gnome = ( $type_constant, "<replaceable class=\"option\">\$1</replaceable>",
  165. $type_func, "<function>\$1</function>",
  166. $type_struct, "<structname>\$1</structname>",
  167. $type_env, "<envar>\$1</envar>",
  168. $type_param, "<parameter>\$1</parameter>" );
  169. my $blankline_gnome = "</para><para>\n";
  170. # these are pretty rough
  171. my %highlights_man = ( $type_constant, "\$1",
  172. $type_func, "\\\\fB\$1\\\\fP",
  173. $type_struct, "\\\\fI\$1\\\\fP",
  174. $type_param, "\\\\fI\$1\\\\fP" );
  175. my $blankline_man = "";
  176. # text-mode
  177. my %highlights_text = ( $type_constant, "\$1",
  178. $type_func, "\$1",
  179. $type_struct, "\$1",
  180. $type_param, "\$1" );
  181. my $blankline_text = "";
  182. sub usage {
  183. print "Usage: $0 [ -v ] [ -docbook | -html | -text | -man ]\n";
  184. print " [ -function funcname [ -function funcname ...] ]\n";
  185. print " [ -nofunction funcname [ -nofunction funcname ...] ]\n";
  186. print " c source file(s) > outputfile\n";
  187. exit 1;
  188. }
  189. # read arguments
  190. if ($#ARGV==-1) {
  191. usage();
  192. }
  193. my $verbose = 0;
  194. my $output_mode = "man";
  195. my %highlights = %highlights_man;
  196. my $blankline = $blankline_man;
  197. my $modulename = "Kernel API";
  198. my $function_only = 0;
  199. my $man_date = ('January', 'February', 'March', 'April', 'May', 'June',
  200. 'July', 'August', 'September', 'October',
  201. 'November', 'December')[(localtime)[4]] .
  202. " " . ((localtime)[5]+1900);
  203. # Essentially these are globals
  204. # They probably want to be tidied up made more localised or summat.
  205. # CAVEAT EMPTOR! Some of the others I localised may not want to be which
  206. # could cause "use of undefined value" or other bugs.
  207. my ($function, %function_table,%parametertypes,$declaration_purpose);
  208. my ($type,$declaration_name,$return_type);
  209. my ($newsection,$newcontents,$prototype,$filelist, $brcount, %source_map);
  210. # Generated docbook code is inserted in a template at a point where
  211. # docbook v3.1 requires a non-zero sequence of RefEntry's; see:
  212. # http://www.oasis-open.org/docbook/documentation/reference/html/refentry.html
  213. # We keep track of number of generated entries and generate a dummy
  214. # if needs be to ensure the expanded template can be postprocessed
  215. # into html.
  216. my $section_counter = 0;
  217. my $lineprefix="";
  218. # states
  219. # 0 - normal code
  220. # 1 - looking for function name
  221. # 2 - scanning field start.
  222. # 3 - scanning prototype.
  223. # 4 - documentation block
  224. my $state;
  225. #declaration types: can be
  226. # 'function', 'struct', 'union', 'enum', 'typedef'
  227. my $decl_type;
  228. my $doc_special = "\@\%\$\&";
  229. my $doc_start = '^/\*\*\s*$'; # Allow whitespace at end of comment start.
  230. my $doc_end = '\*/';
  231. my $doc_com = '\s*\*\s*';
  232. my $doc_decl = $doc_com.'(\w+)';
  233. my $doc_sect = $doc_com.'(['.$doc_special.']?[\w ]+):(.*)';
  234. my $doc_content = $doc_com.'(.*)';
  235. my $doc_block = $doc_com.'DOC:\s*(.*)?';
  236. my %constants;
  237. my %parameterdescs;
  238. my @parameterlist;
  239. my %sections;
  240. my @sectionlist;
  241. my $contents = "";
  242. my $section_default = "Description"; # default section
  243. my $section_intro = "Introduction";
  244. my $section = $section_default;
  245. my $section_context = "Context";
  246. my $undescribed = "-- undescribed --";
  247. reset_state();
  248. while ($ARGV[0] =~ m/^-(.*)/) {
  249. my $cmd = shift @ARGV;
  250. if ($cmd eq "-html") {
  251. $output_mode = "html";
  252. %highlights = %highlights_html;
  253. $blankline = $blankline_html;
  254. } elsif ($cmd eq "-man") {
  255. $output_mode = "man";
  256. %highlights = %highlights_man;
  257. $blankline = $blankline_man;
  258. } elsif ($cmd eq "-text") {
  259. $output_mode = "text";
  260. %highlights = %highlights_text;
  261. $blankline = $blankline_text;
  262. } elsif ($cmd eq "-docbook") {
  263. $output_mode = "xml";
  264. %highlights = %highlights_xml;
  265. $blankline = $blankline_xml;
  266. } elsif ($cmd eq "-gnome") {
  267. $output_mode = "gnome";
  268. %highlights = %highlights_gnome;
  269. $blankline = $blankline_gnome;
  270. } elsif ($cmd eq "-module") { # not needed for XML, inherits from calling document
  271. $modulename = shift @ARGV;
  272. } elsif ($cmd eq "-function") { # to only output specific functions
  273. $function_only = 1;
  274. $function = shift @ARGV;
  275. $function_table{$function} = 1;
  276. } elsif ($cmd eq "-nofunction") { # to only output specific functions
  277. $function_only = 2;
  278. $function = shift @ARGV;
  279. $function_table{$function} = 1;
  280. } elsif ($cmd eq "-v") {
  281. $verbose = 1;
  282. } elsif (($cmd eq "-h") || ($cmd eq "--help")) {
  283. usage();
  284. } elsif ($cmd eq '-filelist') {
  285. $filelist = shift @ARGV;
  286. }
  287. }
  288. # generate a sequence of code that will splice in highlighting information
  289. # using the s// operator.
  290. my $dohighlight = "";
  291. foreach my $pattern (keys %highlights) {
  292. # print "scanning pattern $pattern ($highlights{$pattern})\n";
  293. $dohighlight .= "\$contents =~ s:$pattern:$highlights{$pattern}:gs;\n";
  294. }
  295. ##
  296. # dumps section contents to arrays/hashes intended for that purpose.
  297. #
  298. sub dump_section {
  299. my $name = shift;
  300. my $contents = join "\n", @_;
  301. if ($name =~ m/$type_constant/) {
  302. $name = $1;
  303. # print STDERR "constant section '$1' = '$contents'\n";
  304. $constants{$name} = $contents;
  305. } elsif ($name =~ m/$type_param/) {
  306. # print STDERR "parameter def '$1' = '$contents'\n";
  307. $name = $1;
  308. $parameterdescs{$name} = $contents;
  309. } else {
  310. # print STDERR "other section '$name' = '$contents'\n";
  311. $sections{$name} = $contents;
  312. push @sectionlist, $name;
  313. }
  314. }
  315. ##
  316. # output function
  317. #
  318. # parameterdescs, a hash.
  319. # function => "function name"
  320. # parameterlist => @list of parameters
  321. # parameterdescs => %parameter descriptions
  322. # sectionlist => @list of sections
  323. # sections => %descriont descriptions
  324. #
  325. sub output_highlight {
  326. my $contents = join "\n",@_;
  327. my $line;
  328. # DEBUG
  329. # if (!defined $contents) {
  330. # use Carp;
  331. # confess "output_highlight got called with no args?\n";
  332. # }
  333. eval $dohighlight;
  334. die $@ if $@;
  335. foreach $line (split "\n", $contents) {
  336. if ($line eq ""){
  337. print $lineprefix, $blankline;
  338. } else {
  339. $line =~ s/\\\\\\/\&/g;
  340. print $lineprefix, $line;
  341. }
  342. print "\n";
  343. }
  344. }
  345. #output sections in html
  346. sub output_section_html(%) {
  347. my %args = %{$_[0]};
  348. my $section;
  349. foreach $section (@{$args{'sectionlist'}}) {
  350. print "<h3>$section</h3>\n";
  351. print "<blockquote>\n";
  352. output_highlight($args{'sections'}{$section});
  353. print "</blockquote>\n";
  354. }
  355. }
  356. # output enum in html
  357. sub output_enum_html(%) {
  358. my %args = %{$_[0]};
  359. my ($parameter);
  360. my $count;
  361. print "<h2>enum ".$args{'enum'}."</h2>\n";
  362. print "<b>enum ".$args{'enum'}."</b> {<br>\n";
  363. $count = 0;
  364. foreach $parameter (@{$args{'parameterlist'}}) {
  365. print " <b>".$parameter."</b>";
  366. if ($count != $#{$args{'parameterlist'}}) {
  367. $count++;
  368. print ",\n";
  369. }
  370. print "<br>";
  371. }
  372. print "};<br>\n";
  373. print "<h3>Constants</h3>\n";
  374. print "<dl>\n";
  375. foreach $parameter (@{$args{'parameterlist'}}) {
  376. print "<dt><b>".$parameter."</b>\n";
  377. print "<dd>";
  378. output_highlight($args{'parameterdescs'}{$parameter});
  379. }
  380. print "</dl>\n";
  381. output_section_html(@_);
  382. print "<hr>\n";
  383. }
  384. # output tyepdef in html
  385. sub output_typedef_html(%) {
  386. my %args = %{$_[0]};
  387. my ($parameter);
  388. my $count;
  389. print "<h2>typedef ".$args{'typedef'}."</h2>\n";
  390. print "<b>typedef ".$args{'typedef'}."</b>\n";
  391. output_section_html(@_);
  392. print "<hr>\n";
  393. }
  394. # output struct in html
  395. sub output_struct_html(%) {
  396. my %args = %{$_[0]};
  397. my ($parameter);
  398. print "<h2>".$args{'type'}." ".$args{'struct'}."</h2>\n";
  399. print "<b>".$args{'type'}." ".$args{'struct'}."</b> {<br>\n";
  400. foreach $parameter (@{$args{'parameterlist'}}) {
  401. if ($parameter =~ /^#/) {
  402. print "$parameter<br>\n";
  403. next;
  404. }
  405. my $parameter_name = $parameter;
  406. $parameter_name =~ s/\[.*//;
  407. ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
  408. $type = $args{'parametertypes'}{$parameter};
  409. if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
  410. # pointer-to-function
  411. print " <i>$1</i><b>$parameter</b>) <i>($2)</i>;<br>\n";
  412. } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
  413. print " <i>$1</i> <b>$parameter</b>$2;<br>\n";
  414. } else {
  415. print " <i>$type</i> <b>$parameter</b>;<br>\n";
  416. }
  417. }
  418. print "};<br>\n";
  419. print "<h3>Members</h3>\n";
  420. print "<dl>\n";
  421. foreach $parameter (@{$args{'parameterlist'}}) {
  422. ($parameter =~ /^#/) && next;
  423. my $parameter_name = $parameter;
  424. $parameter_name =~ s/\[.*//;
  425. ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
  426. print "<dt><b>".$parameter."</b>\n";
  427. print "<dd>";
  428. output_highlight($args{'parameterdescs'}{$parameter_name});
  429. }
  430. print "</dl>\n";
  431. output_section_html(@_);
  432. print "<hr>\n";
  433. }
  434. # output function in html
  435. sub output_function_html(%) {
  436. my %args = %{$_[0]};
  437. my ($parameter, $section);
  438. my $count;
  439. print "<h2>Function</h2>\n";
  440. print "<i>".$args{'functiontype'}."</i>\n";
  441. print "<b>".$args{'function'}."</b>\n";
  442. print "(";
  443. $count = 0;
  444. foreach $parameter (@{$args{'parameterlist'}}) {
  445. $type = $args{'parametertypes'}{$parameter};
  446. if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
  447. # pointer-to-function
  448. print "<i>$1</i><b>$parameter</b>) <i>($2)</i>";
  449. } else {
  450. print "<i>".$type."</i> <b>".$parameter."</b>";
  451. }
  452. if ($count != $#{$args{'parameterlist'}}) {
  453. $count++;
  454. print ",\n";
  455. }
  456. }
  457. print ")\n";
  458. print "<h3>Arguments</h3>\n";
  459. print "<dl>\n";
  460. foreach $parameter (@{$args{'parameterlist'}}) {
  461. my $parameter_name = $parameter;
  462. $parameter_name =~ s/\[.*//;
  463. ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
  464. print "<dt><b>".$parameter."</b>\n";
  465. print "<dd>";
  466. output_highlight($args{'parameterdescs'}{$parameter_name});
  467. }
  468. print "</dl>\n";
  469. output_section_html(@_);
  470. print "<hr>\n";
  471. }
  472. # output intro in html
  473. sub output_intro_html(%) {
  474. my %args = %{$_[0]};
  475. my ($parameter, $section);
  476. my $count;
  477. foreach $section (@{$args{'sectionlist'}}) {
  478. print "<h3>$section</h3>\n";
  479. print "<ul>\n";
  480. output_highlight($args{'sections'}{$section});
  481. print "</ul>\n";
  482. }
  483. print "<hr>\n";
  484. }
  485. sub output_section_xml(%) {
  486. my %args = %{$_[0]};
  487. my $section;
  488. # print out each section
  489. $lineprefix=" ";
  490. foreach $section (@{$args{'sectionlist'}}) {
  491. print "<refsect1>\n <title>$section</title>\n <para>\n";
  492. if ($section =~ m/EXAMPLE/i) {
  493. print "<example><para>\n";
  494. }
  495. output_highlight($args{'sections'}{$section});
  496. if ($section =~ m/EXAMPLE/i) {
  497. print "</para></example>\n";
  498. }
  499. print " </para>\n</refsect1>\n";
  500. }
  501. }
  502. # output function in XML DocBook
  503. sub output_function_xml(%) {
  504. my %args = %{$_[0]};
  505. my ($parameter, $section);
  506. my $count;
  507. my $id;
  508. $id = "API-".$args{'function'};
  509. $id =~ s/[^A-Za-z0-9]/-/g;
  510. print "<refentry>\n";
  511. print "<refmeta>\n";
  512. print "<refentrytitle><phrase id=\"$id\">".$args{'function'}."</phrase></refentrytitle>\n";
  513. print "</refmeta>\n";
  514. print "<refnamediv>\n";
  515. print " <refname>".$args{'function'}."</refname>\n";
  516. print " <refpurpose>\n";
  517. print " ";
  518. output_highlight ($args{'purpose'});
  519. print " </refpurpose>\n";
  520. print "</refnamediv>\n";
  521. print "<refsynopsisdiv>\n";
  522. print " <title>Synopsis</title>\n";
  523. print " <funcsynopsis><funcprototype>\n";
  524. print " <funcdef>".$args{'functiontype'}." ";
  525. print "<function>".$args{'function'}." </function></funcdef>\n";
  526. $count = 0;
  527. if ($#{$args{'parameterlist'}} >= 0) {
  528. foreach $parameter (@{$args{'parameterlist'}}) {
  529. $type = $args{'parametertypes'}{$parameter};
  530. if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
  531. # pointer-to-function
  532. print " <paramdef>$1<parameter>$parameter</parameter>)\n";
  533. print " <funcparams>$2</funcparams></paramdef>\n";
  534. } else {
  535. print " <paramdef>".$type;
  536. print " <parameter>$parameter</parameter></paramdef>\n";
  537. }
  538. }
  539. } else {
  540. print " <void>\n";
  541. }
  542. print " </funcprototype></funcsynopsis>\n";
  543. print "</refsynopsisdiv>\n";
  544. # print parameters
  545. print "<refsect1>\n <title>Arguments</title>\n";
  546. if ($#{$args{'parameterlist'}} >= 0) {
  547. print " <variablelist>\n";
  548. foreach $parameter (@{$args{'parameterlist'}}) {
  549. my $parameter_name = $parameter;
  550. $parameter_name =~ s/\[.*//;
  551. print " <varlistentry>\n <term><parameter>$parameter</parameter></term>\n";
  552. print " <listitem>\n <para>\n";
  553. $lineprefix=" ";
  554. output_highlight($args{'parameterdescs'}{$parameter_name});
  555. print " </para>\n </listitem>\n </varlistentry>\n";
  556. }
  557. print " </variablelist>\n";
  558. } else {
  559. print " <para>\n None\n </para>\n";
  560. }
  561. print "</refsect1>\n";
  562. output_section_xml(@_);
  563. print "</refentry>\n\n";
  564. }
  565. # output struct in XML DocBook
  566. sub output_struct_xml(%) {
  567. my %args = %{$_[0]};
  568. my ($parameter, $section);
  569. my $id;
  570. $id = "API-struct-".$args{'struct'};
  571. $id =~ s/[^A-Za-z0-9]/-/g;
  572. print "<refentry>\n";
  573. print "<refmeta>\n";
  574. print "<refentrytitle><phrase id=\"$id\">".$args{'type'}." ".$args{'struct'}."</phrase></refentrytitle>\n";
  575. print "</refmeta>\n";
  576. print "<refnamediv>\n";
  577. print " <refname>".$args{'type'}." ".$args{'struct'}."</refname>\n";
  578. print " <refpurpose>\n";
  579. print " ";
  580. output_highlight ($args{'purpose'});
  581. print " </refpurpose>\n";
  582. print "</refnamediv>\n";
  583. print "<refsynopsisdiv>\n";
  584. print " <title>Synopsis</title>\n";
  585. print " <programlisting>\n";
  586. print $args{'type'}." ".$args{'struct'}." {\n";
  587. foreach $parameter (@{$args{'parameterlist'}}) {
  588. if ($parameter =~ /^#/) {
  589. print "$parameter\n";
  590. next;
  591. }
  592. my $parameter_name = $parameter;
  593. $parameter_name =~ s/\[.*//;
  594. defined($args{'parameterdescs'}{$parameter_name}) || next;
  595. ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
  596. $type = $args{'parametertypes'}{$parameter};
  597. if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
  598. # pointer-to-function
  599. print " $1 $parameter) ($2);\n";
  600. } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
  601. print " $1 $parameter$2;\n";
  602. } else {
  603. print " ".$type." ".$parameter.";\n";
  604. }
  605. }
  606. print "};";
  607. print " </programlisting>\n";
  608. print "</refsynopsisdiv>\n";
  609. print " <refsect1>\n";
  610. print " <title>Members</title>\n";
  611. print " <variablelist>\n";
  612. foreach $parameter (@{$args{'parameterlist'}}) {
  613. ($parameter =~ /^#/) && next;
  614. my $parameter_name = $parameter;
  615. $parameter_name =~ s/\[.*//;
  616. defined($args{'parameterdescs'}{$parameter_name}) || next;
  617. ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
  618. print " <varlistentry>";
  619. print " <term>$parameter</term>\n";
  620. print " <listitem><para>\n";
  621. output_highlight($args{'parameterdescs'}{$parameter_name});
  622. print " </para></listitem>\n";
  623. print " </varlistentry>\n";
  624. }
  625. print " </variablelist>\n";
  626. print " </refsect1>\n";
  627. output_section_xml(@_);
  628. print "</refentry>\n\n";
  629. }
  630. # output enum in XML DocBook
  631. sub output_enum_xml(%) {
  632. my %args = %{$_[0]};
  633. my ($parameter, $section);
  634. my $count;
  635. my $id;
  636. $id = "API-enum-".$args{'enum'};
  637. $id =~ s/[^A-Za-z0-9]/-/g;
  638. print "<refentry>\n";
  639. print "<refmeta>\n";
  640. print "<refentrytitle><phrase id=\"$id\">enum ".$args{'enum'}."</phrase></refentrytitle>\n";
  641. print "</refmeta>\n";
  642. print "<refnamediv>\n";
  643. print " <refname>enum ".$args{'enum'}."</refname>\n";
  644. print " <refpurpose>\n";
  645. print " ";
  646. output_highlight ($args{'purpose'});
  647. print " </refpurpose>\n";
  648. print "</refnamediv>\n";
  649. print "<refsynopsisdiv>\n";
  650. print " <title>Synopsis</title>\n";
  651. print " <programlisting>\n";
  652. print "enum ".$args{'enum'}." {\n";
  653. $count = 0;
  654. foreach $parameter (@{$args{'parameterlist'}}) {
  655. print " $parameter";
  656. if ($count != $#{$args{'parameterlist'}}) {
  657. $count++;
  658. print ",";
  659. }
  660. print "\n";
  661. }
  662. print "};";
  663. print " </programlisting>\n";
  664. print "</refsynopsisdiv>\n";
  665. print "<refsect1>\n";
  666. print " <title>Constants</title>\n";
  667. print " <variablelist>\n";
  668. foreach $parameter (@{$args{'parameterlist'}}) {
  669. my $parameter_name = $parameter;
  670. $parameter_name =~ s/\[.*//;
  671. print " <varlistentry>";
  672. print " <term>$parameter</term>\n";
  673. print " <listitem><para>\n";
  674. output_highlight($args{'parameterdescs'}{$parameter_name});
  675. print " </para></listitem>\n";
  676. print " </varlistentry>\n";
  677. }
  678. print " </variablelist>\n";
  679. print "</refsect1>\n";
  680. output_section_xml(@_);
  681. print "</refentry>\n\n";
  682. }
  683. # output typedef in XML DocBook
  684. sub output_typedef_xml(%) {
  685. my %args = %{$_[0]};
  686. my ($parameter, $section);
  687. my $id;
  688. $id = "API-typedef-".$args{'typedef'};
  689. $id =~ s/[^A-Za-z0-9]/-/g;
  690. print "<refentry>\n";
  691. print "<refmeta>\n";
  692. print "<refentrytitle><phrase id=\"$id\">typedef ".$args{'typedef'}."</phrase></refentrytitle>\n";
  693. print "</refmeta>\n";
  694. print "<refnamediv>\n";
  695. print " <refname>typedef ".$args{'typedef'}."</refname>\n";
  696. print " <refpurpose>\n";
  697. print " ";
  698. output_highlight ($args{'purpose'});
  699. print " </refpurpose>\n";
  700. print "</refnamediv>\n";
  701. print "<refsynopsisdiv>\n";
  702. print " <title>Synopsis</title>\n";
  703. print " <synopsis>typedef ".$args{'typedef'}.";</synopsis>\n";
  704. print "</refsynopsisdiv>\n";
  705. output_section_xml(@_);
  706. print "</refentry>\n\n";
  707. }
  708. # output in XML DocBook
  709. sub output_intro_xml(%) {
  710. my %args = %{$_[0]};
  711. my ($parameter, $section);
  712. my $count;
  713. my $id = $args{'module'};
  714. $id =~ s/[^A-Za-z0-9]/-/g;
  715. # print out each section
  716. $lineprefix=" ";
  717. foreach $section (@{$args{'sectionlist'}}) {
  718. print "<refsect1>\n <title>$section</title>\n <para>\n";
  719. if ($section =~ m/EXAMPLE/i) {
  720. print "<example><para>\n";
  721. }
  722. output_highlight($args{'sections'}{$section});
  723. if ($section =~ m/EXAMPLE/i) {
  724. print "</para></example>\n";
  725. }
  726. print " </para>\n</refsect1>\n";
  727. }
  728. print "\n\n";
  729. }
  730. # output in XML DocBook
  731. sub output_function_gnome {
  732. my %args = %{$_[0]};
  733. my ($parameter, $section);
  734. my $count;
  735. my $id;
  736. $id = $args{'module'}."-".$args{'function'};
  737. $id =~ s/[^A-Za-z0-9]/-/g;
  738. print "<sect2>\n";
  739. print " <title id=\"$id\">".$args{'function'}."</title>\n";
  740. print " <funcsynopsis>\n";
  741. print " <funcdef>".$args{'functiontype'}." ";
  742. print "<function>".$args{'function'}." ";
  743. print "</function></funcdef>\n";
  744. $count = 0;
  745. if ($#{$args{'parameterlist'}} >= 0) {
  746. foreach $parameter (@{$args{'parameterlist'}}) {
  747. $type = $args{'parametertypes'}{$parameter};
  748. if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
  749. # pointer-to-function
  750. print " <paramdef>$1 <parameter>$parameter</parameter>)\n";
  751. print " <funcparams>$2</funcparams></paramdef>\n";
  752. } else {
  753. print " <paramdef>".$type;
  754. print " <parameter>$parameter</parameter></paramdef>\n";
  755. }
  756. }
  757. } else {
  758. print " <void>\n";
  759. }
  760. print " </funcsynopsis>\n";
  761. if ($#{$args{'parameterlist'}} >= 0) {
  762. print " <informaltable pgwide=\"1\" frame=\"none\" role=\"params\">\n";
  763. print "<tgroup cols=\"2\">\n";
  764. print "<colspec colwidth=\"2*\">\n";
  765. print "<colspec colwidth=\"8*\">\n";
  766. print "<tbody>\n";
  767. foreach $parameter (@{$args{'parameterlist'}}) {
  768. my $parameter_name = $parameter;
  769. $parameter_name =~ s/\[.*//;
  770. print " <row><entry align=\"right\"><parameter>$parameter</parameter></entry>\n";
  771. print " <entry>\n";
  772. $lineprefix=" ";
  773. output_highlight($args{'parameterdescs'}{$parameter_name});
  774. print " </entry></row>\n";
  775. }
  776. print " </tbody></tgroup></informaltable>\n";
  777. } else {
  778. print " <para>\n None\n </para>\n";
  779. }
  780. # print out each section
  781. $lineprefix=" ";
  782. foreach $section (@{$args{'sectionlist'}}) {
  783. print "<simplesect>\n <title>$section</title>\n";
  784. if ($section =~ m/EXAMPLE/i) {
  785. print "<example><programlisting>\n";
  786. } else {
  787. }
  788. print "<para>\n";
  789. output_highlight($args{'sections'}{$section});
  790. print "</para>\n";
  791. if ($section =~ m/EXAMPLE/i) {
  792. print "</programlisting></example>\n";
  793. } else {
  794. }
  795. print " </simplesect>\n";
  796. }
  797. print "</sect2>\n\n";
  798. }
  799. ##
  800. # output function in man
  801. sub output_function_man(%) {
  802. my %args = %{$_[0]};
  803. my ($parameter, $section);
  804. my $count;
  805. print ".TH \"$args{'function'}\" 9 \"$args{'function'}\" \"$man_date\" \"Kernel Hacker's Manual\" LINUX\n";
  806. print ".SH NAME\n";
  807. print $args{'function'}." \\- ".$args{'purpose'}."\n";
  808. print ".SH SYNOPSIS\n";
  809. print ".B \"".$args{'functiontype'}."\" ".$args{'function'}."\n";
  810. $count = 0;
  811. my $parenth = "(";
  812. my $post = ",";
  813. foreach my $parameter (@{$args{'parameterlist'}}) {
  814. if ($count == $#{$args{'parameterlist'}}) {
  815. $post = ");";
  816. }
  817. $type = $args{'parametertypes'}{$parameter};
  818. if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
  819. # pointer-to-function
  820. print ".BI \"".$parenth.$1."\" ".$parameter." \") (".$2.")".$post."\"\n";
  821. } else {
  822. $type =~ s/([^\*])$/$1 /;
  823. print ".BI \"".$parenth.$type."\" ".$parameter." \"".$post."\"\n";
  824. }
  825. $count++;
  826. $parenth = "";
  827. }
  828. print ".SH ARGUMENTS\n";
  829. foreach $parameter (@{$args{'parameterlist'}}) {
  830. my $parameter_name = $parameter;
  831. $parameter_name =~ s/\[.*//;
  832. print ".IP \"".$parameter."\" 12\n";
  833. output_highlight($args{'parameterdescs'}{$parameter_name});
  834. }
  835. foreach $section (@{$args{'sectionlist'}}) {
  836. print ".SH \"", uc $section, "\"\n";
  837. output_highlight($args{'sections'}{$section});
  838. }
  839. }
  840. ##
  841. # output enum in man
  842. sub output_enum_man(%) {
  843. my %args = %{$_[0]};
  844. my ($parameter, $section);
  845. my $count;
  846. print ".TH \"$args{'module'}\" 9 \"enum $args{'enum'}\" \"$man_date\" \"API Manual\" LINUX\n";
  847. print ".SH NAME\n";
  848. print "enum ".$args{'enum'}." \\- ".$args{'purpose'}."\n";
  849. print ".SH SYNOPSIS\n";
  850. print "enum ".$args{'enum'}." {\n";
  851. $count = 0;
  852. foreach my $parameter (@{$args{'parameterlist'}}) {
  853. print ".br\n.BI \" $parameter\"\n";
  854. if ($count == $#{$args{'parameterlist'}}) {
  855. print "\n};\n";
  856. last;
  857. }
  858. else {
  859. print ", \n.br\n";
  860. }
  861. $count++;
  862. }
  863. print ".SH Constants\n";
  864. foreach $parameter (@{$args{'parameterlist'}}) {
  865. my $parameter_name = $parameter;
  866. $parameter_name =~ s/\[.*//;
  867. print ".IP \"".$parameter."\" 12\n";
  868. output_highlight($args{'parameterdescs'}{$parameter_name});
  869. }
  870. foreach $section (@{$args{'sectionlist'}}) {
  871. print ".SH \"$section\"\n";
  872. output_highlight($args{'sections'}{$section});
  873. }
  874. }
  875. ##
  876. # output struct in man
  877. sub output_struct_man(%) {
  878. my %args = %{$_[0]};
  879. my ($parameter, $section);
  880. print ".TH \"$args{'module'}\" 9 \"".$args{'type'}." ".$args{'struct'}."\" \"$man_date\" \"API Manual\" LINUX\n";
  881. print ".SH NAME\n";
  882. print $args{'type'}." ".$args{'struct'}." \\- ".$args{'purpose'}."\n";
  883. print ".SH SYNOPSIS\n";
  884. print $args{'type'}." ".$args{'struct'}." {\n.br\n";
  885. foreach my $parameter (@{$args{'parameterlist'}}) {
  886. if ($parameter =~ /^#/) {
  887. print ".BI \"$parameter\"\n.br\n";
  888. next;
  889. }
  890. my $parameter_name = $parameter;
  891. $parameter_name =~ s/\[.*//;
  892. ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
  893. $type = $args{'parametertypes'}{$parameter};
  894. if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
  895. # pointer-to-function
  896. print ".BI \" ".$1."\" ".$parameter." \") (".$2.")"."\"\n;\n";
  897. } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
  898. print ".BI \" ".$1."\" ".$parameter.$2." \""."\"\n;\n";
  899. } else {
  900. $type =~ s/([^\*])$/$1 /;
  901. print ".BI \" ".$type."\" ".$parameter." \""."\"\n;\n";
  902. }
  903. print "\n.br\n";
  904. }
  905. print "};\n.br\n";
  906. print ".SH Arguments\n";
  907. foreach $parameter (@{$args{'parameterlist'}}) {
  908. ($parameter =~ /^#/) && next;
  909. my $parameter_name = $parameter;
  910. $parameter_name =~ s/\[.*//;
  911. ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
  912. print ".IP \"".$parameter."\" 12\n";
  913. output_highlight($args{'parameterdescs'}{$parameter_name});
  914. }
  915. foreach $section (@{$args{'sectionlist'}}) {
  916. print ".SH \"$section\"\n";
  917. output_highlight($args{'sections'}{$section});
  918. }
  919. }
  920. ##
  921. # output typedef in man
  922. sub output_typedef_man(%) {
  923. my %args = %{$_[0]};
  924. my ($parameter, $section);
  925. print ".TH \"$args{'module'}\" 9 \"$args{'typedef'}\" \"$man_date\" \"API Manual\" LINUX\n";
  926. print ".SH NAME\n";
  927. print "typedef ".$args{'typedef'}." \\- ".$args{'purpose'}."\n";
  928. foreach $section (@{$args{'sectionlist'}}) {
  929. print ".SH \"$section\"\n";
  930. output_highlight($args{'sections'}{$section});
  931. }
  932. }
  933. sub output_intro_man(%) {
  934. my %args = %{$_[0]};
  935. my ($parameter, $section);
  936. my $count;
  937. print ".TH \"$args{'module'}\" 9 \"$args{'module'}\" \"$man_date\" \"API Manual\" LINUX\n";
  938. foreach $section (@{$args{'sectionlist'}}) {
  939. print ".SH \"$section\"\n";
  940. output_highlight($args{'sections'}{$section});
  941. }
  942. }
  943. ##
  944. # output in text
  945. sub output_function_text(%) {
  946. my %args = %{$_[0]};
  947. my ($parameter, $section);
  948. print "Function:\n\n";
  949. my $start=$args{'functiontype'}." ".$args{'function'}." (";
  950. print $start;
  951. my $count = 0;
  952. foreach my $parameter (@{$args{'parameterlist'}}) {
  953. $type = $args{'parametertypes'}{$parameter};
  954. if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
  955. # pointer-to-function
  956. print $1.$parameter.") (".$2;
  957. } else {
  958. print $type." ".$parameter;
  959. }
  960. if ($count != $#{$args{'parameterlist'}}) {
  961. $count++;
  962. print ",\n";
  963. print " " x length($start);
  964. } else {
  965. print ");\n\n";
  966. }
  967. }
  968. print "Arguments:\n\n";
  969. foreach $parameter (@{$args{'parameterlist'}}) {
  970. my $parameter_name = $parameter;
  971. $parameter_name =~ s/\[.*//;
  972. print $parameter."\n\t".$args{'parameterdescs'}{$parameter_name}."\n";
  973. }
  974. output_section_text(@_);
  975. }
  976. #output sections in text
  977. sub output_section_text(%) {
  978. my %args = %{$_[0]};
  979. my $section;
  980. print "\n";
  981. foreach $section (@{$args{'sectionlist'}}) {
  982. print "$section:\n\n";
  983. output_highlight($args{'sections'}{$section});
  984. }
  985. print "\n\n";
  986. }
  987. # output enum in text
  988. sub output_enum_text(%) {
  989. my %args = %{$_[0]};
  990. my ($parameter);
  991. my $count;
  992. print "Enum:\n\n";
  993. print "enum ".$args{'enum'}." {\n";
  994. $count = 0;
  995. foreach $parameter (@{$args{'parameterlist'}}) {
  996. print "\t$parameter";
  997. if ($count != $#{$args{'parameterlist'}}) {
  998. $count++;
  999. print ",";
  1000. }
  1001. print "\n";
  1002. }
  1003. print "};\n\n";
  1004. print "Constants:\n\n";
  1005. foreach $parameter (@{$args{'parameterlist'}}) {
  1006. print "$parameter\n\t";
  1007. print $args{'parameterdescs'}{$parameter}."\n";
  1008. }
  1009. output_section_text(@_);
  1010. }
  1011. # output typedef in text
  1012. sub output_typedef_text(%) {
  1013. my %args = %{$_[0]};
  1014. my ($parameter);
  1015. my $count;
  1016. print "Typedef:\n\n";
  1017. print "typedef ".$args{'typedef'}."\n";
  1018. output_section_text(@_);
  1019. }
  1020. # output struct as text
  1021. sub output_struct_text(%) {
  1022. my %args = %{$_[0]};
  1023. my ($parameter);
  1024. print $args{'type'}." ".$args{'struct'}.":\n\n";
  1025. print $args{'type'}." ".$args{'struct'}." {\n";
  1026. foreach $parameter (@{$args{'parameterlist'}}) {
  1027. if ($parameter =~ /^#/) {
  1028. print "$parameter\n";
  1029. next;
  1030. }
  1031. my $parameter_name = $parameter;
  1032. $parameter_name =~ s/\[.*//;
  1033. ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
  1034. $type = $args{'parametertypes'}{$parameter};
  1035. if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
  1036. # pointer-to-function
  1037. print "\t$1 $parameter) ($2);\n";
  1038. } elsif ($type =~ m/^(.*?)\s*(:.*)/) {
  1039. print "\t$1 $parameter$2;\n";
  1040. } else {
  1041. print "\t".$type." ".$parameter.";\n";
  1042. }
  1043. }
  1044. print "};\n\n";
  1045. print "Members:\n\n";
  1046. foreach $parameter (@{$args{'parameterlist'}}) {
  1047. ($parameter =~ /^#/) && next;
  1048. my $parameter_name = $parameter;
  1049. $parameter_name =~ s/\[.*//;
  1050. ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
  1051. print "$parameter\n\t";
  1052. print $args{'parameterdescs'}{$parameter_name}."\n";
  1053. }
  1054. print "\n";
  1055. output_section_text(@_);
  1056. }
  1057. sub output_intro_text(%) {
  1058. my %args = %{$_[0]};
  1059. my ($parameter, $section);
  1060. foreach $section (@{$args{'sectionlist'}}) {
  1061. print " $section:\n";
  1062. print " -> ";
  1063. output_highlight($args{'sections'}{$section});
  1064. }
  1065. }
  1066. ##
  1067. # generic output function for typedefs
  1068. sub output_declaration {
  1069. no strict 'refs';
  1070. my $name = shift;
  1071. my $functype = shift;
  1072. my $func = "output_${functype}_$output_mode";
  1073. if (($function_only==0) ||
  1074. ( $function_only == 1 && defined($function_table{$name})) ||
  1075. ( $function_only == 2 && !defined($function_table{$name})))
  1076. {
  1077. &$func(@_);
  1078. $section_counter++;
  1079. }
  1080. }
  1081. ##
  1082. # generic output function - calls the right one based
  1083. # on current output mode.
  1084. sub output_intro {
  1085. no strict 'refs';
  1086. my $func = "output_intro_".$output_mode;
  1087. &$func(@_);
  1088. $section_counter++;
  1089. }
  1090. ##
  1091. # takes a declaration (struct, union, enum, typedef) and
  1092. # invokes the right handler. NOT called for functions.
  1093. sub dump_declaration($$) {
  1094. no strict 'refs';
  1095. my ($prototype, $file) = @_;
  1096. my $func = "dump_".$decl_type;
  1097. &$func(@_);
  1098. }
  1099. sub dump_union($$) {
  1100. dump_struct(@_);
  1101. }
  1102. sub dump_struct($$) {
  1103. my $x = shift;
  1104. my $file = shift;
  1105. if ($x =~/(struct|union)\s+(\w+)\s*{(.*)}/) {
  1106. $declaration_name = $2;
  1107. my $members = $3;
  1108. # ignore embedded structs or unions
  1109. $members =~ s/{.*?}//g;
  1110. create_parameterlist($members, ';', $file);
  1111. output_declaration($declaration_name,
  1112. 'struct',
  1113. {'struct' => $declaration_name,
  1114. 'module' => $modulename,
  1115. 'parameterlist' => \@parameterlist,
  1116. 'parameterdescs' => \%parameterdescs,
  1117. 'parametertypes' => \%parametertypes,
  1118. 'sectionlist' => \@sectionlist,
  1119. 'sections' => \%sections,
  1120. 'purpose' => $declaration_purpose,
  1121. 'type' => $decl_type
  1122. });
  1123. }
  1124. else {
  1125. print STDERR "Error(${file}:$.): Cannot parse struct or union!\n";
  1126. ++$errors;
  1127. }
  1128. }
  1129. sub dump_enum($$) {
  1130. my $x = shift;
  1131. my $file = shift;
  1132. if ($x =~ /enum\s+(\w+)\s*{(.*)}/) {
  1133. $declaration_name = $1;
  1134. my $members = $2;
  1135. foreach my $arg (split ',', $members) {
  1136. $arg =~ s/^\s*(\w+).*/$1/;
  1137. push @parameterlist, $arg;
  1138. if (!$parameterdescs{$arg}) {
  1139. $parameterdescs{$arg} = $undescribed;
  1140. print STDERR "Warning(${file}:$.): Enum value '$arg' ".
  1141. "not described in enum '$declaration_name'\n";
  1142. }
  1143. }
  1144. output_declaration($declaration_name,
  1145. 'enum',
  1146. {'enum' => $declaration_name,
  1147. 'module' => $modulename,
  1148. 'parameterlist' => \@parameterlist,
  1149. 'parameterdescs' => \%parameterdescs,
  1150. 'sectionlist' => \@sectionlist,
  1151. 'sections' => \%sections,
  1152. 'purpose' => $declaration_purpose
  1153. });
  1154. }
  1155. else {
  1156. print STDERR "Error(${file}:$.): Cannot parse enum!\n";
  1157. ++$errors;
  1158. }
  1159. }
  1160. sub dump_typedef($$) {
  1161. my $x = shift;
  1162. my $file = shift;
  1163. while (($x =~ /\(*.\)\s*;$/) || ($x =~ /\[*.\]\s*;$/)) {
  1164. $x =~ s/\(*.\)\s*;$/;/;
  1165. $x =~ s/\[*.\]\s*;$/;/;
  1166. }
  1167. if ($x =~ /typedef.*\s+(\w+)\s*;/) {
  1168. $declaration_name = $1;
  1169. output_declaration($declaration_name,
  1170. 'typedef',
  1171. {'typedef' => $declaration_name,
  1172. 'module' => $modulename,
  1173. 'sectionlist' => \@sectionlist,
  1174. 'sections' => \%sections,
  1175. 'purpose' => $declaration_purpose
  1176. });
  1177. }
  1178. else {
  1179. print STDERR "Error(${file}:$.): Cannot parse typedef!\n";
  1180. ++$errors;
  1181. }
  1182. }
  1183. sub create_parameterlist($$$) {
  1184. my $args = shift;
  1185. my $splitter = shift;
  1186. my $file = shift;
  1187. my $type;
  1188. my $param;
  1189. while ($args =~ /(\([^\),]+),/) {
  1190. $args =~ s/(\([^\),]+),/$1#/g;
  1191. }
  1192. foreach my $arg (split($splitter, $args)) {
  1193. # strip comments
  1194. $arg =~ s/\/\*.*\*\///;
  1195. # strip leading/trailing spaces
  1196. $arg =~ s/^\s*//;
  1197. $arg =~ s/\s*$//;
  1198. $arg =~ s/\s+/ /;
  1199. if ($arg =~ /^#/) {
  1200. # Treat preprocessor directive as a typeless variable just to fill
  1201. # corresponding data structures "correctly". Catch it later in
  1202. # output_* subs.
  1203. push_parameter($arg, "", $file);
  1204. } elsif ($arg =~ m/\(/) {
  1205. # pointer-to-function
  1206. $arg =~ tr/#/,/;
  1207. $arg =~ m/[^\(]+\(\*([^\)]+)\)/;
  1208. $param = $1;
  1209. $type = $arg;
  1210. $type =~ s/([^\(]+\(\*)$param/$1/;
  1211. push_parameter($param, $type, $file);
  1212. } else {
  1213. $arg =~ s/\s*:\s*/:/g;
  1214. $arg =~ s/\s*\[/\[/g;
  1215. my @args = split('\s*,\s*', $arg);
  1216. if ($args[0] =~ m/\*/) {
  1217. $args[0] =~ s/(\*+)\s*/ $1/;
  1218. }
  1219. my @first_arg = split('\s+', shift @args);
  1220. unshift(@args, pop @first_arg);
  1221. $type = join " ", @first_arg;
  1222. foreach $param (@args) {
  1223. if ($param =~ m/^(\*+)\s*(.*)/) {
  1224. push_parameter($2, "$type $1", $file);
  1225. }
  1226. elsif ($param =~ m/(.*?):(\d+)/) {
  1227. push_parameter($1, "$type:$2", $file)
  1228. }
  1229. else {
  1230. push_parameter($param, $type, $file);
  1231. }
  1232. }
  1233. }
  1234. }
  1235. }
  1236. sub push_parameter($$$) {
  1237. my $param = shift;
  1238. my $type = shift;
  1239. my $file = shift;
  1240. my $param_name = $param;
  1241. $param_name =~ s/\[.*//;
  1242. if ($type eq "" && $param eq "...")
  1243. {
  1244. $type="";
  1245. $param="...";
  1246. $parameterdescs{"..."} = "variable arguments";
  1247. }
  1248. elsif ($type eq "" && ($param eq "" or $param eq "void"))
  1249. {
  1250. $type="";
  1251. $param="void";
  1252. $parameterdescs{void} = "no arguments";
  1253. }
  1254. if (defined $type && $type && !defined $parameterdescs{$param_name}) {
  1255. $parameterdescs{$param_name} = $undescribed;
  1256. if (($type eq 'function') || ($type eq 'enum')) {
  1257. print STDERR "Warning(${file}:$.): Function parameter ".
  1258. "or member '$param' not " .
  1259. "described in '$declaration_name'\n";
  1260. }
  1261. print STDERR "Warning(${file}:$.):".
  1262. " No description found for parameter '$param'\n";
  1263. ++$warnings;
  1264. }
  1265. push @parameterlist, $param;
  1266. $parametertypes{$param} = $type;
  1267. }
  1268. ##
  1269. # takes a function prototype and the name of the current file being
  1270. # processed and spits out all the details stored in the global
  1271. # arrays/hashes.
  1272. sub dump_function($$) {
  1273. my $prototype = shift;
  1274. my $file = shift;
  1275. $prototype =~ s/^static +//;
  1276. $prototype =~ s/^extern +//;
  1277. $prototype =~ s/^inline +//;
  1278. $prototype =~ s/^__inline__ +//;
  1279. $prototype =~ s/^#define +//; #ak added
  1280. $prototype =~ s/__attribute__ \(\([a-z,]*\)\)//;
  1281. # Yes, this truly is vile. We are looking for:
  1282. # 1. Return type (may be nothing if we're looking at a macro)
  1283. # 2. Function name
  1284. # 3. Function parameters.
  1285. #
  1286. # All the while we have to watch out for function pointer parameters
  1287. # (which IIRC is what the two sections are for), C types (these
  1288. # regexps don't even start to express all the possibilities), and
  1289. # so on.
  1290. #
  1291. # If you mess with these regexps, it's a good idea to check that
  1292. # the following functions' documentation still comes out right:
  1293. # - parport_register_device (function pointer parameters)
  1294. # - atomic_set (macro)
  1295. # - pci_match_device (long return type)
  1296. if ($prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
  1297. $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
  1298. $prototype =~ m/^(\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
  1299. $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
  1300. $prototype =~ m/^(\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
  1301. $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
  1302. $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
  1303. $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
  1304. $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
  1305. $prototype =~ m/^(\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
  1306. $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
  1307. $prototype =~ m/^(\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
  1308. $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
  1309. $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/) {
  1310. $return_type = $1;
  1311. $declaration_name = $2;
  1312. my $args = $3;
  1313. create_parameterlist($args, ',', $file);
  1314. } else {
  1315. print STDERR "Error(${file}:$.): cannot understand prototype: '$prototype'\n";
  1316. ++$errors;
  1317. return;
  1318. }
  1319. output_declaration($declaration_name,
  1320. 'function',
  1321. {'function' => $declaration_name,
  1322. 'module' => $modulename,
  1323. 'functiontype' => $return_type,
  1324. 'parameterlist' => \@parameterlist,
  1325. 'parameterdescs' => \%parameterdescs,
  1326. 'parametertypes' => \%parametertypes,
  1327. 'sectionlist' => \@sectionlist,
  1328. 'sections' => \%sections,
  1329. 'purpose' => $declaration_purpose
  1330. });
  1331. }
  1332. sub process_file($);
  1333. # Read the file that maps relative names to absolute names for
  1334. # separate source and object directories and for shadow trees.
  1335. if (open(SOURCE_MAP, "<.tmp_filelist.txt")) {
  1336. my ($relname, $absname);
  1337. while(<SOURCE_MAP>) {
  1338. chop();
  1339. ($relname, $absname) = (split())[0..1];
  1340. $relname =~ s:^/+::;
  1341. $source_map{$relname} = $absname;
  1342. }
  1343. close(SOURCE_MAP);
  1344. }
  1345. if ($filelist) {
  1346. open(FLIST,"<$filelist") or die "Can't open file list $filelist";
  1347. while(<FLIST>) {
  1348. chop;
  1349. process_file($_);
  1350. }
  1351. }
  1352. foreach (@ARGV) {
  1353. chomp;
  1354. process_file($_);
  1355. }
  1356. if ($verbose && $errors) {
  1357. print STDERR "$errors errors\n";
  1358. }
  1359. if ($verbose && $warnings) {
  1360. print STDERR "$warnings warnings\n";
  1361. }
  1362. exit($errors);
  1363. sub reset_state {
  1364. $function = "";
  1365. %constants = ();
  1366. %parameterdescs = ();
  1367. %parametertypes = ();
  1368. @parameterlist = ();
  1369. %sections = ();
  1370. @sectionlist = ();
  1371. $prototype = "";
  1372. $state = 0;
  1373. }
  1374. sub process_state3_function($$) {
  1375. my $x = shift;
  1376. my $file = shift;
  1377. if ($x =~ m#\s*/\*\s+MACDOC\s*#io || ($x =~ /^#/ && $x !~ /^#define/)) {
  1378. # do nothing
  1379. }
  1380. elsif ($x =~ /([^\{]*)/) {
  1381. $prototype .= $1;
  1382. }
  1383. if (($x =~ /\{/) || ($x =~ /\#define/) || ($x =~ /;/)) {
  1384. $prototype =~ s@/\*.*?\*/@@gos; # strip comments.
  1385. $prototype =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
  1386. $prototype =~ s@^\s+@@gos; # strip leading spaces
  1387. dump_function($prototype,$file);
  1388. reset_state();
  1389. }
  1390. }
  1391. sub process_state3_type($$) {
  1392. my $x = shift;
  1393. my $file = shift;
  1394. $x =~ s@/\*.*?\*/@@gos; # strip comments.
  1395. $x =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
  1396. $x =~ s@^\s+@@gos; # strip leading spaces
  1397. $x =~ s@\s+$@@gos; # strip trailing spaces
  1398. if ($x =~ /^#/) {
  1399. # To distinguish preprocessor directive from regular declaration later.
  1400. $x .= ";";
  1401. }
  1402. while (1) {
  1403. if ( $x =~ /([^{};]*)([{};])(.*)/ ) {
  1404. $prototype .= $1 . $2;
  1405. ($2 eq '{') && $brcount++;
  1406. ($2 eq '}') && $brcount--;
  1407. if (($2 eq ';') && ($brcount == 0)) {
  1408. dump_declaration($prototype,$file);
  1409. reset_state();
  1410. last;
  1411. }
  1412. $x = $3;
  1413. } else {
  1414. $prototype .= $x;
  1415. last;
  1416. }
  1417. }
  1418. }
  1419. # replace <, >, and &
  1420. sub xml_escape($) {
  1421. my $text = shift;
  1422. $text =~ s/\&/\\\\\\amp;/g;
  1423. $text =~ s/\</\\\\\\lt;/g;
  1424. $text =~ s/\>/\\\\\\gt;/g;
  1425. return $text;
  1426. }
  1427. sub process_file($) {
  1428. my ($file) = "$ENV{'SRCTREE'}@_";
  1429. my $identifier;
  1430. my $func;
  1431. my $initial_section_counter = $section_counter;
  1432. if (defined($source_map{$file})) {
  1433. $file = $source_map{$file};
  1434. }
  1435. if (!open(IN,"<$file")) {
  1436. print STDERR "Error: Cannot open file $file\n";
  1437. ++$errors;
  1438. return;
  1439. }
  1440. $section_counter = 0;
  1441. while (<IN>) {
  1442. if ($state == 0) {
  1443. if (/$doc_start/o) {
  1444. $state = 1; # next line is always the function name
  1445. }
  1446. } elsif ($state == 1) { # this line is the function name (always)
  1447. if (/$doc_block/o) {
  1448. $state = 4;
  1449. $contents = "";
  1450. if ( $1 eq "" ) {
  1451. $section = $section_intro;
  1452. } else {
  1453. $section = $1;
  1454. }
  1455. }
  1456. elsif (/$doc_decl/o) {
  1457. $identifier = $1;
  1458. if (/\s*([\w\s]+?)\s*-/) {
  1459. $identifier = $1;
  1460. }
  1461. $state = 2;
  1462. if (/-(.*)/) {
  1463. $declaration_purpose = xml_escape($1);
  1464. } else {
  1465. $declaration_purpose = "";
  1466. }
  1467. if ($identifier =~ m/^struct/) {
  1468. $decl_type = 'struct';
  1469. } elsif ($identifier =~ m/^union/) {
  1470. $decl_type = 'union';
  1471. } elsif ($identifier =~ m/^enum/) {
  1472. $decl_type = 'enum';
  1473. } elsif ($identifier =~ m/^typedef/) {
  1474. $decl_type = 'typedef';
  1475. } else {
  1476. $decl_type = 'function';
  1477. }
  1478. if ($verbose) {
  1479. print STDERR "Info(${file}:$.): Scanning doc for $identifier\n";
  1480. }
  1481. } else {
  1482. print STDERR "Warning(${file}:$.): Cannot understand $_ on line $.",
  1483. " - I thought it was a doc line\n";
  1484. ++$warnings;
  1485. $state = 0;
  1486. }
  1487. } elsif ($state == 2) { # look for head: lines, and include content
  1488. if (/$doc_sect/o) {
  1489. $newsection = $1;
  1490. $newcontents = $2;
  1491. if ($contents ne "") {
  1492. dump_section($section, xml_escape($contents));
  1493. $section = $section_default;
  1494. }
  1495. $contents = $newcontents;
  1496. if ($contents ne "") {
  1497. $contents .= "\n";
  1498. }
  1499. $section = $newsection;
  1500. } elsif (/$doc_end/) {
  1501. if ($contents ne "") {
  1502. dump_section($section, xml_escape($contents));
  1503. $section = $section_default;
  1504. $contents = "";
  1505. }
  1506. $prototype = "";
  1507. $state = 3;
  1508. $brcount = 0;
  1509. # print STDERR "end of doc comment, looking for prototype\n";
  1510. } elsif (/$doc_content/) {
  1511. # miguel-style comment kludge, look for blank lines after
  1512. # @parameter line to signify start of description
  1513. if ($1 eq "" &&
  1514. ($section =~ m/^@/ || $section eq $section_context)) {
  1515. dump_section($section, xml_escape($contents));
  1516. $section = $section_default;
  1517. $contents = "";
  1518. } else {
  1519. $contents .= $1."\n";
  1520. }
  1521. } else {
  1522. # i dont know - bad line? ignore.
  1523. print STDERR "Warning(${file}:$.): bad line: $_";
  1524. ++$warnings;
  1525. }
  1526. } elsif ($state == 3) { # scanning for function { (end of prototype)
  1527. if ($decl_type eq 'function') {
  1528. process_state3_function($_, $file);
  1529. } else {
  1530. process_state3_type($_, $file);
  1531. }
  1532. } elsif ($state == 4) {
  1533. # Documentation block
  1534. if (/$doc_block/) {
  1535. dump_section($section, $contents);
  1536. output_intro({'sectionlist' => \@sectionlist,
  1537. 'sections' => \%sections });
  1538. $contents = "";
  1539. $function = "";
  1540. %constants = ();
  1541. %parameterdescs = ();
  1542. %parametertypes = ();
  1543. @parameterlist = ();
  1544. %sections = ();
  1545. @sectionlist = ();
  1546. $prototype = "";
  1547. if ( $1 eq "" ) {
  1548. $section = $section_intro;
  1549. } else {
  1550. $section = $1;
  1551. }
  1552. }
  1553. elsif (/$doc_end/)
  1554. {
  1555. dump_section($section, $contents);
  1556. output_intro({'sectionlist' => \@sectionlist,
  1557. 'sections' => \%sections });
  1558. $contents = "";
  1559. $function = "";
  1560. %constants = ();
  1561. %parameterdescs = ();
  1562. %parametertypes = ();
  1563. @parameterlist = ();
  1564. %sections = ();
  1565. @sectionlist = ();
  1566. $prototype = "";
  1567. $state = 0;
  1568. }
  1569. elsif (/$doc_content/)
  1570. {
  1571. if ( $1 eq "" )
  1572. {
  1573. $contents .= $blankline;
  1574. }
  1575. else
  1576. {
  1577. $contents .= $1 . "\n";
  1578. }
  1579. }
  1580. }
  1581. }
  1582. if ($initial_section_counter == $section_counter) {
  1583. print STDERR "Warning(${file}): no structured comments found\n";
  1584. if ($output_mode eq "xml") {
  1585. # The template wants at least one RefEntry here; make one.
  1586. print "<refentry>\n";
  1587. print " <refnamediv>\n";
  1588. print " <refname>\n";
  1589. print " ${file}\n";
  1590. print " </refname>\n";
  1591. print " <refpurpose>\n";
  1592. print " Document generation inconsistency\n";
  1593. print " </refpurpose>\n";
  1594. print " </refnamediv>\n";
  1595. print " <refsect1>\n";
  1596. print " <title>\n";
  1597. print " Oops\n";
  1598. print " </title>\n";
  1599. print " <warning>\n";
  1600. print " <para>\n";
  1601. print " The template for this document tried to insert\n";
  1602. print " the structured comment from the file\n";
  1603. print " <filename>${file}</filename> at this point,\n";
  1604. print " but none was found.\n";
  1605. print " This dummy section is inserted to allow\n";
  1606. print " generation to continue.\n";
  1607. print " </para>\n";
  1608. print " </warning>\n";
  1609. print " </refsect1>\n";
  1610. print "</refentry>\n";
  1611. }
  1612. }
  1613. }