2016-05-03 21:35:12 +00:00
|
|
|
// A specialized tokenizer for tokenizing the fish language. In the future, the tokenizer should be
|
|
|
|
// extended to support marks, tokenizing multiple strings and disposing of unused string segments.
|
2016-05-18 22:30:21 +00:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
2019-10-13 22:50:48 +00:00
|
|
|
#include "tokenizer.h"
|
|
|
|
|
2016-05-03 21:35:12 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <unistd.h>
|
2005-09-20 13:26:39 +00:00
|
|
|
#include <wctype.h>
|
2017-02-11 02:47:02 +00:00
|
|
|
|
2019-10-13 22:50:48 +00:00
|
|
|
#include <cwchar>
|
2015-07-25 15:14:25 +00:00
|
|
|
#include <string>
|
2017-02-11 02:47:02 +00:00
|
|
|
#include <type_traits>
|
2006-02-28 13:17:16 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
#include "common.h"
|
2016-05-03 21:35:12 +00:00
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
2018-04-24 22:53:30 +00:00
|
|
|
#include "future_feature_flags.h"
|
2019-10-13 23:06:16 +00:00
|
|
|
#include "tokenizer.h"
|
2016-05-03 21:35:12 +00:00
|
|
|
#include "wutil.h" // IWYU pragma: keep
|
2006-07-19 22:55:49 +00:00
|
|
|
|
2019-03-14 22:12:14 +00:00
|
|
|
// _(s) is already wgettext(s).c_str(), so let's not convert back to wcstring
|
2019-05-05 10:09:25 +00:00
|
|
|
const wchar_t *tokenizer_get_error_message(tokenizer_error_t err) {
|
2018-09-28 01:25:49 +00:00
|
|
|
switch (err) {
|
|
|
|
case tokenizer_error_t::none:
|
|
|
|
return L"";
|
|
|
|
case tokenizer_error_t::unterminated_quote:
|
|
|
|
return _(L"Unexpected end of string, quotes are not balanced");
|
|
|
|
case tokenizer_error_t::unterminated_subshell:
|
|
|
|
return _(L"Unexpected end of string, expecting ')'");
|
|
|
|
case tokenizer_error_t::unterminated_slice:
|
|
|
|
return _(L"Unexpected end of string, square brackets do not match");
|
|
|
|
case tokenizer_error_t::unterminated_escape:
|
|
|
|
return _(L"Unexpected end of string, incomplete escape sequence");
|
|
|
|
case tokenizer_error_t::invalid_redirect:
|
|
|
|
return _(L"Invalid input/output redirection");
|
|
|
|
case tokenizer_error_t::invalid_pipe:
|
|
|
|
return _(L"Cannot use stdin (fd 0) as pipe output");
|
2019-10-27 21:35:14 +00:00
|
|
|
case tokenizer_error_t::invalid_pipe_ampersand:
|
|
|
|
return _(L"|& is not valid. In fish, use &| to pipe both stdout and stderr.");
|
2018-09-28 01:25:49 +00:00
|
|
|
case tokenizer_error_t::closing_unopened_subshell:
|
|
|
|
return _(L"Unexpected ')' for unopened parenthesis");
|
|
|
|
case tokenizer_error_t::illegal_slice:
|
|
|
|
return _(L"Unexpected '[' at this location");
|
|
|
|
case tokenizer_error_t::closing_unopened_brace:
|
|
|
|
return _(L"Unexpected '}' for unopened brace expansion");
|
|
|
|
case tokenizer_error_t::unterminated_brace:
|
|
|
|
return _(L"Unexpected end of string, incomplete parameter expansion");
|
|
|
|
case tokenizer_error_t::expected_pclose_found_bclose:
|
|
|
|
return _(L"Unexpected '}' found, expecting ')'");
|
|
|
|
case tokenizer_error_t::expected_bclose_found_pclose:
|
|
|
|
return _(L"Unexpected ')' found, expecting '}'");
|
|
|
|
}
|
|
|
|
assert(0 && "Unexpected tokenizer error");
|
2019-03-14 22:12:14 +00:00
|
|
|
return nullptr;
|
2018-03-13 18:45:15 +00:00
|
|
|
}
|
2018-02-24 01:28:12 +00:00
|
|
|
|
2018-04-24 22:53:30 +00:00
|
|
|
// Whether carets redirect stderr.
|
2018-05-06 02:44:57 +00:00
|
|
|
static bool caret_redirs() { return !feature_test(features_t::stderr_nocaret); }
|
2018-04-24 22:53:30 +00:00
|
|
|
|
2018-02-23 22:30:15 +00:00
|
|
|
/// Return an error token and mark that we no longer have a next token.
|
2018-09-28 01:25:49 +00:00
|
|
|
tok_t tokenizer_t::call_error(tokenizer_error_t error_type, const wchar_t *token_start,
|
2019-10-27 22:44:08 +00:00
|
|
|
const wchar_t *error_loc, maybe_t<size_t> token_length) {
|
2018-09-28 01:25:49 +00:00
|
|
|
assert(error_type != tokenizer_error_t::none && "tokenizer_error_t::none passed to call_error");
|
2018-02-23 22:30:15 +00:00
|
|
|
assert(error_loc >= token_start && "Invalid error location");
|
2019-11-09 00:40:15 +00:00
|
|
|
assert(this->token_cursor >= token_start && "Invalid buff location");
|
2018-02-23 22:30:15 +00:00
|
|
|
|
2019-10-27 23:08:49 +00:00
|
|
|
// If continue_after_error is set and we have a real token length, then skip past it.
|
|
|
|
// Otherwise give up.
|
|
|
|
if (token_length.has_value() && continue_after_error) {
|
2019-11-09 00:40:15 +00:00
|
|
|
assert(this->token_cursor < error_loc + *token_length && "Unable to continue past error");
|
|
|
|
this->token_cursor = error_loc + *token_length;
|
2019-10-27 23:08:49 +00:00
|
|
|
} else {
|
|
|
|
this->has_next = false;
|
|
|
|
}
|
2018-02-23 22:30:15 +00:00
|
|
|
|
2019-10-13 23:06:16 +00:00
|
|
|
tok_t result{token_type_t::error};
|
2018-02-23 22:30:15 +00:00
|
|
|
result.error = error_type;
|
|
|
|
result.offset = token_start - this->start;
|
2019-10-27 22:44:08 +00:00
|
|
|
// If we are passed a token_length, then use it; otherwise infer it from the buffer.
|
2019-11-09 00:40:15 +00:00
|
|
|
result.length = token_length ? *token_length : this->token_cursor - token_start;
|
2019-11-09 00:55:54 +00:00
|
|
|
result.error_offset_within_token = error_loc - token_start;
|
2018-02-23 22:30:15 +00:00
|
|
|
return result;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2019-11-09 00:40:15 +00:00
|
|
|
tokenizer_t::tokenizer_t(const wchar_t *start, tok_flags_t flags)
|
|
|
|
: token_cursor(start), start(start) {
|
2018-02-19 23:10:10 +00:00
|
|
|
assert(start != nullptr && "Invalid start");
|
2006-07-15 12:40:05 +00:00
|
|
|
|
2016-10-21 04:14:40 +00:00
|
|
|
this->accept_unfinished = static_cast<bool>(flags & TOK_ACCEPT_UNFINISHED);
|
|
|
|
this->show_comments = static_cast<bool>(flags & TOK_SHOW_COMMENTS);
|
|
|
|
this->show_blank_lines = static_cast<bool>(flags & TOK_SHOW_BLANK_LINES);
|
2019-10-27 23:08:49 +00:00
|
|
|
this->continue_after_error = static_cast<bool>(flags & TOK_CONTINUE_AFTER_ERROR);
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2019-10-13 23:06:16 +00:00
|
|
|
tok_t::tok_t(token_type_t type) : type(type) {}
|
2015-07-26 06:05:47 +00:00
|
|
|
|
2018-04-24 22:53:30 +00:00
|
|
|
/// Tests if this character can be a part of a string. The redirect ^ is allowed unless it's the
|
|
|
|
/// first character. Hash (#) starts a comment if it's the first character in a token; otherwise it
|
|
|
|
/// is considered a string character. See issue #953.
|
|
|
|
static bool tok_is_string_character(wchar_t c, bool is_first) {
|
2016-05-03 21:35:12 +00:00
|
|
|
switch (c) {
|
2012-11-19 08:31:03 +00:00
|
|
|
case L'\0':
|
|
|
|
case L' ':
|
|
|
|
case L'\n':
|
|
|
|
case L'|':
|
|
|
|
case L'\t':
|
|
|
|
case L';':
|
|
|
|
case L'\r':
|
|
|
|
case L'<':
|
|
|
|
case L'>':
|
2018-04-24 22:53:30 +00:00
|
|
|
case L'&': {
|
|
|
|
// Unconditional separators.
|
2012-11-19 08:31:03 +00:00
|
|
|
return false;
|
2018-04-24 22:53:30 +00:00
|
|
|
}
|
|
|
|
case L'^': {
|
|
|
|
// Conditional separator.
|
|
|
|
return !caret_redirs() || !is_first;
|
|
|
|
}
|
2019-05-05 10:09:25 +00:00
|
|
|
default: {
|
|
|
|
return true;
|
|
|
|
}
|
2012-07-11 03:30:54 +00:00
|
|
|
}
|
2005-10-26 10:51:02 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 21:35:12 +00:00
|
|
|
/// Quick test to catch the most common 'non-magical' characters, makes read_string slightly faster
|
|
|
|
/// by adding a fast path for the most common characters. This is obviously not a suitable
|
|
|
|
/// replacement for iswalpha.
|
2018-03-11 00:42:56 +00:00
|
|
|
static inline int myal(wchar_t c) { return (c >= L'a' && c <= L'z') || (c >= L'A' && c <= L'Z'); }
|
|
|
|
|
2018-09-25 04:23:18 +00:00
|
|
|
namespace tok_modes {
|
|
|
|
enum {
|
|
|
|
regular_text = 0, // regular text
|
2018-03-11 00:42:56 +00:00
|
|
|
subshell = 1 << 0, // inside of subshell parentheses
|
|
|
|
array_brackets = 1 << 1, // inside of array brackets
|
|
|
|
curly_braces = 1 << 2,
|
|
|
|
char_escape = 1 << 3,
|
2018-03-11 17:13:55 +00:00
|
|
|
};
|
2019-12-21 23:52:38 +00:00
|
|
|
} // namespace tok_modes
|
2018-09-25 04:23:18 +00:00
|
|
|
using tok_mode_t = uint32_t;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 21:35:12 +00:00
|
|
|
/// Read the next token as a string.
|
2018-02-23 22:30:15 +00:00
|
|
|
tok_t tokenizer_t::read_string() {
|
2018-09-25 04:23:18 +00:00
|
|
|
tok_mode_t mode{tok_modes::regular_text};
|
2018-03-11 00:42:56 +00:00
|
|
|
std::vector<int> paran_offsets;
|
2018-03-12 01:06:45 +00:00
|
|
|
std::vector<int> brace_offsets;
|
2018-03-12 00:36:10 +00:00
|
|
|
std::vector<char> expecting;
|
2018-03-11 00:42:56 +00:00
|
|
|
int slice_offset = 0;
|
2019-11-09 00:40:15 +00:00
|
|
|
const wchar_t *const buff_start = this->token_cursor;
|
2018-04-24 22:53:30 +00:00
|
|
|
bool is_first = true;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2018-03-11 00:42:56 +00:00
|
|
|
while (true) {
|
2019-11-09 00:40:15 +00:00
|
|
|
wchar_t c = *this->token_cursor;
|
2018-03-11 00:42:56 +00:00
|
|
|
#if false
|
|
|
|
wcstring msg = L"Handling 0x%x (%lc)";
|
|
|
|
tok_mode mode_begin = mode;
|
|
|
|
#endif
|
|
|
|
|
2018-03-11 22:10:16 +00:00
|
|
|
if (c == L'\0') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-03-11 00:42:56 +00:00
|
|
|
// Make sure this character isn't being escaped before anything else
|
2018-09-25 04:23:18 +00:00
|
|
|
if ((mode & tok_modes::char_escape) == tok_modes::char_escape) {
|
|
|
|
mode &= ~(tok_modes::char_escape);
|
2018-03-11 00:42:56 +00:00
|
|
|
// and do nothing more
|
2018-09-25 04:23:18 +00:00
|
|
|
} else if (myal(c)) {
|
2018-03-11 17:13:55 +00:00
|
|
|
// Early exit optimization in case the character is just a letter,
|
|
|
|
// which has no special meaning to the tokenizer, i.e. the same mode continues.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now proceed with the evaluation of the token, first checking to see if the token
|
|
|
|
// has been explicitly ignored (escaped).
|
|
|
|
else if (c == L'\\') {
|
2018-09-25 04:23:18 +00:00
|
|
|
mode |= tok_modes::char_escape;
|
|
|
|
} else if (c == L'(') {
|
2019-11-09 00:40:15 +00:00
|
|
|
paran_offsets.push_back(this->token_cursor - this->start);
|
2018-03-12 00:36:10 +00:00
|
|
|
expecting.push_back(L')');
|
2018-09-25 04:23:18 +00:00
|
|
|
mode |= tok_modes::subshell;
|
|
|
|
} else if (c == L'{') {
|
2019-11-09 00:40:15 +00:00
|
|
|
brace_offsets.push_back(this->token_cursor - this->start);
|
2018-03-12 00:36:10 +00:00
|
|
|
expecting.push_back(L'}');
|
2018-09-25 04:23:18 +00:00
|
|
|
mode |= tok_modes::curly_braces;
|
|
|
|
} else if (c == L')') {
|
2019-11-19 00:56:46 +00:00
|
|
|
if (!expecting.empty() && expecting.back() == L'}') {
|
2018-09-28 01:25:49 +00:00
|
|
|
return this->call_error(tokenizer_error_t::expected_bclose_found_pclose,
|
2019-11-09 00:53:13 +00:00
|
|
|
this->token_cursor, this->token_cursor, 1);
|
2018-03-12 00:36:10 +00:00
|
|
|
}
|
2019-11-09 00:53:13 +00:00
|
|
|
if (paran_offsets.empty()) {
|
|
|
|
return this->call_error(tokenizer_error_t::closing_unopened_subshell,
|
|
|
|
this->token_cursor, this->token_cursor, 1);
|
|
|
|
}
|
|
|
|
paran_offsets.pop_back();
|
|
|
|
if (paran_offsets.empty()) {
|
|
|
|
mode &= ~(tok_modes::subshell);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2018-03-12 01:06:45 +00:00
|
|
|
expecting.pop_back();
|
2018-09-25 04:23:18 +00:00
|
|
|
} else if (c == L'}') {
|
2019-11-19 00:56:46 +00:00
|
|
|
if (!expecting.empty() && expecting.back() == L')') {
|
2018-09-28 01:25:49 +00:00
|
|
|
return this->call_error(tokenizer_error_t::expected_pclose_found_bclose,
|
2019-11-09 00:53:13 +00:00
|
|
|
this->token_cursor, this->token_cursor, 1);
|
|
|
|
}
|
|
|
|
if (brace_offsets.empty()) {
|
|
|
|
return this->call_error(tokenizer_error_t::closing_unopened_brace,
|
|
|
|
this->token_cursor,
|
|
|
|
this->token_cursor + wcslen(this->token_cursor));
|
2018-03-12 00:36:10 +00:00
|
|
|
}
|
2019-11-09 00:53:13 +00:00
|
|
|
brace_offsets.pop_back();
|
|
|
|
if (brace_offsets.empty()) {
|
|
|
|
mode &= ~(tok_modes::curly_braces);
|
2018-03-12 00:36:10 +00:00
|
|
|
}
|
2018-03-12 01:06:45 +00:00
|
|
|
expecting.pop_back();
|
2018-09-25 04:23:18 +00:00
|
|
|
} else if (c == L'[') {
|
2019-11-09 00:40:15 +00:00
|
|
|
if (this->token_cursor != buff_start) {
|
2018-09-25 04:23:18 +00:00
|
|
|
mode |= tok_modes::array_brackets;
|
2019-11-09 00:40:15 +00:00
|
|
|
slice_offset = this->token_cursor - this->start;
|
2018-09-28 01:25:49 +00:00
|
|
|
} else {
|
|
|
|
// This is actually allowed so the test operator `[` can be used as the head of a
|
|
|
|
// command
|
2018-03-11 00:42:56 +00:00
|
|
|
}
|
2018-03-11 22:10:16 +00:00
|
|
|
}
|
|
|
|
// Only exit bracket mode if we are in bracket mode.
|
|
|
|
// Reason: `]` can be a parameter, e.g. last parameter to `[` test alias.
|
|
|
|
// e.g. echo $argv[([ $x -eq $y ])] # must not end bracket mode on first bracket
|
2018-09-25 04:23:18 +00:00
|
|
|
else if (c == L']' && ((mode & tok_modes::array_brackets) == tok_modes::array_brackets)) {
|
|
|
|
mode &= ~(tok_modes::array_brackets);
|
|
|
|
} else if (c == L'\'' || c == L'"') {
|
2019-11-09 00:40:15 +00:00
|
|
|
const wchar_t *end = quote_end(this->token_cursor);
|
2018-03-11 22:10:16 +00:00
|
|
|
if (end) {
|
2019-11-09 00:40:15 +00:00
|
|
|
this->token_cursor = end;
|
2018-03-11 22:10:16 +00:00
|
|
|
} else {
|
2019-11-09 00:40:15 +00:00
|
|
|
const wchar_t *error_loc = this->token_cursor;
|
|
|
|
this->token_cursor += std::wcslen(this->token_cursor);
|
2018-03-11 22:10:16 +00:00
|
|
|
if ((!this->accept_unfinished)) {
|
2018-09-28 01:25:49 +00:00
|
|
|
return this->call_error(tokenizer_error_t::unterminated_quote, buff_start,
|
|
|
|
error_loc);
|
2015-08-11 01:30:44 +00:00
|
|
|
}
|
2018-03-11 00:42:56 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-09-25 04:23:18 +00:00
|
|
|
} else if (mode == tok_modes::regular_text && !tok_is_string_character(c, is_first)) {
|
2018-03-11 22:10:16 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2018-03-11 00:42:56 +00:00
|
|
|
#if false
|
|
|
|
if (mode != mode_begin) {
|
|
|
|
msg.append(L": mode 0x%x -> 0x%x\n");
|
|
|
|
} else {
|
|
|
|
msg.push_back(L'\n');
|
|
|
|
}
|
2019-05-30 09:54:09 +00:00
|
|
|
FLOGF(error, msg.c_str(), c, c, int(mode_begin), int(mode));
|
2018-03-11 00:42:56 +00:00
|
|
|
#endif
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2019-11-09 00:40:15 +00:00
|
|
|
this->token_cursor++;
|
2018-04-24 22:53:30 +00:00
|
|
|
is_first = false;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2018-09-25 04:23:18 +00:00
|
|
|
if ((!this->accept_unfinished) && (mode != tok_modes::regular_text)) {
|
2019-10-13 23:06:16 +00:00
|
|
|
if (mode & tok_modes::char_escape) {
|
|
|
|
return this->call_error(tokenizer_error_t::unterminated_escape, buff_start,
|
2019-11-09 00:40:15 +00:00
|
|
|
this->token_cursor - 1, 1);
|
2019-10-13 23:06:16 +00:00
|
|
|
} else if (mode & tok_modes::array_brackets) {
|
|
|
|
return this->call_error(tokenizer_error_t::unterminated_slice, buff_start,
|
|
|
|
this->start + slice_offset);
|
|
|
|
} else if (mode & tok_modes::subshell) {
|
2019-11-19 00:56:46 +00:00
|
|
|
assert(!paran_offsets.empty());
|
2018-03-11 00:42:56 +00:00
|
|
|
size_t offset_of_open_paran = paran_offsets.back();
|
2016-05-03 21:35:12 +00:00
|
|
|
|
2019-10-13 23:06:16 +00:00
|
|
|
return this->call_error(tokenizer_error_t::unterminated_subshell, buff_start,
|
|
|
|
this->start + offset_of_open_paran);
|
|
|
|
} else if (mode & tok_modes::curly_braces) {
|
2019-11-19 00:56:46 +00:00
|
|
|
assert(!brace_offsets.empty());
|
2018-03-12 01:06:45 +00:00
|
|
|
size_t offset_of_open_brace = brace_offsets.back();
|
2018-03-12 00:36:10 +00:00
|
|
|
|
2019-10-13 23:06:16 +00:00
|
|
|
return this->call_error(tokenizer_error_t::unterminated_brace, buff_start,
|
|
|
|
this->start + offset_of_open_brace);
|
|
|
|
} else {
|
|
|
|
DIE("Unknown non-regular-text mode");
|
2018-03-12 00:36:10 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2019-10-13 23:06:16 +00:00
|
|
|
tok_t result(token_type_t::string);
|
2018-02-23 22:30:15 +00:00
|
|
|
result.offset = buff_start - this->start;
|
2019-11-09 00:40:15 +00:00
|
|
|
result.length = this->token_cursor - buff_start;
|
2018-02-23 22:30:15 +00:00
|
|
|
return result;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2019-10-14 20:20:31 +00:00
|
|
|
// Parse an fd from the non-empty string [start, end), all of which are digits.
|
|
|
|
// Return the fd, or -1 on overflow.
|
|
|
|
static int parse_fd(const wchar_t *start, const wchar_t *end) {
|
|
|
|
assert(start < end && "String cannot be empty");
|
2013-10-13 23:58:40 +00:00
|
|
|
long long big_fd = 0;
|
2019-10-14 20:20:31 +00:00
|
|
|
for (const wchar_t *cursor = start; cursor < end; ++cursor) {
|
|
|
|
assert(L'0' <= *cursor && *cursor <= L'9' && "Not a digit");
|
|
|
|
big_fd = big_fd * 10 + (*cursor - L'0');
|
|
|
|
if (big_fd > INT_MAX) return -1;
|
2013-10-13 20:26:52 +00:00
|
|
|
}
|
2019-10-14 20:20:31 +00:00
|
|
|
assert(big_fd <= INT_MAX && "big_fd should be in range");
|
|
|
|
return static_cast<int>(big_fd);
|
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2019-10-14 20:20:31 +00:00
|
|
|
pipe_or_redir_t::pipe_or_redir_t() = default;
|
|
|
|
|
|
|
|
maybe_t<pipe_or_redir_t> pipe_or_redir_t::from_string(const wchar_t *buff) {
|
|
|
|
pipe_or_redir_t result{};
|
|
|
|
|
|
|
|
/* Examples of supported syntaxes.
|
|
|
|
Note we are only responsible for parsing the redirection part, not 'cmd' or 'file'.
|
|
|
|
|
|
|
|
cmd | cmd normal pipe
|
2019-10-14 22:45:40 +00:00
|
|
|
cmd &| cmd normal pipe plus stderr-merge
|
2019-10-14 20:20:31 +00:00
|
|
|
cmd >| cmd pipe with explicit fd
|
|
|
|
cmd 2>| cmd pipe with explicit fd
|
|
|
|
cmd < file stdin redirection
|
|
|
|
cmd > file redirection
|
|
|
|
cmd >> file appending redirection
|
|
|
|
cmd >? file noclobber redirection
|
|
|
|
cmd >>? file appending noclobber redirection
|
|
|
|
cmd 2> file file redirection with explicit fd
|
|
|
|
cmd >&2 file fd redirection with no explicit src fd (stdout is used)
|
|
|
|
cmd 1>&2 file fd redirection with an explicit src fd
|
|
|
|
cmd <&2 file fd redirection with no explicit src fd (stdin is used)
|
|
|
|
cmd 3<&0 file fd redirection with an explicit src fd
|
2019-10-14 22:45:40 +00:00
|
|
|
cmd &> file redirection with stderr merge
|
2019-10-14 20:20:31 +00:00
|
|
|
cmd ^ file caret (stderr) redirection, perhaps disabled via feature flags
|
|
|
|
cmd ^^ file caret (stderr) redirection, perhaps disabled via feature flags
|
|
|
|
*/
|
|
|
|
|
|
|
|
const wchar_t *cursor = buff;
|
|
|
|
|
|
|
|
// Extract a range of leading fd.
|
|
|
|
const wchar_t *fd_start = cursor;
|
|
|
|
while (iswdigit(*cursor)) cursor++;
|
|
|
|
const wchar_t *fd_end = cursor;
|
|
|
|
bool has_fd = (fd_end > fd_start);
|
|
|
|
|
|
|
|
// Try consuming a given character.
|
|
|
|
// Return true if consumed. On success, advances cursor.
|
|
|
|
auto try_consume = [&cursor](wchar_t c) -> bool {
|
|
|
|
if (*cursor != c) return false;
|
|
|
|
cursor++;
|
|
|
|
return true;
|
|
|
|
};
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2019-10-14 20:20:31 +00:00
|
|
|
// Like try_consume, but asserts on failure.
|
|
|
|
auto consume = [&](wchar_t c) {
|
|
|
|
assert(*cursor == c && "Failed to consume char");
|
|
|
|
cursor++;
|
|
|
|
};
|
|
|
|
|
|
|
|
switch (*cursor) {
|
|
|
|
case L'|': {
|
|
|
|
if (has_fd) {
|
|
|
|
// Like 123|
|
|
|
|
return none();
|
2016-05-03 21:35:12 +00:00
|
|
|
}
|
2019-10-14 20:20:31 +00:00
|
|
|
consume(L'|');
|
|
|
|
assert(*cursor != L'|' &&
|
|
|
|
"|| passed as redirection, this should have been handled as 'or' by the caller");
|
|
|
|
result.fd = STDOUT_FILENO;
|
|
|
|
result.is_pipe = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case L'>': {
|
|
|
|
consume(L'>');
|
2020-02-17 07:52:41 +00:00
|
|
|
if (try_consume(L'>')) result.mode = redirection_mode_t::append;
|
2019-10-14 20:20:31 +00:00
|
|
|
if (try_consume(L'|')) {
|
|
|
|
// Note we differ from bash here.
|
|
|
|
// Consider `echo foo 2>| bar`
|
|
|
|
// In fish, this is a *pipe*. Run bar as a command and attach foo's stderr to bar's
|
|
|
|
// stdin, while leaving stdout as tty.
|
|
|
|
// In bash, this is a *redirection* to bar as a file. It is like > but ignores
|
|
|
|
// noclobber.
|
|
|
|
result.is_pipe = true;
|
|
|
|
result.fd = has_fd ? parse_fd(fd_start, fd_end) // like 2>|
|
|
|
|
: STDOUT_FILENO; // like >|
|
|
|
|
} else if (try_consume(L'&')) {
|
|
|
|
// This is a redirection to an fd.
|
2020-02-17 13:12:27 +00:00
|
|
|
// Note that we allow ">>&", but it's still just writing to the fd - "appending" to
|
|
|
|
// it doesn't make sense.
|
2019-10-14 20:20:31 +00:00
|
|
|
result.mode = redirection_mode_t::fd;
|
|
|
|
result.fd = has_fd ? parse_fd(fd_start, fd_end) // like 1>&2
|
|
|
|
: STDOUT_FILENO; // like >&2
|
|
|
|
} else {
|
|
|
|
// This is a redirection to a file.
|
|
|
|
result.fd = has_fd ? parse_fd(fd_start, fd_end) // like 1> file.txt
|
|
|
|
: STDOUT_FILENO; // like > file.txt
|
2020-02-17 13:12:27 +00:00
|
|
|
if (result.mode != redirection_mode_t::append)
|
|
|
|
result.mode = redirection_mode_t::overwrite;
|
2019-10-14 20:20:31 +00:00
|
|
|
// Note 'echo abc >>? file' is valid: it means append and noclobber.
|
|
|
|
// But here "noclobber" means the file must not exist, so appending
|
|
|
|
// can be ignored.
|
|
|
|
if (try_consume(L'?')) result.mode = redirection_mode_t::noclob;
|
2016-05-03 21:35:12 +00:00
|
|
|
}
|
2019-10-14 20:20:31 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case L'<': {
|
|
|
|
consume(L'<');
|
|
|
|
if (try_consume('&')) {
|
|
|
|
result.mode = redirection_mode_t::fd;
|
|
|
|
} else {
|
|
|
|
result.mode = redirection_mode_t::input;
|
2018-04-24 22:53:30 +00:00
|
|
|
}
|
2019-10-14 20:20:31 +00:00
|
|
|
result.fd = has_fd ? parse_fd(fd_start, fd_end) // like 1<&3 or 1< /tmp/file.txt
|
|
|
|
: STDIN_FILENO; // like <&3 or < /tmp/file.txt
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case L'^': {
|
|
|
|
if (!caret_redirs()) {
|
|
|
|
// ^ is not special if caret_redirs is disabled.
|
|
|
|
return none();
|
|
|
|
} else {
|
|
|
|
if (has_fd) {
|
|
|
|
return none();
|
|
|
|
}
|
|
|
|
consume(L'^');
|
|
|
|
result.fd = STDERR_FILENO;
|
|
|
|
result.mode = redirection_mode_t::overwrite;
|
2020-02-14 19:27:30 +00:00
|
|
|
if (try_consume(L'^')) {
|
|
|
|
result.mode = redirection_mode_t::append;
|
|
|
|
} else if (try_consume(L'&')) {
|
|
|
|
// This is a redirection to an fd.
|
|
|
|
result.mode = redirection_mode_t::fd;
|
|
|
|
}
|
2019-10-14 20:20:31 +00:00
|
|
|
if (try_consume(L'?')) result.mode = redirection_mode_t::noclob;
|
2014-01-15 09:40:40 +00:00
|
|
|
break;
|
2016-05-03 21:35:12 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2019-10-14 22:45:40 +00:00
|
|
|
case L'&': {
|
|
|
|
consume(L'&');
|
|
|
|
if (try_consume(L'|')) {
|
|
|
|
// &| is pipe with stderr merge.
|
|
|
|
result.fd = STDOUT_FILENO;
|
|
|
|
result.is_pipe = true;
|
|
|
|
result.stderr_merge = true;
|
|
|
|
} else if (try_consume(L'>')) {
|
|
|
|
result.fd = STDOUT_FILENO;
|
|
|
|
result.stderr_merge = true;
|
|
|
|
result.mode = redirection_mode_t::overwrite;
|
|
|
|
if (try_consume(L'>')) result.mode = redirection_mode_t::append; // like &>>
|
|
|
|
if (try_consume(L'?'))
|
|
|
|
result.mode = redirection_mode_t::noclob; // like &>? or &>>?
|
|
|
|
} else {
|
|
|
|
return none();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2019-10-14 20:20:31 +00:00
|
|
|
default: {
|
|
|
|
// Not a redirection.
|
|
|
|
return none();
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2018-02-23 23:19:58 +00:00
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2019-10-14 20:20:31 +00:00
|
|
|
result.consumed = (cursor - buff);
|
|
|
|
assert(result.consumed > 0 && "Should have consumed at least one character on success");
|
2018-02-23 23:19:58 +00:00
|
|
|
return result;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2019-10-14 20:20:31 +00:00
|
|
|
int pipe_or_redir_t::oflags() const {
|
|
|
|
switch (mode) {
|
|
|
|
case redirection_mode_t::append: {
|
2014-01-15 09:40:40 +00:00
|
|
|
return O_CREAT | O_APPEND | O_WRONLY;
|
2016-05-03 21:35:12 +00:00
|
|
|
}
|
2019-10-14 20:20:31 +00:00
|
|
|
case redirection_mode_t::overwrite: {
|
2014-01-15 09:40:40 +00:00
|
|
|
return O_CREAT | O_WRONLY | O_TRUNC;
|
2016-05-03 21:35:12 +00:00
|
|
|
}
|
2019-10-14 20:20:31 +00:00
|
|
|
case redirection_mode_t::noclob: {
|
2014-01-15 09:40:40 +00:00
|
|
|
return O_CREAT | O_EXCL | O_WRONLY;
|
2016-05-03 21:35:12 +00:00
|
|
|
}
|
2019-10-14 20:20:31 +00:00
|
|
|
case redirection_mode_t::input: {
|
2014-01-15 09:40:40 +00:00
|
|
|
return O_RDONLY;
|
2016-05-03 21:35:12 +00:00
|
|
|
}
|
2019-10-14 20:20:31 +00:00
|
|
|
case redirection_mode_t::fd:
|
2019-03-11 21:53:26 +00:00
|
|
|
default: {
|
|
|
|
return -1;
|
|
|
|
}
|
2013-12-23 22:53:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-14 20:20:31 +00:00
|
|
|
/// Test if a character is whitespace. Differs from iswspace in that it does not consider a
|
|
|
|
/// newline to be whitespace.
|
2018-02-19 23:47:02 +00:00
|
|
|
static bool iswspace_not_nl(wchar_t c) {
|
|
|
|
switch (c) {
|
|
|
|
case L' ':
|
|
|
|
case L'\t':
|
|
|
|
case L'\r':
|
|
|
|
return true;
|
|
|
|
case L'\n':
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
return iswspace(c);
|
|
|
|
}
|
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2019-10-13 23:06:16 +00:00
|
|
|
maybe_t<tok_t> tokenizer_t::next() {
|
2016-05-03 21:35:12 +00:00
|
|
|
if (!this->has_next) {
|
2018-02-23 22:30:15 +00:00
|
|
|
return none();
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
|
|
|
|
2019-10-14 20:20:31 +00:00
|
|
|
// Consume non-newline whitespace. If we get an escaped newline, mark it and continue past
|
|
|
|
// it.
|
2018-05-07 22:22:09 +00:00
|
|
|
bool preceding_escaped_nl = false;
|
2018-02-19 23:47:02 +00:00
|
|
|
for (;;) {
|
2019-11-09 00:40:15 +00:00
|
|
|
if (this->token_cursor[0] == L'\\' && this->token_cursor[1] == L'\n') {
|
|
|
|
this->token_cursor += 2;
|
2018-03-12 13:35:09 +00:00
|
|
|
this->continue_line_after_comment = true;
|
2018-05-07 22:22:09 +00:00
|
|
|
preceding_escaped_nl = true;
|
2019-11-09 00:40:15 +00:00
|
|
|
} else if (iswspace_not_nl(this->token_cursor[0])) {
|
|
|
|
this->token_cursor++;
|
2016-05-03 21:35:12 +00:00
|
|
|
} else {
|
2012-11-19 00:30:30 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2019-11-09 00:40:15 +00:00
|
|
|
while (*this->token_cursor == L'#') {
|
2018-02-19 23:47:02 +00:00
|
|
|
// We have a comment, walk over the comment.
|
2019-11-09 00:40:15 +00:00
|
|
|
const wchar_t *comment_start = this->token_cursor;
|
|
|
|
while (this->token_cursor[0] != L'\n' && this->token_cursor[0] != L'\0')
|
|
|
|
this->token_cursor++;
|
|
|
|
size_t comment_len = this->token_cursor - comment_start;
|
2018-02-19 23:47:02 +00:00
|
|
|
|
2018-03-12 13:35:09 +00:00
|
|
|
// If we are going to continue after the comment, skip any trailing newline.
|
2019-11-09 00:40:15 +00:00
|
|
|
if (this->token_cursor[0] == L'\n' && this->continue_line_after_comment)
|
|
|
|
this->token_cursor++;
|
2018-03-12 13:35:09 +00:00
|
|
|
|
2018-02-19 23:47:02 +00:00
|
|
|
// Maybe return the comment.
|
|
|
|
if (this->show_comments) {
|
2019-10-13 23:06:16 +00:00
|
|
|
tok_t result(token_type_t::comment);
|
2018-02-23 22:30:15 +00:00
|
|
|
result.offset = comment_start - this->start;
|
|
|
|
result.length = comment_len;
|
2018-05-07 22:22:09 +00:00
|
|
|
result.preceding_escaped_nl = preceding_escaped_nl;
|
2018-02-23 22:30:15 +00:00
|
|
|
return result;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2019-11-09 00:40:15 +00:00
|
|
|
while (iswspace_not_nl(this->token_cursor[0])) this->token_cursor++;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2018-03-12 13:35:09 +00:00
|
|
|
// We made it past the comments and ate any trailing newlines we wanted to ignore.
|
|
|
|
this->continue_line_after_comment = false;
|
2019-11-09 00:40:15 +00:00
|
|
|
const size_t start_pos = this->token_cursor - this->start;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2019-10-13 23:06:16 +00:00
|
|
|
maybe_t<tok_t> result{};
|
2019-11-09 00:40:15 +00:00
|
|
|
switch (*this->token_cursor) {
|
2016-05-03 21:35:12 +00:00
|
|
|
case L'\0': {
|
2015-07-26 07:58:32 +00:00
|
|
|
this->has_next = false;
|
2018-02-23 22:30:15 +00:00
|
|
|
return none();
|
2016-05-03 21:35:12 +00:00
|
|
|
}
|
|
|
|
case L'\r': // carriage-return
|
|
|
|
case L'\n': // newline
|
|
|
|
case L';': {
|
2019-10-13 23:06:16 +00:00
|
|
|
result.emplace(token_type_t::end);
|
|
|
|
result->offset = start_pos;
|
|
|
|
result->length = 1;
|
2019-11-09 00:40:15 +00:00
|
|
|
this->token_cursor++;
|
2016-05-03 21:35:12 +00:00
|
|
|
// Hack: when we get a newline, swallow as many as we can. This compresses multiple
|
|
|
|
// subsequent newlines into a single one.
|
|
|
|
if (!this->show_blank_lines) {
|
2019-11-09 00:40:15 +00:00
|
|
|
while (*this->token_cursor == L'\n' || *this->token_cursor == 13 /* CR */ ||
|
|
|
|
*this->token_cursor == ' ' || *this->token_cursor == '\t') {
|
|
|
|
this->token_cursor++;
|
2014-11-25 18:43:03 +00:00
|
|
|
}
|
2014-11-24 09:20:57 +00:00
|
|
|
}
|
2012-11-19 08:31:03 +00:00
|
|
|
break;
|
2016-05-03 21:35:12 +00:00
|
|
|
}
|
|
|
|
case L'&': {
|
2019-11-09 00:40:15 +00:00
|
|
|
if (this->token_cursor[1] == L'&') {
|
2019-10-14 22:45:40 +00:00
|
|
|
// && is and.
|
2019-10-13 23:06:16 +00:00
|
|
|
result.emplace(token_type_t::andand);
|
|
|
|
result->offset = start_pos;
|
|
|
|
result->length = 2;
|
2019-11-09 00:40:15 +00:00
|
|
|
this->token_cursor += 2;
|
|
|
|
} else if (this->token_cursor[1] == L'>' || this->token_cursor[1] == L'|') {
|
2019-10-14 22:45:40 +00:00
|
|
|
// &> and &| redirect both stdout and stderr.
|
2019-11-09 00:40:15 +00:00
|
|
|
auto redir = pipe_or_redir_t::from_string(this->token_cursor);
|
2019-10-14 22:45:40 +00:00
|
|
|
assert(redir.has_value() &&
|
|
|
|
"Should always succeed to parse a &> or &| redirection");
|
|
|
|
result.emplace(redir->token_type());
|
|
|
|
result->offset = start_pos;
|
|
|
|
result->length = redir->consumed;
|
2019-11-09 00:40:15 +00:00
|
|
|
this->token_cursor += redir->consumed;
|
2018-03-01 20:56:15 +00:00
|
|
|
} else {
|
2019-10-13 23:06:16 +00:00
|
|
|
result.emplace(token_type_t::background);
|
|
|
|
result->offset = start_pos;
|
|
|
|
result->length = 1;
|
2019-11-09 00:40:15 +00:00
|
|
|
this->token_cursor++;
|
2018-03-01 20:56:15 +00:00
|
|
|
}
|
2012-11-19 08:31:03 +00:00
|
|
|
break;
|
2016-05-03 21:35:12 +00:00
|
|
|
}
|
|
|
|
case L'|': {
|
2019-11-09 00:40:15 +00:00
|
|
|
if (this->token_cursor[1] == L'|') {
|
2019-10-14 22:45:40 +00:00
|
|
|
// || is or.
|
2019-10-13 23:06:16 +00:00
|
|
|
result.emplace(token_type_t::oror);
|
|
|
|
result->offset = start_pos;
|
|
|
|
result->length = 2;
|
2019-11-09 00:40:15 +00:00
|
|
|
this->token_cursor += 2;
|
|
|
|
} else if (this->token_cursor[1] == L'&') {
|
2019-10-27 21:35:14 +00:00
|
|
|
// |& is a bashism; in fish it's &|.
|
2019-11-09 00:40:15 +00:00
|
|
|
return this->call_error(tokenizer_error_t::invalid_pipe_ampersand,
|
|
|
|
this->token_cursor, this->token_cursor, 2);
|
2018-03-01 20:56:15 +00:00
|
|
|
} else {
|
2019-11-09 00:40:15 +00:00
|
|
|
auto pipe = pipe_or_redir_t::from_string(this->token_cursor);
|
2019-10-14 20:20:31 +00:00
|
|
|
assert(pipe.has_value() && pipe->is_pipe &&
|
|
|
|
"Should always succeed to parse a | pipe");
|
|
|
|
result.emplace(pipe->token_type());
|
2019-10-13 23:06:16 +00:00
|
|
|
result->offset = start_pos;
|
2019-10-14 20:20:31 +00:00
|
|
|
result->length = pipe->consumed;
|
2019-11-09 00:40:15 +00:00
|
|
|
this->token_cursor += pipe->consumed;
|
2018-03-01 20:56:15 +00:00
|
|
|
}
|
2012-11-19 08:31:03 +00:00
|
|
|
break;
|
2016-05-03 21:35:12 +00:00
|
|
|
}
|
2012-11-19 08:31:03 +00:00
|
|
|
case L'>':
|
2018-04-01 20:43:05 +00:00
|
|
|
case L'<': {
|
2019-10-14 20:20:31 +00:00
|
|
|
// There's some duplication with the code in the default case below. The key
|
|
|
|
// difference here is that we must never parse these as a string; a failed
|
|
|
|
// redirection is an error!
|
2019-11-09 00:40:15 +00:00
|
|
|
auto redir_or_pipe = pipe_or_redir_t::from_string(this->token_cursor);
|
2018-02-23 23:19:58 +00:00
|
|
|
if (!redir_or_pipe || redir_or_pipe->fd < 0) {
|
2019-11-09 00:40:15 +00:00
|
|
|
return this->call_error(tokenizer_error_t::invalid_redirect, this->token_cursor,
|
|
|
|
this->token_cursor,
|
2019-10-27 22:44:08 +00:00
|
|
|
redir_or_pipe ? redir_or_pipe->consumed : 0);
|
2013-10-13 20:26:52 +00:00
|
|
|
}
|
2019-10-14 20:20:31 +00:00
|
|
|
result.emplace(redir_or_pipe->token_type());
|
2019-10-13 23:06:16 +00:00
|
|
|
result->offset = start_pos;
|
|
|
|
result->length = redir_or_pipe->consumed;
|
2019-11-09 00:40:15 +00:00
|
|
|
this->token_cursor += redir_or_pipe->consumed;
|
2016-05-03 21:35:12 +00:00
|
|
|
break;
|
2013-10-13 20:26:52 +00:00
|
|
|
}
|
2016-05-03 21:35:12 +00:00
|
|
|
default: {
|
|
|
|
// Maybe a redirection like '2>&1', maybe a pipe like 2>|, maybe just a string.
|
2019-11-09 00:40:15 +00:00
|
|
|
const wchar_t *error_location = this->token_cursor;
|
2019-10-14 20:20:31 +00:00
|
|
|
maybe_t<pipe_or_redir_t> redir_or_pipe{};
|
2019-11-09 00:40:15 +00:00
|
|
|
if (iswdigit(*this->token_cursor) || (*this->token_cursor == L'^' && caret_redirs())) {
|
|
|
|
redir_or_pipe = pipe_or_redir_t::from_string(this->token_cursor);
|
2015-10-07 18:38:13 +00:00
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2019-10-14 20:20:31 +00:00
|
|
|
if (redir_or_pipe) {
|
2016-05-03 21:35:12 +00:00
|
|
|
// It looks like a redirection or a pipe. But we don't support piping fd 0. Note
|
2019-10-14 20:20:31 +00:00
|
|
|
// that fd 0 may be -1, indicating overflow; but we don't treat that as a
|
|
|
|
// tokenizer error.
|
|
|
|
if (redir_or_pipe->is_pipe && redir_or_pipe->fd == 0) {
|
2018-09-28 01:25:49 +00:00
|
|
|
return this->call_error(tokenizer_error_t::invalid_pipe, error_location,
|
2019-10-27 22:44:08 +00:00
|
|
|
error_location, redir_or_pipe->consumed);
|
2012-11-19 08:31:03 +00:00
|
|
|
}
|
2019-10-14 20:20:31 +00:00
|
|
|
result.emplace(redir_or_pipe->token_type());
|
2019-10-13 23:06:16 +00:00
|
|
|
result->offset = start_pos;
|
|
|
|
result->length = redir_or_pipe->consumed;
|
2019-11-09 00:40:15 +00:00
|
|
|
this->token_cursor += redir_or_pipe->consumed;
|
2016-05-03 21:35:12 +00:00
|
|
|
} else {
|
|
|
|
// Not a redirection or pipe, so just a string.
|
2018-02-23 22:30:15 +00:00
|
|
|
result = this->read_string();
|
2013-10-13 20:26:52 +00:00
|
|
|
}
|
2016-05-03 21:35:12 +00:00
|
|
|
break;
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2019-10-13 23:06:16 +00:00
|
|
|
assert(result.has_value() && "Should have a token");
|
|
|
|
result->preceding_escaped_nl = preceding_escaped_nl;
|
2018-02-23 22:30:15 +00:00
|
|
|
return result;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 21:35:12 +00:00
|
|
|
wcstring tok_first(const wcstring &str) {
|
2018-02-24 01:28:12 +00:00
|
|
|
tokenizer_t t(str.c_str(), 0);
|
2019-10-13 23:06:16 +00:00
|
|
|
if (auto token = t.next()) {
|
|
|
|
if (token->type == token_type_t::string) {
|
|
|
|
return t.text_of(*token);
|
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2018-02-23 22:30:15 +00:00
|
|
|
return {};
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2020-02-23 23:16:12 +00:00
|
|
|
wcstring tok_command(const wcstring &str) {
|
|
|
|
tokenizer_t t(str.c_str(), 0);
|
|
|
|
while (auto token = t.next()) {
|
|
|
|
if (token->type != token_type_t::string) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
wcstring text = t.text_of(*token);
|
|
|
|
if (variable_assignment_equals_pos(text)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2016-05-03 21:35:12 +00:00
|
|
|
bool move_word_state_machine_t::consume_char_punctuation(wchar_t c) {
|
2018-02-25 15:30:15 +00:00
|
|
|
enum { s_always_one = 0, s_rest, s_whitespace_rest, s_whitespace, s_alphanumeric, s_end };
|
2012-12-22 20:21:31 +00:00
|
|
|
|
2012-12-21 01:37:09 +00:00
|
|
|
bool consumed = false;
|
2016-05-03 21:35:12 +00:00
|
|
|
while (state != s_end && !consumed) {
|
|
|
|
switch (state) {
|
|
|
|
case s_always_one: {
|
|
|
|
// Always consume the first character.
|
2012-12-21 01:37:09 +00:00
|
|
|
consumed = true;
|
2018-02-25 15:30:15 +00:00
|
|
|
if (iswspace(c)) {
|
|
|
|
state = s_whitespace;
|
|
|
|
} else {
|
|
|
|
// Don't allow switching type (ws->nonws) after non-whitespace.
|
|
|
|
state = s_rest;
|
|
|
|
}
|
2012-12-21 01:37:09 +00:00
|
|
|
break;
|
2016-05-03 21:35:12 +00:00
|
|
|
}
|
2018-02-25 15:30:15 +00:00
|
|
|
case s_rest: {
|
|
|
|
if (iswspace(c)) {
|
|
|
|
// Consume only trailing whitespace.
|
|
|
|
state = s_whitespace_rest;
|
|
|
|
} else if (iswalnum(c)) {
|
|
|
|
// Consume only alnums.
|
|
|
|
state = s_alphanumeric;
|
|
|
|
} else {
|
|
|
|
consumed = false;
|
|
|
|
state = s_end;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case s_whitespace_rest:
|
2016-05-03 21:35:12 +00:00
|
|
|
case s_whitespace: {
|
2018-02-25 15:30:15 +00:00
|
|
|
// "whitespace" consumes whitespace and switches to alnums,
|
|
|
|
// "whitespace_rest" only consumes whitespace.
|
2016-05-03 21:35:12 +00:00
|
|
|
if (iswspace(c)) {
|
|
|
|
// Consumed whitespace.
|
2012-12-21 01:37:09 +00:00
|
|
|
consumed = true;
|
2016-05-03 21:35:12 +00:00
|
|
|
} else {
|
2018-02-25 15:30:15 +00:00
|
|
|
state = state == s_whitespace ? s_alphanumeric : s_end;
|
2012-12-21 01:37:09 +00:00
|
|
|
}
|
|
|
|
break;
|
2016-05-03 21:35:12 +00:00
|
|
|
}
|
|
|
|
case s_alphanumeric: {
|
|
|
|
if (iswalnum(c)) {
|
|
|
|
consumed = true; // consumed alphanumeric
|
|
|
|
} else {
|
2012-12-21 01:37:09 +00:00
|
|
|
state = s_end;
|
|
|
|
}
|
|
|
|
break;
|
2016-05-03 21:35:12 +00:00
|
|
|
}
|
2012-12-21 01:37:09 +00:00
|
|
|
case s_end:
|
2019-05-05 10:09:25 +00:00
|
|
|
default: {
|
|
|
|
break;
|
|
|
|
}
|
2012-12-21 01:37:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return consumed;
|
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 21:35:12 +00:00
|
|
|
bool move_word_state_machine_t::is_path_component_character(wchar_t c) {
|
2018-04-24 22:53:30 +00:00
|
|
|
// Always treat separators as first. All this does is ensure that we treat ^ as a string
|
2019-10-14 20:20:31 +00:00
|
|
|
// character instead of as stderr redirection, which I hypothesize is usually what is
|
|
|
|
// desired.
|
2019-04-26 17:07:33 +00:00
|
|
|
return tok_is_string_character(c, true) && !std::wcschr(L"/={,}'\":@", c);
|
2012-12-11 00:23:08 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 21:35:12 +00:00
|
|
|
bool move_word_state_machine_t::consume_char_path_components(wchar_t c) {
|
|
|
|
enum {
|
2012-12-21 01:37:09 +00:00
|
|
|
s_initial_punctuation,
|
|
|
|
s_whitespace,
|
|
|
|
s_separator,
|
|
|
|
s_slash,
|
|
|
|
s_path_component_characters,
|
|
|
|
s_end
|
|
|
|
};
|
2012-12-22 20:21:31 +00:00
|
|
|
|
2019-03-12 21:06:01 +00:00
|
|
|
// std::fwprintf(stdout, L"state %d, consume '%lc'\n", state, c);
|
2012-12-11 00:23:08 +00:00
|
|
|
bool consumed = false;
|
2016-05-03 21:35:12 +00:00
|
|
|
while (state != s_end && !consumed) {
|
|
|
|
switch (state) {
|
|
|
|
case s_initial_punctuation: {
|
|
|
|
if (!is_path_component_character(c)) {
|
2012-12-21 01:37:09 +00:00
|
|
|
consumed = true;
|
|
|
|
}
|
|
|
|
state = s_whitespace;
|
|
|
|
break;
|
2016-05-03 21:35:12 +00:00
|
|
|
}
|
|
|
|
case s_whitespace: {
|
|
|
|
if (iswspace(c)) {
|
|
|
|
consumed = true; // consumed whitespace
|
|
|
|
} else if (c == L'/' || is_path_component_character(c)) {
|
|
|
|
state = s_slash; // path component
|
|
|
|
} else {
|
|
|
|
state = s_separator; // path separator
|
2012-12-11 00:23:08 +00:00
|
|
|
}
|
|
|
|
break;
|
2016-05-03 21:35:12 +00:00
|
|
|
}
|
|
|
|
case s_separator: {
|
|
|
|
if (!iswspace(c) && !is_path_component_character(c)) {
|
|
|
|
consumed = true; // consumed separator
|
2016-05-03 23:23:30 +00:00
|
|
|
} else {
|
2012-12-11 00:23:08 +00:00
|
|
|
state = s_end;
|
|
|
|
}
|
|
|
|
break;
|
2016-05-03 21:35:12 +00:00
|
|
|
}
|
|
|
|
case s_slash: {
|
|
|
|
if (c == L'/') {
|
|
|
|
consumed = true; // consumed slash
|
|
|
|
} else {
|
2012-12-21 01:37:09 +00:00
|
|
|
state = s_path_component_characters;
|
2012-12-11 00:23:08 +00:00
|
|
|
}
|
|
|
|
break;
|
2016-05-03 21:35:12 +00:00
|
|
|
}
|
|
|
|
case s_path_component_characters: {
|
|
|
|
if (is_path_component_character(c)) {
|
|
|
|
consumed = true; // consumed string character except slash
|
|
|
|
} else {
|
2012-12-11 00:23:08 +00:00
|
|
|
state = s_end;
|
|
|
|
}
|
|
|
|
break;
|
2016-05-03 21:35:12 +00:00
|
|
|
}
|
2012-12-11 00:23:08 +00:00
|
|
|
case s_end:
|
2019-05-05 10:09:25 +00:00
|
|
|
default: {
|
|
|
|
break;
|
|
|
|
}
|
2012-12-11 00:23:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return consumed;
|
|
|
|
}
|
|
|
|
|
2016-05-03 21:35:12 +00:00
|
|
|
bool move_word_state_machine_t::consume_char_whitespace(wchar_t c) {
|
|
|
|
enum { s_always_one = 0, s_blank, s_graph, s_end };
|
Add 'bigword' vi key bindings
- Add four new functions: forward-bigword, backward-bigword,
kill-bigword, backward-kill-bigword
- Add new enum move_word_style_whitespace and related state machine
method
- Change vi key bindings to operate on bigwords: B, gE, W, E, dW, diW,
daW, dE, dB, dgE, cW, ciW, caW, cE, cB, cgE, yW, yiW, yaW, yE, yB,
ygE
2015-05-30 22:44:25 +00:00
|
|
|
|
|
|
|
bool consumed = false;
|
2016-05-03 21:35:12 +00:00
|
|
|
while (state != s_end && !consumed) {
|
|
|
|
switch (state) {
|
|
|
|
case s_always_one: {
|
|
|
|
consumed = true; // always consume the first character
|
Add 'bigword' vi key bindings
- Add four new functions: forward-bigword, backward-bigword,
kill-bigword, backward-kill-bigword
- Add new enum move_word_style_whitespace and related state machine
method
- Change vi key bindings to operate on bigwords: B, gE, W, E, dW, diW,
daW, dE, dB, dgE, cW, ciW, caW, cE, cB, cgE, yW, yiW, yaW, yE, yB,
ygE
2015-05-30 22:44:25 +00:00
|
|
|
state = s_blank;
|
|
|
|
break;
|
2016-05-03 21:35:12 +00:00
|
|
|
}
|
|
|
|
case s_blank: {
|
|
|
|
if (iswblank(c)) {
|
|
|
|
consumed = true; // consumed whitespace
|
|
|
|
} else {
|
Add 'bigword' vi key bindings
- Add four new functions: forward-bigword, backward-bigword,
kill-bigword, backward-kill-bigword
- Add new enum move_word_style_whitespace and related state machine
method
- Change vi key bindings to operate on bigwords: B, gE, W, E, dW, diW,
daW, dE, dB, dgE, cW, ciW, caW, cE, cB, cgE, yW, yiW, yaW, yE, yB,
ygE
2015-05-30 22:44:25 +00:00
|
|
|
state = s_graph;
|
|
|
|
}
|
|
|
|
break;
|
2016-05-03 21:35:12 +00:00
|
|
|
}
|
|
|
|
case s_graph: {
|
|
|
|
if (iswgraph(c)) {
|
|
|
|
consumed = true; // consumed printable non-space
|
|
|
|
} else {
|
Add 'bigword' vi key bindings
- Add four new functions: forward-bigword, backward-bigword,
kill-bigword, backward-kill-bigword
- Add new enum move_word_style_whitespace and related state machine
method
- Change vi key bindings to operate on bigwords: B, gE, W, E, dW, diW,
daW, dE, dB, dgE, cW, ciW, caW, cE, cB, cgE, yW, yiW, yaW, yE, yB,
ygE
2015-05-30 22:44:25 +00:00
|
|
|
state = s_end;
|
|
|
|
}
|
|
|
|
break;
|
2016-05-03 21:35:12 +00:00
|
|
|
}
|
Add 'bigword' vi key bindings
- Add four new functions: forward-bigword, backward-bigword,
kill-bigword, backward-kill-bigword
- Add new enum move_word_style_whitespace and related state machine
method
- Change vi key bindings to operate on bigwords: B, gE, W, E, dW, diW,
daW, dE, dB, dgE, cW, ciW, caW, cE, cB, cgE, yW, yiW, yaW, yE, yB,
ygE
2015-05-30 22:44:25 +00:00
|
|
|
case s_end:
|
2019-05-05 10:09:25 +00:00
|
|
|
default: {
|
|
|
|
break;
|
|
|
|
}
|
Add 'bigword' vi key bindings
- Add four new functions: forward-bigword, backward-bigword,
kill-bigword, backward-kill-bigword
- Add new enum move_word_style_whitespace and related state machine
method
- Change vi key bindings to operate on bigwords: B, gE, W, E, dW, diW,
daW, dE, dB, dgE, cW, ciW, caW, cE, cB, cgE, yW, yiW, yaW, yE, yB,
ygE
2015-05-30 22:44:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return consumed;
|
|
|
|
}
|
|
|
|
|
2016-05-03 21:35:12 +00:00
|
|
|
bool move_word_state_machine_t::consume_char(wchar_t c) {
|
|
|
|
switch (style) {
|
|
|
|
case move_word_style_punctuation: {
|
2012-12-22 20:21:31 +00:00
|
|
|
return consume_char_punctuation(c);
|
2016-05-03 21:35:12 +00:00
|
|
|
}
|
|
|
|
case move_word_style_path_components: {
|
2012-12-22 20:21:31 +00:00
|
|
|
return consume_char_path_components(c);
|
2016-05-03 21:35:12 +00:00
|
|
|
}
|
|
|
|
case move_word_style_whitespace: {
|
Add 'bigword' vi key bindings
- Add four new functions: forward-bigword, backward-bigword,
kill-bigword, backward-kill-bigword
- Add new enum move_word_style_whitespace and related state machine
method
- Change vi key bindings to operate on bigwords: B, gE, W, E, dW, diW,
daW, dE, dB, dgE, cW, ciW, caW, cE, cB, cgE, yW, yiW, yaW, yE, yB,
ygE
2015-05-30 22:44:25 +00:00
|
|
|
return consume_char_whitespace(c);
|
2016-05-03 21:35:12 +00:00
|
|
|
}
|
2012-12-21 01:37:09 +00:00
|
|
|
}
|
2016-11-07 01:48:26 +00:00
|
|
|
|
|
|
|
DIE("should not reach this statement"); // silence some compiler errors about not returning
|
2012-12-21 01:37:09 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 21:35:12 +00:00
|
|
|
move_word_state_machine_t::move_word_state_machine_t(move_word_style_t syl)
|
|
|
|
: state(0), style(syl) {}
|
2012-12-21 01:37:09 +00:00
|
|
|
|
2016-05-03 21:35:12 +00:00
|
|
|
void move_word_state_machine_t::reset() { state = 0; }
|
2020-02-23 23:14:39 +00:00
|
|
|
|
|
|
|
// Return the location of the equals sign, or npos if the string does
|
|
|
|
// not look like a variable assignment like FOO=bar. The detection
|
|
|
|
// works similar as in some POSIX shells: only letters and numbers qre
|
|
|
|
// allowed on the left hand side, no quotes or escaping.
|
|
|
|
maybe_t<size_t> variable_assignment_equals_pos(const wcstring &txt) {
|
|
|
|
enum { init, has_some_variable_identifier } state = init;
|
|
|
|
// TODO bracket indexing
|
|
|
|
for (size_t i = 0; i < txt.size(); i++) {
|
|
|
|
wchar_t c = txt[i];
|
|
|
|
if (state == init) {
|
|
|
|
if (!valid_var_name_char(c)) return {};
|
|
|
|
state = has_some_variable_identifier;
|
|
|
|
} else {
|
|
|
|
if (c == '=') return {i};
|
|
|
|
if (!valid_var_name_char(c)) return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|