home *** CD-ROM | disk | FTP | other *** search
/ CLIX - Fazer Clix Custa Nix / CLIX-CD.cdr / mac / lib / Term / ReadLine.pm < prev   
Text File  |  1997-05-19  |  9KB  |  320 lines

  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. The stub package included with the perl distribution allows two
  109. additional methods: C<tkRunning> and C<ornaments>.  The first one
  110. makes Tk event loop run when waiting for user input (i.e., during
  111. C<readline> method), the second one makes the command line stand out
  112. by using termcap data.  The argument to C<ornaments> should be 0, 1,
  113. or a string of a form "aa,bb,cc,dd".  Four components of this string
  114. should be names of I<terminal capacities>, first two will be issued to
  115. make the prompt standout, last two to make the input line standout.
  116.  
  117. =head1 EXPORTS
  118.  
  119. None
  120.  
  121. =head1 ENVIRONMENT
  122.  
  123. The variable C<PERL_RL> governs which ReadLine clone is loaded. If the
  124. value is false, a dummy interface is used. If the value is true, it
  125. should be tail of the name of the package to use, such as C<Perl> or
  126. C<Gnu>. 
  127.  
  128. If the variable is not set, the best available package is loaded.
  129.  
  130. =cut
  131.  
  132. package Term::ReadLine::Stub;
  133. @ISA = qw'Term::ReadLine::Tk Term::ReadLine::TermCap';
  134.  
  135. $DB::emacs = $DB::emacs;    # To peacify -w
  136. *rl_term_set = \@Term::ReadLine::TermCap::rl_term_set;
  137.  
  138. sub ReadLine {'Term::ReadLine::Stub'}
  139. sub readline {
  140.   my $self = shift;
  141.   my ($in,$out,$str) = @$self;
  142.   # Modified for MacOS to chop off leading prompts
  143.   my ($prompt) = shift;
  144.   print $out $rl_term_set[0], $prompt, $rl_term_set[1], $rl_term_set[2]; 
  145.   $self->register_Tk 
  146.      if not $Term::ReadLine::registered and $Term::ReadLine::toloop
  147.     and defined &Tk::DoOneEvent;
  148.   #$str = scalar <$in>;
  149.   $str = $self->get_line;
  150.   $str =~ s/^\s*\Q$prompt\E// if ($^O eq 'MacOS');
  151.   print $out $rl_term_set[3]; 
  152.   # bug in 5.000: chomping empty string creats length -1:
  153.   chomp $str if defined $str;
  154.   $str;
  155. }
  156. sub addhistory {}
  157.  
  158. sub findConsole {
  159.     my $console;
  160.  
  161.     if ($^O eq 'MacOS') {
  162.         $console = "Dev:Console";
  163.     } elsif (-e "/dev/tty") {
  164.     $console = "/dev/tty";
  165.     } elsif (-e "con") {
  166.     $console = "con";
  167.     } else {
  168.     $console = "sys\$command";
  169.     }
  170.  
  171.     if ($^O eq 'amigaos') {
  172.     $console = undef;
  173.     }
  174.     elsif ($^O eq 'os2') {
  175.       if ($DB::emacs) {
  176.     $console = undef;
  177.       } else {
  178.     $console = "/dev/con";
  179.       }
  180.     }
  181.  
  182.     $consoleOUT = $console;
  183.     $console = "&STDIN" unless defined $console;
  184.     if (!defined $consoleOUT) {
  185.       $consoleOUT = defined fileno(STDERR) ? "&STDERR" : "&STDOUT";
  186.     }
  187.     ($console,$consoleOUT);
  188. }
  189.  
  190. sub new {
  191.   die "method new called with wrong number of arguments" 
  192.     unless @_==2 or @_==4;
  193.   #local (*FIN, *FOUT);
  194.   my ($FIN, $FOUT);
  195.   if (@_==2) {
  196.     ($console, $consoleOUT) = findConsole;
  197.  
  198.     open(FIN, "<$console"); 
  199.     open(FOUT,">$consoleOUT");
  200.     #OUT->autoflush(1);        # Conflicts with debugger?
  201.     $sel = select(FOUT);
  202.     $| = 1;                # for DB::OUT
  203.     select($sel);
  204.     bless [\*FIN, \*FOUT];
  205.   } else {            # Filehandles supplied
  206.     $FIN = $_[2]; $FOUT = $_[3];
  207.     #OUT->autoflush(1);        # Conflicts with debugger?
  208.     $sel = select($FOUT);
  209.     $| = 1;                # for DB::OUT
  210.     select($sel);
  211.     bless [$FIN, $FOUT];
  212.   }
  213. }
  214. sub IN { shift->[0] }
  215. sub OUT { shift->[1] }
  216. sub MinLine { undef }
  217. sub Attribs { {} }
  218.  
  219. my %features = (tkRunning => 1, ornaments => 1);
  220. sub Features { \%features }
  221.  
  222. package Term::ReadLine;        # So late to allow the above code be defined?
  223.  
  224. my $which = $ENV{PERL_RL};
  225. if ($which) {
  226.   if ($which =~ /\bgnu\b/i){
  227.     eval "use Term::ReadLine::Gnu;";
  228.   } elsif ($which =~ /\bperl\b/i) {
  229.     eval "use Term::ReadLine::Perl;";
  230.   } else {
  231.     eval "use Term::ReadLine::$which;";
  232.   }
  233. } elsif (defined $which) {    # Defined but false
  234.   # Do nothing fancy
  235. } else {
  236.   eval "use Term::ReadLine::Gnu; 1" or eval "use Term::ReadLine::Perl; 1";
  237. }
  238.  
  239. #require FileHandle;
  240.  
  241. # To make possible switch off RL in debugger: (Not needed, work done
  242. # in debugger).
  243.  
  244. if (defined &Term::ReadLine::Gnu::readline) {
  245.   @ISA = qw(Term::ReadLine::Gnu Term::ReadLine::Stub);
  246. } elsif (defined &Term::ReadLine::Perl::readline) {
  247.   @ISA = qw(Term::ReadLine::Perl Term::ReadLine::Stub);
  248. } else {
  249.   @ISA = qw(Term::ReadLine::Stub);
  250. }
  251.  
  252. package Term::ReadLine::TermCap;
  253.  
  254. # Prompt-start, prompt-end, command-line-start, command-line-end
  255. #     -- zero-width beautifies to emit around prompt and the command line.
  256. @rl_term_set = ("","","","");
  257. # string encoded:
  258. $rl_term_set = ',,,';
  259.  
  260. sub LoadTermCap {
  261.   return if defined $terminal;
  262.   
  263.   require Term::Cap;
  264.   $terminal = Tgetent Term::Cap ({OSPEED => 9600}); # Avoid warning.
  265. }
  266.  
  267. sub ornaments {
  268.   shift;
  269.   return $rl_term_set unless @_;
  270.   $rl_term_set = shift;
  271.   $rl_term_set ||= ',,,';
  272.   $rl_term_set = 'us,ue,md,me' if $rl_term_set == 1;
  273.   my @ts = split /,/, $rl_term_set, 4;
  274.   eval { LoadTermCap };
  275.   warn("Cannot find termcap: $@\n"), return unless defined $terminal;
  276.   @rl_term_set = map {$_ ? $terminal->Tputs($_,1) || '' : ''} @ts;
  277.   return $rl_term_set;
  278. }
  279.  
  280.  
  281. package Term::ReadLine::Tk;
  282.  
  283. $count_handle = $count_DoOne = $count_loop = 0;
  284.  
  285. sub handle {$giveup = 1; $count_handle++}
  286.  
  287. sub Tk_loop {
  288.   # Tk->tkwait('variable',\$giveup);    # needs Widget
  289.   $count_DoOne++, Tk::DoOneEvent(0) until $giveup;
  290.   $count_loop++;
  291.   $giveup = 0;
  292. }
  293.  
  294. sub register_Tk {
  295.   my $self = shift;
  296.   $Term::ReadLine::registered++ 
  297.     or Tk->fileevent($self->IN,'readable',\&handle);
  298. }
  299.  
  300. sub tkRunning {
  301.   $Term::ReadLine::toloop = $_[1] if @_ > 1;
  302.   $Term::ReadLine::toloop;
  303. }
  304.  
  305. sub get_c {
  306.   my $self = shift;
  307.   $self->Tk_loop if $Term::ReadLine::toloop && defined &Tk::DoOneEvent;
  308.   return getc $self->IN;
  309. }
  310.  
  311. sub get_line {
  312.   my $self = shift;
  313.   $self->Tk_loop if $Term::ReadLine::toloop && defined &Tk::DoOneEvent;
  314.   my $in = $self->IN;
  315.   return scalar <$in>;
  316. }
  317.  
  318. 1;
  319.  
  320.