home *** CD-ROM | disk | FTP | other *** search
/ Acorn User 10 / AU_CD10.iso / Updates / Perl / RPC / !Perl / lib / zip / Term / ReadLine.pm < prev   
Encoding:
Perl POD Document  |  1999-04-02  |  9.8 KB  |  367 lines

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