775f8feab48f0235aadb47d7703b81da845e7763
[platform/upstream/lcov.git] / bin / gendesc
1 #!/usr/bin/env perl
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, see
17 #   <http://www.gnu.org/licenses/>.
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
38 use strict;
39 use warnings;
40 use File::Basename; 
41 use Getopt::Long;
42 use Cwd qw/abs_path/;
43
44
45 # Constants
46 our $tool_dir           = abs_path(dirname($0));
47 our $lcov_version       = "LCOV version 1.14";
48 our $lcov_url           = "http://ltp.sourceforge.net/coverage/lcov.php";
49 our $tool_name          = basename($0);
50
51
52 # Prototypes
53 sub print_usage(*);
54 sub gen_desc();
55 sub warn_handler($);
56 sub die_handler($);
57
58
59 # Global variables
60 our $help;
61 our $version;
62 our $output_filename;
63 our $input_filename;
64
65
66 #
67 # Code entry point
68 #
69
70 $SIG{__WARN__} = \&warn_handler;
71 $SIG{__DIE__} = \&die_handler;
72
73 # Parse command line options
74 if (!GetOptions("output-filename=s" => \$output_filename,
75                 "version" =>\$version,
76                 "help|?" => \$help
77                 ))
78 {
79         print(STDERR "Use $tool_name --help to get usage information\n");
80         exit(1);
81 }
82
83 $input_filename = $ARGV[0];
84
85 # Check for help option
86 if ($help)
87 {
88         print_usage(*STDOUT);
89         exit(0);
90 }
91
92 # Check for version option
93 if ($version)
94 {
95         print("$tool_name: $lcov_version\n");
96         exit(0);
97 }
98
99
100 # Check for input filename
101 if (!$input_filename)
102 {
103         die("No input filename specified\n".
104             "Use $tool_name --help to get usage information\n");
105 }
106
107 # Do something
108 gen_desc();
109
110
111 #
112 # print_usage(handle)
113 #
114 # Write out command line usage information to given filehandle.
115 #
116
117 sub print_usage(*)
118 {
119         local *HANDLE = $_[0];
120
121         print(HANDLE <<END_OF_USAGE)
122 Usage: $tool_name [OPTIONS] INPUTFILE
123
124 Convert a test case description file into a format as understood by genhtml.
125
126   -h, --help                        Print this help, then exit
127   -v, --version                     Print version number, then exit
128   -o, --output-filename FILENAME    Write description to FILENAME
129
130 For more information see: $lcov_url
131 END_OF_USAGE
132         ;
133 }
134
135
136 #
137 # gen_desc()
138 #
139 # Read text file INPUT_FILENAME and convert the contained description to a
140 # format as understood by genhtml, i.e.
141 #
142 #    TN:<test name>
143 #    TD:<test description>
144 #
145 # If defined, write output to OUTPUT_FILENAME, otherwise to stdout.
146 #
147 # Die on error.
148 #
149
150 sub gen_desc()
151 {
152         local *INPUT_HANDLE;
153         local *OUTPUT_HANDLE;
154         my $empty_line = "ignore";
155
156         open(INPUT_HANDLE, "<", $input_filename)
157                 or die("ERROR: cannot open $input_filename!\n");
158
159         # Open output file for writing
160         if ($output_filename)
161         {
162                 open(OUTPUT_HANDLE, ">", $output_filename)
163                         or die("ERROR: cannot create $output_filename!\n");
164         }
165         else
166         {
167                 *OUTPUT_HANDLE = *STDOUT;
168         }
169
170         # Process all lines in input file
171         while (<INPUT_HANDLE>)
172         {
173                 chomp($_);
174
175                 if (/^(\w[\w-]*)(\s*)$/)
176                 {
177                         # Matched test name
178                         # Name starts with alphanum or _, continues with
179                         # alphanum, _ or -
180                         print(OUTPUT_HANDLE "TN: $1\n");
181                         $empty_line = "ignore";
182                 }
183                 elsif (/^(\s+)(\S.*?)\s*$/)
184                 {
185                         # Matched test description
186                         if ($empty_line eq "insert")
187                         {
188                                 # Write preserved empty line
189                                 print(OUTPUT_HANDLE "TD: \n");
190                         }
191                         print(OUTPUT_HANDLE "TD: $2\n");
192                         $empty_line = "observe";
193                 }
194                 elsif (/^\s*$/)
195                 {
196                         # Matched empty line to preserve paragraph separation
197                         # inside description text
198                         if ($empty_line eq "observe")
199                         {
200                                 $empty_line = "insert";
201                         }
202                 }
203         }
204
205         # Close output file if defined
206         if ($output_filename)
207         {
208                 close(OUTPUT_HANDLE);
209         }
210
211         close(INPUT_HANDLE);
212 }
213
214 sub warn_handler($)
215 {
216         my ($msg) = @_;
217
218         warn("$tool_name: $msg");
219 }
220
221 sub die_handler($)
222 {
223         my ($msg) = @_;
224
225         die("$tool_name: $msg");
226 }