Term::Readline patch for AmigaOS
[p5sagit/p5-mst-13.2.git] / lib / Term / ReadLine.pm
1 =head1 NAME
2
3 Term::ReadLine - Perl interface to various C<readline> packages. If
4 no real package is found, substitutes stubs instead of basic functions.
5
6 =head1 SYNOPSIS
7
8   use Term::ReadLine;
9   $term = new Term::ReadLine 'Simple Perl calc';
10   $prompt = "Enter your arithmetic expression: ";
11   $OUT = $term->OUT || STDOUT;
12   while ( defined ($_ = $term->readline($prompt)) ) {
13     $res = eval($_), "\n";
14     warn $@ if $@;
15     print $OUT $res, "\n" unless $@;
16     $term->addhistory($_) if /\S/;
17   }
18
19 =head1 DESCRIPTION
20
21 This package is just a front end to some other packages. At the moment
22 this description is written, the only such package is Term-ReadLine,
23 available on CPAN near you. The real target of this stub package is to
24 set up a common interface to whatever Readline emerges with time.
25
26 =head1 Minimal set of supported functions
27
28 All the supported functions should be called as methods, i.e., either as 
29
30   $term = new Term::ReadLine 'name';
31
32 or as 
33
34   $term->addhistory('row');
35
36 where $term is a return value of Term::ReadLine-E<gt>Init.
37
38 =over 12
39
40 =item C<ReadLine>
41
42 returns the actual package that executes the commands. Among possible
43 values are C<Term::ReadLine::Gnu>, C<Term::ReadLine::Perl>,
44 C<Term::ReadLine::Stub Exporter>.
45
46 =item C<new>
47
48 returns the handle for subsequent calls to following
49 functions. Argument is the name of the application. Optionally can be
50 followed by two arguments for C<IN> and C<OUT> filehandles. These
51 arguments should be globs.
52
53 =item C<readline>
54
55 gets an input line, I<possibly> with actual C<readline>
56 support. Trailing newline is removed. Returns C<undef> on C<EOF>.
57
58 =item C<addhistory>
59
60 adds the line to the history of input, from where it can be used if
61 the actual C<readline> is present.
62
63 =item C<IN>, $C<OUT>
64
65 return the filehandles for input and output or C<undef> if C<readline>
66 input and output cannot be used for Perl.
67
68 =item C<MinLine>
69
70 If argument is specified, it is an advice on minimal size of line to
71 be included into history.  C<undef> means do not include anything into
72 history. Returns the old value.
73
74 =item C<findConsole>
75
76 returns an array with two strings that give most appropriate names for
77 files for input and output using conventions C<"E<lt>$in">, C<"E<gt>out">.
78
79 =item Attribs
80
81 returns a reference to a hash which describes internal configuration
82 of the package. Names of keys in this hash conform to standard
83 conventions with the leading C<rl_> stripped.
84
85 =item C<Features>
86
87 Returns a reference to a hash with keys being features present in
88 current implementation. Several optional features are used in the
89 minimal interface: C<appname> should be present if the first argument
90 to C<new> is recognized, and C<minline> should be present if
91 C<MinLine> method is not dummy.  C<autohistory> should be present if
92 lines are put into history automatically (maybe subject to
93 C<MinLine>), and C<addhistory> if C<addhistory> method is not dummy.
94
95 If C<Features> method reports a feature C<attribs> as present, the
96 method C<Attribs> is not dummy.
97
98 =back
99
100 =head1 Additional supported functions
101
102 Actually C<Term::ReadLine> can use some other package, that will
103 support reacher set of commands.
104
105 All these commands are callable via method interface and have names
106 which conform to standard conventions with the leading C<rl_> stripped.
107
108 =head1 EXPORTS
109
110 None
111
112 =head1 ENVIRONMENT
113
114 The variable C<PERL_RL> governs which ReadLine clone is loaded. If the
115 value is false, a dummy interface is used. If the value is true, it
116 should be tail of the name of the package to use, such as C<Perl> or
117 C<Gnu>. 
118
119 If the variable is not set, the best available package is loaded.
120
121 =cut
122
123 package Term::ReadLine::Stub;
124 @ISA = 'Term::ReadLine::Tk';
125
126 $DB::emacs = $DB::emacs;        # To peacify -w
127
128 sub ReadLine {'Term::ReadLine::Stub'}
129 sub readline {
130   my $self = shift;
131   my ($in,$out,$str) = @$self;
132   print $out shift; 
133   $self->register_Tk 
134      if not $Term::ReadLine::registered and $Term::ReadLine::toloop
135         and defined &Tk::DoOneEvent;
136   #$str = scalar <$in>;
137   $str = $self->get_line;
138   # bug in 5.000: chomping empty string creats length -1:
139   chomp $str if defined $str;
140   $str;
141 }
142 sub addhistory {}
143
144 sub findConsole {
145     my $console;
146
147     if (-e "/dev/tty") {
148         $console = "/dev/tty";
149     } elsif (-e "con") {
150         $console = "con";
151     } else {
152         $console = "sys\$command";
153     }
154
155     if (defined $ENV{'OS2_SHELL'}) { # In OS/2
156       if ($DB::emacs) {
157         $console = undef;
158       } else {
159         $console = "/dev/con";
160       }
161     }
162
163     if ($^O eq 'amigaos') {
164         $console = undef;
165     }
166
167     $consoleOUT = $console;
168     $console = "&STDIN" unless defined $console;
169     if (!defined $consoleOUT) {
170       $consoleOUT = defined fileno(STDERR) ? "&STDERR" : "&STDOUT";
171     }
172     ($console,$consoleOUT);
173 }
174
175 sub new {
176   die "method new called with wrong number of arguments" 
177     unless @_==2 or @_==4;
178   #local (*FIN, *FOUT);
179   my ($FIN, $FOUT);
180   if (@_==2) {
181     ($console, $consoleOUT) = findConsole;
182
183     open(FIN, "<$console"); 
184     open(FOUT,">$consoleOUT");
185     #OUT->autoflush(1);         # Conflicts with debugger?
186     $sel = select(FOUT);
187     $| = 1;                             # for DB::OUT
188     select($sel);
189     bless [\*FIN, \*FOUT];
190   } else {                      # Filehandles supplied
191     $FIN = $_[2]; $FOUT = $_[3];
192     #OUT->autoflush(1);         # Conflicts with debugger?
193     $sel = select($FOUT);
194     $| = 1;                             # for DB::OUT
195     select($sel);
196     bless [$FIN, $FOUT];
197   }
198 }
199 sub IN { shift->[0] }
200 sub OUT { shift->[1] }
201 sub MinLine { undef }
202 sub Attribs { {} }
203
204 my %features = (tkRunning => 1);
205 sub Features { \%features }
206
207 package Term::ReadLine;         # So late to allow the above code be defined?
208
209 my $which = $ENV{PERL_RL};
210 if ($which) {
211   if ($which =~ /\bgnu\b/i){
212     eval "use Term::ReadLine::Gnu;";
213   } elsif ($which =~ /\bperl\b/i) {
214     eval "use Term::ReadLine::Perl;";
215   } else {
216     eval "use Term::ReadLine::$which;";
217   }
218 } elsif (defined $which) {      # Defined but false
219   # Do nothing fancy
220 } else {
221   eval "use Term::ReadLine::Gnu; 1" or eval "use Term::ReadLine::Perl; 1";
222 }
223
224 #require FileHandle;
225
226 # To make possible switch off RL in debugger: (Not needed, work done
227 # in debugger).
228
229 if (defined &Term::ReadLine::Gnu::readline) {
230   @ISA = qw(Term::ReadLine::Gnu Term::ReadLine::Stub);
231 } elsif (defined &Term::ReadLine::Perl::readline) {
232   @ISA = qw(Term::ReadLine::Perl Term::ReadLine::Stub);
233 } else {
234   @ISA = qw(Term::ReadLine::Stub);
235 }
236
237 package Term::ReadLine::Tk;
238
239 $count_handle = $count_DoOne = $count_loop = 0;
240
241 sub handle {$giveup = 1; $count_handle++}
242
243 sub Tk_loop {
244   # Tk->tkwait('variable',\$giveup);    # needs Widget
245   $count_DoOne++, Tk::DoOneEvent(0) until $giveup;
246   $count_loop++;
247   $giveup = 0;
248 }
249
250 sub register_Tk {
251   my $self = shift;
252   $Term::ReadLine::registered++ 
253     or Tk->fileevent($self->IN,'readable',\&handle);
254 }
255
256 sub tkRunning {
257   $Term::ReadLine::toloop = $_[1] if @_ > 1;
258   $Term::ReadLine::toloop;
259 }
260
261 sub get_c {
262   my $self = shift;
263   $self->Tk_loop if $Term::ReadLine::toloop && defined &Tk::DoOneEvent;
264   return getc $self->IN;
265 }
266
267 sub get_line {
268   my $self = shift;
269   $self->Tk_loop if $Term::ReadLine::toloop && defined &Tk::DoOneEvent;
270   my $in = $self->IN;
271   return scalar <$in>;
272 }
273
274 1;
275