1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
#!/usr/bin/perl
use Getopt::Std;
# xpostconf - extract parameter info from postconf prototype file
# Usage: xpostconf [options] protofile [parameter...]
#
# -b: Brief output: print only the first sentence of each definition
#
# -c: print the classes named on the command line (default: all).
#
# -h: print help message.
#
# -p: print the parameters named on the command line (default: all).
#
# -s specfile: process the entries listed in the named file: ordinary
# text is copied as is,
# %CLASS class-name mode
# %PARAM param-name mode
# are replaced by the respective information. Mode is b (brief)
# f (full) or i (ignore).
#
# If no -s is specified, extracts the named parameter text (all
# parameters by default).
$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);
# Save one definition.
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";
}
}
# Read the whole file even if we want to print only one parameter.
open(POSTCONF, $protofile) || die " cannot open $protofile: $!\n";
while(<POSTCONF>) {
next if /^#/ && $text eq "";
next unless ($name || /\S/);
if (/^%(PARAM|CLASS)/) {
# Save the accumulated text.
if ($name && $text) {
save_text();
}
# Reset the parameter name and accumulated text.
$name = $text = "";
$category = $1;
# Accumulate the parameter name and default value.
do {
$text .= $_;
} while(($_ = <POSTCONF>) && /\S/);
($junk, $name, $junk) = split(/\s+/, $text, 3);
}
# Accumulate the text in the class or parameter definition.
$text .= $_;
}
# Save the last definition.
if ($name && $text) {
save_text();
}
# If working from a spec file, emit output in the specified order.
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;
}
# Print all the parameters.
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";
}
}
# Print parameters in the specified order.
else {
for $name (@ARGV) {
$text = ${$what}{$name};
$text =~ s/\.\s.*/.\n\n/s if ($opt_b);
print $text;
}
}
|