Hopefully better fix for prompt under Windows. Things that may store

NOT_A_WCHAR must be wint_t, not wchar_t
This commit is contained in:
ridiculousfish 2014-05-14 13:30:41 +08:00
parent 3225d7e169
commit c7aca5cc35

View File

@ -72,7 +72,7 @@ parts of fish.
#include "util.cpp" #include "util.cpp"
#include "fallback.cpp" #include "fallback.cpp"
#define NOT_A_WCHAR WEOF #define NOT_A_WCHAR (static_cast<wint_t>(WEOF))
struct termios shell_modes; struct termios shell_modes;
@ -1137,8 +1137,8 @@ static size_t read_unquoted_escape(const wchar_t *input, wcstring *result, bool
return 0; return 0;
} }
/* Here's the character we'll ultimately append. Note that L'\0' is a valid thing to append. */ /* Here's the character we'll ultimately append, or NOT_A_WCHAR for none. Note that L'\0' is a valid thing to append. */
wchar_t result_char = NOT_A_WCHAR; wint_t result_char_or_none = NOT_A_WCHAR;
bool errored = false; bool errored = false;
size_t in_pos = 1; //in_pos always tracks the next character to read (and therefore the number of characters read so far) size_t in_pos = 1; //in_pos always tracks the next character to read (and therefore the number of characters read so far)
@ -1240,7 +1240,7 @@ static size_t read_unquoted_escape(const wchar_t *input, wcstring *result, bool
if (res <= max_val) if (res <= max_val)
{ {
result_char = (wchar_t)((byte_literal ? ENCODE_DIRECT_BASE : 0)+res); result_char_or_none = (wchar_t)((byte_literal ? ENCODE_DIRECT_BASE : 0)+res);
} }
else else
{ {
@ -1253,14 +1253,14 @@ static size_t read_unquoted_escape(const wchar_t *input, wcstring *result, bool
/* \a means bell (alert) */ /* \a means bell (alert) */
case L'a': case L'a':
{ {
result_char = L'\a'; result_char_or_none = L'\a';
break; break;
} }
/* \b means backspace */ /* \b means backspace */
case L'b': case L'b':
{ {
result_char = L'\b'; result_char_or_none = L'\b';
break; break;
} }
@ -1270,11 +1270,11 @@ static size_t read_unquoted_escape(const wchar_t *input, wcstring *result, bool
const wchar_t sequence_char = input[in_pos++]; const wchar_t sequence_char = input[in_pos++];
if (sequence_char >= L'a' && sequence_char <= (L'a'+32)) if (sequence_char >= L'a' && sequence_char <= (L'a'+32))
{ {
result_char = sequence_char-L'a'+1; result_char_or_none = sequence_char-L'a'+1;
} }
else if (sequence_char >= L'A' && sequence_char <= (L'A'+32)) else if (sequence_char >= L'A' && sequence_char <= (L'A'+32))
{ {
result_char = sequence_char-L'A'+1; result_char_or_none = sequence_char-L'A'+1;
} }
else else
{ {
@ -1286,7 +1286,7 @@ static size_t read_unquoted_escape(const wchar_t *input, wcstring *result, bool
/* \x1b means escape */ /* \x1b means escape */
case L'e': case L'e':
{ {
result_char = L'\x1b'; result_char_or_none = L'\x1b';
break; break;
} }
@ -1295,7 +1295,7 @@ static size_t read_unquoted_escape(const wchar_t *input, wcstring *result, bool
*/ */
case L'f': case L'f':
{ {
result_char = L'\f'; result_char_or_none = L'\f';
break; break;
} }
@ -1304,7 +1304,7 @@ static size_t read_unquoted_escape(const wchar_t *input, wcstring *result, bool
*/ */
case L'n': case L'n':
{ {
result_char = L'\n'; result_char_or_none = L'\n';
break; break;
} }
@ -1313,7 +1313,7 @@ static size_t read_unquoted_escape(const wchar_t *input, wcstring *result, bool
*/ */
case L'r': case L'r':
{ {
result_char = L'\r'; result_char_or_none = L'\r';
break; break;
} }
@ -1322,7 +1322,7 @@ static size_t read_unquoted_escape(const wchar_t *input, wcstring *result, bool
*/ */
case L't': case L't':
{ {
result_char = L'\t'; result_char_or_none = L'\t';
break; break;
} }
@ -1331,14 +1331,14 @@ static size_t read_unquoted_escape(const wchar_t *input, wcstring *result, bool
*/ */
case L'v': case L'v':
{ {
result_char = L'\v'; result_char_or_none = L'\v';
break; break;
} }
/* If a backslash is followed by an actual newline, swallow them both */ /* If a backslash is followed by an actual newline, swallow them both */
case L'\n': case L'\n':
{ {
result_char = NOT_A_WCHAR; result_char_or_none = NOT_A_WCHAR;
break; break;
} }
@ -1346,13 +1346,16 @@ static size_t read_unquoted_escape(const wchar_t *input, wcstring *result, bool
{ {
if (unescape_special) if (unescape_special)
result->push_back(INTERNAL_SEPARATOR); result->push_back(INTERNAL_SEPARATOR);
result_char = c; result_char_or_none = c;
break; break;
} }
} }
if (! errored && result_char != NOT_A_WCHAR) if (! errored && result_char_or_none != NOT_A_WCHAR)
{ {
wchar_t result_char = static_cast<wchar_t>(result_char_or_none);
// if result_char is not NOT_A_WCHAR, it must be a valid wchar
assert((wint_t)result_char == result_char_or_none);
result->push_back(result_char); result->push_back(result_char);
} }
return errored ? 0 : in_pos; return errored ? 0 : in_pos;
@ -1382,7 +1385,7 @@ static bool unescape_string_internal(const wchar_t * const input, const size_t i
{ {
const wchar_t c = input[input_position]; const wchar_t c = input[input_position];
/* Here's the character we'll append to result, or NOT_A_WCHAR to suppress it */ /* Here's the character we'll append to result, or NOT_A_WCHAR to suppress it */
wchar_t to_append = c; wint_t to_append_or_none = c;
if (mode == mode_unquoted) if (mode == mode_unquoted)
{ {
@ -1404,7 +1407,7 @@ static bool unescape_string_internal(const wchar_t * const input, const size_t i
input_position += escape_chars - 1; input_position += escape_chars - 1;
} }
/* We've already appended, don't append anything else */ /* We've already appended, don't append anything else */
to_append = NOT_A_WCHAR; to_append_or_none = NOT_A_WCHAR;
break; break;
} }
@ -1412,7 +1415,7 @@ static bool unescape_string_internal(const wchar_t * const input, const size_t i
{ {
if (unescape_special && (input_position == 0)) if (unescape_special && (input_position == 0))
{ {
to_append = HOME_DIRECTORY; to_append_or_none = HOME_DIRECTORY;
} }
break; break;
} }
@ -1421,7 +1424,7 @@ static bool unescape_string_internal(const wchar_t * const input, const size_t i
{ {
if (unescape_special && (input_position == 0)) if (unescape_special && (input_position == 0))
{ {
to_append = PROCESS_EXPAND; to_append_or_none = PROCESS_EXPAND;
} }
break; break;
} }
@ -1435,11 +1438,11 @@ static bool unescape_string_internal(const wchar_t * const input, const size_t i
{ {
assert(result.size() > 0); assert(result.size() > 0);
result.resize(result.size() - 1); result.resize(result.size() - 1);
to_append = ANY_STRING_RECURSIVE; to_append_or_none = ANY_STRING_RECURSIVE;
} }
else else
{ {
to_append = ANY_STRING; to_append_or_none = ANY_STRING;
} }
} }
break; break;
@ -1449,7 +1452,7 @@ static bool unescape_string_internal(const wchar_t * const input, const size_t i
{ {
if (unescape_special) if (unescape_special)
{ {
to_append = ANY_CHAR; to_append_or_none = ANY_CHAR;
} }
break; break;
} }
@ -1458,7 +1461,7 @@ static bool unescape_string_internal(const wchar_t * const input, const size_t i
{ {
if (unescape_special) if (unescape_special)
{ {
to_append = VARIABLE_EXPAND; to_append_or_none = VARIABLE_EXPAND;
} }
break; break;
} }
@ -1468,7 +1471,7 @@ static bool unescape_string_internal(const wchar_t * const input, const size_t i
if (unescape_special) if (unescape_special)
{ {
bracket_count++; bracket_count++;
to_append = BRACKET_BEGIN; to_append_or_none = BRACKET_BEGIN;
} }
break; break;
} }
@ -1478,7 +1481,7 @@ static bool unescape_string_internal(const wchar_t * const input, const size_t i
if (unescape_special) if (unescape_special)
{ {
bracket_count--; bracket_count--;
to_append = BRACKET_END; to_append_or_none = BRACKET_END;
} }
break; break;
} }
@ -1488,7 +1491,7 @@ static bool unescape_string_internal(const wchar_t * const input, const size_t i
/* If the last character was a separator, then treat this as a literal comma */ /* If the last character was a separator, then treat this as a literal comma */
if (unescape_special && bracket_count > 0 && string_last_char(result) != BRACKET_SEP) if (unescape_special && bracket_count > 0 && string_last_char(result) != BRACKET_SEP)
{ {
to_append = BRACKET_SEP; to_append_or_none = BRACKET_SEP;
} }
break; break;
} }
@ -1496,14 +1499,14 @@ static bool unescape_string_internal(const wchar_t * const input, const size_t i
case L'\'': case L'\'':
{ {
mode = mode_single_quotes; mode = mode_single_quotes;
to_append = unescape_special ? INTERNAL_SEPARATOR : NOT_A_WCHAR; to_append_or_none = unescape_special ? INTERNAL_SEPARATOR : NOT_A_WCHAR;
break; break;
} }
case L'\"': case L'\"':
{ {
mode = mode_double_quotes; mode = mode_double_quotes;
to_append = unescape_special ? INTERNAL_SEPARATOR : NOT_A_WCHAR; to_append_or_none = unescape_special ? INTERNAL_SEPARATOR : NOT_A_WCHAR;
break; break;
} }
} }
@ -1518,7 +1521,7 @@ static bool unescape_string_internal(const wchar_t * const input, const size_t i
case '\\': case '\\':
case L'\'': case L'\'':
{ {
to_append = input[input_position + 1]; to_append_or_none = input[input_position + 1];
input_position += 1; /* Skip over the backslash */ input_position += 1; /* Skip over the backslash */
break; break;
} }
@ -1535,7 +1538,7 @@ static bool unescape_string_internal(const wchar_t * const input, const size_t i
// 'We may ever escape a NULL character, but still appending a \ in case I am wrong.' // 'We may ever escape a NULL character, but still appending a \ in case I am wrong.'
// Not sure what it means or the importance of this // Not sure what it means or the importance of this
input_position += 1; /* Skip over the backslash */ input_position += 1; /* Skip over the backslash */
to_append = L'\\'; to_append_or_none = L'\\';
} }
} }
break; break;
@ -1549,7 +1552,7 @@ static bool unescape_string_internal(const wchar_t * const input, const size_t i
} }
else if (c == L'\'') else if (c == L'\'')
{ {
to_append = unescape_special ? INTERNAL_SEPARATOR : NOT_A_WCHAR; to_append_or_none = unescape_special ? INTERNAL_SEPARATOR : NOT_A_WCHAR;
mode = mode_unquoted; mode = mode_unquoted;
} }
} }
@ -1560,7 +1563,7 @@ static bool unescape_string_internal(const wchar_t * const input, const size_t i
case L'"': case L'"':
{ {
mode = mode_unquoted; mode = mode_unquoted;
to_append = unescape_special ? INTERNAL_SEPARATOR : NOT_A_WCHAR; to_append_or_none = unescape_special ? INTERNAL_SEPARATOR : NOT_A_WCHAR;
break; break;
} }
@ -1576,7 +1579,7 @@ static bool unescape_string_internal(const wchar_t * const input, const size_t i
} }
else else
{ {
to_append = L'\0'; to_append_or_none = L'\0';
} }
} }
break; break;
@ -1585,7 +1588,7 @@ static bool unescape_string_internal(const wchar_t * const input, const size_t i
case L'$': case L'$':
case '"': case '"':
{ {
to_append = input[input_position + 1]; to_append_or_none = input[input_position + 1];
input_position += 1; /* Skip over the backslash */ input_position += 1; /* Skip over the backslash */
break; break;
} }
@ -1593,7 +1596,7 @@ static bool unescape_string_internal(const wchar_t * const input, const size_t i
case '\n': case '\n':
{ {
/* Swallow newline */ /* Swallow newline */
to_append = NOT_A_WCHAR; to_append_or_none = NOT_A_WCHAR;
input_position += 1; /* Skip over the backslash */ input_position += 1; /* Skip over the backslash */
break; break;
} }
@ -1611,7 +1614,7 @@ static bool unescape_string_internal(const wchar_t * const input, const size_t i
{ {
if (unescape_special) if (unescape_special)
{ {
to_append = VARIABLE_EXPAND_SINGLE; to_append_or_none = VARIABLE_EXPAND_SINGLE;
} }
break; break;
} }
@ -1619,11 +1622,13 @@ static bool unescape_string_internal(const wchar_t * const input, const size_t i
} }
} }
wchar_t not_a_wchar = NOT_A_WCHAR;
/* Now maybe append the char */ /* Now maybe append the char */
if (to_append != not_a_wchar) if (to_append_or_none != NOT_A_WCHAR)
{ {
result.push_back(to_append); wchar_t to_append_char = static_cast<wchar_t>(to_append_or_none);
// if result_char is not NOT_A_WCHAR, it must be a valid wchar
assert((wint_t)to_append_char == to_append_or_none);
result.push_back(to_append_char);
} }
} }