now get non-dual lived code
[p5sagit/p5-mst-13.2.git] / lib / Term / ReadLine.pm
CommitLineData
cb31d310 1=head1 NAME
2
7ef5744c 3Term::ReadLine - Perl interface to various C<readline> packages.
4If no real package is found, substitutes stubs instead of basic functions.
cb31d310 5
6=head1 SYNOPSIS
7
8 use Term::ReadLine;
2b393bf4 9 my $term = Term::ReadLine->new('Simple Perl calc');
7824b127 10 my $prompt = "Enter your arithmetic expression: ";
11 my $OUT = $term->OUT || \*STDOUT;
cb31d310 12 while ( defined ($_ = $term->readline($prompt)) ) {
d49f26d4 13 my $res = eval($_);
cb31d310 14 warn $@ if $@;
15 print $OUT $res, "\n" unless $@;
16 $term->addhistory($_) if /\S/;
17 }
18
c07a80fd 19=head1 DESCRIPTION
20
bbb077f8 21This package is just a front end to some other packages. It's a stub to
22set up a common interface to the various ReadLine implementations found on
23CPAN (under the C<Term::ReadLine::*> namespace).
c07a80fd 24
cb31d310 25=head1 Minimal set of supported functions
26
27All the supported functions should be called as methods, i.e., either as
28
2b393bf4 29 $term = Term::ReadLine->new('name');
cb31d310 30
31or as
32
33 $term->addhistory('row');
34
d49f26d4 35where $term is a return value of Term::ReadLine-E<gt>new().
cb31d310 36
37=over 12
38
39=item C<ReadLine>
40
41returns the actual package that executes the commands. Among possible
42values are C<Term::ReadLine::Gnu>, C<Term::ReadLine::Perl>,
e15d0a48 43C<Term::ReadLine::Stub>.
cb31d310 44
45=item C<new>
46
47returns the handle for subsequent calls to following
48functions. Argument is the name of the application. Optionally can be
49followed by two arguments for C<IN> and C<OUT> filehandles. These
50arguments should be globs.
51
52=item C<readline>
53
54gets an input line, I<possibly> with actual C<readline>
55support. Trailing newline is removed. Returns C<undef> on C<EOF>.
56
57=item C<addhistory>
58
59adds the line to the history of input, from where it can be used if
60the actual C<readline> is present.
61
d49f26d4 62=item C<IN>, C<OUT>
cb31d310 63
64return the filehandles for input and output or C<undef> if C<readline>
65input and output cannot be used for Perl.
66
67=item C<MinLine>
68
69If argument is specified, it is an advice on minimal size of line to
70be included into history. C<undef> means do not include anything into
71history. Returns the old value.
72
73=item C<findConsole>
74
75returns an array with two strings that give most appropriate names for
1fef88e7 76files for input and output using conventions C<"E<lt>$in">, C<"E<gt>out">.
cb31d310 77
a737e074 78=item Attribs
79
80returns a reference to a hash which describes internal configuration
81of the package. Names of keys in this hash conform to standard
82conventions with the leading C<rl_> stripped.
83
cb31d310 84=item C<Features>
85
86Returns a reference to a hash with keys being features present in
87current implementation. Several optional features are used in the
88minimal interface: C<appname> should be present if the first argument
89to C<new> is recognized, and C<minline> should be present if
90C<MinLine> method is not dummy. C<autohistory> should be present if
91lines are put into history automatically (maybe subject to
92C<MinLine>), and C<addhistory> if C<addhistory> method is not dummy.
93
a737e074 94If C<Features> method reports a feature C<attribs> as present, the
95method C<Attribs> is not dummy.
96
cb31d310 97=back
98
a737e074 99=head1 Additional supported functions
100
cb31d310 101Actually C<Term::ReadLine> can use some other package, that will
bbb077f8 102support a richer set of commands.
cb31d310 103
a737e074 104All these commands are callable via method interface and have names
105which conform to standard conventions with the leading C<rl_> stripped.
106
f36776d9 107The stub package included with the perl distribution allows some
108additional methods:
109
110=over 12
111
112=item C<tkRunning>
113
7a2e2cd6 114makes Tk event loop run when waiting for user input (i.e., during
f36776d9 115C<readline> method).
116
117=item C<ornaments>
118
119makes the command line stand out by using termcap data. The argument
120to C<ornaments> should be 0, 1, or a string of a form
121C<"aa,bb,cc,dd">. Four components of this string should be names of
122I<terminal capacities>, first two will be issued to make the prompt
123standout, last two to make the input line standout.
124
125=item C<newTTY>
126
127takes two arguments which are input filehandle and output filehandle.
128Switches to use these filehandles.
129
130=back
131
132One can check whether the currently loaded ReadLine package supports
133these methods by checking for corresponding C<Features>.
7a2e2cd6 134
cb31d310 135=head1 EXPORTS
136
137None
138
a737e074 139=head1 ENVIRONMENT
140
8dcee03e 141The environment variable C<PERL_RL> governs which ReadLine clone is
405ff068 142loaded. If the value is false, a dummy interface is used. If the value
143is true, it should be tail of the name of the package to use, such as
144C<Perl> or C<Gnu>.
a737e074 145
405ff068 146As a special case, if the value of this variable is space-separated,
147the tail might be used to disable the ornaments by setting the tail to
148be C<o=0> or C<ornaments=0>. The head should be as described above, say
149
150If the variable is not set, or if the head of space-separated list is
151empty, the best available package is loaded.
152
153 export "PERL_RL=Perl o=0" # Use Perl ReadLine without ornaments
154 export "PERL_RL= o=0" # Use best available ReadLine without ornaments
155
156(Note that processing of C<PERL_RL> for ornaments is in the discretion of the
157particular used C<Term::ReadLine::*> package).
a737e074 158
4c3ccbe1 159=head1 CAVEATS
160
161It seems that using Term::ReadLine from Emacs minibuffer doesn't work
162quite right and one will get an error message like
163
164 Cannot open /dev/tty for read at ...
165
166One possible workaround for this is to explicitly open /dev/tty like this
167
168 open (FH, "/dev/tty" )
169 or eval 'sub Term::ReadLine::findConsole { ("&STDIN", "&STDERR") }';
170 die $@ if $@;
171 close (FH);
172
173or you can try using the 4-argument form of Term::ReadLine->new().
174
cb31d310 175=cut
176
b75c8c73 177use strict;
178
cb31d310 179package Term::ReadLine::Stub;
b75c8c73 180our @ISA = qw'Term::ReadLine::Tk Term::ReadLine::TermCap';
cb31d310 181
182$DB::emacs = $DB::emacs; # To peacify -w
b75c8c73 183our @rl_term_set;
7a2e2cd6 184*rl_term_set = \@Term::ReadLine::TermCap::rl_term_set;
cb31d310 185
2499d329 186sub PERL_UNICODE_STDIN () { 0x0001 }
187
cb31d310 188sub ReadLine {'Term::ReadLine::Stub'}
189sub readline {
a737e074 190 my $self = shift;
191 my ($in,$out,$str) = @$self;
6d697788 192 my $prompt = shift;
193 print $out $rl_term_set[0], $prompt, $rl_term_set[1], $rl_term_set[2];
a737e074 194 $self->register_Tk
195 if not $Term::ReadLine::registered and $Term::ReadLine::toloop
196 and defined &Tk::DoOneEvent;
197 #$str = scalar <$in>;
198 $str = $self->get_line;
6d697788 199 $str =~ s/^\s*\Q$prompt\E// if ($^O eq 'MacOS');
2499d329 200 utf8::upgrade($str)
11412ee6 201 if (${^UNICODE} & PERL_UNICODE_STDIN || defined ${^ENCODING}) &&
202 utf8::valid($str);
7a2e2cd6 203 print $out $rl_term_set[3];
cb31d310 204 # bug in 5.000: chomping empty string creats length -1:
205 chomp $str if defined $str;
206 $str;
207}
208sub addhistory {}
209
210sub findConsole {
211 my $console;
3b2be7b3 212 my $consoleOUT;
cb31d310 213
6d697788 214 if ($^O eq 'MacOS') {
215 $console = "Dev:Console";
216 } elsif (-e "/dev/tty") {
cb31d310 217 $console = "/dev/tty";
8878e869 218 } elsif (-e "con" or $^O eq 'MSWin32') {
3b2be7b3 219 $console = 'CONIN$';
220 $consoleOUT = 'CONOUT$';
cb31d310 221 } else {
222 $console = "sys\$command";
223 }
224
4d2c4e07 225 if (($^O eq 'amigaos') || ($^O eq 'beos') || ($^O eq 'epoc')) {
287f63d2 226 $console = undef;
227 }
228 elsif ($^O eq 'os2') {
cb31d310 229 if ($DB::emacs) {
230 $console = undef;
231 } else {
232 $console = "/dev/con";
233 }
234 }
235
3b2be7b3 236 $consoleOUT = $console unless defined $consoleOUT;
cb31d310 237 $console = "&STDIN" unless defined $console;
238 if (!defined $consoleOUT) {
3b2be7b3 239 $consoleOUT = defined fileno(STDERR) && $^O ne 'MSWin32' ? "&STDERR" : "&STDOUT";
cb31d310 240 }
241 ($console,$consoleOUT);
242}
243
244sub new {
245 die "method new called with wrong number of arguments"
246 unless @_==2 or @_==4;
247 #local (*FIN, *FOUT);
405ff068 248 my ($FIN, $FOUT, $ret);
cb31d310 249 if (@_==2) {
4c3ccbe1 250 my($console, $consoleOUT) = $_[0]->findConsole;
cb31d310 251
3b2be7b3 252
253 # the Windows CONIN$ needs GENERIC_WRITE mode to allow
254 # a SetConsoleMode() if we end up using Term::ReadKey
255 open FIN, ( $^O eq 'MSWin32' && $console eq 'CONIN$' ) ? "+<$console" :
256 "<$console";
257 open FOUT,">$consoleOUT";
258
cb31d310 259 #OUT->autoflush(1); # Conflicts with debugger?
b75c8c73 260 my $sel = select(FOUT);
cb31d310 261 $| = 1; # for DB::OUT
262 select($sel);
405ff068 263 $ret = bless [\*FIN, \*FOUT];
cb31d310 264 } else { # Filehandles supplied
265 $FIN = $_[2]; $FOUT = $_[3];
266 #OUT->autoflush(1); # Conflicts with debugger?
b75c8c73 267 my $sel = select($FOUT);
cb31d310 268 $| = 1; # for DB::OUT
269 select($sel);
405ff068 270 $ret = bless [$FIN, $FOUT];
cb31d310 271 }
405ff068 272 if ($ret->Features->{ornaments}
273 and not ($ENV{PERL_RL} and $ENV{PERL_RL} =~ /\bo\w*=0/)) {
274 local $Term::ReadLine::termcap_nowarn = 1;
275 $ret->ornaments(1);
276 }
277 return $ret;
cb31d310 278}
f36776d9 279
280sub newTTY {
281 my ($self, $in, $out) = @_;
282 $self->[0] = $in;
283 $self->[1] = $out;
284 my $sel = select($out);
285 $| = 1; # for DB::OUT
286 select($sel);
287}
288
cb31d310 289sub IN { shift->[0] }
290sub OUT { shift->[1] }
291sub MinLine { undef }
a737e074 292sub Attribs { {} }
293
84902520 294my %features = (tkRunning => 1, ornaments => 1, 'newTTY' => 1);
a737e074 295sub Features { \%features }
cb31d310 296
4471601f 297sub get_line {
298 my $self = shift;
299 my $in = $self->IN;
300 local ($/) = "\n";
301 return scalar <$in>;
302}
303
cb31d310 304package Term::ReadLine; # So late to allow the above code be defined?
a737e074 305
4471601f 306our $VERSION = '1.03';
b75c8c73 307
405ff068 308my ($which) = exists $ENV{PERL_RL} ? split /\s+/, $ENV{PERL_RL} : undef;
a737e074 309if ($which) {
310 if ($which =~ /\bgnu\b/i){
311 eval "use Term::ReadLine::Gnu;";
312 } elsif ($which =~ /\bperl\b/i) {
313 eval "use Term::ReadLine::Perl;";
314 } else {
315 eval "use Term::ReadLine::$which;";
316 }
405ff068 317} elsif (defined $which and $which ne '') { # Defined but false
a737e074 318 # Do nothing fancy
319} else {
320 eval "use Term::ReadLine::Gnu; 1" or eval "use Term::ReadLine::Perl; 1";
321}
cb31d310 322
323#require FileHandle;
324
325# To make possible switch off RL in debugger: (Not needed, work done
326# in debugger).
b75c8c73 327our @ISA;
cb31d310 328if (defined &Term::ReadLine::Gnu::readline) {
329 @ISA = qw(Term::ReadLine::Gnu Term::ReadLine::Stub);
330} elsif (defined &Term::ReadLine::Perl::readline) {
331 @ISA = qw(Term::ReadLine::Perl Term::ReadLine::Stub);
e15d0a48 332} elsif (defined $which && defined &{"Term::ReadLine::$which\::readline"}) {
333 @ISA = "Term::ReadLine::$which";
cb31d310 334} else {
335 @ISA = qw(Term::ReadLine::Stub);
336}
337
7a2e2cd6 338package Term::ReadLine::TermCap;
339
340# Prompt-start, prompt-end, command-line-start, command-line-end
341# -- zero-width beautifies to emit around prompt and the command line.
b75c8c73 342our @rl_term_set = ("","","","");
7a2e2cd6 343# string encoded:
b75c8c73 344our $rl_term_set = ',,,';
7a2e2cd6 345
b75c8c73 346our $terminal;
7a2e2cd6 347sub LoadTermCap {
348 return if defined $terminal;
349
350 require Term::Cap;
351 $terminal = Tgetent Term::Cap ({OSPEED => 9600}); # Avoid warning.
352}
353
354sub ornaments {
355 shift;
356 return $rl_term_set unless @_;
357 $rl_term_set = shift;
358 $rl_term_set ||= ',,,';
7b8d334a 359 $rl_term_set = 'us,ue,md,me' if $rl_term_set eq '1';
7a2e2cd6 360 my @ts = split /,/, $rl_term_set, 4;
361 eval { LoadTermCap };
405ff068 362 unless (defined $terminal) {
363 warn("Cannot find termcap: $@\n") unless $Term::ReadLine::termcap_nowarn;
364 $rl_term_set = ',,,';
365 return;
366 }
7a2e2cd6 367 @rl_term_set = map {$_ ? $terminal->Tputs($_,1) || '' : ''} @ts;
368 return $rl_term_set;
369}
370
371
a737e074 372package Term::ReadLine::Tk;
373
b75c8c73 374our($count_handle, $count_DoOne, $count_loop);
a737e074 375$count_handle = $count_DoOne = $count_loop = 0;
376
b75c8c73 377our($giveup);
a737e074 378sub handle {$giveup = 1; $count_handle++}
379
380sub Tk_loop {
381 # Tk->tkwait('variable',\$giveup); # needs Widget
382 $count_DoOne++, Tk::DoOneEvent(0) until $giveup;
383 $count_loop++;
384 $giveup = 0;
385}
386
387sub register_Tk {
388 my $self = shift;
389 $Term::ReadLine::registered++
390 or Tk->fileevent($self->IN,'readable',\&handle);
391}
392
393sub tkRunning {
394 $Term::ReadLine::toloop = $_[1] if @_ > 1;
395 $Term::ReadLine::toloop;
396}
397
398sub get_c {
399 my $self = shift;
400 $self->Tk_loop if $Term::ReadLine::toloop && defined &Tk::DoOneEvent;
401 return getc $self->IN;
402}
403
404sub get_line {
405 my $self = shift;
406 $self->Tk_loop if $Term::ReadLine::toloop && defined &Tk::DoOneEvent;
407 my $in = $self->IN;
4e83e451 408 local ($/) = "\n";
a737e074 409 return scalar <$in>;
410}
cb31d310 411
4121;
413