| 1 | #!/usr/bin/perl | ||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||
| 3 | # my $self_running = ( ! caller ); | ||||||||||||||||||||||||||||||||||||
| 4 | # END { Term::ShellKit::shell if ( $self_running ); } | ||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||
| 6 | ###################################################################### | ||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||
| 8 | package Term::ShellKit; | ||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||
| 10 | 1 | $VERSION = 1.002; | |||||||||||||||||||||||||||||||||||
| 11 | 1 | @EXPORT = qw( shell ); | |||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | sub import { require Exporter and goto &Exporter::import } # lazy Exporter | ||||||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use Text::ParseWords; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||
| 18 | ###################################################################### | ||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use vars qw( $Prompt $SubReadLine ); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||
| 22 | 1 | ▶ | $SubReadLine = ( -t STDIN ) ? \&readline_term : \&readline_raw; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 23 | 1 | ▶ | $Prompt ||= 'Term::ShellKit> '; | ||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$Term::ShellKit::Prompt ||= "Term::ShellKit> "
MC/DC0%0/1
$Term::ShellKit::Prompt ||= "Term::ShellKit> "
$Term::ShellKit::Prompt
Truth table50%1/2
A
A= $Term::ShellKit::Prompt
| |||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||
| 25 | 1 | ▶ | sub readline_raw { | ||||||||||||||||||||||||||||||||||
|
Subroutine
readline_raw: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 26 | 1 | ▶ | my $prompt = scalar(@_) ? shift : $Prompt; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 27 | 1 | print($prompt); | |||||||||||||||||||||||||||||||||||
| 28 | 1 | $_ = <>; | |||||||||||||||||||||||||||||||||||
| 29 | 1 | ▶ | chomp if defined $_; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 30 | 1 | print($_."\n"); | |||||||||||||||||||||||||||||||||||
| 31 | 1 | $_ | |||||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||
| 34 | 1 | my $TermReadLine; | |||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | sub readline_term { | ||||||||||||||||||||||||||||||||||
|
Subroutine
readline_term: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 37 | 0 | ▶ | if ( ! $TermReadLine ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 38 | 0 | require Term::ReadLine; | |||||||||||||||||||||||||||||||||||
| 39 | 0 | $TermReadLine = Term::ReadLine->new('Term-ShellKit'); | |||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||
| 41 | # $ShellReadLine->OUT is autoflushed anyway | ||||||||||||||||||||||||||||||||||||
| 42 | 0 | my $odef = select STDERR; | |||||||||||||||||||||||||||||||||||
| 43 | 0 | $| = 1; | |||||||||||||||||||||||||||||||||||
| 44 | 0 | select STDOUT; | |||||||||||||||||||||||||||||||||||
| 45 | 0 | $| = 1; | |||||||||||||||||||||||||||||||||||
| 46 | 0 | select $odef; | |||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | my $prompt = scalar(@_) ? shift : $Prompt; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 49 | 0 | $TermReadLine->readline( $prompt ) | |||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||
| 52 | ###################################################################### | ||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||
| 54 | 1 | ▶ | use vars qw( $SubDoCommand $CurrentPackage @CommandPackages ); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||
| 56 | 1 | $SubDoCommand = \&do_command; | |||||||||||||||||||||||||||||||||||
| 57 | 1 | ▶ | $CurrentPackage ||= 'main'; | ||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$Term::ShellKit::CurrentPackage ||= "main"
MC/DC0%0/1
$Term::ShellKit::CurrentPackage ||= "main"
$Term::ShellKit::CurrentPackage
Truth table50%1/2
A
A= $Term::ShellKit::CurrentPackage
| |||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||
| 59 | 1 | ▶ | use vars qw( @CommandQueue ); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||
| 61 | 3 | ▶ | sub do_command { | ||||||||||||||||||||||||||||||||||
|
Subroutine
do_command: called CC=18 SCAR=43.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 62 | 3 | my $input = shift; | |||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||
| 64 | 3 | ▶ | length $input or return; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||
| 66 | 3 | ▶ | $input =~ /\A\s*(\S+)(?:\s(?![\)])(.*))?\Z/ | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 67 | or die "Can't parse command line '$input'\n"; | ||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||
| 69 | 3 | my ($command, $args) = ( $1, $2 ); | |||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||
| 71 | 3 | my $sub; | |||||||||||||||||||||||||||||||||||
| 72 | 3 | ▶ | if ( $command =~ /^(.*)::([^:]+)$/ ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 73 | 3 | my ($pack, $func) = ($1, $2); | |||||||||||||||||||||||||||||||||||
| 74 | 3 | $sub = UNIVERSAL::can($pack, $func); | |||||||||||||||||||||||||||||||||||
| 75 | } else { | ||||||||||||||||||||||||||||||||||||
| 76 | 0 | foreach my $package ( $CurrentPackage, @CommandPackages ) { | |||||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | if ( $sub = UNIVERSAL::can($package, $command) ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 78 | 0 | last; | |||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||
| 82 | 3 | ▶ | if ( ! $sub ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 83 | 0 | foreach my $package ( @CommandPackages ) { | |||||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | my $rewriter = UNIVERSAL::can($package, '_shell_rewrite') or next; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 85 | 0 | my @out = &$rewriter( $input ); | |||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | if ( scalar @out ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 87 | 0 | unshift @CommandQueue, @out; | |||||||||||||||||||||||||||||||||||
| 88 | 0 | return; | |||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||
| 91 | 0 | die "Can't find command or function named '$command'\n"; | |||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||
| 94 | 3 | my $ptype = prototype( $sub ); | |||||||||||||||||||||||||||||||||||
| 95 | 3 | my @args; | |||||||||||||||||||||||||||||||||||
| 96 | 3 | ▶ | if ( ! defined $ptype or $ptype eq '@' ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition66%2/3
not defined $ptype or $ptype eq "\@"
MC/DC50%1/2
not defined $ptype or $ptype eq "\@"
not defined $ptype
$ptype eq "\@"
Truth table66%2/3
not(A) or B
A= not defined $ptypeB= $ptype eq "\@"
| |||||||||||||||||||||||||||||||||||||
| 97 | 2 | eval { @args = Text::ParseWords::shellwords($args) }; | |||||||||||||||||||||||||||||||||||
| 98 | 2 | ▶ | croak("Can't parse arguments for $command($ptype): $@") if $@; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 99 | } elsif ( $ptype eq ';$' ) { | ||||||||||||||||||||||||||||||||||||
| 100 | 1 | ▶ | @args = defined($args) ? $args : (); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 101 | } elsif ( $ptype eq '$' ) { | ||||||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | croak("Missing required argument for $command($ptype)") unless (length $args); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 103 | 0 | @args = $args; | |||||||||||||||||||||||||||||||||||
| 104 | } else { | ||||||||||||||||||||||||||||||||||||
| 105 | 0 | eval { @args = Text::ParseWords::shellwords($args) }; | |||||||||||||||||||||||||||||||||||
| 106 | 0 | ▶ | croak("Can't parse arguments for $command($ptype): $@") if $@; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||
| 108 | 3 | &$sub( @args ); | |||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | sub command_rewrite { | ||||||||||||||||||||||||||||||||||
|
Subroutine
command_rewrite: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 112 | 0 | unshift @CommandQueue, @_; | |||||||||||||||||||||||||||||||||||
| 113 | 0 | die "Term::ShellKit command completed"; | |||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||
| 116 | ###################################################################### | ||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||
| 118 | 1 | ▶ | use vars qw( $PrintResultsSub ); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 119 | 1 | $PrintResultsSub = \&print_results; | |||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||
| 121 | # require Dumpvalue; | ||||||||||||||||||||||||||||||||||||
| 122 | # $Dumper = Dumpvalue->new(); | ||||||||||||||||||||||||||||||||||||
| 123 | # print $Dumper->dumpValue($value); | ||||||||||||||||||||||||||||||||||||
| 124 | 3 | ▶ | sub print_results { | ||||||||||||||||||||||||||||||||||
|
Subroutine
print_results: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 125 | 3 | ▶ | print join '', map { /\n\Z/m ? $_ : "$_\n" } grep { length $_ } @_; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||
| 128 | ###################################################################### | ||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||
| 130 | 1 | ▶ | use vars qw( @DefaultStartup ); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 131 | 1 | @DefaultStartup = ( | |||||||||||||||||||||||||||||||||||
| 132 | 'Term::ShellKit::require_package Term::ShellKit::Commands', | ||||||||||||||||||||||||||||||||||||
| 133 | 'Term::ShellKit::Commands::echo Term::ShellKit: Starting interactive shell; commands include help, exit.', | ||||||||||||||||||||||||||||||||||||
| 134 | 'Term::ShellKit::load_kit Commands', | ||||||||||||||||||||||||||||||||||||
| 135 | @ARGV, | ||||||||||||||||||||||||||||||||||||
| 136 | ); | ||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||
| 138 | 1 | ▶ | sub shell { | ||||||||||||||||||||||||||||||||||
|
Subroutine
shell: called CC=7 SCAR=21.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 139 | 1 | local $Prompt = $Prompt; | |||||||||||||||||||||||||||||||||||
| 140 | 1 | ▶ | local @CommandQueue = scalar(@_) ? @_ : @DefaultStartup; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||
| 142 | 1 | while ( 1 ) { | |||||||||||||||||||||||||||||||||||
| 143 | 4 | ▶ | if ( ! scalar @CommandQueue ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 144 | 1 | ▶ | my $get_cmd = $SubReadLine or confess "No \$SubReadLine"; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 145 | 1 | @CommandQueue = &$get_cmd(); | |||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||
| 148 | 4 | my $cmd = shift @CommandQueue; | |||||||||||||||||||||||||||||||||||
| 149 | 4 | ▶ | ( defined $cmd ) or last; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||
| 151 | 3 | my @results = eval { &$SubDoCommand( $cmd ) }; | |||||||||||||||||||||||||||||||||||
| 152 | 3 | ▶ | if ( $@ ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 153 | 0 | ▶ | if ( $@ =~ /Term::ShellKit command completed/ ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 154 | 0 | next; | |||||||||||||||||||||||||||||||||||
| 155 | } else { | ||||||||||||||||||||||||||||||||||||
| 156 | 0 | warn "Exception: $@"; | |||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||
| 159 | 3 | &$PrintResultsSub( @results ); | |||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||
| 163 | ###################################################################### | ||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||
| 165 | 2 | ▶ | sub require_package { | ||||||||||||||||||||||||||||||||||
|
Subroutine
require_package: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 166 | 2 | my $package = shift; | |||||||||||||||||||||||||||||||||||
| 167 | 2 | $package =~ s/;\s*$//; | |||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||
| 169 | 2 | (my $file = $package . '.pm' ) =~ s|::|/|go; | |||||||||||||||||||||||||||||||||||
| 170 | 2 | ▶ | return $package if ( $::INC{ $file } ); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||
| 172 | 1 | eval { | |||||||||||||||||||||||||||||||||||
| 173 | 1 | local $SIG{__DIE__} = ''; | |||||||||||||||||||||||||||||||||||
| 174 | 1 | require $file; | |||||||||||||||||||||||||||||||||||
| 175 | }; | ||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||
| 177 | 1 | ▶ | if ( $@ ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 178 | 0 | die "Unable to dynamically load $package: $@" | |||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||
| 181 | return | ||||||||||||||||||||||||||||||||||||
| 182 | 1 | } | |||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||
| 184 | 1 | ▶ | sub load_kit { | ||||||||||||||||||||||||||||||||||
|
Subroutine
load_kit: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 185 | 1 | my $package = shift; | |||||||||||||||||||||||||||||||||||
| 186 | 1 | $package =~ s/;\s*$//; | |||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||
| 188 | 1 | ▶ | $package = "Term::ShellKit::$package" unless $package =~ /::/; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||
| 190 | 1 | require_package($package); | |||||||||||||||||||||||||||||||||||
| 191 | 1 | ▶ | $package->import if ( $package->can('import') ); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||
| 193 | 1 | push @CommandPackages, $package; | |||||||||||||||||||||||||||||||||||
| 194 | 1 | "Activating $package"; | |||||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||
| 197 | ###################################################################### | ||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||
| 199 | package main; | ||||||||||||||||||||||||||||||||||||
| 200 | 1 | ▶ | Term::ShellKit::shell unless caller; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||
| 202 | ###################################################################### | ||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||
| 204 | 1 | 1; | |||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||
| 206 | __END__ | ||||||||||||||||||||||||||||||||||||