CfgTie/CfgArgs.pm

filter: Statementstmt 31.0% 18 / 58 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 22.2% 2 / 9 Podpod 0.0% 0 / 7 totaltotal 19.6% 20 / 102 CC 15 SCAR 37.6
CC
15
Cov
22%
Args_exclusive
72.0
Args_parse
56.0
FmtEStr
6.0
CRAP 43.0
« Secure/File.pm ↑ Summary CfgTie/filever.pm »
1 #!/usr/bin/perl
2 #Copyright 1998-2001, Randall Maas. All rights reserved. This program is free
3 #software; you can redistribute it and/or modify it under the same terms as
4 #PERL itself.
5  
6 package CfgTie::CfgArgs;
7 1 require Exporter;
8 1 @ISA=qw(Exporter);
9 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 $VERSION='0.41';
11 1 @EXPORT_OK=qw(Fatal Err $Prg);
12  
13 =head1 NAME
14  
15 C<CfgTie::CfgArgs> -- Configuration module for parsing commandline arguments
16  
17 =head1 SYNOPSIS
18  
19 This module is meant to help create useful configuration tools and utilities.
20  
21 =head1 DESCRIPTION
22  
23 A tool to allow many of your computer's subsystems to be configured. This
24 module parses commandline arguments. It is provided to help create a
25 standardized lexicon.
26  
27 =head2 Scope controls and settings
28  
29 To specify how much of your system should be affected by the change in
30 settings:
31  
32 --scope=session|application|user|group|system
33  
34  
35 In addition, each of the individual parts can specified (instead of their
36 defaults):
37  
38 =over 1
39  
40 =item C<--application=>I<NAME>
41  
42 =item C<--application >I<NAME>
43  
44 This specifies the application.
45  
46 =item C<--user=>I<NAME>
47  
48 =item C<--user >I<NAME>
49  
50 This specifies the user name.
51  
52 =item C<--group=>I<NAME>
53  
54 =item C<--group >I<NAME>
55  
56 This specifies the group name.
57  
58 =back
59  
60 =head2 Operations on variables
61  
62 The specific operation to be done:
63  
64 --op=set|unset|remove|delete|exists|fetch|get|copy|rename
65  
66 or:
67  
68 --copy name1=name2 name3=name4 ...
69 --exists name1 name2 name3 ...
70 --test name1=value1 name2=value2 ...
71 --unset name1 name2 ...
72  
73 =over 1
74  
75 =item C<--delete >I<NAME>
76  
77 =item C<--delete=>I<NAME>
78  
79 This will remove the entry specified by I<NAME>. I<NAME> may be a regular
80 expression.
81  
82 =item C<--fetch >I<NAME>
83  
84 =item C<--fetch=>I<NAME>
85  
86 This will retrieve the information associated with I<NAME>. If I<NAME> is a
87 regular expression, information will retrieved for every entry that matches the
88 pattern.
89  
90 =item C<--remove >I<NAME>
91  
92 =item C<--remove=>I<NAME>
93  
94 Like C<delete> above, this will remove the entry specified by I<NAME>. I<NAME>
95 may be a regular expression.
96  
97 =item C<--rename> I<NAME-NEW>=I<NAME-OLD>
98  
99 This will change all of the occurrences or references that match I<NAME-OLD> to
100 the newer form of I<NAME-NEW>. This may be a regular expression, similar to;
101  
102 s/NAME-OLD/NAME-NEW/
103  
104 =item C<--set> I<NAME>=I<VALUE>
105  
106 This will create an entry called I<NAME> with a setting of I<VALUE>.
107  
108 =back
109  
110  
111 The variable names are optional, and can be explicitly specified:
112  
113 --name
114  
115 Otherwise it is assumed to be the first no flag parameter.
116  
117 Similarly, the value can be specified
118  
119 --value
120  
121 =head2 Other flags
122  
123 =over 1
124  
125 =item C<--file >FILE
126  
127 =item C<--file=>FILE
128  
129 This specifies the configuration file to employ. If none is specified, the
130 default for the particular subsystem will be used instead.
131  
132 =item C<--comment >COMMENT
133  
134 =item C<--comment=>COMMENT
135  
136 This provides a text comment on what changes are being made.
137  
138 =back
139  
140 -n,
141 --dry-run,
142 --just-print
143 --recon
144  
145 With these flags, the utility program I<should not modify any files>.
146 Instead, it should merely document what changes it would make, what programs
147 it would run, etc.
148  
149 --copyright
150 --help
151 --info
152 --information
153 --manual
154 --verbose
155 --version
156 --warranty
157  
158 =head2 Exit value
159  
160 If the operation exists the return value is zero, otherwise it is nonzero.
161  
162 =head2 Return from parsing
163  
164 The hash return:
165  
166 {
167 SCOPE=> session,application,user,group,system
168 OP => COPY, RENAME, STORE, DELETE, FETCH, or EXISTS
169 KEY =>
170 VALUE=>
171 }
172  
173 =head1 AUTHOR
174  
175 Randall Maas (L<mailto:randym@acm.org>, L<http://www.hamline.edu/~rcmaas/>)
176  
177 =cut
178  
179 1 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
180  
181 1 my @GetoptLong_Rules=(
182 "scope=s","application:s","session!","user:s","group:s","op=s",
183  
184 #Options that take associative arrays
185 "set:s%", "rename:s%","copy:s%",
186  
187 #Options that take lists
188 "remove|delete|unset:s@","exists:s@","fetch:s@",
189  
190 #Options that take single values
191 "system:s","name=s","value=s","file=s",
192  
193 #Control of wether or not it will actually do it
194 "dry-run|just-print|recon!",
195 "list!",
196 #Miscellaneous stuff:
197 "copyright!", "help|info|information|manual:s","warranty!","verbose!",
198 "version!");
199  
200 1 my $Op2Op =
201 {
202 set => STORE,
203 unset => DELETE,
204 remove => DELETE,
205 'delete'=> DELETE,
206 'exists'=> EXISTS,
207 fetch => FETCH,
208 get => GET,
209 name => KEY,
210 key => KEY,
211 value => VALUE,
212 group => GROUP,
213 user => USER,
214 application=>APPLICATION,
215 };
216  
217 sub Args_parse (@)
218 0 ▶{
Subroutine Args_parse: not called CC=7 SCAR=40.3
Pod undocumented
219 0 my $N ={};
220 0 foreach my $I (@_)
221 {
222 0 ▶ if (/^\s*--scope=(session|application|user|group|system|rename|copy)\s*$/i)
Branch: 3 branches
BranchTrueFalseTotal
if (/^\s*--scope=(session|application|user|group|system|rename|copy)\s*$/i) { } 0 0 0
elsif (/^\s*--(?=op=)(set|unset|remove|delete|exists|fetch|get)\s*$/i) { } 0 0 0
elsif (/^\s*--(application|user|group|name|value|key)=(.+)\s*$/i) { } 0 0 0
223 {
224 0 ▶ if (!exists $N->{SCOPE}) {$N->{SCOPE}=lc($1);}
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $N->{'SCOPE'}) { } 0 0 0
225 0 else {warn "multiple attempts to set scope (to $1)!\n";}
226 }
227 elsif (/^\s*--(?=op=)(set|unset|remove|delete|exists|fetch|get)\s*$/i)
228 0 {$N->{OP}=$Op2Op->{lc($1)};}
229 elsif (/^\s*--(application|user|group|name|value|key)=(.+)\s*$/i)
230 {
231 0 my $k=$Op2Op->{lc($1)};
232 0 ▶ if (!exists $N->{$k}) {$N->{$k}=lc($2);}
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $N->{$k}) { } 0 0 0
233 0 else {warn "multiple attempts to set $k (to $2)!\n";}
234 }
235 # elsif (!exists $N->{KEY} && /^\s*xxx
236 # elsif (!exists $N->{VALUE}
237 # else {save this for later...}
238 }
239 }
240  
241 #while <F>
242 # Args_parse(split /(?:\\)\s/);
243 #}
244  
245  
246 sub Help()
247 0 ▶{
Subroutine Help: not called CC=1 SCAR=6.9
Pod undocumented
248 #This returns a text string describing the flags. It is meant to help
249 #put together a command line tool help message
250 0 my $L="\t ";
251 0 "Main operation mode:\n".
252 "$L--op=set|unset|remove|delete|exists|fetch|get|copy|rename\n".
253 "$L--copy name1=name2 name3=name4 ...\n".
254 "$L--delete name1 name2 ...\n".
255 "$L--exists name1 name2 name3 ...\n".
256 "$L--fetch name1 name2 name3 ...\n".
257 "$L--remove name1 name2 ...\n".
258 "\nSpecifying scope of actions:\n".
259 "$L--scope=session|application|user|group|system\n".
260 "$L--application=\n\t\t\tThis allows you specify which application\n".
261 "$L--user=\tSpecifies which user. If option is used without a user\n".
262 "\t\t\tname, it employs the effective user ID.\n".
263 "$L--group=\tSpecifies which group. If this option is used without a\n".
264 "\t\t\tgroup name, the effective group ID is employed.\n".
265 "\t\n".
266 "\nInformative output:\n".
267 "$L--copyright\tPrints this programs copyright information.\n".
268 "$L--help\tPrints this help list. Exits without doing any\n\t\t\toperation\n".
269 "$L--verbose\t\n".
270 "$L--version\tDisplays this programs version information. Exits\n".
271 "\t\t\twithout doing any operation.\n".
272 "$L--warranty\tDisplays the programs warranty information. Exits\n".
273 "\t\t\twithout doing any operation.\n".
274 "\n";
275 }
276  
277 sub Args_exclusive
278 0 ▶{
Subroutine Args_exclusive: not called CC=8 SCAR=42.8
Pod undocumented
279 0 my $MyArgs=shift;
280 0 my @Rest = @_;
281 0 my $ExitNum=0;
282 0 foreach my $I (@_)
283 {
284 0 shift @Rest;
285 0 ▶ if (!defined $I || !exists $MyArgs->{$I}) {next;}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $I and exists $MyArgs->{$I}) 0 0 0
Condition0%0/3
defined $I and exists $MyArgs->{$I}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $I and exists $MyArgs->{$I}
defined $I exists $MyArgs->{$I}
Truth table0%0/3
A and B
A= defined $IB= exists $MyArgs->{$I}
ABresult
0X0
100
111
286 0 foreach my $J (@Rest)
287 {
288 0 ▶ if (!defined $J || !exists $MyArgs->{$J}) {next;}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $J and exists $MyArgs->{$J}) 0 0 0
Condition0%0/3
defined $J and exists $MyArgs->{$J}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $J and exists $MyArgs->{$J}
defined $J exists $MyArgs->{$J}
Truth table0%0/3
A and B
A= defined $JB= exists $MyArgs->{$J}
ABresult
0X0
100
111
289 0 $ExitNum=-1;
290 0 ▶ if (defined $main::Prg) {print STDERR $main::Prg,": ";}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $main::Prg) 0 0 0
291 0 print STDERR "--$I and --$J modes can not be intermixed.\n";
292 }
293 }
294 0 $ExitNum;
295 }
296  
297 sub do
298 0 ▶{
Subroutine do: not called CC=1 SCAR=6.9
Pod undocumented
299 0 GetOptions(shift, @GetoptLong_Rules, @_);
300 }
301  
302 # --- Error handling ----------------------------------------------------------
303 #Def of good error message
304 # 1. Identifies the module generating the message (manufacturer, type, etc)
305 # 2. Identifies the error type (a code number usually)
306 # 3. Identifies the subsystems or relevant locus of a problem -
307 # 4. Human description of the problem
308 # 5. What will, should, or needs to happen
309  
310 1 my %Msgs;
311 1 my $Lang='english';
312 1 $Msgs{'english'} =
313 {
314 EXITING => "Exiting.",
315 GROUP_MISSING => "Group was not specified",
316 USER_NONAME => "Your user id does not have a name.",
317 USER_BAD_REAL => "Real user id is root. This is not allowed.",
318 USER_BAD_EFF => "Effective user id is root. This is not allowed, ".
319 "using the real user id.",
320 TAINTED => "\\1 is tainted.",
321 EMAIL_BAD => "Item to add has invalid email address: \"\\1\"",
322 FILE_NEXIST => "file \"\\1\" does not exist!",
323 FILE_EXISTS => "file \"\\1\" exists, will not overwrite.",
324 FILE_CANTCREATE=> "Can not create file \"\\1\" for output.",
325 };
326 1 my $ErrMsgs = $Msgs{$Lang};
327  
328 sub FmtEStr
329 0 ▶{
Subroutine FmtEStr: not called CC=2 SCAR=17.9
Pod undocumented
330 0 my ($E,$A)=@_;
331 0 my $Str= $ErrMsgs->{$E};
332 0 ▶ if (!defined $Str)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Str) 0 0 0
333 0 {warn "Can't look up error message for $E\n"; $Str=$E;}
334
335 #Add in the users parameters
336 0 $Str=~ s/\\1/$A/g;
337 0 $Str;
338 }
339  
340 1 $Prg='';
341 sub Fatal
342 0 ▶{
Subroutine Fatal: not called CC=1 SCAR=6.9
Pod undocumented
343 0 my $Str=CfgTie::CfgArgs::FmtEStr(@_);
344  
345 #Announce to the user, and die
346 0 die "$Prg: $Str\n";
347 }
348  
349 sub Err
350 0 ▶{
Subroutine Err: not called CC=1 SCAR=6.9
Pod undocumented
351 0 my $Str=CfgTie::CfgArgs::FmtEStr(@_);
352  
353 #Announce to the user
354 0 print STDERR "$Prg: $Str\n";
355 }
356  
357 1 1;
« Secure/File.pm ↑ Summary CfgTie/filever.pm »