-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWordExpander.cpp
368 lines (300 loc) · 11.5 KB
/
WordExpander.cpp
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#include "WordExpander.h"
#include <ctype.h>
#include <string>
#include <string.h>
#include "Global.h"
#include "Tokenizer.h"
#include "executor.h"
#include <pwd.h>
#include <errno.h>
#include "utils.h"
#include <glob.h>
static bool is_one_letter_variable_name(char ch) {
return utils::no_locale_isdigit(ch) || strchr("!@#$?-*", ch) != nullptr;
}
static bool can_start_variable_name(char ch) {
return utils::no_locale_isalpha(ch) || ch == '_';
}
static bool can_be_in_variable_name(char ch) {
return utils::no_locale_isalnum(ch) || ch == '_';
}
bool WordExpander::expand_into(std::vector<std::string> &buf)
{
out = &buf;
can_expand_to_empty_word = true;
// Assume we can't expand to an empty word (for example, the word contains quotations
// like ""$var). If the assumption turns out to be incorrect and we did not expand to
// anything, pop that empty string back at the end of this function
out->emplace_back();
enum { FREE, SINGLE_QUOTED, DOUBLE_QUOTED } state = FREE;
for(size_t i = 0; i < input.size(); i++) {
char ch = input[i];
std::optional<char> next_ch, prev_ch;
if(i != input.size() - 1)
next_ch = input[i + 1];
if(i != 0)
prev_ch = input[i - 1];
if(state == FREE && ch == '"') {
state = DOUBLE_QUOTED;
can_expand_to_empty_word = false;
} else if(state == FREE && ch == '\'') {
state = SINGLE_QUOTED;
can_expand_to_empty_word = false;
} else if(state == DOUBLE_QUOTED && ch == '"') {
state = FREE;
} else if(state == SINGLE_QUOTED && ch == '\'') {
state = FREE;
} else if((state == FREE || state == DOUBLE_QUOTED) && ch == '\\') {
if(next_ch)
add_character_quoted(next_ch.value());
i += 1;
} else if(state == FREE && ch == '$' && next_ch.has_value() && is_one_letter_variable_name(next_ch.value())) {
expand_special_variable_free(next_ch.value());
i += 1;
} else if(state == DOUBLE_QUOTED && ch == '$' && next_ch.has_value() && is_one_letter_variable_name(next_ch.value())) {
expand_special_variable_double_quoted(next_ch.value());
i += 1;
} else if(state == FREE && ch == '$' && can_start_variable_name(next_ch.value_or('\0'))) {
i = expand_variable_free(i + 1);
} else if(state == DOUBLE_QUOTED && ch == '$' && can_start_variable_name(next_ch.value_or('\0'))) {
i = expand_variable_double_quoted(i + 1); // TODO: add a test that fails if this does expand_variable_free
} else if(opt.unsafeExpansions && state == FREE && ch == '$' && next_ch.value_or('\0') == '(') {
i = expand_command_substitution_free(i + 2);
} else if(opt.unsafeExpansions && state == DOUBLE_QUOTED && ch == '$' && next_ch.value_or('\0') == '(') {
i = expand_command_substitution_double_quoted(i + 2);
} else if(state == FREE && ch == '~' && (!prev_ch.has_value() || prev_ch.value() == ':')) {
i = expand_tilda(i + 1);
} else if(state == SINGLE_QUOTED) {
add_character_quoted(ch);
} else if(state == DOUBLE_QUOTED) {
add_character_quoted(ch);
} else if(state == FREE) {
add_character_literal(ch);
}
}
do_pathname_expansion_on_last_word();
// Note that word expansion might result in multiple words - out->back() being empty
// does not necessarily mean the expansion did not expand to anything
// For example:
// set -- 1 2 ''; echo "$@"
// However, this only matters in situations where quotes are used - so can_expand_to_empty_word is false.
// That's why it's not necessary to check if out->size() has changed due to expansion to multiple words.
if(can_expand_to_empty_word && out->back().empty()) {
out->pop_back();
}
return true;
}
bool WordExpander::expand_into(std::string &buf)
{
// TODO: avoid a copy here somehow
std::vector<std::string> buf_vec;
expand_into(buf_vec);
if(buf_vec.size() > 1) {
return false;
}
if(buf_vec.size() == 0) {
buf = "";
return true;
}
buf = buf_vec.at(0);
return true;
}
// Used for expanded ~ and literals
void WordExpander::add_character_literal(char ch)
{
// This should not conform to $IFS
if(ch == ' ' || ch == '\t' || ch == '\n') {
delimit_by_whitespace();
return;
}
add_character_quoted(ch);
if(ch == '*' || ch == '?') {
mark_pathname_expansion_character_location();
}
}
// Used for expanded unquoted strings (like $())
void WordExpander::add_character_unquoted(char ch)
{
/* TODO: Implement $IFS here */
if(ch == ' ' || ch == '\t' || ch == '\n') {
delimit_by_whitespace();
return;
}
add_character_quoted(ch);
if(ch == '*' || ch == '?') {
mark_pathname_expansion_character_location();
}
}
void WordExpander::add_character_quoted(char ch)
{
out->back().push_back(ch);
}
void WordExpander::mark_pathname_expansion_character_location()
{
size_t current_location = out->back().size() - 1;
pathname_expansion_pattern_location_on_last_word.push_back(current_location);
}
void WordExpander::delimit_by_whitespace()
{
if(!opt.fieldSplitting)
return;
// TODO: Handle IFS=', '
// "a , b" -> {"a", "b"}
// If delimiting by whitespace, don't delimit multiple times if there's adjoining whitespace
// For example, "a b" -> {"a", "b"}
if(out->back().size() == 0)
return;
delimit_by_non_whitespace();
}
void WordExpander::delimit_by_non_whitespace()
{
// Delimiting by non-whitespace (with $IFS) should result in empty fields in case of repeated delimeters
// For example, "a::b" -> {"a", "", "b"}
do_pathname_expansion_on_last_word();
out->emplace_back();
}
void WordExpander::do_pathname_expansion_on_last_word()
{
if(opt.pathnameExpansion == Options::NEVER)
return;
if(!pathname_expansion_pattern_location_on_last_word.empty()) {
glob_t globbuf;
// TODO: implement glob() ourselves - don't rely on the os-provided one
// there could be incompatibilities in how it's implemented
// TODO: implement nullglob (don't check if gl_pathc != 0 if it's on)
int globres;
if((globres = glob(out->back().c_str(), 0, nullptr, &globbuf)) == 0 && globbuf.gl_pathc != 0) {
// have at least one match - replace expanded text with matches
out->pop_back();
for(std::size_t i = 0; i < globbuf.gl_pathc; i++) {
out->emplace_back(globbuf.gl_pathv[i]);
}
}
if(globres == 0)
globfree(&globbuf);
}
pathname_expansion_pattern_location_on_last_word.clear();
}
size_t WordExpander::expand_tilda(size_t username_begin)
{
size_t username_end = username_begin;
while(username_end < input.size() && (utils::no_locale_isalnum(input[username_end]) || strchr("._-", input[username_end]) != nullptr)) {
username_end++;
}
std::string username = std::string(input.substr(username_begin, username_end - username_begin));
std::string_view expanded;
passwd *pw;
std::optional<std::string> HOME;
if(username.empty()) {
// Expanding a lonely "~"
HOME = g.get_variable("HOME");
// If $HOME somehow isn't set, put back a tilde
static const char* static_tilde = "~";
expanded = HOME.value_or(static_tilde);
} else {
// Expanding the longer "~user"
// Retry getpwnam as many times as neccessary if interrupted
do {
errno = 0;
pw = getpwnam(username.c_str());
} while(pw == nullptr && errno == EINTR);
if(pw == nullptr) {
// On different systems getpwnam sets errno to 0 or ENOENT or ESRCH or EBADF or EPERM or ...
// when the given name could not be found.
// Those are the only real errors calling getpwnam can result in:
if(errno == EIO || errno == EMFILE || errno == ENFILE || errno == ENOMEM) {
perror("kish: getpwnam");
}
// No matching username found: Put the original "~user" expression back as an expanded expression
expanded = input.substr(username_begin - 1, username_end - username_begin + 1);
} else {
// Successfully got the home directory of ~user
expanded = pw->pw_dir;
}
}
out->back().append(expanded);
return username_end - 1;
}
size_t WordExpander::expand_command_substitution_free(size_t input_position)
{
Tokenizer::Options opt;
opt.countToUntil = '(';
opt.until = ')';
Tokenizer tokenizer(input.substr(input_position));
std::vector<Token> tokens = tokenizer.tokenize(opt);
std::string output;
executor::subshell_capture_output(tokens, output);
for(char ch : output) {
add_character_unquoted(ch);
}
return input_position + tokenizer.consumedChars();
}
size_t WordExpander::expand_command_substitution_double_quoted(size_t input_position)
{
Tokenizer::Options opt;
opt.countToUntil = '(';
opt.until = ')';
Tokenizer tokenizer(input.substr(input_position));
std::vector<Token> tokens = tokenizer.tokenize(opt);
// capture to the output directly
executor::subshell_capture_output(tokens, out->back());
return input_position + tokenizer.consumedChars();
}
void WordExpander::expand_special_variable_free(char varname)
{
// Treat free unquoted $@ like unquoted $*
if(varname == '@') {
varname = '*';
}
if(std::optional<std::string> var_value = g.get_variable(std::string(1, varname))) {
for(char ch : var_value.value()) {
add_character_unquoted(ch);
}
}
}
void WordExpander::expand_special_variable_double_quoted(char varname)
{
if(varname == '@') {
// "$@"
if(g.argv.size() <= 1) {
can_expand_to_empty_word = true;
} else {
for(std::size_t i = 1; i < g.argv.size(); i++) {
if(i != 1)
out->emplace_back();
out->back().append(g.argv.at(i));
}
}
} else {
// "$*", "$1", "$!", ...
if(std::optional<std::string> var_value = g.get_variable(std::string(1, varname))) {
out->back().append(var_value.value());
}
}
}
size_t WordExpander::expand_variable_free(size_t variable_name_begin)
{
size_t variable_name_end = variable_name_begin;
while(variable_name_end < input.size() && can_be_in_variable_name(input[variable_name_end])) {
variable_name_end++;
}
std::string variable_name = std::string(input.substr(variable_name_begin, variable_name_end - variable_name_begin));
if(std::optional<std::string> variable_value = g.get_variable(variable_name)) {
for(char ch : variable_value.value()) {
add_character_unquoted(ch);
}
}
return variable_name_end - 1;
}
size_t WordExpander::expand_variable_double_quoted(size_t variable_name_begin)
{
size_t variable_name_end = variable_name_begin;
while(variable_name_end < input.size() && can_be_in_variable_name(input[variable_name_end])) {
variable_name_end++;
}
std::string variable_name = std::string(input.substr(variable_name_begin, variable_name_end - variable_name_begin));
if(std::optional<std::string> variable_value = g.get_variable(variable_name)) {
out->back().append(variable_value.value());
}
return variable_name_end - 1;
}