X-Git-Url: http://gb7djk.dxcluster.net/gitweb/gitweb.cgi?a=blobdiff_plain;f=perl%2FScript.pm;h=4c8d0f3bf3f3af1a55b3dc699e22375068fe0bc1;hb=60d6599887f29ec966d075f413c2c73b9e913212;hp=434109ceaf688b0f4966874014ce95a83d77a34e;hpb=ec57aa982f870daf09a1fc7839d06e52980cfb55;p=spider.git diff --git a/perl/Script.pm b/perl/Script.pm index 434109ce..4c8d0f3b 100644 --- a/perl/Script.pm +++ b/perl/Script.pm @@ -25,15 +25,32 @@ $main::branch += $BRANCH; my $base = "$main::root/scripts"; +sub clean +{ + my $s = shift; + $s =~ s/[^-\w\.]//g; + return $s; +} + sub new { my $pkg = shift; - my $script = shift; - my $fn = "$base/$script"; + my $script = clean(shift); + my $mybase = shift || $base; + my $fn = "$mybase/$script"; - my $fh = new IO::File $fn; - return undef unless $fh; - my $self = bless {call => $script}, $pkg; + my $self = {call => $script}; + my $fh = IO::File->new($fn); + if ($fh) { + $self->{fn} = $fn; + } else { + $fh = IO::File->new(lc $fn); + if ($fh) { + $self->{fn} = $fn; + } else { + return undef; + } + } my @lines; while (<$fh>) { chomp; @@ -41,22 +58,71 @@ sub new } $fh->close; $self->{lines} = \@lines; - return $self; + $self->{inscript} = 1; + return bless $self, $pkg; } sub run { my $self = shift; my $dxchan = shift; + my $return_output = shift; + my @out; + foreach my $l (@{$self->{lines}}) { unless ($l =~ /^\s*\#/ || $l =~ /^\s*$/) { - my @out = DXCommandmode::run_cmd($dxchan, $l); - if ($dxchan->can('send_ans')) { - $dxchan->send_ans(@out); - } else { - dbg($_) for @out; - } - last if @out && $l =~ /^pri?v?/; + $dxchan->inscript(1) if $self->{inscript}; + push @out, DXCommandmode::run_cmd($dxchan, $l); + $dxchan->inscript(0) if $self->{inscript}; + last if @out && $l =~ /^pri?v?/i; + } + } + if ($return_output) { + return @out; + } else { + if ($dxchan->can('send_ans')) { + $dxchan->send_ans(@out); + } else { + dbg($_) for @out; } } + return (); +} + +sub inscript +{ + my $self = shift; + $self->{inscript} = shift if @_; + return $self->{inscript}; +} + +sub store +{ + my $call = clean(lc shift); + my @out; + my $ref = ref $_[0] ? shift : \@_; + my $count; + my $fn = "$base/$call"; + + rename $fn, "$fn.o" if -e $fn; + my $f = IO::File->new(">$fn") || return undef; + for (@$ref) { + $f->print("$_\n"); + $count++; + } + $f->close; + unlink $fn unless $count; + return $count; +} + +sub lines +{ + my $self = shift; + return @{$self->{lines}}; +} + +sub erase +{ + my $self = shift; + unlink $self->{fn}; }