public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Andrew Burgess <aburgess@redhat.com>
To: binutils@sourceware.org
Cc: Andrew Burgess <aburgess@redhat.com>
Subject: [PATCH 2/3] objdump: introduce OBJDUMP_COLORS environment variable
Date: Wed, 10 Aug 2022 15:24:38 +0100	[thread overview]
Message-ID: <f7417b830a2f086bcc932be8a283184b4e056643.1660139020.git.aburgess@redhat.com> (raw)
In-Reply-To: <cover.1660139020.git.aburgess@redhat.com>

Add OBJDUMP_COLORS environment variable, and allow this to control the
default behaviour of objdump when --disassembler-color is not used,
and we are disassembling to a terminal.

After this commit:

  commit a88c79b77036e4778e70d62081c3cfd1044bb8e3
  Date:   Tue Aug 9 14:57:48 2022 +0100

      Default to enabling colored disassembly if output is to a terminal.

If objdump is disassembling to a terminal, and the
--disassembler-color argument is not used, then objdump will apply
styling by default as if --disassembler-color=color was used.

However, some users might not like this color on by default approach,
or might prefer to use --disassembler-color=extended-color by default.

With this commit objdump will use the OBJDUMP_COLORS environment
variable to control its behaviour.

With:

  OBJDUMP_COLORS=off

The default behaviour is --disassembler-color=off, that is no colors
are added unless the user explicitly uses --disassembler-color at the
command line.

With:

  OBJDUMP_COLORS=color

Only the basic colors are used, as if --disassembler-color=color was
specified.  This can be overridden by the user at the command line by
explicitly passing --disassembler-color again.

With:

  OBJDUMP_COLORS=extended

Only the extended 256-colors are used, as if
--disassembler-color=extended-color was specified.  This can be
overridden by the user at the command line by explicitly passing
--disassembler-color again.

	* doc/binutils.texi (objdump): Add an @xref to the
	--disassembler-color description.  Add a new section describing
	how to use OBJDUMP_COLORS environment variable.
	* objdump.c (objdump_colors_var): New global.
	(objdump_default_disassembler_color_mode): New function.
	(main): Use objdump_default_disassembler_color_mode.
---
 binutils/doc/binutils.texi | 22 ++++++++++++++++++++++
 binutils/objdump.c         | 31 ++++++++++++++++++++++++++++++-
 2 files changed, 52 insertions(+), 1 deletion(-)

diff --git a/binutils/doc/binutils.texi b/binutils/doc/binutils.texi
index 41f38f479f6..6606d2c51ac 100644
--- a/binutils/doc/binutils.texi
+++ b/binutils/doc/binutils.texi
@@ -2830,6 +2830,8 @@
 
 If this option is not specified then the default is to enable color
 output if displaying to a terminal, but not otherwise.
+@xref{Customizing Objdump's Colors}, for more information on
+controlling the colors objdump uses in its disassembler output.
 
 @item -W[lLiaprmfFsoORtUuTgAckK]
 @itemx --dwarf[=rawline,=decodedline,=info,=abbrev,=pubnames,=aranges,=macro,=frames,=frames-interp,=str,=str-offsets,=loc,=Ranges,=pubtypes,=trace_info,=trace_abbrev,=trace_aranges,=gdb_index,=addr,=cu_index,=links,=follow-links]
@@ -3009,6 +3011,26 @@
 any other data.
 @end table
 
+@anchor{Customizing Objdump's Colors}
+@section Customizing Objdump's Colors
+
+The environment variable @code{OBJDUMP_COLORS} can be used to control
+the colors objdump uses for it's disassembler output.
+
+When objdump disassembles to a terminal, and the
+@option{--disassembler-color} argument is not used, objdump will, by
+default, behave as if @option{--disassembler-color=color} had been
+used.
+
+If @code{OBJDUMP_COLORS} is set to @code{off} then objdump will behave
+as if @option{--disassembler-color=off} was given by default.
+
+If @code{OBJDUMP_COLORS} is set to @code{color} then objdump will
+behave as if @option{--disassembler-color=color} was given by default.
+
+If @code{OBJDUMP_COLORS} is set to @code{extended} then objdump will
+behave as if @option{--disassembler-color=extended-color} was given by
+default.
 @c man end
 
 @ignore
diff --git a/binutils/objdump.c b/binutils/objdump.c
index 9259c76c716..1231cda3657 100644
--- a/binutils/objdump.c
+++ b/binutils/objdump.c
@@ -139,6 +139,9 @@ static enum color_selection
     extended				/* --disassembler-color=extended-color.  */
   } disassembler_color = on_if_terminal_output;
 
+/* The environment variable to read for color information.  */
+static const char *objdump_colors_var = "OBJDUMP_COLORS";
+
 static int dump_any_debugging;
 static int demangle_flags = DMGL_ANSI | DMGL_PARAMS;
 
@@ -2155,6 +2158,32 @@ objdump_sprintf (SFILE *f, const char *format, ...)
   return n;
 }
 
+/* Figure out a default disassembler color mode.  */
+
+static enum color_selection
+objdump_default_disassembler_color_mode (void)
+{
+  enum color_selection mode;
+
+  if (isatty (1))
+    {
+      const char *tmp = getenv (objdump_colors_var);
+
+      if (tmp == NULL || strncmp (tmp, "color", 5) == 0)
+	mode = on;
+      if (strncmp (tmp, "extended", 8) == 0)
+	mode = extended;
+      else if (strncmp (tmp, "off", 3) == 0)
+	mode = off;
+      else
+	mode = on;
+    }
+  else
+    mode = off;
+
+  return mode;
+}
+
 /* Return an integer greater than, or equal to zero, representing the color
    for STYLE, or -1 if no color should be used.  */
 
@@ -5931,7 +5960,7 @@ main (int argc, char **argv)
     }
 
   if (disassembler_color == on_if_terminal_output)
-    disassembler_color = isatty (1) ? on : off;
+    disassembler_color = objdump_default_disassembler_color_mode ();
 
   if (show_version)
     print_version ("objdump");
-- 
2.25.4


  parent reply	other threads:[~2022-08-10 14:24 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-10 14:24 [PATCH 0/3] Disassembler styling, bug fixes and customisation Andrew Burgess
2022-08-10 14:24 ` [PATCH 1/3] objdump: fix extended (256) disassembler colors Andrew Burgess
2022-08-10 15:32   ` Nick Clifton
2022-08-10 16:12     ` Andrew Burgess
2022-08-10 14:24 ` Andrew Burgess [this message]
2022-08-10 15:48   ` [PATCH 2/3] objdump: introduce OBJDUMP_COLORS environment variable Nick Clifton
2022-08-10 14:24 ` [PATCH 3/3] objdump: allow the disassembler colors to be customized Andrew Burgess
2022-08-10 15:57   ` Nick Clifton

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=f7417b830a2f086bcc932be8a283184b4e056643.1660139020.git.aburgess@redhat.com \
    --to=aburgess@redhat.com \
    --cc=binutils@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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).