File: sh

package info (click to toggle)
libmakefile-dom-perl 0.004-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 616 kB
  • ctags: 535
  • sloc: perl: 6,552; makefile: 2
file content (223 lines) | stat: -rw-r--r-- 5,543 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!perl
#: sh
#: Perl simulator for /bin/sh (Bourne Shell)
#: 2006-02-01 2006-02-13

use strict;
use warnings;

use FindBin;
use lib "$FindBin::Bin/..";
use lib "$FindBin::Bin/../t/lib";
use Test::Util::Base;
#use Smart::Comments;

use Getopt::Std;
use Time::HiRes qw( sleep );

my %opts;
getopts('c', \%opts);

my $ExitCode = 0;
my $RedirectIndex;

$| = 1;

if ($opts{c}) {
    eval {
        process_shell(join ' ', @ARGV);
    };
    warn $@ if $@;
    exit($ExitCode);
} else {
    print '$ ';
    process_prompt();
    exit(0);
}

sub process_prompt {
    while (<STDIN>) {
        chomp;
        ### Got shell: $_
        last if /^\s*exit(?:\s+\d+)?\s*$/;
        eval {
            process_shell($_);
        };
        if ($@) {
            warn "$@\n";
            warn "[Error code $ExitCode returned.]\n";
        }
        print '$ ';
    }
}

sub process_shell {
    my $cmd = shift;
    #warn Dumper($cmd);

    $cmd =~ s/\n+/ /gso;
    my @raw_args = split_arg($cmd);
	### @raw_args
    my @args = process_args(0, @raw_args);
    return eval_cmd(@args);
}

sub process_args ($$) {
    #warn "!!!!!!!!!!!!!!!!!!!!!!";
    my $level = shift;
    my @raw_args = @_;
    my @args;
    foreach (@raw_args) {
        #warn "----------\n";
        #warn Dumper(@args, $_, @raw_args);
        #warn "----------\n";
        if ($_ eq ';') {
            eval_cmd(@args);
            @args = ();
        } elsif ($_ eq '>' or $_ eq '>>' or $_ eq '<') {
            $RedirectIndex = $#args;
            push @args, $_;
        } elsif ($_ eq '&&') {
            eval_cmd(@args);
            return if $ExitCode != 0;
            @args = ();
        } elsif ($_ eq '||') {
            eval_cmd(@args);
            return if $ExitCode == 0;
            $ExitCode = 0;
            @args = ();
        } elsif (/^"(.*)"$/o) {
            #warn "---------";
            #warn qq{Pusing "$1" into args\n};
            my $s = $1;
            process_escape( $s, q{\\@$\#} );
            #warn "$s";
            subs_env($s);
            push @args, $s if $s ne '';
        } elsif (/^'(.*)'$/o) {
            #warn "  Pusing '$1' into args\n";
            push @args, $1 if $1 ne '';
        } elsif (/^['"]/o) {
            $ExitCode = 1;
            die "sh: unexpected EOF while looking for matching `$&'\n";
        } else {
            #warn "  Remaining: $_\n";
            my $s = $_;
            if ($level == 0 and $s =~ /^\#/o) {
                return @args;
            }
            process_unquoted($s, $level, \@args);
            #warn "~~~~~~~~~~~~~~\n";
            #warn Dumper(@args, $_, @raw_args);
            #warn "~~~~~~~~~~~~~~\n";
        }
    }
    return @args;
}

sub process_unquoted {
    my ($s, $level, $rargs) = @_;
    return if $s eq '';
	## before: $s
    $s =~ s/\\(.)/$1/gso;
	## after: $s
    subs_env($s);
    my @subargs = split_arg($s) if $s =~ / /o;
    #warn Dumper(@subargs);
    if (@subargs > 1) {
        push @$rargs, process_args ($level+1, @subargs);
    } else {
        my @files = glob $s;
        if (@files > 1) {
            push @$rargs, @files;
        } else {
            push @$rargs, $s if $s ne '';
        }
    }
}

sub subs_env {
    $_[0] =~ s/\$(\w+)/defined $ENV{$1} ? "$ENV{$1}" : ''/geo;
}

sub touch (@) {
    my @files = @_;
    foreach my $file (@files) {
        my $in;
        open $in, ">>$file" and
            print $in '' and close $in or
            die "Can't touch $file: $!";
    }
}

sub eval_cmd {
    my @args = @_;
    return 0 if not @args;
    my $redir = $RedirectIndex;
    undef $RedirectIndex;
	my $exec = shift @args;
	if ($exec =~ /^\s+$/) {
		$exec = shift @args;
	}
    ### Got exec: $exec
	### Got args: @args
	if ($exec eq 'echo') {
        if ($redir) {
            my $op = $args[$redir];
            my @elems = @args[0..$redir-1];
            if ($op eq '>') {
                my $file = $args[$redir+1];
                open my $out, "> $file" or
                    die "Can't open $file for writing: $!\n";
                print $out "@elems\n";
                close $out;
            } elsif ($op eq '>>') {
                my $file = $args[$redir+1];
                open my $out, ">> $file" or
                    die "Can't open $file for writing: $!\n";
                print $out "@elems\n";
                close $out;
            } elsif ($op eq '<') {
                print "@elems\n";
            } else {
                die "Unexpected redirection operator: $op";
            }
        } else {
            print "@args\n";
        }
        return 0;
    } elsif ($exec eq 'rm') {
        foreach my $file (@args) {
            $file =~ s/\s+$//;
            if (not unlink $file) {
                warn "rm: cannot lstat `$file': $!\n";
                $ExitCode = 1;
                return;
            }
        }
        return 0;
    } elsif ($exec eq 'sleep') {
        sleep ($args[0]);
    } elsif ($exec eq 'pwd') {
        require 'Cwd.pm';
        print Cwd::cwd(), "\n";
    } elsif ($exec eq 'cd') {
        chdir $args[0];
    } elsif ($exec eq 'touch') {
        touch(@args);
        $ExitCode = 0;
        return;
    } elsif ($exec eq ':') {
        return;
    } elsif ($exec eq 'exit') {
        $ExitCode = $args[0] if defined $args[0];
        exit($ExitCode);
    } elsif ($exec =~ /make/i or ($args[0] and $args[0] =~ /make/i)) {
        $ExitCode = system $exec, @args;
        return;
    } else {
        warn "sh: unknown shell command: $exec";
        $ExitCode = 1;
        return;
    }
}