From 8a522c6cab56bd55f1454638786f999f6f636354 Mon Sep 17 00:00:00 2001 From: Philippe Waroquiers Date: Sat, 9 Mar 2019 23:55:44 +0100 Subject: Have 'thread|frame apply' style their output. 'thread|frame apply CMD' launches CMD so that CMD output goes to a string_file. This patch ensures that string_file for such CMD output contains style escape sequences that 'thread|frame apply' will later on output on the real terminal, so as to have CMD output properly styled. The idea is to have the class ui_file having overridable methods to indicate that the output to this ui_file should be done using 'terminal' behaviour such as styling. Then these methods are overriden in string_file so that a specially constructed string_file will get output with style escape sequences. After this patch, the output of CMD by thread|frame apply CMD is styled similarly as when CMD is launched directly. Note that string_file (term_out true) could also support wrapping, but this is not done (yet?). Tested on debian/amd64. gdb/ChangeLog 2019-04-27 Philippe Waroquiers Support style in 'frame|thread apply' * gdbcmd.h (execute_command_to_string): New term_out parameter. * record.c (record_start, record_stop): Update callers of execute_command_to_string with false. * ui-file.h (class ui_file): New term_out and can_emit_style_escape methods. (class string_file): New constructor with term_out parameter. Override methods term_out and can_emit_style_escape. New member term_out. (class stdio_file): Override can_emit_style_escape. (class tee_file): Override term_out and can_emit_style_escape. * utils.h (can_emit_style_escape): Remove. * utils.c (can_emit_style_escape): Likewise. Update all callers of can_emit_style_escape (SOMESTREAM) to SOMESTREAM->can_emit_style_escape. * source-cache.c (source_cache::get_source_lines): Likewise. * stack.c (frame_apply_command_count): Call execute_command_to_string passing the term_out characteristic of the current gdb_stdout. * thread.c (thr_try_catch_cmd): Likewise. * top.c (execute_command_to_string): pass term_out parameter to construct the string_file for the command output. * ui-file.c (term_cli_styling): New function (most code moved from utils.c can_emit_style_escape). (string_file::string_file, string_file::can_emit_style_escape, stdio_file::can_emit_style_escape, tee_file::term_out, tee_file::can_emit_style_escape): New functions. --- gdb/ui-file.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) (limited to 'gdb/ui-file.c') diff --git a/gdb/ui-file.c b/gdb/ui-file.c index 77f6b31..4139b5d 100644 --- a/gdb/ui-file.c +++ b/gdb/ui-file.c @@ -101,6 +101,33 @@ ui_file_isatty (struct ui_file *file) return file->isatty (); } +/* true if the gdb terminal supports styling, and styling is enabled. */ + +static bool +term_cli_styling () +{ + extern int cli_styling; + + if (!cli_styling) + return false; + + const char *term = getenv ("TERM"); + /* Windows doesn't by default define $TERM, but can support styles + regardless. */ +#ifndef _WIN32 + if (term == nullptr || !strcmp (term, "dumb")) + return false; +#else + /* But if they do define $TERM, let us behave the same as on Posix + platforms, for the benefit of programs which invoke GDB as their + back-end. */ + if (term && !strcmp (term, "dumb")) + return false; +#endif + return true; +} + + void ui_file_write (struct ui_file *file, const char *buf, @@ -140,6 +167,22 @@ string_file::write (const char *buf, long length_buf) m_string.append (buf, length_buf); } +/* See ui-file.h. */ + +bool +string_file::term_out () +{ + return m_term_out; +} + +/* See ui-file.h. */ + +bool +string_file::can_emit_style_escape () +{ + return m_term_out && term_cli_styling (); +} + stdio_file::stdio_file (FILE *file, bool close_p) @@ -255,6 +298,16 @@ stdio_file::isatty () return ::isatty (m_fd); } +/* See ui-file.h. */ + +bool +stdio_file::can_emit_style_escape () +{ + return (this == gdb_stdout + && this->isatty () + && term_cli_styling ()); +} + /* This is the implementation of ui_file method 'write' for stderr. @@ -332,3 +385,21 @@ tee_file::isatty () { return m_one->isatty (); } + +/* See ui-file.h. */ + +bool +tee_file::term_out () +{ + return m_one->term_out (); +} + +/* See ui-file.h. */ + +bool +tee_file::can_emit_style_escape () +{ + return (this == gdb_stdout + && m_one->term_out () + && term_cli_styling ()); +} -- cgit v1.1