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