forked from bminor/bash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bracecomp.cc
200 lines (159 loc) · 5.34 KB
/
bracecomp.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* bracecomp.cc -- Complete a filename with the possible completions enclosed
in csh-style braces such that the list of completions is available to the
shell. */
/* Original version by tromey@cns.caltech.edu, Fri Feb 7 1992. */
/* Copyright (C) 1993-2020 Free Software Foundation, Inc.
This file is part of GNU Bash, the Bourne Again SHell.
Bash is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Bash is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Bash. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#if defined(BRACE_EXPANSION) && defined(READLINE)
#include "readline.hh"
#include "shell.hh"
namespace bash
{
static int _strcompare (char **, char **);
/* Find greatest common prefix of two strings. */
static int
string_gcd (const char *s1, const char *s2)
{
if (s1 == NULL || s2 == NULL)
return 0;
int i;
for (i = 0; *s1 && *s2; ++s1, ++s2, ++i)
{
if (*s1 != *s2)
break;
}
return i;
}
static char *
really_munge_braces (char **array, int real_start, int real_end, int gcd_zero)
{
int start, end, gcd;
char *subterm, *x;
int tlen;
bool flag = false;
if (real_start == real_end)
{
x = array[real_start]
? sh_backslash_quote (array[real_start] + gcd_zero, 0, 0)
: sh_backslash_quote (array[0], 0, 0);
return x;
}
std::string result;
for (start = real_start; start < real_end; start = end + 1)
{
gcd = strlen (array[start]);
for (end = start + 1; end < real_end; end++)
{
int temp;
temp = string_gcd (array[start], array[end]);
if (temp <= gcd_zero)
break;
gcd = temp;
}
end--;
if (gcd_zero == 0 && start == real_start && end != (real_end - 1))
{
/* In this case, add in a leading '{', because we are at
top level, and there isn't a consistent prefix. */
result.push_back ('{');
flag = true;
}
/* Make sure we backslash quote every substring we insert into the
resultant brace expression. This is so the default filename
quoting function won't inappropriately quote the braces. */
if (start == end)
{
x = savestring (array[start] + gcd_zero);
subterm = sh_backslash_quote (x, 0, 0);
delete[] x;
}
else
{
/* If there is more than one element in the subarray,
insert the (quoted) prefix and an opening brace. */
tlen = gcd - gcd_zero;
x = new char[tlen + 1];
strncpy (x, array[start] + gcd_zero, tlen);
x[tlen] = '\0';
subterm = sh_backslash_quote (x, 0, 0);
delete[] x;
result.append (subterm);
delete[] subterm;
result.push_back ('{');
subterm = really_munge_braces (array, start, end + 1, gcd);
subterm[strlen (subterm) - 1] = '}';
}
result.append (subterm);
result.push_back (',');
delete[] subterm;
}
if (gcd_zero == 0 && flag)
result.push_back ('}');
return savestring (result);
}
static int
_strcompare (char **s1, char **s2)
{
int result;
result = **s1 - **s2;
if (result == 0)
result = strcmp (*s1, *s2);
return result;
}
static int
hack_braces_completion (char **names)
{
int i = strvec_len (names);
if (MB_CUR_MAX > 1 && i > 2)
qsort (names + 1, i - 1, sizeof (char *), (QSFUNC *)_strcompare);
char *temp = really_munge_braces (names, 1, i, 0);
for (i = 0; names[i]; ++i)
{
free (names[i]);
names[i] = NULL;
}
names[0] = temp;
return 0;
}
/* We handle quoting ourselves within hack_braces_completion, so we turn off
rl_filename_quoting_desired and rl_filename_quoting_function. */
int
bash_brace_completion (int count, int ignore)
{
rl_compignore_func_t *orig_ignore_func;
rl_compentry_func_t *orig_entry_func;
rl_quote_func_t *orig_quoting_func;
rl_completion_func_t *orig_attempt_func;
int orig_quoting_desired, r;
orig_ignore_func = rl_ignore_some_completions_function;
orig_attempt_func = rl_attempted_completion_function;
orig_entry_func = rl_completion_entry_function;
orig_quoting_func = rl_filename_quoting_function;
orig_quoting_desired = rl_filename_quoting_desired;
rl_completion_entry_function = rl_filename_completion_function;
rl_attempted_completion_function = (rl_completion_func_t *)NULL;
rl_ignore_some_completions_function = hack_braces_completion;
rl_filename_quoting_function = (rl_quote_func_t *)NULL;
rl_filename_quoting_desired = 0;
r = rl_complete_internal (TAB);
rl_ignore_some_completions_function = orig_ignore_func;
rl_attempted_completion_function = orig_attempt_func;
rl_completion_entry_function = orig_entry_func;
rl_filename_quoting_function = orig_quoting_func;
rl_filename_quoting_desired = orig_quoting_desired;
return r;
}
} // namespace bash
#endif /* BRACE_EXPANSION && READLINE */