All pastes #40331 Raw Edit

Untitled

public text v1 · immutable
#40331 ·published 2006-02-06 13:54 UTC
rendered paste body
use Term::ReadLine;
use Data::Dumper;

use strict;
use warnings;
my $term = new Term::ReadLine 'c64shell';
$SIG{$_} = 'IGNORE' for keys %SIG;

my %commands=(
"reset"=> {func=>\&reset, desc=>"reset floppy",
        help=>"usage: reset\nresets the floppy drives"},
"dir"=>   {func=>\&dir,   desc=>"list floppy contents",
        help=>"usage: dir [drive id]\nlists the contents of the floppy in the specified drive >, drive defaults to id 8"},
"exit"=>  {func=>\&exit,  desc=>"exits",
        help=>"usage: exit\nquits the transfer shell"},
"help"=>  {func=>\&help,  desc=>"display help for a command",
        help=>"usage: help [command]\ngives help for a specific command"},
"detect"=>{func=>\&detect,desc=>"detect floppies",
        help=>"usage: detect\ndisplays all found floppies with type and id"},
"format"=>{func=>\&format,desc=>"format floppy",
        help=>"usage: format [drive id] [name] [floppy id]\nformats the floppy in the specified drive, sets name and id"},
"copy"=>  {func=>\&copy,  desc=>"read/write floppy images",
        help=>"usage copy <source> <dest>\ncopies source to dest, at least one of them has to be a floppydrive-id"},
);
my $commands=\%commands;
$term->MinLine(100);
while ( defined ($_ = $term->readline('c64>')) ) {
        next unless /^([^ ]*) ?/;
        my @args=split " ",$';
        my $cmd=$commands->{lc $1};
        if (not defined $cmd) {
                print "command not known - use 'help' for a list of commands\n";
                next;
        }
        $cmd->{func}->(@args);
}

sub reset {
        print "reseting floppy...";
        system {"cbmctrl"} ("cbmctrl","reset");
        print "done.\n";
}

sub detect {
        print "searching for floppy drives...\n";
        system {"cbmctrl"} ("cbmctrl","detect");
        print "done.\n";
}
sub dir {
        $_=shift;
        print "dir listing:\n";
        system {"cbmctrl"} ("cbmctrl","dir",defined $_?$_:8);
}
sub copy {
        my ($from,$to)=@_;
        print "copying from $from to $to...\n";
        system {"d64copy"} ("d64copy","-w","-t","serial2",$from,$to);
        print "done.\n";
}

sub format {
        print "do you really want to format the floppy and destroy all the data on it?(y/n) ";
        return unless <>=~m/^y/;
        my $drive=defined $_[0]?$_[0]:8;
        my $name=defined $_[1]?$_[1]:"foo";
        my $id=defined $_[2]?$_[2]:int rand 255;
        print "formatting floppy in drive $drive, name \"$name\", id $id...\n";
        system {"cbmformat"} ("cbmformat","-s","-p",$drive,$name.",".$id);
        print "done.\n";
}

sub help {
        $_=shift;
        if (not defined $_) {
                print "command list:\n";
                foreach(sort keys %commands) {
                        my $d=$commands->{$_}->{desc};
                        print "$_:\t$d\n";
                }
                return;
        }
        my $cmd=$commands->{lc $_};
        if (not defined $cmd) {
                print "command not known\n";
                next;
        }
        print $_.": ".$cmd->{help}."\n";
}

sub exit {
        print "bye\n";
        exit;
}