Source code of Windows XP (NT5)
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.

111 lines
2.8 KiB

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