From dcced5f1bcb2ac196f4f0d95e4171366e65eb294 Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Mon, 18 Jun 2018 00:01:32 -0500 Subject: [PATCH] Replace \e with \x1B, as the former is a gcc extension While supported by gcc and clang, \e is a gcc-specific extension and not formally defined in the C or C++ standards. See [0] for a list of valid escapes. [0]: https://stackoverflow.com/a/10220539/17027 --- src/builtin_echo.cpp | 2 +- src/builtin_printf.cpp | 4 ++-- src/builtin_set_color.cpp | 6 +++--- src/common.cpp | 6 +++--- src/expand.cpp | 2 +- src/fish_key_reader.cpp | 2 +- src/fish_tests.cpp | 22 +++++++++++----------- src/input.cpp | 10 +++++----- src/input_common.cpp | 2 +- src/output.cpp | 6 +++--- src/proc.cpp | 4 ++-- src/reader.cpp | 4 ++-- src/screen.cpp | 10 +++++----- 13 files changed, 40 insertions(+), 40 deletions(-) diff --git a/src/builtin_echo.cpp b/src/builtin_echo.cpp index 875e804a4..d10b1d581 100644 --- a/src/builtin_echo.cpp +++ b/src/builtin_echo.cpp @@ -214,7 +214,7 @@ int builtin_echo(parser_t &parser, io_streams_t &streams, wchar_t **argv) { break; } case L'e': { - wc = L'\e'; + wc = L'\x1B'; break; } case L'f': { diff --git a/src/builtin_printf.cpp b/src/builtin_printf.cpp index 24cf9a9e1..2e016d004 100644 --- a/src/builtin_printf.cpp +++ b/src/builtin_printf.cpp @@ -26,7 +26,7 @@ // \a = alert (bell) // \b = backspace // \c = produce no further output -// \e = escape +// \x1B = escape // \f = form feed // \n = new line // \r = carriage return @@ -317,7 +317,7 @@ void builtin_printf_state_t::print_esc_char(wchar_t c) { break; } case L'e': { // escape - this->append_output(L'\e'); + this->append_output(L'\x1B'); break; } case L'f': { // form feed diff --git a/src/builtin_set_color.cpp b/src/builtin_set_color.cpp index 3971712c0..c39a9c828 100644 --- a/src/builtin_set_color.cpp +++ b/src/builtin_set_color.cpp @@ -62,9 +62,9 @@ static const struct woption long_options[] = {{L"background", required_argument, {NULL, 0, NULL, 0}}; #if __APPLE__ -char sitm_esc[] = "\e[3m"; -char ritm_esc[] = "\e[23m"; -char dim_esc[] = "\e[2m"; +char sitm_esc[] = "\x1B[3m"; +char ritm_esc[] = "\x1B[23m"; +char dim_esc[] = "\x1B[2m"; #endif /// set_color builtin. diff --git a/src/common.cpp b/src/common.cpp index 6dc4001af..0cd0075dd 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -981,7 +981,7 @@ static void escape_string_script(const wchar_t *orig_in, size_t in_len, wcstring need_escape = need_complex_escape = 1; break; } - case L'\e': { + case L'\x1B': { out += L'\\'; out += L'e'; need_escape = need_complex_escape = 1; @@ -1243,9 +1243,9 @@ size_t read_unquoted_escape(const wchar_t *input, wcstring *result, bool allow_i } break; } - // \e means escape. + // \x1B means escape. case L'e': { - result_char_or_none = L'\e'; + result_char_or_none = L'\x1B'; break; } // \f means form feed. diff --git a/src/expand.cpp b/src/expand.cpp index 7e3476886..6eb0aec10 100644 --- a/src/expand.cpp +++ b/src/expand.cpp @@ -132,7 +132,7 @@ static int is_quotable(const wchar_t *str) { case L'\t': case L'\r': case L'\b': - case L'\e': { + case L'\x1B': { return 0; } default: { return is_quotable(str + 1); } diff --git a/src/fish_key_reader.cpp b/src/fish_key_reader.cpp index aec1626be..5dd842db1 100644 --- a/src/fish_key_reader.cpp +++ b/src/fish_key_reader.cpp @@ -40,7 +40,7 @@ struct config_paths_t determine_config_directory_paths(const char *argv0); static const wchar_t *ctrl_symbolic_names[] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, L"\\a", L"\\b", L"\\t", L"\\n", L"\\v", L"\\f", L"\\r", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, - NULL, NULL, NULL, NULL, NULL, L"\\e", NULL, NULL, NULL, NULL}; + NULL, NULL, NULL, NULL, NULL, L"\\x1B", NULL, NULL, NULL, NULL}; static bool keep_running = true; /// Return true if the recent sequence of characters indicates the user wants to exit the program. diff --git a/src/fish_tests.cpp b/src/fish_tests.cpp index e70f4eeb1..070e5705c 100644 --- a/src/fish_tests.cpp +++ b/src/fish_tests.cpp @@ -125,7 +125,7 @@ static void err(const wchar_t *blah, ...) { // Show errors in red. if (colorize) { - fputws(L"\e[31m", stdout); + fputws(L"\x1B[31m", stdout); } fwprintf(stdout, L"Error: "); vfwprintf(stdout, blah, va); @@ -133,7 +133,7 @@ static void err(const wchar_t *blah, ...) { // Return to normal color. if (colorize) { - fputws(L"\e[0m", stdout); + fputws(L"\x1B[0m", stdout); } fwprintf(stdout, L"\n"); @@ -1398,25 +1398,25 @@ static void test_escape_sequences() { if (escape_code_length(L"") != 0) err(L"test_escape_sequences failed on line %d\n", __LINE__); if (escape_code_length(L"abcd") != 0) err(L"test_escape_sequences failed on line %d\n", __LINE__); - if (escape_code_length(L"\e[2J") != 4) + if (escape_code_length(L"\x1B[2J") != 4) err(L"test_escape_sequences failed on line %d\n", __LINE__); - if (escape_code_length(L"\e[38;5;123mABC") != strlen("\e[38;5;123m")) + if (escape_code_length(L"\x1B[38;5;123mABC") != strlen("\x1B[38;5;123m")) err(L"test_escape_sequences failed on line %d\n", __LINE__); - if (escape_code_length(L"\e@") != 2) + if (escape_code_length(L"\x1B@") != 2) err(L"test_escape_sequences failed on line %d\n", __LINE__); // iTerm2 escape sequences. - if (escape_code_length(L"\e]50;CurrentDir=test/foo\x07NOT_PART_OF_SEQUENCE") != 25) + if (escape_code_length(L"\x1B]50;CurrentDir=test/foo\x07NOT_PART_OF_SEQUENCE") != 25) err(L"test_escape_sequences failed on line %d\n", __LINE__); - if (escape_code_length(L"\e]50;SetMark\x07NOT_PART_OF_SEQUENCE") != 13) + if (escape_code_length(L"\x1B]50;SetMark\x07NOT_PART_OF_SEQUENCE") != 13) err(L"test_escape_sequences failed on line %d\n", __LINE__); - if (escape_code_length(L"\e]6;1;bg;red;brightness;255\x07NOT_PART_OF_SEQUENCE") != 28) + if (escape_code_length(L"\x1B]6;1;bg;red;brightness;255\x07NOT_PART_OF_SEQUENCE") != 28) err(L"test_escape_sequences failed on line %d\n", __LINE__); - if (escape_code_length(L"\e]Pg4040ff\e\\NOT_PART_OF_SEQUENCE") != 12) + if (escape_code_length(L"\x1B]Pg4040ff\x1B\\NOT_PART_OF_SEQUENCE") != 12) err(L"test_escape_sequences failed on line %d\n", __LINE__); - if (escape_code_length(L"\e]blahblahblah\e\\") != 16) + if (escape_code_length(L"\x1B]blahblahblah\x1B\\") != 16) err(L"test_escape_sequences failed on line %d\n", __LINE__); - if (escape_code_length(L"\e]blahblahblah\x07") != 15) + if (escape_code_length(L"\x1B]blahblahblah\x07") != 15) err(L"test_escape_sequences failed on line %d\n", __LINE__); } diff --git a/src/input.cpp b/src/input.cpp index 6cde72d71..0af4ba0d4 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -265,10 +265,10 @@ void init_input() { input_mapping_add(L"\x5", L"bind"); input_mapping_add(L"\x7f", L"backward-delete-char"); // Arrows - can't have functions, so *-or-search isn't available. - input_mapping_add(L"\e[A", L"up-line"); - input_mapping_add(L"\e[B", L"down-line"); - input_mapping_add(L"\e[C", L"forward-char"); - input_mapping_add(L"\e[D", L"backward-char"); + input_mapping_add(L"\x1B[A", L"up-line"); + input_mapping_add(L"\x1B[B", L"down-line"); + input_mapping_add(L"\x1B[C", L"forward-char"); + input_mapping_add(L"\x1B[D", L"backward-char"); } input_initialized = true; @@ -392,7 +392,7 @@ static bool input_mapping_is_match(const input_mapping_t &m) { // If we just read an escape, we need to add a timeout for the next char, // to distinguish between the actual escape key and an "alt"-modifier. - timed = (str[i] == L'\e'); + timed = (str[i] == L'\x1B'); } return true; diff --git a/src/input_common.cpp b/src/input_common.cpp index 4f6c21f02..59e0e77d7 100644 --- a/src/input_common.cpp +++ b/src/input_common.cpp @@ -29,7 +29,7 @@ #include "wutil.h" /// Time in milliseconds to wait for another byte to be available for reading -/// after \e is read before assuming that escape key was pressed, and not an +/// after \x1B is read before assuming that escape key was pressed, and not an /// escape sequence. #define WAIT_ON_ESCAPE_DEFAULT 300 static int wait_on_escape_ms = WAIT_ON_ESCAPE_DEFAULT; diff --git a/src/output.cpp b/src/output.cpp index 995c7d808..129ed701c 100644 --- a/src/output.cpp +++ b/src/output.cpp @@ -80,9 +80,9 @@ static bool write_color_escape(char *todo, unsigned char idx, bool is_fg) { // with what we do here, will make the brights actually work for virtual consoles/ancient // emulators. if (max_colors == 8 && idx > 8) idx -= 8; - snprintf(buff, sizeof buff, "\e[%dm", ((idx > 7) ? 82 : 30) + idx + !is_fg * 10); + snprintf(buff, sizeof buff, "\x1B[%dm", ((idx > 7) ? 82 : 30) + idx + !is_fg * 10); } else { - snprintf(buff, sizeof buff, "\e[%d;5;%dm", is_fg ? 38 : 48, idx); + snprintf(buff, sizeof buff, "\x1B[%d;5;%dm", is_fg ? 38 : 48, idx); } int (*writer)(char) = output_get_writer(); @@ -131,7 +131,7 @@ bool write_color(rgb_color_t color, bool is_fg) { // Background: ^[48;2;;;m color24_t rgb = color.to_color24(); char buff[128]; - snprintf(buff, sizeof buff, "\e[%d;2;%u;%u;%um", is_fg ? 38 : 48, rgb.rgb[0], rgb.rgb[1], + snprintf(buff, sizeof buff, "\x1B[%d;2;%u;%u;%um", is_fg ? 38 : 48, rgb.rgb[0], rgb.rgb[1], rgb.rgb[2]); int (*writer)(char) = output_get_writer(); if (writer) { diff --git a/src/proc.cpp b/src/proc.cpp index b7fb937ae..1a8761a2a 100644 --- a/src/proc.cpp +++ b/src/proc.cpp @@ -476,7 +476,7 @@ static void format_job_info(const job_t *j, job_status_t status) { if (cur_term) { tputs(clr_eol, 1, &writeb); } else { - fwprintf(stdout, L"\e[K"); + fwprintf(stdout, L"\x1B[K"); } fwprintf(stdout, L"\n"); } @@ -582,7 +582,7 @@ static int process_clean_after_marking(bool allow_interactive) { if (cur_term != NULL) { tputs(clr_eol, 1, &writeb); } else { - fwprintf(stdout, L"\e[K"); // no term set up - do clr_eol manually + fwprintf(stdout, L"\x1B[K"); // no term set up - do clr_eol manually } fwprintf(stdout, L"\n"); } diff --git a/src/reader.cpp b/src/reader.cpp index b2ec3c980..e3154a29c 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -687,7 +687,7 @@ void reader_write_title(const wcstring &cmd, bool reset_cursor_position) { proc_push_interactive(0); if (exec_subshell(fish_title_command, lst, false /* ignore exit status */) != -1 && !lst.empty()) { - fputws(L"\e]0;", stdout); + fputws(L"\x1B]0;", stdout); for (size_t i = 0; i < lst.size(); i++) { fputws(lst.at(i).c_str(), stdout); } @@ -2687,7 +2687,7 @@ const wchar_t *reader_readline(int nchars) { break; } // Escape was pressed. - case L'\e': { + case L'\x1B': { if (data->search_mode != history_search_mode_t::none) { data->search_mode = history_search_mode_t::none; diff --git a/src/screen.cpp b/src/screen.cpp index da5c2803d..3750ac818 100644 --- a/src/screen.cpp +++ b/src/screen.cpp @@ -125,7 +125,7 @@ static bool is_screen_name_escape_seq(const wchar_t *code, size_t *resulting_len } #endif - const wchar_t *const screen_name_end_sentinel = L"\e\\"; + const wchar_t *const screen_name_end_sentinel = L"\x1B\\"; const wchar_t *screen_name_end = wcsstr(&code[2], screen_name_end_sentinel); if (screen_name_end == NULL) { // Consider just k to be the code. @@ -146,7 +146,7 @@ static bool is_iterm2_escape_seq(const wchar_t *code, size_t *resulting_length) size_t cursor = 2; for (; code[cursor] != L'\0'; cursor++) { // Consume a sequence of characters up to \ or . - if (code[cursor] == '\x07' || (code[cursor] == '\\' && code[cursor - 1] == '\e')) { + if (code[cursor] == '\x07' || (code[cursor] == '\\' && code[cursor - 1] == '\x1B')) { found = true; break; } @@ -261,11 +261,11 @@ static bool is_visual_escape_seq(const wchar_t *code, size_t *resulting_length) } /// Returns the number of characters in the escape code starting at 'code'. We only handle sequences -/// that begin with \e. If it doesn't we return zero. We also return zero if we don't recognize the +/// that begin with \x1B. If it doesn't we return zero. We also return zero if we don't recognize the /// escape sequence based on querying terminfo and other heuristics. size_t escape_code_length(const wchar_t *code) { assert(code != NULL); - if (*code != L'\e') return 0; + if (*code != L'\x1B') return 0; size_t esc_seq_len = cached_layouts.find_escape_code(code); if (esc_seq_len) return esc_seq_len; @@ -313,7 +313,7 @@ static prompt_layout_t calc_prompt_layout(const wcstring &prompt, layout_cache_t size_t current_line_width = 0; for (int j = 0; prompt[j]; j++) { - if (prompt[j] == L'\e') { + if (prompt[j] == L'\x1B') { // This is the start of an escape code. Skip over it if it's at least one char long. size_t len = escape_code_length(&prompt[j]); if (len > 0) j += len - 1;