xref: /openbsd-src/gnu/usr.bin/perl/dist/Term-ReadLine/lib/Term/ReadLine.pm (revision 9f11ffb7133c203312a01e4b986886bc88c7d74b)
1898184e3Ssthen=head1 NAME
2898184e3Ssthen
3898184e3SsthenTerm::ReadLine - Perl interface to various C<readline> packages.
4898184e3SsthenIf no real package is found, substitutes stubs instead of basic functions.
5898184e3Ssthen
6898184e3Ssthen=head1 SYNOPSIS
7898184e3Ssthen
8898184e3Ssthen  use Term::ReadLine;
9898184e3Ssthen  my $term = Term::ReadLine->new('Simple Perl calc');
10898184e3Ssthen  my $prompt = "Enter your arithmetic expression: ";
11898184e3Ssthen  my $OUT = $term->OUT || \*STDOUT;
12898184e3Ssthen  while ( defined ($_ = $term->readline($prompt)) ) {
13898184e3Ssthen    my $res = eval($_);
14898184e3Ssthen    warn $@ if $@;
15898184e3Ssthen    print $OUT $res, "\n" unless $@;
16898184e3Ssthen    $term->addhistory($_) if /\S/;
17898184e3Ssthen  }
18898184e3Ssthen
19898184e3Ssthen=head1 DESCRIPTION
20898184e3Ssthen
21898184e3SsthenThis package is just a front end to some other packages. It's a stub to
22898184e3Ssthenset up a common interface to the various ReadLine implementations found on
23898184e3SsthenCPAN (under the C<Term::ReadLine::*> namespace).
24898184e3Ssthen
25898184e3Ssthen=head1 Minimal set of supported functions
26898184e3Ssthen
27898184e3SsthenAll the supported functions should be called as methods, i.e., either as
28898184e3Ssthen
29898184e3Ssthen  $term = Term::ReadLine->new('name');
30898184e3Ssthen
31898184e3Ssthenor as
32898184e3Ssthen
33898184e3Ssthen  $term->addhistory('row');
34898184e3Ssthen
35898184e3Ssthenwhere $term is a return value of Term::ReadLine-E<gt>new().
36898184e3Ssthen
37898184e3Ssthen=over 12
38898184e3Ssthen
39898184e3Ssthen=item C<ReadLine>
40898184e3Ssthen
41898184e3Ssthenreturns the actual package that executes the commands. Among possible
42898184e3Ssthenvalues are C<Term::ReadLine::Gnu>, C<Term::ReadLine::Perl>,
43898184e3SsthenC<Term::ReadLine::Stub>.
44898184e3Ssthen
45898184e3Ssthen=item C<new>
46898184e3Ssthen
47898184e3Ssthenreturns the handle for subsequent calls to following
48898184e3Ssthenfunctions. Argument is the name of the application. Optionally can be
49898184e3Ssthenfollowed by two arguments for C<IN> and C<OUT> filehandles. These
50898184e3Ssthenarguments should be globs.
51898184e3Ssthen
52898184e3Ssthen=item C<readline>
53898184e3Ssthen
54898184e3Ssthengets an input line, I<possibly> with actual C<readline>
55898184e3Ssthensupport. Trailing newline is removed. Returns C<undef> on C<EOF>.
56898184e3Ssthen
57898184e3Ssthen=item C<addhistory>
58898184e3Ssthen
59898184e3Ssthenadds the line to the history of input, from where it can be used if
60898184e3Ssthenthe actual C<readline> is present.
61898184e3Ssthen
62898184e3Ssthen=item C<IN>, C<OUT>
63898184e3Ssthen
64898184e3Ssthenreturn the filehandles for input and output or C<undef> if C<readline>
65898184e3Sstheninput and output cannot be used for Perl.
66898184e3Ssthen
67898184e3Ssthen=item C<MinLine>
68898184e3Ssthen
69898184e3SsthenIf argument is specified, it is an advice on minimal size of line to
70898184e3Ssthenbe included into history.  C<undef> means do not include anything into
71898184e3Ssthenhistory. Returns the old value.
72898184e3Ssthen
73898184e3Ssthen=item C<findConsole>
74898184e3Ssthen
75898184e3Ssthenreturns an array with two strings that give most appropriate names for
76898184e3Ssthenfiles for input and output using conventions C<"E<lt>$in">, C<"E<gt>out">.
77898184e3Ssthen
78*9f11ffb7Safresh1The strings returned may not be useful for 3-argument open().
79*9f11ffb7Safresh1
80898184e3Ssthen=item Attribs
81898184e3Ssthen
82898184e3Ssthenreturns a reference to a hash which describes internal configuration
83898184e3Ssthenof the package. Names of keys in this hash conform to standard
84898184e3Ssthenconventions with the leading C<rl_> stripped.
85898184e3Ssthen
86898184e3Ssthen=item C<Features>
87898184e3Ssthen
88898184e3SsthenReturns a reference to a hash with keys being features present in
89898184e3Ssthencurrent implementation. Several optional features are used in the
90898184e3Ssthenminimal interface: C<appname> should be present if the first argument
91898184e3Ssthento C<new> is recognized, and C<minline> should be present if
92898184e3SsthenC<MinLine> method is not dummy.  C<autohistory> should be present if
93898184e3Ssthenlines are put into history automatically (maybe subject to
94898184e3SsthenC<MinLine>), and C<addhistory> if C<addhistory> method is not dummy.
95898184e3Ssthen
96898184e3SsthenIf C<Features> method reports a feature C<attribs> as present, the
97898184e3Ssthenmethod C<Attribs> is not dummy.
98898184e3Ssthen
99898184e3Ssthen=back
100898184e3Ssthen
101898184e3Ssthen=head1 Additional supported functions
102898184e3Ssthen
103898184e3SsthenActually C<Term::ReadLine> can use some other package, that will
104898184e3Ssthensupport a richer set of commands.
105898184e3Ssthen
106898184e3SsthenAll these commands are callable via method interface and have names
107898184e3Ssthenwhich conform to standard conventions with the leading C<rl_> stripped.
108898184e3Ssthen
109898184e3SsthenThe stub package included with the perl distribution allows some
110898184e3Ssthenadditional methods:
111898184e3Ssthen
112898184e3Ssthen=over 12
113898184e3Ssthen
114898184e3Ssthen=item C<tkRunning>
115898184e3Ssthen
116898184e3Ssthenmakes Tk event loop run when waiting for user input (i.e., during
117898184e3SsthenC<readline> method).
118898184e3Ssthen
119898184e3Ssthen=item C<event_loop>
120898184e3Ssthen
121898184e3SsthenRegisters call-backs to wait for user input (i.e., during C<readline>
12291f110e0Safresh1method).  This supersedes tkRunning.
123898184e3Ssthen
124898184e3SsthenThe first call-back registered is the call back for waiting.  It is
125898184e3Ssthenexpected that the callback will call the current event loop until
126898184e3Ssthenthere is something waiting to get on the input filehandle.  The parameter
127898184e3Ssthenpassed in is the return value of the second call back.
128898184e3Ssthen
129898184e3SsthenThe second call-back registered is the call back for registration.  The
130898184e3Sstheninput filehandle (often STDIN, but not necessarily) will be passed in.
131898184e3Ssthen
132898184e3SsthenFor example, with AnyEvent:
133898184e3Ssthen
134898184e3Ssthen  $term->event_loop(sub {
135898184e3Ssthen    my $data = shift;
136898184e3Ssthen    $data->[1] = AE::cv();
137898184e3Ssthen    $data->[1]->recv();
138898184e3Ssthen  }, sub {
139898184e3Ssthen    my $fh = shift;
140898184e3Ssthen    my $data = [];
141898184e3Ssthen    $data->[0] = AE::io($fh, 0, sub { $data->[1]->send() });
142898184e3Ssthen    $data;
143898184e3Ssthen  });
144898184e3Ssthen
145898184e3SsthenThe second call-back is optional if the call back is registered prior to
146898184e3Ssthenthe call to $term-E<gt>readline.
147898184e3Ssthen
148898184e3SsthenDeregistration is done in this case by calling event_loop with C<undef>
149898184e3Ssthenas its parameter:
150898184e3Ssthen
151898184e3Ssthen    $term->event_loop(undef);
152898184e3Ssthen
153898184e3SsthenThis will cause the data array ref to be removed, allowing normal garbage
154898184e3Ssthencollection to clean it up.  With AnyEvent, that will cause $data->[0] to
155898184e3Ssthenbe cleaned up, and AnyEvent will automatically cancel the watcher at that
156898184e3Ssthentime.  If another loop requires more than that to clean up a file watcher,
157898184e3Ssthenthat will be up to the caller to handle.
158898184e3Ssthen
159898184e3Ssthen=item C<ornaments>
160898184e3Ssthen
161898184e3Ssthenmakes the command line stand out by using termcap data.  The argument
162898184e3Ssthento C<ornaments> should be 0, 1, or a string of a form
163898184e3SsthenC<"aa,bb,cc,dd">.  Four components of this string should be names of
164898184e3SsthenI<terminal capacities>, first two will be issued to make the prompt
165898184e3Ssthenstandout, last two to make the input line standout.
166898184e3Ssthen
167898184e3Ssthen=item C<newTTY>
168898184e3Ssthen
169898184e3Ssthentakes two arguments which are input filehandle and output filehandle.
170898184e3SsthenSwitches to use these filehandles.
171898184e3Ssthen
172898184e3Ssthen=back
173898184e3Ssthen
174898184e3SsthenOne can check whether the currently loaded ReadLine package supports
175898184e3Ssthenthese methods by checking for corresponding C<Features>.
176898184e3Ssthen
177898184e3Ssthen=head1 EXPORTS
178898184e3Ssthen
179898184e3SsthenNone
180898184e3Ssthen
181898184e3Ssthen=head1 ENVIRONMENT
182898184e3Ssthen
183898184e3SsthenThe environment variable C<PERL_RL> governs which ReadLine clone is
184898184e3Ssthenloaded. If the value is false, a dummy interface is used. If the value
185898184e3Ssthenis true, it should be tail of the name of the package to use, such as
186898184e3SsthenC<Perl> or C<Gnu>.
187898184e3Ssthen
188898184e3SsthenAs a special case, if the value of this variable is space-separated,
189898184e3Ssthenthe tail might be used to disable the ornaments by setting the tail to
190898184e3Ssthenbe C<o=0> or C<ornaments=0>.  The head should be as described above, say
191898184e3Ssthen
192898184e3SsthenIf the variable is not set, or if the head of space-separated list is
193898184e3Ssthenempty, the best available package is loaded.
194898184e3Ssthen
195898184e3Ssthen  export "PERL_RL=Perl o=0" # Use Perl ReadLine sans ornaments
196898184e3Ssthen  export "PERL_RL= o=0"     # Use best available ReadLine sans ornaments
197898184e3Ssthen
198898184e3Ssthen(Note that processing of C<PERL_RL> for ornaments is in the discretion of the
199898184e3Ssthenparticular used C<Term::ReadLine::*> package).
200898184e3Ssthen
201898184e3Ssthen=cut
202898184e3Ssthen
203898184e3Ssthenuse strict;
204898184e3Ssthen
205898184e3Ssthenpackage Term::ReadLine::Stub;
206898184e3Ssthenour @ISA = qw'Term::ReadLine::Tk Term::ReadLine::TermCap';
207898184e3Ssthen
2086fb12b70Safresh1$DB::emacs = $DB::emacs;	# To pacify -w
209898184e3Ssthenour @rl_term_set;
210898184e3Ssthen*rl_term_set = \@Term::ReadLine::TermCap::rl_term_set;
211898184e3Ssthen
212898184e3Ssthensub PERL_UNICODE_STDIN () { 0x0001 }
213898184e3Ssthen
214898184e3Ssthensub ReadLine {'Term::ReadLine::Stub'}
215898184e3Ssthensub readline {
216898184e3Ssthen  my $self = shift;
217898184e3Ssthen  my ($in,$out,$str) = @$self;
218898184e3Ssthen  my $prompt = shift;
219898184e3Ssthen  print $out $rl_term_set[0], $prompt, $rl_term_set[1], $rl_term_set[2];
220898184e3Ssthen  $self->register_Tk
221898184e3Ssthen     if not $Term::ReadLine::registered and $Term::ReadLine::toloop;
222898184e3Ssthen  #$str = scalar <$in>;
223898184e3Ssthen  $str = $self->get_line;
224898184e3Ssthen  utf8::upgrade($str)
225898184e3Ssthen      if (${^UNICODE} & PERL_UNICODE_STDIN || defined ${^ENCODING}) &&
226898184e3Ssthen         utf8::valid($str);
227898184e3Ssthen  print $out $rl_term_set[3];
2286fb12b70Safresh1  # bug in 5.000: chomping empty string creates length -1:
229898184e3Ssthen  chomp $str if defined $str;
230898184e3Ssthen  $str;
231898184e3Ssthen}
232898184e3Ssthensub addhistory {}
233898184e3Ssthen
234*9f11ffb7Safresh1# used for testing purpose
235*9f11ffb7Safresh1sub devtty { return '/dev/tty' }
236*9f11ffb7Safresh1
237898184e3Ssthensub findConsole {
238898184e3Ssthen    my $console;
239898184e3Ssthen    my $consoleOUT;
240898184e3Ssthen
241*9f11ffb7Safresh1    my $devtty = devtty();
242*9f11ffb7Safresh1
243*9f11ffb7Safresh1    if ($^O ne 'MSWin32' and -e $devtty) {
244*9f11ffb7Safresh1	$console = $devtty;
245b8851fccSafresh1    } elsif ($^O eq 'MSWin32' or $^O eq 'msys' or -e "con") {
246898184e3Ssthen       $console = 'CONIN$';
247898184e3Ssthen       $consoleOUT = 'CONOUT$';
24891f110e0Safresh1    } elsif ($^O eq 'VMS') {
249898184e3Ssthen	$console = "sys\$command";
25091f110e0Safresh1    } elsif ($^O eq 'os2' && !$DB::emacs) {
251898184e3Ssthen	$console = "/dev/con";
25291f110e0Safresh1    } else {
25391f110e0Safresh1	$console = undef;
254898184e3Ssthen    }
255898184e3Ssthen
256898184e3Ssthen    $consoleOUT = $console unless defined $consoleOUT;
257898184e3Ssthen    $console = "&STDIN" unless defined $console;
258*9f11ffb7Safresh1    if ($console eq $devtty && !open(my $fh, "<", $console)) {
259898184e3Ssthen      $console = "&STDIN";
260898184e3Ssthen      undef($consoleOUT);
261898184e3Ssthen    }
262898184e3Ssthen    if (!defined $consoleOUT) {
263898184e3Ssthen      $consoleOUT = defined fileno(STDERR) && $^O ne 'MSWin32' ? "&STDERR" : "&STDOUT";
264898184e3Ssthen    }
265898184e3Ssthen    ($console,$consoleOUT);
266898184e3Ssthen}
267898184e3Ssthen
268898184e3Ssthensub new {
269898184e3Ssthen  die "method new called with wrong number of arguments"
270898184e3Ssthen    unless @_==2 or @_==4;
271898184e3Ssthen  #local (*FIN, *FOUT);
272898184e3Ssthen  my ($FIN, $FOUT, $ret);
273898184e3Ssthen  if (@_==2) {
274898184e3Ssthen    my($console, $consoleOUT) = $_[0]->findConsole;
275898184e3Ssthen
276898184e3Ssthen    # the Windows CONIN$ needs GENERIC_WRITE mode to allow
277898184e3Ssthen    # a SetConsoleMode() if we end up using Term::ReadKey
278*9f11ffb7Safresh1    open FIN, (( $^O eq 'MSWin32' && $console eq 'CONIN$' ) ? '+<' : '<' ), $console;
279*9f11ffb7Safresh1    # RT #132008:  Still need 2-arg open here
280898184e3Ssthen    open FOUT,">$consoleOUT";
281898184e3Ssthen
282898184e3Ssthen    #OUT->autoflush(1);		# Conflicts with debugger?
283898184e3Ssthen    my $sel = select(FOUT);
284898184e3Ssthen    $| = 1;				# for DB::OUT
285898184e3Ssthen    select($sel);
286898184e3Ssthen    $ret = bless [\*FIN, \*FOUT];
287898184e3Ssthen  } else {			# Filehandles supplied
288898184e3Ssthen    $FIN = $_[2]; $FOUT = $_[3];
289898184e3Ssthen    #OUT->autoflush(1);		# Conflicts with debugger?
290898184e3Ssthen    my $sel = select($FOUT);
291898184e3Ssthen    $| = 1;				# for DB::OUT
292898184e3Ssthen    select($sel);
293898184e3Ssthen    $ret = bless [$FIN, $FOUT];
294898184e3Ssthen  }
295898184e3Ssthen  if ($ret->Features->{ornaments}
296898184e3Ssthen      and not ($ENV{PERL_RL} and $ENV{PERL_RL} =~ /\bo\w*=0/)) {
297898184e3Ssthen    local $Term::ReadLine::termcap_nowarn = 1;
298898184e3Ssthen    $ret->ornaments(1);
299898184e3Ssthen  }
300898184e3Ssthen  return $ret;
301898184e3Ssthen}
302898184e3Ssthen
303898184e3Ssthensub newTTY {
304898184e3Ssthen  my ($self, $in, $out) = @_;
305898184e3Ssthen  $self->[0] = $in;
306898184e3Ssthen  $self->[1] = $out;
307898184e3Ssthen  my $sel = select($out);
308898184e3Ssthen  $| = 1;				# for DB::OUT
309898184e3Ssthen  select($sel);
310898184e3Ssthen}
311898184e3Ssthen
312898184e3Ssthensub IN { shift->[0] }
313898184e3Ssthensub OUT { shift->[1] }
314898184e3Ssthensub MinLine { undef }
315898184e3Ssthensub Attribs { {} }
316898184e3Ssthen
317898184e3Ssthenmy %features = (tkRunning => 1, ornaments => 1, 'newTTY' => 1);
318898184e3Ssthensub Features { \%features }
319898184e3Ssthen
320898184e3Ssthen#sub get_line {
321898184e3Ssthen#  my $self = shift;
322898184e3Ssthen#  my $in = $self->IN;
323898184e3Ssthen#  local ($/) = "\n";
324898184e3Ssthen#  return scalar <$in>;
325898184e3Ssthen#}
326898184e3Ssthen
327898184e3Ssthenpackage Term::ReadLine;		# So late to allow the above code be defined?
328898184e3Ssthen
329*9f11ffb7Safresh1our $VERSION = '1.17';
330898184e3Ssthen
331898184e3Ssthenmy ($which) = exists $ENV{PERL_RL} ? split /\s+/, $ENV{PERL_RL} : undef;
332898184e3Ssthenif ($which) {
333898184e3Ssthen  if ($which =~ /\bgnu\b/i){
334898184e3Ssthen    eval "use Term::ReadLine::Gnu;";
335898184e3Ssthen  } elsif ($which =~ /\bperl\b/i) {
336898184e3Ssthen    eval "use Term::ReadLine::Perl;";
337898184e3Ssthen  } elsif ($which =~ /^(Stub|TermCap|Tk)$/) {
338898184e3Ssthen    # it is already in memory to avoid false exception as seen in:
339898184e3Ssthen    # PERL_RL=Stub perl -e'$SIG{__DIE__} = sub { print @_ }; require Term::ReadLine'
340898184e3Ssthen  } else {
341898184e3Ssthen    eval "use Term::ReadLine::$which;";
342898184e3Ssthen  }
343898184e3Ssthen} elsif (defined $which and $which ne '') {	# Defined but false
344898184e3Ssthen  # Do nothing fancy
345898184e3Ssthen} else {
3466fb12b70Safresh1  eval "use Term::ReadLine::Gnu; 1" or eval "use Term::ReadLine::EditLine; 1" or eval "use Term::ReadLine::Perl; 1";
347898184e3Ssthen}
348898184e3Ssthen
349898184e3Ssthen#require FileHandle;
350898184e3Ssthen
351898184e3Ssthen# To make possible switch off RL in debugger: (Not needed, work done
352898184e3Ssthen# in debugger).
353898184e3Ssthenour @ISA;
354898184e3Ssthenif (defined &Term::ReadLine::Gnu::readline) {
355898184e3Ssthen  @ISA = qw(Term::ReadLine::Gnu Term::ReadLine::Stub);
3566fb12b70Safresh1} elsif (defined &Term::ReadLine::EditLine::readline) {
3576fb12b70Safresh1  @ISA = qw(Term::ReadLine::EditLine Term::ReadLine::Stub);
358898184e3Ssthen} elsif (defined &Term::ReadLine::Perl::readline) {
359898184e3Ssthen  @ISA = qw(Term::ReadLine::Perl Term::ReadLine::Stub);
360898184e3Ssthen} elsif (defined $which && defined &{"Term::ReadLine::$which\::readline"}) {
361898184e3Ssthen  @ISA = "Term::ReadLine::$which";
362898184e3Ssthen} else {
363898184e3Ssthen  @ISA = qw(Term::ReadLine::Stub);
364898184e3Ssthen}
365898184e3Ssthen
366898184e3Ssthenpackage Term::ReadLine::TermCap;
367898184e3Ssthen
368898184e3Ssthen# Prompt-start, prompt-end, command-line-start, command-line-end
369898184e3Ssthen#     -- zero-width beautifies to emit around prompt and the command line.
370898184e3Ssthenour @rl_term_set = ("","","","");
371898184e3Ssthen# string encoded:
372898184e3Ssthenour $rl_term_set = ',,,';
373898184e3Ssthen
374898184e3Ssthenour $terminal;
375898184e3Ssthensub LoadTermCap {
376898184e3Ssthen  return if defined $terminal;
377898184e3Ssthen
378898184e3Ssthen  require Term::Cap;
379898184e3Ssthen  $terminal = Tgetent Term::Cap ({OSPEED => 9600}); # Avoid warning.
380898184e3Ssthen}
381898184e3Ssthen
382898184e3Ssthensub ornaments {
383898184e3Ssthen  shift;
384898184e3Ssthen  return $rl_term_set unless @_;
385898184e3Ssthen  $rl_term_set = shift;
386898184e3Ssthen  $rl_term_set ||= ',,,';
387898184e3Ssthen  $rl_term_set = 'us,ue,md,me' if $rl_term_set eq '1';
388898184e3Ssthen  my @ts = split /,/, $rl_term_set, 4;
389898184e3Ssthen  eval { LoadTermCap };
390898184e3Ssthen  unless (defined $terminal) {
391898184e3Ssthen    warn("Cannot find termcap: $@\n") unless $Term::ReadLine::termcap_nowarn;
392898184e3Ssthen    $rl_term_set = ',,,';
393898184e3Ssthen    return;
394898184e3Ssthen  }
395898184e3Ssthen  @rl_term_set = map {$_ ? $terminal->Tputs($_,1) || '' : ''} @ts;
396898184e3Ssthen  return $rl_term_set;
397898184e3Ssthen}
398898184e3Ssthen
399898184e3Ssthen
400898184e3Ssthenpackage Term::ReadLine::Tk;
401898184e3Ssthen
402898184e3Ssthen# This package inserts a Tk->fileevent() before the diamond operator.
403898184e3Ssthen# The Tk watcher dispatches Tk events until the filehandle returned by
404898184e3Ssthen# the$term->IN() accessor becomes ready for reading.  It's assumed
405898184e3Ssthen# that the diamond operator will return a line of input immediately at
406898184e3Ssthen# that point.
407898184e3Ssthen
408898184e3Ssthenmy ($giveup);
409898184e3Ssthen
410898184e3Ssthen# maybe in the future the Tk-specific aspects will be removed.
411898184e3Ssthensub Tk_loop{
412898184e3Ssthen    if (ref $Term::ReadLine::toloop)
413898184e3Ssthen    {
414898184e3Ssthen        $Term::ReadLine::toloop->[0]->($Term::ReadLine::toloop->[2]);
415898184e3Ssthen    }
416898184e3Ssthen    else
417898184e3Ssthen    {
418898184e3Ssthen        Tk::DoOneEvent(0) until $giveup;
419898184e3Ssthen        $giveup = 0;
420898184e3Ssthen    }
421898184e3Ssthen};
422898184e3Ssthen
423898184e3Ssthensub register_Tk {
424898184e3Ssthen    my $self = shift;
425898184e3Ssthen    unless ($Term::ReadLine::registered++)
426898184e3Ssthen    {
427898184e3Ssthen        if (ref $Term::ReadLine::toloop)
428898184e3Ssthen        {
429898184e3Ssthen            $Term::ReadLine::toloop->[2] = $Term::ReadLine::toloop->[1]->($self->IN) if $Term::ReadLine::toloop->[1];
430898184e3Ssthen        }
431898184e3Ssthen        else
432898184e3Ssthen        {
433898184e3Ssthen            Tk->fileevent($self->IN,'readable',sub { $giveup = 1});
434898184e3Ssthen        }
435898184e3Ssthen    }
436898184e3Ssthen};
437898184e3Ssthen
438898184e3Ssthensub tkRunning {
439898184e3Ssthen  $Term::ReadLine::toloop = $_[1] if @_ > 1;
440898184e3Ssthen  $Term::ReadLine::toloop;
441898184e3Ssthen}
442898184e3Ssthen
443898184e3Ssthensub event_loop {
444898184e3Ssthen    shift;
445898184e3Ssthen
446898184e3Ssthen    # T::RL::Gnu and T::RL::Perl check that this exists, if not,
447898184e3Ssthen    # it doesn't call the loop.  Those modules will need to be
448898184e3Ssthen    # fixed before this can be removed.
449898184e3Ssthen    if (not defined &Tk::DoOneEvent)
450898184e3Ssthen    {
451898184e3Ssthen        *Tk::DoOneEvent = sub {
452898184e3Ssthen            die "what?"; # this shouldn't be called.
453898184e3Ssthen        }
454898184e3Ssthen    }
455898184e3Ssthen
456898184e3Ssthen    # store the callback in toloop, again so that other modules will
457898184e3Ssthen    # recognise it and call us for the loop.
458898184e3Ssthen    $Term::ReadLine::toloop = [ @_ ] if @_ > 0; # 0 because we shifted off $self.
459898184e3Ssthen    $Term::ReadLine::toloop;
460898184e3Ssthen}
461898184e3Ssthen
462898184e3Ssthensub PERL_UNICODE_STDIN () { 0x0001 }
463898184e3Ssthen
464898184e3Ssthensub get_line {
465898184e3Ssthen  my $self = shift;
466898184e3Ssthen  my ($in,$out,$str) = @$self;
467898184e3Ssthen
468898184e3Ssthen  if ($Term::ReadLine::toloop) {
469898184e3Ssthen    $self->register_Tk if not $Term::ReadLine::registered;
470898184e3Ssthen    $self->Tk_loop;
471898184e3Ssthen  }
472898184e3Ssthen
473898184e3Ssthen  local ($/) = "\n";
474898184e3Ssthen  $str = <$in>;
475898184e3Ssthen
476898184e3Ssthen  utf8::upgrade($str)
477898184e3Ssthen      if (${^UNICODE} & PERL_UNICODE_STDIN || defined ${^ENCODING}) &&
478898184e3Ssthen         utf8::valid($str);
479898184e3Ssthen  print $out $rl_term_set[3];
4806fb12b70Safresh1  # bug in 5.000: chomping empty string creates length -1:
481898184e3Ssthen  chomp $str if defined $str;
482898184e3Ssthen
483898184e3Ssthen  $str;
484898184e3Ssthen}
485898184e3Ssthen
486898184e3Ssthen1;
487898184e3Ssthen
488