From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id HXOTH6dFz2GUegAAWB0awg (envelope-from ) for ; Fri, 31 Dec 2021 13:02:15 -0500 Received: by simark.ca (Postfix, from userid 112) id 6EF891F0D7; Fri, 31 Dec 2021 13:02:15 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 0E6401ECEB for ; Fri, 31 Dec 2021 13:02:14 -0500 (EST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 63448385802F for ; Fri, 31 Dec 2021 18:02:13 +0000 (GMT) Received: from outbound-ss-820.bluehost.com (outbound-ss-820.bluehost.com [69.89.24.241]) by sourceware.org (Postfix) with ESMTPS id B51873858D39 for ; Fri, 31 Dec 2021 18:02:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B51873858D39 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tromey.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tromey.com Received: from cmgw13.mail.unifiedlayer.com (unknown [10.0.90.128]) by progateway2.mail.pro1.eigbox.com (Postfix) with ESMTP id DBDFE10043153 for ; Fri, 31 Dec 2021 18:02:00 +0000 (UTC) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with ESMTP id 3ME4nxRgnEaNC3ME4npNo4; Fri, 31 Dec 2021 18:02:00 +0000 X-Authority-Reason: nr=8 X-Authority-Analysis: v=2.4 cv=dJtjJMVb c=1 sm=1 tr=0 ts=61cf4598 a=ApxJNpeYhEAb1aAlGBBbmA==:117 a=ApxJNpeYhEAb1aAlGBBbmA==:17 a=dLZJa+xiwSxG16/P+YVxDGlgEgI=:19 a=IOMw9HtfNCkA:10:nop_rcvd_month_year a=Qbun_eYptAEA:10:endurance_base64_authed_username_1 a=-jz7mdv8sTmdR3wcw9cA:9 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=Content-Transfer-Encoding:MIME-Version:Message-Id:Date:Subject: Cc:To:From:Sender:Reply-To:Content-Type:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=RqFlSBNzL9TMssCHxk3jVeHR0oXPPNLfol+QrY6Z6Ys=; b=tdjUWZhoUmEtQ3CFQ7+rx9wBFW jSu4/tER+8B4aUuyTQkpt6sEnhyCljVhgSHZBB9gDdYX+G5hGF1Mj5ejybBiAnkXQGPnLmv8Dzt6B Bxduj0AIBtIl/7imq4i40q4RG; Received: from 75-166-134-30.hlrn.qwest.net ([75.166.134.30]:52040 helo=localhost.localdomain) by box5379.bluehost.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1n3ME3-003o57-UM; Fri, 31 Dec 2021 11:02:00 -0700 From: Tom Tromey To: gdb-patches@sourceware.org Subject: [PATCH] Implement putstr and putstrn in ui_file Date: Fri, 31 Dec 2021 11:01:57 -0700 Message-Id: <20211231180157.946230-1-tom@tromey.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - box5379.bluehost.com X-AntiAbuse: Original Domain - sourceware.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tromey.com X-BWhitelist: no X-Source-IP: 75.166.134.30 X-Source-L: No X-Exim-ID: 1n3ME3-003o57-UM X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 75-166-134-30.hlrn.qwest.net (localhost.localdomain) [75.166.134.30]:52040 X-Source-Auth: tom+tromey.com X-Email-Count: 1 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Tom Tromey Errors-To: gdb-patches-bounces+public-inbox=simark.ca@sourceware.org Sender: "Gdb-patches" 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) /* 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); +} + 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_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; } -/* 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); -} - /* 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