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