From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from omta38.uswest2.a.cloudfilter.net (omta38.uswest2.a.cloudfilter.net [35.89.44.37]) by sourceware.org (Postfix) with ESMTPS id 5EF2438EBEF9 for ; Sat, 16 Sep 2023 00:09:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5EF2438EBEF9 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 eig-obgw-6009a.ext.cloudfilter.net ([10.0.30.184]) by cmsmtp with ESMTP id hIjDqmF8NQFHRhIs5qwUSG; Sat, 16 Sep 2023 00:09:13 +0000 Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with ESMTPS id hIs4q4ZeFOCehhIs5qa9tX; Sat, 16 Sep 2023 00:09:13 +0000 X-Authority-Analysis: v=2.4 cv=feK+dmcF c=1 sm=1 tr=0 ts=6504f229 a=ApxJNpeYhEAb1aAlGBBbmA==:117 a=ApxJNpeYhEAb1aAlGBBbmA==:17 a=OWjo9vPv0XrRhIrVQ50Ab3nP57M=:19 a=dLZJa+xiwSxG16/P+YVxDGlgEgI=:19 a=zNV7Rl7Rt7sA:10 a=Qbun_eYptAEA:10 a=RShPybgiAAAA:8 a=ol_Dc8pcHcha-_7wPPoA:9 a=YLGRqFcrAwIahUk1acIU:22 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=dBMtl9UR29DjmWz1wWClLjPHbOBDAP0BLZCFjrcbwOQ=; b=YZEL6Msh95QMR9WwFW/R+xEs0o Ru1InCtILotDdkQcW2JWykpduue9figGmvjwaBAsgtLYGH27BjOIfMsNIPb+djCp/DSvC+M5rIbkq yCgSS3H5H02eUMyvvzNIouc2N; Received: from [161.98.8.3] (port=56194 helo=prentzel.ci.boulder.co.us) by box5379.bluehost.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1qhIs4-0031pk-0v; Fri, 15 Sep 2023 18:09:12 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH] Support the NO_COLOR environment variable Date: Fri, 15 Sep 2023 18:09:08 -0600 Message-ID: <20230916000908.110459-1-tom@tromey.com> X-Mailer: git-send-email 2.41.0 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: 161.98.8.3 X-Source-L: No X-Exim-ID: 1qhIs4-0031pk-0v X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: (prentzel.ci.boulder.co.us) [161.98.8.3]:56194 X-Source-Auth: tom+tromey.com X-Email-Count: 1 X-Org: HG=bhshared;ORG=bluehost; X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-CMAE-Envelope: MS4xfNZwE0xnrnfquS43ZQpKad4H/ezIPfbis12eFxtyNKCz2v4z0sn8LNGkj/R6s5u/StfekbW11LbSStDw+zXoR7QNb8iZiF+sl1KxbJSMLI3ZIBu5ADWP w5C5rzm5s+nN8ePa2EhJ4d8DA5Gw/7A/z06JGgGqntAIzy30orr0nxR3Q/MFxXH5jRxmb4YAr/tS8/EJG+GQHx8heiexK3G4dCs= X-Spam-Status: No, score=-3024.7 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,GIT_PATCH_0,JMQ_SPF_NEUTRAL,KAM_SHORT,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: I ran across this site: https://no-color.org/ ... which lobbies for tools to recognize the NO_COLOR environment variable and disable any terminal styling when it is seen. This patch implements this for gdb. Regression tested on x86-64 Fedora 38. --- gdb/NEWS | 4 ++++ gdb/doc/gdb.texinfo | 3 +++ gdb/main.c | 4 ++++ gdb/testsuite/gdb.base/early-init-file.exp | 3 ++- .../gdb.base/style-interp-exec-mi.exp | 3 ++- gdb/testsuite/gdb.base/style-logging.exp | 3 ++- gdb/testsuite/gdb.base/style.exp | 9 ++++++--- .../build-id-no-debug-warning.exp | 3 ++- gdb/testsuite/gdb.debuginfod/crc_mismatch.exp | 3 ++- gdb/testsuite/gdb.python/py-format-string.exp | 3 ++- gdb/testsuite/gdb.python/py-source-styling.exp | 3 ++- gdb/testsuite/gdb.python/py-startup-opt.exp | 3 ++- gdb/testsuite/gdb.rust/rust-style.exp | 3 ++- gdb/testsuite/gdb.tui/tui-layout.exp | 18 +++++++++++------- gdb/testsuite/lib/gdb.exp | 2 +- gdb/testsuite/lib/tuiterm.exp | 3 ++- 16 files changed, 49 insertions(+), 21 deletions(-) diff --git a/gdb/NEWS b/gdb/NEWS index 98ff00d5efc..aa5fc9aa494 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -3,6 +3,10 @@ *** Changes since GDB 13 +* GDB now recognizes the NO_COLOR environment variable and disables + styling according to the spec. See https://no-color.org/. + Styling can be re-enabled with "set style enabled on". + * The AArch64 'org.gnu.gdb.aarch64.pauth' Pointer Authentication feature string has been deprecated in favor of the 'org.gnu.gdb.aarch64.pauth_v2' feature string. diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index 9b7e06f3156..2892dd7765c 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -27229,6 +27229,9 @@ and styles can also be disabled entirely. Enable or disable all styling. The default is host-dependent, with most hosts defaulting to @samp{on}. +If the @env{NO_COLOR} environment variable is set to a non-empty +value, then @value{GDBN} will change this to @samp{off} at startup. + @item show style enabled Show the current state of styling. diff --git a/gdb/main.c b/gdb/main.c index cf46f6acb20..97e04f5b5d8 100644 --- a/gdb/main.c +++ b/gdb/main.c @@ -654,6 +654,10 @@ captured_main_1 (struct captured_main_args *context) int save_auto_load; int ret = 1; + const char *no_color = getenv ("NO_COLOR"); + if (no_color != nullptr && *no_color != '\0') + cli_styling = false; + #ifdef HAVE_USEFUL_SBRK /* Set this before constructing scoped_command_stats. */ lim_at_start = (char *) sbrk (0); diff --git a/gdb/testsuite/gdb.base/early-init-file.exp b/gdb/testsuite/gdb.base/early-init-file.exp index 237439f9db7..ff0818b26ba 100644 --- a/gdb/testsuite/gdb.base/early-init-file.exp +++ b/gdb/testsuite/gdb.base/early-init-file.exp @@ -99,9 +99,10 @@ proc check_gdb_startups_up_quietly { message } { } } -save_vars { env(TERM) } { +save_vars { env(TERM) env(NO_COLOR) } { # We need an ANSI-capable terminal to get the output. setenv TERM ansi + setenv NO_COLOR "" # Start GDB and confirm that the version string is styled. save_vars { INTERNAL_GDBFLAGS env(HOME) env(XDG_CONFIG_HOME) } { diff --git a/gdb/testsuite/gdb.base/style-interp-exec-mi.exp b/gdb/testsuite/gdb.base/style-interp-exec-mi.exp index 9b2ed87513f..00cccf7d300 100644 --- a/gdb/testsuite/gdb.base/style-interp-exec-mi.exp +++ b/gdb/testsuite/gdb.base/style-interp-exec-mi.exp @@ -23,9 +23,10 @@ standard_testfile -save_vars { env(TERM) } { +save_vars { env(TERM) env(NO_COLOR) } { # We need an ANSI-capable terminal to get the output. setenv TERM ansi + setenv NO_COLOR "" if { [prepare_for_testing "failed to prepare" \ ${testfile} ${srcfile}] } { diff --git a/gdb/testsuite/gdb.base/style-logging.exp b/gdb/testsuite/gdb.base/style-logging.exp index a156c39146f..f82e714448f 100644 --- a/gdb/testsuite/gdb.base/style-logging.exp +++ b/gdb/testsuite/gdb.base/style-logging.exp @@ -22,9 +22,10 @@ require {!is_remote host} standard_testfile style.c -save_vars { env(TERM) } { +save_vars { env(TERM) env(NO_COLOR) } { # We need an ANSI-capable terminal to get the output. setenv TERM ansi + setenv NO_COLOR "" if {[prepare_for_testing "failed to prepare" $testfile $srcfile debug]} { return -1 diff --git a/gdb/testsuite/gdb.base/style.exp b/gdb/testsuite/gdb.base/style.exp index 60f909e2402..1afce166e93 100644 --- a/gdb/testsuite/gdb.base/style.exp +++ b/gdb/testsuite/gdb.base/style.exp @@ -69,9 +69,10 @@ proc run_style_tests { } { global testfile srcfile hex binfile global currently_disabled_style decimal hex - save_vars { env(TERM) } { + save_vars { env(TERM) env(NO_COLOR) } { # We need an ANSI-capable terminal to get the output. setenv TERM ansi + setenv NO_COLOR "" # Restart GDB with the correct TERM variable setting, this # means that GDB will enable styling. @@ -319,9 +320,10 @@ proc run_style_tests { } { # Python pygments module is not available, so, when we disable # disassembler styling, we should always see a change in output. proc test_disable_disassembler_styling { } { - save_vars { env(TERM) } { + save_vars { env(TERM) env(NO_COLOR) } { # We need an ANSI-capable terminal to get the output. setenv TERM ansi + setenv NO_COLOR "" # Restart GDB with the correct TERM variable setting, this # means that GDB will enable styling. @@ -427,9 +429,10 @@ proc test_disassembler_error_handling { } { return } - save_vars { env(TERM) } { + save_vars { env(TERM) env(NO_COLOR) } { # We need an ANSI-capable terminal to get the output. setenv TERM ansi + setenv NO_COLOR "" # Restart GDB with the correct TERM variable setting, this # means that GDB will enable styling. diff --git a/gdb/testsuite/gdb.debuginfod/build-id-no-debug-warning.exp b/gdb/testsuite/gdb.debuginfod/build-id-no-debug-warning.exp index 148363a7c05..be7041a7520 100644 --- a/gdb/testsuite/gdb.debuginfod/build-id-no-debug-warning.exp +++ b/gdb/testsuite/gdb.debuginfod/build-id-no-debug-warning.exp @@ -98,9 +98,10 @@ gdb_test "file ${build_id_debug_file}" \ # Do the same thing again, but this time check that the styling is # correct. with_test_prefix "check styling" { - save_vars { env(TERM) } { + save_vars { env(TERM) env(NO_COLOR) } { # We need an ANSI-capable terminal to get styled output. setenv TERM ansi + setenv NO_COLOR "" clean_restart diff --git a/gdb/testsuite/gdb.debuginfod/crc_mismatch.exp b/gdb/testsuite/gdb.debuginfod/crc_mismatch.exp index 074028eadfd..51d2073f4ae 100644 --- a/gdb/testsuite/gdb.debuginfod/crc_mismatch.exp +++ b/gdb/testsuite/gdb.debuginfod/crc_mismatch.exp @@ -71,9 +71,10 @@ if {[build_executable crc_mismatch.exp crc_mismatch-2 crc_mismatch-2.c debug] != # Do the same thing again, but this time check that the styling is # correct. with_test_prefix "check styling" { - save_vars { env(TERM) } { + save_vars { env(TERM) env(NO_COLOR) } { # We need an ANSI-capable terminal to get styled output. setenv TERM ansi + setenv NO_COLOR "" clean_restart diff --git a/gdb/testsuite/gdb.python/py-format-string.exp b/gdb/testsuite/gdb.python/py-format-string.exp index 55a37766bff..e9bc1fac097 100644 --- a/gdb/testsuite/gdb.python/py-format-string.exp +++ b/gdb/testsuite/gdb.python/py-format-string.exp @@ -1184,11 +1184,12 @@ with_test_prefix "format_string" { # Perform C Tests. if { [build_inferior "${binfile}" "c"] == 0 } { with_test_prefix "lang_c" { - save_vars { env(TERM) } { + save_vars { env(TERM) env(NO_COLOR) } { # We run all of these tests in an environment where styling # could work, but we only expect the final call to # test_styling to actually produce any styled output. setenv TERM ansi + setenv NO_COLOR "" set current_lang "c" prepare_gdb "${binfile}" test_all_common diff --git a/gdb/testsuite/gdb.python/py-source-styling.exp b/gdb/testsuite/gdb.python/py-source-styling.exp index f92d6f7a706..7ecec185dbd 100644 --- a/gdb/testsuite/gdb.python/py-source-styling.exp +++ b/gdb/testsuite/gdb.python/py-source-styling.exp @@ -20,12 +20,13 @@ load_lib gdb-python.exp standard_testfile -save_vars { env(TERM) } { +save_vars { env(TERM) env(NO_COLOR) } { # We need an ANSI-capable terminal to get the output, additionally # we need to set LC_ALL so GDB knows the terminal is UTF-8 # capable, otherwise we'll get a UnicodeEncodeError trying to # encode the output. setenv TERM ansi + setenv NO_COLOR "" if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } { return -1 diff --git a/gdb/testsuite/gdb.python/py-startup-opt.exp b/gdb/testsuite/gdb.python/py-startup-opt.exp index 08d1c79eee3..ed2ba2d6b64 100644 --- a/gdb/testsuite/gdb.python/py-startup-opt.exp +++ b/gdb/testsuite/gdb.python/py-startup-opt.exp @@ -97,9 +97,10 @@ proc test_python_settings { exp_state } { gdb_exit } -save_vars { env(TERM) } { +save_vars { env(TERM) env(NO_COLOR) } { # We need an ANSI-capable terminal to get the output. setenv TERM ansi + setenv NO_COLOR "" # Check the features are off by default. test_python_settings "off" diff --git a/gdb/testsuite/gdb.rust/rust-style.exp b/gdb/testsuite/gdb.rust/rust-style.exp index 0555ea4b8a4..0d0e78bbfdc 100644 --- a/gdb/testsuite/gdb.rust/rust-style.exp +++ b/gdb/testsuite/gdb.rust/rust-style.exp @@ -19,9 +19,10 @@ load_lib rust-support.exp require allow_rust_tests require {can_compile rust} -save_vars { env(TERM) } { +save_vars { env(TERM) env(NO_COLOR) } { # We need an ANSI-capable terminal to get the output. setenv TERM ansi + setenv NO_COLOR "" standard_testfile .rs if {[prepare_for_testing "failed to prepare" $testfile $srcfile \ diff --git a/gdb/testsuite/gdb.tui/tui-layout.exp b/gdb/testsuite/gdb.tui/tui-layout.exp index 13823387e72..90f27c5eac1 100644 --- a/gdb/testsuite/gdb.tui/tui-layout.exp +++ b/gdb/testsuite/gdb.tui/tui-layout.exp @@ -44,13 +44,17 @@ proc test_layout_or_focus {layout_name terminal execution} { set dumb_terminal [string equal $terminal "dumb"] - if {$dumb_terminal} { - clean_restart $binfile - } else { - Term::clean_restart 24 80 $binfile - if {![Term::prepare_for_tui]} { - unsupported "TUI not supported" - return + global env + save_vars { env(TERM) } { + setenv TERM $terminal + if {$dumb_terminal} { + clean_restart $binfile + } else { + Term::clean_restart 24 80 $binfile + if {![Term::prepare_for_tui]} { + unsupported "TUI not supported" + return + } } } diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp index 1b9179401c4..0a4f74d8000 100644 --- a/gdb/testsuite/lib/gdb.exp +++ b/gdb/testsuite/lib/gdb.exp @@ -6468,7 +6468,7 @@ proc default_gdb_init { test_file_name } { # This disables style output, which would interfere with many # tests. - setenv TERM "dumb" + setenv NO_COLOR sorry # This setting helps detect bugs in the Python code and doesn't # seem to have a significant downside for the tests. diff --git a/gdb/testsuite/lib/tuiterm.exp b/gdb/testsuite/lib/tuiterm.exp index 598c1f8cbdf..854fde56393 100644 --- a/gdb/testsuite/lib/tuiterm.exp +++ b/gdb/testsuite/lib/tuiterm.exp @@ -844,8 +844,9 @@ namespace eval Term { # BODY. proc with_tuiterm {rows cols body} { global env stty_init - save_vars {env(TERM) stty_init} { + save_vars {env(TERM) env(NO_COLOR) stty_init} { setenv TERM ansi + setenv NO_COLOR "" _setup $rows $cols uplevel $body -- 2.41.0