| 1 | # -*- Perl -*- | ||
| 2 | package Term::ReadLine::Perl5; | ||
| 3 | =encoding utf8 | ||
| 4 | |||
| 5 | =head1 Name | ||
| 6 | |||
| 7 | Term::ReadLine::Perl5 - A Perl5 implementation GNU Readline | ||
| 8 | |||
| 9 | =head2 Overview | ||
| 10 | |||
| 11 | This is a implementation of the GNU Readline/History Library written | ||
| 12 | in Perl5. | ||
| 13 | |||
| 14 | GNU Readline reads lines from an interactive terminal with I<emacs> or | ||
| 15 | I<vi> editing capabilities. It provides as mechanism for saving | ||
| 16 | history of previous input. | ||
| 17 | |||
| 18 | This package typically used in command-line interfaces and REPLs (Read, | ||
| 19 | Eval, Print, Loop). | ||
| 20 | |||
| 21 | =head2 Demo program | ||
| 22 | |||
| 23 | Another package, L<Term::ReadLine::Perl5::Demo> is available to let | ||
| 24 | you run I<Term::ReadLine::Perl5> to experiment with its capabilities | ||
| 25 | and show how to use the API. | ||
| 26 | |||
| 27 | =head1 Synopsis | ||
| 28 | |||
| 29 | use Term::ReadLine::Perl5; | ||
| 30 | $term = Term::ReadLine::Perl5->new('ProgramName'); | ||
| 31 | while ( defined ($_ = $term->readline('prompt>')) ) { | ||
| 32 | ... | ||
| 33 | } | ||
| 34 | |||
| 35 | =cut | ||
| 36 | use warnings; use strict; | ||
| 37 | use Term::ReadLine::Perl5::readline; | ||
| 38 | no warnings 'once'; | ||
| 39 | |||
| 40 | our $VERSION = '1.43'; | ||
| 41 | |||
| 42 | use Carp; | ||
| 43 | eval "use rlib '.' "; # rlib is now optional | ||
| 44 | use Term::ReadLine::Perl5::History; | ||
| 45 | use Term::ReadLine::Perl5::OO; | ||
| 46 | use Term::ReadLine::Perl5::OO::History; | ||
| 47 | use Term::ReadLine::Perl5::Tie; | ||
| 48 | use Term::ReadLine::Perl5::readline; | ||
| 49 | |||
| 50 | if (require Term::ReadLine) { | ||
| 51 | our @ISA = qw(Term::ReadLine::Stub Exporter); | ||
| 52 | } | ||
| 53 | my (%attribs, $term); | ||
| 54 | |||
| 55 | our @EXPORT = qw(IN OUT); | ||
| 56 | |||
| 57 | |||
| 58 | =head2 Variables | ||
| 59 | |||
| 60 | Following GNU Readline/History Library variables can be accessed from | ||
| 61 | Perl program. See 'GNU Readline Library Manual' and ' GNU History | ||
| 62 | Library Manual' for each variable. You can access them via the | ||
| 63 | C<Attribs> method. Names of keys in this hash conform to standard | ||
| 64 | conventions with the leading C<rl_> stripped. | ||
| 65 | |||
| 66 | Example: | ||
| 67 | |||
| 68 | $term = Term::ReadLine::Perl5->new('ReadLineTest'); | ||
| 69 | $attribs = $term->Attribs; | ||
| 70 | $v = $attribs->{history_base}; # history_base | ||
| 71 | |||
| 72 | =head3 Attribute Names | ||
| 73 | |||
| 74 | completion_suppress_append (bool) | ||
| 75 | history_base (int) | ||
| 76 | history_stifled (int) | ||
| 77 | max_input_history (int) | ||
| 78 | outstream (file handle) | ||
| 79 | |||
| 80 | =cut | ||
| 81 | |||
| 82 | my %features = ( | ||
| 83 | appname => 1, # "new" is recognized | ||
| 84 | minline => 1, # we have a working MinLine() | ||
| 85 | autohistory => 1, # lines are put into history automatically, | ||
| 86 | # subject to MinLine() | ||
| 87 | getHistory => 1, # we have a working getHistory() | ||
| 88 | setHistory => 1, # we have a working setHistory() | ||
| 89 | addHistory => 1, # we have a working add_history(), addhistory(), | ||
| 90 | # or addHistory() | ||
| 91 | readHistory => 1, # we have read_history() or readHistory() | ||
| 92 | writeHistory => 1, # we have writeHistory() | ||
| 93 | preput => 1, # the second argument to readline is processed | ||
| 94 | attribs => 1, | ||
| 95 | newTTY => 1, # we have newTTY() | ||
| 96 | stiflehistory => 1, # we have stifle_history() | ||
| 97 | ); | ||
| 98 | |||
| 99 | tie %attribs, 'Term::ReadLine::Perl5::Tie' or die ; | ||
| 100 | sub Attribs { | ||
| 101 | \%attribs; | ||
| 102 | } | ||
| 103 | |||
| 104 | =head1 Subroutine | ||
| 105 | |||
| 106 | =head2 Standard Term::ReadLine Methods | ||
| 107 | |||
| 108 | These methods are standard methods defined by | ||
| 109 | L<Term::ReadLine>. | ||
| 110 | |||
| 111 | =head3 C<ReadLine> | ||
| 112 | |||
| 113 | Readline() -> 'Term::ReadLine::Perl5' | ||
| 114 | |||
| 115 | returns the actual package that executes the commands. If this package | ||
| 116 | is used, the value is C<Term::ReadLine::Perl5>. | ||
| 117 | |||
| 118 | =cut | ||
| 119 | sub ReadLine {'Term::ReadLine::Perl5'} | ||
| 120 | |||
| 121 | |||
| 122 | =head3 readline | ||
| 123 | |||
| 124 | $bool = $term->readline($prompt, $default) | ||
| 125 | |||
| 126 | The main routine to call interactively read lines. Parameter | ||
| 127 | I<$prompt> is the text you want to prompt with If it is empty string, | ||
| 128 | no preceding prompt text is given. It is I<undef> a default value of | ||
| 129 | "INPUT> " is used. | ||
| 130 | |||
| 131 | Parameter I<$default> is the default value; it can be can be | ||
| 132 | omitted. The next input line is returned or I<undef> on EOF. | ||
| 133 | |||
| 134 | =cut | ||
| 135 | |||
| 136 | sub readline { | ||
| 137 | shift; | ||
| 138 | &Term::ReadLine::Perl5::readline::readline(@_); | ||
| 139 | } | ||
| 140 | |||
| 141 | =head3 new | ||
| 142 | |||
| 143 | B<new>(I<$name>,[I<IN>[,I<OUT>]]) | ||
| 144 | |||
| 145 | returns the handle for subsequent calls to following functions. | ||
| 146 | Argument is the name of the application. Optionally can be followed | ||
| 147 | by two arguments for C<IN> and C<OUT> file handles. These arguments | ||
| 148 | should be globs. | ||
| 149 | |||
| 150 | I<$name> is the name of the application. | ||
| 151 | |||
| 152 | This routine may also get called via | ||
| 153 | C<Term::ReadLine-E<gt>new($term_name)> if you have | ||
| 154 | C<$ENV{PERL_RL}> set to 'Perl5'; | ||
| 155 | |||
| 156 | At present, because this code has lots of global state, we currently don't | ||
| 157 | support more than one readline instance. | ||
| 158 | |||
| 159 | =cut | ||
| 160 | sub new { | ||
| 161 | my $class = shift; | ||
| 162 | if (require Term::ReadLine) { | ||
| 163 | $features{tkRunning} = Term::ReadLine::Stub->Features->{'tkRunning'}; | ||
| 164 | $features{ornaments} = Term::ReadLine::Stub->Features->{'ornaments'}; | ||
| 165 | } | ||
| 166 | if (defined $term) { | ||
| 167 | my $stderr = $Term::ReadLine::Perl5::readline::term_OUT; | ||
| 168 | print $stderr "Cannot create second readline interface\n"; | ||
| 169 | print "Using experimental OO interface based on Caroline\n"; | ||
| 170 | my ($name, $in, $out) = @_; | ||
| 171 | my $opts = { | ||
| 172 | name => $name, | ||
| 173 | in => $in, | ||
| 174 | out => $out, | ||
| 175 | }; | ||
| 176 | return Term::ReadLine::Perl5::OO->new($opts); | ||
| 177 | } | ||
| 178 | shift; # Package name | ||
| 179 | if (@_) { | ||
| 180 | if ($term) { | ||
| 181 | warn "Ignoring name of second readline interface.\n" | ||
| 182 | if defined $term; | ||
| 183 | shift; | ||
| 184 | } else { | ||
| 185 | # Set Name | ||
| 186 | $Term::ReadLine::Perl5::readline::rl_readline_name = shift; | ||
| 187 | } | ||
| 188 | } | ||
| 189 | if (!@_) { | ||
| 190 | if (!defined $term) { | ||
| 191 | my ($IN,$OUT) = Term::ReadLine->findConsole(); | ||
| 192 | # Old Term::ReadLine did not have a workaround for a bug | ||
| 193 | # in Win devdriver | ||
| 194 | $IN = 'CONIN$' if $^O eq 'MSWin32' and "\U$IN" eq 'CON'; | ||
| 195 | open(my $in_fh, | ||
| 196 | # A workaround for another bug in Win device driver | ||
| 197 | (($IN eq 'CONIN$' and $^O eq 'MSWin32') ? "+< $IN" : "< $IN")) | ||
| 198 | or croak "Cannot open $IN for read"; | ||
| 199 | open(my $out_fh, ">$OUT") || croak "Cannot open $OUT for write: $!"; | ||
| 200 | $Term::ReadLine::Perl5::readline::term_IN = $in_fh; | ||
| 201 | $Term::ReadLine::Perl5::readline::term_OUT = $out_fh; | ||
| 202 | } | ||
| 203 | } else { | ||
| 204 | if (defined $term and ($term->IN ne $_[0] or $term->OUT ne $_[1]) ) { | ||
| 205 | croak "Request for a second readline interface with different terminal"; | ||
| 206 | } | ||
| 207 | $Term::ReadLine::Perl5::readline::term_IN = shift; | ||
| 208 | $Term::ReadLine::readline::term_OUT = shift | ||
| 209 | } | ||
| 210 | # The following is here since it is mostly used for perl input: | ||
| 211 | # $readline::rl_basic_word_break_characters .= '-:+/*,[])}'; | ||
| 212 | $term = bless [$readline::term_IN,$readline::term_OUT]; | ||
| 213 | my $self = { | ||
| 214 | 'IN' => $readline::term_IN, | ||
| 215 | 'OUT' => $readline::term_OUT, | ||
| 216 | }; | ||
| 217 | bless $self, $class; | ||
| 218 | |||
| 219 | unless ($ENV{PERL_RL} and $ENV{PERL_RL} =~ /\bo\w*=0/) { | ||
| 220 | local $Term::ReadLine::termcap_nowarn = 1; # With newer Perls | ||
| 221 | local $SIG{__WARN__} = sub {}; # With older Perls | ||
| 222 | $term->ornaments(1); | ||
| 223 | } | ||
| 224 | |||
| 225 | # FIXME: something rl_term_set in here causes terminal attributes | ||
| 226 | # like bold and underline to work. | ||
| 227 | Term::ReadLine::Perl5::readline::rl_term_set(); | ||
| 228 | |||
| 229 | return $self; | ||
| 230 | } | ||
| 231 | |||
| 232 | =head3 IN | ||
| 233 | |||
| 234 | B<Term::ReadLine::Perl5-E<gt>IN> | ||
| 235 | |||
| 236 | Returns the input filehandle | ||
| 237 | =cut | ||
| 238 | sub IN { | ||
| 239 | my ($self) = @_; | ||
| 240 | $self->{IN}; | ||
| 241 | } | ||
| 242 | |||
| 243 | =head3 OUT | ||
| 244 | |||
| 245 | B<Term::ReadLine::Perl5-E<gt>OUT> | ||
| 246 | |||
| 247 | Returns the output filehandle | ||
| 248 | =cut | ||
| 249 | sub OUT { | ||
| 250 | my ($self) = @_; | ||
| 251 | $self->{OUT}; | ||
| 252 | } | ||
| 253 | |||
| 254 | |||
| 255 | =head3 newTTY | ||
| 256 | |||
| 257 | B<Term::ReadLine::Perl5-E<gt>newTTY>(I<IN>, I<OUT>) | ||
| 258 | |||
| 259 | takes two arguments which are input filehandle and output filehandle. | ||
| 260 | Switches to use these filehandles. | ||
| 261 | =cut | ||
| 262 | sub newTTY($$$) { | ||
| 263 | my ($self, $in, $out) = @_; | ||
| 264 | $Term::ReadLine::Perl5::readline::term_IN = $self->{'IN'} = $in; | ||
| 265 | $Term::ReadLine::Perl5::readline::term_OUT = $self->{'OUT'} = $out; | ||
| 266 | my $sel = select($out); | ||
| 267 | $| = 1; # for DB::OUT | ||
| 268 | select($sel); | ||
| 269 | } | ||
| 270 | |||
| 271 | =head3 Minline | ||
| 272 | |||
| 273 | B<MinLine>([I<$minlength>])> | ||
| 274 | |||
| 275 | If B<$minlength> is given, set C<$readline::minlength> the minimum | ||
| 276 | length a $line for it to go into the readline history. | ||
| 277 | |||
| 278 | The previous value is returned. | ||
| 279 | =cut | ||
| 280 | sub MinLine($;$) { | ||
| 281 | my $old = $minlength; | ||
| 282 | $minlength = $_[1] if @_ == 2; | ||
| 283 | return $old; | ||
| 284 | } | ||
| 285 | |||
| 286 | #################### History ########################################## | ||
| 287 | |||
| 288 | =head3 add_history | ||
| 289 | |||
| 290 | $term->add_history>($line1, $line2, ...) | ||
| 291 | |||
| 292 | adds the lines, I<$line1>, etc. to the input history list. | ||
| 293 | |||
| 294 | I<AddHistory> is an alias for this function. | ||
| 295 | |||
| 296 | =cut | ||
| 297 | |||
| 298 | # GNU ReadLine names | ||
| 299 | *add_history = \&Term::ReadLine::Perl5::History::add_history; | ||
| 300 | *remove_history = \&Term::ReadLine::Perl5::History::remove_history; | ||
| 301 | *replace_history_entry = \&Term::ReadLine::Perl5::History::replace_history_entry; | ||
| 302 | |||
| 303 | *clear_history = \&Term::ReadLine::Perl5::History::clear_history; | ||
| 304 | |||
| 305 | *history_is_stifled = \&Term::ReadLine::Perl5::History::history_is_stifled; | ||
| 306 | *read_history = \&Term::ReadLine::Perl5::History::read_history; | ||
| 307 | *unstifle_history = \&Term::ReadLine::Perl5::History::unstifle_history; | ||
| 308 | *write_history = \&Term::ReadLine::Perl5::History::write_history; | ||
| 309 | |||
| 310 | # Not sure about the difference between history_list and GetHistory. | ||
| 311 | *history_list = \&Term::ReadLine::Perl5::OO::GetHistory; | ||
| 312 | |||
| 313 | *rl_History = *Term::ReadLine::Perl5::rl_History; | ||
| 314 | |||
| 315 | |||
| 316 | # Some Term::ReadLine::Gnu names | ||
| 317 | *AddHistory = \&add_history; | ||
| 318 | *GetHistory = \&Term::ReadLine::Perl5::History::GetHistory; | ||
| 319 | *SetHistory = \&Term::ReadLine::Perl5::History::SetHistory; | ||
| 320 | *ReadHistory = \&Term::ReadLine::Perl5::History::ReadHistory; | ||
| 321 | *WriteHistory = \&Term::ReadLine::Perl5::History::WriteHistory; | ||
| 322 | |||
| 323 | # Backward compatibility: | ||
| 324 | *addhistory = \&add_history; | ||
| 325 | *StifleHistory = \&stifle_history; | ||
| 326 | |||
| 327 | =head3 stifle_history | ||
| 328 | |||
| 329 | $term->stifle_history($max) | ||
| 330 | |||
| 331 | Stifle or put a cap on the history list, remembering only C<$max> | ||
| 332 | number of lines. | ||
| 333 | |||
| 334 | I<StifleHistory> is an alias for this function. | ||
| 335 | |||
| 336 | =cut | ||
| 337 | ### FIXME: stifle_history is still here because it updates $attribs. | ||
| 338 | ## Pass a reference? | ||
| 339 | sub stifle_history($$) { | ||
| 340 | my ($self, $max) = @_; | ||
| 341 | $max = 0 if !defined($max) || $max < 0; | ||
| 342 | |||
| 343 | if (scalar @rl_History > $max) { | ||
| 344 | splice @rl_History, $max; | ||
| 345 | $attribs{history_length} = scalar @rl_History; | ||
| 346 | } | ||
| 347 | |||
| 348 | $Term::ReadLine::Perl5::History::history_stifled = 1; | ||
| 349 | $attribs{max_input_history} = $self->{rl_max_input_history} = $max; | ||
| 350 | } | ||
| 351 | |||
| 352 | =head3 Features | ||
| 353 | |||
| 354 | B<Features()> | ||
| 355 | |||
| 356 | Returns a reference to a hash with keys being features present in | ||
| 357 | current implementation. Several optional features are used in the | ||
| 358 | minimal interface: | ||
| 359 | |||
| 360 | =over | ||
| 361 | |||
| 362 | =item * | ||
| 363 | I<addHistory> is present if you can add lines to history list via | ||
| 364 | the I<addHistory()> method | ||
| 365 | |||
| 366 | =item * | ||
| 367 | I<appname> is be present if a name, the first argument | ||
| 368 | to I<new()> was given | ||
| 369 | |||
| 370 | =item * | ||
| 371 | I<autohistory> is present if lines are put into history automatically | ||
| 372 | subject to the line being longer than I<MinLine>. | ||
| 373 | |||
| 374 | =item * | ||
| 375 | I<getHistory> is present if we get retrieve history via the I<getHistory()> | ||
| 376 | method | ||
| 377 | |||
| 378 | =item * | ||
| 379 | I<minline> is present if the I<MinLine> method available. | ||
| 380 | |||
| 381 | =item * | ||
| 382 | I<preput> is present if the second argument to I<readline> method can | ||
| 383 | append text to the input to be read subsequently | ||
| 384 | |||
| 385 | =item * | ||
| 386 | I<readHistory> is present you can read history | ||
| 387 | items previosly saved in a file. | ||
| 388 | |||
| 389 | =item * | ||
| 390 | I<setHistory> is present if we can set history | ||
| 391 | |||
| 392 | =item * | ||
| 393 | I<stifleHistory> is present you can put a limit of the nubmer of history | ||
| 394 | items to save via the I<writeHistory()> method | ||
| 395 | |||
| 396 | =item * | ||
| 397 | I<tkRunning> is present if a Tk application may run while I<ReadLine> is | ||
| 398 | getting input. | ||
| 399 | |||
| 400 | =item * | ||
| 401 | I<writeHistory> is present you can save history to a file via the | ||
| 402 | I<writeHistory()> method | ||
| 403 | |||
| 404 | =back | ||
| 405 | |||
| 406 | =cut | ||
| 407 | |||
| 408 | sub Features { \%features; } | ||
| 409 | |||
| 410 | =head1 See also | ||
| 411 | |||
| 412 | =over | ||
| 413 | |||
| 414 | =item * | ||
| 415 | |||
| 416 | L<Term::ReadLine::Perl5::OO> is the newer but unfinished fully OO version. | ||
| 417 | |||
| 418 | =item * | ||
| 419 | |||
| 420 | L<Term::ReadLine::Perl5> is the first try at the OO package that most | ||
| 421 | programmers will use. | ||
| 422 | |||
| 423 | =item * | ||
| 424 | |||
| 425 | L<Term::ReadLine::Perl5::readline-guide> is guide to the guts of the | ||
| 426 | non-OO portion of L<Term::ReadLine::Perl5> | ||
| 427 | |||
| 428 | =item * | ||
| 429 | |||
| 430 | L<Term::ReadLine::Perl5::History> describes the history | ||
| 431 | mechanism | ||
| 432 | |||
| 433 | =item * | ||
| 434 | |||
| 435 | L<Term::ReadLine> is a generic package which can be used to | ||
| 436 | select this among other compatible GNU Readline packages. | ||
| 437 | |||
| 438 | =back | ||
| 439 | |||
| 440 | =cut | ||
| 441 | 1; |