diff options
Diffstat (limited to 'gdb/ui-style.c')
-rw-r--r-- | gdb/ui-style.c | 62 |
1 files changed, 36 insertions, 26 deletions
diff --git a/gdb/ui-style.c b/gdb/ui-style.c index f0a8e81..c8f2e11 100644 --- a/gdb/ui-style.c +++ b/gdb/ui-style.c @@ -1,5 +1,5 @@ /* Styling for ui_file - Copyright (C) 2018-2024 Free Software Foundation, Inc. + Copyright (C) 2018-2025 Free Software Foundation, Inc. This file is part of GDB. @@ -21,12 +21,14 @@ #include "gdbsupport/gdb_regex.h" /* A regular expression that is used for matching ANSI terminal escape - sequences. */ + sequences. Note that this will actually match any prefix of such a + sequence. This property is used so that other code can buffer + incomplete sequences as needed. */ static const char ansi_regex_text[] = - /* Introduction. */ - "^\033\\[" -#define DATA_SUBEXP 1 + /* Introduction. Only the escape character is truly required. */ + "^\033(\\[" +#define DATA_SUBEXP 2 /* Capture parameter and intermediate bytes. */ "(" /* Parameter bytes. */ @@ -36,16 +38,17 @@ static const char ansi_regex_text[] = /* End the first capture. */ ")" /* The final byte. */ -#define FINAL_SUBEXP 2 - "([\x40-\x7e])"; +#define FINAL_SUBEXP 3 + "([\x40-\x7e]))?"; /* The number of subexpressions to allocate space for, including the "0th" whole match subexpression. */ -#define NUM_SUBEXPRESSIONS 3 +#define NUM_SUBEXPRESSIONS 4 /* The compiled form of ansi_regex_text. */ -static regex_t ansi_regex; +static compiled_regex ansi_regex (ansi_regex_text, REG_EXTENDED, + _("Error in ANSI terminal escape sequences regex")); /* This maps 8-color palette to RGB triples. The values come from plain linux terminal. */ @@ -364,12 +367,21 @@ ui_file_style::parse (const char *buf, size_t *n_read) { regmatch_t subexps[NUM_SUBEXPRESSIONS]; - int match = regexec (&ansi_regex, buf, ARRAY_SIZE (subexps), subexps, 0); + int match = ansi_regex.exec (buf, ARRAY_SIZE (subexps), subexps, 0); if (match == REG_NOMATCH) { *n_read = 0; return false; } + + /* If the final subexpression did not match, then that means there + was an incomplete sequence. These are ignored here. */ + if (subexps[FINAL_SUBEXP].rm_so == -1) + { + *n_read = 0; + return false; + } + /* Other failures mean the regexp is broken. */ gdb_assert (match == 0); /* The regexp is anchored. */ @@ -526,27 +538,25 @@ ui_file_style::parse (const char *buf, size_t *n_read) /* See ui-style.h. */ -bool -skip_ansi_escape (const char *buf, int *n_read) +ansi_escape_result +examine_ansi_escape (const char *buf, int *n_read) { + gdb_assert (*buf == '\033'); + regmatch_t subexps[NUM_SUBEXPRESSIONS]; - int match = regexec (&ansi_regex, buf, ARRAY_SIZE (subexps), subexps, 0); - if (match == REG_NOMATCH || buf[subexps[FINAL_SUBEXP].rm_so] != 'm') - return false; + int match = ansi_regex.exec (buf, ARRAY_SIZE (subexps), subexps, 0); + if (match == REG_NOMATCH) + return ansi_escape_result::NO_MATCH; - *n_read = subexps[FINAL_SUBEXP].rm_eo; - return true; -} + if (subexps[FINAL_SUBEXP].rm_so == -1) + return ansi_escape_result::INCOMPLETE; -void _initialize_ui_style (); -void -_initialize_ui_style () -{ - int code = regcomp (&ansi_regex, ansi_regex_text, REG_EXTENDED); - /* If the regular expression was incorrect, it was a programming - error. */ - gdb_assert (code == 0); + if (buf[subexps[FINAL_SUBEXP].rm_so] != 'm') + return ansi_escape_result::NO_MATCH; + + *n_read = subexps[FINAL_SUBEXP].rm_eo; + return ansi_escape_result::MATCHED; } /* See ui-style.h. */ |