kerneldoc2doxygen.pl 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/usr/bin/perl -w
  2. #
  3. ##########################################################################
  4. # Convert kernel-doc style comments to Doxygen comments.
  5. ##########################################################################
  6. #
  7. # This script reads a C source file from stdin, and writes
  8. # to stdout. Normal usage:
  9. #
  10. # $ mv file.c file.c.gtkdoc
  11. # $ kerneldoc2doxygen.pl <file.c.gtkdoc >file.c
  12. #
  13. # Or to do the same thing with multiple files:
  14. # $ perl -i.gtkdoc kerneldoc2doxygen.pl *.c *.h
  15. #
  16. # This script may also be suitable for use as a Doxygen input filter,
  17. # but that has not been tested.
  18. #
  19. # Back up your source files before using this script!!
  20. #
  21. ##########################################################################
  22. # Copyright (C) 2003 Jonathan Foster <jon@jon-foster.co.uk>
  23. # Copyright (C) 2005-2008 Jouni Malinen <j@w1.fi>
  24. # (modified for kerneldoc format used in wpa_supplicant)
  25. #
  26. # This program is free software; you can redistribute it and/or modify
  27. # it under the terms of the GNU General Public License version 2 as
  28. # published by the Free Software Foundation.
  29. #
  30. # This program is distributed in the hope that it will be useful,
  31. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  32. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  33. # GNU General Public License for more details.
  34. #
  35. # You should have received a copy of the GNU General Public License
  36. # along with this program; if not, write to the Free Software
  37. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  38. # or look at http://www.gnu.org/licenses/gpl.html
  39. ##########################################################################
  40. ##########################################################################
  41. #
  42. # This function converts a single comment from gtk-doc to Doxygen format.
  43. # The parameter does not include the opening or closing lines
  44. # (i.e. given a comment like this:
  45. # "/**\n"
  46. # " * FunctionName:\n"
  47. # " * @foo: This describes the foo parameter\n"
  48. # " * @bar: This describes the bar parameter\n"
  49. # " * @Returns: This describes the return value\n"
  50. # " *\n"
  51. # " * This describes the function.\n"
  52. # " */\n"
  53. # This function gets:
  54. # " * FunctionName:\n"
  55. # " * @foo: This describes the foo parameter\n"
  56. # " * @bar: This describes the bar parameter\n"
  57. # " * @Returns: This describes the return value\n"
  58. # " *\n"
  59. # " * This describes the function.\n"
  60. # And it returns:
  61. # " * This describes the function.\n"
  62. # " *\n"
  63. # " * @param foo This describes the foo parameter\n"
  64. # " * @param bar This describes the bar parameter\n"
  65. # " * @return This describes the return value\n"
  66. # )
  67. #
  68. sub fixcomment {
  69. $t = $_[0];
  70. # wpa_supplicant -> %wpa_supplicant except for struct wpa_supplicant
  71. $t =~ s/struct wpa_supplicant/struct STRUCTwpa_supplicant/sg;
  72. $t =~ s/ wpa_supplicant/ \%wpa_supplicant/sg;
  73. $t =~ s/struct STRUCTwpa_supplicant/struct wpa_supplicant/sg;
  74. # " * func: foo" --> "\brief foo\n"
  75. # " * struct bar: foo" --> "\brief foo\n"
  76. # If this fails, not a kernel-doc comment ==> return unmodified.
  77. ($t =~ s/^[\t ]*\*[\t ]*(struct )?([^ \t\n]*) - ([^\n]*)/\\brief $3\n/s)
  78. or return $t;
  79. # " * Returns: foo" --> "\return foo"
  80. $t =~ s/\n[\t ]*\*[\t ]*Returns:/\n\\return/sig;
  81. # " * @foo: bar" --> "\param foo bar"
  82. # Handle two common typos: No ":", or "," instead of ":".
  83. $t =~ s/\n[\t ]*\*[\t ]*\@([^ :,]*)[:,]?[\t ]*/\n\\param $1 /sg;
  84. return $t;
  85. }
  86. ##########################################################################
  87. # Start of main code
  88. # Read entire stdin into memory - one multi-line string.
  89. $_ = do { local $/; <> };
  90. s{^/\*\n \*}{/\*\* \\file\n\\brief};
  91. s{ \* Copyright}{\\par Copyright\nCopyright};
  92. # Fix any comments like "/*************" so they don't match.
  93. # "/***" ===> "/* *"
  94. s{/\*\*\*}{/\* \*}gs;
  95. # The main comment-detection code.
  96. s{
  97. ( # $1 = Open comment
  98. /\*\* # Open comment
  99. (?!\*) # Do not match /*** (redundant due to fixup above).
  100. [\t ]*\n? # If 1st line is whitespace, match the lot (including the newline).
  101. )
  102. (.*?) # $2 = Body of comment (multi-line)
  103. ( # $3 = Close comment
  104. ( # If possible, match the whitespace before the close-comment
  105. (?<=\n) # This part only matches after a newline
  106. [\t ]* # Eat whitespace
  107. )?
  108. \*/ # Close comment
  109. )
  110. }
  111. {
  112. $1 . fixcomment($2) . $3
  113. }gesx;
  114. # ^^^^ Modes: g - Global, match all occurances.
  115. # e - Evaluate the replacement as an expression.
  116. # s - Single-line - allows the pattern to match across newlines.
  117. # x - eXtended pattern, ignore embedded whitespace
  118. # and allow comments.
  119. # Write results to stdout
  120. print $_;