shelldap
changeset 73 b3b840a4b56c
parent 72 39e9f802eb40
child 76 589332cac30b
equal deleted inserted replaced
72:39e9f802eb40 73:b3b840a4b56c
   474 {
   474 {
   475 	my $self = shift;
   475 	my $self = shift;
   476 	$self->{'API'}->{'match_uniq'} = 0;
   476 	$self->{'API'}->{'match_uniq'} = 0;
   477 
   477 
   478 	$self->{'editor'} = $conf->{'editor'} || $ENV{'EDITOR'} || 'vi';
   478 	$self->{'editor'} = $conf->{'editor'} || $ENV{'EDITOR'} || 'vi';
   479 	$self->{'pager'} = $conf->{'pager'} || $ENV{'PAGER'} || 'less';
   479 	$self->{'pager'}  = $conf->{'pager'}  || $ENV{'PAGER'}  || 'less';
   480 	$self->{'env'}	= [ qw/ debug cacheage timeout / ];
   480 	$self->{'env'}	  = [ qw/ debug cacheage timeout / ];
   481 
   481 
   482 	# let autocomplete work with the '=' character
   482 	# let autocomplete work with the '=' character
   483 	my $term = $self->term();
   483 	my $term = $self->term();
   484 	$term->Attribs->{'basic_word_break_characters'}	 =~ s/=//m;
   484 	$term->Attribs->{'basic_word_break_characters'}	 =~ s/=//m;
   485 	$term->Attribs->{'completer_word_break_characters'} =~ s/=//m;
   485 	$term->Attribs->{'completer_word_break_characters'} =~ s/=//m;
   665 		$self->{'ldif'}	      = Net::LDAP::LDIF->new( $fname, 'w', sort => 1, wrap => 0 );
   665 		$self->{'ldif'}	      = Net::LDAP::LDIF->new( $fname, 'w', sort => 1, wrap => 0 );
   666 		$self->{'ldif_fname'} = $fname;
   666 		$self->{'ldif_fname'} = $fname;
   667 	}
   667 	}
   668 
   668 
   669 	# ldif -> stdout
   669 	# ldif -> stdout
       
   670 	#
   670 	else {
   671 	else {
   671 		$self->{'ldif'} = Net::LDAP::LDIF->new( \*STDOUT, 'w', sort => 1, wrap => $self->wrapsize );
   672 		$self->{'ldif'} = Net::LDAP::LDIF->new( \*STDOUT, 'w', sort => 1, wrap => $self->wrapsize );
   672 	}
   673 	}
   673 
   674 
   674 	return $self->{'ldif'};
   675 	return $self->{'ldif'};
   798 	my $self = shift;
   799 	my $self = shift;
   799 	my $dn   = shift or return 0;
   800 	my $dn   = shift or return 0;
   800 
   801 
   801 	my $r = $self->search({ base => $dn });
   802 	my $r = $self->search({ base => $dn });
   802 	return $r->{'code'} == LDAP_SUCCESS ? 1 : 0;
   803 	return $r->{'code'} == LDAP_SUCCESS ? 1 : 0;
       
   804 }
       
   805 
       
   806 
       
   807 ### Emit LDIF to the terminal.
       
   808 ###
       
   809 sub display
       
   810 {
       
   811 	my $self  = shift;
       
   812 	my $dn    = shift;
       
   813 	my @attrs = @{;shift};
       
   814 	my $use_pager = shift;
       
   815 
       
   816 	unless ( $dn ) {
       
   817 		print "No dn provided.\n";
       
   818 		return;
       
   819 	}
       
   820 
       
   821 	# support '.'
       
   822 	$dn = $self->base() if $dn eq '.';
       
   823 
       
   824 	# support globbing
       
   825 	#
       
   826 	my $s;
       
   827 	if ( $dn eq '*' ) {
       
   828 		$s = $self->search({
       
   829 			scope  => 'one',
       
   830 			vals   => 1,
       
   831 			attrs  => \@attrs
       
   832 		});
       
   833 	}
       
   834 	elsif ( $dn =~ /\*/ ) {
       
   835 		$s = $self->search({
       
   836 			scope  => 'one',
       
   837 			vals   => 1,
       
   838 			filter => $dn,
       
   839 			attrs  => \@attrs
       
   840 		});
       
   841 	}
       
   842 
       
   843 	# absolute/relative dn
       
   844 	#
       
   845 	else {
       
   846 		$dn = $self->path_to_dn( $dn );
       
   847 		$s = $self->search({
       
   848 			base   => $dn,
       
   849 			vals   => 1,
       
   850 			attrs  => \@attrs
       
   851 		});
       
   852 	}
       
   853 
       
   854 	# emit error, if any
       
   855 	#
       
   856 	if ( $s->{'code'} ) {
       
   857 		print $s->{'message'} . "\n";
       
   858 		return;
       
   859 	}
       
   860 
       
   861 	# display to stdout or pager
       
   862 	#
       
   863 	my $ldif = $self->ldif( $use_pager );
       
   864 	foreach my $e ( @{ $s->{'entries'} } ) {
       
   865 		$ldif->write_entry( $e );
       
   866 	}
       
   867 	if( $use_pager ) {
       
   868 		system( $self->{'pager'}, $self->{'ldif_fname'} );
       
   869 		unlink $self->{'ldif_fname'};
       
   870 	}	
       
   871 	return;
   803 }
   872 }
   804 
   873 
   805 
   874 
   806 ### Perform an LDAP search.
   875 ### Perform an LDAP search.
   807 ###
   876 ###
  1127 	foreach ( %cmd_map ) {
  1196 	foreach ( %cmd_map ) {
  1128 		next unless $_;
  1197 		next unless $_;
  1129 		my $sub = "comp_$_";
  1198 		my $sub = "comp_$_";
  1130 		*$sub   = \&autocomplete_cwd;
  1199 		*$sub   = \&autocomplete_cwd;
  1131 	}
  1200 	}
  1132 	*comp_touch   = \&comp_create;
  1201 	*comp_touch  = \&comp_create;
  1133 	*comp_export  = \&comp_setenv;
  1202 	*comp_export = \&comp_setenv;
  1134 
  1203 
  1135 	# setup alias subs
  1204 	# setup alias subs
  1136 	#
  1205 	#
  1137 	# Term::Shell has an alias_* feature, but
  1206 	# Term::Shell has an alias_* feature, but
  1138 	# it seems to work about 90% of the time.
  1207 	# it seems to work about 90% of the time.
  1200 {
  1269 {
  1201 	my $self  = shift;
  1270 	my $self  = shift;
  1202 	my $dn    = shift;
  1271 	my $dn    = shift;
  1203 	my @attrs = (@_) ? @_ : ('*');
  1272 	my @attrs = (@_) ? @_ : ('*');
  1204 
  1273 
  1205 	display($self, $dn, \@attrs, 0);
  1274 	$self->display( $dn, \@attrs, 0 );
  1206 }
  1275 }
  1207 
  1276 
       
  1277 
       
  1278 ### Display an entry as LDIF to the terminal with external pagination.
       
  1279 ###
  1208 sub run_less
  1280 sub run_less
  1209 {
  1281 {
  1210 	my $self  = shift;
  1282 	my $self  = shift;
  1211 	my $dn    = shift;
  1283 	my $dn    = shift;
  1212 	my @attrs = (@_) ? @_ : ('*');
  1284 	my @attrs = (@_) ? @_ : ('*');
  1213 
  1285 
  1214 	display($self, $dn, \@attrs, 1);
  1286 	$self->display( $dn, \@attrs, 1 );
  1215 }
       
  1216 
       
  1217 sub display{
       
  1218 	my $self  = shift;
       
  1219 	my $dn    = shift;
       
  1220 	my @attrs = @{;shift};
       
  1221 	my $use_pager = shift;
       
  1222 
       
  1223 	unless ( $dn ) {
       
  1224 		print "No dn provided.\n";
       
  1225 		return;
       
  1226 	}
       
  1227 
       
  1228 	# support '.'
       
  1229 	$dn = $self->base() if $dn eq '.';
       
  1230 
       
  1231 	# support globbing
       
  1232 	#
       
  1233 	my $s;
       
  1234 	if ( $dn eq '*' ) {
       
  1235 		$s = $self->search({
       
  1236 			scope  => 'one',
       
  1237 			vals   => 1,
       
  1238 			attrs  => \@attrs
       
  1239 		});
       
  1240 	}
       
  1241 	elsif ( $dn =~ /\*/ ) {
       
  1242 		$s = $self->search({
       
  1243 			scope  => 'one',
       
  1244 			vals   => 1,
       
  1245 			filter => $dn,
       
  1246 			attrs  => \@attrs
       
  1247 		});
       
  1248 	}
       
  1249 
       
  1250 	# absolute/relative dn
       
  1251 	#
       
  1252 	else {
       
  1253 		$dn = $self->path_to_dn( $dn );
       
  1254 		$s = $self->search({
       
  1255 			base   => $dn,
       
  1256 			vals   => 1,
       
  1257 			attrs  => \@attrs
       
  1258 		});
       
  1259 	}
       
  1260 
       
  1261 	# emit error, if any
       
  1262 	#
       
  1263 	if ( $s->{'code'} ) {
       
  1264 		print $s->{'message'} . "\n";
       
  1265 		return;
       
  1266 	}
       
  1267 
       
  1268 	# display to stdout or pager
       
  1269 	#
       
  1270 	my $ldif = $self->ldif($use_pager);
       
  1271 	foreach my $e ( @{ $s->{'entries'} } ) {
       
  1272 		$ldif->write_entry( $e );
       
  1273 	}
       
  1274 	if($use_pager) {
       
  1275 		system( $self->{'pager'}, $self->{'ldif_fname'} );
       
  1276 		unlink $self->{'ldif_fname'};
       
  1277 	}	
       
  1278 	return;
       
  1279 }
  1287 }
  1280 
  1288 
  1281 
  1289 
  1282 ### Change shelldap's idea of a current working 'directory',
  1290 ### Change shelldap's idea of a current working 'directory',
  1283 ### by adjusting the current default basedn for all searches.
  1291 ### by adjusting the current default basedn for all searches.
  2276 package main;
  2284 package main;
  2277 use strict;
  2285 use strict;
  2278 use warnings;
  2286 use warnings;
  2279 
  2287 
  2280 $0 = 'shelldap';
  2288 $0 = 'shelldap';
  2281 my $VERSION = '1.2.0';
  2289 my $VERSION = '1.3.0';
  2282 
  2290 
  2283 use Getopt::Long;
  2291 use Getopt::Long;
  2284 use YAML::Syck;
  2292 use YAML::Syck;
  2285 use Pod::Usage;
  2293 use Pod::Usage;
  2286 eval 'use Term::ReadLine::Gnu';
  2294 eval 'use Term::ReadLine::Gnu';