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 {
|
sub handle {
|
||||||
my ($command, $env) = @_;
|
my ($command, $env) = @_;
|
||||||
|
#print STDERR "DEBUG: Handling command: $command\n";
|
||||||
# Check if this is a custom command
|
# Check if this is a custom command
|
||||||
foreach my $cmd (keys %COMMANDS) {
|
foreach my $cmd (keys %COMMANDS) {
|
||||||
if ($command =~ /^$cmd\b/) {
|
if ($command =~ /^$cmd\b/) {
|
||||||
|
#print STDERR "DEBUG: Found custom command: $cmd\n";
|
||||||
return $COMMANDS{$cmd}->($command, $env) ? 1 : 0;
|
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
|
return -1; # Command not found
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
21
pshell.pl
21
pshell.pl
|
@ -60,20 +60,17 @@ while (1) {
|
||||||
# Skip empty commands
|
# Skip empty commands
|
||||||
next unless length $command;
|
next unless length $command;
|
||||||
|
|
||||||
# Try custom commands first
|
# Handle command
|
||||||
my $cmd_status = CustomCommands::handle($command, $env);
|
my $cmd_status = CustomCommands::handle($command, $env);
|
||||||
if ($cmd_status == 1) {
|
if ($cmd_status >= 0) { # 1=handled, 2=system command
|
||||||
next;
|
# Save valid commands to history
|
||||||
}
|
$history->add($command);
|
||||||
elsif ($cmd_status == -1) {
|
$term->addhistory($command);
|
||||||
print "Command not found: $command\n";
|
|
||||||
next;
|
next;
|
||||||
}
|
}
|
||||||
|
|
||||||
# Save command to history
|
# Command not found
|
||||||
$history->add($command);
|
#print STDERR "DEBUG: Main program detected command not found\n";
|
||||||
$term->addhistory($command);
|
print "Command not found: $command\n";
|
||||||
|
next;
|
||||||
# Execute command
|
|
||||||
system($command);
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue