gendesc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #!/usr/bin/perl -w
  2. #
  3. # Copyright (c) International Business Machines Corp., 2002
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or (at
  8. # your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful, but
  11. # WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. # General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. #
  19. #
  20. # gendesc
  21. #
  22. # This script creates a description file as understood by genhtml.
  23. # Input file format:
  24. #
  25. # For each test case:
  26. # <test name><optional whitespace>
  27. # <at least one whitespace character (blank/tab)><test description>
  28. #
  29. # Actual description may consist of several lines. By default, output is
  30. # written to stdout. Test names consist of alphanumeric characters
  31. # including _ and -.
  32. #
  33. #
  34. # History:
  35. # 2002-09-02: created by Peter Oberparleiter <Peter.Oberparleiter@de.ibm.com>
  36. #
  37. use strict;
  38. use File::Basename;
  39. use Getopt::Long;
  40. # Constants
  41. our $lcov_version = 'LCOV version 1.10';
  42. our $lcov_url = "http://ltp.sourceforge.net/coverage/lcov.php";
  43. our $tool_name = basename($0);
  44. # Prototypes
  45. sub print_usage(*);
  46. sub gen_desc();
  47. sub warn_handler($);
  48. sub die_handler($);
  49. # Global variables
  50. our $help;
  51. our $version;
  52. our $output_filename;
  53. our $input_filename;
  54. #
  55. # Code entry point
  56. #
  57. $SIG{__WARN__} = \&warn_handler;
  58. $SIG{__DIE__} = \&die_handler;
  59. # Prettify version string
  60. $lcov_version =~ s/\$\s*Revision\s*:?\s*(\S+)\s*\$/$1/;
  61. # Parse command line options
  62. if (!GetOptions("output-filename=s" => \$output_filename,
  63. "version" =>\$version,
  64. "help|?" => \$help
  65. ))
  66. {
  67. print(STDERR "Use $tool_name --help to get usage information\n");
  68. exit(1);
  69. }
  70. $input_filename = $ARGV[0];
  71. # Check for help option
  72. if ($help)
  73. {
  74. print_usage(*STDOUT);
  75. exit(0);
  76. }
  77. # Check for version option
  78. if ($version)
  79. {
  80. print("$tool_name: $lcov_version\n");
  81. exit(0);
  82. }
  83. # Check for input filename
  84. if (!$input_filename)
  85. {
  86. die("No input filename specified\n".
  87. "Use $tool_name --help to get usage information\n");
  88. }
  89. # Do something
  90. gen_desc();
  91. #
  92. # print_usage(handle)
  93. #
  94. # Write out command line usage information to given filehandle.
  95. #
  96. sub print_usage(*)
  97. {
  98. local *HANDLE = $_[0];
  99. print(HANDLE <<END_OF_USAGE)
  100. Usage: $tool_name [OPTIONS] INPUTFILE
  101. Convert a test case description file into a format as understood by genhtml.
  102. -h, --help Print this help, then exit
  103. -v, --version Print version number, then exit
  104. -o, --output-filename FILENAME Write description to FILENAME
  105. For more information see: $lcov_url
  106. END_OF_USAGE
  107. ;
  108. }
  109. #
  110. # gen_desc()
  111. #
  112. # Read text file INPUT_FILENAME and convert the contained description to a
  113. # format as understood by genhtml, i.e.
  114. #
  115. # TN:<test name>
  116. # TD:<test description>
  117. #
  118. # If defined, write output to OUTPUT_FILENAME, otherwise to stdout.
  119. #
  120. # Die on error.
  121. #
  122. sub gen_desc()
  123. {
  124. local *INPUT_HANDLE;
  125. local *OUTPUT_HANDLE;
  126. my $empty_line = "ignore";
  127. open(INPUT_HANDLE, "<", $input_filename)
  128. or die("ERROR: cannot open $input_filename!\n");
  129. # Open output file for writing
  130. if ($output_filename)
  131. {
  132. open(OUTPUT_HANDLE, ">", $output_filename)
  133. or die("ERROR: cannot create $output_filename!\n");
  134. }
  135. else
  136. {
  137. *OUTPUT_HANDLE = *STDOUT;
  138. }
  139. # Process all lines in input file
  140. while (<INPUT_HANDLE>)
  141. {
  142. chomp($_);
  143. if (/^(\w[\w-]*)(\s*)$/)
  144. {
  145. # Matched test name
  146. # Name starts with alphanum or _, continues with
  147. # alphanum, _ or -
  148. print(OUTPUT_HANDLE "TN: $1\n");
  149. $empty_line = "ignore";
  150. }
  151. elsif (/^(\s+)(\S.*?)\s*$/)
  152. {
  153. # Matched test description
  154. if ($empty_line eq "insert")
  155. {
  156. # Write preserved empty line
  157. print(OUTPUT_HANDLE "TD: \n");
  158. }
  159. print(OUTPUT_HANDLE "TD: $2\n");
  160. $empty_line = "observe";
  161. }
  162. elsif (/^\s*$/)
  163. {
  164. # Matched empty line to preserve paragraph separation
  165. # inside description text
  166. if ($empty_line eq "observe")
  167. {
  168. $empty_line = "insert";
  169. }
  170. }
  171. }
  172. # Close output file if defined
  173. if ($output_filename)
  174. {
  175. close(OUTPUT_HANDLE);
  176. }
  177. close(INPUT_HANDLE);
  178. }
  179. sub warn_handler($)
  180. {
  181. my ($msg) = @_;
  182. warn("$tool_name: $msg");
  183. }
  184. sub die_handler($)
  185. {
  186. my ($msg) = @_;
  187. die("$tool_name: $msg");
  188. }