pshell/pshell.pl

79 lines
1.8 KiB
Perl
Executable file

#!/usr/bin/env perl
use lib 'lib';
use History::SQLite;
use Environment::SQLite;
use Term::ReadLine;
use Cwd;
use File::HomeDir;
use CustomCommands;
$ENV{TERM} = 'xterm-256color' unless exists $ENV{TERM};
# Initialize command history and environment
my $history = History::SQLite->new(
db_path => 'pshell_history.db'
);
my $env = Environment::SQLite->new(
db_path => 'pshell_env.db'
);
# Signal handling setup
$SIG{INT} = sub { print "\n"; };
$SIG{CHLD} = 'IGNORE';
$SIG{WINCH} = sub { $term->resize_terminal if defined $term };
print "Perl Shell (pshell) - Type 'exit' to quit\n";
my $term = Term::ReadLine->new('pshell');
$term->ornaments(0);
# Load previous history
my @history = $history->get_all;
$term->addhistory($_) for @history;
# Load environment variables
my %env_vars = $env->get_all;
$ENV{$_} = $env_vars{$_} for keys %env_vars;
# Load and execute .pshellrc if it exists
my $rc_file = File::HomeDir->my_home . '/.pshellrc';
if (-e $rc_file && -r $rc_file) {
open my $fh, '<', $rc_file or warn "Could not open $rc_file: $!";
while (my $line = <$fh>) {
chomp $line;
next unless length $line;
system($line);
}
close $fh;
}
while (1) {
my $cwd = getcwd();
my $prompt = "pshell:$cwd> ";
my $command = $term->readline($prompt);
last unless defined $command;
chomp $command;
last if $command =~ /^exit$/i;
# Skip empty commands
next unless length $command;
# Try custom commands first
my $cmd_status = CustomCommands::handle($command, $env);
if ($cmd_status == 1) {
next;
}
elsif ($cmd_status == -1) {
print "Command not found: $command\n";
next;
}
# Save command to history
$history->add($command);
$term->addhistory($command);
# Execute command
system($command);
}