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.

120 lines
3.2 KiB

  1. ;#
  2. #
  3. # This library is no longer being maintained, and is included for backward
  4. # compatibility with Perl 4 programs which may require it.
  5. #
  6. # In particular, this should not be used as an example of modern Perl
  7. # programming techniques.
  8. #
  9. # Suggested alternative: Term::Complete
  10. #
  11. ;# @(#)complete.pl,v1.1 ([email protected]) 09/23/91
  12. ;#
  13. ;# Author: Wayne Thompson
  14. ;#
  15. ;# Description:
  16. ;# This routine provides word completion.
  17. ;# (TAB) attempts word completion.
  18. ;# (^D) prints completion list.
  19. ;# (These may be changed by setting $Complete'complete, etc.)
  20. ;#
  21. ;# Diagnostics:
  22. ;# Bell when word completion fails.
  23. ;#
  24. ;# Dependencies:
  25. ;# The tty driver is put into raw mode.
  26. ;#
  27. ;# Bugs:
  28. ;#
  29. ;# Usage:
  30. ;# $input = &Complete('prompt_string', *completion_list);
  31. ;# or
  32. ;# $input = &Complete('prompt_string', @completion_list);
  33. ;#
  34. CONFIG: {
  35. package Complete;
  36. $complete = "\004";
  37. $kill = "\025";
  38. $erase1 = "\177";
  39. $erase2 = "\010";
  40. }
  41. sub Complete {
  42. package Complete;
  43. local($prompt, @cmp_list, $return, @match, $l, $test, $cmp, $r);
  44. if ($_[1] =~ /^StB\0/) {
  45. ($prompt, *_) = @_;
  46. }
  47. else {
  48. $prompt = shift(@_);
  49. }
  50. @cmp_lst = sort(@_);
  51. system('stty raw -echo');
  52. LOOP: {
  53. print($prompt, $return);
  54. while (($_ = getc(STDIN)) ne "\r") {
  55. CASE: {
  56. # (TAB) attempt completion
  57. $_ eq "\t" && do {
  58. @match = grep(/^$return/, @cmp_lst);
  59. $l = length($test = shift(@match));
  60. unless ($#match < 0) {
  61. foreach $cmp (@match) {
  62. until (substr($cmp, 0, $l) eq substr($test, 0, $l)) {
  63. $l--;
  64. }
  65. }
  66. print("\a");
  67. }
  68. print($test = substr($test, $r, $l - $r));
  69. $r = length($return .= $test);
  70. last CASE;
  71. };
  72. # (^D) completion list
  73. $_ eq $complete && do {
  74. print(join("\r\n", '', grep(/^$return/, @cmp_lst)), "\r\n");
  75. redo LOOP;
  76. };
  77. # (^U) kill
  78. $_ eq $kill && do {
  79. if ($r) {
  80. undef $r;
  81. undef $return;
  82. print("\r\n");
  83. redo LOOP;
  84. }
  85. last CASE;
  86. };
  87. # (DEL) || (BS) erase
  88. ($_ eq $erase1 || $_ eq $erase2) && do {
  89. if($r) {
  90. print("\b \b");
  91. chop($return);
  92. $r--;
  93. }
  94. last CASE;
  95. };
  96. # printable char
  97. ord >= 32 && do {
  98. $return .= $_;
  99. $r++;
  100. print;
  101. last CASE;
  102. };
  103. }
  104. }
  105. }
  106. system('stty -raw echo');
  107. print("\n");
  108. $return;
  109. }
  110. 1;