# HG changeset patch # User Davor Ocelic # Date 1556487747 -7200 # Node ID b60189929c4b1f60fb1ff312cc788aa65c58f93b # Parent 20752fcf201c23b58b8f862d742ce0dc6f56b0da Streamline %cmd_map %cmd_map was previously a hash containing: { real_function => alias_function, ... } Now the structure of the hash is: { alias_or_real_func => [ real_func/if_any, autocompleter_func ], ... } This allows for a more streamlined definition of commands and aliases, and makes it possible to keep all data in %cmd_map instead of having additional special cases. diff -r 20752fcf201c -r b60189929c4b shelldap --- a/shelldap Sun Apr 28 22:38:38 2019 +0200 +++ b/shelldap Sun Apr 28 23:42:27 2019 +0200 @@ -1195,7 +1195,7 @@ ### Autocomplete values: Returns cached children entries. ### -sub autocomplete_cwd +sub comp_cwd { my $self = shift; return @{ $self->{'cwd_entries'} }; @@ -1231,52 +1231,6 @@ } -### Inject various autocomplete and alias routines into the symbol table. -### -{ - no warnings; - no strict 'refs'; - - # command, alias - my %cmd_map = ( - whoami => 'id', - list => 'ls', - grep => 'search', - edit => 'vi', - delete => 'rm', - copy => 'cp', - cat => 'read', - move => 'mv', - less => undef, - cd => undef, - passwd => undef - ); - - # setup autocompletes - foreach ( %cmd_map ) { - next unless $_; - my $sub = "comp_$_"; - *$sub = \&autocomplete_cwd; - } - *comp_touch = \&comp_create; - *comp_export = \&comp_setenv; - - # setup alias subs - # - # Term::Shell has an alias_* feature, but - # it seems to work about 90% of the time. - # that last 10% is something of a mystery. - # - $cmd_map{'create'} = 'touch'; - foreach my $cmd ( keys %cmd_map ) { - next unless defined $cmd_map{$cmd}; - my $alias_sub = 'run_' . $cmd_map{$cmd}; - my $real_sub = 'run_' . $cmd; - *$alias_sub = \&$real_sub; - } -} - - ### Given an $arrayref, remove LDIF continuation wrapping in place, ### effectively making each entry a single line for LCS comparisons. ### @@ -1409,6 +1363,44 @@ ### S H E L L M E T H O D S ######################################################################## +# alias_or_command => [ real_command_name, completion_function ] +# +# It is not necessary to list all real commands here, but you can/should +# do so to assign autocompleter function to them, and/or to define aliases. +my %cmd_map = ( + #'whoami' => [ undef ], + #'pwd' => [ undef ], + 'list' => [ undef, 'comp_cwd' ], + 'grep' => [ undef, 'comp_cwd' ], + 'edit' => [ undef, 'comp_cwd' ], + 'delete' => [ undef, 'comp_cwd' ], + 'copy' => [ undef, 'comp_cwd' ], + 'cat' => [ undef, 'comp_cwd' ], + 'move' => [ undef, 'comp_cwd' ], + 'less' => [ undef, 'comp_cwd' ], + 'cd' => [ undef, 'comp_cwd' ], + 'create' => [ undef, 'comp_create' ], + 'setenv' => [ undef, 'comp_setenv' ], + #'passwd' => [ undef ], + ##'clear' => [ undef ], + #'env' => [ undef ], + #'help' => [ undef ], + #'mkdir' => [ undef ], + #'inspect' => [ undef ], + + 'id' => [ 'whoami' ], + 'ls' => [ 'list', 'comp_cwd' ], + 'search' => [ 'grep', 'comp_cwd' ], + 'vi' => [ 'edit', 'comp_cwd' ], + 'rm' => [ 'delete', 'comp_cwd' ], + 'cp' => [ 'copy', 'comp_cwd' ], + 'read' => [ 'read', 'comp_cwd' ], + 'mv' => [ 'move', 'comp_cwd' ], + 'touch' => [ 'create', 'comp_create' ], + 'export' => [ 'setenv', 'comp_setenv' ], +); + + ### Don't die on a newline, just no-op. ### sub run_ { return; } @@ -1927,9 +1919,14 @@ sub run_help { my $self = shift; + my $section = 'SHELL COMMANDS'; + my $command = shift; - my $section = 'SHELL COMMANDS'; - $section .= "/$command" if $command; + if( $command ) { + # If it is an alias, resolve to real name: + $command = $cmd_map{$command}[0] if $cmd_map{$command}[0]; + $section .= "/$command" + } return Pod::Usage::pod2usage( -exitval => 'NOEXIT', @@ -2338,6 +2335,31 @@ } +### Inject various autocomplete and alias routines into the symbol table. +### + +# setup alias subs +# +# Term::Shell has an alias_* feature, but +# it seems to work about 90% of the time. +# that last 10% is something of a mystery. +# +{ no strict 'refs'; + while(my($cmd, $data) = each %cmd_map ) { + if( $$data[0]) { + my $alias_sub = 'run_' . $cmd; + my $real_sub = 'run_' . $$data[0]; + *$alias_sub = \&$real_sub; + } + + if( $$data[1]) { + my $comp_sub = "comp_$cmd"; + *$comp_sub = \&{$$data[1]} + } + } +} + + ### Recursively walk an objectClass hierarchy, returning an array ### of inheritence. ###