diff --git a/lib/CustomCommands.pm b/lib/CustomCommands.pm index 19f84e7..033195d 100644 --- a/lib/CustomCommands.pm +++ b/lib/CustomCommands.pm @@ -1,22 +1,50 @@ package CustomCommands; +use Cwd; +use File::HomeDir; + our %COMMANDS = ( - # Example command: - # 'hello' => sub { print "Hello, World!\n"; return 1; } + # Built-in commands + 'cd' => sub { + my ($command) = @_; + if ($command =~ /^cd\s*(.*)/) { + my $dir = $1 || $ENV{HOME}; + chdir $dir or warn "Could not change to directory $dir: $!"; + return 1; + } + return 0; + }, + + 'export' => sub { + my ($command, $env) = @_; + if ($command =~ /^export\s+(\w+)=(.*)/) { + $ENV{$1} = $2; + $env->set($1, $2); + return 1; + } + return 0; + }, + + 'env' => sub { + print "$_=$ENV{$_}\n" for sort keys %ENV; + return 1; + }, + + # Example custom commands 'quit' => sub { exit; return 1; } ); sub handle { - my ($command) = @_; + my ($command, $env) = @_; # Check if this is a custom command foreach my $cmd (keys %COMMANDS) { if ($command =~ /^$cmd\b/) { - return $COMMANDS{$cmd}->($command); + return $COMMANDS{$cmd}->($command, $env) ? 1 : 0; } } - return 0; # Not a custom command + return -1; # Command not found } 1; diff --git a/pshell.pl b/pshell.pl index d65a829..8b3cf35 100755 --- a/pshell.pl +++ b/pshell.pl @@ -61,32 +61,15 @@ while (1) { next unless length $command; # Try custom commands first - if (CustomCommands::handle($command)) { + my $cmd_status = CustomCommands::handle($command, $env); + if ($cmd_status == 1) { next; } - - # Handle cd command specially - if ($command =~ /^cd\s*(.*)/) { - my $dir = $1 || $ENV{HOME}; - chdir $dir or warn "Could not change to directory $dir: $!"; + elsif ($cmd_status == -1) { + print "Command not found: $command\n"; next; } - # Handle environment variable assignment - if ($command =~ /^export\s+(\w+)=(.*)/) { - $ENV{$1} = $2; - $env->set($1, $2); - next; - } - - # Handle environment variable display - if ($command eq 'env') { - print "$_=$ENV{$_}\n" for sort keys %ENV; - next; - } - - # I guess we can insert more custom commands here - # Save command to history $history->add($command); $term->addhistory($command);