Properly handle Ctrl+C; add '?' and 'man' aliases; don't import symbols
authorDavor Ocelic <docelic@crystallabs.io>
Mon, 29 Apr 2019 21:57:57 +0200
changeset 108 24340fdef276
parent 107 3e31beb6c845
child 109 d37c6197818f
Properly handle Ctrl+C; add '?' and 'man' aliases; don't import symbols Make Ctrl+C behave like in the shell (abort current line, redisplay) Add '?' and 'man' as aliases for help Add qw// on module 'use's to not import any symbols
shelldap
--- a/shelldap	Mon Apr 29 00:16:14 2019 +0200
+++ b/shelldap	Mon Apr 29 21:57:57 2019 +0200
@@ -461,9 +461,9 @@
 package LDAP::Shell;
 use strict;
 use warnings;
-use Term::ReadKey;
-use Term::Shell;
-use Digest::MD5;
+use Term::ReadKey qw//;
+use Term::Shell qw//;
+use Digest::MD5 qw//;
 use Net::LDAP qw/
 	LDAP_SUCCESS
 	LDAP_SERVER_DOWN
@@ -478,16 +478,16 @@
 	LDAP_CONNECT_ERROR
 	LDAP_CONTROL_PAGED /;
 use Net::LDAP::Util qw/ canonical_dn ldap_explode_dn /;
-use Net::LDAP::LDIF;
-use Net::LDAP::Extension::SetPassword;
-use Net::LDAP::Control::Paged;
-use Data::Dumper;
-use File::Temp;
-use Algorithm::Diff;
+use Net::LDAP::LDIF qw//;
+use Net::LDAP::Extension::SetPassword qw//;
+use Net::LDAP::Control::Paged qw//;
+use Data::Dumper qw//;
+use File::Temp qw//;
+use Algorithm::Diff qw//;
 use Carp 'confess';
+use POSIX qw//;
 use base 'Term::Shell';
 
-
 my $conf = $main::conf;
 
 # make 'die' backtrace in debug mode
@@ -1410,6 +1410,8 @@
 	'mv'      => [ 'move',   'comp_cwd' ],
 	'touch'   => [ 'create', 'comp_create' ],
 	'export'  => [ 'setenv', 'comp_setenv' ],
+	'?'       => [ 'help' ],
+	'man'     => [ 'help' ],
 );
 
 
@@ -2457,9 +2459,22 @@
 $conf->{'timeout'}    ||= 10;
 $conf->{'attributes'} ||= ['*'];
 
-# create and enter shell loop
-my $shell = LDAP::Shell->new();
+
+# create and enter shell loop while also handling Ctrl+C correctly.
+my $shell = LDAP::Shell->new;
+my $sigset = POSIX::SigSet->new();
+sub ctrl_c_handler {
+	print "\n";
+	$shell->term->on_new_line;
+	$shell->term->replace_line('', 0);
+	$shell->term->redisplay;
+}
+my $sigaction = POSIX::SigAction->new( \&ctrl_c_handler, $sigset, 0);
+my $old_action = POSIX::SigAction->new;
+POSIX::sigaction(&POSIX::SIGINT, $sigaction, $old_action); # save default one
 $shell->cmdloop();
+POSIX::sigaction(&POSIX::SIGINT, $old_action); # restore default one
+
 
 ### load YAML config into global conf.
 ###