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