From patchwork Fri Jun 9 09:18:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom de Vries X-Patchwork-Id: 70816 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 76196385661C for ; Fri, 9 Jun 2023 09:19:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 76196385661C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1686302348; bh=COenok1TSaoTlREgx7xUdF+wtME8cSUouhJ10VU/PCw=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=ZAuLPF9mvitOrLnE4j/2C4rD9/a1tzBYMB9fOTovABME1rM7CdmHkwx4Q6pjUZaO3 5v8/ZsFz7ZzomdYsu8FrUQgYc/j6ucAxiJEHjrcK5K94llla7Uph4x4oOMU7CcxcGm nkhn58vnxcHQPChk1okXgBwK72noFZCCrUpPeF00= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by sourceware.org (Postfix) with ESMTPS id 31DED3858D35 for ; Fri, 9 Jun 2023 09:18:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 31DED3858D35 Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 5DB4C1FDF9; Fri, 9 Jun 2023 09:18:41 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 4769D139C8; Fri, 9 Jun 2023 09:18:41 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id b+xoEHHugmStdQAAMHmgww (envelope-from ); Fri, 09 Jun 2023 09:18:41 +0000 To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH v2 1/2] [gdb/tui] Simplify tui_puts_internal Date: Fri, 9 Jun 2023 11:18:49 +0200 Message-Id: <20230609091850.21301-1-tdevries@suse.de> X-Mailer: git-send-email 2.35.3 MIME-Version: 1.0 X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org 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: , X-Patchwork-Original-From: Tom de Vries via Gdb-patches From: Tom de Vries Reply-To: Tom de Vries Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" Simplify tui_puts_internal by using continue, as per this [1] coding standard rule, making the function more readable and easier to understand. No functional changes. Tested on x86_64-linux. [1] https://llvm.org/docs/CodingStandards.html#use-early-exits-and-continue-to-simplify-code Reviewed-By: Tom Tromey --- gdb/tui/tui-io.c | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) base-commit: 30711c89cc7dcd2bd4ea772b2f5dc639c5b1cfcc diff --git a/gdb/tui/tui-io.c b/gdb/tui/tui-io.c index 908cb834e4c..8cb68d12408 100644 --- a/gdb/tui/tui-io.c +++ b/gdb/tui/tui-io.c @@ -523,36 +523,37 @@ tui_puts_internal (WINDOW *w, const char *string, int *height) while ((c = *string++) != 0) { - if (c == '\n') - saw_nl = true; - if (c == '\1' || c == '\2') { /* Ignore these, they are readline escape-marking sequences. */ + continue; } - else + + if (c == '\033') { - if (c == '\033') + size_t bytes_read = apply_ansi_escape (w, string - 1); + if (bytes_read > 0) { - size_t bytes_read = apply_ansi_escape (w, string - 1); - if (bytes_read > 0) - { - string = string + bytes_read - 1; - continue; - } + string = string + bytes_read - 1; + continue; } - do_tui_putc (w, c); + } - if (height != nullptr) - { - int col = getcurx (w); - if (col <= prev_col) - ++*height; - prev_col = col; - } + if (c == '\n') + saw_nl = true; + + do_tui_putc (w, c); + + if (height != nullptr) + { + int col = getcurx (w); + if (col <= prev_col) + ++*height; + prev_col = col; } } + if (TUI_CMD_WIN != nullptr && w == TUI_CMD_WIN->handle.get ()) update_cmdwin_start_line (); if (saw_nl) From patchwork Fri Jun 9 09:18:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Tom de Vries X-Patchwork-Id: 70817 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2B3223856DE7 for ; Fri, 9 Jun 2023 09:19:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2B3223856DE7 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1686302350; bh=MqFVGS01eOX0MHzDU1IYx563Dxox9OM61fTBGquDFd8=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=nkHG+Q0XXSt+QyNteuA8PyjowEE6t1XcvJeHWgXyOCkv0SVhqClqVgq9ug+WzczrE +Zfc3GLnIo2ZvnXjY4JGXjX03AuE3N3r8+omFCMZwU9kHUccKljRLGxeApE3QZy8p1 fYl/pYD1w+ZD5EQom+T8muLx1R7O6MaGTeuyS11A= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by sourceware.org (Postfix) with ESMTPS id 4B8973858D3C for ; Fri, 9 Jun 2023 09:18:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4B8973858D3C Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 75D651FDFB; Fri, 9 Jun 2023 09:18:41 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 60D8613A61; Fri, 9 Jun 2023 09:18:41 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 8MO3FnHugmStdQAAMHmgww (envelope-from ); Fri, 09 Jun 2023 09:18:41 +0000 To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH v2 2/2] [gdb/tui] Handle unicode chars in prompt Date: Fri, 9 Jun 2023 11:18:50 +0200 Message-Id: <20230609091850.21301-2-tdevries@suse.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230609091850.21301-1-tdevries@suse.de> References: <20230609091850.21301-1-tdevries@suse.de> MIME-Version: 1.0 X-Spam-Status: No, score=-12.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org 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: , X-Patchwork-Original-From: Tom de Vries via Gdb-patches From: Tom de Vries Reply-To: Tom de Vries Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" Let's try to set the prompt using a unicode character, say '❯', aka U+276F (heavy right-pointing angle quotation mark ornament). This works fine on an xterm with CLI (with X marking the position of the blinking cursor): ... $ gdb -q -ex "set prompt GDB❯ " GDB❯ X ... but with TUI: ... $ gdb -q -tui -ex "set prompt GDB❯ " ... we get instead: ... GDB GDB X ... We can use the test-case gdb.tui/unicode-prompt.exp to get more details, using tuiterm. With Term::dump_screen we have: ... 16 (gdb) set prompt GDB❯ 17 GDB❯ GDB❯ GDB❯ set prompt (gdb) 18 (gdb) ... and with Term::dump_screen_with_attrs (summarizing using attribute sets and ): ... 16 (gdb) set prompt GDB❯ 17 GDB GDB GDB set prompt (gdb) 18 (gdb) ... where: ... == == ... This explains why we didn't see the unicode char on xterm: it's hidden because the invisible attribute is set. So, there seem to be two problems: - the attributes are incorrect, and - the prompt is repeated a couple of times. In TUI, the prompt is written out by tui_puts_internal, which outputs one byte at a time using waddch, which apparantly breaks multi-byte char support. Fix this by detecting multi-byte chars in tui_puts_internal, and printing them using waddnstr. Tested on x86_64-linux. Reported-By: wuzy01@qq.com PR tui/28800 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=28800 --- gdb/testsuite/gdb.tui/unicode-prompt.exp | 43 +++++++++ gdb/tui/tui-io.c | 106 +++++++++++++++++++---- 2 files changed, 134 insertions(+), 15 deletions(-) create mode 100644 gdb/testsuite/gdb.tui/unicode-prompt.exp diff --git a/gdb/testsuite/gdb.tui/unicode-prompt.exp b/gdb/testsuite/gdb.tui/unicode-prompt.exp new file mode 100644 index 00000000000..84ac33d71bf --- /dev/null +++ b/gdb/testsuite/gdb.tui/unicode-prompt.exp @@ -0,0 +1,43 @@ +# Copyright 2023 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +require allow_tui_tests + +tuiterm_env + +save_vars { env(LC_ALL) } { + # Override "C" settings from default_gdb_init. + setenv LC_ALL "C.UTF-8" + + Term::clean_restart 24 80 + + if {![Term::enter_tui]} { + unsupported "TUI not supported" + return + } + + set unicode_char "\u276F" + + set prompt "GDB$unicode_char " + set prompt_re [string_to_regexp $prompt] + + # Set new prompt. + send_gdb "set prompt $prompt\n" + # Set old prompt back. + send_gdb "set prompt (gdb) \n" + + gdb_assert { [Term::wait_for "^${prompt_re}set prompt $gdb_prompt "] } \ + "prompt with unicode char" +} diff --git a/gdb/tui/tui-io.c b/gdb/tui/tui-io.c index 8cb68d12408..75ad20a74d1 100644 --- a/gdb/tui/tui-io.c +++ b/gdb/tui/tui-io.c @@ -514,6 +514,55 @@ tui_puts (const char *string, WINDOW *w) update_cmdwin_start_line (); } +/* Use HAVE_BTOWC as sign that we have functioning wchar_t support. See also + gdb_wchar.h. */ + +#ifdef HAVE_BTOWC +/* Return true if STRING starts with a multi-byte char. Return the length of + the multi-byte char in LEN, or 0 in case it's a multi-byte null char. + Implementation based on _rl_read_mbchar. */ + +static bool +is_mb_char (const char *string, int &len) +{ + for (len = 1; len <= MB_CUR_MAX; len++) + { + size_t res; + + { + mbstate_t ps; + memset (&ps, 0, sizeof (mbstate_t)); + res = mbrtowc (nullptr, string, len, &ps); + } + + if (res == (size_t)(-1)) + { + /* Not a multi-byte char. */ + return false; + } + + if (res == (size_t)(-2)) + { + /* Part of a multi-byte char. */ + continue; + } + + if (res == 0) + { + /* Multi-byte null char. */ + len = 0; + return true; + } + + /* Complete multi-byte char. */ + gdb_assert (res == len); + return true; + } + + return false; +} +#endif + static void tui_puts_internal (WINDOW *w, const char *string, int *height) { @@ -521,29 +570,56 @@ tui_puts_internal (WINDOW *w, const char *string, int *height) int prev_col = 0; bool saw_nl = false; - while ((c = *string++) != 0) + while (true) { - if (c == '\1' || c == '\2') - { - /* Ignore these, they are readline escape-marking - sequences. */ - continue; - } + bool handled = false; - if (c == '\033') +#ifdef HAVE_BTOWC + { + int mb_len; + if (is_mb_char (string, mb_len) && mb_len != 1) + { + if (mb_len == 0) + { + /* Multi-byte null char. */ + break; + } + + waddnstr (w, string, mb_len); + string += mb_len; + handled = true; + } + } +#endif + + if (!handled) { - size_t bytes_read = apply_ansi_escape (w, string - 1); - if (bytes_read > 0) + c = *string++; + if (c == '\0') + break; + + if (c == '\1' || c == '\2') { - string = string + bytes_read - 1; + /* Ignore these, they are readline escape-marking + sequences. */ continue; } - } - if (c == '\n') - saw_nl = true; + if (c == '\033') + { + size_t bytes_read = apply_ansi_escape (w, string - 1); + if (bytes_read > 0) + { + string = string + bytes_read - 1; + continue; + } + } + + if (c == '\n') + saw_nl = true; - do_tui_putc (w, c); + do_tui_putc (w, c); + } if (height != nullptr) {