From d5c7b0189ae05b99a2ed496df5445089cbf562e8 Mon Sep 17 00:00:00 2001 From: Guilhem Moulin Date: Sun, 29 Mar 2015 05:43:42 +0200 Subject: Fix bash completion. --- cli/icevault | 89 ++++++++++++++---------------------------------------------- 1 file changed, 21 insertions(+), 68 deletions(-) (limited to 'cli/icevault') diff --git a/cli/icevault b/cli/icevault index be7902f..1f662bf 100755 --- a/cli/icevault +++ b/cli/icevault @@ -308,73 +308,6 @@ sub matches($) { return @matches; } -# Get all identities with the given $prefix. If there are multiple -# matches and $all is false, limit the output to one depth more than the -# longuest common prefix. -sub complete($;$) { - my $prefix = shift // ''; - my $all = shift; - - my ($s, $h, $i); # extract URI components from the prefix - if ($prefix =~ /\A([A-Za-z0-9-]+):\/\/([^\P{Graph}:\/]+(?::\d+)?)\/([^\P{Print}\/]*)\z/) { - ($s, $h, $i) = ($1, $2, $3); - } elsif ($prefix =~ /\A([A-Za-z0-9-]+):\/\/([^\P{Graph}\/]*)\z/) { - ($s, $h, $i) = ($1, $2, undef); - } elsif ($prefix =~ /\A([A-Za-z0-9-]*)(:\/?)?\z/) { - ($s, $h, $i) = ($1, (defined $2 ? '' : undef), undef); - } else { - exit; - } - - # construct a glob pattern with these URI components - my ($gs, $gh, $gi) = ($s, $h, $i); - s/([\\\[\]\{\}\*\?\~])/\\$1/g foreach grep defined, ($gs, $gh, $gi); # escape meta chars - - # add trailing wildcards - $gs .= '*' if defined $gs and !defined $gh; - $gh .= '*' if defined $gh and !defined $gi; - $gi .= '*' if defined $gi; - - # construct regexp to extract the URI compontents of the matching URIs - my ($ps, $ph, $pi) = ($s, $h, $i); - $ps = defined $h ? qr/(?\Q$s\E)/ : (defined $s and $s ne '') ? qr/(?\Q$s\E[A-Za-z0-9-]*)/ : qr/(?[A-Za-z0-9-]+)/; - $ph = defined $i ? qr/(?\Q$h\E)/ : (defined $h and $h ne '') ? qr/(?\Q$h\E[^\P{Graph}\/]*)/ : qr/(?[^\P{Graph}\/]+)/; - $pi = (defined $i and $i ne '') ? qr/(?\Q$i\E[^\P{Print}\/]*)/ : qr/(?[^\P{Print}\/]+)/; - - my $store = $LOCALE->encode($CONFIG{store}); - my $template = $LOCALE->encode($CONFIG{template}); - $template =~ s/(\%.)([^\%]*)\z/$1.quotemeta($2)/e; - $template =~ s{(.*?)\%(.)}{$2 eq '%' ? '%' : - $2 eq 's' ? quotemeta($1).$ps : - $2 eq 'h' ? quotemeta($1).$ph : - $2 eq 'i' ? quotemeta($1).$pi : - die "Invalid placeholder %$1"}ge; - my $pattern = qr/\A\Q$store\/\E$template\z/; - myprintf \*STDERR, "Using regexp C<%s>\n", "$pattern" if $CONFIG{debug}; - - my @matches; - foreach my $filename (myglob($gs, $gh, $gi)) { - next unless -f $filename; - $LOCALE->decode($filename) =~ $pattern or die "$filename doesn't match $pattern"; - push @matches, "$+{s}://$+{h}/$+{i}"; - } - return @matches if $all or $#matches < 1; - - # find the longest common prefix to determine the depth level of completion - $matches[0] =~ /\A([A-Za-z0-9-]+):\/\/([^\P{Graph}:\/]+(?::\d+)?)\// or die; - ($s, $h) = ($1, $2); - - if (all { /\A\Q$s\E:\/\/\Q$h\E\// } @matches) { # common host: list all ids - } elsif (all { /\A\Q$s\E:\/\// } @matches) { # common scheme: list only hosts - s#/[^\P{Print}\/]+\z#/# foreach @matches; - } else { # no common scheme: list only schemes - s#://[^\P{Graph}\/]+/[^\P{Print}\/]+\z#://# foreach @matches; - } - - my %matches = map {( $_ => 1 )} @matches; - return sort keys %matches; -} - # Redact passwords, unless $CONFIG{'show-passwords'} is set. sub safeValue($;$) { my $field = shift; @@ -776,15 +709,35 @@ sub getopts(%) { if ($COMMAND eq '_complete') { # used internaly for auto-completion GetOptions(\%CONFIG, qw/zero|0/) or die; + loadConfig(); die unless $#ARGV == 0; + + $CONFIG{recursive} = 1; + my @matches = grep defined, + map { $_->{id} =~ /\A\Q$ARGV[0]\E/ ? $_->{id} : undef} + list( $ARGV[0] =~ /\A(.*\/)/ ? $1 : $ARGV[0] =~ /\A([A-Za-z0-9-]+):\z/ ? "$1://" : () ); + + # find the longest common prefix to determine the depth level of completion + $matches[0] =~ /\A([A-Za-z0-9-]+):\/\/([^\P{Graph}:\/]+(?::\d+)?)\// or die; + my ($s, $h) = ($1, $2); + + if (all { /\A\Q$s\E:\/\/\Q$h\E\// } @matches) { # common host: list all ids + } elsif (all { /\A\Q$s\E:\/\// } @matches) { # common scheme: list only hosts + s#/[^\P{Print}\/]+\z#/# foreach @matches; + } else { # no common scheme: list only schemes + s#://[^\P{Graph}\/]+/[^\P{Print}\/]+\z#://# foreach @matches; + } + my %matches = map {($_ => 1)} grep defined, @matches; + my $delim = $CONFIG{zero} ? "\0" : "\n"; - print $LOCALE->encode($_), $delim foreach complete(shift @ARGV); + print $LOCALE->encode($_), $delim foreach sort keys %matches; exit; } elsif ($COMMAND eq '_geturi') { # used internaly for auto-completion GetOptions(\%CONFIG, qw/socket|s=s/) or die; + loadConfig(); die if @ARGV; print $LOCALE->encode( &connect($CONFIG{socket}) ), "\n"; sendCommand 'QUIT'; -- cgit v1.2.3