Add PATH command lookup and improve command handling logic
This commit is contained in:
parent
3b6dffa176
commit
9204a8f519
2 changed files with 25 additions and 13 deletions
|
@ -36,14 +36,29 @@ our %COMMANDS = (
|
|||
|
||||
sub handle {
|
||||
my ($command, $env) = @_;
|
||||
|
||||
#print STDERR "DEBUG: Handling command: $command\n";
|
||||
# Check if this is a custom command
|
||||
foreach my $cmd (keys %COMMANDS) {
|
||||
if ($command =~ /^$cmd\b/) {
|
||||
#print STDERR "DEBUG: Found custom command: $cmd\n";
|
||||
return $COMMANDS{$cmd}->($command, $env) ? 1 : 0;
|
||||
}
|
||||
}
|
||||
|
||||
# Check if it's a system command
|
||||
my ($cmd_name) = split(/\s+/, $command);
|
||||
#print STDERR "DEBUG: Checking system command: $cmd_name\n";
|
||||
|
||||
# Check PATH directly
|
||||
foreach my $path (split(/:/, $ENV{PATH})) {
|
||||
my $full_path = "$path/$cmd_name";
|
||||
if (-x $full_path) {
|
||||
#print STDERR "DEBUG: Found in PATH: $full_path\n";
|
||||
system($command);
|
||||
return 2; # System command executed
|
||||
}
|
||||
}
|
||||
#print STDERR "DEBUG: Command not found: $command\n";
|
||||
return -1; # Command not found
|
||||
}
|
||||
|
||||
|
|
19
pshell.pl
19
pshell.pl
|
@ -60,20 +60,17 @@ while (1) {
|
|||
# Skip empty commands
|
||||
next unless length $command;
|
||||
|
||||
# Try custom commands first
|
||||
# Handle command
|
||||
my $cmd_status = CustomCommands::handle($command, $env);
|
||||
if ($cmd_status == 1) {
|
||||
if ($cmd_status >= 0) { # 1=handled, 2=system command
|
||||
# Save valid commands to history
|
||||
$history->add($command);
|
||||
$term->addhistory($command);
|
||||
next;
|
||||
}
|
||||
elsif ($cmd_status == -1) {
|
||||
|
||||
# Command not found
|
||||
#print STDERR "DEBUG: Main program detected command not found\n";
|
||||
print "Command not found: $command\n";
|
||||
next;
|
||||
}
|
||||
|
||||
# Save command to history
|
||||
$history->add($command);
|
||||
$term->addhistory($command);
|
||||
|
||||
# Execute command
|
||||
system($command);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue