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.

154 lines
3.6 KiB

  1. package Term::Complete;
  2. require 5.000;
  3. require Exporter;
  4. @ISA = qw(Exporter);
  5. @EXPORT = qw(Complete);
  6. # @(#)complete.pl,v1.2 ([email protected]) 09/23/91
  7. =head1 NAME
  8. Term::Complete - Perl word completion module
  9. =head1 SYNOPSIS
  10. $input = Complete('prompt_string', \@completion_list);
  11. $input = Complete('prompt_string', @completion_list);
  12. =head1 DESCRIPTION
  13. This routine provides word completion on the list of words in
  14. the array (or array ref).
  15. The tty driver is put into raw mode using the system command
  16. C<stty raw -echo> and restored using C<stty -raw echo>.
  17. The following command characters are defined:
  18. =over 4
  19. =item E<lt>tabE<gt>
  20. Attempts word completion.
  21. Cannot be changed.
  22. =item ^D
  23. Prints completion list.
  24. Defined by I<$Term::Complete::complete>.
  25. =item ^U
  26. Erases the current input.
  27. Defined by I<$Term::Complete::kill>.
  28. =item E<lt>delE<gt>, E<lt>bsE<gt>
  29. Erases one character.
  30. Defined by I<$Term::Complete::erase1> and I<$Term::Complete::erase2>.
  31. =back
  32. =head1 DIAGNOSTICS
  33. Bell sounds when word completion fails.
  34. =head1 BUGS
  35. The completion character E<lt>tabE<gt> cannot be changed.
  36. =head1 AUTHOR
  37. Wayne Thompson
  38. =cut
  39. CONFIG: {
  40. $complete = "\004";
  41. $kill = "\025";
  42. $erase1 = "\177";
  43. $erase2 = "\010";
  44. }
  45. sub Complete {
  46. my($prompt, @cmp_list, $cmp, $test, $l, @match);
  47. my ($return, $r) = ("", 0);
  48. $return = "";
  49. $r = 0;
  50. $prompt = shift;
  51. if (ref $_[0] || $_[0] =~ /^\*/) {
  52. @cmp_lst = sort @{$_[0]};
  53. }
  54. else {
  55. @cmp_lst = sort(@_);
  56. }
  57. system('stty raw -echo');
  58. LOOP: {
  59. print($prompt, $return);
  60. while (($_ = getc(STDIN)) ne "\r") {
  61. CASE: {
  62. # (TAB) attempt completion
  63. $_ eq "\t" && do {
  64. @match = grep(/^$return/, @cmp_lst);
  65. unless ($#match < 0) {
  66. $l = length($test = shift(@match));
  67. foreach $cmp (@match) {
  68. until (substr($cmp, 0, $l) eq substr($test, 0, $l)) {
  69. $l--;
  70. }
  71. }
  72. print("\a");
  73. print($test = substr($test, $r, $l - $r));
  74. $r = length($return .= $test);
  75. }
  76. last CASE;
  77. };
  78. # (^D) completion list
  79. $_ eq $complete && do {
  80. print(join("\r\n", '', grep(/^$return/, @cmp_lst)), "\r\n");
  81. redo LOOP;
  82. };
  83. # (^U) kill
  84. $_ eq $kill && do {
  85. if ($r) {
  86. $r = 0;
  87. $return = "";
  88. print("\r\n");
  89. redo LOOP;
  90. }
  91. last CASE;
  92. };
  93. # (DEL) || (BS) erase
  94. ($_ eq $erase1 || $_ eq $erase2) && do {
  95. if($r) {
  96. print("\b \b");
  97. chop($return);
  98. $r--;
  99. }
  100. last CASE;
  101. };
  102. # printable char
  103. ord >= 32 && do {
  104. $return .= $_;
  105. $r++;
  106. print;
  107. last CASE;
  108. };
  109. }
  110. }
  111. }
  112. system('stty -raw echo');
  113. print("\n");
  114. $return;
  115. }
  116. 1;