From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH] Implement putstr and putstrn in ui_file
Date: Fri, 31 Dec 2021 11:01:57 -0700 [thread overview]
Message-ID: <20211231180157.946230-1-tom@tromey.com> (raw)
In my tour of the ui_file subsystem, I found that fputstr and fputstrn
can be simplified. The _filtered forms are never used (and IMO
unlikely to ever be used) and so can be removed. And, the interface
can be simplified by removing a callback function and moving the
implementation directly to ui_file.
Regression tested on x86-64 Fedora 34.
---
gdb/guile/scm-ports.c | 2 +-
gdb/mi/mi-console.c | 20 ++-------
gdb/mi/mi-main.c | 2 +-
gdb/mi/mi-out.c | 2 +-
gdb/ui-file.c | 69 ++++++++++++++++++++++++++++--
gdb/ui-file.h | 21 +++++++++-
gdb/utils.c | 97 -------------------------------------------
gdb/utils.h | 15 -------
8 files changed, 93 insertions(+), 135 deletions(-)
diff --git a/gdb/guile/scm-ports.c b/gdb/guile/scm-ports.c
index 41ccf30202d..2d3f36bee36 100644
--- a/gdb/guile/scm-ports.c
+++ b/gdb/guile/scm-ports.c
@@ -191,7 +191,7 @@ ioscm_open_port (scm_t_port_type *port_type, long mode_bits, scm_t_bits stream)
\f
/* Support for connecting Guile's stdio ports to GDB's stdio ports. */
-/* Like fputstrn_filtered, but don't escape characters, except nul.
+/* Print a string, but don't escape characters, except nul.
Also like fputs_filtered, but a length is specified. */
static void
diff --git a/gdb/mi/mi-console.c b/gdb/mi/mi-console.c
index be0129d3a88..10e496e1e14 100644
--- a/gdb/mi/mi-console.c
+++ b/gdb/mi/mi-console.c
@@ -48,16 +48,6 @@ mi_console_file::write (const char *buf, long length_buf)
this->flush ();
}
-/* Write C to STREAM's in an async-safe way. */
-
-static int
-do_fputc_async_safe (int c, ui_file *stream)
-{
- char ch = c;
- stream->write_async_safe (&ch, 1);
- return c;
-}
-
void
mi_console_file::write_async_safe (const char *buf, long length_buf)
{
@@ -65,12 +55,11 @@ mi_console_file::write_async_safe (const char *buf, long length_buf)
if (m_quote)
{
m_raw->write_async_safe (&m_quote, 1);
- fputstrn_unfiltered (buf, length_buf, m_quote, do_fputc_async_safe,
- m_raw);
+ m_raw->putstrn (buf, length_buf, m_quote, true);
m_raw->write_async_safe (&m_quote, 1);
}
else
- fputstrn_unfiltered (buf, length_buf, 0, do_fputc_async_safe, m_raw);
+ m_raw->putstrn (buf, length_buf, 0, true);
char nl = '\n';
m_raw->write_async_safe (&nl, 1);
@@ -91,14 +80,13 @@ mi_console_file::flush ()
if (m_quote)
{
fputc_unfiltered (m_quote, m_raw);
- fputstrn_unfiltered (buf, length_buf, m_quote, fputc_unfiltered,
- m_raw);
+ m_raw->putstrn (buf, length_buf, m_quote);
fputc_unfiltered (m_quote, m_raw);
fputc_unfiltered ('\n', m_raw);
}
else
{
- fputstrn_unfiltered (buf, length_buf, 0, fputc_unfiltered, m_raw);
+ m_raw->putstrn (buf, length_buf, 0);
fputc_unfiltered ('\n', m_raw);
}
gdb_flush (m_raw);
diff --git a/gdb/mi/mi-main.c b/gdb/mi/mi-main.c
index 1f210c161e6..9f1a0c3ddee 100644
--- a/gdb/mi/mi-main.c
+++ b/gdb/mi/mi-main.c
@@ -1866,7 +1866,7 @@ mi_print_exception (const char *token, const struct gdb_exception &exception)
if (exception.message == NULL)
fputs_unfiltered ("unknown error", mi->raw_stdout);
else
- fputstr_unfiltered (exception.what (), '"', mi->raw_stdout);
+ mi->raw_stdout->putstr (exception.what (), '"');
fputs_unfiltered ("\"", mi->raw_stdout);
switch (exception.error)
diff --git a/gdb/mi/mi-out.c b/gdb/mi/mi-out.c
index 0b223476167..af5f4ee007c 100644
--- a/gdb/mi/mi-out.c
+++ b/gdb/mi/mi-out.c
@@ -135,7 +135,7 @@ mi_ui_out::do_field_string (int fldno, int width, ui_align align,
fprintf_unfiltered (stream, "%s=", fldname);
fprintf_unfiltered (stream, "\"");
if (string)
- fputstr_unfiltered (string, '"', stream);
+ stream->putstr (string, '"');
fprintf_unfiltered (stream, "\"");
}
diff --git a/gdb/ui-file.c b/gdb/ui-file.c
index eb1d72bf8b3..f76f71aea03 100644
--- a/gdb/ui-file.c
+++ b/gdb/ui-file.c
@@ -47,13 +47,15 @@ ui_file::printf (const char *format, ...)
void
ui_file::putstr (const char *str, int quoter)
{
- fputstr_unfiltered (str, quoter, this);
+ while (*str)
+ printchar (*str++, quoter, false);
}
void
-ui_file::putstrn (const char *str, int n, int quoter)
+ui_file::putstrn (const char *str, int n, int quoter, bool async_safe)
{
- fputstrn_unfiltered (str, n, quoter, fputc_unfiltered, this);
+ for (int i = 0; i < n; i++)
+ printchar (str[i], quoter, async_safe);
}
int
@@ -68,6 +70,67 @@ ui_file::vprintf (const char *format, va_list args)
vfprintf_unfiltered (this, format, args);
}
+/* See ui-file.h. */
+
+void
+ui_file::printchar (int c, int quoter, bool async_safe)
+{
+ char buf[4];
+ int out = 0;
+
+ c &= 0xFF; /* Avoid sign bit follies */
+
+ if (c < 0x20 || /* Low control chars */
+ (c >= 0x7F && c < 0xA0) || /* DEL, High controls */
+ (sevenbit_strings && c >= 0x80))
+ { /* high order bit set */
+ buf[out++] = '\\';
+
+ switch (c)
+ {
+ case '\n':
+ buf[out++] = 'n';
+ break;
+ case '\b':
+ buf[out++] = 'b';
+ break;
+ case '\t':
+ buf[out++] = 't';
+ break;
+ case '\f':
+ buf[out++] = 'f';
+ break;
+ case '\r':
+ buf[out++] = 'r';
+ break;
+ case '\033':
+ buf[out++] = 'e';
+ break;
+ case '\007':
+ buf[out++] = 'a';
+ break;
+ default:
+ {
+ buf[out++] = '0';
+ buf[out++] = '0';
+ buf[out++] = '0';
+ break;
+ }
+ }
+ }
+ else
+ {
+ if (quoter != 0 && (c == '\\' || c == quoter))
+ buf[out++] = '\\';
+ buf[out++] = c;
+ }
+
+ if (async_safe)
+ this->write_async_safe (buf, out);
+ else
+ this->write (buf, out);
+}
+
\f
void
diff --git a/gdb/ui-file.h b/gdb/ui-file.h
index 0faf84996aa..a4448d8ea9f 100644
--- a/gdb/ui-file.h
+++ b/gdb/ui-file.h
@@ -39,7 +39,10 @@ class ui_file
independent of the language of the program being debugged. */
void putstr (const char *str, int quoter);
- void putstrn (const char *str, int n, int quoter);
+ /* Like putstr, but only print the first N characters. If
+ ASYNC_SAFE is true, then the output is done via the
+ write_async_safe method. */
+ void putstrn (const char *str, int n, int quoter, bool async_safe = false);
int putc (int c);
@@ -96,6 +99,22 @@ class ui_file
default. */
return false;
}
+
+private:
+
+ /* Helper function for putstr and putstrn.
+
+ Print the character C on this stream as part of the contents of a
+ literal string whose delimiter is QUOTER. Note that this routine
+ should only be called for printing things which are independent
+ of the language of the program being debugged.
+
+ printchar will normally escape backslashes and instances of
+ QUOTER. If QUOTER is 0, printchar won't escape backslashes or any
+ quoting character. As a side effect, if you pass the backslash
+ character as the QUOTER, printchar will escape backslashes as
+ usual, but not any other quoting character. */
+ void printchar (int c, int quoter, bool async_safe);
};
typedef std::unique_ptr<ui_file> ui_file_up;
diff --git a/gdb/utils.c b/gdb/utils.c
index f8c898dd502..a28503e4853 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -1129,103 +1129,6 @@ parse_escape (struct gdbarch *gdbarch, const char **string_ptr)
return target_char;
}
\f
-/* Print the character C on STREAM as part of the contents of a literal
- string whose delimiter is QUOTER. Note that this routine should only
- be called for printing things which are independent of the language
- of the program being debugged.
-
- printchar will normally escape backslashes and instances of QUOTER. If
- QUOTER is 0, printchar won't escape backslashes or any quoting character.
- As a side effect, if you pass the backslash character as the QUOTER,
- printchar will escape backslashes as usual, but not any other quoting
- character. */
-
-static void
-printchar (int c, do_fputc_ftype do_fputc, ui_file *stream, int quoter)
-{
- c &= 0xFF; /* Avoid sign bit follies */
-
- if (c < 0x20 || /* Low control chars */
- (c >= 0x7F && c < 0xA0) || /* DEL, High controls */
- (sevenbit_strings && c >= 0x80))
- { /* high order bit set */
- do_fputc ('\\', stream);
-
- switch (c)
- {
- case '\n':
- do_fputc ('n', stream);
- break;
- case '\b':
- do_fputc ('b', stream);
- break;
- case '\t':
- do_fputc ('t', stream);
- break;
- case '\f':
- do_fputc ('f', stream);
- break;
- case '\r':
- do_fputc ('r', stream);
- break;
- case '\033':
- do_fputc ('e', stream);
- break;
- case '\007':
- do_fputc ('a', stream);
- break;
- default:
- {
- do_fputc ('0' + ((c >> 6) & 0x7), stream);
- do_fputc ('0' + ((c >> 3) & 0x7), stream);
- do_fputc ('0' + ((c >> 0) & 0x7), stream);
- break;
- }
- }
- }
- else
- {
- if (quoter != 0 && (c == '\\' || c == quoter))
- do_fputc ('\\', stream);
- do_fputc (c, stream);
- }
-}
-
-/* Print the character C on STREAM as part of the contents of a
- literal string whose delimiter is QUOTER. Note that these routines
- should only be call for printing things which are independent of
- the language of the program being debugged. */
-
-void
-fputstr_filtered (const char *str, int quoter, struct ui_file *stream)
-{
- while (*str)
- printchar (*str++, fputc_filtered, stream, quoter);
-}
-
-void
-fputstr_unfiltered (const char *str, int quoter, struct ui_file *stream)
-{
- while (*str)
- printchar (*str++, fputc_unfiltered, stream, quoter);
-}
-
-void
-fputstrn_filtered (const char *str, int n, int quoter,
- struct ui_file *stream)
-{
- for (int i = 0; i < n; i++)
- printchar (str[i], fputc_filtered, stream, quoter);
-}
-
-void
-fputstrn_unfiltered (const char *str, int n, int quoter,
- do_fputc_ftype do_fputc, struct ui_file *stream)
-{
- for (int i = 0; i < n; i++)
- printchar (str[i], do_fputc, stream, quoter);
-}
-\f
/* Number of lines per page or UINT_MAX if paging is disabled. */
static unsigned int lines_per_page;
diff --git a/gdb/utils.h b/gdb/utils.h
index 54cf090974a..d199e1f0d15 100644
--- a/gdb/utils.h
+++ b/gdb/utils.h
@@ -464,21 +464,6 @@ extern void print_spaces_filtered (int, struct ui_file *);
extern const char *n_spaces (int);
-extern void fputstr_filtered (const char *str, int quotr,
- struct ui_file * stream);
-
-extern void fputstr_unfiltered (const char *str, int quotr,
- struct ui_file * stream);
-
-extern void fputstrn_filtered (const char *str, int n, int quotr,
- struct ui_file * stream);
-
-typedef int (*do_fputc_ftype) (int c, ui_file *stream);
-
-extern void fputstrn_unfiltered (const char *str, int n, int quotr,
- do_fputc_ftype do_fputc,
- struct ui_file * stream);
-
/* Return nonzero if filtered printing is initialized. */
extern int filtered_printing_initialized (void);
--
2.31.1
next reply other threads:[~2021-12-31 18:02 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-12-31 18:01 Tom Tromey [this message]
2021-12-31 19:27 ` Lancelot SIX via Gdb-patches
2022-01-02 23:46 ` Tom Tromey
2022-01-05 17:39 ` Andrew Burgess via Gdb-patches
2022-01-05 18:00 ` Tom Tromey
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20211231180157.946230-1-tom@tromey.com \
--to=tom@tromey.com \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox