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
38use strict;
39use File::Basename; 
40use Getopt::Long;
41
42
43# Constants
44our $lcov_version	= 'LCOV version 1.10';
45our $lcov_url		= "http://ltp.sourceforge.net/coverage/lcov.php";
46our $tool_name		= basename($0);
47
48
49# Prototypes
50sub print_usage(*);
51sub gen_desc();
52sub warn_handler($);
53sub die_handler($);
54
55
56# Global variables
57our $help;
58our $version;
59our $output_filename;
60our $input_filename;
61
62
63#
64# Code entry point
65#
66
67$SIG{__WARN__} = \&warn_handler;
68$SIG{__DIE__} = \&die_handler;
69
70# Prettify version string
71$lcov_version =~ s/\$\s*Revision\s*:?\s*(\S+)\s*\$/$1/;
72
73# Parse command line options
74if (!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
86if ($help)
87{
88	print_usage(*STDOUT);
89	exit(0);
90}
91
92# Check for version option
93if ($version)
94{
95	print("$tool_name: $lcov_version\n");
96	exit(0);
97}
98
99
100# Check for input filename
101if (!$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
108gen_desc();
109
110
111#
112# print_usage(handle)
113#
114# Write out command line usage information to given filehandle.
115#
116
117sub print_usage(*)
118{
119	local *HANDLE = $_[0];
120
121	print(HANDLE <<END_OF_USAGE)
122Usage: $tool_name [OPTIONS] INPUTFILE
123
124Convert 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
130For more information see: $lcov_url
131END_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
150sub 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
214sub warn_handler($)
215{
216	my ($msg) = @_;
217
218	warn("$tool_name: $msg");
219}
220
221sub die_handler($)
222{
223	my ($msg) = @_;
224
225	die("$tool_name: $msg");
226}
227