Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

369 lines
10 KiB

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