line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Term::Complete; |
2
|
|
|
|
|
|
|
require 5.000; |
3
|
|
|
|
|
|
|
require Exporter; |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
1039
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
1239
|
|
6
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
7
|
|
|
|
|
|
|
our @EXPORT = qw(Complete); |
8
|
|
|
|
|
|
|
our $VERSION = '1.402'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
# @(#)complete.pl,v1.2 (me@anywhere.EBay.Sun.COM) 09/23/91 |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 NAME |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
Term::Complete - Perl word completion module |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 SYNOPSIS |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
$input = Complete('prompt_string', \@completion_list); |
19
|
|
|
|
|
|
|
$input = Complete('prompt_string', @completion_list); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=head1 DESCRIPTION |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
This routine provides word completion on the list of words in |
24
|
|
|
|
|
|
|
the array (or array ref). |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
The tty driver is put into raw mode and restored using an operating |
27
|
|
|
|
|
|
|
system specific command, in UNIX-like environments C. |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
The following command characters are defined: |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=over 4 |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=item EtabE |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
Attempts word completion. |
36
|
|
|
|
|
|
|
Cannot be changed. |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=item ^D |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
Prints completion list. |
41
|
|
|
|
|
|
|
Defined by I<$Term::Complete::complete>. |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=item ^U |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
Erases the current input. |
46
|
|
|
|
|
|
|
Defined by I<$Term::Complete::kill>. |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=item EdelE, EbsE |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
Erases one character. |
51
|
|
|
|
|
|
|
Defined by I<$Term::Complete::erase1> and I<$Term::Complete::erase2>. |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=back |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=head1 DIAGNOSTICS |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
Bell sounds when word completion fails. |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=head1 BUGS |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
The completion character EtabE cannot be changed. |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=head1 AUTHOR |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
Wayne Thompson |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=cut |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
our($complete, $kill, $erase1, $erase2, $tty_raw_noecho, $tty_restore, $stty, $tty_safe_restore); |
70
|
|
|
|
|
|
|
our($tty_saved_state) = ''; |
71
|
|
|
|
|
|
|
CONFIG: { |
72
|
|
|
|
|
|
|
$complete = "\004"; |
73
|
|
|
|
|
|
|
$kill = "\025"; |
74
|
|
|
|
|
|
|
$erase1 = "\177"; |
75
|
|
|
|
|
|
|
$erase2 = "\010"; |
76
|
|
|
|
|
|
|
foreach my $s (qw(/bin/stty /usr/bin/stty)) { |
77
|
|
|
|
|
|
|
if (-x $s) { |
78
|
|
|
|
|
|
|
$tty_raw_noecho = "$s raw -echo"; |
79
|
|
|
|
|
|
|
$tty_restore = "$s -raw echo"; |
80
|
|
|
|
|
|
|
$tty_safe_restore = $tty_restore; |
81
|
|
|
|
|
|
|
$stty = $s; |
82
|
|
|
|
|
|
|
last; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub Complete { |
88
|
4
|
|
|
4
|
0
|
4190
|
my($prompt, @cmp_lst, $cmp, $test, $l, @match); |
89
|
4
|
|
|
|
|
8
|
my ($return, $r) = ("", 0); |
90
|
|
|
|
|
|
|
|
91
|
4
|
|
|
|
|
6
|
$return = ""; |
92
|
4
|
|
|
|
|
5
|
$r = 0; |
93
|
|
|
|
|
|
|
|
94
|
4
|
|
|
|
|
5
|
$prompt = shift; |
95
|
4
|
100
|
66
|
|
|
25
|
if (ref $_[0] || $_[0] =~ /^\*/) { |
96
|
1
|
|
|
|
|
3
|
@cmp_lst = sort @{$_[0]}; |
|
1
|
|
|
|
|
9
|
|
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
else { |
99
|
3
|
|
|
|
|
13
|
@cmp_lst = sort(@_); |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# Attempt to save the current stty state, to be restored later |
103
|
4
|
0
|
33
|
|
|
15
|
if (defined $stty && defined $tty_saved_state && $tty_saved_state eq '') { |
|
|
|
33
|
|
|
|
|
104
|
0
|
|
|
|
|
0
|
$tty_saved_state = qx($stty -g 2>/dev/null); |
105
|
0
|
0
|
|
|
|
0
|
if ($?) { |
106
|
|
|
|
|
|
|
# stty -g not supported |
107
|
0
|
|
|
|
|
0
|
$tty_saved_state = undef; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
else { |
110
|
0
|
|
|
|
|
0
|
$tty_saved_state =~ s/\s+$//g; |
111
|
0
|
|
|
|
|
0
|
$tty_restore = qq($stty "$tty_saved_state" 2>/dev/null); |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
} |
114
|
4
|
50
|
|
|
|
12
|
system $tty_raw_noecho if defined $tty_raw_noecho; |
115
|
12
|
|
|
|
|
15
|
LOOP: { |
116
|
4
|
|
|
|
|
5
|
local $_; |
117
|
12
|
|
|
|
|
33
|
print($prompt, $return); |
118
|
12
|
|
|
|
|
90
|
while (($_ = getc(STDIN)) ne "\r") { |
119
|
|
|
|
|
|
|
CASE: { |
120
|
|
|
|
|
|
|
# (TAB) attempt completion |
121
|
26
|
100
|
|
|
|
170
|
$_ eq "\t" && do { |
|
26
|
|
|
|
|
51
|
|
122
|
1
|
|
|
|
|
22
|
@match = grep(/^\Q$return/, @cmp_lst); |
123
|
1
|
50
|
|
|
|
4
|
unless ($#match < 0) { |
124
|
1
|
|
|
|
|
2
|
$l = length($test = shift(@match)); |
125
|
1
|
|
|
|
|
2
|
foreach $cmp (@match) { |
126
|
2
|
|
|
|
|
9
|
until (substr($cmp, 0, $l) eq substr($test, 0, $l)) { |
127
|
5
|
|
|
|
|
15
|
$l--; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
} |
130
|
1
|
|
|
|
|
3
|
print("\a"); |
131
|
1
|
|
|
|
|
8
|
print($test = substr($test, $r, $l - $r)); |
132
|
1
|
|
|
|
|
5
|
$r = length($return .= $test); |
133
|
|
|
|
|
|
|
} |
134
|
1
|
|
|
|
|
4
|
last CASE; |
135
|
|
|
|
|
|
|
}; |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
# (^D) completion list |
138
|
25
|
100
|
|
|
|
47
|
$_ eq $complete && do { |
139
|
4
|
|
|
|
|
80
|
print(join("\r\n", '', grep(/^\Q$return/, @cmp_lst)), "\r\n"); |
140
|
4
|
|
|
|
|
40
|
redo LOOP; |
141
|
|
|
|
|
|
|
}; |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
# (^U) kill |
144
|
21
|
100
|
|
|
|
43
|
$_ eq $kill && do { |
145
|
4
|
50
|
|
|
|
9
|
if ($r) { |
146
|
4
|
|
|
|
|
5
|
$r = 0; |
147
|
4
|
|
|
|
|
4
|
$return = ""; |
148
|
4
|
|
|
|
|
9
|
print("\r\n"); |
149
|
4
|
|
|
|
|
27
|
redo LOOP; |
150
|
|
|
|
|
|
|
} |
151
|
0
|
|
|
|
|
0
|
last CASE; |
152
|
|
|
|
|
|
|
}; |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
# (DEL) || (BS) erase |
155
|
17
|
50
|
33
|
|
|
75
|
($_ eq $erase1 || $_ eq $erase2) && do { |
156
|
0
|
0
|
|
|
|
0
|
if($r) { |
157
|
0
|
|
|
|
|
0
|
print("\b \b"); |
158
|
0
|
|
|
|
|
0
|
chop($return); |
159
|
0
|
|
|
|
|
0
|
$r--; |
160
|
|
|
|
|
|
|
} |
161
|
0
|
|
|
|
|
0
|
last CASE; |
162
|
|
|
|
|
|
|
}; |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
# printable char |
165
|
17
|
50
|
|
|
|
40
|
ord >= 32 && do { |
166
|
17
|
|
|
|
|
21
|
$return .= $_; |
167
|
17
|
|
|
|
|
15
|
$r++; |
168
|
17
|
|
|
|
|
37
|
print; |
169
|
17
|
|
|
|
|
108
|
last CASE; |
170
|
|
|
|
|
|
|
}; |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
} |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
# system $tty_restore if defined $tty_restore; |
176
|
4
|
50
|
33
|
|
|
52
|
if (defined $tty_saved_state && defined $tty_restore && defined $tty_safe_restore) |
|
|
|
33
|
|
|
|
|
177
|
|
|
|
|
|
|
{ |
178
|
0
|
|
|
|
|
0
|
system $tty_restore; |
179
|
0
|
0
|
|
|
|
0
|
if ($?) { |
180
|
|
|
|
|
|
|
# tty_restore caused error |
181
|
0
|
|
|
|
|
0
|
system $tty_safe_restore; |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
} |
184
|
4
|
|
|
|
|
8
|
print("\n"); |
185
|
4
|
|
|
|
|
33
|
$return; |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
1; |