| 1 | #!/usr/bin/perl -w | ||||||||||||
| 2 | # See copyright, etc in below POD section. | ||||||||||||
| 3 | ###################################################################### | ||||||||||||
| 4 | |||||||||||||
| 5 | 3 | ▶ | use lib './blib/lib'; # testing | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 3 | ▶ | use IO::Handle; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 3 | ▶ | use Getopt::Long; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 3 | ▶ | use Cwd qw(); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 3 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 3 | ▶ | use vars qw(@CmdParts $VERSION); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | |||||||||||||
| 13 | 3 | $VERSION = '1.500'; | |||||||||||
| 14 | |||||||||||||
| 15 | #====================================================================== | ||||||||||||
| 16 | # main | ||||||||||||
| 17 | |||||||||||||
| 18 | 3 | my $Opt_Cd; | |||||||||||
| 19 | my @Opt_Param; | ||||||||||||
| 20 | 3 | my $Opt_Shell = "/bin/sh -c"; | |||||||||||
| 21 | 3 | my $opt_show; | |||||||||||
| 22 | |||||||||||||
| 23 | 3 | autoflush STDOUT; | |||||||||||
| 24 | 3 | autoflush STDERR; | |||||||||||
| 25 | |||||||||||||
| 26 | 3 | Getopt::Long::config ("no_auto_abbrev","require_order"); | |||||||||||
| 27 | 3 | ▶ | if (! GetOptions ( | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 28 | "help" => \&usage, | ||||||||||||
| 29 | 1 | ▶ | "version" => sub { print "Version $VERSION\n"; exit(0); }, | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 30 | "cd=s" => \$Opt_Cd, | ||||||||||||
| 31 | "shell=s" => \$Opt_Shell, | ||||||||||||
| 32 | "show!" => \$opt_show, | ||||||||||||
| 33 | )) { | ||||||||||||
| 34 | 0 | die "%Error: Bad usage, try 'uriexec --help'\n"; | |||||||||||
| 35 | } | ||||||||||||
| 36 | 1 | my @opt_cmd = @ARGV; | |||||||||||
| 37 | 1 | ▶ | $#opt_cmd >= 0 or die "%Error: uriexec: No command specified, see 'uriexec --help'\n"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 38 | |||||||||||||
| 39 | 1 | push (@Opt_Param, @ARGV); # Any remaining arguments (i.e. after a -- on the command line) | |||||||||||
| 40 | 1 | ▶ | @Opt_Param or die "%Error: uriexec: No command argument was specified\n"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 41 | |||||||||||||
| 42 | 1 | ▶ | if ($opt_show) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 43 | 0 | ▶ | print "cd ",user_decode($Opt_Cd),"\n" if $Opt_Cd; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 44 | 0 | ▶ | print "shell ",user_decode($Opt_Shell),"\n" if $Opt_Shell; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 45 | 0 | print "cmd ",user_decode(join(' ',@Opt_Param)),"\n"; | |||||||||||
| 46 | 0 | ▶ | exit(0) if $opt_show; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 47 | } | ||||||||||||
| 48 | |||||||||||||
| 49 | 1 | ▶ | if ($Opt_Cd) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 50 | 0 | my $dir = uri_decode($Opt_Cd); | |||||||||||
| 51 | 0 | ▶ | Cwd::chdir($dir) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 52 | or die "%Error: uriexec: Could not chdir to \"$dir\"\n"; | ||||||||||||
| 53 | } | ||||||||||||
| 54 | |||||||||||||
| 55 | # Decode all arguments and exec them. | ||||||||||||
| 56 | 1 | my $shell = uri_decode($Opt_Shell); | |||||||||||
| 57 | 1 | my $cmd = uri_decode(join(' ', @Opt_Param)); | |||||||||||
| 58 | 1 | my @cmds = (split(' ',$shell), $cmd); | |||||||||||
| 59 | 1 | exec(@cmds); | |||||||||||
| 60 | 0 | die "%Error: uriexec: exec failed: '".join(' ',@cmds),"\n"; | |||||||||||
| 61 | |||||||||||||
| 62 | #---------------------------------------------------------------------- | ||||||||||||
| 63 | |||||||||||||
| 64 | sub usage { | ||||||||||||
| 65 | 1 | ▶ | eval 'use Pod::Usage;'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
usage: called CC=1 SCAR=0.0
| |||||||||||||
| 66 | 1 | print "Version: $VERSION\n"; | |||||||||||
| 67 | 1 | pod2usage(-verbose=>2, -exitval=>2, -output=>\*STDOUT, -noperldoc=>1); | |||||||||||
| 68 | 0 | exit (1); | |||||||||||
| 69 | } | ||||||||||||
| 70 | |||||||||||||
| 71 | ####################################################################### | ||||||||||||
| 72 | |||||||||||||
| 73 | sub uri_decode { | ||||||||||||
| 74 | 2 | ▶ | my $str = shift; | ||||||||||
|
Subroutine
uri_decode: called CC=1 SCAR=0.0
| |||||||||||||
| 75 | 2 | $str =~ tr/+/ /; # Plus encodes a space | |||||||||||
| 76 | 2 | $str =~ s/%([a-fA-F0-9]{2,2})/chr(hex($1))/eg; # Decode the % encoding | |||||||||||
| 77 | 2 | return $str; | |||||||||||
| 78 | } | ||||||||||||
| 79 | |||||||||||||
| 80 | sub user_decode { | ||||||||||||
| 81 | 0 | ▶ | my $str = shift; | ||||||||||
|
Subroutine
user_decode: not called CC=3 SCAR=24.8
| |||||||||||||
| 82 | 0 | ▶ | my $level = shift || 1; | ||||||||||
|
Condition0%0/2
shift() || 1
MC/DC0%0/1
shift() || 1
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||
| 83 | # Recursively decode a string for user's convenience | ||||||||||||
| 84 | 0 | $str = uri_decode($str); | |||||||||||
| 85 | 0 | ▶ | if ($str =~ /(.*?\buriexec\s)(.*)$/) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 86 | 0 | $str = $1." ".user_decode($2,$level+1); | |||||||||||
| 87 | 0 | return "{".$level."{ ".$str." }".$level."} "; | |||||||||||
| 88 | } | ||||||||||||
| 89 | 0 | return "{".$level."{ ".$str." }".$level." "; | |||||||||||
| 90 | } | ||||||||||||
| 91 | |||||||||||||
| 92 | ####################################################################### | ||||||||||||
| 93 | __END__ | ||||||||||||