blib/lib/Term/TUI.pm

filter: Statementstmt 15.5% 29 / 186 Branchbran 0.0% 0 / 96 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 33.3% 7 / 21 Podpod 100% 4 / 4 totaltotal 11.5% 40 / 345 SCAR 79.1
CC
60
Cov
10%
_Help
380.0
_Cmd
110.0
TUI_Run
56.0
CRAP 2727.7
↑ Summary Build »
1 package Term::TUI;
2 # Copyright (c) 1999-2008 Sullivan Beck. All rights reserved.
3 # This program is free software; you can redistribute it and/or modify it
4 # under the same terms as Perl itself.
5  
6 ########################################################################
7 # TODO
8 ########################################################################
9  
10 # improve completion:
11 # /math
12 # ad<TAB>
13 # completes correctly to add but
14 # /math/ad<TAB>
15 # doesn't autocomplete.
16  
17 # add abbreviation
18  
19 # case insensitivity
20  
21 # add .. and . to valid mode strings
22  
23 # "Hr. Jochen Stenzel" <Jochen.Stenzel.gp@icn.siemens.de>
24 # alias command
25 # history file (stored last commands)
26  
27 # config file (store commands to execute)
28  
29 ########################################################################
30  
31 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
33 2 $VERSION="1.23";
34  
35 2 require 5.000;
36 2 require Exporter;
37  
38 2 ▶use Term::ReadLine;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 2 ▶use Text::ParseWords;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 #use Text::Abbrev;
41  
42 2 @ISA = qw(Exporter);
43 2 @EXPORT = qw(TUI_Run);
44 2 @EXPORT_OK = qw(TUI_Script TUI_Out TUI_Version);
45 2 %EXPORT_TAGS = (all => [ @EXPORT, @EXPORT_OK ]);
46  
47 2 ▶use strict "vars";
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 sub TUI_Version {
50 0 ▶ return $VERSION;
Subroutine TUI_Version: not called CC=1 SCAR=6.9
Pod documented
51 }
52  
53 BEGIN {
54 2 ▶ my($term,$out);
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 #
57 # Takes a program name (to be used in the prompt) and an interface
58 # description, and runs with it.
59 #
60  
61 #
62 # Interactive version.
63 #
64 sub TUI_Run {
65 0 ▶ my($program,$hashref)=@_;
Subroutine TUI_Run: not called CC=7 SCAR=40.3
Pod documented
66 0 my(@mode,$line,$err);
67 0 my($prompt)="$program> ";
68 0 $term=new Term::ReadLine $program;
69 0 $term->ornaments(0);
70  
71 # Command line completion
72 0 $term->Attribs->{'do_expand'}=1;
73 $term->Attribs->{'completion_entry_function'} =
74 0 $term->Attribs->{'list_completion_function'};
75  
76 0 ▶ $out=$term->OUT || STDOUT;
Condition0%0/2
$term->OUT || 'STDOUT'
l!l
00
MC/DC0%0/1
$term->OUT || 'STDOUT'
$term->OUT
Truth table0%0/2
A
A= $term->OUT
Aresult
11
00
77  
78 0 my($ret)=0;
79  
80 # Command line completion
81 # The strings for completion
82 0 my(@completions) = _GetStrings(\@mode,$hashref);
83 0 $term->Attribs->{'completion_word'} = \@completions;
84  
85 0 ▶ while (defined ($line=$term->readline($prompt)) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = $term->readline($prompt))) 0 0 0
86 0 $err=_Line(\@mode,$hashref,$line);
87  
88 # Command line completion
89 0 @completions = _GetStrings(\@mode,$hashref);
90 0 $term->Attribs->{'completion_word'} = \@completions;
91  
92 0 ▶ if ($err =~ /^exit\[(\d+)\]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err =~ /^exit\[(\d+)\]$/) 0 0 0
93 0 $ret=$1;
94 0 last;
95 }
96 0 ▶ print $out $err if ($err && $err !~ /^\d+$/);
Branch: 1 branch
BranchTrueFalseTotal
if $err and not $err =~ /^\d+$/ 0 0 0
Condition0%0/3
$err and not $err =~ /^\d+$/
!ll&&!rl&&r
000
MC/DC0%0/2
$err and not $err =~ /^\d+$/
$err not $err =~ /^\d+$/
Truth table0%0/3
A and not(B)
A= $errB= not $err =~ /^\d+$/
ABresult
0X0
100
111
97  
98 0 ▶ if (@mode) {
Branch: 1 branch
BranchTrueFalseTotal
if (@mode) { } 0 0 0
99 0 $prompt=$program . ":" . join("/",@mode) . "> ";
100 } else {
101 0 $prompt="$program> ";
102 }
103 }
104 0 return $ret;
105 }
106  
107 #
108 # Non-interactive version.
109 #
110 sub TUI_Script {
111 0 ▶ my($hashref,$script,$sep)=@_;
Subroutine TUI_Script: not called CC=5 SCAR=34.0
Pod documented
112 0 $out=STDOUT;
113  
114 0 ▶ $sep=";" if (! $sep);
Branch: 1 branch
BranchTrueFalseTotal
unless $sep 0 0 0
115 0 my(@cmd)=split(/$sep/,$script);
116  
117 0 my($err,$cmd,@mode);
118 0 my($ret)=0;
119 0 foreach $cmd (@cmd) {
120 0 $err=_Line(\@mode,$hashref,$cmd);
121 0 ▶ if ($err =~ /^exit\[(\d+)\]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err =~ /^exit\[(\d+)\]$/) 0 0 0
122 0 $ret=$1;
123 0 last;
124 }
125 0 ▶ print $out $err if ($err);
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
126 }
127 0 return $ret;
128 }
129  
130 #
131 # Prints a message.
132 #
133 sub TUI_Out {
134 0 ▶ my($mess)=@_;
Subroutine TUI_Out: not called CC=1 SCAR=6.9
Pod documented
135 0 print $out $mess;
136 }
137 }
138  
139  
140 ########################################################################
141 # NOT FOR EXPORT
142 ########################################################################
143  
144 {
145 # Stuff for doing completion.
146  
147 2 my $i;
148 2 my @matches;
149  
150 sub _TUI_completion_function {
151 0 ▶ my($text,$state)=@_;
Subroutine _TUI_completion_function: not called CC=6 SCAR=37.4
152 0 ▶ $i = ($state ? $i : 0);
Branch: 1 branch
BranchTrueFalseTotal
$state ? : 0 0 0
153  
154 0 ▶ if (! $i) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $i) 0 0 0
155 0 ▶ if ($text =~ /^\s*(\S+)\s+(\S+)$/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($text =~ /^\s*(\S+)\s+(\S+)$/) { } 0 0 0
elsif ($text =~ /^\s*(\S+)\s+$/) { } 0 0 0
elsif ($text =~ /^\s*(\S+)$/) { } 0 0 0
156 # MODE CMD^
157 # completes CMD
158 # MODE/CMD OPTION^
159 # no matches
160  
161 } elsif ($text =~ /^\s*(\S+)\s+$/) {
162 # MODE ^
163 # completes CMD
164 # MODE/CMD ^
165 # no matches
166  
167 } elsif ($text =~ /^\s*(\S+)$/) {
168 # MODE^
169 # MODE/CMD^
170  
171 } else {
172 0 @matches=();
173 }
174 }
175 }
176 }
177  
178 #
179 # Takes the current mode (as a list), the interface description, and
180 # the current line and acts on the line.
181 #
182 2 sub _Line {
183 0 ▶ my($moderef,$cmdref,$line)=@_;
Subroutine _Line: not called CC=2 SCAR=17.9
184  
185 0 $line =~ s/\s+$//;
186 0 $line =~ s/^\s+//;
187 0 ▶ return if (! $line);
Branch: 1 branch
BranchTrueFalseTotal
unless $line 0 0 0
188  
189 0 my(@cmd)=shellwords($line);
190 0 return _Cmd($moderef,$cmdref,@cmd);
191 }
192  
193 BEGIN {
194 2 ▶ my(%Cmds) =
Subroutine BEGIN: called CC=1 SCAR=0.0
195 (
196 ".." => [ "Go up one level", "_Mode",0 ],
197 "/" => [ "Go to top level", "_Mode",1 ],
198 "help" => [ "Online help", "_Help" ],
199 "exit" => [ "Exit", "_Exit",0 ],
200 "quit" => [ "An alias for exit", "_Exit",0 ],
201 "abort" => [ "Exit without saving", "_Exit",1 ]
202 );
203 2 my($Moderef,$Cmdref);
204  
205 #
206 # Returns an array of strings (commands or modes) that can be
207 # entered given a mode
208 #
209 sub _GetStrings {
210 0 ▶ my ($moderef,$cmdref) = @_;
Subroutine _GetStrings: not called CC=4 SCAR=30.0
211 0 my @strings;
212  
213 0 ▶ if (!defined $Cmdref || ref $Cmdref ne "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $Cmdref or ref $Cmdref ne "HASH") 0 0 0
Condition0%0/3
not defined $Cmdref or ref $Cmdref ne "HASH"
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $Cmdref or ref $Cmdref ne "HASH"
not defined $Cmdref ref $Cmdref ne "HASH"
Truth table0%0/3
not(A) or B
A= not defined $CmdrefB= ref $Cmdref ne "HASH"
ABresult
1X1
011
000
214 0 $Cmdref = $cmdref;
215 }
216 0 my $desc = _GetMode(@{$moderef});
217 0 ▶ if ( ref $desc eq "HASH" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $desc eq "HASH") 0 0 0
218 0 @strings = grep !/^\./, sort keys %$desc;
219 }
220 0 push @strings,keys %Cmds;
221 0 return @strings;
222 }
223  
224 #
225 # Takes the current mode (as a list), the interface description, and the
226 # current command (as a list) and executes the command.
227 #
228 sub _Cmd {
229 0 ▶ my($moderef,$cmdref,@args)=@_;
Subroutine _Cmd: not called CC=10 SCAR=47.0
230 0 my($cmd)=shift(@args);
231 0 $Moderef=$moderef;
232 0 $Cmdref=$cmdref;
233 0 my(@mode,$desc,$mode,$help);
234  
235 0 ▶ if (exists $Cmds{lc $cmd}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Cmds{lc $cmd}) { } 0 0 0
236 0 $desc=$Cmds{lc $cmd};
237  
238 } else {
239 0 ($mode,@mode)=_CheckMode(\$cmd);
240  
241 0 ▶ if ($mode && $cmd) {
Branch: 3 branches
BranchTrueFalseTotal
if ($mode and $cmd) { } 0 0 0
elsif ($mode and @args) { } 0 0 0
elsif ($mode) { } 0 0 0
Condition0%0/3
$mode and $cmd
!ll&&!rl&&r
000
MC/DC0%0/2
$mode and $cmd
$mode $cmd
Truth table0%0/3
A and B
A= $modeB= $cmd
ABresult
0X0
100
111
242 #
243 # MODE/CMD [ARGS]
244 # CMD [ARGS]
245 #
246 0 $desc=_CheckCmd($mode,$cmd);
247  
248 ▶ } elsif ($mode && @args) {
Condition0%0/3
$mode and @args
!ll&&!rl&&r
000
MC/DC0%0/2
$mode and @args
$mode @args
Truth table0%0/3
A and B
A= $modeB= @args
ABresult
0X0
100
111
249 #
250 # MODE CMD [ARGS]
251 #
252 0 $cmd=shift(@args);
253 0 $desc=_CheckCmd($mode,$cmd);
254  
255 } elsif ($mode) {
256 #
257 # MODE
258 #
259 0 $desc=[ "","_Mode",2,@mode ]
260 }
261 }
262  
263 0 my(@args0);
264 0 ▶ if (ref $desc eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $desc eq 'ARRAY') { } 0 0 0
265 0 ($help,$cmd,@args0)=@$desc;
266 0 ▶ if (! defined &$cmd) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &$cmd) 0 0 0
267 0 $cmd="::$cmd";
268 0 ▶ if (! defined &$cmd) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &$cmd) 0 0 0
269 0 return "ERROR: invalid subroutine\n";
270 }
271 }
272 0 return &$cmd(@args0,@args);
273 } else {
274 0 return "ERROR: unknown command\n";
275 }
276 }
277  
278 #
279 # Takes a mode and/or command (as a list) and determines the mode
280 # to use. Returns a description of that mode.
281 #
282 sub _CheckMode {
283 0 ▶ my($cmdref)=@_;
Subroutine _CheckMode: not called CC=5 SCAR=34.0
284 0 my($cmd)=$$cmdref;
285 0 my(@mode,$tmp2);
286  
287 0 ▶ if ($cmd =~ s,^/,,) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd =~ s[^/][]) { } 0 0 0
288 0 @mode=split(m|/|,$cmd);
289 } else {
290 0 @mode=(@$Moderef,split(m|/|,$cmd));
291 }
292  
293 0 my($tmp)=_GetMode(@mode);
294 0 ▶ if ($tmp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp) { } 0 0 0
295 0 $$cmdref="";
296 } else {
297 0 $tmp2=pop(@mode);
298 0 $tmp=_GetMode(@mode);
299 0 ▶ $$cmdref=$tmp2 if ($tmp);
Branch: 1 branch
BranchTrueFalseTotal
if $tmp 0 0 0
300 }
301  
302 0 ▶ @mode=() if (! $tmp);
Branch: 1 branch
BranchTrueFalseTotal
unless $tmp 0 0 0
303 0 return ($tmp,@mode);
304 }
305  
306 #
307 # Takes a mode (as a list) and returns it's description (or "" if it's
308 # not a mode).
309 #
310 sub _GetMode {
311 0 ▶ my(@mode)=@_;
Subroutine _GetMode: not called CC=4 SCAR=30.0
312 0 my($tmp)=$Cmdref;
313 0 my($mode);
314  
315 0 foreach $mode (@mode) {
316 0 ▶ if (exists $$tmp{$mode} &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $tmp->{$mode} and ref $tmp->{$mode} eq 'HASH') { } 0 0 0
Condition0%0/3
exists $tmp->{$mode} and ref $tmp->{$mode} eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $tmp->{$mode} and ref $tmp->{$mode} eq "HASH"
exists $tmp->{$mode} ref $tmp->{$mode} eq "HASH"
Truth table0%0/3
A and B
A= exists $tmp->{$mode}B= ref $tmp->{$mode} eq "HASH"
ABresult
0X0
100
111
317 ref $$tmp{$mode} eq "HASH") {
318 0 $tmp=$$tmp{$mode};
319 } else {
320 0 $tmp="";
321 0 last;
322 }
323 }
324 0 $tmp;
325 }
326  
327 ##############################################
328  
329 #
330 # A command to change the mode.
331 # .. op=0
332 # / op=1
333 # MODE op=2
334 #
335 sub _Mode {
336 0 ▶ my($op,@mode)=@_;
Subroutine _Mode: not called CC=5 SCAR=34.0
337  
338 0 ▶ if ($op==0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($op == 0) { } 0 0 0
elsif ($op == 1) { } 0 0 0
elsif ($op == 2) { } 0 0 0
339 # Up one level
340 0 ▶ if ($#$Moderef>=0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Moderef->$#* >= 0) { } 0 0 0
341 0 pop(@$Moderef);
342 } else {
343 0 return "WARNING: Invalid operation\n";
344 }
345  
346 } elsif ($op==1) {
347 # Top
348 0 @$Moderef=();
349  
350 } elsif ($op==2) {
351 # Change modes
352 0 @$Moderef=@mode;
353  
354 } else {
355 0 return "ERROR: Invalid mode operation: $op\n";
356 }
357 0 return "";
358 }
359  
360 sub _Help {
361 0 ▶ my($cmd,@args)=@_;
Subroutine _Help: not called CC=19 SCAR=59.4
362  
363 0 my($tmp,$mode,@mode);
364  
365 0 ▶ ($tmp,@mode)=_CheckMode(\$cmd) if ($cmd);
Branch: 1 branch
BranchTrueFalseTotal
if $cmd 0 0 0
366 0 ▶ if (! $tmp) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tmp) 0 0 0
367 0 @mode=@$Moderef;
368 0 ▶ if (@mode) {
Branch: 1 branch
BranchTrueFalseTotal
if (@mode) { } 0 0 0
369 0 $tmp=_GetMode(@mode);
370 } else {
371 0 $tmp=$Cmdref;
372 }
373 }
374  
375 0 ▶ return "IMPOSSIBLE: invalid mode\n" if (! $tmp);
Branch: 1 branch
BranchTrueFalseTotal
unless $tmp 0 0 0
376  
377 0 my($mess);
378 0 ▶ $cmd=shift(@args) if (! $cmd && @args);
Branch: 1 branch
BranchTrueFalseTotal
if not $cmd and @args 0 0 0
Condition0%0/3
not $cmd and @args
!ll&&!rl&&r
000
MC/DC0%0/2
not $cmd and @args
not $cmd @args
Truth table0%0/3
not(A) and B
A= not $cmdB= @args
ABresult
0X0
100
111
379 0 ▶ if ($cmd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd) { } 0 0 0
380 #
381 # Help on a command
382 #
383 0 ▶ if (exists $Cmds{$cmd}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $Cmds{$cmd}) { } 0 0 0
elsif (exists $tmp->{$cmd}) { } 0 0 0
384 0 $tmp=$Cmds{$cmd};
385 0 $mess=$$tmp[0];
386  
387 } elsif (exists $$tmp{$cmd}) {
388 0 $tmp=$$tmp{$cmd};
389 0 ▶ if (ref $tmp ne "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tmp ne 'ARRAY') { } 0 0 0
390 0 $mess="Invalid command $cmd";
391 } else {
392 0 $mess=$$tmp[0];
393 0 ▶ $mess="No help available" if (! $mess);
Branch: 1 branch
BranchTrueFalseTotal
unless $mess 0 0 0
394 }
395 } else {
396 0 $mess="Invalid command: $cmd";
397 }
398  
399 } else {
400 #
401 # Help on a mode
402 #
403 0 ▶ if (exists $$tmp{".HELP"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $tmp->{'.HELP'}) { } 0 0 0
404 0 $mess=$$tmp{".HELP"};
405 0 my(@gc)=sort grep /^([^.]|\.\.)/i,keys %Cmds;
406 0 my(@cmd)=sort grep /^[^.]/,keys %{ $tmp };
407 0 my(@m,@c)=();
408 0 foreach $cmd (@cmd) {
409 0 ▶ if (ref $$tmp{$cmd} eq "ARRAY") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $tmp->{$cmd} eq 'ARRAY') { } 0 0 0
elsif (ref $tmp->{$cmd} eq 'HASH') { } 0 0 0
410 0 push(@c,$cmd);
411 } elsif (ref $$tmp{$cmd} eq "HASH") {
412 0 push(@m,$cmd);
413 }
414 }
415 0 $mess .= "\n\nAdditional help:\n\n";
416 0 ▶ $mess .= " Modes: @m\n" if (@m);
Branch: 1 branch
BranchTrueFalseTotal
if @m 0 0 0
417 0 $mess .= " Cmds : @gc";
418 0 ▶ $mess .= "\n" if (@c);
Branch: 1 branch
BranchTrueFalseTotal
if @c 0 0 0
419 0 ▶ $mess .= " @c" if (@c);
Branch: 1 branch
BranchTrueFalseTotal
if @c 0 0 0
420  
421 } else {
422 0 $mess="No help available";
423 }
424 }
425  
426 0 return "\n$mess\n\n";
427 }
428 }
429  
430 #
431 # Takes a mode and command and return a description of the command.
432 #
433 sub _CheckCmd {
434 0 ▶ my($moderef,$cmd)=@_;
Subroutine _CheckCmd: not called CC=3 SCAR=24.8
435 return $$moderef{$cmd}
436 if (exists $$moderef{$cmd} &&
437 0 ▶ ref $$moderef{$cmd} eq "ARRAY");
Branch: 1 branch
BranchTrueFalseTotal
if exists $moderef->{$cmd} and ref $moderef->{$cmd} eq "ARRAY" 0 0 0
Condition0%0/3
exists $moderef->{$cmd} and ref $moderef->{$cmd} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $moderef->{$cmd} and ref $moderef->{$cmd} eq "ARRAY"
exists $moderef->{$cmd} ref $moderef->{$cmd} eq "ARRAY"
Truth table0%0/3
A and B
A= exists $moderef->{$cmd}B= ref $moderef->{$cmd} eq "ARRAY"
ABresult
0X0
100
111
438 0 return ();
439 }
440  
441 sub _Exit {
442 0 ▶ my($flag)=@_;
Subroutine _Exit: not called CC=1 SCAR=6.9
443 0 return "exit[$flag]";
444 }
445  
446 # sub {
447 # map {lc($_)} (keys %commands, keys %aliases)
448 # };
449  
450 # $term->Attribs->{'do_expand'}=1;
451 # $term->Attribs->{'completion_entry_function'} =
452 # sub {
453 # $term->Attribs->{'line_buffer'} =~ /\s/ ?
454 # &{$term->Attribs->{'filename_completion_function'}}(@_) :
455 # &{$term->Attribs->{'list_completion_function'}}(@_)
456 # };
457 # $term->Attribs->{'completion_word'}=[(map {lc($_)} (keys %commands))];
458  
459 2 1;
460 # Local Variables:
461 # mode: cperl
462 # indent-tabs-mode: nil
463 # cperl-indent-level: 3
464 # cperl-continued-statement-offset: 2
465 # cperl-continued-brace-offset: 0
466 # cperl-brace-offset: 0
467 # cperl-brace-imaginary-offset: 0
468 # cperl-label-offset: -2
469 # End:
↑ Summary Build »