use Getopt::Std;
$opt_b = undef;
$opt_c = undef;
$opt_p = undef;
$opt_s = undef;
$opt_v = undef;
getopts("bcps:v");
die "Usage: $0 [-bcpv] [-s specfile] protofile [parameter...]\n"
unless $protofile = shift(@ARGV);
sub save_text {
if ($category eq "PARAM") {
$param_text{$name} = $text;
if ($opt_v) {
printf "saving entry %s %.20s..\n", $name, $text;
}
} elsif ($category eq "CLASS") {
$class_text{$name} = $text;
if ($opt_v) {
printf "saving class %s %.20s..\n", $name, $text;
}
} else {
die "Unknown category: $category. Need PARAM or CLASS.\n";
}
}
open(POSTCONF, $protofile) || die " cannot open $protofile: $!\n";
while(<POSTCONF>) {
next if /^ next unless ($name || /\S/);
if (/^%(PARAM|CLASS)/) {
if ($name && $text) {
save_text();
}
$name = $text = "";
$category = $1;
do {
$text .= $_;
} while(($_ = <POSTCONF>) && /\S/);
($junk, $name, $junk) = split(/\s+/, $text, 3);
}
$text .= $_;
}
if ($name && $text) {
save_text();
}
if ($opt_s) {
open(SPEC, "$opt_s") || die "cannot open $opt_s: $!\m";
while(<SPEC>) {
if (/^%/) {
($category, $name, $mode) = split(/\s+/, substr($_, 1));
if ($category eq "CLASS") {
die "Unknown class name: $name.\n"
unless $text = $class_text{$name};
} elsif ($category eq "PARAM") {
die "Unknown parameter name: $name.\n"
unless $text = $param_text{$name};
} else {
die "Unknown category: $category. Need CLASS or PARAM\n";
}
if ($mode eq "i") {
next;
} elsif ($mode eq "b") {
$text =~ s/\.\s.*/.\n\n/s;
} elsif ($mode ne "p") {
die "Unknown mode: $mode. Need b or p or i,\n";
}
print $text, "\n";
} else {
print;
}
}
exit;
}
if ($opt_c) {
$what = \%class_text;
} else {
$what = \%param_text;
}
if ($#ARGV < 0) {
for $name (sort keys %{$what}) {
$text = ${$what}{$name};
$text =~ s/\.\s.*/.\n\n/s if ($opt_b);
print $text, "\n";
}
}
else {
for $name (@ARGV) {
$text = ${$what}{$name};
$text =~ s/\.\s.*/.\n\n/s if ($opt_b);
print $text;
}
}