[PATCH 2/3] objdump: introduce OBJDUMP_COLORS environment variable

Andrew Burgess aburgess@redhat.com
Wed Aug 10 14:24:38 GMT 2022


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



More information about the Binutils mailing list