[LDAPsh-cvs] ldapsh ldapsh,1.33,1.34
Status: Beta
Brought to you by:
rcorvalan
From: <j-d...@us...> - 2003-12-13 10:22:52
|
Update of /cvsroot/ldapsh/ldapsh In directory sc8-pr-cvs1:/tmp/cvs-serv30622 Modified Files: ldapsh Log Message: * Revamped tab-completion for 'cd' commands and "expansion" commands. Improvements include: - 'cd' commands now match only DNs, not attributes. - "expansion" commands can cope with partially-written LDAP filters. * 'redir' and 'sink' now auto-quote their arguments. Index: ldapsh =================================================================== RCS file: /cvsroot/ldapsh/ldapsh/ldapsh,v retrieving revision 1.33 retrieving revision 1.34 diff -C2 -d -r1.33 -r1.34 *** ldapsh 11 Dec 2003 12:41:51 -0000 1.33 --- ldapsh 13 Dec 2003 10:22:48 -0000 1.34 *************** *** 621,692 **** } - ######################################### - if (0) { - ######################################### - ### Variable Completion ### - my $str1 = substr($line, 0, $end); - my $str = reverse($str1); - - my $AccoladeCounter = 0; - my $AccoladeFound = 0; - my $Found = 0; - my $i; - my $word = ''; - my $wordCompleted = 0; - my $beforeWord = ''; - my $beforeWordCompleted = 0; - for ($i = 0; $i < length($str); $i++) { - $_ = substr($str, $i, 1); - last unless /[][{}\$@%\w>-]/; - - unless ($wordCompleted) { - if (/[\w-]/) { - $word = "$_$word"; - } else { - $wordCompleted = 1; - } - } - - if ($wordCompleted) { - unless ($beforeWordCompleted) { - if (/[[{>-]/) { - $beforeWord = "$_$beforeWord"; - } else { - $beforeWordCompleted = 1; - } - } - } - - $AccoladeCounter++, $AccoladeFound = 1, next if /[{[]/; - $AccoladeCounter--, next if /[]}]/; - - if (/[\$%@]/) { - if (($AccoladeFound and $AccoladeCounter == 1) or (not $AccoladeFound and $AccoladeCounter == 0)) { - $Found = 1; - last; - } - } - } - - if ($Found) { - my $FoundStr = substr($str1, length($str1)-$i-1); - my $prefix = substr($FoundStr, 0, length($FoundStr)-length($word)-length($beforeWord)); - _debug("Found: '$FoundStr'", $word, $beforeWord, $prefix); - - if ($beforeWord eq '') { - } - } - - ######################################### - } - ######################################### - ### Parameter Completion ### my $line2 = $line; $line2 =~ s/^\s+//; my ($cmd, $rest) = split(/\s+/, $line2, 2); ! if ($cmd =~ /^(cd|acd|setdn|pushd|cat|vi|ls|list|dump|ldif|search|cp)$/) { # DN Completion return _cdCommandCompletion(@_); } elsif ($cmd =~ /^(help|which)$/) { # Command Completion --- 621,634 ---- } ### Parameter Completion ### my $line2 = $line; $line2 =~ s/^\s+//; my ($cmd, $rest) = split(/\s+/, $line2, 2); ! if ($cmd =~ /^(cd|acd|setdn|pushd)$/) { # DN Completion return _cdCommandCompletion(@_); + } elsif ($cmd =~ /^(cat|vi|ls|list|csv|dump|ldif|search|cp)$/) { + # Filter Completion + return _filterCommandCompletion(@_); } elsif ($cmd =~ /^(help|which)$/) { # Command Completion *************** *** 727,742 **** my @parts2 = reverse(@parts1); ! my $BaseDN = join(',', @parts2, $CWD); ! my $attr; ! $attr = $1 if $partToComplete =~ /^([^=]+)=/; ! ! if (defined $attr) { ! return _entryDNCompletion($prefix, $attr, $partToComplete, $BaseDN); ! } else { return _attributeNamesDNCompletion($prefix, $partToComplete, $BaseDN); } } sub _attributeEditCompletion{ my ($text, $line, $start, $end) = @_; --- 669,754 ---- my @parts2 = reverse(@parts1); ! my $BaseDN = join(',', @parts2, $Globals->{CWD}{VALUE}); ! #$TermAttribs->{completion_append_character} = "\0"; ! if ($partToComplete =~ m/=/) { ! return _RDNCompletion($prefix, $partToComplete, $BaseDN); ! } ! else { return _attributeNamesDNCompletion($prefix, $partToComplete, $BaseDN); } } + sub _attributeNamesDNCompletion($$$) { + my ($prefix, $partToComplete, $BaseDN) = @_; + + my $result = _searchWEC( + base => $BaseDN, + scope => 'one', + attrs => [], + filter => "(objectclass=*)" + ); + return undef unless defined($result); + + if (_isReallyLDAPError($result->code)) { + if ($result->code == Net::LDAP::Constant::LDAP_NO_SUCH_OBJECT()) { + return undef; + } else { + printf STDERR "Cannot search attribute names. LDAP Error no %s (%s)\n", $result->code, $result->error; + return undef; + } + } else { + my @entries = $result->entries; + my @possible_entries = map {my $a = rdn($_->dn); $a =~ s/=.*/=/; $a;} @entries; + @possible_entries = grep {/^$partToComplete/i} @possible_entries; + return undef unless scalar(@possible_entries); + my $common = _maxCommon(@possible_entries); + if ($common eq $possible_entries[0]) { + # move immediately to the RDNs + $partToComplete = $possible_entries[0]; + @possible_entries = map {rdn($_->dn)} @entries; + @possible_entries = grep {/^$partToComplete/i} @possible_entries; + return undef unless scalar(@possible_entries); + } + if ($prefix) { + map {$_ = "${prefix},$_"} @possible_entries; + $common = "${prefix},${common}"; + } + return $common, @possible_entries; + } + } + + sub _RDNCompletion($$$) { + my ($prefix, $partToComplete, $BaseDN) = @_; + + my $result = _searchWEC( + base => $BaseDN, + scope => 'one', + attrs => [], + filter => "$partToComplete*" + ); + return undef unless defined($result); + + if (_isReallyLDAPError($result->code)) { + if ($result->code == Net::LDAP::Constant::LDAP_NO_SUCH_OBJECT()) { + return undef; + } else { + printf STDERR "Cannot search attribute names. LDAP Error no %s (%s)\n", $result->code, $result->error; + return undef; + } + } else { + my @entries = $result->entries; + my @possible_entries = map {rdn($_->dn)} @entries; + @possible_entries = grep {/^$partToComplete/i} @possible_entries; + return undef unless scalar(@possible_entries); + my $common = _maxCommon(@possible_entries); + if ($prefix) { + map {$_ = "${prefix},$_"} @possible_entries; + $common = "${prefix},${common}"; + } + return $common, @possible_entries; + } + } + sub _attributeEditCompletion{ my ($text, $line, $start, $end) = @_; *************** *** 778,815 **** } - sub _attributeNamesDNCompletion($$$) { - my ($prefix, $partToComplete, $BaseDN) = @_; - $TermAttribs->{completion_append_character} = "\0"; - - my $result = _searchWEC( - base => $BaseDN, - scope => 'one', - attrs => [], - filter => "(objectclass=*)" - ); - return unless defined($result); - - if (_isReallyLDAPError($result->code)) { - if ($result->code == Net::LDAP::Constant::LDAP_NO_SUCH_OBJECT()) { - return undef; - } else { - printf STDERR "Cannot search attribute names. LDAP Error no %s (%s)\n", $result->code, $result->error; - return undef; - } - } else { - my @entries = $result->entries; - my @possible_entries = map {my $a = rdn($_->dn); $a =~ s/=.*/=/; $a;} @entries; - @possible_entries = grep {/^$partToComplete/i} @possible_entries; - return undef unless scalar(@possible_entries); - my $common = _maxCommon(@possible_entries); - if ($prefix) { - map {$_ = "${prefix},$_"} @possible_entries; - $common = "${prefix},${common}"; - } - return $common, @possible_entries; - } - } - - sub _maxCommon { return '' unless scalar(@_); --- 790,793 ---- *************** *** 824,838 **** } ! sub _entryDNCompletion($$$$) { ! my ($prefix, $attr, $partToComplete, $BaseDN) = @_; my $result = _searchWEC( ! base => $BaseDN, scope => 'one', attrs => [$attr], ! filter => "$partToComplete*" ); ! return unless defined($result); if (_isReallyLDAPError($result->code)) { --- 802,823 ---- } + sub _filterCommandCompletion($$$$) { + my ($text, $line, $start, $end) = @_; ! _bindneeded() or return undef; ! ! $TermAttribs->{completion_append_character} = '\0'; ! ! # match the 'filtertype' portion of an LDAP filter (RFC 2254 and RFC 2251) ! my ($prefix, $attr, $extra, $partToComplete) = $text =~ m/(^|^.*\()([a-z][a-z0-9-]*)((?:;[a-z0-9-]*)?(?:=|~=|>=|<=))(.*)$/i; ! return undef unless defined($extra); my $result = _searchWEC( ! base => $Globals->{CWD}{VALUE}, scope => 'one', attrs => [$attr], ! filter => "$attr=$partToComplete*" ); ! return undef unless defined($result); if (_isReallyLDAPError($result->code)) { *************** *** 847,856 **** my @entries = $result->entries; return undef unless scalar(@entries); ! my @possible_entries = map {sprintf("%s=%s", $attr, scalar($_->get_value($attr)))} @entries; my $common = _maxCommon(@possible_entries); - if ($prefix) { - map {$_ = "${prefix},$_"} @possible_entries; - $common = "${prefix},${common}"; - } return $common, @possible_entries; } --- 832,837 ---- my @entries = $result->entries; return undef unless scalar(@entries); ! my @possible_entries = map {$prefix.$attr.$extra.scalar($_->get_value($attr))} @entries; # Bug: only uses first attribute value. my $common = _maxCommon(@possible_entries); return $common, @possible_entries; } *************** *** 2599,2603 **** # complaining that "ou=Users" it's an illegal variable set, and # that uses a bareword.... ! s/^(cd|acd|setdn|pushd|lcd|help|which)\s+((?:[^'"].*)?[^'"])$/$1 "$2"/; # The line below will quote some barewords such as "-l", "-a" etc... --- 2580,2584 ---- # complaining that "ou=Users" it's an illegal variable set, and # that uses a bareword.... ! s/^(cd|acd|setdn|pushd|lcd|help|which|redir|sink)\s+((?:[^'"].*)?[^'"])$/$1 "$2"/; # The line below will quote some barewords such as "-l", "-a" etc... |