| 1 |
#!perl |
| 2 |
#: sh |
| 3 |
#: Perl simulator for /bin/sh (Bourne Shell) |
| 4 |
#: 2006-02-01 2006-02-13 |
| 5 |
|
| 6 |
use strict; |
| 7 |
use warnings; |
| 8 |
|
| 9 |
use FindBin; |
| 10 |
use lib "$FindBin::Bin/.."; |
| 11 |
use lib "$FindBin::Bin/../t/lib"; |
| 12 |
use Test::Util::Base; |
| 13 |
#use Smart::Comments; |
| 14 |
|
| 15 |
use Getopt::Std; |
| 16 |
use Time::HiRes qw( sleep ); |
| 17 |
|
| 18 |
my %opts; |
| 19 |
getopts('c', \%opts); |
| 20 |
|
| 21 |
my $ExitCode = 0; |
| 22 |
my $RedirectIndex; |
| 23 |
|
| 24 |
$| = 1; |
| 25 |
|
| 26 |
if ($opts{c}) { |
| 27 |
eval { |
| 28 |
process_shell(join ' ', @ARGV); |
| 29 |
}; |
| 30 |
warn $@ if $@; |
| 31 |
exit($ExitCode); |
| 32 |
} else { |
| 33 |
print '$ '; |
| 34 |
process_prompt(); |
| 35 |
exit(0); |
| 36 |
} |
| 37 |
|
| 38 |
sub process_prompt { |
| 39 |
while (<STDIN>) { |
| 40 |
chomp; |
| 41 |
### Got shell: $_ |
| 42 |
last if /^\s*exit(?:\s+\d+)?\s*$/; |
| 43 |
eval { |
| 44 |
process_shell($_); |
| 45 |
}; |
| 46 |
if ($@) { |
| 47 |
warn "$@\n"; |
| 48 |
warn "[Error code $ExitCode returned.]\n"; |
| 49 |
} |
| 50 |
print '$ '; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
sub process_shell { |
| 55 |
my $cmd = shift; |
| 56 |
#warn Dumper($cmd); |
| 57 |
|
| 58 |
$cmd =~ s/\n+/ /gso; |
| 59 |
my @raw_args = split_arg($cmd); |
| 60 |
### @raw_args |
| 61 |
my @args = process_args(0, @raw_args); |
| 62 |
return eval_cmd(@args); |
| 63 |
} |
| 64 |
|
| 65 |
sub process_args ($$) { |
| 66 |
#warn "!!!!!!!!!!!!!!!!!!!!!!"; |
| 67 |
my $level = shift; |
| 68 |
my @raw_args = @_; |
| 69 |
my @args; |
| 70 |
foreach (@raw_args) { |
| 71 |
#warn "----------\n"; |
| 72 |
#warn Dumper(@args, $_, @raw_args); |
| 73 |
#warn "----------\n"; |
| 74 |
if ($_ eq ';') { |
| 75 |
eval_cmd(@args); |
| 76 |
@args = (); |
| 77 |
} elsif ($_ eq '>' or $_ eq '>>' or $_ eq '<') { |
| 78 |
$RedirectIndex = $#args; |
| 79 |
push @args, $_; |
| 80 |
} elsif ($_ eq '&&') { |
| 81 |
eval_cmd(@args); |
| 82 |
return if $ExitCode != 0; |
| 83 |
@args = (); |
| 84 |
} elsif ($_ eq '||') { |
| 85 |
eval_cmd(@args); |
| 86 |
return if $ExitCode == 0; |
| 87 |
$ExitCode = 0; |
| 88 |
@args = (); |
| 89 |
} elsif (/^"(.*)"$/o) { |
| 90 |
#warn "---------"; |
| 91 |
#warn qq{Pusing "$1" into args\n}; |
| 92 |
my $s = $1; |
| 93 |
process_escape( $s, q{\\@$\#} ); |
| 94 |
#warn "$s"; |
| 95 |
subs_env($s); |
| 96 |
push @args, $s if $s ne ''; |
| 97 |
} elsif (/^'(.*)'$/o) { |
| 98 |
#warn " Pusing '$1' into args\n"; |
| 99 |
push @args, $1 if $1 ne ''; |
| 100 |
} elsif (/^['"]/o) { |
| 101 |
$ExitCode = 1; |
| 102 |
die "sh: unexpected EOF while looking for matching `$&'\n"; |
| 103 |
} else { |
| 104 |
#warn " Remaining: $_\n"; |
| 105 |
my $s = $_; |
| 106 |
if ($level == 0 and $s =~ /^\#/o) { |
| 107 |
return @args; |
| 108 |
} |
| 109 |
process_unquoted($s, $level, \@args); |
| 110 |
#warn "~~~~~~~~~~~~~~\n"; |
| 111 |
#warn Dumper(@args, $_, @raw_args); |
| 112 |
#warn "~~~~~~~~~~~~~~\n"; |
| 113 |
} |
| 114 |
} |
| 115 |
return @args; |
| 116 |
} |
| 117 |
|
| 118 |
sub process_unquoted { |
| 119 |
my ($s, $level, $rargs) = @_; |
| 120 |
return if $s eq ''; |
| 121 |
## before: $s |
| 122 |
$s =~ s/\\(.)/$1/gso; |
| 123 |
## after: $s |
| 124 |
subs_env($s); |
| 125 |
my @subargs = split_arg($s) if $s =~ / /o; |
| 126 |
#warn Dumper(@subargs); |
| 127 |
if (@subargs > 1) { |
| 128 |
push @$rargs, process_args ($level+1, @subargs); |
| 129 |
} else { |
| 130 |
my @files = glob $s; |
| 131 |
if (@files > 1) { |
| 132 |
push @$rargs, @files; |
| 133 |
} else { |
| 134 |
push @$rargs, $s if $s ne ''; |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
sub subs_env { |
| 140 |
$_[0] =~ s/\$(\w+)/defined $ENV{$1} ? "$ENV{$1}" : ''/geo; |
| 141 |
} |
| 142 |
|
| 143 |
sub touch (@) { |
| 144 |
my @files = @_; |
| 145 |
foreach my $file (@files) { |
| 146 |
my $in; |
| 147 |
open $in, ">>$file" and |
| 148 |
print $in '' and close $in or |
| 149 |
die "Can't touch $file: $!"; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
sub eval_cmd { |
| 154 |
my @args = @_; |
| 155 |
return 0 if not @args; |
| 156 |
my $redir = $RedirectIndex; |
| 157 |
undef $RedirectIndex; |
| 158 |
my $exec = shift @args; |
| 159 |
if ($exec =~ /^\s+$/) { |
| 160 |
$exec = shift @args; |
| 161 |
} |
| 162 |
### Got exec: $exec |
| 163 |
### Got args: @args |
| 164 |
if ($exec eq 'echo') { |
| 165 |
if ($redir) { |
| 166 |
my $op = $args[$redir]; |
| 167 |
my @elems = @args[0..$redir-1]; |
| 168 |
if ($op eq '>') { |
| 169 |
my $file = $args[$redir+1]; |
| 170 |
open my $out, "> $file" or |
| 171 |
die "Can't open $file for writing: $!\n"; |
| 172 |
print $out "@elems\n"; |
| 173 |
close $out; |
| 174 |
} elsif ($op eq '>>') { |
| 175 |
my $file = $args[$redir+1]; |
| 176 |
open my $out, ">> $file" or |
| 177 |
die "Can't open $file for writing: $!\n"; |
| 178 |
print $out "@elems\n"; |
| 179 |
close $out; |
| 180 |
} elsif ($op eq '<') { |
| 181 |
print "@elems\n"; |
| 182 |
} else { |
| 183 |
die "Unexpected redirection operator: $op"; |
| 184 |
} |
| 185 |
} else { |
| 186 |
print "@args\n"; |
| 187 |
} |
| 188 |
return 0; |
| 189 |
} elsif ($exec eq 'rm') { |
| 190 |
foreach my $file (@args) { |
| 191 |
$file =~ s/\s+$//; |
| 192 |
if (not unlink $file) { |
| 193 |
warn "rm: cannot lstat `$file': $!\n"; |
| 194 |
$ExitCode = 1; |
| 195 |
return; |
| 196 |
} |
| 197 |
} |
| 198 |
return 0; |
| 199 |
} elsif ($exec eq 'sleep') { |
| 200 |
sleep ($args[0]); |
| 201 |
} elsif ($exec eq 'pwd') { |
| 202 |
require 'Cwd.pm'; |
| 203 |
print Cwd::cwd(), "\n"; |
| 204 |
} elsif ($exec eq 'cd') { |
| 205 |
chdir $args[0]; |
| 206 |
} elsif ($exec eq 'touch') { |
| 207 |
touch(@args); |
| 208 |
$ExitCode = 0; |
| 209 |
return; |
| 210 |
} elsif ($exec eq ':') { |
| 211 |
return; |
| 212 |
} elsif ($exec eq 'exit') { |
| 213 |
$ExitCode = $args[0] if defined $args[0]; |
| 214 |
exit($ExitCode); |
| 215 |
} elsif ($exec =~ /make/i or ($args[0] and $args[0] =~ /make/i)) { |
| 216 |
$ExitCode = system $exec, @args; |
| 217 |
return; |
| 218 |
} else { |
| 219 |
warn "sh: unknown shell command: $exec"; |
| 220 |
$ExitCode = 1; |
| 221 |
return; |
| 222 |
} |
| 223 |
} |